Remove tm.h and xm.h handling, as it wasn't used. Use nm.h only when needed.
[dragonfly.git] / contrib / ipfilter / ipft_sn.c
blob859bf5ed9df72a4e843be9f15d89232ced972432
1 /*
2 * Copyright (C) 1993-2001 by Darren Reed.
4 * See the IPFILTER.LICENCE file for details on licencing.
5 */
7 /*
8 * Written to comply with the recent RFC 1761 from Sun.
9 */
10 #if defined(__sgi) && (IRIX > 602)
11 # include <sys/ptimers.h>
12 #endif
13 #include <stdio.h>
14 #include <string.h>
15 #if !defined(__SVR4) && !defined(__GNUC__)
16 #include <strings.h>
17 #endif
18 #include <sys/types.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <stddef.h>
22 #include <sys/socket.h>
23 #include <sys/ioctl.h>
24 #include <sys/param.h>
25 #include <sys/time.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/tcp.h>
33 #include <net/if.h>
34 #include "ip_compat.h"
35 #include <netinet/tcpip.h>
36 #include "ipf.h"
37 #include "snoop.h"
38 #include "ipt.h"
40 #if !defined(lint)
41 static const char rcsid[] = "@(#)$Id: ipft_sn.c,v 2.2.2.4 2002/12/06 11:40:26 darrenr Exp $";
42 #endif
44 struct llc {
45 int lc_sz; /* LLC header length */
46 int lc_to; /* LLC Type offset */
47 int lc_tl; /* LLC Type length */
51 * While many of these maybe the same, some do have different header formats
52 * which make this useful.
54 static struct llc llcs[SDL_MAX+1] = {
55 { 0, 0, 0 }, /* SDL_8023 */
56 { 0, 0, 0 }, /* SDL_8024 */
57 { 0, 0, 0 }, /* SDL_8025 */
58 { 0, 0, 0 }, /* SDL_8026 */
59 { 14, 12, 2 }, /* SDL_ETHER */
60 { 0, 0, 0 }, /* SDL_HDLC */
61 { 0, 0, 0 }, /* SDL_CHSYNC */
62 { 0, 0, 0 }, /* SDL_IBMCC */
63 { 0, 0, 0 }, /* SDL_FDDI */
64 { 0, 0, 0 }, /* SDL_OTHER */
67 static int snoop_open __P((char *));
68 static int snoop_close __P((void));
69 static int snoop_readip __P((char *, int, char **, int *));
71 static int sfd = -1, s_type = -1;
72 static int snoop_read_rec __P((struct snooppkt *));
74 struct ipread snoop = { snoop_open, snoop_close, snoop_readip };
77 static int snoop_open(fname)
78 char *fname;
80 struct snoophdr sh;
81 int fd;
82 int s_v;
84 if (sfd != -1)
85 return sfd;
87 if (!strcmp(fname, "-"))
88 fd = 0;
89 else if ((fd = open(fname, O_RDONLY)) == -1)
90 return -1;
92 if (read(fd, (char *)&sh, sizeof(sh)) != sizeof(sh))
93 return -2;
95 s_v = (int)ntohl(sh.s_v);
96 s_type = (int)ntohl(sh.s_type);
98 if (s_v != SNOOP_VERSION ||
99 s_type < 0 || s_type > SDL_MAX) {
100 (void) close(fd);
101 return -2;
104 sfd = fd;
105 printf("opened snoop file %s:\n", fname);
106 printf("\tid: %8.8s version: %d type: %d\n", sh.s_id, s_v, s_type);
108 return fd;
112 static int snoop_close()
114 return close(sfd);
119 * read in the header (and validate) which should be the first record
120 * in a snoop file.
122 static int snoop_read_rec(rec)
123 struct snooppkt *rec;
125 int n, plen, ilen;
127 if (read(sfd, (char *)rec, sizeof(*rec)) != sizeof(*rec))
128 return -2;
130 ilen = (int)ntohl(rec->sp_ilen);
131 plen = (int)ntohl(rec->sp_plen);
132 if (ilen > plen || plen < sizeof(*rec))
133 return -2;
135 plen -= sizeof(*rec);
136 n = MIN(plen, ilen);
137 if (!n || n < 0)
138 return -3;
140 return plen;
144 #ifdef notyet
146 * read an entire snoop packet record. only the data part is copied into
147 * the available buffer, with the number of bytes copied returned.
149 static int snoop_read(buf, cnt)
150 char *buf;
151 int cnt;
153 struct snooppkt rec;
154 static char *bufp = NULL;
155 int i, n;
157 if ((i = snoop_read_rec(&rec)) <= 0)
158 return i;
160 if (!bufp)
161 bufp = malloc(i);
162 else
163 bufp = realloc(bufp, i);
165 if (read(sfd, bufp, i) != i)
166 return -2;
168 n = MIN(i, cnt);
169 bcopy(bufp, buf, n);
170 return n;
172 #endif
176 * return only an IP packet read into buf
178 static int snoop_readip(buf, cnt, ifn, dir)
179 char *buf, **ifn;
180 int cnt, *dir;
182 static char *bufp = NULL;
183 struct snooppkt rec;
184 struct llc *l;
185 char ty[4], *s;
186 int i, n;
188 do {
189 if ((i = snoop_read_rec(&rec)) <= 0)
190 return i;
192 if (!bufp)
193 bufp = malloc(i);
194 else
195 bufp = realloc(bufp, i);
196 s = bufp;
198 if (read(sfd, s, i) != i)
199 return -2;
201 l = &llcs[s_type];
202 i -= l->lc_to;
203 s += l->lc_to;
205 * XXX - bogus assumption here on the part of the time field
206 * that it won't be greater than 4 bytes and the 1st two will
207 * have the values 8 and 0 for IP. Should be a table of
208 * these too somewhere. Really only works for SDL_ETHER.
210 bcopy(s, ty, l->lc_tl);
211 } while (ty[0] != 0x8 && ty[1] != 0);
213 i -= l->lc_tl;
214 s += l->lc_tl;
215 n = MIN(i, cnt);
216 bcopy(s, buf, n);
218 return n;