1 %{
2 /* $OpenBSD: scanner.l,v 1.19 2007/01/02 18:31:21 reyk Exp $ */
3
4 /*
5 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that: (1) source code distributions
10 * retain the above copyright notice and this paragraph in its entirety, (2)
11 * distributions including binary code include the above copyright notice and
12 * this paragraph in its entirety in the documentation or other materials
13 * provided with the distribution, and (3) all advertising materials mentioning
14 * features or use of this software display the following acknowledgement:
15 * ``This product includes software developed by the University of California,
16 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
17 * the University nor the names of its contributors may be used to endorse
18 * or promote products derived from this software without specific prior
19 * written permission.
20 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
21 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
22 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 */
24
25 #include <sys/types.h>
26 #include <sys/time.h>
27
28 #include <ctype.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <vis.h>
32
33 #include "pcap-int.h"
34
35 #include "gencode.h"
36 #include <pcap-namedb.h>
37
38 #ifdef INET6
39 #include <netdb.h>
40 #include <sys/socket.h>
41 #endif /*INET6*/
42
43 __RCSID("$Header: /cvs/src/lib/libpcap/scanner.l,v 1.19 2007/01/02 18:31:21 reyk Exp $ (LBL)");
44 __RCSID("$MirOS: src/lib/libpcap/scanner.l,v 1.2 2007/04/28 00:12:45 tg Exp $");
45
46 #ifdef HAVE_OS_PROTO_H
47 #include "os-proto.h"
48 #endif
49
50 #include "grammar.h"
51
52 static int stoi(char *);
53 static inline int xdtoi(int);
54
55 #ifdef FLEX_SCANNER
56 #define YY_NO_UNPUT
57 #undef YY_INPUT
58 #define YY_INPUT(buf, result, max)\
59 {\
60 char *src = in_buffer;\
61 int i;\
62 \
63 if (*src == 0)\
64 result = YY_NULL;\
65 else {\
66 for (i = 0; *src && i < max; ++i)\
67 buf[i] = *src++;\
68 in_buffer += i;\
69 result = i;\
70 }\
71 }
72 #else
73 #undef getc
74 #define getc(fp) (*in_buffer == 0 ? EOF : *in_buffer++)
75 #endif
76
77 extern YYSTYPE yylval;
78
79 static char *in_buffer;
80
81 %}
82
83 N ([0-9]+|(0X|0x)[0-9A-Fa-f]+)
84 B ([0-9A-Fa-f][0-9A-Fa-f]?)
85 W ([0-9A-Fa-f][0-9A-Fa-f]?[0-9A-Fa-f]?[0-9A-Fa-f]?)
86
87 %a 15000
88 %o 17000
89 %e 6000
90 %k 4000
91 %p 19000
92
93 V680 {W}:{W}:{W}:{W}:{W}:{W}:{W}:{W}
94
95 V670 ::{W}:{W}:{W}:{W}:{W}:{W}:{W}
96 V671 {W}::{W}:{W}:{W}:{W}:{W}:{W}
97 V672 {W}:{W}::{W}:{W}:{W}:{W}:{W}
98 V673 {W}:{W}:{W}::{W}:{W}:{W}:{W}
99 V674 {W}:{W}:{W}:{W}::{W}:{W}:{W}
100 V675 {W}:{W}:{W}:{W}:{W}::{W}:{W}
101 V676 {W}:{W}:{W}:{W}:{W}:{W}::{W}
102 V677 {W}:{W}:{W}:{W}:{W}:{W}:{W}::
103
104 V660 ::{W}:{W}:{W}:{W}:{W}:{W}
105 V661 {W}::{W}:{W}:{W}:{W}:{W}
106 V662 {W}:{W}::{W}:{W}:{W}:{W}
107 V663 {W}:{W}:{W}::{W}:{W}:{W}
108 V664 {W}:{W}:{W}:{W}::{W}:{W}
109 V665 {W}:{W}:{W}:{W}:{W}::{W}
110 V666 {W}:{W}:{W}:{W}:{W}:{W}::
111
112 V650 ::{W}:{W}:{W}:{W}:{W}
113 V651 {W}::{W}:{W}:{W}:{W}
114 V652 {W}:{W}::{W}:{W}:{W}
115 V653 {W}:{W}:{W}::{W}:{W}
116 V654 {W}:{W}:{W}:{W}::{W}
117 V655 {W}:{W}:{W}:{W}:{W}::
118
119 V640 ::{W}:{W}:{W}:{W}
120 V641 {W}::{W}:{W}:{W}
121 V642 {W}:{W}::{W}:{W}
122 V643 {W}:{W}:{W}::{W}
123 V644 {W}:{W}:{W}:{W}::
124
125 V630 ::{W}:{W}:{W}
126 V631 {W}::{W}:{W}
127 V632 {W}:{W}::{W}
128 V633 {W}:{W}:{W}::
129
130 V620 ::{W}:{W}
131 V621 {W}::{W}
132 V622 {W}:{W}::
133
134 V610 ::{W}
135 V611 {W}::
136
137 V600 ::
138
139 V6604 {W}:{W}:{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
140
141 V6504 ::{W}:{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
142 V6514 {W}::{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
143 V6524 {W}:{W}::{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
144 V6534 {W}:{W}:{W}::{W}:{W}:{N}\.{N}\.{N}\.{N}
145 V6544 {W}:{W}:{W}:{W}::{W}:{N}\.{N}\.{N}\.{N}
146 V6554 {W}:{W}:{W}:{W}:{W}::{N}\.{N}\.{N}\.{N}
147
148 V6404 ::{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
149 V6414 {W}::{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
150 V6424 {W}:{W}::{W}:{W}:{N}\.{N}\.{N}\.{N}
151 V6434 {W}:{W}:{W}::{W}:{N}\.{N}\.{N}\.{N}
152 V6444 {W}:{W}:{W}:{W}::{N}\.{N}\.{N}\.{N}
153
154 V6304 ::{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
155 V6314 {W}::{W}:{W}:{N}\.{N}\.{N}\.{N}
156 V6324 {W}:{W}::{W}:{N}\.{N}\.{N}\.{N}
157 V6334 {W}:{W}:{W}::{N}\.{N}\.{N}\.{N}
158
159 V6204 ::{W}:{W}:{N}\.{N}\.{N}\.{N}
160 V6214 {W}::{W}:{N}\.{N}\.{N}\.{N}
161 V6224 {W}:{W}::{N}\.{N}\.{N}\.{N}
162
163 V6104 ::{W}:{N}\.{N}\.{N}\.{N}
164 V6114 {W}::{N}\.{N}\.{N}\.{N}
165
166 V6004 ::{N}\.{N}\.{N}\.{N}
167
168
169 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})
170
171 %%
172 dst return DST;
173 src return SRC;
174
175 link|ether|ppp|slip return LINK;
176 fddi return LINK;
177 arp return ARP;
178 rarp return RARP;
179 ip return IP;
180 tcp return TCP;
181 udp return UDP;
182 icmp return ICMP;
183 igmp return IGMP;
184 igrp return IGRP;
185 pim return PIM;
186
187 ip6 return IPV6;
188 icmp6 return ICMPV6;
189 ah return AH;
190 esp return ESP;
191
192 atalk return ATALK;
193 decnet return DECNET;
194 lat return LAT;
195 sca return SCA;
196 moprc return MOPRC;
197 mopdl return MOPDL;
198 stp return STP;
199
200 host return HOST;
201 net return NET;
202 mask return MASK;
203 port return PORT;
204 proto return PROTO;
205 protochain {
206 #ifdef NO_PROTOCHAIN
207 bpf_error("%s not supported", yytext);
208 #else
209 return PROTOCHAIN;
210 #endif
211 }
212
213 gateway return GATEWAY;
214
215 less return LESS;
216 greater return GREATER;
217 byte return BYTE;
218 broadcast return TK_BROADCAST;
219 multicast return TK_MULTICAST;
220
221 and|"&&" return AND;
222 or|"||" return OR;
223 not return '!';
224
225 len|length return LEN;
226 inbound return INBOUND;
227 outbound return OUTBOUND;
228
229 on|ifname return PF_IFNAME;
230 rset|ruleset return PF_RSET;
231 rnr|rulenum return PF_RNR;
232 srnr|subrulenum return PF_SRNR;
233 reason return PF_REASON;
234 action return PF_ACTION;
235
236 wlan return LINK;
237 type return TYPE;
238 subtype return SUBTYPE;
239 direction|dir return DIR;
240 address1|addr1 return ADDR1;
241 address2|addr2 return ADDR2;
242 address3|addr3 return ADDR3;
243 address4|addr4 return ADDR4;
244
245 [ \n\t] ;
246 [+\-*/:\[\]!<>()&|=] return yytext[0];
247 ">=" return GEQ;
248 "<=" return LEQ;
249 "!=" return NEQ;
250 "==" return '=';
251 "<<" return LSH;
252 ">>" return RSH;
253 {N} { yylval.i = stoi((char *)yytext); return NUM; }
254 ({N}\.{N})|({N}\.{N}\.{N})|({N}\.{N}\.{N}\.{N}) {
255 yylval.s = sdup((char *)yytext); return HID; }
256 {B}:{B}:{B}:{B}:{B}:{B} { yylval.e = pcap_ether_aton((char *)yytext);
257 return EID; }
258 {V6} {
259 #ifdef INET6
260 struct addrinfo hints, *res;
261 memset(&hints, 0, sizeof(hints));
262 hints.ai_family = AF_INET6;
263 hints.ai_flags = AI_NUMERICHOST;
264 if (getaddrinfo(yytext, NULL, &hints, &res))
265 bpf_error("bogus IPv6 address %s", yytext);
266 else {
267 yylval.e = sdup((char *)yytext); return HID6;
268 }
269 #else
270 bpf_error("IPv6 address %s not supported", yytext);
271 #endif /*INET6*/
272 }
273 {B}:+({B}:+)+ { bpf_error("bogus ethernet address %s", yytext); }
274 [A-Za-z0-9][-_.A-Za-z0-9]*[.A-Za-z0-9] {
275 yylval.s = sdup((char *)yytext); return ID; }
276 [A-Za-z] { yylval.s = sdup((char *)yytext); return ID; }
277 "\\"[^ !()\n\t]+ { yylval.s = sdup((char *)yytext + 1); return ID; }
278 [^ \[\]\t\n\-_.A-Za-z0-9!<>()&|=]+ {
279 size_t len = strlen(yytext) * 4 + 1;
280 char *v = malloc(len);
281 if (v != NULL)
282 strnvis(v, yytext, len, 0);
283 bpf_error("illegal token: %s", v);
284 free(v);
285 }
286 . {
287 char v[5];
288
289 vis(v, *yytext, VIS_OCTAL, 0);
290 bpf_error("illegal char '%s'", v);
291 }
292 %%
293 void
lex_init(buf)294 lex_init(buf)
295 char *buf;
296 {
297 in_buffer = buf;
298 yyrestart(NULL);
299 }
300
301 /*
302 * Also define a yywrap. Note that if we're using flex, it will
303 * define a macro to map this identifier to pcap_wrap.
304 */
305 int
yywrap()306 yywrap()
307 {
308 return 1;
309 }
310
311 /* Hex digit to integer. */
312 static inline int
xdtoi(c)313 xdtoi(c)
314 register int c;
315 {
316 if (isdigit(c))
317 return c - '0';
318 else if (islower(c))
319 return c - 'a' + 10;
320 else
321 return c - 'A' + 10;
322 }
323
324 /*
325 * Convert string to integer. Just like atoi(), but checks for
326 * preceding 0x or 0 and uses hex or octal instead of decimal.
327 */
328 static int
stoi(s)329 stoi(s)
330 char *s;
331 {
332 int base = 10;
333 int n = 0;
334
335 if (*s == '0') {
336 if (s[1] == 'x' || s[1] == 'X') {
337 s += 2;
338 base = 16;
339 }
340 else {
341 base = 8;
342 s += 1;
343 }
344 }
345 while (*s)
346 n = n * base + xdtoi(*s++);
347
348 return n;
349 }
350