4 * Parser for display filters
6 * $Id: dfilter-grammar.y,v 1.8 1999/08/12 21:16:31 guy 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.
28 #define yylex dfilter_lex
34 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
38 #ifdef HAVE_NETINET_IN_H
39 # include <netinet/in.h>
46 #include <string.h> /* during testing */
64 #include "dfilter-int.h"
70 void dfilter_yacc_init(void);
71 static GNode* dfilter_mknode_join(GNode *n1, enum node_type ntype, int operand, GNode *n2);
72 static GNode* dfilter_mknode_unary(int operand, GNode *n2);
73 static GNode* dfilter_mknode_numeric_variable(gint id);
74 static GNode* dfilter_mknode_numeric_value(guint32 val);
75 static GNode* dfilter_mknode_ether_value(guint8*);
76 static GNode* dfilter_mknode_ether_variable(gint id);
77 static GNode* dfilter_mknode_ipxnet_value(guint32);
78 static GNode* dfilter_mknode_ipxnet_variable(gint id);
79 static GNode* dfilter_mknode_ipv4_value(char *host);
80 static GNode* dfilter_mknode_ipv4_variable(gint id);
81 static GNode* dfilter_mknode_existence(gint id);
82 static GNode* dfilter_mknode_bytes_value(GByteArray *barray);
83 static GNode* dfilter_mknode_bytes_variable(gint id, gint offset, guint length);
84 static GNode* dfilter_mknode_boolean_value(gint truth_value);
85 static GNode* dfilter_mknode_boolean_variable(gint id);
87 static guint32 string_to_value(char *s);
89 /* space for dfilter_nodes */
90 GMemChunk *gmc_dfilter_nodes = NULL;
92 /* this is how we pass display filter tree (dfcode) back to calling routine */
93 GNode *dfilter_tree = NULL;
95 /* list of byte arrays we allocate during parse. We can traverse this list
96 * faster than the tree when we go back and free the byte arrays */
97 GSList *dfilter_list_byte_arrays = NULL;
102 gint operand; /* logical, relation, alternation */
114 %type <node> statement expression relation
115 %type <node> numeric_value numeric_variable
116 %type <node> ether_value ether_variable
117 %type <node> ipxnet_value ipxnet_variable
118 %type <node> ipv4_value ipv4_variable
119 %type <node> variable_name
120 %type <node> bytes_value bytes_variable
121 %type <node> boolean_value boolean_variable
123 %type <operand> numeric_relation
124 %type <operand> equality_relation
125 %type <operand> bytes_relation
127 %type <variable> any_variable_type
129 %token <variable> T_FT_UINT8
130 %token <variable> T_FT_UINT16
131 %token <variable> T_FT_UINT32
132 %token <variable> T_FT_ETHER
133 %token <variable> T_FT_IPv4
134 %token <variable> T_FT_NONE
135 %token <variable> T_FT_BYTES
136 %token <variable> T_FT_BOOLEAN
137 %token <variable> T_FT_STRING
138 %token <variable> T_FT_IPXNET
140 %token <id> T_VAL_UNQUOTED_STRING
141 %token <ether> T_VAL_ETHER
142 %token <bytes> T_VAL_BYTES
143 %token <byte_range> T_VAL_BYTE_RANGE
145 %token <operand> TOK_AND TOK_OR TOK_NOT TOK_XOR
146 %token <operand> TOK_EQ TOK_NE TOK_GT TOK_GE TOK_LT TOK_LE
147 %token <operand> TOK_TRUE TOK_FALSE
156 statement: expression
160 | /* NULL */ { dfilter_tree = NULL; }
163 expression: '(' expression ')' { $$ = $2; }
164 | expression TOK_AND expression { $$ = dfilter_mknode_join($1, logical, $2, $3); }
165 | expression TOK_OR expression { $$ = dfilter_mknode_join($1, logical, $2, $3); }
166 | expression TOK_XOR expression { $$ = dfilter_mknode_join($1, logical, $2, $3); }
167 | TOK_NOT expression { $$ = dfilter_mknode_unary(TOK_NOT, $2); }
168 | relation { $$ = $1; }
169 | variable_name { $$ = $1; }
172 relation: numeric_variable numeric_relation numeric_value
174 $$ = dfilter_mknode_join($1, relation, $2, $3);
176 | numeric_variable numeric_relation numeric_variable
178 $$ = dfilter_mknode_join($1, relation, $2, $3);
181 | ether_variable equality_relation ether_value
183 $$ = dfilter_mknode_join($1, relation, $2, $3);
185 | ether_variable equality_relation ether_variable
187 $$ = dfilter_mknode_join($1, relation, $2, $3);
190 | ipxnet_variable equality_relation ipxnet_value
192 $$ = dfilter_mknode_join($1, relation, $2, $3);
194 | ipxnet_variable equality_relation ipxnet_variable
196 $$ = dfilter_mknode_join($1, relation, $2, $3);
200 | ipv4_variable numeric_relation ipv4_value
202 $$ = dfilter_mknode_join($1, relation, $2, $3);
204 | ipv4_variable numeric_relation ipv4_variable
206 $$ = dfilter_mknode_join($1, relation, $2, $3);
209 | bytes_variable bytes_relation bytes_value
211 $$ = dfilter_mknode_join($1, relation, $2, $3);
213 | bytes_variable bytes_relation bytes_variable
215 $$ = dfilter_mknode_join($1, relation, $2, $3);
218 | boolean_variable equality_relation boolean_value
220 $$ = dfilter_mknode_join($1, relation, $2, $3);
222 | boolean_variable equality_relation boolean_variable
224 $$ = dfilter_mknode_join($1, relation, $2, $3);
230 numeric_value: T_VAL_UNQUOTED_STRING
232 $$ = dfilter_mknode_numeric_value(string_to_value($1));
237 ether_value: T_VAL_ETHER
239 $$ = dfilter_mknode_ether_value($1);
243 ipxnet_value: T_VAL_UNQUOTED_STRING
245 $$ = dfilter_mknode_ipxnet_value(string_to_value($1));
249 ipv4_value: T_VAL_UNQUOTED_STRING
251 $$ = dfilter_mknode_ipv4_value($1);
256 bytes_value: T_VAL_BYTES
257 { /* 2 - 5, or > 6 bytes */
258 $$ = dfilter_mknode_bytes_value($1);
261 | T_VAL_UNQUOTED_STRING
262 { /* one or 4 bytes */
265 /* the next function appends to dfilter_list_byte_arrays for me */
266 barray = byte_str_to_guint8_array($1);
267 $$ = dfilter_mknode_bytes_value(barray);
273 GByteArray *barray = g_byte_array_new();
275 dfilter_list_byte_arrays = g_slist_append(dfilter_list_byte_arrays, barray);
276 g_byte_array_append(barray, $1, 6);
277 $$ = dfilter_mknode_bytes_value(barray);
282 boolean_value: TOK_TRUE { $$ = dfilter_mknode_boolean_value($1); }
283 | TOK_FALSE { $$ = dfilter_mknode_boolean_value($1); }
287 numeric_variable: T_FT_UINT8 { $$ = dfilter_mknode_numeric_variable($1); }
288 | T_FT_UINT16 { $$ = dfilter_mknode_numeric_variable($1); }
289 | T_FT_UINT32 { $$ = dfilter_mknode_numeric_variable($1); }
292 ether_variable: T_FT_ETHER { $$ = dfilter_mknode_ether_variable($1); }
295 ipxnet_variable: T_FT_IPXNET { $$ = dfilter_mknode_ipxnet_variable($1); }
298 ipv4_variable: T_FT_IPv4 { $$ = dfilter_mknode_ipv4_variable($1); }
301 variable_name: any_variable_type { $$ = dfilter_mknode_existence($1); }
304 bytes_variable: any_variable_type T_VAL_BYTE_RANGE
306 $$ = dfilter_mknode_bytes_variable($1, $2.offset, $2.length);
310 boolean_variable: T_FT_BOOLEAN { $$ = dfilter_mknode_boolean_variable($1); }
313 any_variable_type: T_FT_UINT8 { $$ = $1; }
314 | T_FT_UINT16 { $$ = $1; }
315 | T_FT_UINT32 { $$ = $1; }
316 | T_FT_ETHER { $$ = $1; }
317 | T_FT_IPv4 { $$ = $1; }
318 | T_FT_NONE { $$ = $1; }
319 | T_FT_BYTES { $$ = $1; }
320 | T_FT_BOOLEAN { $$ = $1; }
321 | T_FT_STRING { $$ = $1; }
324 numeric_relation: TOK_EQ { $$ = TOK_EQ; }
325 | TOK_NE { $$ = TOK_NE; }
326 | TOK_GT { $$ = TOK_GT; }
327 | TOK_GE { $$ = TOK_GE; }
328 | TOK_LT { $$ = TOK_LT; }
329 | TOK_LE { $$ = TOK_LE; }
332 equality_relation: TOK_EQ { $$ = TOK_EQ; }
333 | TOK_NE { $$ = TOK_NE; }
336 bytes_relation: TOK_EQ { $$ = TOK_EQ; }
337 | TOK_NE { $$ = TOK_NE; }
338 | TOK_GT { $$ = TOK_GT; }
339 | TOK_LT { $$ = TOK_LT; }
345 dfilter_yacc_init(void)
347 if (gmc_dfilter_nodes)
348 g_mem_chunk_destroy(gmc_dfilter_nodes);
350 gmc_dfilter_nodes = g_mem_chunk_new("gmc_dfilter_nodes",
351 sizeof(dfilter_node), 50 * sizeof(dfilter_node),
354 if (dfilter_list_byte_arrays) {
355 /* clear the byte arrays */
356 g_slist_free(dfilter_list_byte_arrays);
362 dfilter_yacc_cleanup(void)
364 if (gmc_dfilter_nodes)
365 g_mem_chunk_destroy(gmc_dfilter_nodes);
370 dfilter_mknode_join(GNode *n1, enum node_type ntype, int operand, GNode *n2)
372 dfilter_node *node_root;
375 node_root = g_mem_chunk_alloc(gmc_dfilter_nodes);
376 node_root->ntype = ntype;
377 node_root->elem_size = 0;
378 node_root->fill_array_func = NULL;
379 node_root->check_relation_func = NULL;
380 if (ntype == relation) {
381 node_root->value.relation = operand;
383 else if (ntype == logical) {
384 node_root->value.logical = operand;
387 g_assert_not_reached();
390 gnode_root = g_node_new(node_root);
391 g_node_append(gnode_root, n1);
392 g_node_append(gnode_root, n2);
398 dfilter_mknode_unary(int operand, GNode *n2)
400 dfilter_node *node_root;
403 node_root = g_mem_chunk_alloc(gmc_dfilter_nodes);
404 node_root->ntype = logical;
405 node_root->value.logical = operand;
406 node_root->elem_size = 0;
407 node_root->fill_array_func = NULL;
408 node_root->check_relation_func = NULL;
410 gnode_root = g_node_new(node_root);
411 g_node_append(gnode_root, n2);
418 dfilter_mknode_numeric_variable(gint id)
423 node = g_mem_chunk_alloc(gmc_dfilter_nodes);
424 node->ntype = variable;
425 node->elem_size = sizeof(guint32);
426 node->fill_array_func = fill_array_numeric_variable;
427 node->check_relation_func = check_relation_numeric;
428 node->value.variable = id;
429 gnode = g_node_new(node);
435 dfilter_mknode_ether_variable(gint id)
440 node = g_mem_chunk_alloc(gmc_dfilter_nodes);
441 node->ntype = variable;
442 node->elem_size = sizeof(guint8) * 6;
443 node->fill_array_func = fill_array_ether_variable;
444 node->check_relation_func = check_relation_ether;
445 node->value.variable = id;
446 gnode = g_node_new(node);
452 dfilter_mknode_ipxnet_variable(gint id)
457 node = g_mem_chunk_alloc(gmc_dfilter_nodes);
458 node->ntype = variable;
459 node->elem_size = sizeof(guint8) * 4;
460 node->fill_array_func = fill_array_numeric_variable; /* cheating ! */
461 node->check_relation_func = check_relation_numeric; /* cheating ! */
462 node->value.variable = id;
463 gnode = g_node_new(node);
469 dfilter_mknode_ipv4_variable(gint id)
474 node = g_mem_chunk_alloc(gmc_dfilter_nodes);
475 node->ntype = variable;
476 node->elem_size = sizeof(guint32);
477 node->fill_array_func = fill_array_numeric_variable; /* cheating ! */
478 node->check_relation_func = check_relation_numeric; /* cheating ! */
479 node->value.variable = id;
480 gnode = g_node_new(node);
486 dfilter_mknode_bytes_variable(gint id, gint offset, guint length)
491 node = g_mem_chunk_alloc(gmc_dfilter_nodes);
492 node->ntype = variable;
493 node->elem_size = sizeof(GByteArray*);
494 node->fill_array_func = fill_array_bytes_variable;
495 node->check_relation_func = check_relation_bytes;
496 node->value.variable = id;
497 node->offset = offset;
498 node->length = length;
499 gnode = g_node_new(node);
505 dfilter_mknode_boolean_variable(gint id)
510 node = g_mem_chunk_alloc(gmc_dfilter_nodes);
511 node->ntype = variable;
512 node->elem_size = sizeof(guint32);
513 node->fill_array_func = fill_array_boolean_variable; /* cheating ! */
514 node->check_relation_func = check_relation_boolean; /* cheating ! */
515 node->value.variable = id;
516 gnode = g_node_new(node);
522 dfilter_mknode_numeric_value(guint32 val)
527 node = g_mem_chunk_alloc(gmc_dfilter_nodes);
528 node->ntype = numeric;
529 node->elem_size = sizeof(guint32);
530 node->fill_array_func = fill_array_numeric_value;
531 node->check_relation_func = check_relation_numeric;
532 node->value.numeric = val;
533 gnode = g_node_new(node);
539 dfilter_mknode_ether_value(guint8 *ether_bytes)
544 node = g_mem_chunk_alloc(gmc_dfilter_nodes);
546 node->elem_size = sizeof(guint8) * 6;
547 node->fill_array_func = fill_array_ether_value;
548 node->check_relation_func = check_relation_ether;
550 memcpy(&node->value.ether, ether_bytes, 6);
552 gnode = g_node_new(node);
557 dfilter_mknode_ipxnet_value(guint32 ipx_net_val)
562 node = g_mem_chunk_alloc(gmc_dfilter_nodes);
563 node->ntype = ipxnet;
564 node->elem_size = sizeof(guint8) * 4;
565 node->fill_array_func = fill_array_numeric_value; /* cheating ! */
566 node->check_relation_func = check_relation_numeric; /* cheating ! */
567 node->value.numeric = ipx_net_val;
568 gnode = g_node_new(node);
574 dfilter_mknode_ipv4_value(char *host)
579 node = g_mem_chunk_alloc(gmc_dfilter_nodes);
580 node->ntype = numeric;
581 node->elem_size = sizeof(guint32);
582 node->fill_array_func = fill_array_numeric_value; /* cheating ! */
583 node->check_relation_func = check_relation_numeric; /* cheating ! */
584 node->value.numeric = get_host_ipaddr(host);
585 node->value.numeric = htonl(node->value.numeric);
586 gnode = g_node_new(node);
592 dfilter_mknode_bytes_value(GByteArray *barray)
597 node = g_mem_chunk_alloc(gmc_dfilter_nodes);
599 node->elem_size = sizeof(GByteArray*);
600 node->fill_array_func = fill_array_bytes_value;
601 node->check_relation_func = check_relation_bytes;
602 node->value.bytes = barray;
603 node->offset = G_MAXINT;
604 node->length = barray->len;
605 gnode = g_node_new(node);
611 dfilter_mknode_boolean_value(gint truth_value)
616 node = g_mem_chunk_alloc(gmc_dfilter_nodes);
617 node->ntype = numeric;
618 node->elem_size = sizeof(guint32);
619 node->fill_array_func = fill_array_boolean_value;
620 node->check_relation_func = check_relation_boolean;
621 node->value.boolean = truth_value == TOK_TRUE ? TRUE : FALSE;
622 gnode = g_node_new(node);
628 string_to_value(char *s)
633 val = strtoul(s, &endptr, 0);
634 /* I should probably check errno here */
640 dfilter_mknode_existence(gint id)
645 node = g_mem_chunk_alloc(gmc_dfilter_nodes);
646 node->ntype = existence;
647 node->elem_size = sizeof(guint32);
648 node->fill_array_func = NULL;
649 node->check_relation_func = NULL;
650 node->value.variable = id;
651 gnode = g_node_new(node);