vici_message.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Copyright (C) 2015 Tobias Brunner
  3. * HSR Hochschule fuer Technik Rapperswil
  4. *
  5. * Copyright (C) 2014 Martin Willi
  6. * Copyright (C) 2014 revosec AG
  7. *
  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>.
  12. *
  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
  16. * for more details.
  17. */
  18. /**
  19. * @defgroup vici_message vici_message
  20. * @{ @ingroup vici
  21. */
  22. #ifndef VICI_MESSAGE_H_
  23. #define VICI_MESSAGE_H_
  24. #include <library.h>
  25. typedef struct vici_message_t vici_message_t;
  26. typedef struct vici_parse_context_t vici_parse_context_t;
  27. typedef enum vici_type_t vici_type_t;
  28. /**
  29. * Vici message encoding types
  30. */
  31. enum vici_type_t {
  32. /** never used in an argument list, needed by dump as initial value */
  33. VICI_START = 0,
  34. /** begin of new section, argument is section name as char* */
  35. VICI_SECTION_START = 1,
  36. /** end of current section, no arguments */
  37. VICI_SECTION_END = 2,
  38. /** key/value, arguments are key as char*, value as chunk_t */
  39. VICI_KEY_VALUE = 3,
  40. /** list start, argument is list name as char* */
  41. VICI_LIST_START = 4,
  42. /** list item, argument is item value as chunk_t */
  43. VICI_LIST_ITEM = 5,
  44. /** end of list, no arguments */
  45. VICI_LIST_END = 6,
  46. /** end of argument list, no arguments (never encoded) */
  47. VICI_END = 7
  48. };
  49. /**
  50. * Callback function for key/value and list items, invoked by parse().
  51. *
  52. * @param user user data, as passed to parse()
  53. * @param message message currently parsing
  54. * @param name name of key or list
  55. * @param value parsed value
  56. * @return TRUE if parsed successfully
  57. */
  58. typedef bool (*vici_value_cb_t)(void *user, vici_message_t *message,
  59. char *name, chunk_t value);
  60. /**
  61. * Callback function for sections, invoked by parse().
  62. *
  63. * @param user user data, as passed to parse()
  64. * @param message message currently parsing
  65. * @param ctx parse context, to pass to recursive parse() invocations.
  66. * @param name name of the section
  67. * @return TRUE if parsed successfully
  68. */
  69. typedef bool (*vici_section_cb_t)(void *user, vici_message_t *message,
  70. vici_parse_context_t *ctx, char *name);
  71. /**
  72. * Names for vici encoding types
  73. */
  74. extern enum_name_t *vici_type_names;
  75. /**
  76. * Vici message representation, encoding/decoding routines.
  77. */
  78. struct vici_message_t {
  79. /**
  80. * Create an enumerator over message contents.
  81. *
  82. * The enumerator takes a fixed list of arguments, but depending on the
  83. * type may set not all of them. It returns VICI_END as last argument
  84. * to indicate the message end, and returns FALSE if parsing the message
  85. * failed.
  86. *
  87. * @return enumerator over (vici_type_t, char*, chunk_t)
  88. */
  89. enumerator_t* (*create_enumerator)(vici_message_t *this);
  90. /**
  91. * Get the value of a key/value pair as a string.
  92. *
  93. * @param def default value if not found
  94. * @param fmt printf style format string for key, with sections
  95. * @param ... arguments to fmt string
  96. * @return string
  97. */
  98. char* (*get_str)(vici_message_t *this, char *def, char *fmt, ...);
  99. /**
  100. * Get the value of a key/value pair as a string, va_list variant.
  101. *
  102. * @param def default value if not found
  103. * @param fmt printf style format string for key, with sections
  104. * @param args arguments to fmt string
  105. * @return string
  106. */
  107. char* (*vget_str)(vici_message_t *this, char *def, char *fmt, va_list args);
  108. /**
  109. * Get the value of a key/value pair as integer.
  110. *
  111. * @param def default value if not found
  112. * @param fmt printf style format string for key, with sections
  113. * @param ... arguments to fmt string
  114. * @return value
  115. */
  116. int (*get_int)(vici_message_t *this, int def, char *fmt, ...);
  117. /**
  118. * Get the value of a key/value pair as integer, va_list variant
  119. *
  120. * @param def default value if not found
  121. * @param fmt printf style format string for key, with sections
  122. * @param args arguments to fmt string
  123. * @return value
  124. */
  125. int (*vget_int)(vici_message_t *this, int def, char *fmt, va_list args);
  126. /**
  127. * Get the value of a key/value pair as boolean.
  128. *
  129. * @param def default value if not found
  130. * @param fmt printf style format string for key, with sections
  131. * @param ... arguments to fmt string
  132. * @return value
  133. */
  134. bool (*get_bool)(vici_message_t *this, bool def, char *fmt, ...);
  135. /**
  136. * Get the value of a key/value pair as boolean, va_list variant
  137. *
  138. * @param def default value if not found
  139. * @param fmt printf style format string for key, with sections
  140. * @param args arguments to fmt string
  141. * @return value
  142. */
  143. bool (*vget_bool)(vici_message_t *this, bool def, char *fmt, va_list args);
  144. /**
  145. * Get the raw value of a key/value pair.
  146. *
  147. * @param def default value if not found
  148. * @param fmt printf style format string for key, with sections
  149. * @param ... arguments to fmt string
  150. * @return value
  151. */
  152. chunk_t (*get_value)(vici_message_t *this, chunk_t def, char *fmt, ...);
  153. /**
  154. * Get the raw value of a key/value pair, va_list variant.
  155. *
  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
  159. * @return value
  160. */
  161. chunk_t (*vget_value)(vici_message_t *this, chunk_t def,
  162. char *fmt, va_list args);
  163. /**
  164. * Get encoded message.
  165. *
  166. * @return message data, points to internal data
  167. */
  168. chunk_t (*get_encoding)(vici_message_t *this);
  169. /**
  170. * Parse a message using callback functions.
  171. *
  172. * Any of the callbacks may be NULL to skip this kind of item. Callbacks are
  173. * invoked for the current section level only. To descent into sections,
  174. * call parse() from within a section callback using the provided parse
  175. * context.
  176. *
  177. * @param ctx parse context, NULL for root level
  178. * @param section callback invoked for each section
  179. * @param kv callback invoked for key/value pairs
  180. * @param li callback invoked for list items
  181. * @param user user data to pass to callbacks
  182. * @return TRUE if parsed successfully
  183. */
  184. bool (*parse)(vici_message_t *this, vici_parse_context_t *ctx,
  185. vici_section_cb_t section, vici_value_cb_t kv,
  186. vici_value_cb_t li, void *user);
  187. /**
  188. * Dump a message text representation to a FILE stream.
  189. *
  190. * @param label label to print for message
  191. * @param pretty use pretty print with indentation
  192. * @param out FILE stream to dump to
  193. * @return TRUE if message valid
  194. */
  195. bool (*dump)(vici_message_t *this, char *label, bool pretty, FILE *out);
  196. /**
  197. * Destroy a vici_message_t.
  198. */
  199. void (*destroy)(vici_message_t *this);
  200. };
  201. /**
  202. * Create a vici_message from encoded data.
  203. *
  204. * @param data message encoding
  205. * @param cleanup TRUE to free data during
  206. * @return message representation
  207. */
  208. vici_message_t *vici_message_create_from_data(chunk_t data, bool cleanup);
  209. /**
  210. * Create a vici_message from an enumerator.
  211. *
  212. * The enumerator uses the same signature as the enumerator returned
  213. * by create_enumerator(), and gets destroyed by this function. It should
  214. * return VICI_END to close the message, return FALSE to indicate a failure.
  215. *
  216. * @param enumerator enumerator over (vici_type_t, char*, chunk_t)
  217. * @return message representation, NULL on error
  218. */
  219. vici_message_t *vici_message_create_from_enumerator(enumerator_t *enumerator);
  220. /**
  221. * Create vici message from a variable argument list.
  222. *
  223. * @param type first type beginning message
  224. * @param ... vici_type_t and args, terminated by VICI_END
  225. * @return message representation, NULL on error
  226. */
  227. vici_message_t *vici_message_create_from_args(vici_type_t type, ...);
  228. /**
  229. * Check if a chunk has a printable string, and print it to buf.
  230. *
  231. * @param chunk chunk containing potential string
  232. * @param buf buffer to write string to
  233. * @param size size of buf
  234. * @return TRUE if printable and string written to buf
  235. */
  236. bool vici_stringify(chunk_t chunk, char *buf, size_t size);
  237. /**
  238. * Verify the occurrence of a given type for given section/list nesting
  239. */
  240. bool vici_verify_type(vici_type_t type, u_int section, bool list);
  241. #endif /** VICI_MESSAGE_H_ @}*/