Wireshark 4.7.0
The Wireshark network protocol analyzer
Loading...
Searching...
No Matches
tvbparse.h
Go to the documentation of this file.
1
14/*
15 The intention behind this is to ease the writing of dissectors that have to
16 parse text without the need of writing into buffers.
17
18 It was originally written to avoid using lex and yacc for the xml dissector.
19
20 the parser is able to look for wanted elements these can be:
21
22 simple tokens:
23 - a char out of a string of needles
24 - a char not belonging to a string of needles
25 - a sequence of chars that belong to a set of chars
26 - a sequence of chars that do not belong to a set of chars
27 - a string
28 - a caseless string
29 - all the characters up to a certain wanted element (included or excluded)
30
31 composed elements:
32 - one of a given group of wanted elements
33 - a sequence of wanted elements
34 - some (at least one) instances of a wanted element
35
36 Once a wanted element is successfully extracted, by either tvbparse_get or
37 tvbparse_find, the parser will invoke a given callback
38 before and another one after every of its component's subelement's callbacks
39 are being called.
40
41 If tvbparse_get or tvbparse_find fail to extract the wanted element the
42 subelements callbacks are not going to be invoked.
43
44 The wanted elements are instantiated once by the proto_register_xxx function.
45
46 The parser is instantiated for every packet and it maintains its state.
47
48 The element's data is destroyed before the next packet is dissected.
49 */
50
51#ifndef _TVB_PARSE_H_
52#define _TVB_PARSE_H_
53
54#include <epan/tvbuff.h>
55#include "ws_symbol_export.h"
56
59typedef struct _tvbparse_t tvbparse_t;
60
61
62/*
63 * a callback function to be called before or after an element has been
64 * successfully extracted.
65 *
66 * Note that if the token belongs to a composed token the callbacks of the
67 * components won't be called unless the composed token is successfully
68 * extracted.
69 *
70 * tvbparse_data: the private data of the parser
71 * wanted_data: the private data of the wanted element
72 * elem: the extracted element
73 */
74typedef void (*tvbparse_action_t)(void* tvbparse_data, const void* wanted_data, struct _tvbparse_elem_t* elem);
75
76typedef int (*tvbparse_condition_t)
77(tvbparse_t*, const int,
78 const tvbparse_wanted_t*,
80
81
82typedef enum {
83 TP_UNTIL_INCLUDE, /* last elem is included, its span is spent by the parser */
84 TP_UNTIL_SPEND, /* last elem is not included, but its span is spent by the parser */
85 TP_UNTIL_LEAVE /* last elem is not included, neither its span is spent by the parser */
86} until_mode_t;
87
88
97 int id;
99 tvbparse_condition_t condition;
108 union {
109 const char* str;
111 struct {
112 union {
113 int64_t i;
114 uint64_t u;
115 double f;
116 } value;
118 enum ftenum ftenum;
119 struct {
120 until_mode_t mode;
122 } until;
123 struct {
127 } hash;
128 GPtrArray* elems;
130 void* p;
132
133 int len;
135 unsigned min;
136 unsigned max;
138 const void* data;
140 tvbparse_action_t before;
141 tvbparse_action_t after;
142};
143
160
161
162/* a matching token returned by either tvbparser_get or tvb_parser_find */
164 int id;
165
166 tvbparse_t* parser;
167 tvbuff_t* tvb;
168 int offset;
169 int len;
170
171 void* data;
172
173 struct _tvbparse_elem_t* sub;
174
175 struct _tvbparse_elem_t* next;
176 struct _tvbparse_elem_t* last;
177
178 const tvbparse_wanted_t* wanted;
179};
180
181
182/*
183 * definition of wanted token types
184 *
185 * the following functions define the tokens we will be able to look for in a tvb
186 * common parameters are:
187 *
188 * id: an arbitrary id that will be copied to the eventual token (don't use 0)
189 * private_data: persistent data to be passed to the callback action (wanted_data)
190 * before_cb: an callback function to be called before those of the subelements
191 * after_cb: an callback function to be called after those of the subelements
192 */
193
194
195/*
196 * a char element.
197 *
198 * When looked for it returns a simple element one character long if the char
199 * at the current offset matches one of the needles.
200 */
201WS_DLL_PUBLIC
202tvbparse_wanted_t* tvbparse_char(const int id,
203 const char* needles,
204 const void* private_data,
205 tvbparse_action_t before_cb,
206 tvbparse_action_t after_cb);
207
208/*
209 * a not_char element.
210 *
211 * When looked for it returns a simple element one character long if the char
212 * at the current offset does not match one of the needles.
213 */
214WS_DLL_PUBLIC
215tvbparse_wanted_t* tvbparse_not_char(const int id,
216 const char* needle,
217 const void* private_data,
218 tvbparse_action_t before_cb,
219 tvbparse_action_t after_cb);
220
221/*
222 * a chars element
223 *
224 * When looked for it returns a simple element one or more characters long if
225 * one or more char(s) starting from the current offset match one of the needles.
226 * An element will be returned if at least min_len chars are given (1 if it's 0)
227 * It will get at most max_len chars or as much as it can if max_len is 0.
228 */
229WS_DLL_PUBLIC
230tvbparse_wanted_t* tvbparse_chars(const int id,
231 const unsigned min_len,
232 const unsigned max_len,
233 const char* needles,
234 const void* private_data,
235 tvbparse_action_t before_cb,
236 tvbparse_action_t after_cb);
237
238/*
239 * a not_chars element
240 *
241 * When looked for it returns a simple element one or more characters long if
242 * one or more char(s) starting from the current offset do not match one of the
243 * needles.
244 * An element will be returned if at least min_len chars are given (1 if it's 0)
245 * It will get at most max_len chars or as much as it can if max_len is 0.
246 */
247WS_DLL_PUBLIC
248tvbparse_wanted_t* tvbparse_not_chars(const int id,
249 const unsigned min_len,
250 const unsigned max_len,
251 const char* needles,
252 const void* private_data,
253 tvbparse_action_t before_cb,
254 tvbparse_action_t after_cb);
255
256/*
257 * a string element
258 *
259 * When looked for it returns a simple element if we have the given string at
260 * the current offset
261 */
262WS_DLL_PUBLIC
263tvbparse_wanted_t* tvbparse_string(const int id,
264 const char* string,
265 const void* private_data,
266 tvbparse_action_t before_cb,
267 tvbparse_action_t after_cb);
268
269/*
270 * casestring
271 *
272 * When looked for it returns a simple element if we have a matching string at
273 * the current offset
274 */
275WS_DLL_PUBLIC
276tvbparse_wanted_t* tvbparse_casestring(const int id,
277 const char* str,
278 const void* data,
279 tvbparse_action_t before_cb,
280 tvbparse_action_t after_cb);
281
282/*
283 * until
284 *
285 * When looked for it returns a simple element containing all the characters
286 * found until the first match of the ending element if the ending element is
287 * found.
288 *
289 * When looking for until elements it calls tvbparse_find so it can be very slow.
290 *
291 * It won't have a subelement, the ending's callbacks won't get called.
292 */
293
294/*
295 * op_mode values determine how the terminating element and the current offset
296 * of the parser are handled
297 */
298WS_DLL_PUBLIC
299tvbparse_wanted_t* tvbparse_until(const int id,
300 const void* private_data,
301 tvbparse_action_t before_cb,
302 tvbparse_action_t after_cb,
303 const tvbparse_wanted_t* ending,
304 until_mode_t until_mode);
305
306/*
307 * one_of
308 *
309 * When looked for it will try to match to the given candidates and return a
310 * composed element whose subelement is the first match.
311 *
312 * The list of candidates is terminated with a NULL
313 *
314 */
315WS_DLL_PUBLIC
316tvbparse_wanted_t* tvbparse_set_oneof(const int id,
317 const void* private_data,
318 tvbparse_action_t before_cb,
319 tvbparse_action_t after_cb,
320 ...);
321
322/*
323 * hashed
324 */
325WS_DLL_PUBLIC
326tvbparse_wanted_t* tvbparse_hashed(const int id,
327 const void* data,
328 tvbparse_action_t before_cb,
329 tvbparse_action_t after_cb,
331 tvbparse_wanted_t* other,
332 ...);
333
334WS_DLL_PUBLIC
335void tvbparse_hashed_add(tvbparse_wanted_t* w, ...);
336
337/*
338 * sequence
339 *
340 * When looked for it will try to match in order all the given candidates. If
341 * every candidate is found in the given order it will return a composed
342 * element whose subelements are the matched elements.
343 *
344 * The list of candidates is terminated with a NULL.
345 *
346 */
347WS_DLL_PUBLIC
348tvbparse_wanted_t* tvbparse_set_seq(const int id,
349 const void* private_data,
350 tvbparse_action_t before_cb,
351 tvbparse_action_t after_cb,
352 ...);
353
354/*
355 * some
356 *
357 * When looked for it will try to match the given candidate at least min times
358 * and at most max times. If the given candidate is matched at least min times
359 * a composed element is returned.
360 *
361 */
362WS_DLL_PUBLIC
363tvbparse_wanted_t* tvbparse_some(const int id,
364 const unsigned min,
365 const unsigned max,
366 const void* private_data,
367 tvbparse_action_t before_cb,
368 tvbparse_action_t after_cb,
369 const tvbparse_wanted_t* wanted);
370
371#define tvbparse_one_or_more(id, private_data, before_cb, after_cb, wanted)\
372 tvbparse_some(id, 1, INT_MAX, private_data, before_cb, after_cb, wanted)
373
374
375/*
376 * handle
377 *
378 * this is a pointer to a pointer to a wanted element (that might have not
379 * been initialized yet) so that recursive structures
380 */
381WS_DLL_PUBLIC
382tvbparse_wanted_t* tvbparse_handle(tvbparse_wanted_t** handle);
383
384/* quoted
385 * this is a composed candidate, that will try to match a quoted string
386 * (included the quotes) including into it every escaped quote.
387 *
388 * C strings are matched with tvbparse_quoted(-1,NULL,NULL,NULL,"\"","\\")
389 */
390WS_DLL_PUBLIC
391tvbparse_wanted_t* tvbparse_quoted(const int id,
392 const void* data,
393 tvbparse_action_t before_cb,
394 tvbparse_action_t after_cb,
395 const char quote,
396 const char escape);
397
398/*
399 * a helper callback for quoted strings that will shrink the token to contain
400 * only the string and not the quotes
401 */
402WS_DLL_PUBLIC
403void tvbparse_shrink_token_cb(void* tvbparse_data,
404 const void* wanted_data,
405 tvbparse_elem_t* tok);
406
407
408
409
410/* initialize the parser (at every packet)
411 * scope: memory scope/pool
412 * tvb: what are we parsing?
413 * offset: from where
414 * len: for how many bytes
415 * private_data: will be passed to the action callbacks
416 * ignore: a wanted token type to be ignored (the associated cb WILL be called when it matches)
417 */
418WS_DLL_PUBLIC
419tvbparse_t* tvbparse_init(wmem_allocator_t *scope,
420 tvbuff_t* tvb,
421 const int offset,
422 int len,
423 void* private_data,
424 const tvbparse_wanted_t* ignore);
425
426/* reset the parser */
427WS_DLL_PUBLIC
428bool tvbparse_reset(tvbparse_t* tt, const int offset, int len);
429
430WS_DLL_PUBLIC
431unsigned tvbparse_curr_offset(tvbparse_t* tt);
432unsigned tvbparse_len_left(tvbparse_t* tt);
433
434
435
436/*
437 * This will look for the wanted token at the current offset or after any given
438 * number of ignored tokens returning false if there's no match or true if there
439 * is a match.
440 * The parser will be left in its original state and no callbacks will be called.
441 */
442WS_DLL_PUBLIC
443bool tvbparse_peek(tvbparse_t* tt,
444 const tvbparse_wanted_t* wanted);
445
446/*
447 * This will look for the wanted token at the current offset or after any given
448 * number of ignored tokens returning NULL if there's no match.
449 * if there is a match it will set the offset of the current parser after
450 * the end of the token
451 */
452WS_DLL_PUBLIC
453tvbparse_elem_t* tvbparse_get(tvbparse_t* tt,
454 const tvbparse_wanted_t* wanted);
455
456/*
457 * Like tvbparse_get but this will look for a wanted token even beyond the
458 * current offset.
459 * This function is slow.
460 */
461WS_DLL_PUBLIC
462tvbparse_elem_t* tvbparse_find(tvbparse_t* tt,
463 const tvbparse_wanted_t* wanted);
464
465
466WS_DLL_PUBLIC
467void tvbparse_tree_add_elem(proto_tree* tree, tvbparse_elem_t* curr);
468
469#endif
Definition proto.h:907
Definition tvbparse.h:163
Represents an instance of a per-packet parser for tvbuff data.
Definition tvbparse.h:151
int end_offset
Definition tvbparse.h:155
wmem_allocator_t * scope
Definition tvbparse.h:152
int recursion_depth
Definition tvbparse.h:158
void * data
Definition tvbparse.h:156
int offset
Definition tvbparse.h:154
const tvbparse_wanted_t * ignore
Definition tvbparse.h:157
tvbuff_t * tvb
Definition tvbparse.h:153
Describes a parsing rule or expectation for a tvbuff parser.
Definition tvbparse.h:96
const tvbparse_wanted_t * subelem
Definition tvbparse.h:121
unsigned max
Definition tvbparse.h:136
struct _tvbparse_wanted_t ** handle
Definition tvbparse.h:110
double f
Definition tvbparse.h:115
struct _tvbparse_wanted_t * key
Definition tvbparse.h:125
wmem_map_t * table
Definition tvbparse.h:124
int len
Definition tvbparse.h:133
const void * data
Definition tvbparse.h:138
tvbparse_action_t after
Definition tvbparse.h:141
struct _tvbparse_wanted_t::@496::@497 number
void * p
Definition tvbparse.h:130
GPtrArray * elems
Definition tvbparse.h:128
uint64_t u
Definition tvbparse.h:114
enum ftenum ftenum
Definition tvbparse.h:118
tvbparse_action_t before
Definition tvbparse.h:140
const char * str
Definition tvbparse.h:109
unsigned min
Definition tvbparse.h:135
tvbparse_condition_t condition
Definition tvbparse.h:99
struct _tvbparse_wanted_t * other
Definition tvbparse.h:126
until_mode_t mode
Definition tvbparse.h:120
union _tvbparse_wanted_t::@496 control
Control parameters for the parsing rule.
int id
Definition tvbparse.h:97
int64_t i
Definition tvbparse.h:113
Internal memory allocator interface used by the wmem subsystem.
Definition wmem_allocator.h:34
Definition wmem_map.c:60
Definition tvbuff-int.h:35