added options for virtual IP, UDP encapsulation, IPComp
[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 #ifndef FALSE
112 # define FALSE 0
113 #endif /* FALSE */
114 #ifndef TRUE
115 # define TRUE 1
116 #endif /* TRUE */
117
118 typedef enum status_t status_t;
119
120 /**
121 * Return values of function calls.
122 */
123 enum status_t {
124 /**
125 * Call succeeded.
126 */
127 SUCCESS,
128
129 /**
130 * Call failed.
131 */
132 FAILED,
133
134 /**
135 * Out of resources.
136 */
137 OUT_OF_RES,
138
139 /**
140 * The suggested operation is already done
141 */
142 ALREADY_DONE,
143
144 /**
145 * Not supported.
146 */
147 NOT_SUPPORTED,
148
149 /**
150 * One of the arguments is invalid.
151 */
152 INVALID_ARG,
153
154 /**
155 * Something could not be found.
156 */
157 NOT_FOUND,
158
159 /**
160 * Error while parsing.
161 */
162 PARSE_ERROR,
163
164 /**
165 * Error while verifying.
166 */
167 VERIFY_ERROR,
168
169 /**
170 * Object in invalid state.
171 */
172 INVALID_STATE,
173
174 /**
175 * Destroy object which called method belongs to.
176 */
177 DESTROY_ME,
178
179 /**
180 * Another call to the method is required.
181 */
182 NEED_MORE,
183 };
184
185 /**
186 * enum_names for type status_t.
187 */
188 extern enum_name_t *status_names;
189
190 /**
191 * deprecated pluto style return value:
192 * error message, NULL for success
193 */
194 typedef const char *err_t;
195
196 /**
197 * Handle struct timeval like an own type.
198 */
199 typedef struct timeval timeval_t;
200
201 /**
202 * Handle struct timespec like an own type.
203 */
204 typedef struct timespec timespec_t;
205
206 /**
207 * Handle struct chunk_t like an own type.
208 */
209 typedef struct sockaddr sockaddr_t;
210
211 /**
212 * Clone a data to a newly allocated buffer
213 */
214 void *clalloc(void *pointer, size_t size);
215
216 /**
217 * Same as memcpy, but XORs src into dst instead of copy
218 */
219 void memxor(u_int8_t dest[], u_int8_t src[], size_t n);
220
221 /**
222 * returns null
223 */
224 void *return_null();
225
226 /**
227 * No-Operation function
228 */
229 void nop();
230
231 /**
232 * Special type to count references
233 */
234 typedef volatile u_int refcount_t;
235
236 /**
237 * Get a new reference.
238 *
239 * Increments the reference counter atomic.
240 *
241 * @param ref pointer to ref counter
242 */
243 void ref_get(refcount_t *ref);
244
245 /**
246 * Put back a unused reference.
247 *
248 * Decrements the reference counter atomic and
249 * says if more references available.
250 *
251 * @param ref pointer to ref counter
252 * @return TRUE if no more references counted
253 */
254 bool ref_put(refcount_t *ref);
255
256 /**
257 * Get printf hooks for time.
258 *
259 * Arguments are:
260 * time_t* time
261 * Arguments using #-specificer
262 * time_t* time, bool utc
263 */
264 printf_hook_functions_t time_get_printf_hooks();
265
266 /**
267 * Get printf hooks for time deltas.
268 *
269 * Arguments are:
270 * time_t* delta
271 * Arguments using #-specificer
272 * time_t* begin, time_t* end
273 */
274 printf_hook_functions_t time_delta_get_printf_hooks();
275
276 /**
277 * Get printf hooks for time deltas.
278 *
279 * Arguments are:
280 * u_char *ptr, int len
281 */
282 printf_hook_functions_t mem_get_printf_hooks();
283
284 #endif /* UTILS_H_ @}*/