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