4 * Copyright (C) 2012 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];
50 wordtab_t *yywordtab = NULL;
52 wordtab_t *yysavewords[30];
55 static wordtab_t *yyfindkey __P((char *));
56 static int yygetc __P((int));
57 static void yyunputc __P((int));
58 static int yyswallow __P((int));
59 static char *yytexttostr __P((int, int));
60 static void yystrtotext __P((char *));
61 static char *yytexttochar __P((void));
63 static int yygetc(docont)
75 if (yypos == YYBUFSIZ)
78 if (pos >= string_start && pos <= string_end) {
79 c = string_val[pos - string_start];
83 if (docont && (c == '\\')) {
101 static void yyunputc(c)
110 static int yyswallow(last)
115 while (((c = yygetc(0)) > '\0') && (c != last))
126 static char *yytexttochar()
130 for (i = 0; i < yypos; i++)
131 yychars[i] = (char)(yytext[i] & 0xff);
137 static void yystrtotext(str)
147 for (s = str; *s != '\0' && len > 0; s++, len--)
148 yytext[yylast++] = *s;
149 yytext[yylast] = '\0';
153 static char *yytexttostr(offset, max)
159 if ((yytext[offset] == '\'' || yytext[offset] == '"') &&
160 (yytext[offset] == yytext[offset + max - 1])) {
167 str = malloc(max + 1);
169 for (i = offset; i < max; i++)
170 str[i - offset] = (char)(yytext[i] & 0xff);
171 str[i - offset] = '\0';
179 static int prior = 0;
180 static int priornum = 0;
181 int c, n, isbuilding, rval, lnext, nokey = 0;
197 printf("yygetc = (%x) %c [%*.*s]\n",
198 c, c, yypos, yypos, yytexttochar());
208 if (isbuilding == 1) {
212 if (yylast > yypos) {
213 bcopy(yytext + yypos, yytext,
214 sizeof(yytext[0]) * (yylast - yypos + 1));
217 if (yyexpectaddr == 2)
227 if (yylast == yypos) {
241 if ((isbuilding == 0) && !ISALNUM(c)) {
251 if (isbuilding == 1) {
260 if (isbuilding == 1) {
266 if (yyswallow('}') == -1) {
278 } while (ISALPHA(n) || ISDIGIT(n) || n == '_');
282 name = yytexttostr(1, yypos); /* skip $ */
285 string_val = get_variable(name, NULL, yylineNum);
287 if (string_val != NULL) {
288 name = yytexttostr(yypos, yylast);
292 yystrtotext(string_val);
305 if (isbuilding == 1) {
310 if (n == EOF || n == TOOLONG) {
332 fprintf(stderr, "reset at EOF\n");
337 if (strchr("=,/;{}()@", c) != NULL) {
338 if (isbuilding == 1) {
344 } else if (c == '.') {
345 if (isbuilding == 0) {
349 if (yybreakondot != 0) {
377 if (isbuilding == 1) {
393 if (isbuilding == 1) {
413 if (isbuilding == 1) {
432 * Now for the reason this is here...IPv6 address parsing.
433 * The longest string we can expect is of this form:
434 * 0000:0000:0000:0000:0000:0000:000.000.000.000
436 * 0000:0000:0000:0000:0000:0000:0000:0000
439 if (yyexpectaddr != 0 && isbuilding == 0 &&
440 (ishex(c) || isdigit(c) || c == ':')) {
441 char ipv6buf[45 + 1], *s, oc;
449 if (prior == YY_NUMBER && c == ':') {
450 sprintf(s, "%d", priornum);
455 * Perhaps we should implement stricter controls on what we
456 * swallow up here, but surely it would just be duplicating
457 * the code in inet_pton() anyway.
462 } while ((ishex(c) || c == ':' || c == '.') &&
467 if (inet_pton(AF_INET6, ipv6buf, &yylval.ip6) == 1) {
477 if ((c == ':') && (rval != YY_IPV6) && (triedv6 == 0)) {
479 yystr = yytexttostr(0, yypos - 1);
483 for (s = yystr; *s && ishex(*s); s++)
496 if (isbuilding == 1) {
504 if (isbuilding == 0 && c == '0') {
518 * No negative numbers with leading - sign..
520 if (isbuilding == 0 && ISDIGIT(c)) {
523 } while (ISDIGIT(n));
533 yystr = yytexttostr(0, yypos);
536 printf("isbuilding %d yyvarnext %d nokey %d fixed %d addr %d\n",
537 isbuilding, yyvarnext, nokey, yydictfixed, yyexpectaddr);
538 if (isbuilding == 1) {
544 if ((yyvarnext == 0) && (nokey == 0)) {
545 w = yyfindkey(yystr);
546 if (w == NULL && yywordtab != NULL && !yydictfixed) {
548 w = yyfindkey(yystr);
558 if (rval == YY_STR) {
559 if (yysavedepth > 0 && !yydictfixed)
561 if (yyexpectaddr != 0)
568 printf("lexed(%s) %d,%d,%d [%d,%d,%d] => %d @%d\n",
569 yystr, isbuilding, yyexpectaddr, yysavedepth,
570 string_start, string_end, pos, rval, yysavedepth);
575 sscanf(yystr, "%u", &yylval.num);
579 sscanf(yystr, "0x%x", (u_int *)&yylval.num);
583 yylval.str = strdup(yystr);
591 bcopy(yytext + yypos, yytext,
592 sizeof(yytext[0]) * (yylast - yypos + 1));
597 if (rval == YY_NUMBER)
598 priornum = yylval.num;
604 static wordtab_t *yyfindkey(key)
609 if (yywordtab == NULL)
612 for (w = yywordtab; w->w_word != 0; w++)
613 if (strcasecmp(key, w->w_word) == 0)
619 char *yykeytostr(num)
624 if (yywordtab == NULL)
627 for (w = yywordtab; w->w_word; w++)
628 if (w->w_value == num)
634 wordtab_t *yysettab(words)
648 char *txt, letter[2];
651 if (yytokentype < 256) {
652 letter[0] = yytokentype;
655 } else if (yytokentype == YY_STR || yytokentype == YY_HEX ||
656 yytokentype == YY_NUMBER) {
658 txt = yytexttostr(yypos, YYBUFSIZ);
663 txt = yykeytostr(yytokentype);
665 fprintf(stderr, "%s error at \"%s\", line %d\n", msg, txt, yylineNum);
672 void yysetfixeddict(newdict)
676 printf("yysetfixeddict(%lx)\n", (u_long)newdict);
678 if (yysavedepth == sizeof(yysavewords)/sizeof(yysavewords[0])) {
679 fprintf(stderr, "%d: at maximum dictionary depth\n",
684 yysavewords[yysavedepth++] = yysettab(newdict);
686 printf("yysavedepth++ => %d\n", yysavedepth);
691 void yysetdict(newdict)
695 printf("yysetdict(%lx)\n", (u_long)newdict);
697 if (yysavedepth == sizeof(yysavewords)/sizeof(yysavewords[0])) {
698 fprintf(stderr, "%d: at maximum dictionary depth\n",
703 yysavewords[yysavedepth++] = yysettab(newdict);
705 printf("yysavedepth++ => %d\n", yysavedepth);
711 printf("yyresetdict(%d)\n", yysavedepth);
712 if (yysavedepth > 0) {
713 yysettab(yysavewords[--yysavedepth]);
715 printf("yysavedepth-- => %d\n", yysavedepth);
731 while ((n = yylex()) != 0)
732 printf("%d.n = %d [%s] %d %d\n",
733 yylineNum, n, yystr, yypos, yylast);