]>
Commit | Line | Data |
---|---|---|
552cc11b | 1 | /* |
38031382 | 2 | * Copyright (C) 2008-2010 Tobias Brunner |
552cc11b MW |
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. | |
552cc11b MW |
15 | */ |
16 | ||
17 | #include "utils.h" | |
18 | ||
6c20579a | 19 | #include <sys/stat.h> |
552cc11b | 20 | #include <string.h> |
552cc11b | 21 | #include <stdio.h> |
6c20579a | 22 | #include <unistd.h> |
74b14b40 | 23 | #include <stdint.h> |
d24a74c5 | 24 | #include <limits.h> |
6c20579a | 25 | #include <dirent.h> |
f464d750 | 26 | #include <time.h> |
552cc11b | 27 | |
fac3bfa5 TB |
28 | #include "enum.h" |
29 | #include "debug.h" | |
552cc11b | 30 | |
a8809bb0 | 31 | ENUM(status_names, SUCCESS, NEED_MORE, |
552cc11b MW |
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); | |
7daf5226 | 53 | |
552cc11b | 54 | memcpy(data, pointer, size); |
7daf5226 | 55 | |
552cc11b MW |
56 | return (data); |
57 | } | |
58 | ||
59 | /** | |
60 | * Described in header. | |
61 | */ | |
01e43e31 | 62 | void memxor(u_int8_t dst[], u_int8_t src[], size_t n) |
552cc11b | 63 | { |
01e43e31 | 64 | int m, i; |
7daf5226 | 65 | |
01e43e31 | 66 | /* byte wise XOR until dst aligned */ |
09846603 | 67 | for (i = 0; (uintptr_t)&dst[i] % sizeof(long) && i < n; i++) |
4fd233a7 | 68 | { |
01e43e31 | 69 | dst[i] ^= src[i]; |
4fd233a7 | 70 | } |
01e43e31 | 71 | /* try to use words if src shares an aligment with dst */ |
74b14b40 | 72 | switch (((uintptr_t)&src[i] % sizeof(long))) |
552cc11b | 73 | { |
01e43e31 MW |
74 | case 0: |
75 | for (m = n - sizeof(long); i <= m; i += sizeof(long)) | |
76 | { | |
77 | *(long*)&dst[i] ^= *(long*)&src[i]; | |
78 | } | |
79 | break; | |
80 | case sizeof(int): | |
81 | for (m = n - sizeof(int); i <= m; i += sizeof(int)) | |
82 | { | |
83 | *(int*)&dst[i] ^= *(int*)&src[i]; | |
84 | } | |
85 | break; | |
86 | case sizeof(short): | |
87 | for (m = n - sizeof(short); i <= m; i += sizeof(short)) | |
88 | { | |
89 | *(short*)&dst[i] ^= *(short*)&src[i]; | |
90 | } | |
91 | break; | |
92 | default: | |
93 | break; | |
94 | } | |
95 | /* byte wise XOR of the rest */ | |
96 | for (; i < n; i++) | |
97 | { | |
98 | dst[i] ^= src[i]; | |
552cc11b MW |
99 | } |
100 | } | |
101 | ||
81736d7d TB |
102 | /** |
103 | * Described in header. | |
104 | */ | |
105 | void *memstr(const void *haystack, const char *needle, size_t n) | |
106 | { | |
107 | unsigned const char *pos = haystack; | |
108 | size_t l = strlen(needle); | |
109 | for (; n >= l; ++pos, --n) | |
110 | { | |
111 | if (memeq(pos, needle, l)) | |
112 | { | |
113 | return (void*)pos; | |
114 | } | |
115 | } | |
116 | return NULL; | |
117 | } | |
118 | ||
d543d9ca TB |
119 | /** |
120 | * Described in header. | |
121 | */ | |
122 | char* translate(char *str, const char *from, const char *to) | |
123 | { | |
124 | char *pos = str; | |
125 | if (strlen(from) != strlen(to)) | |
126 | { | |
127 | return str; | |
128 | } | |
129 | while (pos && *pos) | |
130 | { | |
131 | char *match; | |
132 | if ((match = strchr(from, *pos)) != NULL) | |
133 | { | |
134 | *pos = to[match - from]; | |
135 | } | |
136 | pos++; | |
137 | } | |
138 | return str; | |
139 | } | |
140 | ||
6c20579a TB |
141 | /** |
142 | * Described in header. | |
143 | */ | |
144 | bool mkdir_p(const char *path, mode_t mode) | |
145 | { | |
fc1afcc8 | 146 | int len; |
6c20579a TB |
147 | char *pos, full[PATH_MAX]; |
148 | pos = full; | |
149 | if (!path || *path == '\0') | |
150 | { | |
151 | return TRUE; | |
152 | } | |
153 | len = snprintf(full, sizeof(full)-1, "%s", path); | |
154 | if (len < 0 || len >= sizeof(full)-1) | |
155 | { | |
8b0e0910 | 156 | DBG1(DBG_LIB, "path string %s too long", path); |
6c20579a TB |
157 | return FALSE; |
158 | } | |
159 | /* ensure that the path ends with a '/' */ | |
160 | if (full[len-1] != '/') | |
161 | { | |
162 | full[len++] = '/'; | |
163 | full[len] = '\0'; | |
164 | } | |
165 | /* skip '/' at the beginning */ | |
166 | while (*pos == '/') | |
167 | { | |
168 | pos++; | |
169 | } | |
170 | while ((pos = strchr(pos, '/'))) | |
171 | { | |
172 | *pos = '\0'; | |
173 | if (access(full, F_OK) < 0) | |
174 | { | |
175 | if (mkdir(full, mode) < 0) | |
176 | { | |
8b0e0910 | 177 | DBG1(DBG_LIB, "failed to create directory %s", full); |
6c20579a TB |
178 | return FALSE; |
179 | } | |
180 | } | |
181 | *pos = '/'; | |
182 | pos++; | |
183 | } | |
184 | return TRUE; | |
185 | } | |
186 | ||
3f310c0d MW |
187 | /** |
188 | * Return monotonic time | |
189 | */ | |
190 | time_t time_monotonic(timeval_t *tv) | |
191 | { | |
b2944d71 TB |
192 | #if defined(HAVE_CLOCK_GETTIME) && \ |
193 | (defined(HAVE_CONDATTR_CLOCK_MONOTONIC) || \ | |
194 | defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC)) | |
3d5818ec MW |
195 | /* as we use time_monotonic() for condvar operations, we use the |
196 | * monotonic time source only if it is also supported by pthread. */ | |
3f310c0d | 197 | timespec_t ts; |
7daf5226 | 198 | |
3f310c0d MW |
199 | if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) |
200 | { | |
201 | if (tv) | |
202 | { | |
203 | tv->tv_sec = ts.tv_sec; | |
204 | tv->tv_usec = ts.tv_nsec / 1000; | |
205 | } | |
206 | return ts.tv_sec; | |
207 | } | |
b2944d71 | 208 | #endif /* HAVE_CLOCK_GETTIME && (...) */ |
3f310c0d MW |
209 | /* Fallback to non-monotonic timestamps: |
210 | * On MAC OS X, creating monotonic timestamps is rather difficult. We | |
211 | * could use mach_absolute_time() and catch sleep/wakeup notifications. | |
3d5818ec MW |
212 | * We stick to the simpler (non-monotonic) gettimeofday() for now. |
213 | * But keep in mind: we need the same time source here as in condvar! */ | |
3f310c0d MW |
214 | if (!tv) |
215 | { | |
216 | return time(NULL); | |
217 | } | |
218 | if (gettimeofday(tv, NULL) != 0) | |
219 | { /* should actually never fail if passed pointers are valid */ | |
220 | return -1; | |
221 | } | |
222 | return tv->tv_sec; | |
223 | } | |
224 | ||
081ae2eb MW |
225 | /** |
226 | * return null | |
227 | */ | |
228 | void *return_null() | |
229 | { | |
230 | return NULL; | |
231 | } | |
232 | ||
da17b016 MW |
233 | /** |
234 | * returns TRUE | |
235 | */ | |
236 | bool return_true() | |
237 | { | |
238 | return TRUE; | |
239 | } | |
240 | ||
241 | /** | |
242 | * returns FALSE | |
243 | */ | |
244 | bool return_false() | |
245 | { | |
246 | return FALSE; | |
247 | } | |
248 | ||
233b853d MW |
249 | /** |
250 | * nop operation | |
251 | */ | |
252 | void nop() | |
253 | { | |
254 | } | |
255 | ||
efd0fe21 MW |
256 | #ifndef HAVE_GCC_ATOMIC_OPERATIONS |
257 | #include <pthread.h> | |
258 | ||
552cc11b | 259 | /** |
7daf5226 | 260 | * We use a single mutex for all refcount variables. |
552cc11b MW |
261 | */ |
262 | static pthread_mutex_t ref_mutex = PTHREAD_MUTEX_INITIALIZER; | |
263 | ||
264 | /** | |
efd0fe21 | 265 | * Increase refcount |
552cc11b MW |
266 | */ |
267 | void ref_get(refcount_t *ref) | |
268 | { | |
269 | pthread_mutex_lock(&ref_mutex); | |
270 | (*ref)++; | |
271 | pthread_mutex_unlock(&ref_mutex); | |
272 | } | |
273 | ||
274 | /** | |
efd0fe21 | 275 | * Decrease refcount |
552cc11b MW |
276 | */ |
277 | bool ref_put(refcount_t *ref) | |
278 | { | |
279 | bool more_refs; | |
7daf5226 | 280 | |
552cc11b MW |
281 | pthread_mutex_lock(&ref_mutex); |
282 | more_refs = --(*ref); | |
283 | pthread_mutex_unlock(&ref_mutex); | |
284 | return !more_refs; | |
285 | } | |
efd0fe21 | 286 | #endif /* HAVE_GCC_ATOMIC_OPERATIONS */ |
552cc11b MW |
287 | |
288 | /** | |
d25ce370 | 289 | * Described in header. |
552cc11b | 290 | */ |
d25ce370 TB |
291 | int time_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec, |
292 | const void *const *args) | |
552cc11b MW |
293 | { |
294 | static const char* months[] = { | |
295 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
296 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" | |
297 | }; | |
298 | time_t *time = *((time_t**)(args[0])); | |
d25ce370 | 299 | bool utc = *((bool*)(args[1]));; |
552cc11b | 300 | struct tm t; |
7daf5226 | 301 | |
552cc11b MW |
302 | if (time == UNDEFINED_TIME) |
303 | { | |
d25ce370 TB |
304 | return print_in_hook(dst, len, "--- -- --:--:--%s----", |
305 | utc ? " UTC " : " "); | |
552cc11b MW |
306 | } |
307 | if (utc) | |
308 | { | |
309 | gmtime_r(time, &t); | |
310 | } | |
311 | else | |
312 | { | |
313 | localtime_r(time, &t); | |
314 | } | |
d25ce370 TB |
315 | return print_in_hook(dst, len, "%s %02d %02d:%02d:%02d%s%04d", |
316 | months[t.tm_mon], t.tm_mday, t.tm_hour, t.tm_min, | |
317 | t.tm_sec, utc ? " UTC " : " ", t.tm_year + 1900); | |
552cc11b MW |
318 | } |
319 | ||
320 | /** | |
d25ce370 | 321 | * Described in header. |
552cc11b | 322 | */ |
d25ce370 TB |
323 | int time_delta_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec, |
324 | const void *const *args) | |
552cc11b MW |
325 | { |
326 | char* unit = "second"; | |
d25ce370 TB |
327 | time_t *arg1 = *((time_t**)(args[0])); |
328 | time_t *arg2 = *((time_t**)(args[1])); | |
329 | time_t delta = abs(*arg1 - *arg2); | |
7daf5226 | 330 | |
552cc11b MW |
331 | if (delta > 2 * 60 * 60 * 24) |
332 | { | |
333 | delta /= 60 * 60 * 24; | |
334 | unit = "day"; | |
335 | } | |
336 | else if (delta > 2 * 60 * 60) | |
337 | { | |
338 | delta /= 60 * 60; | |
339 | unit = "hour"; | |
340 | } | |
341 | else if (delta > 2 * 60) | |
342 | { | |
343 | delta /= 60; | |
344 | unit = "minute"; | |
345 | } | |
d25ce370 | 346 | return print_in_hook(dst, len, "%d %s%s", delta, unit, (delta == 1)? "":"s"); |
552cc11b MW |
347 | } |
348 | ||
349 | /** | |
350 | * Number of bytes per line to dump raw data | |
351 | */ | |
352 | #define BYTES_PER_LINE 16 | |
353 | ||
354 | static char hexdig_upper[] = "0123456789ABCDEF"; | |
355 | ||
356 | /** | |
d25ce370 | 357 | * Described in header. |
552cc11b | 358 | */ |
d25ce370 TB |
359 | int mem_printf_hook(char *dst, size_t dstlen, |
360 | printf_hook_spec_t *spec, const void *const *args) | |
552cc11b MW |
361 | { |
362 | char *bytes = *((void**)(args[0])); | |
363 | int len = *((size_t*)(args[1])); | |
7daf5226 | 364 | |
552cc11b MW |
365 | char buffer[BYTES_PER_LINE * 3]; |
366 | char ascii_buffer[BYTES_PER_LINE + 1]; | |
367 | char *buffer_pos = buffer; | |
368 | char *bytes_pos = bytes; | |
369 | char *bytes_roof = bytes + len; | |
370 | int line_start = 0; | |
371 | int i = 0; | |
372 | int written = 0; | |
7daf5226 | 373 | |
d25ce370 | 374 | written += print_in_hook(dst, dstlen, "=> %d bytes @ %p", len, bytes); |
7daf5226 | 375 | |
552cc11b MW |
376 | while (bytes_pos < bytes_roof) |
377 | { | |
378 | *buffer_pos++ = hexdig_upper[(*bytes_pos >> 4) & 0xF]; | |
379 | *buffer_pos++ = hexdig_upper[ *bytes_pos & 0xF]; | |
380 | ||
381 | ascii_buffer[i++] = | |
382 | (*bytes_pos > 31 && *bytes_pos < 127) ? *bytes_pos : '.'; | |
383 | ||
7daf5226 | 384 | if (++bytes_pos == bytes_roof || i == BYTES_PER_LINE) |
552cc11b MW |
385 | { |
386 | int padding = 3 * (BYTES_PER_LINE - i); | |
7daf5226 | 387 | |
552cc11b MW |
388 | while (padding--) |
389 | { | |
390 | *buffer_pos++ = ' '; | |
391 | } | |
392 | *buffer_pos++ = '\0'; | |
393 | ascii_buffer[i] = '\0'; | |
7daf5226 | 394 | |
d25ce370 | 395 | written += print_in_hook(dst, dstlen, "\n%4d: %s %s", |
323f9f99 | 396 | line_start, buffer, ascii_buffer); |
7daf5226 | 397 | |
552cc11b MW |
398 | buffer_pos = buffer; |
399 | line_start += BYTES_PER_LINE; | |
400 | i = 0; | |
401 | } | |
402 | else | |
403 | { | |
404 | *buffer_pos++ = ' '; | |
405 | } | |
406 | } | |
407 | return written; | |
408 | } |