Merge from vendor branch BIND:
[dragonfly.git] / contrib / ipfilter / ipft_sn.c
1 /*
2  * Copyright (C) 1993-2001 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  */
6
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"
39
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
43
44 struct  llc     {
45         int     lc_sz;  /* LLC header length */
46         int     lc_to;  /* LLC Type offset */
47         int     lc_tl;  /* LLC Type length */
48 };
49
50 /*
51  * While many of these maybe the same, some do have different header formats
52  * which make this useful.
53  */
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 */
65 };
66
67 static  int     snoop_open __P((char *));
68 static  int     snoop_close __P((void));
69 static  int     snoop_readip __P((char *, int, char **, int *));
70
71 static  int     sfd = -1, s_type = -1;
72 static  int     snoop_read_rec __P((struct snooppkt *));
73
74 struct  ipread  snoop = { snoop_open, snoop_close, snoop_readip };
75
76
77 static  int     snoop_open(fname)
78 char    *fname;
79 {
80         struct  snoophdr sh;
81         int     fd;
82         int s_v;
83
84         if (sfd != -1)
85                 return sfd;
86
87         if (!strcmp(fname, "-"))
88                 fd = 0;
89         else if ((fd = open(fname, O_RDONLY)) == -1)
90                 return -1;
91
92         if (read(fd, (char *)&sh, sizeof(sh)) != sizeof(sh))
93                 return -2;
94
95         s_v = (int)ntohl(sh.s_v);
96         s_type = (int)ntohl(sh.s_type);
97
98         if (s_v != SNOOP_VERSION ||
99             s_type < 0 || s_type > SDL_MAX) {
100                 (void) close(fd);
101                 return -2;
102         }
103
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);
107
108         return fd;
109 }
110
111
112 static  int     snoop_close()
113 {
114         return close(sfd);
115 }
116
117
118 /*
119  * read in the header (and validate) which should be the first record
120  * in a snoop file.
121  */
122 static  int     snoop_read_rec(rec)
123 struct  snooppkt *rec;
124 {
125         int     n, plen, ilen;
126
127         if (read(sfd, (char *)rec, sizeof(*rec)) != sizeof(*rec))
128                 return -2;
129
130         ilen = (int)ntohl(rec->sp_ilen);
131         plen = (int)ntohl(rec->sp_plen);
132         if (ilen > plen || plen < sizeof(*rec))
133                 return -2;
134
135         plen -= sizeof(*rec);
136         n = MIN(plen, ilen);
137         if (!n || n < 0)
138                 return -3;
139
140         return plen;
141 }
142
143
144 #ifdef  notyet
145 /*
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.
148  */
149 static  int     snoop_read(buf, cnt)
150 char    *buf;
151 int     cnt;
152 {
153         struct  snooppkt rec;
154         static  char    *bufp = NULL;
155         int     i, n;
156
157         if ((i = snoop_read_rec(&rec)) <= 0)
158                 return i;
159
160         if (!bufp)
161                 bufp = malloc(i);
162         else
163                 bufp = realloc(bufp, i);
164
165         if (read(sfd, bufp, i) != i)
166                 return -2;
167
168         n = MIN(i, cnt);
169         bcopy(bufp, buf, n);
170         return n;
171 }
172 #endif
173
174
175 /*
176  * return only an IP packet read into buf
177  */
178 static  int     snoop_readip(buf, cnt, ifn, dir)
179 char    *buf, **ifn;
180 int     cnt, *dir;
181 {
182         static  char    *bufp = NULL;
183         struct  snooppkt rec;
184         struct  llc     *l;
185         char    ty[4], *s;
186         int     i, n;
187
188         do {
189                 if ((i = snoop_read_rec(&rec)) <= 0)
190                         return i;
191
192                 if (!bufp)
193                         bufp = malloc(i);
194                 else
195                         bufp = realloc(bufp, i);
196                 s = bufp;
197
198                 if (read(sfd, s, i) != i)
199                         return -2;
200
201                 l = &llcs[s_type];
202                 i -= l->lc_to;
203                 s += l->lc_to;
204                 /*
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.
209                  */
210                 bcopy(s, ty, l->lc_tl);
211         } while (ty[0] != 0x8 && ty[1] != 0);
212
213         i -= l->lc_tl;
214         s += l->lc_tl;
215         n = MIN(i, cnt);
216         bcopy(s, buf, n);
217
218         return n;
219 }