9874d5bc07db98e53bb5f3364a581034693d936a
[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 * Initialize a chunk to a static buffer
155 */
156 #define chunk_from_buf(str) { str, sizeof(str) }
157
158 /**
159 * Clone chunk contents in a newly allocated chunk
160 */
161 chunk_t chunk_clone(chunk_t chunk);
162
163 /**
164 * Allocate a chunk from concatenation of other chunks.
165 * mode is a string 'm' and 'c, 'm' means move chunk,
166 * 'c' means copy chunk.
167 */
168 chunk_t chunk_cat(const char* mode, ...);
169
170 /**
171 * Free contents of a chunk
172 */
173 void chunk_free(chunk_t *chunk);
174
175 /**
176 * Allocate a chunk
177 */
178 chunk_t chunk_alloc(size_t bytes);
179
180 /**
181 * Compare two chunks for equality,
182 * NULL chunks are never equal.
183 */
184 bool chunk_equals(chunk_t a, chunk_t b);
185
186 /**
187 * Compare two chunks for equality,
188 * NULL chunks are always equal.
189 */
190 bool chunk_equals_or_null(chunk_t a, chunk_t b);
191
192 /**
193 * Print a chunk in hexadecimal form
194 * with each byte separated by a colon
195 */
196 void chunk_to_hex(char *buf, size_t buflen, chunk_t chunk);
197
198 /**
199 * Clone a data to a newly allocated buffer
200 */
201 void *clalloc(void *pointer, size_t size);
202
203 /**
204 * Special type to count references
205 */
206 typedef volatile u_int refcount_t;
207
208 /**
209 * @brief Get a new reference.
210 *
211 * Increments the reference counter atomic.
212 *
213 * @param ref pointer to ref counter
214 */
215 void ref_get(refcount_t *ref);
216
217 /**
218 * @brief Put back a unused reference.
219 *
220 * Decrements the reference counter atomic and
221 * says if more references available.
222 *
223 * @param ref pointer to ref counter
224 * @return TRUE if no more references counted
225 */
226 bool ref_put(refcount_t *ref);
227
228
229 #define UNDEFINED_TIME 0
230 #define TIMETOA_BUF 30
231
232 /**
233 * @brief Display a date either in local or UTC time
234 *
235 * @param buf buffer where displayed time will be written to
236 * @param buflen buffer length
237 * @param time time to be displayed
238 * @param utc UTC (TRUE) or local time (FALSE)
239 *
240 */
241 void timetoa(char *buf, size_t buflen, const time_t *time, bool utc);
242
243 #endif /*TYPES_H_*/