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