chunk_to_hex() function declaration deleted
[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 * enum_names for type status_t.
110 */
111 extern enum_name_t *status_names;
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
155 /**
156 * Printf() hook character to dump a chunk using printf.
157 * The argument supplied to printf() is a pointer to a chunk.
158 * E.g. printf("chunk xy is: %B", &xy);
159 */
160 #define CHUNK_PRINTF_SPEC 'B'
161
162 /**
163 * Printf() hook character to dump a chunk using printf.
164 * Two arguments are supplied for one format string charactar,
165 * first a pointer to the buffer, and as second the length of the buffer.
166 * E.g. printf("buffer xy is: %b", buffer, sizeof(buffer));
167 */
168 #define BYTES_PRINTF_SPEC 'b'
169
170 /**
171 * printf specifier for time_t, use #-modifier to print time as UTC
172 */
173 #define TIME_PRINTF_SPEC 'T'
174
175 /**
176 * printf specifier for time_t deltas, uses two arguments
177 * E.g. printf("%V", begin, end);
178 */
179 #define TIME_DELTA_PRINTF_SPEC 'V'
180
181 /**
182 * time_t for a not defined time
183 */
184 #define UNDEFINED_TIME 0
185
186 /**
187 * Initialize a chunk to a static buffer
188 */
189 #define chunk_from_buf(str) { str, sizeof(str) }
190
191 /**
192 * Clone chunk contents in a newly allocated chunk
193 */
194 chunk_t chunk_clone(chunk_t chunk);
195
196 /**
197 * Allocate a chunk from concatenation of other chunks.
198 * mode is a string 'm' and 'c, 'm' means move chunk,
199 * 'c' means copy chunk.
200 */
201 chunk_t chunk_cat(const char* mode, ...);
202
203 /**
204 * Free contents of a chunk
205 */
206 void chunk_free(chunk_t *chunk);
207
208 /**
209 * Allocate a chunk
210 */
211 chunk_t chunk_alloc(size_t bytes);
212
213 /**
214 * Compare two chunks for equality,
215 * NULL chunks are never equal.
216 */
217 bool chunk_equals(chunk_t a, chunk_t b);
218
219 /**
220 * Compare two chunks for equality,
221 * NULL chunks are always equal.
222 */
223 bool chunk_equals_or_null(chunk_t a, chunk_t b);
224
225 /**
226 * Clone a data to a newly allocated buffer
227 */
228 void *clalloc(void *pointer, size_t size);
229
230 /**
231 * Special type to count references
232 */
233 typedef volatile u_int refcount_t;
234
235 /**
236 * @brief Get a new reference.
237 *
238 * Increments the reference counter atomic.
239 *
240 * @param ref pointer to ref counter
241 */
242 void ref_get(refcount_t *ref);
243
244 /**
245 * @brief Put back a unused reference.
246 *
247 * Decrements the reference counter atomic and
248 * says if more references available.
249 *
250 * @param ref pointer to ref counter
251 * @return TRUE if no more references counted
252 */
253 bool ref_put(refcount_t *ref);
254
255
256 #endif /*TYPES_H_*/