]>
Commit | Line | Data |
---|---|---|
8457da75 | 1 | /* |
b9d7319f | 2 | * Copyright (C) 2015 Tobias Brunner |
8457da75 | 3 | * Copyright (C) 2014 Martin Willi |
19ef2aec TB |
4 | * |
5 | * Copyright (C) secunet Security Networks AG | |
8457da75 MW |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify it | |
8 | * under the terms of the GNU General Public License as published by the | |
9 | * Free Software Foundation; either version 2 of the License, or (at your | |
10 | * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, but | |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
14 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 | * for more details. | |
16 | */ | |
17 | ||
18 | /** | |
19 | * @defgroup vici_message vici_message | |
c605a96d | 20 | * @{ @ingroup vici |
8457da75 MW |
21 | */ |
22 | ||
23 | #ifndef VICI_MESSAGE_H_ | |
24 | #define VICI_MESSAGE_H_ | |
25 | ||
26 | #include <library.h> | |
27 | ||
28 | typedef struct vici_message_t vici_message_t; | |
dd5ce0a9 | 29 | typedef struct vici_parse_context_t vici_parse_context_t; |
8457da75 MW |
30 | typedef enum vici_type_t vici_type_t; |
31 | ||
32 | /** | |
33 | * Vici message encoding types | |
34 | */ | |
35 | enum vici_type_t { | |
dacb75f5 AS |
36 | /** never used in an argument list, needed by dump as initial value */ |
37 | VICI_START = 0, | |
38 | ||
8457da75 | 39 | /** begin of new section, argument is section name as char* */ |
dacb75f5 | 40 | VICI_SECTION_START = 1, |
8457da75 | 41 | /** end of current section, no arguments */ |
dacb75f5 | 42 | VICI_SECTION_END = 2, |
8457da75 | 43 | /** key/value, arguments are key as char*, value as chunk_t */ |
dacb75f5 | 44 | VICI_KEY_VALUE = 3, |
8457da75 | 45 | /** list start, argument is list name as char* */ |
dacb75f5 | 46 | VICI_LIST_START = 4, |
8457da75 | 47 | /** list item, argument is item value as chunk_t */ |
dacb75f5 | 48 | VICI_LIST_ITEM = 5, |
8457da75 | 49 | /** end of list, no arguments */ |
dacb75f5 | 50 | VICI_LIST_END = 6, |
8457da75 MW |
51 | |
52 | /** end of argument list, no arguments (never encoded) */ | |
dacb75f5 | 53 | VICI_END = 7 |
8457da75 MW |
54 | }; |
55 | ||
dd5ce0a9 MW |
56 | /** |
57 | * Callback function for key/value and list items, invoked by parse(). | |
58 | * | |
59 | * @param user user data, as passed to parse() | |
60 | * @param message message currently parsing | |
61 | * @param name name of key or list | |
62 | * @param value parsed value | |
63 | * @return TRUE if parsed successfully | |
64 | */ | |
65 | typedef bool (*vici_value_cb_t)(void *user, vici_message_t *message, | |
66 | char *name, chunk_t value); | |
67 | ||
68 | /** | |
69 | * Callback function for sections, invoked by parse(). | |
70 | * | |
71 | * @param user user data, as passed to parse() | |
72 | * @param message message currently parsing | |
73 | * @param ctx parse context, to pass to recursive parse() invocations. | |
74 | * @param name name of the section | |
75 | * @return TRUE if parsed successfully | |
76 | */ | |
77 | typedef bool (*vici_section_cb_t)(void *user, vici_message_t *message, | |
78 | vici_parse_context_t *ctx, char *name); | |
79 | ||
8457da75 MW |
80 | /** |
81 | * Names for vici encoding types | |
82 | */ | |
83 | extern enum_name_t *vici_type_names; | |
84 | ||
85 | /** | |
86 | * Vici message representation, encoding/decoding routines. | |
87 | */ | |
88 | struct vici_message_t { | |
89 | ||
90 | /** | |
91 | * Create an enumerator over message contents. | |
92 | * | |
93 | * The enumerator takes a fixed list of arguments, but depending on the | |
94 | * type may set not all of them. It returns VICI_END as last argument | |
95 | * to indicate the message end, and returns FALSE if parsing the message | |
96 | * failed. | |
97 | * | |
98 | * @return enumerator over (vici_type_t, char*, chunk_t) | |
99 | */ | |
100 | enumerator_t* (*create_enumerator)(vici_message_t *this); | |
101 | ||
db184955 MW |
102 | /** |
103 | * Get the value of a key/value pair as a string. | |
104 | * | |
105 | * @param def default value if not found | |
106 | * @param fmt printf style format string for key, with sections | |
107 | * @param ... arguments to fmt string | |
108 | * @return string | |
109 | */ | |
110 | char* (*get_str)(vici_message_t *this, char *def, char *fmt, ...); | |
111 | ||
112 | /** | |
113 | * Get the value of a key/value pair as a string, va_list variant. | |
114 | * | |
115 | * @param def default value if not found | |
116 | * @param fmt printf style format string for key, with sections | |
117 | * @param args arguments to fmt string | |
118 | * @return string | |
119 | */ | |
120 | char* (*vget_str)(vici_message_t *this, char *def, char *fmt, va_list args); | |
121 | ||
122 | /** | |
123 | * Get the value of a key/value pair as integer. | |
124 | * | |
125 | * @param def default value if not found | |
126 | * @param fmt printf style format string for key, with sections | |
127 | * @param ... arguments to fmt string | |
128 | * @return value | |
129 | */ | |
130 | int (*get_int)(vici_message_t *this, int def, char *fmt, ...); | |
131 | ||
132 | /** | |
133 | * Get the value of a key/value pair as integer, va_list variant | |
134 | * | |
135 | * @param def default value if not found | |
136 | * @param fmt printf style format string for key, with sections | |
137 | * @param args arguments to fmt string | |
138 | * @return value | |
139 | */ | |
140 | int (*vget_int)(vici_message_t *this, int def, char *fmt, va_list args); | |
141 | ||
b9d7319f TB |
142 | /** |
143 | * Get the value of a key/value pair as boolean. | |
144 | * | |
145 | * @param def default value if not found | |
146 | * @param fmt printf style format string for key, with sections | |
147 | * @param ... arguments to fmt string | |
148 | * @return value | |
149 | */ | |
150 | bool (*get_bool)(vici_message_t *this, bool def, char *fmt, ...); | |
151 | ||
152 | /** | |
153 | * Get the value of a key/value pair as boolean, va_list variant | |
154 | * | |
155 | * @param def default value if not found | |
156 | * @param fmt printf style format string for key, with sections | |
157 | * @param args arguments to fmt string | |
158 | * @return value | |
159 | */ | |
160 | bool (*vget_bool)(vici_message_t *this, bool def, char *fmt, va_list args); | |
161 | ||
db184955 MW |
162 | /** |
163 | * Get the raw value of a key/value pair. | |
164 | * | |
165 | * @param def default value if not found | |
166 | * @param fmt printf style format string for key, with sections | |
167 | * @param ... arguments to fmt string | |
168 | * @return value | |
169 | */ | |
170 | chunk_t (*get_value)(vici_message_t *this, chunk_t def, char *fmt, ...); | |
171 | ||
172 | /** | |
173 | * Get the raw value of a key/value pair, va_list variant. | |
174 | * | |
175 | * @param def default value if not found | |
176 | * @param fmt printf style format string for key, with sections | |
177 | * @param args arguments to fmt string | |
178 | * @return value | |
179 | */ | |
180 | chunk_t (*vget_value)(vici_message_t *this, chunk_t def, | |
181 | char *fmt, va_list args); | |
182 | ||
8457da75 MW |
183 | /** |
184 | * Get encoded message. | |
185 | * | |
186 | * @return message data, points to internal data | |
187 | */ | |
188 | chunk_t (*get_encoding)(vici_message_t *this); | |
189 | ||
dd5ce0a9 MW |
190 | /** |
191 | * Parse a message using callback functions. | |
192 | * | |
193 | * Any of the callbacks may be NULL to skip this kind of item. Callbacks are | |
194 | * invoked for the current section level only. To descent into sections, | |
195 | * call parse() from within a section callback using the provided parse | |
196 | * context. | |
197 | * | |
198 | * @param ctx parse context, NULL for root level | |
199 | * @param section callback invoked for each section | |
200 | * @param kv callback invoked for key/value pairs | |
201 | * @param li callback invoked for list items | |
202 | * @param user user data to pass to callbacks | |
203 | * @return TRUE if parsed successfully | |
204 | */ | |
205 | bool (*parse)(vici_message_t *this, vici_parse_context_t *ctx, | |
206 | vici_section_cb_t section, vici_value_cb_t kv, | |
207 | vici_value_cb_t li, void *user); | |
208 | ||
045bdf52 MW |
209 | /** |
210 | * Dump a message text representation to a FILE stream. | |
211 | * | |
dacb75f5 AS |
212 | * @param label label to print for message |
213 | * @param pretty use pretty print with indentation | |
214 | * @param out FILE stream to dump to | |
215 | * @return TRUE if message valid | |
045bdf52 | 216 | */ |
dacb75f5 | 217 | bool (*dump)(vici_message_t *this, char *label, bool pretty, FILE *out); |
045bdf52 | 218 | |
8457da75 MW |
219 | /** |
220 | * Destroy a vici_message_t. | |
221 | */ | |
222 | void (*destroy)(vici_message_t *this); | |
223 | }; | |
224 | ||
225 | /** | |
226 | * Create a vici_message from encoded data. | |
227 | * | |
228 | * @param data message encoding | |
229 | * @param cleanup TRUE to free data during | |
230 | * @return message representation | |
231 | */ | |
232 | vici_message_t *vici_message_create_from_data(chunk_t data, bool cleanup); | |
233 | ||
234 | /** | |
235 | * Create a vici_message from an enumerator. | |
236 | * | |
237 | * The enumerator uses the same signature as the enumerator returned | |
238 | * by create_enumerator(), and gets destroyed by this function. It should | |
239 | * return VICI_END to close the message, return FALSE to indicate a failure. | |
240 | * | |
241 | * @param enumerator enumerator over (vici_type_t, char*, chunk_t) | |
242 | * @return message representation, NULL on error | |
243 | */ | |
244 | vici_message_t *vici_message_create_from_enumerator(enumerator_t *enumerator); | |
245 | ||
246 | /** | |
247 | * Create vici message from a variable argument list. | |
248 | * | |
8383d626 | 249 | * @param type first type beginning message |
8457da75 MW |
250 | * @param ... vici_type_t and args, terminated by VICI_END |
251 | * @return message representation, NULL on error | |
252 | */ | |
253 | vici_message_t *vici_message_create_from_args(vici_type_t type, ...); | |
254 | ||
255 | /** | |
256 | * Check if a chunk has a printable string, and print it to buf. | |
257 | * | |
8383d626 | 258 | * @param chunk chunk containing potential string |
8457da75 MW |
259 | * @param buf buffer to write string to |
260 | * @param size size of buf | |
261 | * @return TRUE if printable and string written to buf | |
262 | */ | |
263 | bool vici_stringify(chunk_t chunk, char *buf, size_t size); | |
264 | ||
8383d626 | 265 | /** |
ed01c1af | 266 | * Verify the occurrence of a given type for given section/list nesting |
8383d626 MW |
267 | */ |
268 | bool vici_verify_type(vici_type_t type, u_int section, bool list); | |
269 | ||
8457da75 | 270 | #endif /** VICI_MESSAGE_H_ @}*/ |