]> The Tcpdump Group git mirrors - libpcap/blob - pcap-snit.c
1601a9a93ec1d6365c6ab0a0fa9771d2e01ae58b
[libpcap] / pcap-snit.c
1 /*
2 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
16 * written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * Modifications made to accommodate the new SunOS4.0 NIT facility by
22 * Micky Liu, micky@cunixc.cc.columbia.edu, Columbia University in May, 1989.
23 * This module now handles the STREAMS based NIT.
24 */
25
26 #ifndef lint
27 static const char rcsid[] =
28 "@(#) $Header: /tcpdump/master/libpcap/pcap-snit.c,v 1.49 2000-07-01 03:35:07 assar Exp $ (LBL)";
29 #endif
30
31 #include <sys/types.h>
32 #include <sys/time.h>
33 #include <sys/timeb.h>
34 #include <sys/dir.h>
35 #include <sys/fcntlcom.h>
36 #include <sys/file.h>
37 #include <sys/ioctl.h>
38 #include <sys/socket.h>
39 #include <sys/stropts.h>
40
41 #include <net/if.h>
42 #include <net/nit.h>
43 #include <net/nit_if.h>
44 #include <net/nit_pf.h>
45 #include <net/nit_buf.h>
46
47 #include <netinet/in.h>
48 #include <netinet/in_systm.h>
49 #include <netinet/ip.h>
50 #include <netinet/if_ether.h>
51 #include <netinet/ip_var.h>
52 #include <netinet/udp.h>
53 #include <netinet/udp_var.h>
54 #include <netinet/tcp.h>
55 #include <netinet/tcpip.h>
56
57 #include <ctype.h>
58 #include <errno.h>
59 #include <stdio.h>
60 #include <string.h>
61 #include <unistd.h>
62
63 #include "pcap-int.h"
64
65 #include "gnuc.h"
66 #ifdef HAVE_OS_PROTO_H
67 #include "os-proto.h"
68 #endif
69
70 /*
71 * The chunk size for NIT. This is the amount of buffering
72 * done for read calls.
73 */
74 #define CHUNKSIZE (2*1024)
75
76 /*
77 * The total buffer space used by NIT.
78 */
79 #define BUFSPACE (4*CHUNKSIZE)
80
81 /* Forwards */
82 static int nit_setflags(int, int, int, char *);
83
84 int
85 pcap_stats(pcap_t *p, struct pcap_stat *ps)
86 {
87
88 *ps = p->md.stat;
89 return (0);
90 }
91
92 int
93 pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
94 {
95 register int cc, n;
96 register struct bpf_insn *fcode = p->fcode.bf_insns;
97 register u_char *bp, *cp, *ep;
98 register struct nit_bufhdr *hdrp;
99 register struct nit_iftime *ntp;
100 register struct nit_iflen *nlp;
101 register struct nit_ifdrops *ndp;
102 register int caplen;
103
104 cc = p->cc;
105 if (cc == 0) {
106 cc = read(p->fd, (char *)p->buffer, p->bufsize);
107 if (cc < 0) {
108 if (errno == EWOULDBLOCK)
109 return (0);
110 snprintf(p->errbuf, sizeof(p->errbuf), "pcap_read: %s",
111 pcap_strerror(errno));
112 return (-1);
113 }
114 bp = p->buffer;
115 } else
116 bp = p->bp;
117
118 /*
119 * loop through each snapshot in the chunk
120 */
121 n = 0;
122 ep = bp + cc;
123 while (bp < ep) {
124 ++p->md.stat.ps_recv;
125 cp = bp;
126
127 /* get past NIT buffer */
128 hdrp = (struct nit_bufhdr *)cp;
129 cp += sizeof(*hdrp);
130
131 /* get past NIT timer */
132 ntp = (struct nit_iftime *)cp;
133 cp += sizeof(*ntp);
134
135 ndp = (struct nit_ifdrops *)cp;
136 p->md.stat.ps_drop = ndp->nh_drops;
137 cp += sizeof *ndp;
138
139 /* get past packet len */
140 nlp = (struct nit_iflen *)cp;
141 cp += sizeof(*nlp);
142
143 /* next snapshot */
144 bp += hdrp->nhb_totlen;
145
146 caplen = nlp->nh_pktlen;
147 if (caplen > p->snapshot)
148 caplen = p->snapshot;
149
150 if (bpf_filter(fcode, cp, nlp->nh_pktlen, caplen)) {
151 struct pcap_pkthdr h;
152 h.ts = ntp->nh_timestamp;
153 h.len = nlp->nh_pktlen;
154 h.caplen = caplen;
155 (*callback)(user, &h, cp);
156 if (++n >= cnt && cnt >= 0) {
157 p->cc = ep - bp;
158 p->bp = bp;
159 return (n);
160 }
161 }
162 }
163 p->cc = 0;
164 return (n);
165 }
166
167 static int
168 nit_setflags(int fd, int promisc, int to_ms, char *ebuf)
169 {
170 bpf_u_int32 flags;
171 struct strioctl si;
172 struct timeval timeout;
173
174 si.ic_timout = INFTIM;
175 if (to_ms != 0) {
176 timeout.tv_sec = to_ms / 1000;
177 timeout.tv_usec = (to_ms * 1000) % 1000000;
178 si.ic_cmd = NIOCSTIME;
179 si.ic_len = sizeof(timeout);
180 si.ic_dp = (char *)&timeout;
181 if (ioctl(fd, I_STR, (char *)&si) < 0) {
182 snprintf(ebuf, PCAP_ERRBUF_SIZE, "NIOCSTIME: %s",
183 pcap_strerror(errno));
184 return (-1);
185 }
186 }
187 flags = NI_TIMESTAMP | NI_LEN | NI_DROPS;
188 if (promisc)
189 flags |= NI_PROMISC;
190 si.ic_cmd = NIOCSFLAGS;
191 si.ic_len = sizeof(flags);
192 si.ic_dp = (char *)&flags;
193 if (ioctl(fd, I_STR, (char *)&si) < 0) {
194 snprintf(ebuf, PCAP_ERRBUF_SIZE, "NIOCSFLAGS: %s",
195 pcap_strerror(errno));
196 return (-1);
197 }
198 return (0);
199 }
200
201 pcap_t *
202 pcap_open_live(char *device, int snaplen, int promisc, int to_ms, char *ebuf)
203 {
204 struct strioctl si; /* struct for ioctl() */
205 struct ifreq ifr; /* interface request struct */
206 int chunksize = CHUNKSIZE;
207 int fd;
208 static char dev[] = "/dev/nit";
209 register pcap_t *p;
210
211 p = (pcap_t *)malloc(sizeof(*p));
212 if (p == NULL) {
213 strlcpy(ebuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE);
214 return (NULL);
215 }
216
217 if (snaplen < 96)
218 /*
219 * NIT requires a snapshot length of at least 96.
220 */
221 snaplen = 96;
222
223 bzero(p, sizeof(*p));
224 p->fd = fd = open(dev, O_RDONLY);
225 if (fd < 0) {
226 snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s: %s", dev,
227 pcap_strerror(errno));
228 goto bad;
229 }
230
231 /* arrange to get discrete messages from the STREAM and use NIT_BUF */
232 if (ioctl(fd, I_SRDOPT, (char *)RMSGD) < 0) {
233 snprintf(ebuf, PCAP_ERRBUF_SIZE, "I_SRDOPT: %s",
234 pcap_strerror(errno));
235 goto bad;
236 }
237 if (ioctl(fd, I_PUSH, "nbuf") < 0) {
238 snprintf(ebuf, PCAP_ERRBUF_SIZE, "push nbuf: %s",
239 pcap_strerror(errno));
240 goto bad;
241 }
242 /* set the chunksize */
243 si.ic_cmd = NIOCSCHUNK;
244 si.ic_timout = INFTIM;
245 si.ic_len = sizeof(chunksize);
246 si.ic_dp = (char *)&chunksize;
247 if (ioctl(fd, I_STR, (char *)&si) < 0) {
248 snprintf(ebuf, PCAP_ERRBUF_SIZE, "NIOCSCHUNK: %s",
249 pcap_strerror(errno));
250 goto bad;
251 }
252
253 /* request the interface */
254 strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
255 ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = ' ';
256 si.ic_cmd = NIOCBIND;
257 si.ic_len = sizeof(ifr);
258 si.ic_dp = (char *)&ifr;
259 if (ioctl(fd, I_STR, (char *)&si) < 0) {
260 snprintf(ebuf, PCAP_ERRBUF_SIZE, "NIOCBIND: %s: %s",
261 ifr.ifr_name, pcap_strerror(errno));
262 goto bad;
263 }
264
265 /* set the snapshot length */
266 si.ic_cmd = NIOCSSNAP;
267 si.ic_len = sizeof(snaplen);
268 si.ic_dp = (char *)&snaplen;
269 if (ioctl(fd, I_STR, (char *)&si) < 0) {
270 snprintf(ebuf, PCAP_ERRBUF_SIZE, "NIOCSSNAP: %s",
271 pcap_strerror(errno));
272 goto bad;
273 }
274 p->snapshot = snaplen;
275 if (nit_setflags(p->fd, promisc, to_ms, ebuf) < 0)
276 goto bad;
277
278 (void)ioctl(fd, I_FLUSH, (char *)FLUSHR);
279 /*
280 * NIT supports only ethernets.
281 */
282 p->linktype = DLT_EN10MB;
283
284 p->bufsize = BUFSPACE;
285 p->buffer = (u_char *)malloc(p->bufsize);
286 if (p->buffer == NULL) {
287 strlcpy(ebuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE);
288 goto bad;
289 }
290 return (p);
291 bad:
292 if (fd >= 0)
293 close(fd);
294 free(p);
295 return (NULL);
296 }
297
298 int
299 pcap_setfilter(pcap_t *p, struct bpf_program *fp)
300 {
301
302 p->fcode = *fp;
303 return (0);
304 }