]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/ipfilter/ipft_pc.c
This commit was generated by cvs2svn to compensate for changes in r54816,
[FreeBSD/FreeBSD.git] / contrib / ipfilter / ipft_pc.c
1 /*
2  * Copyright (C) 1993-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 <string.h>
10 #if !defined(__SVR4) && !defined(__GNUC__)
11 #include <strings.h>
12 #endif
13 #include <sys/types.h>
14 #include <sys/time.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <stddef.h>
18 #include <sys/socket.h>
19 #include <sys/ioctl.h>
20 #include <sys/param.h>
21 #include <netinet/in.h>
22 #include <netinet/in_systm.h>
23 #ifndef linux
24 #include <netinet/ip_var.h>
25 #endif
26 #include <netinet/ip.h>
27 #include <netinet/tcp.h>
28 #include <net/if.h>
29 #include "ip_compat.h"
30 #include <netinet/tcpip.h>
31 #include "ipf.h"
32 #include "pcap.h"
33 #include "ipt.h"
34
35 #if !defined(lint)
36 static const char rcsid[] = "@(#)$Id: ipft_pc.c,v 2.1 1999/08/04 17:30:03 darrenr Exp $";
37 #endif
38
39 struct  llc     {
40         int     lc_sz;  /* LLC header length */
41         int     lc_to;  /* LLC Type offset */
42         int     lc_tl;  /* LLC Type length */
43 };
44
45 /*
46  * While many of these maybe the same, some do have different header formats
47  * which make this useful.
48  */
49 #define DLT_MAX 10
50
51 static  struct  llc     llcs[DLT_MAX+1] = {
52         { 0, 0, 0 },    /* DLT_NULL */
53         { 14, 12, 2 },  /* DLT_E10MB */
54         { 0, 0, 0 },    /* DLT_EN3MB */
55         { 0, 0, 0 },    /* DLT_AX25 */
56         { 0, 0, 0 },    /* DLT_PRONET */
57         { 0, 0, 0 },    /* DLT_CHAOS */
58         { 0, 0, 0 },    /* DLT_IEEE802 */
59         { 0, 0, 0 },    /* DLT_ARCNET */
60         { 0, 0, 0 },    /* DLT_SLIP */
61         { 0, 0, 0 },    /* DLT_PPP */
62         { 0, 0, 0 }     /* DLT_FDDI */
63 };
64
65 static  int     pcap_open __P((char *));
66 static  int     pcap_close __P((void));
67 static  int     pcap_readip __P((char *, int, char **, int *));
68 static  void    swap_hdr __P((pcaphdr_t *));
69 static  int     pcap_read_rec __P((struct pcap_pkthdr *));
70
71 static  int     pfd = -1, s_type = -1, swapped = 0;
72
73 struct  ipread  pcap = { pcap_open, pcap_close, pcap_readip };
74
75 #define SWAPLONG(y)     \
76         ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
77 #define SWAPSHORT(y)    \
78         ( (((y)&0xff)<<8) | (((y)&0xff00)>>8) )
79
80 static  void    swap_hdr(p)
81 pcaphdr_t       *p;
82 {
83         p->pc_v_maj = SWAPSHORT(p->pc_v_maj);
84         p->pc_v_min = SWAPSHORT(p->pc_v_min);
85         p->pc_zone = SWAPLONG(p->pc_zone);
86         p->pc_sigfigs = SWAPLONG(p->pc_sigfigs);
87         p->pc_slen = SWAPLONG(p->pc_slen);
88         p->pc_type = SWAPLONG(p->pc_type);
89 }
90
91 static  int     pcap_open(fname)
92 char    *fname;
93 {
94         pcaphdr_t ph;
95         int     fd;
96
97         if (pfd != -1)
98                 return pfd;
99
100         if (!strcmp(fname, "-"))
101                 fd = 0;
102         else if ((fd = open(fname, O_RDONLY)) == -1)
103                 return -1;
104
105         if (read(fd, (char *)&ph, sizeof(ph)) != sizeof(ph))
106                 return -2;
107
108         if (ph.pc_id != TCPDUMP_MAGIC) {
109                 if (SWAPLONG(ph.pc_id) != TCPDUMP_MAGIC) {
110                         (void) close(fd);
111                         return -2;
112                 }
113                 swapped = 1;
114                 swap_hdr(&ph);
115         }
116
117         if (ph.pc_v_maj != PCAP_VERSION_MAJ || ph.pc_type > DLT_MAX) {
118                 (void) close(fd);
119                 return -2;
120         }
121
122         pfd = fd;
123         s_type = ph.pc_type;
124         printf("opened pcap file %s:\n", fname);
125         printf("\tid: %08x version: %d.%d type: %d snap %d\n",
126                 ph.pc_id, ph.pc_v_maj, ph.pc_v_min, ph.pc_type, ph.pc_slen);
127
128         return fd;
129 }
130
131
132 static  int     pcap_close()
133 {
134         return close(pfd);
135 }
136
137
138 /*
139  * read in the header (and validate) which should be the first record
140  * in a pcap file.
141  */
142 static  int     pcap_read_rec(rec)
143 struct  pcap_pkthdr *rec;
144 {
145         int     n, p;
146
147         if (read(pfd, (char *)rec, sizeof(*rec)) != sizeof(*rec))
148                 return -2;
149
150         if (swapped) {
151                 rec->ph_clen = SWAPLONG(rec->ph_clen);
152                 rec->ph_len = SWAPLONG(rec->ph_len);
153                 rec->ph_ts.tv_sec = SWAPLONG(rec->ph_ts.tv_sec);
154                 rec->ph_ts.tv_usec = SWAPLONG(rec->ph_ts.tv_usec);
155         }
156         p = rec->ph_clen;
157         n = MIN(p, rec->ph_len);
158         if (!n || n < 0)
159                 return -3;
160
161         return p;
162 }
163
164
165 #ifdef  notyet
166 /*
167  * read an entire pcap packet record.  only the data part is copied into
168  * the available buffer, with the number of bytes copied returned.
169  */
170 static  int     pcap_read(buf, cnt)
171 char    *buf;
172 int     cnt;
173 {
174         struct  pcap_pkthdr rec;
175         static  char    *bufp = NULL;
176         int     i, n;
177
178         if ((i = pcap_read_rec(&rec)) <= 0)
179                 return i;
180
181         if (!bufp)
182                 bufp = malloc(i);
183         else
184                 bufp = realloc(bufp, i);
185
186         if (read(pfd, bufp, i) != i)
187                 return -2;
188
189         n = MIN(i, cnt);
190         bcopy(bufp, buf, n);
191         return n;
192 }
193 #endif
194
195
196 /*
197  * return only an IP packet read into buf
198  */
199 static  int     pcap_readip(buf, cnt, ifn, dir)
200 char    *buf, **ifn;
201 int     cnt, *dir;
202 {
203         static  char    *bufp = NULL;
204         struct  pcap_pkthdr rec;
205         struct  llc     *l;
206         char    *s, ty[4];
207         int     i, n;
208
209         do {
210                 if ((i = pcap_read_rec(&rec)) <= 0)
211                         return i;
212
213                 if (!bufp)
214                         bufp = malloc(i);
215                 else
216                         bufp = realloc(bufp, i);
217                 s = bufp;
218
219                 if (read(pfd, s, i) != i)
220                         return -2;
221
222                 l = &llcs[s_type];
223                 i -= l->lc_sz;
224                 s += l->lc_to;
225                 bcopy(s, ty, l->lc_tl);
226                 s += l->lc_tl;
227         } while (ty[0] != 0x8 && ty[1] != 0);
228         n = MIN(i, cnt);
229         bcopy(s, buf, n);
230         return n;
231 }