- Add lwkt_serialize_adaptive_enter(9), it is same as lwkt_serialize_enter(9)
[dfdiff.git] / contrib / ipfilter / ipft_hx.c
blobb26bd93e02aad8b6be38d1e4d6b037625f74909a
1 /*
2 * Copyright (C) 1995-2001 by Darren Reed.
4 * See the IPFILTER.LICENCE file for details on licencing.
5 */
6 #if defined(__sgi) && (IRIX > 602)
7 # include <sys/ptimers.h>
8 #endif
9 #include <stdio.h>
10 #include <ctype.h>
11 #include <assert.h>
12 #include <string.h>
13 #include <sys/types.h>
14 #if !defined(__SVR4) && !defined(__svr4__)
15 #include <strings.h>
16 #else
17 #include <sys/byteorder.h>
18 #endif
19 #include <sys/param.h>
20 #include <sys/time.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <stddef.h>
24 #include <sys/socket.h>
25 #include <sys/ioctl.h>
26 #include <netinet/in.h>
27 #include <netinet/in_systm.h>
28 #ifndef linux
29 #include <netinet/ip_var.h>
30 #endif
31 #include <netinet/ip.h>
32 #include <netinet/udp.h>
33 #include <netinet/tcp.h>
34 #include <netinet/ip_icmp.h>
35 #include <net/if.h>
36 #include <netdb.h>
37 #include <arpa/nameser.h>
38 #include <resolv.h>
39 #include "ip_compat.h"
40 #include <netinet/tcpip.h>
41 #include "ipf.h"
42 #include "ipt.h"
44 #if !defined(lint)
45 static const char sccsid[] = "@(#)ipft_hx.c 1.1 3/9/96 (C) 1996 Darren Reed";
46 static const char rcsid[] = "@(#)$Id: ipft_hx.c,v 2.2.2.6 2002/12/06 11:40:25 darrenr Exp $";
47 #endif
49 extern int opts;
51 static int hex_open __P((char *));
52 static int hex_close __P((void));
53 static int hex_readip __P((char *, int, char **, int *));
54 static char *readhex __P((char *, char *));
56 struct ipread iphex = { hex_open, hex_close, hex_readip };
57 static FILE *tfp = NULL;
58 static int tfd = -1;
60 static int hex_open(fname)
61 char *fname;
63 if (tfp && tfd != -1) {
64 rewind(tfp);
65 return tfd;
68 if (!strcmp(fname, "-")) {
69 tfd = 0;
70 tfp = stdin;
71 } else {
72 tfd = open(fname, O_RDONLY);
73 if (tfd != -1)
74 tfp = fdopen(tfd, "r");
76 return tfd;
80 static int hex_close()
82 int cfd = tfd;
84 tfd = -1;
85 return close(cfd);
89 static int hex_readip(buf, cnt, ifn, dir)
90 char *buf, **ifn;
91 int cnt, *dir;
93 register char *s, *t, *u;
94 char line[513];
95 ip_t *ip;
98 * interpret start of line as possibly "[ifname]" or
99 * "[in/out,ifname]".
101 if (ifn)
102 *ifn = NULL;
103 if (dir)
104 *dir = 0;
105 ip = (ip_t *)buf;
106 while (fgets(line, sizeof(line)-1, tfp)) {
107 if ((s = index(line, '\n'))) {
108 if (s == line)
109 return (char *)ip - buf;
110 *s = '\0';
112 if ((s = index(line, '#')))
113 *s = '\0';
114 if (!*line)
115 continue;
116 if (!(opts & OPT_BRIEF)) {
117 printf("input: %s\n", line);
118 fflush(stdout);
121 if ((*line == '[') && (s = index(line, ']'))) {
122 t = line + 1;
123 if (s - t > 0) {
124 *s++ = '\0';
125 if ((u = index(t, ',')) && (u < s)) {
126 u++;
127 if (ifn)
128 *ifn = strdup(u);
129 if (dir) {
130 if (*t == 'i')
131 *dir = 0;
132 else if (*t == 'o')
133 *dir = 1;
135 } else if (ifn)
136 *ifn = t;
138 } else
139 s = line;
140 ip = (ip_t *)readhex(s, (char *)ip);
142 return -1;
146 static char *readhex(src, dst)
147 register char *src, *dst;
149 int state = 0;
150 char c;
152 while ((c = *src++)) {
153 if (isspace(c)) {
154 if (state) {
155 dst++;
156 state = 0;
158 continue;
159 } else if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') ||
160 (c >= 'A' && c <= 'F')) {
161 c = isdigit(c) ? (c - '0') : (toupper(c) - 55);
162 if (state == 0) {
163 *dst = (c << 4);
164 state++;
165 } else {
166 *dst++ |= c;
167 state = 0;
169 } else
170 break;
172 return dst;