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