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