4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 2001 Gerald Combs
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 #include "dfilter-int.h"
31 #include "syntax-tree.h"
35 #include <epan/epan_dissect.h>
38 #define DFILTER_TOKEN_ID_OFFSET 1
40 /* Global error message space for dfilter_compile errors */
41 static gchar dfilter_error_msg_buf[1024];
42 gchar *dfilter_error_msg; /* NULL when no error resulted */
45 void df_scanner_text(const char *text);
46 void df_scanner_cleanup(void);
49 /* Holds the singular instance of our Lemon parser object */
50 static void* ParserObj = NULL;
53 dfilter_fail(const char *format, ...)
57 /* If we've already reported one error, don't overwite it */
58 if (dfilter_error_msg != NULL)
61 va_start(args, format);
63 g_vsnprintf(dfilter_error_msg_buf, sizeof(dfilter_error_msg_buf),
65 dfilter_error_msg = dfilter_error_msg_buf;
70 /* Initialize the dfilter module */
75 g_message("I expected ParserObj to be NULL\n");
76 /* Free the Lemon Parser object */
77 DfilterFree(ParserObj, g_free);
79 /* Allocate an instance of our Lemon-based parser */
80 ParserObj = DfilterAlloc(g_malloc);
82 /* Enable parser tracing by defining AM_CFLAGS
83 * so that it contains "-DDFTRACE".
87 DfilterTrace(stdout, "lemon> ");
90 /* Initialize the syntax-tree sub-sub-system */
94 /* Clean-up the dfilter module */
98 /* Free the Lemon Parser object */
100 DfilterFree(ParserObj, g_free);
103 /* Clean up the syntax-tree sub-sub-system */
112 df = g_new(dfilter_t, 1);
118 /* Given a GPtrArray of instructions (dfvm_insn_t),
121 free_insns(GPtrArray *insns)
126 for (i = 0; i < insns->len; i++) {
127 insn = g_ptr_array_index(insns, i);
128 dfvm_insn_free(insn);
133 dfilter_free(dfilter_t *df)
136 free_insns(df->insns);
139 if (df->interesting_fields) {
140 g_free(df->interesting_fields);
143 g_free(df->registers);
144 g_free(df->attempted_load);
154 dfw = g_new(dfwork_t, 1);
157 dfw->syntax_error = FALSE;
159 dfw->loaded_fields = NULL;
160 dfw->interesting_fields = NULL;
161 dfw->next_insn_id = 0;
162 dfw->next_register = 0;
168 dfwork_free(dfwork_t *dfw)
171 stnode_free(dfw->st_root);
174 if (dfw->loaded_fields) {
175 g_hash_table_destroy(dfw->loaded_fields);
178 if (dfw->interesting_fields) {
179 g_hash_table_destroy(dfw->interesting_fields);
183 free_insns(dfw->insns);
192 dfilter_compile(const gchar *text, dfilter_t **dfp)
197 gboolean failure = FALSE;
199 dfilter_error_msg = NULL;
203 df_scanner_text(text);
206 df_lval = stnode_new(STTYPE_UNINITIALIZED, NULL);
209 /* Check for scanner failure */
210 if (token == SCAN_FAILED) {
215 /* Check for end-of-input */
220 /* Give the token to the parser */
221 Dfilter(ParserObj, token, df_lval, dfw);
222 /* We've used the stnode_t, so we don't want to free it */
225 if (dfw->syntax_error) {
231 /* If we created an stnode_t but didn't use it, free it; the
232 * parser doesn't know about it and won't free it for us. */
234 stnode_free(df_lval);
238 /* Tell the parser that we have reached the end of input; that
239 * way, it'll reset its state for the next compile. (We want
240 * to do that even if we got a syntax error, to make sure the
241 * parser state is cleaned up; we don't create a new parser
242 * object when we start a new parse, and don't destroy it when
243 * the parse finishes.) */
244 Dfilter(ParserObj, 0, NULL, dfw);
246 /* One last check for syntax error (after EOF) */
247 if (dfw->syntax_error)
251 df_scanner_cleanup();
256 /* Success, but was it an empty filter? If so, discard
257 * it and set *dfp to NULL */
258 if (dfw->st_root == NULL) {
263 /* Check semantics and do necessary type conversion*/
264 if (!dfw_semcheck(dfw)) {
268 /* Create bytecode */
271 /* Tuck away the bytecode in the dfilter_t */
272 dfilter = dfilter_new();
273 dfilter->insns = dfw->insns;
275 dfilter->interesting_fields = dfw_interesting_fields(dfw,
276 &dfilter->num_interesting_fields);
278 /* Initialize run-time space */
279 dfilter->num_registers = dfw->next_register;
280 dfilter->registers = g_new0(GList*, dfilter->num_registers);
281 dfilter->attempted_load = g_new0(gboolean, dfilter->num_registers);
283 /* And give it to the user. */
294 dfilter_fail("Unable to parse filter string \"%s\".", text);
302 dfilter_apply(dfilter_t *df, proto_tree *tree)
304 return dfvm_apply(df, tree);
308 dfilter_apply_edt(dfilter_t *df, epan_dissect_t* edt)
310 return dfvm_apply(df, edt->tree);
315 dfilter_prime_proto_tree(const dfilter_t *df, proto_tree *tree)
319 for (i = 0; i < df->num_interesting_fields; i++) {
320 proto_tree_prime_hfid(tree, df->interesting_fields[i]);
326 dfilter_dump(dfilter_t *df)
328 dfvm_dump(stdout, df->insns);