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