added private flag to asn1_init
[strongswan.git] / src / libstrongswan / types.c
1 /**
2 * @file types.c
3 *
4 * @brief Generic types.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
22 */
23
24 #include <string.h>
25 #include <time.h>
26 #include <stdio.h>
27 #include <stdarg.h>
28 #include <pthread.h>
29 #include <printf.h>
30
31 #include "types.h"
32
33 ENUM(status_names, SUCCESS, DESTROY_ME,
34 "SUCCESS",
35 "FAILED",
36 "OUT_OF_RES",
37 "ALREADY_DONE",
38 "NOT_SUPPORTED",
39 "INVALID_ARG",
40 "NOT_FOUND",
41 "PARSE_ERROR",
42 "VERIFY_ERROR",
43 "INVALID_STATE",
44 "DESTROY_ME",
45 );
46
47
48 /**
49 * Empty chunk.
50 */
51 chunk_t CHUNK_INITIALIZER = { NULL, 0 };
52
53 /**
54 * Described in header.
55 */
56 chunk_t chunk_clone(chunk_t chunk)
57 {
58 chunk_t clone = CHUNK_INITIALIZER;
59
60 if (chunk.ptr && chunk.len > 0)
61 {
62 clone.ptr = malloc(chunk.len);
63 clone.len = chunk.len;
64 memcpy(clone.ptr, chunk.ptr, chunk.len);
65 }
66
67 return clone;
68 }
69
70 /**
71 * Decribed in header.
72 */
73 chunk_t chunk_cat(const char* mode, ...)
74 {
75 chunk_t construct;
76 va_list chunks;
77 u_char *pos;
78 int i;
79 int count = strlen(mode);
80
81 /* sum up lengths of individual chunks */
82 va_start(chunks, mode);
83 construct.len = 0;
84 for (i = 0; i < count; i++)
85 {
86 chunk_t ch = va_arg(chunks, chunk_t);
87 construct.len += ch.len;
88 }
89 va_end(chunks);
90
91 /* allocate needed memory for construct */
92 construct.ptr = malloc(construct.len);
93 pos = construct.ptr;
94
95 /* copy or move the chunks */
96 va_start(chunks, mode);
97 for (i = 0; i < count; i++)
98 {
99 chunk_t ch = va_arg(chunks, chunk_t);
100 switch (*mode++)
101 {
102 case 'm':
103 memcpy(pos, ch.ptr, ch.len);
104 pos += ch.len;
105 free(ch.ptr);
106 break;
107 case 'c':
108 default:
109 memcpy(pos, ch.ptr, ch.len);
110 pos += ch.len;
111 }
112 }
113 va_end(chunks);
114
115 return construct;
116 }
117
118 /**
119 * Described in header.
120 */
121 void chunk_free(chunk_t *chunk)
122 {
123 free(chunk->ptr);
124 chunk->ptr = NULL;
125 chunk->len = 0;
126 }
127
128 /**
129 * Described in header.
130 */
131 chunk_t chunk_alloc(size_t bytes)
132 {
133 chunk_t new_chunk;
134 new_chunk.ptr = malloc(bytes);
135 new_chunk.len = bytes;
136 return new_chunk;
137 }
138
139 /**
140 * Described in header.
141 */
142 bool chunk_equals(chunk_t a, chunk_t b)
143 {
144 return a.ptr != NULL && b.ptr != NULL &&
145 a.len == b.len && memeq(a.ptr, b.ptr, a.len);
146 }
147
148 /**
149 * Described in header.
150 */
151 bool chunk_equals_or_null(chunk_t a, chunk_t b)
152 {
153 if (a.ptr == NULL || b.ptr == NULL)
154 return TRUE;
155 return a.len == b.len && memeq(a.ptr, b.ptr, a.len);
156 }
157
158 /**
159 * Described in header.
160 */
161 void *clalloc(void * pointer, size_t size)
162 {
163 void *data;
164 data = malloc(size);
165
166 memcpy(data, pointer,size);
167
168 return (data);
169 }
170
171 /**
172 * We use a single mutex for all refcount variables. This
173 * is not optimal for performance, but the critical section
174 * is not that long...
175 * TODO: Consider to include a mutex in each refcount_t variable.
176 */
177 static pthread_mutex_t ref_mutex = PTHREAD_MUTEX_INITIALIZER;
178
179 /**
180 * Described in header.
181 *
182 * TODO: May be implemented with atomic CPU instructions
183 * instead of a mutex.
184 */
185 void ref_get(refcount_t *ref)
186 {
187 pthread_mutex_lock(&ref_mutex);
188 (*ref)++;
189 pthread_mutex_unlock(&ref_mutex);
190 }
191
192 /**
193 * Described in header.
194 *
195 * TODO: May be implemented with atomic CPU instructions
196 * instead of a mutex.
197 */
198 bool ref_put(refcount_t *ref)
199 {
200 bool more_refs;
201
202 pthread_mutex_lock(&ref_mutex);
203 more_refs = --(*ref);
204 pthread_mutex_unlock(&ref_mutex);
205 return !more_refs;
206 }
207
208 /**
209 * Number of bytes per line to dump raw data
210 */
211 #define BYTES_PER_LINE 16
212
213 /**
214 * output handler in printf() for byte ranges
215 */
216 static int print_bytes(FILE *stream, const struct printf_info *info,
217 const void *const *args)
218 {
219 char *bytes = *((void**)(args[0]));
220 int len = *((size_t*)(args[1]));
221
222 char buffer[BYTES_PER_LINE * 3];
223 char ascii_buffer[BYTES_PER_LINE + 1];
224 char *buffer_pos = buffer;
225 char *bytes_pos = bytes;
226 char *bytes_roof = bytes + len;
227 int line_start = 0;
228 int i = 0;
229 int written = 0;
230
231 written += fprintf(stream, "=> %d bytes @ %p", len, bytes);
232
233 while (bytes_pos < bytes_roof)
234 {
235 static char hexdig[] = "0123456789ABCDEF";
236
237 *buffer_pos++ = hexdig[(*bytes_pos >> 4) & 0xF];
238 *buffer_pos++ = hexdig[ *bytes_pos & 0xF];
239
240 ascii_buffer[i++] =
241 (*bytes_pos > 31 && *bytes_pos < 127) ? *bytes_pos : '.';
242
243 if (++bytes_pos == bytes_roof || i == BYTES_PER_LINE)
244 {
245 int padding = 3 * (BYTES_PER_LINE - i);
246 int written;
247
248 while (padding--)
249 {
250 *buffer_pos++ = ' ';
251 }
252 *buffer_pos++ = '\0';
253 ascii_buffer[i] = '\0';
254
255 written += fprintf(stream, "\n%4d: %s %s",
256 line_start, buffer, ascii_buffer);
257
258
259 buffer_pos = buffer;
260 line_start += BYTES_PER_LINE;
261 i = 0;
262 }
263 else
264 {
265 *buffer_pos++ = ' ';
266 }
267 }
268 return written;
269 }
270
271 /**
272 * output handler in printf() for chunks
273 */
274 static int print_chunk(FILE *stream, const struct printf_info *info,
275 const void *const *args)
276 {
277 chunk_t *chunk = *((chunk_t**)(args[0]));
278 bool first = TRUE;
279 chunk_t copy = *chunk;
280 int written = 0;
281
282 if (!info->alt)
283 {
284 const void *new_args[] = {&chunk->ptr, &chunk->len};
285 return print_bytes(stream, info, new_args);
286 }
287
288 while (copy.len > 0)
289 {
290 static char hexdig[] = "0123456789abcdef";
291 if (first)
292 {
293 first = FALSE;
294 }
295 else
296 {
297 written += fprintf(stream, ":");
298 }
299 written += fprintf(stream, "%c%c",
300 hexdig[(*copy.ptr >> 4) & 0x0f],
301 hexdig[ *copy.ptr++ & 0x0f]);
302 copy.len--;
303 }
304 return written;
305 }
306
307 /**
308 * output handler in printf() for time_t
309 */
310 static int print_time(FILE *stream, const struct printf_info *info,
311 const void *const *args)
312 {
313 static const char* months[] = {
314 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
315 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
316 };
317 time_t time = *((time_t*)(args[0]));
318 bool utc = TRUE;
319 struct tm t;
320
321 if (info->alt)
322 {
323 utc = *((bool*)(args[1]));
324 }
325 if (time == UNDEFINED_TIME)
326 {
327 return fprintf(stream, "--- -- --:--:--%s----",
328 info->alt ? " UTC " : " ");
329 }
330 if (utc)
331 {
332 gmtime_r(&time, &t);
333 }
334 else
335 {
336 localtime_r(&time, &t);
337 }
338 return fprintf(stream, "%s %02d %02d:%02d:%02d%s%04d",
339 months[t.tm_mon], t.tm_mday, t.tm_hour, t.tm_min,
340 t.tm_sec, info->alt ? " UTC " : " ", t.tm_year + 1900);
341 }
342
343 /**
344 * output handler in printf() for time deltas
345 */
346 static int print_time_delta(FILE *stream, const struct printf_info *info,
347 const void *const *args)
348 {
349 time_t start = *((time_t*)(args[0]));
350 time_t end = *((time_t*)(args[1]));
351 u_int delta = abs(end - start);
352 char* unit = "second";
353
354 if (delta > 2 * 60 * 60 * 24)
355 {
356 delta /= 60 * 60 * 24;
357 unit = "days";
358 }
359 else if (delta > 2 * 60 * 60)
360 {
361 delta /= 60 * 60;
362 unit = "hours";
363 }
364 else if (delta > 2 * 60)
365 {
366 delta /= 60;
367 unit = "minutes";
368 }
369 return fprintf(stream, "%d %s", delta, unit);
370 }
371
372 /**
373 * arginfo handler in printf() for byte ranges
374 */
375 static int print_bytes_arginfo(const struct printf_info *info, size_t n, int *argtypes)
376 {
377 if (n > 1)
378 {
379 argtypes[0] = PA_POINTER;
380 argtypes[1] = PA_INT;
381 }
382 return 2;
383 }
384
385 /**
386 * arginfo handler in printf() for time deltas
387 */
388 static int print_time_delta_arginfo(const struct printf_info *info, size_t n, int *argtypes)
389 {
390 if (n > 1)
391 {
392 argtypes[0] = PA_INT;
393 argtypes[1] = PA_INT;
394 }
395 return 2;
396 }
397
398 /**
399 * arginfo handler in printf() for time_t
400 */
401 static int print_time_arginfo(const struct printf_info *info, size_t n, int *argtypes)
402 {
403 if (info->alt)
404 {
405 if (n > 1)
406 {
407 argtypes[0] = PA_INT;
408 argtypes[1] = PA_INT;
409 }
410 return 2;
411 }
412
413 if (n > 0)
414 {
415 argtypes[0] = PA_INT;
416 }
417 return 1;
418 }
419
420 /**
421 * arginfo handler in printf() for chunks
422 */
423 static int print_chunk_arginfo(const struct printf_info *info, size_t n, int *argtypes)
424 {
425 if (n > 0)
426 {
427 argtypes[0] = PA_POINTER;
428 }
429 return 1;
430 }
431
432 /**
433 * register printf() handlers for time_t
434 */
435 static void __attribute__ ((constructor))print_register()
436 {
437 register_printf_function(CHUNK_PRINTF_SPEC, print_chunk, print_chunk_arginfo);
438 register_printf_function(BYTES_PRINTF_SPEC, print_bytes, print_bytes_arginfo);
439 register_printf_function(TIME_PRINTF_SPEC, print_time, print_time_arginfo);
440 register_printf_function(TIME_DELTA_PRINTF_SPEC, print_time_delta, print_time_delta_arginfo);
441 }