| Wireshark 4.7.0 The Wireshark network protocol analyzer

Loading...
Searching...
No Matches
syntax-tree.h
Go to the documentation of this file.
1/*
2 * Wireshark - Network traffic analyzer
3 * By Gerald Combs <gerald@wireshark.org>
4 * Copyright 2001 Gerald Combs
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9#ifndef SYNTAX_TREE_H
10#define SYNTAX_TREE_H
11
12#include <stdio.h>
13#include <inttypes.h>
14
15#include <wsutil/ws_assert.h>
16#include <wsutil/wslog.h>
17#include <epan/ftypes/ftypes.h>
18#include "dfilter-loc.h"
19
23#ifdef __cplusplus
24 extern "C" {
25#endif /* __cplusplus */
26
27#define ASSERT_STTYPE_NOT_REACHED(st) \
28 ws_error("Invalid syntax node type '%s'.", sttype_name(st))
29
30#define ASSERT_STNODE_OP_NOT_REACHED(op) \
31 ws_error("Invalid stnode op '%s'.", stnode_op_name(op))
32
33 typedef enum {
34 STTYPE_UNINITIALIZED,
35 STTYPE_TEST,
36 STTYPE_UNPARSED, /* Must be resolved into a literal or a field. */
37 STTYPE_LITERAL,
38 STTYPE_REFERENCE,
39 STTYPE_STRING,
40 STTYPE_CHARCONST,
41 STTYPE_NUMBER,
42 STTYPE_FIELD,
43 STTYPE_FVALUE,
44 STTYPE_SLICE,
45 STTYPE_FUNCTION,
46 STTYPE_SET,
47 STTYPE_PCRE,
48 STTYPE_ARITHMETIC,
49 STTYPE_NUM_TYPES
50} sttype_id_t;
51
52 typedef void * (*STTypeNewFunc)(void *);
53 typedef void * (*STTypeDupFunc)(const void *);
54 typedef void (*STTypeFreeFunc)(void *);
55 typedef char* (*STTypeToStrFunc)(const void *, bool pretty);
56
57
58/* Type information */
59 typedef struct {
60 sttype_id_t id;
61 STTypeNewFunc func_new;
62 STTypeFreeFunc func_free;
63 STTypeDupFunc func_dup;
64 STTypeToStrFunc func_tostr;
65} sttype_t;
66
67 typedef enum {
68 STNUM_NONE = 0,
69 STNUM_INTEGER,
70 STNUM_UNSIGNED,
71 STNUM_FLOAT,
72} stnumber_t;
73
74/* Lexical value is ambiguous (can be a protocol field or a literal). */
75#define STFLAG_UNPARSED (1 << 0)
76
78 typedef struct stnode {
79 sttype_t *type;
80 void *data;
81 char *repr_token;
82 char *repr_display;
83 char *repr_debug;
84 df_loc_t location;
85 uint16_t flags;
86} stnode_t;
87
88 typedef enum {
89 STNODE_OP_UNINITIALIZED,
90 STNODE_OP_NOT,
91 STNODE_OP_AND,
92 STNODE_OP_OR,
93 STNODE_OP_ALL_EQ,
94 STNODE_OP_ANY_EQ,
95 STNODE_OP_ALL_NE,
96 STNODE_OP_ANY_NE,
97 STNODE_OP_GT,
98 STNODE_OP_GE,
99 STNODE_OP_LT,
100 STNODE_OP_LE,
101 STNODE_OP_CONTAINS,
102 STNODE_OP_MATCHES,
103 STNODE_OP_IN,
104 STNODE_OP_NOT_IN,
105 STNODE_OP_BITWISE_AND,
106 STNODE_OP_UNARY_MINUS,
107 STNODE_OP_ADD,
108 STNODE_OP_SUBTRACT,
109 STNODE_OP_MULTIPLY,
110 STNODE_OP_DIVIDE,
111 STNODE_OP_MODULO,
112} stnode_op_t;
113
114 typedef enum {
115 STNODE_MATCH_DEF,
116 STNODE_MATCH_ANY,
117 STNODE_MATCH_ALL,
118} stmatch_t;
119
120/* These are the sttype_t registration function prototypes. */
121 void sttype_register_field(void);
122 void sttype_register_function(void);
123 void sttype_register_number(void);
124 void sttype_register_pointer(void);
125 void sttype_register_set(void);
126 void sttype_register_slice(void);
127 void sttype_register_string(void);
128 void sttype_register_opers(void);
129
130 void
131sttype_init(void);
132
133 void
134sttype_cleanup(void);
135
136 void
137sttype_register(sttype_t *type);
138
139WS_DLL_PUBLIC
140 const char *
141sttype_name(const sttype_id_t type);
142
143WS_DLL_PUBLIC
144 const char *
145stnode_op_name(const stnode_op_t op);
146
147WS_DLL_PUBLIC
148 stnode_t*
149stnode_new(sttype_id_t type_id, void *data, char *token, df_loc_t loc);
150
151WS_DLL_PUBLIC
152 stnode_t*
153stnode_new_empty(sttype_id_t type_id);
154
155WS_DLL_PUBLIC
156 stnode_t*
157stnode_dup(const stnode_t *org);
158
159WS_DLL_PUBLIC
160 void
161stnode_clear(stnode_t *node);
162
163WS_DLL_PUBLIC
164 void
165stnode_init(stnode_t *node, sttype_id_t type_id, void *data, char *token, df_loc_t loc);
166
167WS_DLL_PUBLIC
168 void
169stnode_replace(stnode_t *node, sttype_id_t type_id, void *data);
170
171WS_DLL_PUBLIC
172 void
173stnode_mutate(stnode_t *node, sttype_id_t type_id);
174
175WS_DLL_PUBLIC
176 void
177stnode_free(stnode_t *node);
178
179WS_DLL_PUBLIC
180 const char*
181stnode_type_name(const stnode_t *node);
182
183WS_DLL_PUBLIC
184sttype_id_t
185stnode_type_id(const stnode_t *node);
186
187WS_DLL_PUBLIC
188 void *
189stnode_data(stnode_t *node);
190
191WS_DLL_PUBLIC
192GString *
193stnode_string(stnode_t *node);
194
195WS_DLL_PUBLIC
196 void *
197stnode_steal_data(stnode_t *node);
198
199WS_DLL_PUBLIC
200 const char *
201stnode_token(const stnode_t *node);
202
203WS_DLL_PUBLIC
204 df_loc_t
205stnode_location(const stnode_t *node);
206
207WS_DLL_PUBLIC
208 void
209stnode_set_location(stnode_t *node, df_loc_t loc);
210
211WS_DLL_PUBLIC
212 bool
213stnode_get_flags(stnode_t *node, uint16_t flags);
214
215WS_DLL_PUBLIC
216 void
217stnode_set_flags(stnode_t *node, uint16_t flags);
218
219 void
220stnode_merge_location(stnode_t *dst, stnode_t *n1, stnode_t *n2);
221
222WS_DLL_PUBLIC
223 const char *
224stnode_tostr(stnode_t *node, bool pretty);
225
226#define stnode_todisplay(node) stnode_tostr(node, true)
227
228#define stnode_todebug(node) stnode_tostr(node, false)
229
230 void
231log_node_full(enum ws_log_level level,
232 const char *file, int line, const char *func,
233 stnode_t *node, const char *msg);
234
235 void
236log_test_full(enum ws_log_level level,
237 const char *file, int line, const char *func,
238 stnode_t *node, const char *msg);
239
240#ifdef WS_DEBUG
241#define log_node(node) \
242 log_node_full(LOG_LEVEL_NOISY, __FILE__, __LINE__, __func__, node, #node)
243#define log_test(node) \
244 log_test_full(LOG_LEVEL_NOISY, __FILE__, __LINE__, __func__, node, #node)
245#define LOG_NODE(node) \
246 do { \
247 if (stnode_type_id(node) == STTYPE_TEST) \
248 log_test(node); \
249 else \
250 log_node(node); \
251 } while (0)
252#else
253#define log_node(node) (void)0
254#define log_test(node) (void)0
255#define LOG_NODE(node) (void)0
256#endif
257
258 char *
259dump_syntax_tree_str(stnode_t *root);
260
261 void
262log_syntax_tree(enum ws_log_level, stnode_t *root, const char *msg, char **cache_ptr);
263
264#ifdef WS_DEBUG
265#define ws_assert_magic(obj, mnum) \
266 do { \
267 ws_assert(obj); \
268 if ((obj)->magic != (mnum)) { \
269 ws_log_full(LOG_DOMAIN_DFILTER, LOG_LEVEL_ERROR, \
270 __FILE__, __LINE__, __func__, \
271 "Magic num is 0x%08" PRIx32", " \
272 "but should be 0x%08" PRIx32, \
273 (obj)->magic, (mnum)); \
274 } \
275 } while(0)
276#else
277#define ws_assert_magic(obj, mnum) (void)0
278#endif
279
280#ifdef __cplusplus
281}
282#endif /* __cplusplus */
283
284#endif /* SYNTAX_TREE_H */
dfilter-loc.h
ftypes.h
_dfilter_loc
Definition dfilter-loc.h:16
stnode
Definition syntax-tree.h:78
sttype_t
Definition syntax-tree.h:59
stnode_t
struct stnode stnode_t
ws_assert.h
wslog.h