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