implementation of a simple "token enumerator"
[strongswan.git] / src / libstrongswan / utils.h
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 *
15 * $Id$
16 */
17
18 /**
19 * @defgroup utils utils
20 * @{ @ingroup libstrongswan
21 */
22
23 #ifndef UTILS_H_
24 #define UTILS_H_
25
26 #include <sys/types.h>
27 #include <stdlib.h>
28 #include <stddef.h>
29
30 #include <enum.h>
31
32 /**
33 * Number of bits in a byte
34 */
35 #define BITS_PER_BYTE 8
36
37 /**
38 * Default length for various auxiliary text buffers
39 */
40 #define BUF_LEN 512
41
42 /**
43 * Macro compares two strings for equality
44 */
45 #define streq(x,y) (strcmp(x, y) == 0)
46
47 /**
48 * Macro compares two strings for equality
49 */
50 #define strneq(x,y,len) (strncmp(x, y, len) == 0)
51
52 /**
53 * Macro compares two binary blobs for equality
54 */
55 #define memeq(x,y,len) (memcmp(x, y, len) == 0)
56
57 /**
58 * Macro gives back larger of two values.
59 */
60 #define max(x,y) ((x) > (y) ? (x):(y))
61
62 /**
63 * Macro gives back smaller of two values.
64 */
65 #define min(x,y) ((x) < (y) ? (x):(y))
66
67 /**
68 * Call destructor of an object, if object != NULL
69 */
70 #define DESTROY_IF(obj) if (obj) (obj)->destroy(obj)
71
72 /**
73 * Call offset destructor of an object, if object != NULL
74 */
75 #define DESTROY_OFFSET_IF(obj, offset) if (obj) obj->destroy_offset(obj, offset);
76
77 /**
78 * Call function destructor of an object, if object != NULL
79 */
80 #define DESTROY_FUNCTION_IF(obj, fn) if (obj) obj->destroy_function(obj, fn);
81
82 /**
83 * Debug macro to follow control flow
84 */
85 #define POS printf("%s, line %d\n", __FILE__, __LINE__)
86
87 /**
88 * Macro to allocate a sized type.
89 */
90 #define malloc_thing(thing) ((thing*)malloc(sizeof(thing)))
91
92 /**
93 * Get the number of elements in an array
94 */
95 #define countof(array) (sizeof(array)/sizeof(array[0]))
96
97 /**
98 * Assign a function as a class method
99 */
100 #define ASSIGN(method, function) (method = (typeof(method))function)
101
102 /**
103 * time_t not defined
104 */
105 #define UNDEFINED_TIME 0
106
107 /**
108 * General purpose boolean type.
109 */
110 typedef int bool;
111 #define FALSE 0
112 #define TRUE 1
113
114 typedef enum status_t status_t;
115
116 /**
117 * Return values of function calls.
118 */
119 enum status_t {
120 /**
121 * Call succeeded.
122 */
123 SUCCESS,
124
125 /**
126 * Call failed.
127 */
128 FAILED,
129
130 /**
131 * Out of resources.
132 */
133 OUT_OF_RES,
134
135 /**
136 * The suggested operation is already done
137 */
138 ALREADY_DONE,
139
140 /**
141 * Not supported.
142 */
143 NOT_SUPPORTED,
144
145 /**
146 * One of the arguments is invalid.
147 */
148 INVALID_ARG,
149
150 /**
151 * Something could not be found.
152 */
153 NOT_FOUND,
154
155 /**
156 * Error while parsing.
157 */
158 PARSE_ERROR,
159
160 /**
161 * Error while verifying.
162 */
163 VERIFY_ERROR,
164
165 /**
166 * Object in invalid state.
167 */
168 INVALID_STATE,
169
170 /**
171 * Destroy object which called method belongs to.
172 */
173 DESTROY_ME,
174
175 /**
176 * Another call to the method is required.
177 */
178 NEED_MORE,
179 };
180
181 /**
182 * enum_names for type status_t.
183 */
184 extern enum_name_t *status_names;
185
186 /**
187 * deprecated pluto style return value:
188 * error message, NULL for success
189 */
190 typedef const char *err_t;
191
192 /**
193 * Handle struct timeval like an own type.
194 */
195 typedef struct timeval timeval_t;
196
197 /**
198 * Handle struct timespec like an own type.
199 */
200 typedef struct timespec timespec_t;
201
202 /**
203 * Handle struct chunk_t like an own type.
204 */
205 typedef struct sockaddr sockaddr_t;
206
207 /**
208 * Clone a data to a newly allocated buffer
209 */
210 void *clalloc(void *pointer, size_t size);
211
212 /**
213 * Same as memcpy, but XORs src into dst instead of copy
214 */
215 void memxor(u_int8_t dest[], u_int8_t src[], size_t n);
216
217 /**
218 * returns null
219 */
220 void *return_null();
221
222 /**
223 * No-Operation function
224 */
225 void nop();
226
227 /**
228 * Special type to count references
229 */
230 typedef volatile u_int refcount_t;
231
232 /**
233 * Get a new reference.
234 *
235 * Increments the reference counter atomic.
236 *
237 * @param ref pointer to ref counter
238 */
239 void ref_get(refcount_t *ref);
240
241 /**
242 * Put back a unused reference.
243 *
244 * Decrements the reference counter atomic and
245 * says if more references available.
246 *
247 * @param ref pointer to ref counter
248 * @return TRUE if no more references counted
249 */
250 bool ref_put(refcount_t *ref);
251
252 /**
253 * Get printf hooks for time.
254 *
255 * Arguments are:
256 * time_t* time
257 * Arguments using #-specificer
258 * time_t* time, bool utc
259 */
260 printf_hook_functions_t time_get_printf_hooks();
261
262 /**
263 * Get printf hooks for time deltas.
264 *
265 * Arguments are:
266 * time_t* delta
267 * Arguments using #-specificer
268 * time_t* begin, time_t* end
269 */
270 printf_hook_functions_t time_delta_get_printf_hooks();
271
272 /**
273 * Get printf hooks for time deltas.
274 *
275 * Arguments are:
276 * u_char *ptr, int len
277 */
278 printf_hook_functions_t mem_get_printf_hooks();
279
280 #endif /* UTILS_H_ @}*/