Home | History | Annotate | Line # | Download | only in npfctl
npf_scan.l revision 1.12
      1 /*	$NetBSD: npf_scan.l,v 1.12 2013/03/20 00:29:47 christos 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 extern int		yyparsetarget;
     45 extern int		yylineno;
     46 extern const char *	yyfilename;
     47 extern int		yyparse(void);
     48 extern void		yyrestart(FILE *);
     49 
     50 void
     51 npfctl_parse_file(const char *name)
     52 {
     53 	FILE *fp;
     54 
     55 	fp = fopen(name, "r");
     56 	if (fp == NULL) {
     57 		err(EXIT_FAILURE, "open '%s'", name);
     58 	}
     59 	yyparsetarget = NPFCTL_PARSE_FILE;
     60 	yyrestart(fp);
     61 	yylineno = 1;
     62 	yycolumn = 0;
     63 	yyfilename = name;
     64 	yyparse();
     65 	fclose(fp);
     66 }
     67 
     68 void
     69 npfctl_parse_string(const char *str)
     70 {
     71 	YY_BUFFER_STATE bs;
     72 
     73 	yyparsetarget = NPFCTL_PARSE_STRING;
     74 	bs = yy_scan_string(str);
     75 	yyfilename = "stdin";
     76 	yyparse();
     77 	yy_delete_buffer(bs);
     78 }
     79 
     80 %}
     81 
     82 %option noyywrap nounput noinput
     83 
     84 ID	[a-zA-Z_][a-zA-Z_0-9]*
     85 NID	[a-zA-Z_0-9]+
     86 NUMBER	[0-9]+
     87 
     88 %%
     89 alg			return ALG;
     90 table			return TABLE;
     91 type			return TYPE;
     92 hash			return HASH;
     93 tree			return TREE;
     94 static			return TSTATIC;
     95 dynamic			return TDYNAMIC;
     96 file			return TFILE;
     97 map			return MAP;
     98 "<->"			return ARROWBOTH;
     99 "<-"			return ARROWLEFT;
    100 "->"			return ARROWRIGHT;
    101 "-"			return MINUS;
    102 procedure		return PROCEDURE;
    103 \\\n			yylineno++; yycolumn = 0;
    104 \n			yylineno++; yycolumn = 0; return SEPLINE;
    105 ;			return SEPLINE;
    106 name			return NAME;
    107 group			return GROUP;
    108 default			return DEFAULT;
    109 in			return IN;
    110 out			return OUT;
    111 forw			return FORW;
    112 interface		return INTERFACE;
    113 all			return ALL;
    114 block			return BLOCK;
    115 pass			return PASS;
    116 stateful		return STATEFUL;
    117 apply			return APPLY;
    118 final			return FINAL;
    119 quick			return FINAL;
    120 on			return ON;
    121 ifnet			return IFNET;
    122 inet6			return INET6;
    123 inet4			return INET;
    124 inet			return INET;
    125 proto			return PROTO;
    126 family			return FAMILY;
    127 tcp			return TCP;
    128 icmp			{ yylval.num = IPPROTO_ICMP; return ICMP; }
    129 ipv6-icmp		{ yylval.num = IPPROTO_ICMPV6; return ICMP6; }
    130 \"ipv6-icmp\"		{ yylval.num = IPPROTO_ICMPV6; return ICMP6; }
    131 return-rst		return RETURNRST;
    132 return-icmp		return RETURNICMP;
    133 return			return RETURN;
    134 ruleset			return RULESET;
    135 from			return FROM;
    136 to			return TO;
    137 port			return PORT;
    138 flags			return FLAGS;
    139 icmp-type		return ICMPTYPE;
    140 code			return CODE;
    141 any			return ANY;
    142 
    143 "/"			return SLASH;
    144 "{"			return CURLY_OPEN;
    145 "}"			return CURLY_CLOSE;
    146 "("			return PAR_OPEN;
    147 ")"			return PAR_CLOSE;
    148 ","			return COMMA;
    149 "="			return EQ;
    150 
    151 "0x"[0-9a-fA-F]+ {
    152 			char *endp, *buf = ecalloc(1, yyleng + 1);
    153 			buf[yyleng] = 0;
    154 			yylval.num = strtoul(buf+2, &endp, 16);
    155 			free(buf);
    156 			return HEX;
    157 		}
    158 
    159 {NUMBER}"."{NUMBER} {
    160 			char *endp, *buf = estrndup(yytext, yyleng);
    161 			yylval.fpnum = strtod(buf, &endp);
    162 			free(buf);
    163 			return FPNUM;
    164 		}
    165 
    166 [0-9a-fA-F]+":"[0-9a-fA-F:]* {
    167 			yylval.str = estrndup(yytext, yyleng);
    168 			return IPV6ADDR;
    169 		}
    170 
    171 {NUMBER}"."[0-9][0-9.]* {
    172 			yylval.str = estrndup(yytext, yyleng);
    173 			return IPV4ADDR;
    174 		}
    175 
    176 {NUMBER}	{
    177 			char *endp, *buf = estrndup(yytext, yyleng);
    178 			yylval.num = strtoul(buf, &endp, 10);
    179 			free(buf);
    180 			return NUM;
    181 		}
    182 
    183 "<"{NID}">"	{
    184 			yylval.str = estrndup(yytext + 1, yyleng - 2);
    185 			return TABLE_ID;
    186 		}
    187 
    188 "$"{NID}	{
    189 			yylval.str = estrndup(yytext + 1, yyleng - 1);
    190 			return VAR_ID;
    191 		}
    192 
    193 {ID}		{
    194 			yylval.str = estrndup(yytext, yyleng);
    195 			return IDENTIFIER;
    196 		}
    197 
    198 \"[^\"]*\"	{
    199 			yylval.str = estrndup(yytext + 1, yyleng - 2);
    200 			return STRING;
    201 		}
    202 
    203 #.*$		/* drop comment until end of line */
    204 [ \t]		/* eat whitespace */
    205 
    206 :		return COLON;
    207