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