2 * Copyright (C) 2002-2008 by Darren Reed.
4 * See the IPFILTER.LICENCE file for details on licencing.
6 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
7 * Use is subject to license terms.
13 # include "netinet/ip_scan.h"
15 #include <sys/ioctl.h>
31 #define ishex(c) (ISDIGIT(c) || ((c) >= 'a' && (c) <= 'f') || \
32 ((c) >= 'A' && (c) <= 'F'))
35 extern int string_start
;
36 extern int string_end
;
37 extern char *string_val
;
42 int yytext
[YYBUFSIZ
+1];
43 char yychars
[YYBUFSIZ
+1];
51 wordtab_t
*yywordtab
= NULL
;
53 wordtab_t
*yysavewords
[30];
56 static wordtab_t
*yyfindkey
__P((char *));
57 static int yygetc
__P((int));
58 static void yyunputc
__P((int));
59 static int yyswallow
__P((int));
60 static char *yytexttostr
__P((int, int));
61 static void yystrtotext
__P((char *));
62 static char *yytexttochar
__P((void));
64 static int yygetc(docont
)
76 if (yypos
== YYBUFSIZ
)
79 if (pos
>= string_start
&& pos
<= string_end
) {
80 c
= string_val
[pos
- string_start
];
84 if (docont
&& (c
== '\\')) {
102 static void yyunputc(c
)
111 static int yyswallow(last
)
116 while (((c
= yygetc(0)) > '\0') && (c
!= last
))
127 static char *yytexttochar()
131 for (i
= 0; i
< yypos
; i
++)
132 yychars
[i
] = (char)(yytext
[i
] & 0xff);
138 static void yystrtotext(str
)
148 for (s
= str
; *s
!= '\0' && len
> 0; s
++, len
--)
149 yytext
[yylast
++] = *s
;
150 yytext
[yylast
] = '\0';
154 static char *yytexttostr(offset
, max
)
160 if ((yytext
[offset
] == '\'' || yytext
[offset
] == '"') &&
161 (yytext
[offset
] == yytext
[offset
+ max
- 1])) {
168 str
= malloc(max
+ 1);
170 for (i
= offset
; i
< max
; i
++)
171 str
[i
- offset
] = (char)(yytext
[i
] & 0xff);
172 str
[i
- offset
] = '\0';
180 int c
, n
, isbuilding
, rval
, lnext
, nokey
= 0;
195 printf("yygetc = (%x) %c [%*.*s]\n", c
, c
, yypos
, yypos
,
207 if (isbuilding
== 1) {
211 if (yylast
> yypos
) {
212 bcopy(yytext
+ yypos
, yytext
,
213 sizeof(yytext
[0]) * (yylast
- yypos
+ 1));
224 if (yylast
== yypos
) {
238 if ((isbuilding
== 0) && !ISALNUM(c
)) {
247 if (isbuilding
== 1) {
256 if (isbuilding
== 1) {
262 if (yyswallow('}') == -1) {
274 } while (ISALPHA(n
) || ISDIGIT(n
) || n
== '_');
278 name
= yytexttostr(1, yypos
); /* skip $ */
281 string_val
= get_variable(name
, NULL
, yylineNum
);
283 if (string_val
!= NULL
) {
284 name
= yytexttostr(yypos
, yylast
);
288 yystrtotext(string_val
);
301 if (isbuilding
== 1) {
306 if (n
== EOF
|| n
== TOOLONG
) {
330 if (strchr("=,/;{}()@", c
) != NULL
) {
331 if (isbuilding
== 1) {
337 } else if (c
== '.') {
338 if (isbuilding
== 0) {
342 if (yybreakondot
!= 0) {
365 if (isbuilding
== 1) {
381 if (isbuilding
== 1) {
401 if (isbuilding
== 1) {
420 * Now for the reason this is here...IPv6 address parsing.
421 * The longest string we can expect is of this form:
422 * 0000:0000:0000:0000:0000:0000:000.000.000.000
424 * 0000:0000:0000:0000:0000:0000:0000:0000
427 if (isbuilding
== 0 && (ishex(c
) || c
== ':')) {
428 char ipv6buf
[45 + 1], *s
, oc
;
436 * Perhaps we should implement stricter controls on what we
437 * swallow up here, but surely it would just be duplicating
438 * the code in inet_pton() anyway.
443 } while ((ishex(c
) || c
== ':' || c
== '.') &&
448 if (inet_pton(AF_INET6
, ipv6buf
, &yylval
.ip6
) == 1) {
459 if (isbuilding
== 1) {
467 if (isbuilding
== 0 && c
== '0') {
481 * No negative numbers with leading - sign..
483 if (isbuilding
== 0 && ISDIGIT(c
)) {
486 } while (ISDIGIT(n
));
496 yystr
= yytexttostr(0, yypos
);
499 printf("isbuilding %d yyvarnext %d nokey %d\n",
500 isbuilding
, yyvarnext
, nokey
);
501 if (isbuilding
== 1) {
507 if ((yyvarnext
== 0) && (nokey
== 0)) {
508 w
= yyfindkey(yystr
);
509 if (w
== NULL
&& yywordtab
!= NULL
) {
511 w
= yyfindkey(yystr
);
521 if (rval
== YY_STR
&& yysavedepth
> 0)
527 printf("lexed(%s) [%d,%d,%d] => %d @%d\n", yystr
, string_start
,
528 string_end
, pos
, rval
, yysavedepth
);
533 sscanf(yystr
, "%u", &yylval
.num
);
537 sscanf(yystr
, "0x%x", (u_int
*)&yylval
.num
);
541 yylval
.str
= strdup(yystr
);
549 bcopy(yytext
+ yypos
, yytext
,
550 sizeof(yytext
[0]) * (yylast
- yypos
+ 1));
559 static wordtab_t
*yyfindkey(key
)
564 if (yywordtab
== NULL
)
567 for (w
= yywordtab
; w
->w_word
!= 0; w
++)
568 if (strcasecmp(key
, w
->w_word
) == 0)
574 char *yykeytostr(num
)
579 if (yywordtab
== NULL
)
582 for (w
= yywordtab
; w
->w_word
; w
++)
583 if (w
->w_value
== num
)
589 wordtab_t
*yysettab(words
)
603 char *txt
, letter
[2];
606 if (yytokentype
< 256) {
607 letter
[0] = yytokentype
;
610 } else if (yytokentype
== YY_STR
|| yytokentype
== YY_HEX
||
611 yytokentype
== YY_NUMBER
) {
613 txt
= yytexttostr(yypos
, YYBUFSIZ
);
615 fprintf(stderr
, "sorry, out of memory,"
623 txt
= yykeytostr(yytokentype
);
625 fprintf(stderr
, "sorry, out of memory,"
630 fprintf(stderr
, "%s error at \"%s\", line %d\n", msg
, txt
, yylineNum
);
637 void yysetdict(newdict
)
640 if (yysavedepth
== sizeof(yysavewords
)/sizeof(yysavewords
[0])) {
641 fprintf(stderr
, "%d: at maximum dictionary depth\n",
646 yysavewords
[yysavedepth
++] = yysettab(newdict
);
648 printf("yysavedepth++ => %d\n", yysavedepth
);
654 printf("yyresetdict(%d)\n", yysavedepth
);
655 if (yysavedepth
> 0) {
656 yysettab(yysavewords
[--yysavedepth
]);
658 printf("yysavedepth-- => %d\n", yysavedepth
);
673 while ((n
= yylex()) != 0)
674 printf("%d.n = %d [%s] %d %d\n",
675 yylineNum
, n
, yystr
, yypos
, yylast
);