ref_get()/ref_put() use atomic gcc operations if supported, thanks to Thomas Jarosch...
[strongswan.git] / src / libstrongswan / utils.c
1 /*
2 * Copyright (C) 2008 Tobias Brunner
3 * Copyright (C) 2005-2008 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * $Id$
17 */
18
19 #include "utils.h"
20
21 #include <sys/stat.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <dirent.h>
26 #include <time.h>
27
28 #include <enum.h>
29 #include <debug.h>
30
31 ENUM(status_names, SUCCESS, DESTROY_ME,
32 "SUCCESS",
33 "FAILED",
34 "OUT_OF_RES",
35 "ALREADY_DONE",
36 "NOT_SUPPORTED",
37 "INVALID_ARG",
38 "NOT_FOUND",
39 "PARSE_ERROR",
40 "VERIFY_ERROR",
41 "INVALID_STATE",
42 "DESTROY_ME",
43 "NEED_MORE",
44 );
45
46 /**
47 * Described in header.
48 */
49 void *clalloc(void * pointer, size_t size)
50 {
51 void *data;
52 data = malloc(size);
53
54 memcpy(data, pointer, size);
55
56 return (data);
57 }
58
59 /**
60 * Described in header.
61 */
62 void memxor(u_int8_t dest[], u_int8_t src[], size_t n)
63 {
64 int i = 0, m;
65
66 m = n - sizeof(long);
67 while (i < m)
68 {
69 *(long*)(dest + i) ^= *(long*)(src + i);
70 i += sizeof(long);
71 }
72 while (i < n)
73 {
74 dest[i] ^= src[i];
75 i++;
76 }
77 }
78
79 /**
80 * Described in header.
81 */
82 bool mkdir_p(const char *path, mode_t mode)
83 {
84 size_t len;
85 char *pos, full[PATH_MAX];
86 pos = full;
87 if (!path || *path == '\0')
88 {
89 return TRUE;
90 }
91 len = snprintf(full, sizeof(full)-1, "%s", path);
92 if (len < 0 || len >= sizeof(full)-1)
93 {
94 DBG1("path string %s too long", path);
95 return FALSE;
96 }
97 /* ensure that the path ends with a '/' */
98 if (full[len-1] != '/')
99 {
100 full[len++] = '/';
101 full[len] = '\0';
102 }
103 /* skip '/' at the beginning */
104 while (*pos == '/')
105 {
106 pos++;
107 }
108 while ((pos = strchr(pos, '/')))
109 {
110 *pos = '\0';
111 if (access(full, F_OK) < 0)
112 {
113 if (mkdir(full, mode) < 0)
114 {
115 DBG1("failed to create directory %s", full);
116 return FALSE;
117 }
118 }
119 *pos = '/';
120 pos++;
121 }
122 return TRUE;
123 }
124
125 /**
126 * return null
127 */
128 void *return_null()
129 {
130 return NULL;
131 }
132
133 /**
134 * nop operation
135 */
136 void nop()
137 {
138 }
139
140 #ifndef HAVE_GCC_ATOMIC_OPERATIONS
141 #include <pthread.h>
142
143 /**
144 * We use a single mutex for all refcount variables.
145 */
146 static pthread_mutex_t ref_mutex = PTHREAD_MUTEX_INITIALIZER;
147
148 /**
149 * Increase refcount
150 */
151 void ref_get(refcount_t *ref)
152 {
153 pthread_mutex_lock(&ref_mutex);
154 (*ref)++;
155 pthread_mutex_unlock(&ref_mutex);
156 }
157
158 /**
159 * Decrease refcount
160 */
161 bool ref_put(refcount_t *ref)
162 {
163 bool more_refs;
164
165 pthread_mutex_lock(&ref_mutex);
166 more_refs = --(*ref);
167 pthread_mutex_unlock(&ref_mutex);
168 return !more_refs;
169 }
170 #endif /* HAVE_GCC_ATOMIC_OPERATIONS */
171
172 /**
173 * output handler in printf() for time_t
174 */
175 static int time_print(FILE *stream, const struct printf_info *info,
176 const void *const *args)
177 {
178 static const char* months[] = {
179 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
180 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
181 };
182 time_t *time = *((time_t**)(args[0]));
183 bool utc = TRUE;
184 struct tm t;
185
186 if (info->alt)
187 {
188 utc = *((bool*)(args[1]));
189 }
190 if (time == UNDEFINED_TIME)
191 {
192 return fprintf(stream, "--- -- --:--:--%s----",
193 info->alt ? " UTC " : " ");
194 }
195 if (utc)
196 {
197 gmtime_r(time, &t);
198 }
199 else
200 {
201 localtime_r(time, &t);
202 }
203 return fprintf(stream, "%s %02d %02d:%02d:%02d%s%04d",
204 months[t.tm_mon], t.tm_mday, t.tm_hour, t.tm_min,
205 t.tm_sec, utc ? " UTC " : " ", t.tm_year + 1900);
206 }
207
208 /**
209 * arginfo handler for printf() time
210 */
211 static int time_arginfo(const struct printf_info *info, size_t n, int *argtypes)
212 {
213 if (info->alt)
214 {
215 if (n > 1)
216 {
217 argtypes[0] = PA_POINTER;
218 argtypes[1] = PA_INT;
219 }
220 return 2;
221 }
222
223 if (n > 0)
224 {
225 argtypes[0] = PA_POINTER;
226 }
227 return 1;
228 }
229
230 /**
231 * output handler in printf() for time deltas
232 */
233 static int time_delta_print(FILE *stream, const struct printf_info *info,
234 const void *const *args)
235 {
236 char* unit = "second";
237 time_t *arg1, *arg2;
238 time_t delta;
239
240 arg1 = *((time_t**)(args[0]));
241 if (info->alt)
242 {
243 arg2 = *((time_t**)(args[1]));
244 delta = abs(*arg1 - *arg2);
245 }
246 else
247 {
248 delta = *arg1;
249 }
250
251 if (delta > 2 * 60 * 60 * 24)
252 {
253 delta /= 60 * 60 * 24;
254 unit = "day";
255 }
256 else if (delta > 2 * 60 * 60)
257 {
258 delta /= 60 * 60;
259 unit = "hour";
260 }
261 else if (delta > 2 * 60)
262 {
263 delta /= 60;
264 unit = "minute";
265 }
266 return fprintf(stream, "%d %s%s", delta, unit, (delta == 1)? "":"s");
267 }
268
269 /**
270 * arginfo handler for printf() time deltas
271 */
272 int time_delta_arginfo(const struct printf_info *info, size_t n, int *argtypes)
273 {
274 if (info->alt)
275 {
276 if (n > 1)
277 {
278 argtypes[0] = PA_POINTER;
279 argtypes[1] = PA_POINTER;
280 }
281 return 2;
282 }
283
284 if (n > 0)
285 {
286 argtypes[0] = PA_POINTER;
287 }
288 return 1;
289 }
290
291 /**
292 * Number of bytes per line to dump raw data
293 */
294 #define BYTES_PER_LINE 16
295
296 static char hexdig_upper[] = "0123456789ABCDEF";
297
298 /**
299 * output handler in printf() for mem ranges
300 */
301 static int mem_print(FILE *stream, const struct printf_info *info,
302 const void *const *args)
303 {
304 char *bytes = *((void**)(args[0]));
305 int len = *((size_t*)(args[1]));
306
307 char buffer[BYTES_PER_LINE * 3];
308 char ascii_buffer[BYTES_PER_LINE + 1];
309 char *buffer_pos = buffer;
310 char *bytes_pos = bytes;
311 char *bytes_roof = bytes + len;
312 int line_start = 0;
313 int i = 0;
314 int written = 0;
315
316 written += fprintf(stream, "=> %d bytes @ %p", len, bytes);
317
318 while (bytes_pos < bytes_roof)
319 {
320 *buffer_pos++ = hexdig_upper[(*bytes_pos >> 4) & 0xF];
321 *buffer_pos++ = hexdig_upper[ *bytes_pos & 0xF];
322
323 ascii_buffer[i++] =
324 (*bytes_pos > 31 && *bytes_pos < 127) ? *bytes_pos : '.';
325
326 if (++bytes_pos == bytes_roof || i == BYTES_PER_LINE)
327 {
328 int padding = 3 * (BYTES_PER_LINE - i);
329 int written;
330
331 while (padding--)
332 {
333 *buffer_pos++ = ' ';
334 }
335 *buffer_pos++ = '\0';
336 ascii_buffer[i] = '\0';
337
338 written += fprintf(stream, "\n%4d: %s %s",
339 line_start, buffer, ascii_buffer);
340
341
342 buffer_pos = buffer;
343 line_start += BYTES_PER_LINE;
344 i = 0;
345 }
346 else
347 {
348 *buffer_pos++ = ' ';
349 }
350 }
351 return written;
352 }
353
354 /**
355 * arginfo handler for printf() mem ranges
356 */
357 int mem_arginfo(const struct printf_info *info, size_t n, int *argtypes)
358 {
359 if (n > 1)
360 {
361 argtypes[0] = PA_POINTER;
362 argtypes[1] = PA_INT;
363 }
364 return 2;
365 }
366
367 /**
368 * return printf hook functions for a time
369 */
370 printf_hook_functions_t time_get_printf_hooks()
371 {
372 printf_hook_functions_t hooks = {time_print, time_arginfo};
373
374 return hooks;
375 }
376
377 /**
378 * return printf hook functions for a time delta
379 */
380 printf_hook_functions_t time_delta_get_printf_hooks()
381 {
382 printf_hook_functions_t hooks = {time_delta_print, time_delta_arginfo};
383
384 return hooks;
385 }
386
387 /**
388 * return printf hook functions for mem ranges
389 */
390 printf_hook_functions_t mem_get_printf_hooks()
391 {
392 printf_hook_functions_t hooks = {mem_print, mem_arginfo};
393
394 return hooks;
395 }
396