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#pragma once
51#include <epan/tvbuff.h>
52#include "ws_symbol_export.h"
53
56typedef struct _tvbparse_t tvbparse_t;
57
58
59/*
60 * a callback function to be called before or after an element has been
61 * successfully extracted.
62 *
63 * Note that if the token belongs to a composed token the callbacks of the
64 * components won't be called unless the composed token is successfully
65 * extracted.
66 *
67 * tvbparse_data: the private data of the parser
68 * wanted_data: the private data of the wanted element
69 * elem: the extracted element
70 */
71typedef void (*tvbparse_action_t)(void* tvbparse_data, const void* wanted_data, struct _tvbparse_elem_t* elem);
72
73typedef int (*tvbparse_condition_t)
74(tvbparse_t*, const int,
75 const tvbparse_wanted_t*,
77
78
79typedef enum {
80 TP_UNTIL_INCLUDE, /* last elem is included, its span is spent by the parser */
81 TP_UNTIL_SPEND, /* last elem is not included, but its span is spent by the parser */
82 TP_UNTIL_LEAVE /* last elem is not included, neither its span is spent by the parser */
83} until_mode_t;
84
85
94 int id;
96 tvbparse_condition_t condition;
105 union {
106 const char* str;
108 struct {
109 union {
110 int64_t i;
111 uint64_t u;
112 double f;
113 } value;
115 enum ftenum ftenum;
116 struct {
117 until_mode_t mode;
119 } until;
120 struct {
124 } hash;
125 GPtrArray* elems;
127 void* p;
129
130 int len;
132 unsigned min;
133 unsigned max;
135 const void* data;
137 tvbparse_action_t before;
138 tvbparse_action_t after;
139};
140
157
158
159/* a matching token returned by either tvbparser_get or tvb_parser_find */
161 int id;
162
163 tvbparse_t* parser;
164 tvbuff_t* tvb;
165 int offset;
166 int len;
167
168 void* data;
169
170 struct _tvbparse_elem_t* sub;
171
172 struct _tvbparse_elem_t* next;
173 struct _tvbparse_elem_t* last;
174
175 const tvbparse_wanted_t* wanted;
176};
177
178
179/*
180 * definition of wanted token types
181 *
182 * the following functions define the tokens we will be able to look for in a tvb
183 * common parameters are:
184 *
185 * id: an arbitrary id that will be copied to the eventual token (don't use 0)
186 * private_data: persistent data to be passed to the callback action (wanted_data)
187 * before_cb: an callback function to be called before those of the subelements
188 * after_cb: an callback function to be called after those of the subelements
189 */
190
191
192/*
193 * a char element.
194 *
195 * When looked for it returns a simple element one character long if the char
196 * at the current offset matches one of the needles.
197 */
198WS_DLL_PUBLIC
199tvbparse_wanted_t* tvbparse_char(const int id,
200 const char* needles,
201 const void* private_data,
202 tvbparse_action_t before_cb,
203 tvbparse_action_t after_cb);
204
205/*
206 * a not_char element.
207 *
208 * When looked for it returns a simple element one character long if the char
209 * at the current offset does not match one of the needles.
210 */
211WS_DLL_PUBLIC
212tvbparse_wanted_t* tvbparse_not_char(const int id,
213 const char* needle,
214 const void* private_data,
215 tvbparse_action_t before_cb,
216 tvbparse_action_t after_cb);
217
218/*
219 * a chars element
220 *
221 * When looked for it returns a simple element one or more characters long if
222 * one or more char(s) starting from the current offset match one of the needles.
223 * An element will be returned if at least min_len chars are given (1 if it's 0)
224 * It will get at most max_len chars or as much as it can if max_len is 0.
225 */
226WS_DLL_PUBLIC
227tvbparse_wanted_t* tvbparse_chars(const int id,
228 const unsigned min_len,
229 const unsigned max_len,
230 const char* needles,
231 const void* private_data,
232 tvbparse_action_t before_cb,
233 tvbparse_action_t after_cb);
234
235/*
236 * a not_chars element
237 *
238 * When looked for it returns a simple element one or more characters long if
239 * one or more char(s) starting from the current offset do not match one of the
240 * needles.
241 * An element will be returned if at least min_len chars are given (1 if it's 0)
242 * It will get at most max_len chars or as much as it can if max_len is 0.
243 */
244WS_DLL_PUBLIC
245tvbparse_wanted_t* tvbparse_not_chars(const int id,
246 const unsigned min_len,
247 const unsigned max_len,
248 const char* needles,
249 const void* private_data,
250 tvbparse_action_t before_cb,
251 tvbparse_action_t after_cb);
252
253/*
254 * a string element
255 *
256 * When looked for it returns a simple element if we have the given string at
257 * the current offset
258 */
259WS_DLL_PUBLIC
260tvbparse_wanted_t* tvbparse_string(const int id,
261 const char* string,
262 const void* private_data,
263 tvbparse_action_t before_cb,
264 tvbparse_action_t after_cb);
265
266/*
267 * casestring
268 *
269 * When looked for it returns a simple element if we have a matching string at
270 * the current offset
271 */
272WS_DLL_PUBLIC
273tvbparse_wanted_t* tvbparse_casestring(const int id,
274 const char* str,
275 const void* data,
276 tvbparse_action_t before_cb,
277 tvbparse_action_t after_cb);
278
279/*
280 * until
281 *
282 * When looked for it returns a simple element containing all the characters
283 * found until the first match of the ending element if the ending element is
284 * found.
285 *
286 * When looking for until elements it calls tvbparse_find so it can be very slow.
287 *
288 * It won't have a subelement, the ending's callbacks won't get called.
289 */
290
291/*
292 * op_mode values determine how the terminating element and the current offset
293 * of the parser are handled
294 */
295WS_DLL_PUBLIC
296tvbparse_wanted_t* tvbparse_until(const int id,
297 const void* private_data,
298 tvbparse_action_t before_cb,
299 tvbparse_action_t after_cb,
300 const tvbparse_wanted_t* ending,
301 until_mode_t until_mode);
302
303/*
304 * one_of
305 *
306 * When looked for it will try to match to the given candidates and return a
307 * composed element whose subelement is the first match.
308 *
309 * The list of candidates is terminated with a NULL
310 *
311 */
312WS_DLL_PUBLIC
313tvbparse_wanted_t* tvbparse_set_oneof(const int id,
314 const void* private_data,
315 tvbparse_action_t before_cb,
316 tvbparse_action_t after_cb,
317 ...);
318
319/*
320 * hashed
321 */
322WS_DLL_PUBLIC
323tvbparse_wanted_t* tvbparse_hashed(const int id,
324 const void* data,
325 tvbparse_action_t before_cb,
326 tvbparse_action_t after_cb,
328 tvbparse_wanted_t* other,
329 ...);
330
337WS_DLL_PUBLIC
339
340/*
341 * sequence
342 *
343 * When looked for it will try to match in order all the given candidates. If
344 * every candidate is found in the given order it will return a composed
345 * element whose subelements are the matched elements.
346 *
347 * The list of candidates is terminated with a NULL.
348 *
349 */
350WS_DLL_PUBLIC
351tvbparse_wanted_t* tvbparse_set_seq(const int id,
352 const void* private_data,
353 tvbparse_action_t before_cb,
354 tvbparse_action_t after_cb,
355 ...);
356
372WS_DLL_PUBLIC
373tvbparse_wanted_t* tvbparse_some(const int id,
374 const unsigned min,
375 const unsigned max,
376 const void* data,
377 tvbparse_action_t before_cb,
378 tvbparse_action_t after_cb,
379 const tvbparse_wanted_t* wanted);
380
381#define tvbparse_one_or_more(id, private_data, before_cb, after_cb, wanted)\
382 tvbparse_some(id, 1, INT_MAX, private_data, before_cb, after_cb, wanted)
383
384
385/*
386 * handle
387 *
388 * this is a pointer to a pointer to a wanted element (that might have not
389 * been initialized yet) so that recursive structures
390 */
391WS_DLL_PUBLIC
392tvbparse_wanted_t* tvbparse_handle(tvbparse_wanted_t** handle);
393
410WS_DLL_PUBLIC
412 const void* data,
413 tvbparse_action_t before_cb,
414 tvbparse_action_t after_cb,
415 const char quote,
416 const char escape);
417
418/*
419 * a helper callback for quoted strings that will shrink the token to contain
420 * only the string and not the quotes
421 */
432WS_DLL_PUBLIC
433void tvbparse_shrink_token_cb(void* tvbparse_data,
434 const void* wanted_data,
435 tvbparse_elem_t* tok);
436
437
438
439
459WS_DLL_PUBLIC
461 tvbuff_t* tvb,
462 const int offset,
463 int len,
464 void* private_data,
465 const tvbparse_wanted_t* ignore);
466
475WS_DLL_PUBLIC
476bool tvbparse_reset(tvbparse_t* tt, const unsigned offset, unsigned len);
477
484WS_DLL_PUBLIC
485 unsigned tvbparse_curr_offset(tvbparse_t* tt);
486unsigned tvbparse_len_left(tvbparse_t* tt);
487
488
489
502WS_DLL_PUBLIC
504 const tvbparse_wanted_t* wanted);
505
518WS_DLL_PUBLIC
520 const tvbparse_wanted_t* wanted);
521
533WS_DLL_PUBLIC
535 const tvbparse_wanted_t* wanted);
536
543WS_DLL_PUBLIC
Definition proto.h:902
Definition tvbparse.h:160
Represents an instance of a per-packet parser for tvbuff data.
Definition tvbparse.h:148
int end_offset
Definition tvbparse.h:152
wmem_allocator_t * scope
Definition tvbparse.h:149
int recursion_depth
Definition tvbparse.h:155
void * data
Definition tvbparse.h:153
int offset
Definition tvbparse.h:151
const tvbparse_wanted_t * ignore
Definition tvbparse.h:154
tvbuff_t * tvb
Definition tvbparse.h:150
Describes a parsing rule or expectation for a tvbuff parser.
Definition tvbparse.h:93
const tvbparse_wanted_t * subelem
Definition tvbparse.h:118
unsigned max
Definition tvbparse.h:133
struct _tvbparse_wanted_t ** handle
Definition tvbparse.h:107
double f
Definition tvbparse.h:112
struct _tvbparse_wanted_t * key
Definition tvbparse.h:122
wmem_map_t * table
Definition tvbparse.h:121
int len
Definition tvbparse.h:130
const void * data
Definition tvbparse.h:135
tvbparse_action_t after
Definition tvbparse.h:138
struct _tvbparse_wanted_t::@496::@497 number
void * p
Definition tvbparse.h:127
GPtrArray * elems
Definition tvbparse.h:125
uint64_t u
Definition tvbparse.h:111
enum ftenum ftenum
Definition tvbparse.h:115
tvbparse_action_t before
Definition tvbparse.h:137
const char * str
Definition tvbparse.h:106
unsigned min
Definition tvbparse.h:132
tvbparse_condition_t condition
Definition tvbparse.h:96
struct _tvbparse_wanted_t * other
Definition tvbparse.h:123
until_mode_t mode
Definition tvbparse.h:117
union _tvbparse_wanted_t::@496 control
Control parameters for the parsing rule.
int id
Definition tvbparse.h:94
int64_t i
Definition tvbparse.h:110
Internal memory allocator interface used by the wmem subsystem.
Definition wmem_allocator.h:34
Definition wmem_map.c:60
Definition tvbuff-int.h:33
WS_DLL_PUBLIC void tvbparse_hashed_add(tvbparse_wanted_t *w,...)
Adds a hashed element to the wanted list.
Definition tvbparse.c:562
WS_DLL_PUBLIC void tvbparse_shrink_token_cb(void *tvbparse_data, const void *wanted_data, tvbparse_elem_t *tok)
Callback function to shrink token length and offset.
WS_DLL_PUBLIC tvbparse_t * tvbparse_init(wmem_allocator_t *scope, tvbuff_t *tvb, const int offset, int len, void *private_data, const tvbparse_wanted_t *ignore)
Initialize a new TVB parser.
Definition tvbparse.c:871
WS_DLL_PUBLIC unsigned tvbparse_curr_offset(tvbparse_t *tt)
Get the current offset in the TVB parse structure.
Definition tvbparse.c:911
WS_DLL_PUBLIC bool tvbparse_peek(tvbparse_t *tt, const tvbparse_wanted_t *wanted)
Peeks at the next token in the buffer without advancing the parser.
Definition tvbparse.c:951
WS_DLL_PUBLIC void tvbparse_tree_add_elem(proto_tree *tree, tvbparse_elem_t *curr)
Adds an element to a protocol tree.
Definition tvbparse.c:1056
WS_DLL_PUBLIC tvbparse_elem_t * tvbparse_find(tvbparse_t *tt, const tvbparse_wanted_t *wanted)
Finds an element in a TVB parse structure based on a given condition.
Definition tvbparse.c:1018
WS_DLL_PUBLIC bool tvbparse_reset(tvbparse_t *tt, const unsigned offset, unsigned len)
Resets the token buffer parser to a new offset and length.
Definition tvbparse.c:894
WS_DLL_PUBLIC tvbparse_wanted_t * tvbparse_some(const int id, const unsigned min, const unsigned max, const void *data, tvbparse_action_t before_cb, tvbparse_action_t after_cb, const tvbparse_wanted_t *wanted)
Creates a parsing element that matches a given candidate a specified number of times.
Definition tvbparse.c:732
WS_DLL_PUBLIC tvbparse_elem_t * tvbparse_get(tvbparse_t *tt, const tvbparse_wanted_t *wanted)
Retrieves a token based on the specified conditions.
Definition tvbparse.c:983
WS_DLL_PUBLIC tvbparse_wanted_t * tvbparse_quoted(const int id, const void *data, tvbparse_action_t before_cb, tvbparse_action_t after_cb, const char quote, const char escape)
Parses quoted strings in a given data buffer.
Definition tvbparse.c:835