Move /var/svc/log to /var/log/svc
[unleashed/lotheac.git] / usr / src / cmd / ipf / lib / common / ipft_pc.c
blob1fba10f2bb546b29c7846391a5ac1cdab7ff56e0
1 /*
2 * Copyright (C) 1993-2001 by Darren Reed.
4 * See the IPFILTER.LICENCE file for details on licencing.
6 * $Id: ipft_pc.c,v 1.10 2004/02/07 18:17:40 darrenr Exp $
7 */
8 #include "ipf.h"
9 #include "pcap-ipf.h"
10 #include "bpf-ipf.h"
11 #include "ipt.h"
13 static const char rcsid[] = "@(#)$Id: ipft_pc.c,v 1.10 2004/02/07 18:17:40 darrenr Exp $";
15 struct llc {
16 int lc_type;
17 int lc_sz; /* LLC header length */
18 int lc_to; /* LLC Type offset */
19 int lc_tl; /* LLC Type length */
23 * While many of these maybe the same, some do have different header formats
24 * which make this useful.
27 static struct llc llcs[] = {
28 { DLT_NULL, 0, 0, 0 },
29 { DLT_EN10MB, 14, 12, 2 },
30 { DLT_EN3MB, 0, 0, 0 },
31 { DLT_AX25, 0, 0, 0 },
32 { DLT_PRONET, 0, 0, 0 },
33 { DLT_CHAOS, 0, 0, 0 },
34 { DLT_IEEE802, 0, 0, 0 },
35 { DLT_ARCNET, 0, 0, 0 },
36 { DLT_SLIP, 0, 0, 0 },
37 { DLT_PPP, 0, 0, 0 },
38 { DLT_FDDI, 0, 0, 0 },
39 #ifdef DLT_ATMRFC1483
40 { DLT_ATMRFC1483, 0, 0, 0 },
41 #endif
42 { DLT_RAW, 0, 0, 0 },
43 #ifdef DLT_ENC
44 { DLT_ENC, 0, 0, 0 },
45 #endif
46 #ifdef DLT_SLIP_BSDOS
47 { DLT_SLIP_BSDOS, 0, 0, 0 },
48 #endif
49 #ifdef DLT_PPP_BSDOS
50 { DLT_PPP_BSDOS, 0, 0, 0 },
51 #endif
52 #ifdef DLT_HIPPI
53 { DLT_HIPPI, 0, 0, 0 },
54 #endif
55 #ifdef DLT_HDLC
56 { DLT_HDLC, 0, 0, 0 },
57 #endif
58 #ifdef DLT_PPP_SERIAL
59 { DLT_PPP_SERIAL, 4, 4, 0 },
60 #endif
61 #ifdef DLT_PPP_ETHER
62 { DLT_PPP_ETHER, 8, 8, 0 },
63 #endif
64 #ifdef DLT_ECONET
65 { DLT_ECONET, 0, 0, 0 },
66 #endif
67 { -1, -1, -1, -1 }
70 static int pcap_open __P((char *));
71 static int pcap_close __P((void));
72 static int pcap_readip __P((char *, int, char **, int *));
73 static void swap_hdr __P((pcaphdr_t *));
74 static int pcap_read_rec __P((struct pcap_pkthdr *));
76 static int pfd = -1, swapped = 0;
77 static struct llc *llcp = NULL;
79 struct ipread pcap = { pcap_open, pcap_close, pcap_readip, 0 };
81 #define SWAPLONG(y) \
82 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
83 #define SWAPSHORT(y) \
84 ( (((y)&0xff)<<8) | (((y)&0xff00)>>8) )
86 static void swap_hdr(p)
87 pcaphdr_t *p;
89 p->pc_v_maj = SWAPSHORT(p->pc_v_maj);
90 p->pc_v_min = SWAPSHORT(p->pc_v_min);
91 p->pc_zone = SWAPLONG(p->pc_zone);
92 p->pc_sigfigs = SWAPLONG(p->pc_sigfigs);
93 p->pc_slen = SWAPLONG(p->pc_slen);
94 p->pc_type = SWAPLONG(p->pc_type);
97 static int pcap_open(fname)
98 char *fname;
100 pcaphdr_t ph;
101 int fd, i;
103 if (pfd != -1)
104 return pfd;
106 if (!strcmp(fname, "-"))
107 fd = 0;
108 else if ((fd = open(fname, O_RDONLY)) == -1)
109 return -1;
111 if (read(fd, (char *)&ph, sizeof(ph)) != sizeof(ph))
112 return -2;
114 if (ph.pc_id != TCPDUMP_MAGIC) {
115 if (SWAPLONG(ph.pc_id) != TCPDUMP_MAGIC) {
116 (void) close(fd);
117 return -2;
119 swapped = 1;
120 swap_hdr(&ph);
123 if (ph.pc_v_maj != PCAP_VERSION_MAJ) {
124 (void) close(fd);
125 return -2;
128 for (i = 0; llcs[i].lc_type != -1; i++)
129 if (llcs[i].lc_type == ph.pc_type) {
130 llcp = llcs + i;
131 break;
134 if (llcp == NULL) {
135 (void) close(fd);
136 return -2;
139 pfd = fd;
140 printf("opened pcap file %s:\n", fname);
141 printf("\tid: %08x version: %d.%d type: %d snap %d\n",
142 ph.pc_id, ph.pc_v_maj, ph.pc_v_min, ph.pc_type, ph.pc_slen);
144 return fd;
148 static int pcap_close()
150 return close(pfd);
155 * read in the header (and validate) which should be the first record
156 * in a pcap file.
158 static int pcap_read_rec(rec)
159 struct pcap_pkthdr *rec;
161 int n, p;
163 if (read(pfd, (char *)rec, sizeof(*rec)) != sizeof(*rec))
164 return -2;
166 if (swapped) {
167 rec->ph_clen = SWAPLONG(rec->ph_clen);
168 rec->ph_len = SWAPLONG(rec->ph_len);
169 rec->ph_ts.tv_sec = SWAPLONG(rec->ph_ts.tv_sec);
170 rec->ph_ts.tv_usec = SWAPLONG(rec->ph_ts.tv_usec);
172 p = rec->ph_clen;
173 n = MIN(p, rec->ph_len);
174 if (!n || n < 0)
175 return -3;
177 return p;
181 #ifdef notyet
183 * read an entire pcap packet record. only the data part is copied into
184 * the available buffer, with the number of bytes copied returned.
186 static int pcap_read(buf, cnt)
187 char *buf;
188 int cnt;
190 struct pcap_pkthdr rec;
191 static char *bufp = NULL;
192 int i, n;
194 if ((i = pcap_read_rec(&rec)) <= 0)
195 return i;
197 if (!bufp)
198 bufp = malloc(i);
199 else
200 bufp = realloc(bufp, i);
202 if (read(pfd, bufp, i) != i)
203 return -2;
205 n = MIN(i, cnt);
206 bcopy(bufp, buf, n);
207 return n;
209 #endif
213 * return only an IP packet read into buf
215 static int pcap_readip(buf, cnt, ifn, dir)
216 char *buf, **ifn;
217 int cnt, *dir;
219 static char *bufp = NULL;
220 struct pcap_pkthdr rec;
221 struct llc *l;
222 char *s, ty[4];
223 int i, n;
225 l = llcp;
227 /* do { */
228 if ((i = pcap_read_rec(&rec)) <= 0)
229 return i;
231 if (!bufp)
232 bufp = malloc(i);
233 else
234 bufp = realloc(bufp, i);
235 s = bufp;
237 if (read(pfd, s, i) != i)
238 return -2;
240 i -= l->lc_sz;
241 s += l->lc_to;
242 bcopy(s, ty, l->lc_tl);
243 s += l->lc_tl;
244 /* } while (ty[0] != 0x8 && ty[1] != 0); */
245 n = MIN(i, cnt);
246 bcopy(s, buf, n);
247 return n;