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