]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/ipfilter/lib/ipft_hx.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / ipfilter / lib / ipft_hx.c
1 /*      $FreeBSD$       */
2
3 /*
4  * Copyright (C) 2000-2005 by Darren Reed.
5  *
6  * See the IPFILTER.LICENCE file for details on licencing.
7  */
8 #if !defined(lint)
9 static const char sccsid[] = "@(#)ipft_hx.c     1.1 3/9/96 (C) 1996 Darren Reed";
10 static const char rcsid[] = "@(#)$Id: ipft_hx.c,v 1.11.4.4 2006/06/16 17:21:03 darrenr Exp $";
11 #endif
12
13 #include <ctype.h>
14
15 #include "ipf.h"
16 #include "ipt.h"
17
18
19 extern  int     opts;
20
21 static  int     hex_open __P((char *));
22 static  int     hex_close __P((void));
23 static  int     hex_readip __P((char *, int, char **, int *));
24 static  char    *readhex __P((char *, char *));
25
26 struct  ipread  iphex = { hex_open, hex_close, hex_readip, 0 };
27 static  FILE    *tfp = NULL;
28 static  int     tfd = -1;
29
30 static  int     hex_open(fname)
31 char    *fname;
32 {
33         if (tfp && tfd != -1) {
34                 rewind(tfp);
35                 return tfd;
36         }
37
38         if (!strcmp(fname, "-")) {
39                 tfd = 0;
40                 tfp = stdin;
41         } else {
42                 tfd = open(fname, O_RDONLY);
43                 if (tfd != -1)
44                         tfp = fdopen(tfd, "r");
45         }
46         return tfd;
47 }
48
49
50 static  int     hex_close()
51 {
52         int     cfd = tfd;
53
54         tfd = -1;
55         return close(cfd);
56 }
57
58
59 static  int     hex_readip(buf, cnt, ifn, dir)
60 char    *buf, **ifn;
61 int     cnt, *dir;
62 {
63         register char *s, *t, *u;
64         char    line[513];
65         ip_t    *ip;
66
67         /*
68          * interpret start of line as possibly "[ifname]" or
69          * "[in/out,ifname]".
70          */
71         if (ifn)
72                 *ifn = NULL;
73         if (dir)
74                 *dir = 0;
75         ip = (ip_t *)buf;
76         while (fgets(line, sizeof(line)-1, tfp)) {
77                 if ((s = strchr(line, '\n'))) {
78                         if (s == line)
79                                 return (char *)ip - buf;
80                         *s = '\0';
81                 }
82                 if ((s = strchr(line, '#')))
83                         *s = '\0';
84                 if (!*line)
85                         continue;
86                 if ((opts & OPT_DEBUG) != 0) {
87                         printf("input: %s", line);
88                 }
89
90                 if ((*line == '[') && (s = strchr(line, ']'))) {
91                         t = line + 1;
92                         if (s - t > 0) {
93                                 *s++ = '\0';
94                                 if ((u = strchr(t, ',')) && (u < s)) {
95                                         u++;
96                                         if (ifn)
97                                                 *ifn = strdup(u);
98                                         if (dir) {
99                                                 if (*t == 'i')
100                                                         *dir = 0;
101                                                 else if (*t == 'o')
102                                                         *dir = 1;
103                                         }
104                                 } else if (ifn)
105                                         *ifn = t;
106                         }
107                 } else
108                         s = line;
109                 t = (char *)ip;
110                 ip = (ip_t *)readhex(s, (char *)ip);
111                 if ((opts & OPT_DEBUG) != 0) {
112                         if (opts & OPT_ASCII) {
113                                 if (t < (char *)ip)
114                                         putchar('\t');
115                                 while (t < (char *)ip) {
116                                         if (ISPRINT(*t) && ISASCII(*t))
117                                                 putchar(*t);
118                                         else
119                                                 putchar('.');
120                                         t++;
121                                 }
122                         }
123                         putchar('\n');
124                         fflush(stdout);
125                 }
126         }
127         if (feof(tfp))
128                 return 0;
129         return -1;
130 }
131
132
133 static  char    *readhex(src, dst)
134 register char   *src, *dst;
135 {
136         int     state = 0;
137         char    c;
138
139         while ((c = *src++)) {
140                 if (ISSPACE(c)) {
141                         if (state) {
142                                 dst++;
143                                 state = 0;
144                         }
145                         continue;
146                 } else if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') ||
147                            (c >= 'A' && c <= 'F')) {
148                         c = ISDIGIT(c) ? (c - '0') : (TOUPPER(c) - 55);
149                         if (state == 0) {
150                                 *dst = (c << 4);
151                                 state++;
152                         } else {
153                                 *dst++ |= c;
154                                 state = 0;
155                         }
156                 } else
157                         break;
158         }
159         return dst;
160 }