aboutsummaryrefslogtreecommitdiffstats
path: root/dfilter-scanner.l
blob: 53b1b13f25e564577b3fefcfdb56abb49401f388 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
%{

/* dfilter-scanner.l
 * Scanner for display filters
 *
 * $Id: dfilter-scanner.l,v 1.10 1999/08/20 21:19:27 gram Exp $
 *
 * Ethereal - Network traffic analyzer
 * By Gerald Combs <gerald@zing.org>
 * Copyright 1998 Gerald Combs
 *
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif

#ifndef _STDIO_H
#include <stdio.h>
#endif

#ifndef _STRING_H
#include <string.h>
#endif

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

#ifndef __PROTO_H__
#include "proto.h"
#endif

#ifndef __DFILTER_H__
#include "dfilter.h"
#endif

#include "dfilter-int.h"

#include "dfilter-grammar.h"

/* Flex has a few routines which help us get the scanner to read
 * from a string rather than from a file. POSIX lex only provides
 * for reading from a file; any method of reading from a string
 * is inherently non-portable.  Besides reading from a string,
 * we have to worry about resetting the scanner after a bad
 * parse; this too is non-portable. Combine the reset with
 * a string input, and you have major non-portability. I'll provide
 * the routines for flex here. If you really want to modify the
 * scanner and use a non-flex lex implementation, you may
 * add more ifdef's below.
 */
#ifdef FLEX_SCANNER

/* Flex has built-in support for using a string as an input source
 * instead of using a file. Nice!
 */
YY_BUFFER_STATE	string_input_buffer;

#else

static char *in_buffer;
#undef getc
#define getc(fp)  (*in_buffer == 0 ? EOF : *in_buffer++)

#endif

%}

whitespace	[\t ]
hex		[A-Fa-f0-9]{1,2}
hexsep		[-:\.]

%%

[\t\n ]+	/* ignore whitespace */


and|\&\&	{ dfilter_lval.operand = TOK_AND; return TOK_AND; }
or|\|\|		{ dfilter_lval.operand = TOK_OR; return TOK_OR; }
not|\!		{ dfilter_lval.operand = TOK_NOT; return TOK_NOT; }
xor|\^\^	{ dfilter_lval.operand = TOK_XOR; return TOK_XOR; }
eq|\=\=		{ dfilter_lval.operand = TOK_EQ; return TOK_EQ; }
ne|\!\=		{ dfilter_lval.operand = TOK_NE; return TOK_NE; }
gt|\>		{ dfilter_lval.operand = TOK_GT; return TOK_GT; }
ge|\>\=		{ dfilter_lval.operand = TOK_GE; return TOK_GE; }
lt|\<		{ dfilter_lval.operand = TOK_LT; return TOK_LT; }
le|\<\=		{ dfilter_lval.operand = TOK_LE; return TOK_LE; }

true		{ dfilter_lval.operand = TOK_TRUE; return TOK_TRUE; }
false		{ dfilter_lval.operand = TOK_FALSE; return TOK_FALSE; }

\[{whitespace}*-?[0-9]+{whitespace}*:{whitespace}*[0-9]+{whitespace}*\] { /* range [ x : y ] */

	char	*byterange_string = g_strdup(yytext);
	char	*s = byterange_string + 1; /* I don't want the first '[' */
	char	*p;

	/* Get the offset from the string */
	if ((p = strtok(s, ":"))) {
		dfilter_lval.byte_range.offset = strtol(p, NULL, 10);
	}
	else {
		g_free(byterange_string);
		return 0;
	}

	/* Get the Length from the string */
	if ((p = strtok(NULL, "]"))) {
		dfilter_lval.byte_range.length = strtoul(p, NULL, 10);
	}
	else {
		g_free(byterange_string);
		return 0;
	}
	g_free(byterange_string);
	return T_VAL_BYTE_RANGE;
}


{hex}({hexsep}{hex})+ {			/* byte string, any length */
	dfilter_lval.string = g_strdup(yytext);
	return T_VAL_BYTE_STRING;
}


[A-Za-z][A-Za-z0-9\.\_]+	{	/* looks like a protocol or field name */

	int retval = 0;
	enum ftenum ftype;
	dfilter_lval.variable = dfilter_lookup_token(yytext);
	if (dfilter_lval.variable == 0) {
		dfilter_lval.string = g_strdup(yytext);
		return T_VAL_UNQUOTED_STRING;
	}
	
	ftype = proto_registrar_get_ftype(dfilter_lval.variable);
	switch (ftype) {
		case FT_UINT8:
		case FT_VALS_UINT8:
			retval = T_FT_UINT8;
			break;
		case FT_UINT16:
		case FT_VALS_UINT16:
			retval = T_FT_UINT16;
			break;
		case FT_UINT32:
		case FT_VALS_UINT32:
		case FT_VALS_UINT24:
			retval = T_FT_UINT32;
			break;
		case FT_ETHER:
			retval = T_FT_ETHER;
			break;
		case FT_IPv4:
			retval = T_FT_IPv4;
			break;
		case FT_NONE:
			retval = T_FT_NONE;
			break;
		case FT_BYTES:
			retval = T_FT_BYTES;
			break;
		case FT_BOOLEAN:
			retval = T_FT_BOOLEAN;
			break;
		case FT_IPXNET:
			retval = T_FT_IPXNET;
			break;
		default:
			g_assert_not_reached();
			retval = 0;
			break;
	}
	return retval;
}

[0-9]+ {				/* decimal or octal values */
	dfilter_lval.string = g_strdup(yytext);
	return T_VAL_NUMBER_STRING;
}


0[xX][A-Fa-f0-9]+ {			/* hex values */
	dfilter_lval.string = g_strdup(yytext);
	return T_VAL_NUMBER_STRING;
}

[0-9\:\.]+ {
	dfilter_lval.string = g_strdup(yytext);
	return T_VAL_UNQUOTED_STRING;
}

.	return yytext[0];
%%

/* Resets scanner and assigns the char* argument
 * as the text to scan
 */
void
dfilter_scanner_text(char *text)
{
#ifdef FLEX_SCANNER
	string_input_buffer = yy_scan_string(text);
#else
	in_buffer = text;
#endif
}

void
dfilter_scanner_cleanup(void)
{
#ifdef FLEX_SCANNER
	yy_delete_buffer(string_input_buffer);
#else
	/* There is no standard way to reset a lex scanner.
	 * This is necessary after a failed parse on a syntactically
	 * incorrect display filter. You have to reset the scanner
	 * so that yy_lex() doesn't start scanning from the middle
	 * of the previous input string.
	 */
#endif
}

/* Flex has an option '%option noyywrap' so that I don't have to
 * provide this yywrap function, but in order to maintain portability,
 * I'll just use this yywrap() function.
 */
int
yywrap()
{
	return 1; /* stop at EOF, instead of looking for next file */
}

/* converts a string representing a byte array
 * to a guint8 array.
 *
 * Returns a non-null GByteArray pointer on success, NULL on failure.
 */
GByteArray*
byte_str_to_guint8_array(const char *s)
{
	GByteArray	*barray;
	guint8		val;
	char		*byte_str = g_strdup(s); /* local copy of string */
	char		*p, *str;

	barray = g_byte_array_new();
	/* XXX - don't use global_df, but pass in pointer to GSList* */
	global_df->list_of_byte_arrays = g_slist_append(global_df->list_of_byte_arrays, barray);

	byte_str = g_strdup(s);
	str = byte_str;
	while ((p = strtok(str, "-:."))) {
		val = (guint8) strtoul(p, NULL, 16);
		g_byte_array_append(barray, &val, 1);

		/* subsequent calls to strtok() require NULL as arg 1 */
		str = NULL;
	}

	g_free(byte_str);
	return barray;
}