2 * Copyright (C) 2015 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
5 * Copyright (C) 2014 Martin Willi
6 * Copyright (C) 2014 revosec AG
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * @defgroup vici_message vici_message
24 #ifndef VICI_MESSAGE_H_
25 #define VICI_MESSAGE_H_
29 typedef struct vici_message_t vici_message_t
;
30 typedef struct vici_parse_context_t vici_parse_context_t
;
31 typedef enum vici_type_t vici_type_t
;
34 * Vici message encoding types
37 /** never used in an argument list, needed by dump as initial value */
40 /** begin of new section, argument is section name as char* */
41 VICI_SECTION_START
= 1,
42 /** end of current section, no arguments */
44 /** key/value, arguments are key as char*, value as chunk_t */
46 /** list start, argument is list name as char* */
48 /** list item, argument is item value as chunk_t */
50 /** end of list, no arguments */
53 /** end of argument list, no arguments (never encoded) */
58 * Callback function for key/value and list items, invoked by parse().
60 * @param user user data, as passed to parse()
61 * @param message message currently parsing
62 * @param name name of key or list
63 * @param value parsed value
64 * @return TRUE if parsed successfully
66 typedef bool (*vici_value_cb_t
)(void *user
, vici_message_t
*message
,
67 char *name
, chunk_t value
);
70 * Callback function for sections, invoked by parse().
72 * @param user user data, as passed to parse()
73 * @param message message currently parsing
74 * @param ctx parse context, to pass to recursive parse() invocations.
75 * @param name name of the section
76 * @return TRUE if parsed successfully
78 typedef bool (*vici_section_cb_t
)(void *user
, vici_message_t
*message
,
79 vici_parse_context_t
*ctx
, char *name
);
82 * Names for vici encoding types
84 extern enum_name_t
*vici_type_names
;
87 * Vici message representation, encoding/decoding routines.
89 struct vici_message_t
{
92 * Create an enumerator over message contents.
94 * The enumerator takes a fixed list of arguments, but depending on the
95 * type may set not all of them. It returns VICI_END as last argument
96 * to indicate the message end, and returns FALSE if parsing the message
99 * @return enumerator over (vici_type_t, char*, chunk_t)
101 enumerator_t
* (*create_enumerator
)(vici_message_t
*this);
104 * Get the value of a key/value pair as a string.
106 * @param def default value if not found
107 * @param fmt printf style format string for key, with sections
108 * @param ... arguments to fmt string
111 char* (*get_str
)(vici_message_t
*this, char *def
, char *fmt
, ...);
114 * Get the value of a key/value pair as a string, va_list variant.
116 * @param def default value if not found
117 * @param fmt printf style format string for key, with sections
118 * @param args arguments to fmt string
121 char* (*vget_str
)(vici_message_t
*this, char *def
, char *fmt
, va_list args
);
124 * Get the value of a key/value pair as integer.
126 * @param def default value if not found
127 * @param fmt printf style format string for key, with sections
128 * @param ... arguments to fmt string
131 int (*get_int
)(vici_message_t
*this, int def
, char *fmt
, ...);
134 * Get the value of a key/value pair as integer, va_list variant
136 * @param def default value if not found
137 * @param fmt printf style format string for key, with sections
138 * @param args arguments to fmt string
141 int (*vget_int
)(vici_message_t
*this, int def
, char *fmt
, va_list args
);
144 * Get the value of a key/value pair as boolean.
146 * @param def default value if not found
147 * @param fmt printf style format string for key, with sections
148 * @param ... arguments to fmt string
151 bool (*get_bool
)(vici_message_t
*this, bool def
, char *fmt
, ...);
154 * Get the value of a key/value pair as boolean, va_list variant
156 * @param def default value if not found
157 * @param fmt printf style format string for key, with sections
158 * @param args arguments to fmt string
161 bool (*vget_bool
)(vici_message_t
*this, bool def
, char *fmt
, va_list args
);
164 * Get the raw value of a key/value pair.
166 * @param def default value if not found
167 * @param fmt printf style format string for key, with sections
168 * @param ... arguments to fmt string
171 chunk_t (*get_value
)(vici_message_t
*this, chunk_t def
, char *fmt
, ...);
174 * Get the raw value of a key/value pair, va_list variant.
176 * @param def default value if not found
177 * @param fmt printf style format string for key, with sections
178 * @param args arguments to fmt string
181 chunk_t (*vget_value
)(vici_message_t
*this, chunk_t def
,
182 char *fmt
, va_list args
);
185 * Get encoded message.
187 * @return message data, points to internal data
189 chunk_t (*get_encoding
)(vici_message_t
*this);
192 * Parse a message using callback functions.
194 * Any of the callbacks may be NULL to skip this kind of item. Callbacks are
195 * invoked for the current section level only. To descent into sections,
196 * call parse() from within a section callback using the provided parse
199 * @param ctx parse context, NULL for root level
200 * @param section callback invoked for each section
201 * @param kv callback invoked for key/value pairs
202 * @param li callback invoked for list items
203 * @param user user data to pass to callbacks
204 * @return TRUE if parsed successfully
206 bool (*parse
)(vici_message_t
*this, vici_parse_context_t
*ctx
,
207 vici_section_cb_t section
, vici_value_cb_t kv
,
208 vici_value_cb_t li
, void *user
);
211 * Dump a message text representation to a FILE stream.
213 * @param label label to print for message
214 * @param pretty use pretty print with indentation
215 * @param out FILE stream to dump to
216 * @return TRUE if message valid
218 bool (*dump
)(vici_message_t
*this, char *label
, bool pretty
, FILE *out
);
221 * Destroy a vici_message_t.
223 void (*destroy
)(vici_message_t
*this);
227 * Create a vici_message from encoded data.
229 * @param data message encoding
230 * @param cleanup TRUE to free data during
231 * @return message representation
233 vici_message_t
*vici_message_create_from_data(chunk_t data
, bool cleanup
);
236 * Create a vici_message from an enumerator.
238 * The enumerator uses the same signature as the enumerator returned
239 * by create_enumerator(), and gets destroyed by this function. It should
240 * return VICI_END to close the message, return FALSE to indicate a failure.
242 * @param enumerator enumerator over (vici_type_t, char*, chunk_t)
243 * @return message representation, NULL on error
245 vici_message_t
*vici_message_create_from_enumerator(enumerator_t
*enumerator
);
248 * Create vici message from a variable argument list.
250 * @param type first type beginning message
251 * @param ... vici_type_t and args, terminated by VICI_END
252 * @return message representation, NULL on error
254 vici_message_t
*vici_message_create_from_args(vici_type_t type
, ...);
257 * Check if a chunk has a printable string, and print it to buf.
259 * @param chunk chunk containing potential string
260 * @param buf buffer to write string to
261 * @param size size of buf
262 * @return TRUE if printable and string written to buf
264 bool vici_stringify(chunk_t chunk
, char *buf
, size_t size
);
267 * Verify the occurrence of a given type for given section/list nesting
269 bool vici_verify_type(vici_type_t type
, u_int section
, bool list
);
271 #endif /** VICI_MESSAGE_H_ @}*/