(no commit message)
[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
34 /**
35 * String mappings for type status_t.
36 */
37 mapping_t status_m[] = {
38 {SUCCESS, "SUCCESS"},
39 {FAILED, "FAILED"},
40 {OUT_OF_RES, "OUT_OF_RES"},
41 {ALREADY_DONE, "ALREADY_DONE"},
42 {NOT_SUPPORTED, "NOT_SUPPORTED"},
43 {INVALID_ARG, "INVALID_ARG"},
44 {NOT_FOUND, "NOT_FOUND"},
45 {PARSE_ERROR, "PARSE_ERROR"},
46 {VERIFY_ERROR, "VERIFY_ERROR"},
47 {INVALID_STATE, "INVALID_STATE"},
48 {DESTROY_ME, "DESTROY_ME"},
49 {MAPPING_END, NULL}
50 };
51
52 /**
53 * Empty chunk.
54 */
55 chunk_t CHUNK_INITIALIZER = { NULL, 0 };
56
57 /**
58 * Described in header.
59 */
60 chunk_t chunk_clone(chunk_t chunk)
61 {
62 chunk_t clone = CHUNK_INITIALIZER;
63
64 if (chunk.ptr && chunk.len > 0)
65 {
66 clone.ptr = malloc(chunk.len);
67 clone.len = chunk.len;
68 memcpy(clone.ptr, chunk.ptr, chunk.len);
69 }
70
71 return clone;
72 }
73
74 /**
75 * Decribed in header.
76 */
77 chunk_t chunk_cat(const char* mode, ...)
78 {
79 chunk_t construct;
80 va_list chunks;
81 u_char *pos;
82 int i;
83 int count = strlen(mode);
84
85 /* sum up lengths of individual chunks */
86 va_start(chunks, mode);
87 construct.len = 0;
88 for (i = 0; i < count; i++)
89 {
90 chunk_t ch = va_arg(chunks, chunk_t);
91 construct.len += ch.len;
92 }
93 va_end(chunks);
94
95 /* allocate needed memory for construct */
96 construct.ptr = malloc(construct.len);
97 pos = construct.ptr;
98
99 /* copy or move the chunks */
100 va_start(chunks, mode);
101 for (i = 0; i < count; i++)
102 {
103 chunk_t ch = va_arg(chunks, chunk_t);
104 switch (*mode++)
105 {
106 case 'm':
107 memcpy(pos, ch.ptr, ch.len);
108 pos += ch.len;
109 free(ch.ptr);
110 break;
111 case 'c':
112 default:
113 memcpy(pos, ch.ptr, ch.len);
114 pos += ch.len;
115 }
116 }
117 va_end(chunks);
118
119 return construct;
120 }
121
122 /**
123 * Described in header.
124 */
125 void chunk_free(chunk_t *chunk)
126 {
127 free(chunk->ptr);
128 chunk->ptr = NULL;
129 chunk->len = 0;
130 }
131
132 /**
133 * Described in header.
134 */
135 chunk_t chunk_alloc(size_t bytes)
136 {
137 chunk_t new_chunk;
138 new_chunk.ptr = malloc(bytes);
139 new_chunk.len = bytes;
140 return new_chunk;
141 }
142
143 /**
144 * Described in header.
145 */
146 bool chunk_equals(chunk_t a, chunk_t b)
147 {
148 return a.ptr != NULL && b.ptr != NULL &&
149 a.len == b.len && memeq(a.ptr, b.ptr, a.len);
150 }
151
152 /**
153 * Described in header.
154 */
155 bool chunk_equals_or_null(chunk_t a, chunk_t b)
156 {
157 if (a.ptr == NULL || b.ptr == NULL)
158 return TRUE;
159 return a.len == b.len && memeq(a.ptr, b.ptr, a.len);
160 }
161
162 /**
163 * Described in header.
164 */
165 void chunk_to_hex(char *buf, size_t buflen, chunk_t chunk)
166 {
167 bool first = TRUE;
168
169 buflen--; /* reserve space for null termination */
170
171 while (chunk.len > 0 && buflen > 2)
172 {
173 static char hexdig[] = "0123456789abcdef";
174
175 if (first)
176 {
177 first = FALSE;
178 }
179 else
180 {
181 *buf++ = ':'; buflen--;
182 }
183 *buf++ = hexdig[(*chunk.ptr >> 4) & 0x0f];
184 *buf++ = hexdig[ *chunk.ptr++ & 0x0f];
185 buflen -= 2; chunk.len--;
186 }
187 *buf = '\0';
188 }
189
190 /**
191 * Number of bytes per line to dump raw data
192 */
193 #define BYTES_PER_LINE 16
194
195 /**
196 * output handler in printf() for byte ranges
197 */
198 static int print_bytes(FILE *stream, const struct printf_info *info,
199 const void *const *args)
200 {
201 char *bytes = *((void**)(args[0]));
202 int len = *((size_t*)(args[1]));
203
204 char buffer[BYTES_PER_LINE * 3];
205 char ascii_buffer[BYTES_PER_LINE + 1];
206 char *buffer_pos = buffer;
207 char *bytes_pos = bytes;
208 char *bytes_roof = bytes + len;
209 int line_start = 0;
210 int i = 0;
211 int total_written = 0;
212
213 total_written = fprintf(stream, "=> %d bytes @ %p", len, bytes);
214 if (total_written < 0)
215 {
216 return total_written;
217 }
218
219 while (bytes_pos < bytes_roof)
220 {
221 static char hexdig[] = "0123456789ABCDEF";
222
223 *buffer_pos++ = hexdig[(*bytes_pos >> 4) & 0xF];
224 *buffer_pos++ = hexdig[ *bytes_pos & 0xF];
225
226 ascii_buffer[i++] =
227 (*bytes_pos > 31 && *bytes_pos < 127) ? *bytes_pos : '.';
228
229 if (++bytes_pos == bytes_roof || i == BYTES_PER_LINE)
230 {
231 int padding = 3 * (BYTES_PER_LINE - i);
232 int written;
233
234 while (padding--)
235 {
236 *buffer_pos++ = ' ';
237 }
238 *buffer_pos++ = '\0';
239 ascii_buffer[i] = '\0';
240
241 written = fprintf(stream, "\n%4d: %s %s",
242 line_start, buffer, ascii_buffer);
243 if (written < 0)
244 {
245 return written;
246 }
247 total_written += written;
248
249 buffer_pos = buffer;
250 line_start += BYTES_PER_LINE;
251 i = 0;
252 }
253 else
254 {
255 *buffer_pos++ = ' ';
256 }
257 }
258 return total_written;
259 }
260
261 /**
262 * output handler in printf() for chunks
263 */
264 static int print_chunk(FILE *stream, const struct printf_info *info,
265 const void *const *args)
266 {
267 chunk_t *chunk = *((chunk_t**)(args[0]));
268
269 const void *new_args[] = {&chunk->ptr, &chunk->len};
270 return print_bytes(stream, info, new_args);
271 }
272
273 /**
274 * arginfo handler in printf() for chunks
275 */
276 static int print_chunk_arginfo(const struct printf_info *info, size_t n, int *argtypes)
277 {
278 if (n > 0)
279 {
280 argtypes[0] = PA_POINTER;
281 }
282 return 1;
283 }
284
285 /**
286 * arginfo handler in printf() for byte ranges
287 */
288 static int print_bytes_arginfo(const struct printf_info *info, size_t n, int *argtypes)
289 {
290 if (n > 1)
291 {
292 argtypes[0] = PA_POINTER;
293 argtypes[1] = PA_INT;
294 }
295 return 2;
296 }
297
298 /**
299 * register printf() handlers for chunk and byte ranges
300 */
301 static void __attribute__ ((constructor))print_register()
302 {
303 register_printf_function(CHUNK_PRINTF_SPEC, print_chunk, print_chunk_arginfo);
304 register_printf_function(BYTES_PRINTF_SPEC, print_bytes, print_bytes_arginfo);
305 }
306
307 /**
308 * Described in header.
309 */
310 void *clalloc(void * pointer, size_t size)
311 {
312 void *data;
313 data = malloc(size);
314
315 memcpy(data, pointer,size);
316
317 return (data);
318 }
319
320 /**
321 * We use a single mutex for all refcount variables. This
322 * is not optimal for performance, but the critical section
323 * is not that long...
324 * TODO: Consider to include a mutex in each refcount_t variable.
325 */
326 static pthread_mutex_t ref_mutex = PTHREAD_MUTEX_INITIALIZER;
327
328 /**
329 * Described in header.
330 *
331 * TODO: May be implemented with atomic CPU instructions
332 * instead of a mutex.
333 */
334 void ref_get(refcount_t *ref)
335 {
336 pthread_mutex_lock(&ref_mutex);
337 (*ref)++;
338 pthread_mutex_unlock(&ref_mutex);
339 }
340
341 /**
342 * Described in header.
343 *
344 * TODO: May be implemented with atomic CPU instructions
345 * instead of a mutex.
346 */
347 bool ref_put(refcount_t *ref)
348 {
349 bool more_refs;
350
351 pthread_mutex_lock(&ref_mutex);
352 more_refs = --(*ref);
353 pthread_mutex_unlock(&ref_mutex);
354 return !more_refs;
355 }
356
357 /*
358 * Names of the months used by timetoa()
359 */
360 static const char* months[] = {
361 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
362 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
363 };
364
365 /*
366 * Described in header file
367 */
368 void timetoa(char *buf, size_t buflen, const time_t *time, bool utc)
369 {
370 if (*time == UNDEFINED_TIME)
371 snprintf(buf, buflen, "--- -- --:--:--%s----", (utc)?" UTC ":" ");
372 else
373 {
374 struct tm *t = (utc)? gmtime(time) : localtime(time);
375
376 snprintf(buf, buflen, "%s %02d %02d:%02d:%02d%s%04d",
377 months[t->tm_mon], t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec,
378 (utc)?" UTC ":" ", t->tm_year + 1900);
379 }
380 }