aboutsummaryrefslogtreecommitdiffstats
path: root/wiretap/rt-grammar-skel.y
blob: b14d7b31f1e1db105fd8e5b6b7c2505f29bd1384 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
%{

#ifndef __G_LIB_H__
#include <glib.h>
#endif

#include "rt-compile.h"
#include "rt-global.h"
#include "ct-compile.h"

GList* (*mk_bytecmp) (int ftype, int rel_opcode, guint8 *bytes);
void (*mk_optimize) (GList *L);

/* The encapsulation type for which we are compiling the filter */
int comp_encap_type;
int filter_parsed = 0;

bytecmp_info *bytecmp;
eitherof_info *either;
GList *L1, *L2;

/* ct-compile: bytecmp_table */
/*bytecmp_info bytecmp_table[] = {
	{ ETH_TYPE, 12, 2 },
	{ TR_DST, 2, 6 },
	{ TR_SRC, 8, 6 },
	{ ETH_DSTVENDOR, 0, 3 },
	{ 0, 0, 0 }
};*/

/* ct-compile: eitherof_table */
/*eitherof_table[] = {
	{ TR_VENDOR, CTYPE_BYTECMP, TR_SRCVENDOR, TR_DSTVENDOR },
	{ TR_ADDR, CTYPE_BYTECMP, TR_SRCADDR, TR_DSTADDR }
};
*/
%}

%union {
	gint	d;
	guint8	*b;
	GString	*s;
	GList	*L;
}

%type <d>	bytecmp_lval
%type <L>	sentence bytecmp_relation
%type <d>	bytecmp_test;

%token <b>	BYTES
%token <s>	QUOTED TEXT
%token <d>	NUMBER
%token <d>	EQ NE

/* ct-compile: yacc tokens */

%%

paragraph: /* EMPTY */
		| paragraph sentence { mk_optimize($2); filter_parsed = 1; }
		;

sentence:	bytecmp_relation { $$ = $1 }
		;
	

bytecmp_relation:	bytecmp_lval bytecmp_test BYTES
{
	bytecmp = lookup_bytecmp($1);
	if (bytecmp->ctype == CTYPE_EITHEROF) {
		either = lookup_eitherof($1);
		L1 = mk_bytecmp(either->field1, $2, $3);
		L2 = mk_bytecmp(either->field2, $2, $3);
		$$ = g_list_concat(L1, L2);
	}
	else {
		$$ = mk_bytecmp($1, $2, $3);
	}
}
		;

/* ct-compile: bytecmp_lval */
/*bytecmp_lval:	TR_DST { $$ = TR_DST; }
	|	TR_SRC { $$ = TR_SRC; }
	|	TR_SRCVENDOR { $$ = TR_SRCVENDOR; }
	|	TR_DSTVENDOR { $$ = TR_DSTVENDOR; }
	;*/

bytecmp_test:	EQ { $$ = EQ; }
	|	NE { $$ = NE; }
	;

%%

bytecmp_info*
lookup_bytecmp(int ftype)
{
	bytecmp_info *b = &bytecmp_table[0];
	bytecmp_info *ret_val = NULL;

	/* find the field in the table */
	while (b->ftype != 0) {
		if (b->ftype == ftype) {
			ret_val = b;
			break;
		}
		else {
			b++;
		}
	}

	return ret_val;
}


eitherof_info*
lookup_eitherof(int ftype)
{
	eitherof_info *e = &eitherof_table[0];
	eitherof_info *ret_val = NULL;

	/* find the field in the table */
	while (e->ftype != 0) {
		if (e->ftype == ftype) {
			ret_val = e;
			break;
		}
		else {
			e++;
		}
	}

	return ret_val;
}