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