14aee2c0206f18e844079368db4685c984fd9477
[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 ressources.
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 * String mappings for type status_t.
115 */
116 extern mapping_t status_m[];
117
118 /**
119 * Handle struct timeval like an own type.
120 */
121 typedef struct timeval timeval_t;
122
123 /**
124 * Handle struct timespec like an own type.
125 */
126 typedef struct timespec timespec_t;
127
128 /**
129 * Handle struct chunk_t like an own type.
130 */
131 typedef struct sockaddr sockaddr_t;
132
133 /**
134 * Use struct chunk_t as chunk_t.
135 */
136 typedef struct chunk_t chunk_t;
137
138 /**
139 * General purpose pointer/length abstraction.
140 */
141 struct chunk_t {
142 /**
143 * Pointer to start of data
144 */
145 u_char *ptr;
146
147 /**
148 * Length of data in bytes
149 */
150 size_t len;
151 };
152
153 /**
154 * {NULL, 0}-chunk, handy for initialization
155 * of chunks.
156 */
157 extern chunk_t CHUNK_INITIALIZER;
158
159 /**
160 * Initialize a chunk to a static buffer
161 */
162 #define chunk_from_buf(str) { str, sizeof(str) }
163
164 /**
165 * Clone chunk contents in a newly allocated chunk
166 */
167 chunk_t chunk_clone(chunk_t chunk);
168
169 /**
170 * Free contents of a chunk
171 */
172 void chunk_free(chunk_t *chunk);
173
174 /**
175 * Allocate a chunk
176 */
177 chunk_t chunk_alloc(size_t bytes);
178
179 /**
180 * Compare two chunks for equality,
181 * NULL chunks are never equal.
182 */
183 bool chunk_equals(chunk_t a, chunk_t b);
184
185 /**
186 * Print a chunk in hexadecimal form
187 * with each byte separated by a colon
188 */
189 void chunk_to_hex(char *buf, size_t buflen, chunk_t chunk);
190
191 /**
192 * Clone a data to a newly allocated buffer
193 */
194 void *clalloc(void *pointer, size_t size);
195
196
197 #endif /*TYPES_H_*/