xref: /freebsd/contrib/libpcap/scanner.l (revision daf1cffce2e07931f27c6c6998652e90df6ba87e)
1 %{
2 /*
3  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that: (1) source code distributions
8  * retain the above copyright notice and this paragraph in its entirety, (2)
9  * distributions including binary code include the above copyright notice and
10  * this paragraph in its entirety in the documentation or other materials
11  * provided with the distribution, and (3) all advertising materials mentioning
12  * features or use of this software display the following acknowledgement:
13  * ``This product includes software developed by the University of California,
14  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
15  * the University nor the names of its contributors may be used to endorse
16  * or promote products derived from this software without specific prior
17  * written permission.
18  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
19  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21  *
22  * $FreeBSD$
23  */
24 
25 #ifndef lint
26 static const char rcsid[] =
27     "@(#) $Header: /tcpdump/master/libpcap/scanner.l,v 1.60 1999/11/17 04:09:58 assar Exp $ (LBL)";
28 #endif
29 
30 #include <sys/types.h>
31 #include <sys/time.h>
32 
33 #include <ctype.h>
34 #include <unistd.h>
35 #include <string.h>
36 
37 #include "pcap-int.h"
38 
39 #include "gencode.h"
40 #include <pcap-namedb.h>
41 #ifdef INET6
42 #include <netdb.h>
43 #include <sys/socket.h>
44 #endif /*INET6*/
45 #include "tokdefs.h"
46 
47 #include "gnuc.h"
48 #ifdef HAVE_OS_PROTO_H
49 #include "os-proto.h"
50 #endif
51 
52 static int stoi(char *);
53 static inline int xdtoi(int);
54 
55 #ifdef FLEX_SCANNER
56 #define YY_NO_UNPUT
57 #undef YY_INPUT
58 #define YY_INPUT(buf, result, max)\
59  {\
60 	char *src = in_buffer;\
61 	int i;\
62 \
63 	if (*src == 0)\
64 		result = YY_NULL;\
65 	else {\
66 		for (i = 0; *src && i < max; ++i)\
67 			buf[i] = *src++;\
68 		in_buffer += i;\
69 		result = i;\
70 	}\
71  }
72 #else
73 #undef getc
74 #define getc(fp)  (*in_buffer == 0 ? EOF : *in_buffer++)
75 #endif
76 
77 #define yylval pcap_lval
78 extern YYSTYPE yylval;
79 
80 static char *in_buffer;
81 
82 %}
83 
84 N		([0-9]+|(0X|0x)[0-9A-Fa-f]+)
85 B		([0-9A-Fa-f][0-9A-Fa-f]?)
86 W		([0-9A-Fa-f][0-9A-Fa-f]?[0-9A-Fa-f]?[0-9A-Fa-f]?)
87 
88 %a 15000
89 %o 17000
90 %e 6000
91 %k 4000
92 %p 19000
93 
94 V680		{W}:{W}:{W}:{W}:{W}:{W}:{W}:{W}
95 
96 V670		::{W}:{W}:{W}:{W}:{W}:{W}:{W}
97 V671		{W}::{W}:{W}:{W}:{W}:{W}:{W}
98 V672		{W}:{W}::{W}:{W}:{W}:{W}:{W}
99 V673		{W}:{W}:{W}::{W}:{W}:{W}:{W}
100 V674		{W}:{W}:{W}:{W}::{W}:{W}:{W}
101 V675		{W}:{W}:{W}:{W}:{W}::{W}:{W}
102 V676		{W}:{W}:{W}:{W}:{W}:{W}::{W}
103 V677		{W}:{W}:{W}:{W}:{W}:{W}:{W}::
104 
105 V660		::{W}:{W}:{W}:{W}:{W}:{W}
106 V661		{W}::{W}:{W}:{W}:{W}:{W}
107 V662		{W}:{W}::{W}:{W}:{W}:{W}
108 V663		{W}:{W}:{W}::{W}:{W}:{W}
109 V664		{W}:{W}:{W}:{W}::{W}:{W}
110 V665		{W}:{W}:{W}:{W}:{W}::{W}
111 V666		{W}:{W}:{W}:{W}:{W}:{W}::
112 
113 V650		::{W}:{W}:{W}:{W}:{W}
114 V651		{W}::{W}:{W}:{W}:{W}
115 V652		{W}:{W}::{W}:{W}:{W}
116 V653		{W}:{W}:{W}::{W}:{W}
117 V654		{W}:{W}:{W}:{W}::{W}
118 V655		{W}:{W}:{W}:{W}:{W}::
119 
120 V640		::{W}:{W}:{W}:{W}
121 V641		{W}::{W}:{W}:{W}
122 V642		{W}:{W}::{W}:{W}
123 V643		{W}:{W}:{W}::{W}
124 V644		{W}:{W}:{W}:{W}::
125 
126 V630		::{W}:{W}:{W}
127 V631		{W}::{W}:{W}
128 V632		{W}:{W}::{W}
129 V633		{W}:{W}:{W}::
130 
131 V620		::{W}:{W}
132 V621		{W}::{W}
133 V622		{W}:{W}::
134 
135 V610		::{W}
136 V611		{W}::
137 
138 V600		::
139 
140 V6604		{W}:{W}:{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
141 
142 V6504		::{W}:{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
143 V6514		{W}::{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
144 V6524		{W}:{W}::{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
145 V6534		{W}:{W}:{W}::{W}:{W}:{N}\.{N}\.{N}\.{N}
146 V6544		{W}:{W}:{W}:{W}::{W}:{N}\.{N}\.{N}\.{N}
147 V6554		{W}:{W}:{W}:{W}:{W}::{N}\.{N}\.{N}\.{N}
148 
149 V6404		::{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
150 V6414		{W}::{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
151 V6424		{W}:{W}::{W}:{W}:{N}\.{N}\.{N}\.{N}
152 V6434		{W}:{W}:{W}::{W}:{N}\.{N}\.{N}\.{N}
153 V6444		{W}:{W}:{W}:{W}::{N}\.{N}\.{N}\.{N}
154 
155 V6304		::{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
156 V6314		{W}::{W}:{W}:{N}\.{N}\.{N}\.{N}
157 V6324		{W}:{W}::{W}:{N}\.{N}\.{N}\.{N}
158 V6334		{W}:{W}:{W}::{N}\.{N}\.{N}\.{N}
159 
160 V6204		::{W}:{W}:{N}\.{N}\.{N}\.{N}
161 V6214		{W}::{W}:{N}\.{N}\.{N}\.{N}
162 V6224		{W}:{W}::{N}\.{N}\.{N}\.{N}
163 
164 V6104		::{W}:{N}\.{N}\.{N}\.{N}
165 V6114		{W}::{N}\.{N}\.{N}\.{N}
166 
167 V6004		::{N}\.{N}\.{N}\.{N}
168 
169 
170 V6		({V680}|{V670}|{V671}|{V672}|{V673}|{V674}|{V675}|{V676}|{V677}|{V660}|{V661}|{V662}|{V663}|{V664}|{V665}|{V666}|{V650}|{V651}|{V652}|{V653}|{V654}|{V655}|{V640}|{V641}|{V642}|{V643}|{V644}|{V630}|{V631}|{V632}|{V633}|{V620}|{V621}|{V622}|{V610}|{V611}|{V600}|{V6604}|{V6504}|{V6514}|{V6524}|{V6534}|{V6544}|{V6554}|{V6404}|{V6414}|{V6424}|{V6434}|{V6444}|{V6304}|{V6314}|{V6324}|{V6334}|{V6204}|{V6214}|{V6224}|{V6104}|{V6114}|{V6004})
171 
172 %%
173 dst		return DST;
174 src		return SRC;
175 
176 link|ether|ppp|slip  return LINK;
177 fddi		return LINK;
178 arp		return ARP;
179 rarp		return RARP;
180 ip		return IP;
181 tcp		return TCP;
182 udp		return UDP;
183 icmp		return ICMP;
184 igmp		return IGMP;
185 igrp		return IGRP;
186 pim		return PIM;
187 
188 ip6		return IPV6;
189 icmp6		return ICMPV6;
190 ah		return AH;
191 esp		return ESP;
192 
193 atalk		return ATALK;
194 decnet		return DECNET;
195 lat		return LAT;
196 sca		return SCA;
197 moprc		return MOPRC;
198 mopdl		return MOPDL;
199 
200 iso		return ISO;
201 esis		return ESIS;
202 es-is		return ESIS;
203 isis		return ISIS;
204 is-is		return ISIS;
205 
206 host		return HOST;
207 net		return NET;
208 mask		return MASK;
209 port		return PORT;
210 proto		return PROTO;
211 protochain	{
212 #ifdef NO_PROTOCHAIN
213 		  bpf_error("%s not supported", yytext);
214 #else
215 		  return PROTOCHAIN;
216 #endif
217 		}
218 
219 gateway		return GATEWAY;
220 
221 less		return LESS;
222 greater		return GREATER;
223 byte		return BYTE;
224 broadcast	return TK_BROADCAST;
225 multicast	return TK_MULTICAST;
226 
227 and|"&&"	return AND;
228 or|"||"		return OR;
229 not		return '!';
230 
231 len|length	return LEN;
232 inbound		return INBOUND;
233 outbound	return OUTBOUND;
234 
235 [ \n\t]			;
236 [+\-*/:\[\]!<>()&|=]	return yytext[0];
237 ">="			return GEQ;
238 "<="			return LEQ;
239 "!="			return NEQ;
240 "=="			return '=';
241 "<<"			return LSH;
242 ">>"			return RSH;
243 {N}			{ yylval.i = stoi((char *)yytext); return NUM; }
244 ({N}\.{N})|({N}\.{N}\.{N})|({N}\.{N}\.{N}\.{N})	{
245 			yylval.s = sdup((char *)yytext); return HID; }
246 {B}:{B}:{B}:{B}:{B}:{B} { yylval.e = pcap_ether_aton((char *)yytext);
247 			  return EID; }
248 {V6}			{
249 #ifdef INET6
250 			  struct addrinfo hints, *res;
251 			  memset(&hints, 0, sizeof(hints));
252 			  hints.ai_family = AF_INET6;
253 			  hints.ai_flags = AI_NUMERICHOST;
254 			  if (getaddrinfo(yytext, NULL, &hints, &res))
255 				bpf_error("bogus IPv6 address %s", yytext);
256 			  else {
257 				yylval.e = sdup((char *)yytext); return HID6;
258 			  }
259 #else
260 			  bpf_error("IPv6 address %s not supported", yytext);
261 #endif /*INET6*/
262 			}
263 {B}:+({B}:+)+		{ bpf_error("bogus ethernet address %s", yytext); }
264 [A-Za-z0-9][-_.A-Za-z0-9]*[.A-Za-z0-9] {
265 			 yylval.s = sdup((char *)yytext); return ID; }
266 "\\"[^ !()\n\t]+	{ yylval.s = sdup((char *)yytext + 1); return ID; }
267 [^ \[\]\t\n\-_.A-Za-z0-9!<>()&|=]+i {
268 			bpf_error("illegal token: %s\n", yytext); }
269 .			{ bpf_error("illegal char '%c'", *yytext); }
270 %%
271 void
272 lex_init(buf)
273 	char *buf;
274 {
275 	in_buffer = buf;
276 }
277 
278 /*
279  * Also define a yywrap.  Note that if we're using flex, it will
280  * define a macro to map this identifier to pcap_wrap.
281  */
282 int
283 yywrap()
284 {
285 	return 1;
286 }
287 
288 /* Hex digit to integer. */
289 static inline int
290 xdtoi(c)
291 	register int c;
292 {
293 	if (isdigit(c))
294 		return c - '0';
295 	else if (islower(c))
296 		return c - 'a' + 10;
297 	else
298 		return c - 'A' + 10;
299 }
300 
301 /*
302  * Convert string to integer.  Just like atoi(), but checks for
303  * preceding 0x or 0 and uses hex or octal instead of decimal.
304  */
305 static int
306 stoi(s)
307 	char *s;
308 {
309 	int base = 10;
310 	int n = 0;
311 
312 	if (*s == '0') {
313 		if (s[1] == 'x' || s[1] == 'X') {
314 			s += 2;
315 			base = 16;
316 		}
317 		else {
318 			base = 8;
319 			s += 1;
320 		}
321 	}
322 	while (*s)
323 		n = n * base + xdtoi(*s++);
324 
325 	return n;
326 }
327 
328