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