(no commit message)
[strongswan.git] / src / libstrongswan / types.h
1 /**
2 * @file types.h
3 *
4 * @brief Generic types.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
22 */
23
24
25 #ifndef TYPES_H_
26 #define TYPES_H_
27
28 #include <gmp.h>
29 #include <sys/types.h>
30 #include <stdlib.h>
31
32 #include <definitions.h>
33
34 /**
35 * General purpose boolean type.
36 */
37 typedef int bool;
38 #define FALSE 0
39 #define TRUE 1
40
41 /**
42 * error message, or NULL for success
43 */
44 typedef const char *err_t;
45
46 typedef enum status_t status_t;
47
48 /**
49 * Return values of function calls.
50 */
51 enum status_t {
52 /**
53 * Call succeeded.
54 */
55 SUCCESS,
56
57 /**
58 * Call failed.
59 */
60 FAILED,
61
62 /**
63 * Out of resources.
64 */
65 OUT_OF_RES,
66
67 /**
68 * The suggested operation is already done
69 */
70 ALREADY_DONE,
71
72 /**
73 * Not supported.
74 */
75 NOT_SUPPORTED,
76
77 /**
78 * One of the arguments is invalid.
79 */
80 INVALID_ARG,
81
82 /**
83 * Something could not be found.
84 */
85 NOT_FOUND,
86
87 /**
88 * Error while parsing.
89 */
90 PARSE_ERROR,
91
92 /**
93 * Error while verifying.
94 */
95 VERIFY_ERROR,
96
97 /**
98 * Object in invalid state.
99 */
100 INVALID_STATE,
101
102 /**
103 * Destroy object which called method belongs to.
104 */
105 DESTROY_ME,
106 };
107
108 /**
109 * String mappings for type status_t.
110 */
111 extern mapping_t status_m[];
112
113 /**
114 * Handle struct timeval like an own type.
115 */
116 typedef struct timeval timeval_t;
117
118 /**
119 * Handle struct timespec like an own type.
120 */
121 typedef struct timespec timespec_t;
122
123 /**
124 * Handle struct chunk_t like an own type.
125 */
126 typedef struct sockaddr sockaddr_t;
127
128 /**
129 * Use struct chunk_t as chunk_t.
130 */
131 typedef struct chunk_t chunk_t;
132
133 /**
134 * General purpose pointer/length abstraction.
135 */
136 struct chunk_t {
137 /**
138 * Pointer to start of data
139 */
140 u_char *ptr;
141
142 /**
143 * Length of data in bytes
144 */
145 size_t len;
146 };
147
148 /**
149 * used to initialize a chunk to { NULL, 0 }.
150 */
151 extern chunk_t CHUNK_INITIALIZER;
152
153 /**
154 * Printf() hook character to dump a chunk using printf.
155 * The argument supplied to printf() is a pointer to a chunk.
156 * E.g. printf("chunk xy is: %B", &xy);
157 */
158 #define CHUNK_PRINTF_SPEC 'B'
159
160 /**
161 * Printf() hook character to dump a chunk using printf.
162 * Two arguments are supplied for one format string charactar,
163 * first a pointer to the buffer, and as second the length of the buffer.
164 * E.g. printf("buffer xy is: %b", buffer, sizeof(buffer));
165 */
166 #define BYTES_PRINTF_SPEC 'b'
167
168 /**
169 * Initialize a chunk to a static buffer
170 */
171 #define chunk_from_buf(str) { str, sizeof(str) }
172
173 /**
174 * Clone chunk contents in a newly allocated chunk
175 */
176 chunk_t chunk_clone(chunk_t chunk);
177
178 /**
179 * Allocate a chunk from concatenation of other chunks.
180 * mode is a string 'm' and 'c, 'm' means move chunk,
181 * 'c' means copy chunk.
182 */
183 chunk_t chunk_cat(const char* mode, ...);
184
185 /**
186 * Free contents of a chunk
187 */
188 void chunk_free(chunk_t *chunk);
189
190 /**
191 * Allocate a chunk
192 */
193 chunk_t chunk_alloc(size_t bytes);
194
195 /**
196 * Compare two chunks for equality,
197 * NULL chunks are never equal.
198 */
199 bool chunk_equals(chunk_t a, chunk_t b);
200
201 /**
202 * Compare two chunks for equality,
203 * NULL chunks are always equal.
204 */
205 bool chunk_equals_or_null(chunk_t a, chunk_t b);
206
207 /**
208 * Print a chunk in hexadecimal form
209 * with each byte separated by a colon
210 */
211 void chunk_to_hex(char *buf, size_t buflen, chunk_t chunk);
212
213 /**
214 * Clone a data to a newly allocated buffer
215 */
216 void *clalloc(void *pointer, size_t size);
217
218 /**
219 * Special type to count references
220 */
221 typedef volatile u_int refcount_t;
222
223 /**
224 * @brief Get a new reference.
225 *
226 * Increments the reference counter atomic.
227 *
228 * @param ref pointer to ref counter
229 */
230 void ref_get(refcount_t *ref);
231
232 /**
233 * @brief Put back a unused reference.
234 *
235 * Decrements the reference counter atomic and
236 * says if more references available.
237 *
238 * @param ref pointer to ref counter
239 * @return TRUE if no more references counted
240 */
241 bool ref_put(refcount_t *ref);
242
243
244 #define UNDEFINED_TIME 0
245 #define TIMETOA_BUF 30
246
247 /**
248 * @brief Display a date either in local or UTC time
249 *
250 * @param buf buffer where displayed time will be written to
251 * @param buflen buffer length
252 * @param time time to be displayed
253 * @param utc UTC (TRUE) or local time (FALSE)
254 *
255 */
256 void timetoa(char *buf, size_t buflen, const time_t *time, bool utc);
257
258 #endif /*TYPES_H_*/