Home | History | Annotate | Line # | Download | only in npfctl
npf_scan.l revision 1.5
      1 /*	$NetBSD: npf_scan.l,v 1.5 2012/07/19 21:52:29 spz 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 <err.h>
     35 
     36 #include "npfctl.h"
     37 #include "npf_parse.h"
     38 
     39 int	yycolumn;
     40 
     41 #define	YY_USER_ACTION	yycolumn += yyleng;
     42 
     43 %}
     44 
     45 %option noyywrap nounput noinput
     46 
     47 ID	[a-zA-Z_][a-zA-Z_0-9]*
     48 NID	[a-zA-Z_0-9]+
     49 NUMBER	[0-9]+
     50 
     51 %%
     52 table			return TABLE;
     53 type			return TYPE;
     54 hash			return HASH;
     55 tree			return TREE;
     56 static			return TSTATIC;
     57 dynamic			return TDYNAMIC;
     58 file			return TFILE;
     59 map			return MAP;
     60 "<->"			return ARROWBOTH;
     61 "<-"			return ARROWLEFT;
     62 "->"			return ARROWRIGHT;
     63 "-"			return MINUS;
     64 procedure		return PROCEDURE;
     65 \\\n			yylineno++; yycolumn = 0;
     66 \n			yylineno++; yycolumn = 0; return SEPLINE;
     67 ;			return SEPLINE;
     68 name			return NAME;
     69 group			return GROUP;
     70 default			return DEFAULT;
     71 in			return IN;
     72 out			return OUT;
     73 interface		return INTERFACE;
     74 all			return ALL;
     75 block			return BLOCK;
     76 pass			return PASS;
     77 stateful		return STATEFUL;
     78 apply			return APPLY;
     79 final			return FINAL;
     80 quick			return FINAL;
     81 on			return ON;
     82 inet6			return INET6;
     83 inet4			return INET;
     84 inet			return INET;
     85 proto			return PROTO;
     86 family			return FAMILY;
     87 tcp			return TCP;
     88 icmp			{ yylval.num = IPPROTO_ICMP; return ICMP; }
     89 ipv6-icmp		{ yylval.num = IPPROTO_ICMPV6; return ICMP6; }
     90 \"ipv6-icmp\"		{ yylval.num = IPPROTO_ICMPV6; return ICMP6; }
     91 return-rst		return RETURNRST;
     92 return-icmp		return RETURNICMP;
     93 return			return RETURN;
     94 from			return FROM;
     95 to			return TO;
     96 port			return PORT;
     97 flags			return FLAGS;
     98 icmp-type		return ICMPTYPE;
     99 code			return CODE;
    100 any			return ANY;
    101 
    102 "/"			return SLASH;
    103 "{"			return CURLY_OPEN;
    104 "}"			return CURLY_CLOSE;
    105 "("			return PAR_OPEN;
    106 ")"			return PAR_CLOSE;
    107 ","			return COMMA;
    108 "="			return EQ;
    109 
    110 "0x"[0-9a-fA-F]+ {
    111 			char *endp, *buf = zalloc(yyleng + 1);
    112 			buf[yyleng] = 0;
    113 			yylval.num = strtoul(buf+2, &endp, 16);
    114 			free(buf);
    115 			return HEX;
    116 		}
    117 
    118 [0-9a-fA-F]+":"[0-9a-fA-F:]* {
    119 			yylval.str = xstrndup(yytext, yyleng);
    120 			return IPV6ADDR;
    121 		}
    122 
    123 {NUMBER}"."[0-9][0-9.]* {
    124 			yylval.str = xstrndup(yytext, yyleng);
    125 			return IPV4ADDR;
    126 		}
    127 
    128 {NUMBER}	{
    129 			char *endp, *buf = xstrndup(yytext, yyleng);
    130 			yylval.num = strtoul(buf, &endp, 10);
    131 			free(buf);
    132 			return NUM;
    133 		}
    134 
    135 "<"{NID}">"	{
    136 			yylval.str = xstrndup(yytext + 1, yyleng - 2);
    137 			return TABLE_ID;
    138 		}
    139 
    140 "$"{NID}	{
    141 			yylval.str = xstrndup(yytext + 1, yyleng - 1);
    142 			return VAR_ID;
    143 		}
    144 
    145 {ID}		{
    146 			yylval.str = xstrndup(yytext, yyleng);
    147 			return IDENTIFIER;
    148 		}
    149 
    150 \"[^\"]*\"	{
    151 			yylval.str = xstrndup(yytext + 1, yyleng - 2);
    152 			return STRING;
    153 		}
    154 
    155 #.*$		/* drop comment until end of line */
    156 [ \t]		/* eat whitespace */
    157 
    158 :		return COLON;
    159