]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/ipfilter/ipft_hx.c
unfinished sblive driver, playback/mixer only for now - not enabled in
[FreeBSD/FreeBSD.git] / contrib / ipfilter / ipft_hx.c
1 /*
2  * Copyright (C) 1995-1998 by Darren Reed.
3  *
4  * Redistribution and use in source and binary forms are permitted
5  * provided that this notice is preserved and due credit is given
6  * to the original author and the contributors.
7  */
8 #include <stdio.h>
9 #include <ctype.h>
10 #include <assert.h>
11 #include <string.h>
12 #include <sys/types.h>
13 #if !defined(__SVR4) && !defined(__svr4__)
14 #include <strings.h>
15 #else
16 #include <sys/byteorder.h>
17 #endif
18 #include <sys/param.h>
19 #include <sys/time.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <stddef.h>
23 #include <sys/socket.h>
24 #include <sys/ioctl.h>
25 #include <netinet/in.h>
26 #include <netinet/in_systm.h>
27 #ifndef linux
28 #include <netinet/ip_var.h>
29 #endif
30 #include <netinet/ip.h>
31 #include <netinet/udp.h>
32 #include <netinet/tcp.h>
33 #include <netinet/ip_icmp.h>
34 #include <net/if.h>
35 #include <netdb.h>
36 #include <arpa/nameser.h>
37 #include <resolv.h>
38 #include "ip_compat.h"
39 #include <netinet/tcpip.h>
40 #include "ipf.h"
41 #include "ipt.h"
42
43 #if !defined(lint)
44 static const char sccsid[] = "@(#)ipft_hx.c     1.1 3/9/96 (C) 1996 Darren Reed";
45 static const char rcsid[] = "@(#)$Id: ipft_hx.c,v 2.1 1999/08/04 17:30:03 darrenr Exp $";
46 #endif
47
48 extern  int     opts;
49
50 static  int     hex_open __P((char *));
51 static  int     hex_close __P((void));
52 static  int     hex_readip __P((char *, int, char **, int *));
53 static  char    *readhex __P((char *, char *));
54
55 struct  ipread  iphex = { hex_open, hex_close, hex_readip };
56 static  FILE    *tfp = NULL;
57 static  int     tfd = -1;
58
59 static  int     hex_open(fname)
60 char    *fname;
61 {
62         if (tfp && tfd != -1) {
63                 rewind(tfp);
64                 return tfd;
65         }
66
67         if (!strcmp(fname, "-")) {
68                 tfd = 0;
69                 tfp = stdin;
70         } else {
71                 tfd = open(fname, O_RDONLY);
72                 if (tfd != -1)
73                         tfp = fdopen(tfd, "r");
74         }
75         return tfd;
76 }
77
78
79 static  int     hex_close()
80 {
81         int     cfd = tfd;
82
83         tfd = -1;
84         return close(cfd);
85 }
86
87
88 static  int     hex_readip(buf, cnt, ifn, dir)
89 char    *buf, **ifn;
90 int     cnt, *dir;
91 {
92         register char *s, *t, *u;
93         char    line[513];
94         ip_t    *ip;
95
96         ip = (ip_t *)buf;
97         while (fgets(line, sizeof(line)-1, tfp)) {
98                 if ((s = index(line, '\n'))) {
99                         if (s == line)
100                                 return (char *)ip - buf;
101                         *s = '\0';
102                 }
103                 if ((s = index(line, '#')))
104                         *s = '\0';
105                 if (!*line)
106                         continue;
107                 if (!(opts & OPT_BRIEF)) {
108                         printf("input: %s\n", line);
109                         fflush(stdout);
110                 }
111
112                 /*
113                  * interpret start of line as possibly "[ifname]" or
114                  * "[in/out,ifname]".
115                  */
116                 if (ifn)
117                         *ifn = NULL;
118                 if (dir)
119                         *dir = 0;
120                 if ((*buf == '[') && (s = index(line, ']'))) {
121                         t = buf + 1;
122                         if (t - s > 0) {
123                                 if ((u = index(t, ',')) && (u < s)) {
124                                         u++;
125                                         if (ifn)
126                                                 *ifn = u;
127                                         if (dir) {
128                                                 if (*t == 'i')
129                                                         *dir = 0;
130                                                 else if (*t == 'o')
131                                                         *dir = 1;
132                                         }
133                                 } else if (ifn)
134                                         *ifn = t;
135                                 *s++ = '\0';
136                         }
137                 } else
138                         s = line;
139                 ip = (ip_t *)readhex(s, (char *)ip);
140         }
141         return -1;
142 }
143
144
145 static  char    *readhex(src, dst)
146 register char   *src, *dst;
147 {
148         int     state = 0;
149         char    c;
150
151         while ((c = *src++)) {
152                 if (isspace(c)) {
153                         if (state) {
154                                 dst++;
155                                 state = 0;
156                         }
157                         continue;
158                 } else if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') ||
159                            (c >= 'A' && c <= 'F')) {
160                         c = isdigit(c) ? (c - '0') : (toupper(c) - 55);
161                         if (state == 0) {
162                                 *dst = (c << 4);
163                                 state++;
164                         } else {
165                                 *dst++ |= c;
166                                 state = 0;
167                         }
168                 } else
169                         break;
170         }
171         return dst;
172 }