]> The Tcpdump Group git mirrors - libpcap/blob - scanner.l
Fix typpo.
[libpcap] / scanner.l
1 %top {
2 /* Must come first for _LARGE_FILE_API on AIX. */
3 #ifdef HAVE_CONFIG_H
4 #include <config.h>
5 #endif
6
7 /*
8 * Must come first to avoid warnings on Windows.
9 *
10 * Flex-generated scanners may only include <inttypes.h> if __STDC_VERSION__
11 * is defined with a value >= 199901, meaning "full C99", and MSVC may not
12 * define it with that value, because it isn't 100% C99-compliant, even
13 * though it has an <inttypes.h> capable of defining everything the Flex
14 * scanner needs.
15 *
16 * We, however, will include it if we know we have an MSVC version that has
17 * it; this means that we may define the INTn_MAX and UINTn_MAX values in
18 * scanner.c, and then include <stdint.h>, which may define them differently
19 * (same value, but different string of characters), causing compiler warnings.
20 *
21 * If we include it here, and they're defined, that'll prevent scanner.c
22 * from defining them. So we include <pcap/pcap-inttypes.h>, to get
23 * <inttypes.h> if we have it.
24 */
25 #include <pcap/pcap-inttypes.h>
26 }
27
28 /*
29 * We want a reentrant scanner.
30 */
31 %option reentrant
32
33 /*
34 * And we need to pass the compiler state to the scanner.
35 */
36 %option extra-type="compiler_state_t *"
37
38 /*
39 * We don't use input, so don't generate code for it.
40 */
41 %option noinput
42
43 /*
44 * We don't use unput, so don't generate code for it.
45 */
46 %option nounput
47
48 /*
49 * We don't read from the terminal.
50 */
51 %option never-interactive
52
53 /*
54 * We want to stop processing when we get to the end of the input.
55 */
56 %option noyywrap
57
58 /*
59 * We want to generate code that can be used by a reentrant parser
60 * generated by Bison or Berkeley YACC.
61 */
62 %option bison-bridge
63
64 %{
65 /*
66 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
67 * The Regents of the University of California. All rights reserved.
68 *
69 * Redistribution and use in source and binary forms, with or without
70 * modification, are permitted provided that: (1) source code distributions
71 * retain the above copyright notice and this paragraph in its entirety, (2)
72 * distributions including binary code include the above copyright notice and
73 * this paragraph in its entirety in the documentation or other materials
74 * provided with the distribution, and (3) all advertising materials mentioning
75 * features or use of this software display the following acknowledgement:
76 * ``This product includes software developed by the University of California,
77 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
78 * the University nor the names of its contributors may be used to endorse
79 * or promote products derived from this software without specific prior
80 * written permission.
81 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
82 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
83 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
84 */
85
86 #include <ctype.h>
87 #include <string.h>
88
89 #include "pcap-int.h"
90
91 #include "gencode.h"
92
93 #include "grammar.h"
94
95 /*
96 * Earlier versions of Flex don't declare these, so we declare them
97 * ourselves to squelch warnings.
98 */
99 int pcap_get_column(yyscan_t);
100 void pcap_set_column(int, yyscan_t);
101
102 #ifdef INET6
103
104 #ifdef _WIN32
105 #include <winsock2.h>
106 #include <ws2tcpip.h>
107 /*
108 * To quote the MSDN page for getaddrinfo() at
109 *
110 * https://msdn.microsoft.com/en-us/library/windows/desktop/ms738520(v=vs.85).aspx
111 *
112 * "Support for getaddrinfo on Windows 2000 and older versions
113 * The getaddrinfo function was added to the Ws2_32.dll on Windows XP and
114 * later. To execute an application that uses this function on earlier
115 * versions of Windows, then you need to include the Ws2tcpip.h and
116 * Wspiapi.h files. When the Wspiapi.h include file is added, the
117 * getaddrinfo function is defined to the WspiapiGetAddrInfo inline
118 * function in the Wspiapi.h file. At runtime, the WspiapiGetAddrInfo
119 * function is implemented in such a way that if the Ws2_32.dll or the
120 * Wship6.dll (the file containing getaddrinfo in the IPv6 Technology
121 * Preview for Windows 2000) does not include getaddrinfo, then a
122 * version of getaddrinfo is implemented inline based on code in the
123 * Wspiapi.h header file. This inline code will be used on older Windows
124 * platforms that do not natively support the getaddrinfo function."
125 *
126 * We use getaddrinfo(), so we include Wspiapi.h here.
127 */
128 #include <wspiapi.h>
129 #else /* _WIN32 */
130 #include <sys/socket.h> /* for "struct sockaddr" in "struct addrinfo" */
131 #include <netdb.h> /* for "struct addrinfo" */
132 #endif /* _WIN32 */
133
134 /* Workaround for AIX 4.3 */
135 #if !defined(AI_NUMERICHOST)
136 #define AI_NUMERICHOST 0x04
137 #endif
138
139 #endif /*INET6*/
140
141 #include <pcap/namedb.h>
142 #include "grammar.h"
143
144 #ifdef HAVE_OS_PROTO_H
145 #include "os-proto.h"
146 #endif
147
148 static int stoi(char *);
149 static inline int xdtoi(int);
150
151 %}
152
153 N ([0-9]+|(0X|0x)[0-9A-Fa-f]+)
154 B ([0-9A-Fa-f][0-9A-Fa-f]?)
155 B2 ([0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f])
156 W ([0-9A-Fa-f][0-9A-Fa-f]?[0-9A-Fa-f]?[0-9A-Fa-f]?)
157
158 %a 18400
159 %o 21500
160 %e 7600
161 %k 4550
162 %p 27600
163 %n 2000
164
165 V680 {W}:{W}:{W}:{W}:{W}:{W}:{W}:{W}
166
167 V670 ::{W}:{W}:{W}:{W}:{W}:{W}:{W}
168 V671 {W}::{W}:{W}:{W}:{W}:{W}:{W}
169 V672 {W}:{W}::{W}:{W}:{W}:{W}:{W}
170 V673 {W}:{W}:{W}::{W}:{W}:{W}:{W}
171 V674 {W}:{W}:{W}:{W}::{W}:{W}:{W}
172 V675 {W}:{W}:{W}:{W}:{W}::{W}:{W}
173 V676 {W}:{W}:{W}:{W}:{W}:{W}::{W}
174 V677 {W}:{W}:{W}:{W}:{W}:{W}:{W}::
175
176 V660 ::{W}:{W}:{W}:{W}:{W}:{W}
177 V661 {W}::{W}:{W}:{W}:{W}:{W}
178 V662 {W}:{W}::{W}:{W}:{W}:{W}
179 V663 {W}:{W}:{W}::{W}:{W}:{W}
180 V664 {W}:{W}:{W}:{W}::{W}:{W}
181 V665 {W}:{W}:{W}:{W}:{W}::{W}
182 V666 {W}:{W}:{W}:{W}:{W}:{W}::
183
184 V650 ::{W}:{W}:{W}:{W}:{W}
185 V651 {W}::{W}:{W}:{W}:{W}
186 V652 {W}:{W}::{W}:{W}:{W}
187 V653 {W}:{W}:{W}::{W}:{W}
188 V654 {W}:{W}:{W}:{W}::{W}
189 V655 {W}:{W}:{W}:{W}:{W}::
190
191 V640 ::{W}:{W}:{W}:{W}
192 V641 {W}::{W}:{W}:{W}
193 V642 {W}:{W}::{W}:{W}
194 V643 {W}:{W}:{W}::{W}
195 V644 {W}:{W}:{W}:{W}::
196
197 V630 ::{W}:{W}:{W}
198 V631 {W}::{W}:{W}
199 V632 {W}:{W}::{W}
200 V633 {W}:{W}:{W}::
201
202 V620 ::{W}:{W}
203 V621 {W}::{W}
204 V622 {W}:{W}::
205
206 V610 ::{W}
207 V611 {W}::
208
209 V600 ::
210
211 V6604 {W}:{W}:{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
212
213 V6504 ::{W}:{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
214 V6514 {W}::{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
215 V6524 {W}:{W}::{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
216 V6534 {W}:{W}:{W}::{W}:{W}:{N}\.{N}\.{N}\.{N}
217 V6544 {W}:{W}:{W}:{W}::{W}:{N}\.{N}\.{N}\.{N}
218 V6554 {W}:{W}:{W}:{W}:{W}::{N}\.{N}\.{N}\.{N}
219
220 V6404 ::{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
221 V6414 {W}::{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
222 V6424 {W}:{W}::{W}:{W}:{N}\.{N}\.{N}\.{N}
223 V6434 {W}:{W}:{W}::{W}:{N}\.{N}\.{N}\.{N}
224 V6444 {W}:{W}:{W}:{W}::{N}\.{N}\.{N}\.{N}
225
226 V6304 ::{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
227 V6314 {W}::{W}:{W}:{N}\.{N}\.{N}\.{N}
228 V6324 {W}:{W}::{W}:{N}\.{N}\.{N}\.{N}
229 V6334 {W}:{W}:{W}::{N}\.{N}\.{N}\.{N}
230
231 V6204 ::{W}:{W}:{N}\.{N}\.{N}\.{N}
232 V6214 {W}::{W}:{N}\.{N}\.{N}\.{N}
233 V6224 {W}:{W}::{N}\.{N}\.{N}\.{N}
234
235 V6104 ::{W}:{N}\.{N}\.{N}\.{N}
236 V6114 {W}::{N}\.{N}\.{N}\.{N}
237
238 V6004 ::{N}\.{N}\.{N}\.{N}
239
240
241 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})
242
243 MAC ({B}:{B}:{B}:{B}:{B}:{B}|{B}\-{B}\-{B}\-{B}\-{B}\-{B}|{B}\.{B}\.{B}\.{B}\.{B}\.{B}|{B2}\.{B2}\.{B2}|{B2}{3})
244
245
246
247 %%
248 dst return DST;
249 src return SRC;
250
251 link|ether|ppp|slip return LINK;
252 fddi|tr|wlan return LINK;
253 arp return ARP;
254 rarp return RARP;
255 ip return IP;
256 sctp return SCTP;
257 tcp return TCP;
258 udp return UDP;
259 icmp return ICMP;
260 igmp return IGMP;
261 igrp return IGRP;
262 pim return PIM;
263 vrrp return VRRP;
264 carp return CARP;
265 radio return RADIO;
266
267 ip6 return IPV6;
268 icmp6 return ICMPV6;
269 ah return AH;
270 esp return ESP;
271
272 atalk return ATALK;
273 aarp return AARP;
274 decnet return DECNET;
275 lat return LAT;
276 sca return SCA;
277 moprc return MOPRC;
278 mopdl return MOPDL;
279
280 iso return ISO;
281 esis return ESIS;
282 es-is return ESIS;
283 isis return ISIS;
284 is-is return ISIS;
285 l1 return L1;
286 l2 return L2;
287 iih return IIH;
288 lsp return LSP;
289 snp return SNP;
290 csnp return CSNP;
291 psnp return PSNP;
292
293 clnp return CLNP;
294
295 stp return STP;
296
297 ipx return IPX;
298
299 netbeui return NETBEUI;
300
301 host return HOST;
302 net return NET;
303 mask return NETMASK;
304 port return PORT;
305 portrange return PORTRANGE;
306 proto return PROTO;
307 protochain {
308 #ifdef NO_PROTOCHAIN
309 bpf_error(yyextra, "%s not supported", yytext);
310 #else
311 return PROTOCHAIN;
312 #endif
313 }
314
315 gateway return GATEWAY;
316
317 type return TYPE;
318 subtype return SUBTYPE;
319 direction|dir return DIR;
320 address1|addr1 return ADDR1;
321 address2|addr2 return ADDR2;
322 address3|addr3 return ADDR3;
323 address4|addr4 return ADDR4;
324 ra return RA;
325 ta return TA;
326
327 less return LESS;
328 greater return GREATER;
329 byte return CBYTE;
330 broadcast return TK_BROADCAST;
331 multicast return TK_MULTICAST;
332
333 and|"&&" return AND;
334 or|"||" return OR;
335 not return '!';
336
337 len|length return LEN;
338 inbound return INBOUND;
339 outbound return OUTBOUND;
340
341 vlan return VLAN;
342 mpls return MPLS;
343 pppoed return PPPOED;
344 pppoes return PPPOES;
345 geneve return GENEVE;
346
347 lane return LANE;
348 llc return LLC;
349 metac return METAC;
350 bcc return BCC;
351 oam return OAM;
352 oamf4 return OAMF4;
353 oamf4ec return OAMF4EC;
354 oamf4sc return OAMF4SC;
355 sc return SC;
356 ilmic return ILMIC;
357 vpi return VPI;
358 vci return VCI;
359 connectmsg return CONNECTMSG;
360 metaconnect return METACONNECT;
361
362 on|ifname return PF_IFNAME;
363 rset|ruleset return PF_RSET;
364 rnr|rulenum return PF_RNR;
365 srnr|subrulenum return PF_SRNR;
366 reason return PF_REASON;
367 action return PF_ACTION;
368
369 fisu return FISU;
370 lssu return LSSU;
371 lsu return LSSU;
372 msu return MSU;
373 hfisu return HFISU;
374 hlssu return HLSSU;
375 hmsu return HMSU;
376 sio return SIO;
377 opc return OPC;
378 dpc return DPC;
379 sls return SLS;
380 hsio return HSIO;
381 hopc return HOPC;
382 hdpc return HDPC;
383 hsls return HSLS;
384
385 [ \r\n\t] ;
386 [+\-*/%:\[\]!<>()&|\^=] return yytext[0];
387 ">=" return GEQ;
388 "<=" return LEQ;
389 "!=" return NEQ;
390 "==" return '=';
391 "<<" return LSH;
392 ">>" return RSH;
393 ${B} { yylval->e = pcap_ether_aton(((char *)yytext)+1);
394 if (yylval->e == NULL)
395 bpf_error(yyextra, "malloc");
396 return AID; }
397 {MAC} { yylval->e = pcap_ether_aton((char *)yytext);
398 if (yylval->e == NULL)
399 bpf_error(yyextra, "malloc");
400 return EID; }
401 {N} { yylval->i = stoi((char *)yytext); return NUM; }
402 ({N}\.{N})|({N}\.{N}\.{N})|({N}\.{N}\.{N}\.{N}) {
403 yylval->s = sdup(yyextra, (char *)yytext); return HID; }
404 {V6} {
405 #ifdef INET6
406 struct addrinfo hints, *res;
407 memset(&hints, 0, sizeof(hints));
408 hints.ai_family = AF_INET6;
409 hints.ai_flags = AI_NUMERICHOST;
410 if (getaddrinfo(yytext, NULL, &hints, &res))
411 bpf_error(yyextra, "bogus IPv6 address %s", yytext);
412 else {
413 freeaddrinfo(res);
414 yylval->s = sdup(yyextra, (char *)yytext); return HID6;
415 }
416 #else
417 bpf_error(yyextra, "IPv6 address %s not supported", yytext);
418 #endif /*INET6*/
419 }
420 {B}:+({B}:+)+ { bpf_error(yyextra, "bogus ethernet address %s", yytext); }
421 icmptype { yylval->i = 0; return NUM; }
422 icmpcode { yylval->i = 1; return NUM; }
423 icmp-echoreply { yylval->i = 0; return NUM; }
424 icmp-unreach { yylval->i = 3; return NUM; }
425 icmp-sourcequench { yylval->i = 4; return NUM; }
426 icmp-redirect { yylval->i = 5; return NUM; }
427 icmp-echo { yylval->i = 8; return NUM; }
428 icmp-routeradvert { yylval->i = 9; return NUM; }
429 icmp-routersolicit { yylval->i = 10; return NUM; }
430 icmp-timxceed { yylval->i = 11; return NUM; }
431 icmp-paramprob { yylval->i = 12; return NUM; }
432 icmp-tstamp { yylval->i = 13; return NUM; }
433 icmp-tstampreply { yylval->i = 14; return NUM; }
434 icmp-ireq { yylval->i = 15; return NUM; }
435 icmp-ireqreply { yylval->i = 16; return NUM; }
436 icmp-maskreq { yylval->i = 17; return NUM; }
437 icmp-maskreply { yylval->i = 18; return NUM; }
438
439 icmp6type { yylval->i = 0; return NUM; }
440 icmp6code { yylval->i = 1; return NUM; }
441
442 icmp6-echo { yylval->i = 128; return NUM; }
443 icmp6-echoreply { yylval->i = 129; return NUM; }
444 icmp6-multicastlistenerquery { yylval->i = 130; return NUM; }
445 icmp6-multicastlistenerreportv1 { yylval->i = 131; return NUM; }
446 icmp6-multicastlistenerdone { yylval->i = 132; return NUM; }
447 icmp6-routersolicit { yylval->i = 133; return NUM; }
448 icmp6-routeradvert { yylval->i = 134; return NUM; }
449 icmp6-neighborsolicit { yylval->i = 135; return NUM; }
450 icmp6-neighboradvert { yylval->i = 136; return NUM; }
451 icmp6-redirect { yylval->i = 137; return NUM; }
452 icmp6-routerrenum { yylval->i = 138; return NUM; }
453 icmp6-nodeinformationquery { yylval->i = 139; return NUM; }
454 icmp6-nodeinformationresponse { yylval->i = 140; return NUM; }
455 icmp6-ineighbordiscoverysolicit { yylval->i = 141; return NUM; }
456 icmp6-ineighbordiscoveryadvert { yylval->i = 142; return NUM; }
457 icmp6-multicastlistenerreportv2 { yylval->i = 143; return NUM; }
458 icmp6-homeagentdiscoveryrequest { yylval->i = 144; return NUM; }
459 icmp6-homeagentdiscoveryreply { yylval->i = 145; return NUM; }
460 icmp6-mobileprefixsolicit { yylval->i = 146; return NUM; }
461 icmp6-mobileprefixadvert { yylval->i = 147; return NUM; }
462 icmp6-certpathsolicit { yylval->i = 148; return NUM; }
463 icmp6-certpathadvert { yylval->i = 149; return NUM; }
464 icmp6-multicastrouteradvert { yylval->i = 151; return NUM; }
465 icmp6-multicastroutersolicit { yylval->i = 152; return NUM; }
466 icmp6-multicastrouterterm { yylval->i = 153; return NUM; }
467
468 tcpflags { yylval->i = 13; return NUM; }
469 tcp-fin { yylval->i = 0x01; return NUM; }
470 tcp-syn { yylval->i = 0x02; return NUM; }
471 tcp-rst { yylval->i = 0x04; return NUM; }
472 tcp-push { yylval->i = 0x08; return NUM; }
473 tcp-ack { yylval->i = 0x10; return NUM; }
474 tcp-urg { yylval->i = 0x20; return NUM; }
475 tcp-ece { yylval->i = 0x40; return NUM; }
476 tcp-cwr { yylval->i = 0x80; return NUM; }
477 [A-Za-z0-9]([-_.A-Za-z0-9]*[.A-Za-z0-9])? {
478 yylval->s = sdup(yyextra, (char *)yytext); return ID; }
479 "\\"[^ !()\n\t]+ { yylval->s = sdup(yyextra, (char *)yytext + 1); return ID; }
480 [^ \[\]\t\n\-_.A-Za-z0-9!<>()&|=]+ {
481 bpf_error(yyextra, "illegal token: %s", yytext); }
482 . { bpf_error(yyextra, "illegal char '%c'", *yytext); }
483 %%
484
485 /* Hex digit to integer. */
486 static inline int
487 xdtoi(c)
488 register int c;
489 {
490 if (isdigit(c))
491 return c - '0';
492 else if (islower(c))
493 return c - 'a' + 10;
494 else
495 return c - 'A' + 10;
496 }
497
498 /*
499 * Convert string to integer. Just like atoi(), but checks for
500 * preceding 0x or 0 and uses hex or octal instead of decimal.
501 */
502 static int
503 stoi(s)
504 char *s;
505 {
506 int base = 10;
507 int n = 0;
508
509 if (*s == '0') {
510 if (s[1] == 'x' || s[1] == 'X') {
511 s += 2;
512 base = 16;
513 }
514 else {
515 base = 8;
516 s += 1;
517 }
518 }
519 while (*s)
520 n = n * base + xdtoi(*s++);
521
522 return n;
523 }