]> The Tcpdump Group git mirrors - libpcap/blob - scanner.l
Get ready for libpcap 0.7.2
[libpcap] / scanner.l
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
23 #ifndef lint
24 static const char rcsid[] =
25 "@(#) $Header: /tcpdump/master/libpcap/scanner.l,v 1.81.2.4 2002-06-29 07:45:01 guy Exp $ (LBL)";
26 #endif
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <ctype.h>
33 #include <string.h>
34
35 #include "pcap-int.h"
36
37 #include "gencode.h"
38 #ifdef INET6
39 #include <sys/socket.h> /* for "struct sockaddr" in "struct addrinfo" */
40 #include <netdb.h> /* for "struct addrinfo" */
41 /* Workaround for AIX 4.3 */
42 #if !defined(AI_NUMERICHOST)
43 #define AI_NUMERICHOST 0x04
44 #endif
45 #endif /*INET6*/
46 #include <pcap-namedb.h>
47 #include "tokdefs.h"
48
49 #ifdef HAVE_OS_PROTO_H
50 #include "os-proto.h"
51 #endif
52
53 static int stoi(char *);
54 static inline int xdtoi(int);
55
56 #ifdef FLEX_SCANNER
57 #define YY_NO_UNPUT
58 static YY_BUFFER_STATE in_buffer;
59 #else
60 static char *in_buffer;
61
62 #undef getc
63 #define getc(fp) (*in_buffer == 0 ? EOF : *in_buffer++)
64 #endif
65
66 #define yylval pcap_lval
67 extern YYSTYPE yylval;
68
69 %}
70
71 N ([0-9]+|(0X|0x)[0-9A-Fa-f]+)
72 B ([0-9A-Fa-f][0-9A-Fa-f]?)
73 W ([0-9A-Fa-f][0-9A-Fa-f]?[0-9A-Fa-f]?[0-9A-Fa-f]?)
74
75 %a 15000
76 %o 19000
77 %e 6000
78 %k 4000
79 %p 21000
80 %n 2000
81
82 V680 {W}:{W}:{W}:{W}:{W}:{W}:{W}:{W}
83
84 V670 ::{W}:{W}:{W}:{W}:{W}:{W}:{W}
85 V671 {W}::{W}:{W}:{W}:{W}:{W}:{W}
86 V672 {W}:{W}::{W}:{W}:{W}:{W}:{W}
87 V673 {W}:{W}:{W}::{W}:{W}:{W}:{W}
88 V674 {W}:{W}:{W}:{W}::{W}:{W}:{W}
89 V675 {W}:{W}:{W}:{W}:{W}::{W}:{W}
90 V676 {W}:{W}:{W}:{W}:{W}:{W}::{W}
91 V677 {W}:{W}:{W}:{W}:{W}:{W}:{W}::
92
93 V660 ::{W}:{W}:{W}:{W}:{W}:{W}
94 V661 {W}::{W}:{W}:{W}:{W}:{W}
95 V662 {W}:{W}::{W}:{W}:{W}:{W}
96 V663 {W}:{W}:{W}::{W}:{W}:{W}
97 V664 {W}:{W}:{W}:{W}::{W}:{W}
98 V665 {W}:{W}:{W}:{W}:{W}::{W}
99 V666 {W}:{W}:{W}:{W}:{W}:{W}::
100
101 V650 ::{W}:{W}:{W}:{W}:{W}
102 V651 {W}::{W}:{W}:{W}:{W}
103 V652 {W}:{W}::{W}:{W}:{W}
104 V653 {W}:{W}:{W}::{W}:{W}
105 V654 {W}:{W}:{W}:{W}::{W}
106 V655 {W}:{W}:{W}:{W}:{W}::
107
108 V640 ::{W}:{W}:{W}:{W}
109 V641 {W}::{W}:{W}:{W}
110 V642 {W}:{W}::{W}:{W}
111 V643 {W}:{W}:{W}::{W}
112 V644 {W}:{W}:{W}:{W}::
113
114 V630 ::{W}:{W}:{W}
115 V631 {W}::{W}:{W}
116 V632 {W}:{W}::{W}
117 V633 {W}:{W}:{W}::
118
119 V620 ::{W}:{W}
120 V621 {W}::{W}
121 V622 {W}:{W}::
122
123 V610 ::{W}
124 V611 {W}::
125
126 V600 ::
127
128 V6604 {W}:{W}:{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
129
130 V6504 ::{W}:{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
131 V6514 {W}::{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
132 V6524 {W}:{W}::{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
133 V6534 {W}:{W}:{W}::{W}:{W}:{N}\.{N}\.{N}\.{N}
134 V6544 {W}:{W}:{W}:{W}::{W}:{N}\.{N}\.{N}\.{N}
135 V6554 {W}:{W}:{W}:{W}:{W}::{N}\.{N}\.{N}\.{N}
136
137 V6404 ::{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
138 V6414 {W}::{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
139 V6424 {W}:{W}::{W}:{W}:{N}\.{N}\.{N}\.{N}
140 V6434 {W}:{W}:{W}::{W}:{N}\.{N}\.{N}\.{N}
141 V6444 {W}:{W}:{W}:{W}::{N}\.{N}\.{N}\.{N}
142
143 V6304 ::{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
144 V6314 {W}::{W}:{W}:{N}\.{N}\.{N}\.{N}
145 V6324 {W}:{W}::{W}:{N}\.{N}\.{N}\.{N}
146 V6334 {W}:{W}:{W}::{N}\.{N}\.{N}\.{N}
147
148 V6204 ::{W}:{W}:{N}\.{N}\.{N}\.{N}
149 V6214 {W}::{W}:{N}\.{N}\.{N}\.{N}
150 V6224 {W}:{W}::{N}\.{N}\.{N}\.{N}
151
152 V6104 ::{W}:{N}\.{N}\.{N}\.{N}
153 V6114 {W}::{N}\.{N}\.{N}\.{N}
154
155 V6004 ::{N}\.{N}\.{N}\.{N}
156
157
158 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})
159
160 %%
161 dst return DST;
162 src return SRC;
163
164 link|ether|ppp|slip return LINK;
165 fddi|tr return LINK;
166 arp return ARP;
167 rarp return RARP;
168 ip return IP;
169 sctp return SCTP;
170 tcp return TCP;
171 udp return UDP;
172 icmp return ICMP;
173 igmp return IGMP;
174 igrp return IGRP;
175 pim return PIM;
176 vrrp return VRRP;
177
178 ip6 {
179 #ifdef INET6
180 return IPV6;
181 #else
182 bpf_error("%s not supported", yytext);
183 #endif
184 }
185 icmp6 {
186 #ifdef INET6
187 return ICMPV6;
188 #else
189 bpf_error("%s not supported", yytext);
190 #endif
191 }
192 ah return AH;
193 esp return ESP;
194
195 atalk return ATALK;
196 aarp return AARP;
197 decnet return DECNET;
198 lat return LAT;
199 sca return SCA;
200 moprc return MOPRC;
201 mopdl return MOPDL;
202
203 iso return ISO;
204 esis return ESIS;
205 es-is return ESIS;
206 isis return ISIS;
207 is-is return ISIS;
208 clnp return CLNP;
209
210 stp return STP;
211
212 ipx return IPX;
213
214 netbeui return NETBEUI;
215
216 host return HOST;
217 net return NET;
218 mask return MASK;
219 port return PORT;
220 proto return PROTO;
221 protochain {
222 #ifdef NO_PROTOCHAIN
223 bpf_error("%s not supported", yytext);
224 #else
225 return PROTOCHAIN;
226 #endif
227 }
228
229 gateway return GATEWAY;
230
231 less return LESS;
232 greater return GREATER;
233 byte return BYTE;
234 broadcast return TK_BROADCAST;
235 multicast return TK_MULTICAST;
236
237 and|"&&" return AND;
238 or|"||" return OR;
239 not return '!';
240
241 len|length return LEN;
242 inbound return INBOUND;
243 outbound return OUTBOUND;
244
245 vlan return VLAN;
246
247 [ \n\t] ;
248 [+\-*/:\[\]!<>()&|=] return yytext[0];
249 ">=" return GEQ;
250 "<=" return LEQ;
251 "!=" return NEQ;
252 "==" return '=';
253 "<<" return LSH;
254 ">>" return RSH;
255 ${B} { yylval.e = pcap_ether_aton(((char *)yytext)+1);
256 return AID; }
257 {N} { yylval.i = stoi((char *)yytext); return NUM; }
258 ({N}\.{N})|({N}\.{N}\.{N})|({N}\.{N}\.{N}\.{N}) {
259 yylval.s = sdup((char *)yytext); return HID; }
260 {B}:{B}:{B}:{B}:{B}:{B} { yylval.e = pcap_ether_aton((char *)yytext);
261 return EID; }
262 {V6} {
263 #ifdef INET6
264 struct addrinfo hints, *res;
265 memset(&hints, 0, sizeof(hints));
266 hints.ai_family = AF_INET6;
267 hints.ai_flags = AI_NUMERICHOST;
268 if (getaddrinfo(yytext, NULL, &hints, &res))
269 bpf_error("bogus IPv6 address %s", yytext);
270 else {
271 yylval.e = sdup((char *)yytext); return HID6;
272 }
273 #else
274 bpf_error("IPv6 address %s not supported", yytext);
275 #endif /*INET6*/
276 }
277 {B}:+({B}:+)+ { bpf_error("bogus ethernet address %s", yytext); }
278 icmptype { yylval.i = 0; return NUM; }
279 icmpcode { yylval.i = 1; return NUM; }
280 icmp-echoreply { yylval.i = 0; return NUM; }
281 icmp-unreach { yylval.i = 3; return NUM; }
282 icmp-sourcequench { yylval.i = 4; return NUM; }
283 icmp-redirect { yylval.i = 5; return NUM; }
284 icmp-echo { yylval.i = 8; return NUM; }
285 icmp-routeradvert { yylval.i = 9; return NUM; }
286 icmp-routersolicit { yylval.i = 10; return NUM; }
287 icmp-timxceed { yylval.i = 11; return NUM; }
288 icmp-paramprob { yylval.i = 12; return NUM; }
289 icmp-tstamp { yylval.i = 13; return NUM; }
290 icmp-tstampreply { yylval.i = 14; return NUM; }
291 icmp-ireq { yylval.i = 15; return NUM; }
292 icmp-ireqreply { yylval.i = 16; return NUM; }
293 icmp-maskreq { yylval.i = 17; return NUM; }
294 icmp-maskreply { yylval.i = 18; return NUM; }
295 tcpflags { yylval.i = 13; return NUM; }
296 tcp-fin { yylval.i = 0x01; return NUM; }
297 tcp-syn { yylval.i = 0x02; return NUM; }
298 tcp-rst { yylval.i = 0x04; return NUM; }
299 tcp-push { yylval.i = 0x08; return NUM; }
300 tcp-ack { yylval.i = 0x10; return NUM; }
301 tcp-urg { yylval.i = 0x20; return NUM; }
302 [A-Za-z0-9]([-_.A-Za-z0-9]*[.A-Za-z0-9])? {
303 yylval.s = sdup((char *)yytext); return ID; }
304 "\\"[^ !()\n\t]+ { yylval.s = sdup((char *)yytext + 1); return ID; }
305 [^ \[\]\t\n\-_.A-Za-z0-9!<>()&|=]+ {
306 bpf_error("illegal token: %s", yytext); }
307 . { bpf_error("illegal char '%c'", *yytext); }
308 %%
309 void
310 lex_init(buf)
311 char *buf;
312 {
313 #ifdef FLEX_SCANNER
314 in_buffer = yy_scan_string(buf);
315 #else
316 in_buffer = buf;
317 #endif
318 }
319
320 /*
321 * Do any cleanup necessary after parsing.
322 */
323 void
324 lex_cleanup()
325 {
326 #ifdef FLEX_SCANNER
327 if (in_buffer != NULL)
328 yy_delete_buffer(in_buffer);
329 in_buffer = NULL;
330 #endif
331 }
332
333 /*
334 * Also define a yywrap. Note that if we're using flex, it will
335 * define a macro to map this identifier to pcap_wrap.
336 */
337 int
338 yywrap()
339 {
340 return 1;
341 }
342
343 /* Hex digit to integer. */
344 static inline int
345 xdtoi(c)
346 register int c;
347 {
348 if (isdigit(c))
349 return c - '0';
350 else if (islower(c))
351 return c - 'a' + 10;
352 else
353 return c - 'A' + 10;
354 }
355
356 /*
357 * Convert string to integer. Just like atoi(), but checks for
358 * preceding 0x or 0 and uses hex or octal instead of decimal.
359 */
360 static int
361 stoi(s)
362 char *s;
363 {
364 int base = 10;
365 int n = 0;
366
367 if (*s == '0') {
368 if (s[1] == 'x' || s[1] == 'X') {
369 s += 2;
370 base = 16;
371 }
372 else {
373 base = 8;
374 s += 1;
375 }
376 }
377 while (*s)
378 n = n * base + xdtoi(*s++);
379
380 return n;
381 }