4a0eff45f45b8aa0da03ad2712dd5327fae90a93
[strongswan.git] / src / libstrongswan / utils.c
1 /*
2 * Copyright (C) 2008-2009 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
17 #include "utils.h"
18
19 #include <sys/stat.h>
20 #include <string.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <limits.h>
24 #include <dirent.h>
25 #include <time.h>
26
27 #include <enum.h>
28 #include <debug.h>
29
30 ENUM(status_names, SUCCESS, DESTROY_ME,
31 "SUCCESS",
32 "FAILED",
33 "OUT_OF_RES",
34 "ALREADY_DONE",
35 "NOT_SUPPORTED",
36 "INVALID_ARG",
37 "NOT_FOUND",
38 "PARSE_ERROR",
39 "VERIFY_ERROR",
40 "INVALID_STATE",
41 "DESTROY_ME",
42 "NEED_MORE",
43 );
44
45 /**
46 * Described in header.
47 */
48 void *clalloc(void * pointer, size_t size)
49 {
50 void *data;
51 data = malloc(size);
52
53 memcpy(data, pointer, size);
54
55 return (data);
56 }
57
58 /**
59 * Described in header.
60 */
61 void memxor(u_int8_t dest[], u_int8_t src[], size_t n)
62 {
63 int i = 0, m;
64
65 m = n - sizeof(long);
66 while (i < m)
67 {
68 *(long*)(dest + i) ^= *(long*)(src + i);
69 i += sizeof(long);
70 }
71 while (i < n)
72 {
73 dest[i] ^= src[i];
74 i++;
75 }
76 }
77
78 /**
79 * Described in header.
80 */
81 void *memstr(const void *haystack, const char *needle, size_t n)
82 {
83 unsigned const char *pos = haystack;
84 size_t l = strlen(needle);
85 for (; n >= l; ++pos, --n)
86 {
87 if (memeq(pos, needle, l))
88 {
89 return (void*)pos;
90 }
91 }
92 return NULL;
93 }
94
95 /**
96 * Described in header.
97 */
98 bool mkdir_p(const char *path, mode_t mode)
99 {
100 size_t len;
101 char *pos, full[PATH_MAX];
102 pos = full;
103 if (!path || *path == '\0')
104 {
105 return TRUE;
106 }
107 len = snprintf(full, sizeof(full)-1, "%s", path);
108 if (len < 0 || len >= sizeof(full)-1)
109 {
110 DBG1("path string %s too long", path);
111 return FALSE;
112 }
113 /* ensure that the path ends with a '/' */
114 if (full[len-1] != '/')
115 {
116 full[len++] = '/';
117 full[len] = '\0';
118 }
119 /* skip '/' at the beginning */
120 while (*pos == '/')
121 {
122 pos++;
123 }
124 while ((pos = strchr(pos, '/')))
125 {
126 *pos = '\0';
127 if (access(full, F_OK) < 0)
128 {
129 if (mkdir(full, mode) < 0)
130 {
131 DBG1("failed to create directory %s", full);
132 return FALSE;
133 }
134 }
135 *pos = '/';
136 pos++;
137 }
138 return TRUE;
139 }
140
141 /**
142 * return null
143 */
144 void *return_null()
145 {
146 return NULL;
147 }
148
149 /**
150 * returns TRUE
151 */
152 bool return_true()
153 {
154 return TRUE;
155 }
156
157 /**
158 * returns FALSE
159 */
160 bool return_false()
161 {
162 return FALSE;
163 }
164
165 /**
166 * nop operation
167 */
168 void nop()
169 {
170 }
171
172 #ifndef HAVE_GCC_ATOMIC_OPERATIONS
173 #include <pthread.h>
174
175 /**
176 * We use a single mutex for all refcount variables.
177 */
178 static pthread_mutex_t ref_mutex = PTHREAD_MUTEX_INITIALIZER;
179
180 /**
181 * Increase refcount
182 */
183 void ref_get(refcount_t *ref)
184 {
185 pthread_mutex_lock(&ref_mutex);
186 (*ref)++;
187 pthread_mutex_unlock(&ref_mutex);
188 }
189
190 /**
191 * Decrease refcount
192 */
193 bool ref_put(refcount_t *ref)
194 {
195 bool more_refs;
196
197 pthread_mutex_lock(&ref_mutex);
198 more_refs = --(*ref);
199 pthread_mutex_unlock(&ref_mutex);
200 return !more_refs;
201 }
202 #endif /* HAVE_GCC_ATOMIC_OPERATIONS */
203
204 /**
205 * Described in header.
206 */
207 int time_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
208 const void *const *args)
209 {
210 static const char* months[] = {
211 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
212 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
213 };
214 time_t *time = *((time_t**)(args[0]));
215 bool utc = *((bool*)(args[1]));;
216 struct tm t;
217
218 if (time == UNDEFINED_TIME)
219 {
220 return print_in_hook(dst, len, "--- -- --:--:--%s----",
221 utc ? " UTC " : " ");
222 }
223 if (utc)
224 {
225 gmtime_r(time, &t);
226 }
227 else
228 {
229 localtime_r(time, &t);
230 }
231 return print_in_hook(dst, len, "%s %02d %02d:%02d:%02d%s%04d",
232 months[t.tm_mon], t.tm_mday, t.tm_hour, t.tm_min,
233 t.tm_sec, utc ? " UTC " : " ", t.tm_year + 1900);
234 }
235
236 /**
237 * Described in header.
238 */
239 int time_delta_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
240 const void *const *args)
241 {
242 char* unit = "second";
243 time_t *arg1 = *((time_t**)(args[0]));
244 time_t *arg2 = *((time_t**)(args[1]));
245 time_t delta = abs(*arg1 - *arg2);
246
247 if (delta > 2 * 60 * 60 * 24)
248 {
249 delta /= 60 * 60 * 24;
250 unit = "day";
251 }
252 else if (delta > 2 * 60 * 60)
253 {
254 delta /= 60 * 60;
255 unit = "hour";
256 }
257 else if (delta > 2 * 60)
258 {
259 delta /= 60;
260 unit = "minute";
261 }
262 return print_in_hook(dst, len, "%d %s%s", delta, unit, (delta == 1)? "":"s");
263 }
264
265 /**
266 * Number of bytes per line to dump raw data
267 */
268 #define BYTES_PER_LINE 16
269
270 static char hexdig_upper[] = "0123456789ABCDEF";
271
272 /**
273 * Described in header.
274 */
275 int mem_printf_hook(char *dst, size_t dstlen,
276 printf_hook_spec_t *spec, const void *const *args)
277 {
278 char *bytes = *((void**)(args[0]));
279 int len = *((size_t*)(args[1]));
280
281 char buffer[BYTES_PER_LINE * 3];
282 char ascii_buffer[BYTES_PER_LINE + 1];
283 char *buffer_pos = buffer;
284 char *bytes_pos = bytes;
285 char *bytes_roof = bytes + len;
286 int line_start = 0;
287 int i = 0;
288 int written = 0;
289
290 written += print_in_hook(dst, dstlen, "=> %d bytes @ %p", len, bytes);
291
292 while (bytes_pos < bytes_roof)
293 {
294 *buffer_pos++ = hexdig_upper[(*bytes_pos >> 4) & 0xF];
295 *buffer_pos++ = hexdig_upper[ *bytes_pos & 0xF];
296
297 ascii_buffer[i++] =
298 (*bytes_pos > 31 && *bytes_pos < 127) ? *bytes_pos : '.';
299
300 if (++bytes_pos == bytes_roof || i == BYTES_PER_LINE)
301 {
302 int padding = 3 * (BYTES_PER_LINE - i);
303
304 while (padding--)
305 {
306 *buffer_pos++ = ' ';
307 }
308 *buffer_pos++ = '\0';
309 ascii_buffer[i] = '\0';
310
311 written += print_in_hook(dst, dstlen, "\n%4d: %s %s",
312 line_start, buffer, ascii_buffer);
313
314 buffer_pos = buffer;
315 line_start += BYTES_PER_LINE;
316 i = 0;
317 }
318 else
319 {
320 *buffer_pos++ = ' ';
321 }
322 }
323 return written;
324 }