4 * Parser for display filters
6 * $Id: dfilter-grammar.y,v 1.10 1999/08/14 06:24:26 gram Exp $
8 * Ethereal - Network traffic analyzer
9 * By Gerald Combs <gerald@zing.org>
10 * Copyright 1998 Gerald Combs
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 #ifdef HAVE_SYS_TYPES_H
33 # include <sys/types.h>
36 #ifdef HAVE_NETINET_IN_H
37 # include <netinet/in.h>
62 #include "dfilter-int.h"
68 static GNode* dfilter_mknode_join(GNode *n1, enum node_type ntype, int operand, GNode *n2);
69 static GNode* dfilter_mknode_unary(int operand, GNode *n2);
70 static GNode* dfilter_mknode_numeric_variable(gint id);
71 static GNode* dfilter_mknode_numeric_value(guint32 val);
72 static GNode* dfilter_mknode_ether_value(guint8*);
73 static GNode* dfilter_mknode_ether_variable(gint id);
74 static GNode* dfilter_mknode_ipxnet_value(guint32);
75 static GNode* dfilter_mknode_ipxnet_variable(gint id);
76 static GNode* dfilter_mknode_ipv4_value(char *host);
77 static GNode* dfilter_mknode_ipv4_variable(gint id);
78 static GNode* dfilter_mknode_existence(gint id);
79 static GNode* dfilter_mknode_bytes_value(GByteArray *barray);
80 static GNode* dfilter_mknode_bytes_variable(gint id, gint offset, guint length);
81 static GNode* dfilter_mknode_boolean_value(gint truth_value);
82 static GNode* dfilter_mknode_boolean_variable(gint id);
84 static guint32 string_to_value(char *s);
86 /* This is the dfilter we're currently processing. It's how
87 * dfilter_compile communicates with us.
89 dfilter *global_df = NULL;;
94 gint operand; /* logical, relation, alternation */
106 %type <node> statement expression relation
107 %type <node> numeric_value numeric_variable
108 %type <node> ether_value ether_variable
109 %type <node> ipxnet_value ipxnet_variable
110 %type <node> ipv4_value ipv4_variable
111 %type <node> variable_name
112 %type <node> bytes_value bytes_variable
113 %type <node> boolean_value boolean_variable
115 %type <operand> numeric_relation
116 %type <operand> equality_relation
117 %type <operand> bytes_relation
119 %type <variable> any_variable_type
121 %token <variable> T_FT_UINT8
122 %token <variable> T_FT_UINT16
123 %token <variable> T_FT_UINT32
124 %token <variable> T_FT_ETHER
125 %token <variable> T_FT_IPv4
126 %token <variable> T_FT_NONE
127 %token <variable> T_FT_BYTES
128 %token <variable> T_FT_BOOLEAN
129 %token <variable> T_FT_STRING
130 %token <variable> T_FT_IPXNET
132 %token <id> T_VAL_UNQUOTED_STRING
133 %token <ether> T_VAL_ETHER
134 %token <bytes> T_VAL_BYTES
135 %token <byte_range> T_VAL_BYTE_RANGE
137 %token <operand> TOK_AND TOK_OR TOK_NOT TOK_XOR
138 %token <operand> TOK_EQ TOK_NE TOK_GT TOK_GE TOK_LT TOK_LE
139 %token <operand> TOK_TRUE TOK_FALSE
148 statement: expression
150 global_df->dftree = $1;
152 | /* NULL */ { global_df->dftree = NULL; }
155 expression: '(' expression ')' { $$ = $2; }
156 | expression TOK_AND expression { $$ = dfilter_mknode_join($1, logical, $2, $3); }
157 | expression TOK_OR expression { $$ = dfilter_mknode_join($1, logical, $2, $3); }
158 | expression TOK_XOR expression { $$ = dfilter_mknode_join($1, logical, $2, $3); }
159 | TOK_NOT expression { $$ = dfilter_mknode_unary(TOK_NOT, $2); }
160 | relation { $$ = $1; }
161 | variable_name { $$ = $1; }
164 relation: numeric_variable numeric_relation numeric_value
166 $$ = dfilter_mknode_join($1, relation, $2, $3);
168 | numeric_variable numeric_relation numeric_variable
170 $$ = dfilter_mknode_join($1, relation, $2, $3);
173 | ether_variable equality_relation ether_value
175 $$ = dfilter_mknode_join($1, relation, $2, $3);
177 | ether_variable equality_relation ether_variable
179 $$ = dfilter_mknode_join($1, relation, $2, $3);
182 | ipxnet_variable equality_relation ipxnet_value
184 $$ = dfilter_mknode_join($1, relation, $2, $3);
186 | ipxnet_variable equality_relation ipxnet_variable
188 $$ = dfilter_mknode_join($1, relation, $2, $3);
192 | ipv4_variable numeric_relation ipv4_value
194 $$ = dfilter_mknode_join($1, relation, $2, $3);
196 | ipv4_variable numeric_relation ipv4_variable
198 $$ = dfilter_mknode_join($1, relation, $2, $3);
201 | bytes_variable bytes_relation bytes_value
203 $$ = dfilter_mknode_join($1, relation, $2, $3);
205 | bytes_variable bytes_relation bytes_variable
207 $$ = dfilter_mknode_join($1, relation, $2, $3);
210 | boolean_variable equality_relation boolean_value
212 $$ = dfilter_mknode_join($1, relation, $2, $3);
214 | boolean_variable equality_relation boolean_variable
216 $$ = dfilter_mknode_join($1, relation, $2, $3);
222 numeric_value: T_VAL_UNQUOTED_STRING
224 $$ = dfilter_mknode_numeric_value(string_to_value($1));
229 ether_value: T_VAL_ETHER
231 $$ = dfilter_mknode_ether_value($1);
235 ipxnet_value: T_VAL_UNQUOTED_STRING
237 $$ = dfilter_mknode_ipxnet_value(string_to_value($1));
241 ipv4_value: T_VAL_UNQUOTED_STRING
243 $$ = dfilter_mknode_ipv4_value($1);
248 bytes_value: T_VAL_BYTES
249 { /* 2 - 5, or > 6 bytes */
250 $$ = dfilter_mknode_bytes_value($1);
253 | T_VAL_UNQUOTED_STRING
254 { /* one or 4 bytes */
257 /* the next function appends to list_of_byte_arrays for me */
258 barray = byte_str_to_guint8_array($1);
259 $$ = dfilter_mknode_bytes_value(barray);
265 GByteArray *barray = g_byte_array_new();
267 global_df->list_of_byte_arrays = g_slist_append(global_df->list_of_byte_arrays, barray);
268 g_byte_array_append(barray, $1, 6);
269 $$ = dfilter_mknode_bytes_value(barray);
274 boolean_value: TOK_TRUE { $$ = dfilter_mknode_boolean_value($1); }
275 | TOK_FALSE { $$ = dfilter_mknode_boolean_value($1); }
279 numeric_variable: T_FT_UINT8 { $$ = dfilter_mknode_numeric_variable($1); }
280 | T_FT_UINT16 { $$ = dfilter_mknode_numeric_variable($1); }
281 | T_FT_UINT32 { $$ = dfilter_mknode_numeric_variable($1); }
284 ether_variable: T_FT_ETHER { $$ = dfilter_mknode_ether_variable($1); }
287 ipxnet_variable: T_FT_IPXNET { $$ = dfilter_mknode_ipxnet_variable($1); }
290 ipv4_variable: T_FT_IPv4 { $$ = dfilter_mknode_ipv4_variable($1); }
293 variable_name: any_variable_type { $$ = dfilter_mknode_existence($1); }
296 bytes_variable: any_variable_type T_VAL_BYTE_RANGE
298 $$ = dfilter_mknode_bytes_variable($1, $2.offset, $2.length);
302 boolean_variable: T_FT_BOOLEAN { $$ = dfilter_mknode_boolean_variable($1); }
305 any_variable_type: T_FT_UINT8 { $$ = $1; }
306 | T_FT_UINT16 { $$ = $1; }
307 | T_FT_UINT32 { $$ = $1; }
308 | T_FT_ETHER { $$ = $1; }
309 | T_FT_IPv4 { $$ = $1; }
310 | T_FT_NONE { $$ = $1; }
311 | T_FT_BYTES { $$ = $1; }
312 | T_FT_BOOLEAN { $$ = $1; }
313 | T_FT_STRING { $$ = $1; }
316 numeric_relation: TOK_EQ { $$ = TOK_EQ; }
317 | TOK_NE { $$ = TOK_NE; }
318 | TOK_GT { $$ = TOK_GT; }
319 | TOK_GE { $$ = TOK_GE; }
320 | TOK_LT { $$ = TOK_LT; }
321 | TOK_LE { $$ = TOK_LE; }
324 equality_relation: TOK_EQ { $$ = TOK_EQ; }
325 | TOK_NE { $$ = TOK_NE; }
328 bytes_relation: TOK_EQ { $$ = TOK_EQ; }
329 | TOK_NE { $$ = TOK_NE; }
330 | TOK_GT { $$ = TOK_GT; }
331 | TOK_LT { $$ = TOK_LT; }
337 dfilter_mknode_join(GNode *n1, enum node_type ntype, int operand, GNode *n2)
339 dfilter_node *node_root;
342 node_root = g_mem_chunk_alloc(global_df->node_memchunk);
343 node_root->ntype = ntype;
344 node_root->elem_size = 0;
345 node_root->fill_array_func = NULL;
346 node_root->check_relation_func = NULL;
347 if (ntype == relation) {
348 node_root->value.relation = operand;
350 else if (ntype == logical) {
351 node_root->value.logical = operand;
354 g_assert_not_reached();
357 gnode_root = g_node_new(node_root);
358 g_node_append(gnode_root, n1);
359 g_node_append(gnode_root, n2);
365 dfilter_mknode_unary(int operand, GNode *n2)
367 dfilter_node *node_root;
370 node_root = g_mem_chunk_alloc(global_df->node_memchunk);
371 node_root->ntype = logical;
372 node_root->value.logical = operand;
373 node_root->elem_size = 0;
374 node_root->fill_array_func = NULL;
375 node_root->check_relation_func = NULL;
377 gnode_root = g_node_new(node_root);
378 g_node_append(gnode_root, n2);
385 dfilter_mknode_numeric_variable(gint id)
390 node = g_mem_chunk_alloc(global_df->node_memchunk);
391 node->ntype = variable;
392 node->elem_size = sizeof(guint32);
393 node->fill_array_func = fill_array_numeric_variable;
394 node->check_relation_func = check_relation_numeric;
395 node->value.variable = id;
396 gnode = g_node_new(node);
402 dfilter_mknode_ether_variable(gint id)
407 node = g_mem_chunk_alloc(global_df->node_memchunk);
408 node->ntype = variable;
409 node->elem_size = sizeof(guint8) * 6;
410 node->fill_array_func = fill_array_ether_variable;
411 node->check_relation_func = check_relation_ether;
412 node->value.variable = id;
413 gnode = g_node_new(node);
419 dfilter_mknode_ipxnet_variable(gint id)
424 node = g_mem_chunk_alloc(global_df->node_memchunk);
425 node->ntype = variable;
426 node->elem_size = sizeof(guint8) * 4;
427 node->fill_array_func = fill_array_numeric_variable; /* cheating ! */
428 node->check_relation_func = check_relation_numeric; /* cheating ! */
429 node->value.variable = id;
430 gnode = g_node_new(node);
436 dfilter_mknode_ipv4_variable(gint id)
441 node = g_mem_chunk_alloc(global_df->node_memchunk);
442 node->ntype = variable;
443 node->elem_size = sizeof(guint32);
444 node->fill_array_func = fill_array_numeric_variable; /* cheating ! */
445 node->check_relation_func = check_relation_numeric; /* cheating ! */
446 node->value.variable = id;
447 gnode = g_node_new(node);
453 dfilter_mknode_bytes_variable(gint id, gint offset, guint length)
458 node = g_mem_chunk_alloc(global_df->node_memchunk);
459 node->ntype = variable;
460 node->elem_size = sizeof(GByteArray*);
461 node->fill_array_func = fill_array_bytes_variable;
462 node->check_relation_func = check_relation_bytes;
463 node->value.variable = id;
464 node->offset = offset;
465 node->length = length;
466 gnode = g_node_new(node);
472 dfilter_mknode_boolean_variable(gint id)
477 node = g_mem_chunk_alloc(global_df->node_memchunk);
478 node->ntype = variable;
479 node->elem_size = sizeof(guint32);
480 node->fill_array_func = fill_array_boolean_variable; /* cheating ! */
481 node->check_relation_func = check_relation_boolean; /* cheating ! */
482 node->value.variable = id;
483 gnode = g_node_new(node);
489 dfilter_mknode_numeric_value(guint32 val)
494 node = g_mem_chunk_alloc(global_df->node_memchunk);
495 node->ntype = numeric;
496 node->elem_size = sizeof(guint32);
497 node->fill_array_func = fill_array_numeric_value;
498 node->check_relation_func = check_relation_numeric;
499 node->value.numeric = val;
500 gnode = g_node_new(node);
506 dfilter_mknode_ether_value(guint8 *ether_bytes)
511 node = g_mem_chunk_alloc(global_df->node_memchunk);
513 node->elem_size = sizeof(guint8) * 6;
514 node->fill_array_func = fill_array_ether_value;
515 node->check_relation_func = check_relation_ether;
517 memcpy(&node->value.ether, ether_bytes, 6);
519 gnode = g_node_new(node);
524 dfilter_mknode_ipxnet_value(guint32 ipx_net_val)
529 node = g_mem_chunk_alloc(global_df->node_memchunk);
530 node->ntype = ipxnet;
531 node->elem_size = sizeof(guint8) * 4;
532 node->fill_array_func = fill_array_numeric_value; /* cheating ! */
533 node->check_relation_func = check_relation_numeric; /* cheating ! */
534 node->value.numeric = ipx_net_val;
535 gnode = g_node_new(node);
541 dfilter_mknode_ipv4_value(char *host)
546 node = g_mem_chunk_alloc(global_df->node_memchunk);
547 node->ntype = numeric;
548 node->elem_size = sizeof(guint32);
549 node->fill_array_func = fill_array_numeric_value; /* cheating ! */
550 node->check_relation_func = check_relation_numeric; /* cheating ! */
551 node->value.numeric = get_host_ipaddr(host);
552 node->value.numeric = htonl(node->value.numeric);
553 gnode = g_node_new(node);
559 dfilter_mknode_bytes_value(GByteArray *barray)
564 node = g_mem_chunk_alloc(global_df->node_memchunk);
566 node->elem_size = sizeof(GByteArray*);
567 node->fill_array_func = fill_array_bytes_value;
568 node->check_relation_func = check_relation_bytes;
569 node->value.bytes = barray;
570 node->offset = G_MAXINT;
571 node->length = barray->len;
572 gnode = g_node_new(node);
578 dfilter_mknode_boolean_value(gint truth_value)
583 node = g_mem_chunk_alloc(global_df->node_memchunk);
584 node->ntype = numeric;
585 node->elem_size = sizeof(guint32);
586 node->fill_array_func = fill_array_boolean_value;
587 node->check_relation_func = check_relation_boolean;
588 node->value.boolean = truth_value == TOK_TRUE ? TRUE : FALSE;
589 gnode = g_node_new(node);
595 string_to_value(char *s)
600 val = strtoul(s, &endptr, 0);
601 /* I should probably check errno here */
607 dfilter_mknode_existence(gint id)
612 node = g_mem_chunk_alloc(global_df->node_memchunk);
613 node->ntype = existence;
614 node->elem_size = sizeof(guint32);
615 node->fill_array_func = NULL;
616 node->check_relation_func = NULL;
617 node->value.variable = id;
618 gnode = g_node_new(node);