74f0cbf7ae6621caf6b6d2bd36d6026199623c3d
[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 * String mappings for type status_t.
127 */
128 extern mapping_t status_m[];
129
130 /**
131 * Handle struct timeval like an own type.
132 */
133 typedef struct timeval timeval_t;
134
135 /**
136 * Handle struct timespec like an own type.
137 */
138 typedef struct timespec timespec_t;
139
140 /**
141 * Handle struct chunk_t like an own type.
142 */
143 typedef struct sockaddr sockaddr_t;
144
145 /**
146 * Use struct chunk_t as chunk_t.
147 */
148 typedef struct chunk_t chunk_t;
149
150 /**
151 * General purpose pointer/length abstraction.
152 */
153 struct chunk_t {
154 /**
155 * Pointer to start of data
156 */
157 u_char *ptr;
158
159 /**
160 * Length of data in bytes
161 */
162 size_t len;
163 };
164
165 /**
166 * {NULL, 0}-chunk, handy for initialization
167 * of chunks.
168 */
169 extern chunk_t CHUNK_INITIALIZER;
170
171 /**
172 * Initialize a chunk to a static buffer
173 */
174 #define chunk_from_buf(str) { str, sizeof(str) }
175
176 /**
177 * Clone chunk contents in a newly allocated chunk
178 */
179 chunk_t chunk_clone(chunk_t chunk);
180
181 /**
182 * Free contents of a chunk
183 */
184 void chunk_free(chunk_t *chunk);
185
186 /**
187 * Allocate a chunk
188 */
189 chunk_t chunk_alloc(size_t bytes);
190
191 /**
192 * Compare two chunks for equality,
193 * NULL chunks are never equal.
194 */
195 bool chunk_equals(chunk_t a, chunk_t b);
196
197 /**
198 * Print a chunk in hexadecimal form
199 * with each byte separated by a colon
200 */
201 void chunk_to_hex(char *buf, size_t buflen, chunk_t chunk);
202
203 /**
204 * Clone a data to a newly allocated buffer
205 */
206 void *clalloc(void *pointer, size_t size);
207
208 #define UNDEFINED_TIME 0
209 #define TIMETOA_BUF 30
210
211 /**
212 * @brief Display a date either in local or UTC time
213 *
214 * @param buf buffer where displayed time will be written to
215 * @param buflen buffer length
216 * @param time time to be displayed
217 * @param utc UTC (TRUE) or local time (FALSE)
218 *
219 */
220 void timetoa(char *buf, size_t buflen, const time_t *time, bool utc);
221
222 #endif /*TYPES_H_*/