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
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;
119 struct {
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
182
183
184/*
185 * definition of wanted token types
186 *
187 * the following functions define the tokens we will be able to look for in a tvb
188 * common parameters are:
189 *
190 * id: an arbitrary id that will be copied to the eventual token (don't use 0)
191 * private_data: persistent data to be passed to the callback action (wanted_data)
192 * before_cb: an callback function to be called before those of the subelements
193 * after_cb: an callback function to be called after those of the subelements
194 */
195
196
210WS_DLL_PUBLIC
211tvbparse_wanted_t *tvbparse_char(const int id,
212 const char *needles,
213 const void *private_data,
214 tvbparse_action_t before_cb,
215 tvbparse_action_t after_cb);
216
217
231WS_DLL_PUBLIC
233 const char *needle,
234 const void *private_data,
235 tvbparse_action_t before_cb,
236 tvbparse_action_t after_cb);
237
256WS_DLL_PUBLIC
258 const unsigned min_len,
259 const unsigned max_len,
260 const char *needles,
261 const void *private_data,
262 tvbparse_action_t before_cb,
263 tvbparse_action_t after_cb);
264
284WS_DLL_PUBLIC
286 const unsigned min_len,
287 const unsigned max_len,
288 const char *needles,
289 const void *private_data,
290 tvbparse_action_t before_cb,
291 tvbparse_action_t after_cb);
292
306WS_DLL_PUBLIC
308 const char *string,
309 const void *private_data,
310 tvbparse_action_t before_cb,
311 tvbparse_action_t after_cb);
312
326WS_DLL_PUBLIC
328 const char *str,
329 const void *data,
330 tvbparse_action_t before_cb,
331 tvbparse_action_t after_cb);
332
354WS_DLL_PUBLIC
356 const void *private_data,
357 tvbparse_action_t before_cb,
358 tvbparse_action_t after_cb,
359 const tvbparse_wanted_t *ending,
360 until_mode_t until_mode);
361
378WS_DLL_PUBLIC
380 const void *private_data,
381 tvbparse_action_t before_cb,
382 tvbparse_action_t after_cb,
383 ...);
384
399WS_DLL_PUBLIC
401 const void *data,
402 tvbparse_action_t before_cb,
403 tvbparse_action_t after_cb,
405 tvbparse_wanted_t *other,
406 ...);
407
414WS_DLL_PUBLIC
416
434WS_DLL_PUBLIC
436 const void *private_data,
437 tvbparse_action_t before_cb,
438 tvbparse_action_t after_cb,
439 ...);
440
457WS_DLL_PUBLIC
458tvbparse_wanted_t* tvbparse_some(const int id,
459 const unsigned min,
460 const unsigned max,
461 const void* data,
462 tvbparse_action_t before_cb,
463 tvbparse_action_t after_cb,
465
466#define tvbparse_one_or_more(id, private_data, before_cb, after_cb, wanted)\
467 tvbparse_some(id, 1, INT_MAX, private_data, before_cb, after_cb, wanted)
468
469
483WS_DLL_PUBLIC
485
502WS_DLL_PUBLIC
504 const void* data,
505 tvbparse_action_t before_cb,
506 tvbparse_action_t after_cb,
507 const char quote,
508 const char escape);
509
510/*
511 * a helper callback for quoted strings that will shrink the token to contain
512 * only the string and not the quotes
513 */
524WS_DLL_PUBLIC
525void tvbparse_shrink_token_cb(void* tvbparse_data,
526 const void* wanted_data,
527 tvbparse_elem_t* tok);
528
529
530
531
551WS_DLL_PUBLIC
553 tvbuff_t* tvb,
554 const int offset,
555 int len,
556 void* private_data,
557 const tvbparse_wanted_t* ignore);
558
567WS_DLL_PUBLIC
568bool tvbparse_reset(tvbparse_t* tt, const unsigned offset, unsigned len);
569
576WS_DLL_PUBLIC
577unsigned tvbparse_curr_offset(tvbparse_t* tt);
578
586
599WS_DLL_PUBLIC
602
615WS_DLL_PUBLIC
618
630WS_DLL_PUBLIC
633
640WS_DLL_PUBLIC
ftenum
Fundamental field value types used throughout the Wireshark dissector framework.
Definition ftypes.h:26
Definition proto.h:909
Represents a single token matched by the tvbuff parser, forming part of a linked tree of parse result...
Definition tvbparse.h:165
void * data
Definition tvbparse.h:173
struct _tvbparse_elem_t * sub
Definition tvbparse.h:175
tvbparse_t * parser
Definition tvbparse.h:168
tvbuff_t * tvb
Definition tvbparse.h:169
int len
Definition tvbparse.h:171
int id
Definition tvbparse.h:166
const tvbparse_wanted_t * wanted
Definition tvbparse.h:180
struct _tvbparse_elem_t * last
Definition tvbparse.h:178
int offset
Definition tvbparse.h:170
struct _tvbparse_elem_t * next
Definition tvbparse.h:177
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
struct _tvbparse_wanted_t::@497::@498 number
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
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
union _tvbparse_wanted_t::@497 control
Control parameters for the parsing rule.
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
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
Core tvbuff (testy virtual buffer) structure representing a region of packet data,...
Definition tvbuff-int.h:95
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 tvbparse_wanted_t * tvbparse_handle(tvbparse_wanted_t **handle)
Create an indirect reference element for recursive grammars.
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
until_mode_t
Controls how a parser consumes the terminal element when scanning up to the last token.
Definition tvbparse.h:82
@ TP_UNTIL_LEAVE
Definition tvbparse.h:85
@ TP_UNTIL_INCLUDE
Definition tvbparse.h:83
@ TP_UNTIL_SPEND
Definition tvbparse.h:84
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 tvbparse_wanted_t * tvbparse_string(const int id, const char *string, const void *private_data, tvbparse_action_t before_cb, tvbparse_action_t after_cb)
Create a case-sensitive literal string match element.
Definition tvbparse.c:338
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 tvbparse_wanted_t * tvbparse_until(const int id, const void *private_data, tvbparse_action_t before_cb, tvbparse_action_t after_cb, const tvbparse_wanted_t *ending, until_mode_t until_mode)
Create an "until" match element that consumes bytes up to a terminator.
Definition tvbparse.c:816
WS_DLL_PUBLIC tvbparse_wanted_t * tvbparse_set_seq(const int id, const void *private_data, tvbparse_action_t before_cb, tvbparse_action_t after_cb,...)
Create a sequential composition element.
Definition tvbparse.c:636
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_wanted_t * tvbparse_not_char(const int id, const char *needle, const void *private_data, tvbparse_action_t before_cb, tvbparse_action_t after_cb)
Create a single-character exclusion match element.
Definition tvbparse.c:269
WS_DLL_PUBLIC tvbparse_wanted_t * tvbparse_set_oneof(const int id, const void *private_data, tvbparse_action_t before_cb, tvbparse_action_t after_cb,...)
Create a one-of alternation element.
Definition tvbparse.c:440
WS_DLL_PUBLIC tvbparse_wanted_t * tvbparse_not_chars(const int id, const unsigned min_len, const unsigned max_len, const char *needles, const void *private_data, tvbparse_action_t before_cb, tvbparse_action_t after_cb)
Create a multi-character exclusion span match element.
Definition tvbparse.c:286
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_wanted_t * tvbparse_char(const int id, const char *needles, const void *private_data, tvbparse_action_t before_cb, tvbparse_action_t after_cb)
Create a single-character match element.
Definition tvbparse.c:154
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_hashed(const int id, const void *data, tvbparse_action_t before_cb, tvbparse_action_t after_cb, tvbparse_wanted_t *key, tvbparse_wanted_t *other,...)
Create a hash-dispatch element that selects a sub-element by key.
Definition tvbparse.c:529
WS_DLL_PUBLIC tvbparse_wanted_t * tvbparse_chars(const int id, const unsigned min_len, const unsigned max_len, const char *needles, const void *private_data, tvbparse_action_t before_cb, tvbparse_action_t after_cb)
Create a multi-character span match element.
Definition tvbparse.c:207
unsigned tvbparse_len_left(tvbparse_t *tt)
Get the number of bytes left to parse in the TVB parse structure.
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
WS_DLL_PUBLIC tvbparse_wanted_t * tvbparse_casestring(const int id, const char *str, const void *data, tvbparse_action_t before_cb, tvbparse_action_t after_cb)
Create a case-insensitive literal string match element.
Definition tvbparse.c:377