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