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