Sync usage with man page.
[netbsd-mini2440.git] / dist / ipf / lib / ipft_hx.c
blobc4929e38e7695e91dbfb62996b85b5c213c20477
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2000-2005 by Darren Reed.
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
13 #include <ctype.h>
15 #include "ipf.h"
16 #include "ipt.h"
19 extern int opts;
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 *));
26 struct ipread iphex = { hex_open, hex_close, hex_readip, 0 };
27 static FILE *tfp = NULL;
28 static int tfd = -1;
30 static int hex_open(fname)
31 char *fname;
33 if (tfp && tfd != -1) {
34 rewind(tfp);
35 return tfd;
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");
46 return tfd;
50 static int hex_close()
52 int cfd = tfd;
54 tfd = -1;
55 return close(cfd);
59 static int hex_readip(buf, cnt, ifn, dir)
60 char *buf, **ifn;
61 int cnt, *dir;
63 register char *s, *t, *u;
64 char line[513];
65 ip_t *ip;
68 * interpret start of line as possibly "[ifname]" or
69 * "[in/out,ifname]".
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';
82 if ((s = strchr(line, '#')))
83 *s = '\0';
84 if (!*line)
85 continue;
86 if ((opts & OPT_DEBUG) != 0) {
87 printf("input: %s", line);
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;
104 } else if (ifn)
105 *ifn = t;
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++;
123 putchar('\n');
124 fflush(stdout);
127 if (feof(tfp))
128 return 0;
129 return -1;
133 static char *readhex(src, dst)
134 register char *src, *dst;
136 int state = 0;
137 char c;
139 while ((c = *src++)) {
140 if (ISSPACE(c)) {
141 if (state) {
142 dst++;
143 state = 0;
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;
156 } else
157 break;
159 return dst;