8 typedef struct ssfilter * ssfilter_t;
10 #define YYSTYPE ssfilter_t
12 static struct ssfilter * alloc_node(int type, void *pred)
14 struct ssfilter *n = malloc(sizeof(*n));
23 static char **yy_argv;
26 static ssfilter_t *yy_ret;
28 static int yylex(void);
30 static void yyerror(char *s)
32 fprintf(stderr, "ss: bison bellows (while parsing filter): \"%s!\"", s);
37 %token HOSTCOND DCOND SCOND DPORT SPORT LEQ GEQ NEQ AUTOBOUND
50 null: /* NOTHING */ { $$ = NULL; }
54 $$ = alloc_node(SSF_DCOND, $2);
58 $$ = alloc_node(SSF_SCOND, $2);
62 $$ = alloc_node(SSF_D_GE, $3);
66 $$ = alloc_node(SSF_D_LE, $3);
70 $$ = alloc_node(SSF_NOT, alloc_node(SSF_D_LE, $3));
74 $$ = alloc_node(SSF_NOT, alloc_node(SSF_D_GE, $3));
78 $$ = alloc_node(SSF_DCOND, $3);
82 $$ = alloc_node(SSF_NOT, alloc_node(SSF_DCOND, $3));
87 $$ = alloc_node(SSF_S_GE, $3);
91 $$ = alloc_node(SSF_S_LE, $3);
95 $$ = alloc_node(SSF_NOT, alloc_node(SSF_S_LE, $3));
99 $$ = alloc_node(SSF_NOT, alloc_node(SSF_S_GE, $3));
103 $$ = alloc_node(SSF_SCOND, $3);
107 $$ = alloc_node(SSF_NOT, alloc_node(SSF_SCOND, $3));
112 $$ = alloc_node(SSF_S_AUTO, NULL);
116 $$ = alloc_node(SSF_OR, $1);
121 $$ = alloc_node(SSF_AND, $1);
127 $$ = alloc_node(SSF_AND, $1);
132 $$ = alloc_node(SSF_NOT, $2);
141 static char *get_token_from_line(char **ptr)
143 char *tok, *cp = *ptr;
145 while (*cp == ' ' || *cp == '\t') cp++;
154 while (*cp != 0 && *cp != ' ' && *cp != '\t') {
155 /* Backslash escapes everything. */
158 for (tp = cp; tp != tok; tp--)
175 static char argbuf[1024];
176 static char *tokptr = argbuf;
181 while (*tokptr == 0) {
183 if (argc < yy_argc) {
184 tokptr = yy_argv[argc];
187 while (tokptr == NULL) {
188 if (fgets(argbuf, sizeof(argbuf)-1, yy_fp) == NULL)
190 argbuf[sizeof(argbuf)-1] = 0;
191 if (strlen(argbuf) == sizeof(argbuf) - 1) {
192 fprintf(stderr, "Too long line in filter");
195 if (argbuf[strlen(argbuf)-1] == '\n')
196 argbuf[strlen(argbuf)-1] = 0;
197 if (argbuf[0] == '#' || argbuf[0] == '0')
205 } while ((curtok = get_token_from_line(&tokptr)) == NULL);
207 if (strcmp(curtok, "!") == 0 ||
208 strcmp(curtok, "not") == 0)
210 if (strcmp(curtok, "&") == 0 ||
211 strcmp(curtok, "&&") == 0 ||
212 strcmp(curtok, "and") == 0)
214 if (strcmp(curtok, "|") == 0 ||
215 strcmp(curtok, "||") == 0 ||
216 strcmp(curtok, "or") == 0)
218 if (strcmp(curtok, "(") == 0)
220 if (strcmp(curtok, ")") == 0)
222 if (strcmp(curtok, "dst") == 0)
224 if (strcmp(curtok, "src") == 0)
226 if (strcmp(curtok, "dport") == 0)
228 if (strcmp(curtok, "sport") == 0)
230 if (strcmp(curtok, ">=") == 0 ||
231 strcmp(curtok, "ge") == 0 ||
232 strcmp(curtok, "geq") == 0)
234 if (strcmp(curtok, "<=") == 0 ||
235 strcmp(curtok, "le") == 0 ||
236 strcmp(curtok, "leq") == 0)
238 if (strcmp(curtok, "!=") == 0 ||
239 strcmp(curtok, "ne") == 0 ||
240 strcmp(curtok, "neq") == 0)
242 if (strcmp(curtok, "=") == 0 ||
243 strcmp(curtok, "==") == 0 ||
244 strcmp(curtok, "eq") == 0)
246 if (strcmp(curtok, ">") == 0 ||
247 strcmp(curtok, "gt") == 0)
249 if (strcmp(curtok, "<") == 0 ||
250 strcmp(curtok, "lt") == 0)
252 if (strcmp(curtok, "autobound") == 0)
254 yylval = (void*)parse_hostcond(curtok);
255 if (yylval == NULL) {
256 fprintf(stderr, "Cannot parse dst/src address.\n");
262 int ssfilter_parse(struct ssfilter **f, int argc, char **argv, FILE *fp)
270 fprintf(stderr, " Sorry.\n");