ed67913cf3cb1bbcc4ec9a366e9cb4913b7fc66d
[strongswan.git] / src / libstrongswan / types.h
1 /**
2 * @file types.h
3 *
4 * @brief Generic types.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, 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
24 #ifndef TYPES_H_
25 #define TYPES_H_
26
27 #include <gmp.h>
28 #include <sys/types.h>
29 #include <stdlib.h>
30
31 #include <definitions.h>
32
33 /**
34 * General purpose boolean type.
35 */
36 typedef int bool;
37 #define FALSE 0
38 #define TRUE 1
39
40 /**
41 * error message, or NULL for success
42 */
43 typedef const char *err_t;
44
45 typedef enum status_t status_t;
46
47 /**
48 * Return values of function calls.
49 */
50 enum status_t {
51 /**
52 * Call succeeded.
53 */
54 SUCCESS,
55
56 /**
57 * Call failed.
58 */
59 FAILED,
60
61 /**
62 * Out of resources.
63 */
64 OUT_OF_RES,
65
66 /**
67 * The suggested operation is already done
68 */
69 ALREADY_DONE,
70
71 /**
72 * Not supported.
73 */
74 NOT_SUPPORTED,
75
76 /**
77 * One of the arguments is invalid.
78 */
79 INVALID_ARG,
80
81 /**
82 * Something could not be found.
83 */
84 NOT_FOUND,
85
86 /**
87 * Error while parsing.
88 */
89 PARSE_ERROR,
90
91 /**
92 * Error while verifying.
93 */
94 VERIFY_ERROR,
95
96 /**
97 * Object in invalid state.
98 */
99 INVALID_STATE,
100
101 /**
102 * Destroy object which called method belongs to.
103 */
104 DESTROY_ME,
105
106 /**
107 * An object got created.
108 */
109 CREATED,
110 };
111
112
113 /**
114 * Certificate sending policy
115 */
116 typedef enum certpolicy {
117 CERT_ALWAYS_SEND = 0,
118 CERT_SEND_IF_ASKED = 1,
119 CERT_NEVER_SEND = 2,
120
121 CERT_YES_SEND = 3, /* synonym for CERT_ALWAYS_SEND */
122 CERT_NO_SEND = 4 /* synonym for CERT_NEVER_SEND */
123 } certpolicy_t;
124
125 /**
126 * RFC 2459 CRL reason codes
127 */
128
129 /* TODO extern enum_names crl_reason_names; */
130
131 typedef enum {
132 REASON_UNSPECIFIED = 0,
133 REASON_KEY_COMPROMISE = 1,
134 REASON_CA_COMPROMISE = 2,
135 REASON_AFFILIATION_CHANGED = 3,
136 REASON_SUPERSEDED = 4,
137 REASON_CESSATION_OF_OPERATON = 5,
138 REASON_CERTIFICATE_HOLD = 6,
139 REASON_REMOVE_FROM_CRL = 8
140 } crl_reason_t;
141
142 /**
143 * String mappings for type status_t.
144 */
145 extern mapping_t status_m[];
146
147 /**
148 * Handle struct timeval like an own type.
149 */
150 typedef struct timeval timeval_t;
151
152 /**
153 * Handle struct timespec like an own type.
154 */
155 typedef struct timespec timespec_t;
156
157 /**
158 * Handle struct chunk_t like an own type.
159 */
160 typedef struct sockaddr sockaddr_t;
161
162 /**
163 * Use struct chunk_t as chunk_t.
164 */
165 typedef struct chunk_t chunk_t;
166
167 /**
168 * General purpose pointer/length abstraction.
169 */
170 struct chunk_t {
171 /**
172 * Pointer to start of data
173 */
174 u_char *ptr;
175
176 /**
177 * Length of data in bytes
178 */
179 size_t len;
180 };
181
182 /**
183 * used to initialize a chunk to { NULL, 0 }.
184 */
185 extern chunk_t CHUNK_INITIALIZER;
186
187 /**
188 * Initialize a chunk to a static buffer
189 */
190 #define chunk_from_buf(str) { str, sizeof(str) }
191
192 /**
193 * Clone chunk contents in a newly allocated chunk
194 */
195 chunk_t chunk_clone(chunk_t chunk);
196
197 /**
198 * Free contents of a chunk
199 */
200 void chunk_free(chunk_t *chunk);
201
202 /**
203 * Allocate a chunk
204 */
205 chunk_t chunk_alloc(size_t bytes);
206
207 /**
208 * Compare two chunks for equality,
209 * NULL chunks are never equal.
210 */
211 bool chunk_equals(chunk_t a, chunk_t b);
212
213 /**
214 * Compare two chunks for equality,
215 * NULL chunks are always equal.
216 */
217 bool chunk_equals_or_null(chunk_t a, chunk_t b);
218
219 /**
220 * Print a chunk in hexadecimal form
221 * with each byte separated by a colon
222 */
223 void chunk_to_hex(char *buf, size_t buflen, chunk_t chunk);
224
225 /**
226 * Clone a data to a newly allocated buffer
227 */
228 void *clalloc(void *pointer, size_t size);
229
230 #define UNDEFINED_TIME 0
231 #define TIMETOA_BUF 30
232
233 /**
234 * @brief Display a date either in local or UTC time
235 *
236 * @param buf buffer where displayed time will be written to
237 * @param buflen buffer length
238 * @param time time to be displayed
239 * @param utc UTC (TRUE) or local time (FALSE)
240 *
241 */
242 void timetoa(char *buf, size_t buflen, const time_t *time, bool utc);
243
244 #endif /*TYPES_H_*/