npf_scan.l revision 1.1.4.3 1 /* $NetBSD: npf_scan.l,v 1.1.4.3 2012/10/30 19:00:44 yamt Exp $ */
2
3 /*-
4 * Copyright (c) 2011-2012 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Martin Husemann.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 %{
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <err.h>
36
37 #include "npfctl.h"
38 #include "npf_parse.h"
39
40 int yycolumn;
41
42 #define YY_USER_ACTION yycolumn += yyleng;
43
44 %}
45
46 %option noyywrap nounput noinput
47
48 ID [a-zA-Z_][a-zA-Z_0-9]*
49 NID [a-zA-Z_0-9]+
50 NUMBER [0-9]+
51
52 %%
53 table return TABLE;
54 type return TYPE;
55 hash return HASH;
56 tree return TREE;
57 static return TSTATIC;
58 dynamic return TDYNAMIC;
59 file return TFILE;
60 map return MAP;
61 "<->" return ARROWBOTH;
62 "<-" return ARROWLEFT;
63 "->" return ARROWRIGHT;
64 "-" return MINUS;
65 procedure return PROCEDURE;
66 \\\n yylineno++; yycolumn = 0;
67 \n yylineno++; yycolumn = 0; return SEPLINE;
68 ; return SEPLINE;
69 name return NAME;
70 group return GROUP;
71 default return DEFAULT;
72 in return IN;
73 out return OUT;
74 interface return INTERFACE;
75 all return ALL;
76 block return BLOCK;
77 pass return PASS;
78 stateful return STATEFUL;
79 apply return APPLY;
80 final return FINAL;
81 quick return FINAL;
82 on return ON;
83 inet6 return INET6;
84 inet4 return INET;
85 inet return INET;
86 proto return PROTO;
87 family return FAMILY;
88 tcp return TCP;
89 icmp { yylval.num = IPPROTO_ICMP; return ICMP; }
90 ipv6-icmp { yylval.num = IPPROTO_ICMPV6; return ICMP6; }
91 \"ipv6-icmp\" { yylval.num = IPPROTO_ICMPV6; return ICMP6; }
92 return-rst return RETURNRST;
93 return-icmp return RETURNICMP;
94 return return RETURN;
95 from return FROM;
96 to return TO;
97 port return PORT;
98 flags return FLAGS;
99 icmp-type return ICMPTYPE;
100 code return CODE;
101 any return ANY;
102
103 "/" return SLASH;
104 "{" return CURLY_OPEN;
105 "}" return CURLY_CLOSE;
106 "(" return PAR_OPEN;
107 ")" return PAR_CLOSE;
108 "," return COMMA;
109 "=" return EQ;
110
111 "0x"[0-9a-fA-F]+ {
112 char *endp, *buf = zalloc(yyleng + 1);
113 buf[yyleng] = 0;
114 yylval.num = strtoul(buf+2, &endp, 16);
115 free(buf);
116 return HEX;
117 }
118
119 {NUMBER}"."{NUMBER} {
120 char *endp, *buf = xstrndup(yytext, yyleng);
121 yylval.fpnum = strtod(buf, &endp);
122 free(buf);
123 return FPNUM;
124 }
125
126 [0-9a-fA-F]+":"[0-9a-fA-F:]* {
127 yylval.str = xstrndup(yytext, yyleng);
128 return IPV6ADDR;
129 }
130
131 {NUMBER}"."[0-9][0-9.]* {
132 yylval.str = xstrndup(yytext, yyleng);
133 return IPV4ADDR;
134 }
135
136 {NUMBER} {
137 char *endp, *buf = xstrndup(yytext, yyleng);
138 yylval.num = strtoul(buf, &endp, 10);
139 free(buf);
140 return NUM;
141 }
142
143 "<"{NID}">" {
144 yylval.str = xstrndup(yytext + 1, yyleng - 2);
145 return TABLE_ID;
146 }
147
148 "$"{NID} {
149 yylval.str = xstrndup(yytext + 1, yyleng - 1);
150 return VAR_ID;
151 }
152
153 {ID} {
154 yylval.str = xstrndup(yytext, yyleng);
155 return IDENTIFIER;
156 }
157
158 \"[^\"]*\" {
159 yylval.str = xstrndup(yytext + 1, yyleng - 2);
160 return STRING;
161 }
162
163 #.*$ /* drop comment until end of line */
164 [ \t] /* eat whitespace */
165
166 : return COLON;
167