2ca0e4c5c4294a4c41284fa1817d3011e7fcebb0
[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
30 #include "types.h"
31
32
33 /**
34 * String mappings for type status_t.
35 */
36 mapping_t status_m[] = {
37 {SUCCESS, "SUCCESS"},
38 {FAILED, "FAILED"},
39 {OUT_OF_RES, "OUT_OF_RES"},
40 {ALREADY_DONE, "ALREADY_DONE"},
41 {NOT_SUPPORTED, "NOT_SUPPORTED"},
42 {INVALID_ARG, "INVALID_ARG"},
43 {NOT_FOUND, "NOT_FOUND"},
44 {PARSE_ERROR, "PARSE_ERROR"},
45 {VERIFY_ERROR, "VERIFY_ERROR"},
46 {INVALID_STATE, "INVALID_STATE"},
47 {DESTROY_ME, "DESTROY_ME"},
48 {MAPPING_END, NULL}
49 };
50
51 /**
52 * Empty chunk.
53 */
54 chunk_t CHUNK_INITIALIZER = { NULL, 0 };
55
56 /**
57 * Described in header.
58 */
59 chunk_t chunk_clone(chunk_t chunk)
60 {
61 chunk_t clone = CHUNK_INITIALIZER;
62
63 if (chunk.ptr && chunk.len > 0)
64 {
65 clone.ptr = malloc(chunk.len);
66 clone.len = chunk.len;
67 memcpy(clone.ptr, chunk.ptr, chunk.len);
68 }
69
70 return clone;
71 }
72
73 /**
74 * Decribed in header.
75 */
76 chunk_t chunk_cat(const char* mode, ...)
77 {
78 chunk_t construct;
79 va_list chunks;
80 u_char *pos;
81 int i;
82 int count = strlen(mode);
83
84 /* sum up lengths of individual chunks */
85 va_start(chunks, mode);
86 construct.len = 0;
87 for (i = 0; i < count; i++)
88 {
89 chunk_t ch = va_arg(chunks, chunk_t);
90 construct.len += ch.len;
91 }
92 va_end(chunks);
93
94 /* allocate needed memory for construct */
95 construct.ptr = malloc(construct.len);
96 pos = construct.ptr;
97
98 /* copy or move the chunks */
99 va_start(chunks, mode);
100 for (i = 0; i < count; i++)
101 {
102 chunk_t ch = va_arg(chunks, chunk_t);
103 switch (*mode++)
104 {
105 case 'm':
106 memcpy(pos, ch.ptr, ch.len);
107 pos += ch.len;
108 free(ch.ptr);
109 break;
110 case 'c':
111 default:
112 memcpy(pos, ch.ptr, ch.len);
113 pos += ch.len;
114 }
115 }
116 va_end(chunks);
117
118 return construct;
119 }
120
121 /**
122 * Described in header.
123 */
124 void chunk_free(chunk_t *chunk)
125 {
126 free(chunk->ptr);
127 chunk->ptr = NULL;
128 chunk->len = 0;
129 }
130
131 /**
132 * Described in header.
133 */
134 chunk_t chunk_alloc(size_t bytes)
135 {
136 chunk_t new_chunk;
137 new_chunk.ptr = malloc(bytes);
138 new_chunk.len = bytes;
139 return new_chunk;
140 }
141
142 /**
143 * Described in header.
144 */
145 bool chunk_equals(chunk_t a, chunk_t b)
146 {
147 return a.ptr != NULL && b.ptr != NULL &&
148 a.len == b.len && memeq(a.ptr, b.ptr, a.len);
149 }
150
151 /**
152 * Described in header.
153 */
154 bool chunk_equals_or_null(chunk_t a, chunk_t b)
155 {
156 if (a.ptr == NULL || b.ptr == NULL)
157 return TRUE;
158 return a.len == b.len && memeq(a.ptr, b.ptr, a.len);
159 }
160
161 /**
162 * Described in header.
163 */
164 void chunk_to_hex(char *buf, size_t buflen, chunk_t chunk)
165 {
166 bool first = TRUE;
167
168 buflen--; /* reserve space for null termination */
169
170 while (chunk.len > 0 && buflen > 2)
171 {
172 static char hexdig[] = "0123456789abcdef";
173
174 if (first)
175 {
176 first = FALSE;
177 }
178 else
179 {
180 *buf++ = ':'; buflen--;
181 }
182 *buf++ = hexdig[(*chunk.ptr >> 4) & 0x0f];
183 *buf++ = hexdig[ *chunk.ptr++ & 0x0f];
184 buflen -= 2; chunk.len--;
185 }
186 *buf = '\0';
187 }
188
189 /**
190 * Described in header.
191 */
192 void *clalloc(void * pointer, size_t size)
193 {
194 void *data;
195 data = malloc(size);
196
197 memcpy(data, pointer,size);
198
199 return (data);
200 }
201
202 /**
203 * We use a single mutex for all refcount variables. This
204 * is not optimal for performance, but the critical section
205 * is not that long...
206 * TODO: Consider to include a mutex in each refcount_t variable.
207 */
208 static pthread_mutex_t ref_mutex = PTHREAD_MUTEX_INITIALIZER;
209
210 /**
211 * Described in header.
212 *
213 * TODO: May be implemented with atomic CPU instructions
214 * instead of a mutex.
215 */
216 void ref_get(refcount_t *ref)
217 {
218 pthread_mutex_lock(&ref_mutex);
219 (*ref)++;
220 pthread_mutex_unlock(&ref_mutex);
221 }
222
223 /**
224 * Described in header.
225 *
226 * TODO: May be implemented with atomic CPU instructions
227 * instead of a mutex.
228 */
229 bool ref_put(refcount_t *ref)
230 {
231 bool more_refs;
232
233 pthread_mutex_lock(&ref_mutex);
234 more_refs = --(*ref);
235 pthread_mutex_unlock(&ref_mutex);
236 return !more_refs;
237 }
238
239 /*
240 * Names of the months used by timetoa()
241 */
242 static const char* months[] = {
243 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
244 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
245 };
246
247 /*
248 * Described in header file
249 */
250 void timetoa(char *buf, size_t buflen, const time_t *time, bool utc)
251 {
252 if (*time == UNDEFINED_TIME)
253 snprintf(buf, buflen, "--- -- --:--:--%s----", (utc)?" UTC ":" ");
254 else
255 {
256 struct tm *t = (utc)? gmtime(time) : localtime(time);
257
258 snprintf(buf, buflen, "%s %02d %02d:%02d:%02d%s%04d",
259 months[t->tm_mon], t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec,
260 (utc)?" UTC ":" ", t->tm_year + 1900);
261 }
262 }