de rien
[parefeu.git] / policy_test.y
1 %{
2 #include <sys/cdefs.h>
3
4 #include <sys/types.h>
5 #include <sys/param.h>
6 #include <sys/socket.h>
7
8 #include <netinet/in.h>
9
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <fcntl.h>
14 #include <string.h>
15 #include <netdb.h>
16
17 extern FILE *yyin;
18 extern FILE *yyout;
19 extern int yylineno;
20 extern int yy_flex_debug;
21
22 static size_t errors;
23 enum filter_dir {
24 GTW_IN,
25 GTW_OUT,
26 GTW_INOUT
27 };
28
29 #define GODROP 0
30 enum filter_action {
31 GTW_DROP = 0,
32 GTW_PASS,
33 };
34
35 struct mqtt_rule {
36         char topic[128];
37         char from[128];
38         enum filter_action      action;
39         enum filter_dir         dir;
40         int     log;
41         int     quick;
42 };
43
44 static size_t parsed_lines;
45 static size_t errors;
46 static size_t nrule;
47 %}
48
49 %union {
50         int64_t number;
51         char *string;
52 };
53
54 %token ACTION IN OUT FROM QUICK LOG SOCKET
55 %token TOPIC WORD WORD2
56 %token ANY FROM
57 %token SLASH HYPHEN
58 %type <number> ACTION QUICK LOG get_dir get_log get_quick
59 %type <string> get_from get_topic WORD SOCKET
60
61
62 %%
63
64 ruleset: /* empty */
65            | ruleset mqttrule
66            | ruleset comment
67            | ruleset error { errors++; }
68            ;
69
70 comment:        COMMENT '\n'
71            {
72            };
73 mqttrule        : get_log ACTION get_quick get_dir get_topic get_from
74            {
75            parsed_lines++;
76            struct mqtt_rule r;
77            memset(&r, 0, sizeof(r));
78            r.action = $2;
79            r.dir = $4;
80            if ($5)
81                    strlcpy(r.topic, $5, sizeof r.topic);
82            if ($6)
83                    strlcpy(r.from, $6, sizeof r.from);
84
85                 ++nrule;
86                 if ($1==1) {
87                 printf("\t#%zu: %c %s %c %s <from> %s;\n", nrule, $3?'!':' ', r.action == GTW_DROP ? "Drop" : "pass", 
88                 r.dir==GTW_INOUT ? '=' : r.dir==GTW_IN ? '<' :'>',
89                 r.topic[0]!=0 ? r.topic :"*", r.from[0]!=0 ? r.from : "<all>");
90                 }
91            };
92
93 get_quick       : /*empty*/ {$$=0;}
94                         | QUICK {$$=1;}
95                         ;
96 get_log         : /*empty*/ {$$=0;}
97                         | LOG {$$ = 1;}
98                         ;
99
100 get_topic       :       ANY {$$=NULL;}
101                         |       WORD {$$=$1;}
102                         ;
103
104 get_from        : /*empty*/ {$$=NULL;}
105                         | SOCKET {$$ = $1;}
106                         ;
107
108 get_dir :       /*empty*/ {$$=GTW_INOUT;}
109                 |       IN      {$$=GTW_IN;}
110                 |       OUT     {$$=GTW_OUT;}
111                 ;
112 %%
113
114 void
115 yyerror(msg)
116         char *msg;
117 {
118         fprintf(stderr, "while parsing line %d: error occured \"%s\"\n",
119         yylineno,
120                 msg);
121
122         return;
123 }
124
125 int main( int argc, char **argv )
126 {
127         ++argv, --argc;  /* skip over program name */
128         if ( argc > 0 )
129                 yyin = fopen( argv[0], "r" );
130         else
131                 yyin = stdin;
132
133         int devNull = open("/dev/null", O_WRONLY);
134         int dup2Result = dup2(devNull, STDERR_FILENO);
135
136         yyparse();
137         printf("#%zu. R: %zu\tE: %zu\n", parsed_lines, nrule, errors);
138 }