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