4 * Copyright (C) 2002-2006 by Darren Reed.
6 * See the IPFILTER.LICENCE file for details on licencing.
11 # include "netinet/ip_scan.h"
13 #include <sys/ioctl.h>
29 #define ishex(c) (ISDIGIT(c) || ((c) >= 'a' && (c) <= 'f') || \
30 ((c) >= 'A' && (c) <= 'F'))
33 extern int string_start;
34 extern int string_end;
35 extern char *string_val;
40 int yytext[YYBUFSIZ+1];
41 char yychars[YYBUFSIZ+1];
49 wordtab_t *yywordtab = NULL;
51 wordtab_t *yysavewords[30];
54 static wordtab_t *yyfindkey __P((char *));
55 static int yygetc __P((int));
56 static void yyunputc __P((int));
57 static int yyswallow __P((int));
58 static char *yytexttostr __P((int, int));
59 static void yystrtotext __P((char *));
60 static char *yytexttochar __P((void));
62 static int yygetc(docont)
74 if (yypos == YYBUFSIZ)
77 if (pos >= string_start && pos <= string_end) {
78 c = string_val[pos - string_start];
82 if (docont && (c == '\\')) {
100 static void yyunputc(c)
109 static int yyswallow(last)
114 while (((c = yygetc(0)) > '\0') && (c != last))
125 static char *yytexttochar()
129 for (i = 0; i < yypos; i++)
130 yychars[i] = (char)(yytext[i] & 0xff);
136 static void yystrtotext(str)
146 for (s = str; *s != '\0' && len > 0; s++, len--)
147 yytext[yylast++] = *s;
148 yytext[yylast] = '\0';
152 static char *yytexttostr(offset, max)
158 if ((yytext[offset] == '\'' || yytext[offset] == '"') &&
159 (yytext[offset] == yytext[offset + max - 1])) {
166 str = malloc(max + 1);
168 for (i = offset; i < max; i++)
169 str[i - offset] = (char)(yytext[i] & 0xff);
170 str[i - offset] = '\0';
178 int c, n, isbuilding, rval, lnext, nokey = 0;
193 printf("yygetc = (%x) %c [%*.*s]\n", c, c, yypos, yypos, yytexttochar());
203 if (isbuilding == 1) {
207 if (yylast > yypos) {
208 bcopy(yytext + yypos, yytext,
209 sizeof(yytext[0]) * (yylast - yypos + 1));
220 if (yylast == yypos) {
234 if ((isbuilding == 0) && !ISALNUM(c)) {
243 if (isbuilding == 1) {
252 if (isbuilding == 1) {
258 if (yyswallow('}') == -1) {
270 } while (ISALPHA(n) || ISDIGIT(n) || n == '_');
274 name = yytexttostr(1, yypos); /* skip $ */
277 string_val = get_variable(name, NULL, yylineNum);
279 if (string_val != NULL) {
280 name = yytexttostr(yypos, yylast);
284 yystrtotext(string_val);
297 if (isbuilding == 1) {
302 if (n == EOF || n == TOOLONG) {
326 if (strchr("=,/;{}()@", c) != NULL) {
327 if (isbuilding == 1) {
333 } else if (c == '.') {
334 if (isbuilding == 0) {
338 if (yybreakondot != 0) {
361 if (isbuilding == 1) {
377 if (isbuilding == 1) {
397 if (isbuilding == 1) {
416 * Now for the reason this is here...IPv6 address parsing.
417 * The longest string we can expect is of this form:
418 * 0000:0000:0000:0000:0000:0000:000.000.000.000
420 * 0000:0000:0000:0000:0000:0000:0000:0000
423 if (yyexpectaddr == 1 && isbuilding == 0 && (ishex(c) || c == ':')) {
424 char ipv6buf[45 + 1], *s, oc;
432 * Perhaps we should implement stricter controls on what we
433 * swallow up here, but surely it would just be duplicating
434 * the code in inet_pton() anyway.
439 } while ((ishex(c) || c == ':' || c == '.') &&
444 if (inet_pton(AF_INET6, ipv6buf, &yylval.ip6) == 1) {
455 if (isbuilding == 1) {
463 if (isbuilding == 0 && c == '0') {
477 * No negative numbers with leading - sign..
479 if (isbuilding == 0 && ISDIGIT(c)) {
482 } while (ISDIGIT(n));
492 yystr = yytexttostr(0, yypos);
495 printf("isbuilding %d yyvarnext %d nokey %d\n",
496 isbuilding, yyvarnext, nokey);
497 if (isbuilding == 1) {
503 if ((yyvarnext == 0) && (nokey == 0)) {
504 w = yyfindkey(yystr);
505 if (w == NULL && yywordtab != NULL) {
507 w = yyfindkey(yystr);
517 if (rval == YY_STR && yysavedepth > 0)
523 printf("lexed(%s) [%d,%d,%d] => %d @%d\n", yystr, string_start,
524 string_end, pos, rval, yysavedepth);
529 sscanf(yystr, "%u", &yylval.num);
533 sscanf(yystr, "0x%x", (u_int *)&yylval.num);
537 yylval.str = strdup(yystr);
545 bcopy(yytext + yypos, yytext,
546 sizeof(yytext[0]) * (yylast - yypos + 1));
555 static wordtab_t *yyfindkey(key)
560 if (yywordtab == NULL)
563 for (w = yywordtab; w->w_word != 0; w++)
564 if (strcasecmp(key, w->w_word) == 0)
570 char *yykeytostr(num)
575 if (yywordtab == NULL)
578 for (w = yywordtab; w->w_word; w++)
579 if (w->w_value == num)
585 wordtab_t *yysettab(words)
599 char *txt, letter[2];
602 if (yytokentype < 256) {
603 letter[0] = yytokentype;
606 } else if (yytokentype == YY_STR || yytokentype == YY_HEX ||
607 yytokentype == YY_NUMBER) {
609 txt = yytexttostr(yypos, YYBUFSIZ);
614 txt = yykeytostr(yytokentype);
616 fprintf(stderr, "%s error at \"%s\", line %d\n", msg, txt, yylineNum);
623 void yysetdict(newdict)
626 if (yysavedepth == sizeof(yysavewords)/sizeof(yysavewords[0])) {
627 fprintf(stderr, "%d: at maximum dictionary depth\n",
632 yysavewords[yysavedepth++] = yysettab(newdict);
634 printf("yysavedepth++ => %d\n", yysavedepth);
640 printf("yyresetdict(%d)\n", yysavedepth);
641 if (yysavedepth > 0) {
642 yysettab(yysavewords[--yysavedepth]);
644 printf("yysavedepth-- => %d\n", yysavedepth);
659 while ((n = yylex()) != 0)
660 printf("%d.n = %d [%s] %d %d\n",
661 yylineNum, n, yystr, yypos, yylast);