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