add winpcap 4.0.2 from url http://www.winpcap.org/
[natblaster.git] / winpcap / wpcap / libpcap / pcap-snit.c
blob60abe64447b837e27cf741bf22d99045a10a9097
1 /*
2 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996
3 * The Regents of the University of California. All rights reserved.
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.
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.
26 #ifndef lint
27 static const char rcsid[] _U_ =
28 "@(#) $Header: /tcpdump/master/libpcap/pcap-snit.c,v 1.72.2.1 2005/05/03 18:54:38 guy Exp $ (LBL)";
29 #endif
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
35 #include <sys/types.h>
36 #include <sys/time.h>
37 #include <sys/timeb.h>
38 #include <sys/dir.h>
39 #include <sys/fcntlcom.h>
40 #include <sys/file.h>
41 #include <sys/ioctl.h>
42 #include <sys/socket.h>
43 #include <sys/stropts.h>
45 #include <net/if.h>
46 #include <net/nit.h>
47 #include <net/nit_if.h>
48 #include <net/nit_pf.h>
49 #include <net/nit_buf.h>
51 #include <netinet/in.h>
52 #include <netinet/in_systm.h>
53 #include <netinet/ip.h>
54 #include <netinet/if_ether.h>
55 #include <netinet/ip_var.h>
56 #include <netinet/udp.h>
57 #include <netinet/udp_var.h>
58 #include <netinet/tcp.h>
59 #include <netinet/tcpip.h>
61 #include <ctype.h>
62 #include <errno.h>
63 #include <stdio.h>
64 #include <string.h>
65 #include <unistd.h>
67 #include "pcap-int.h"
69 #ifdef HAVE_OS_PROTO_H
70 #include "os-proto.h"
71 #endif
74 * The chunk size for NIT. This is the amount of buffering
75 * done for read calls.
77 #define CHUNKSIZE (2*1024)
80 * The total buffer space used by NIT.
82 #define BUFSPACE (4*CHUNKSIZE)
84 /* Forwards */
85 static int nit_setflags(int, int, int, char *);
87 static int
88 pcap_stats_snit(pcap_t *p, struct pcap_stat *ps)
92 * "ps_recv" counts packets handed to the filter, not packets
93 * that passed the filter. As filtering is done in userland,
94 * this does not include packets dropped because we ran out
95 * of buffer space.
97 * "ps_drop" counts packets dropped inside the "/dev/nit"
98 * device because of flow control requirements or resource
99 * exhaustion; it doesn't count packets dropped by the
100 * interface driver, or packets dropped upstream. As filtering
101 * is done in userland, it counts packets regardless of whether
102 * they would've passed the filter.
104 * These statistics don't include packets not yet read from the
105 * kernel by libpcap or packets not yet read from libpcap by the
106 * application.
108 *ps = p->md.stat;
109 return (0);
112 static int
113 pcap_read_snit(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
115 register int cc, n;
116 register struct bpf_insn *fcode = p->fcode.bf_insns;
117 register u_char *bp, *cp, *ep;
118 register struct nit_bufhdr *hdrp;
119 register struct nit_iftime *ntp;
120 register struct nit_iflen *nlp;
121 register struct nit_ifdrops *ndp;
122 register int caplen;
124 cc = p->cc;
125 if (cc == 0) {
126 cc = read(p->fd, (char *)p->buffer, p->bufsize);
127 if (cc < 0) {
128 if (errno == EWOULDBLOCK)
129 return (0);
130 snprintf(p->errbuf, sizeof(p->errbuf), "pcap_read: %s",
131 pcap_strerror(errno));
132 return (-1);
134 bp = p->buffer;
135 } else
136 bp = p->bp;
139 * loop through each snapshot in the chunk
141 n = 0;
142 ep = bp + cc;
143 while (bp < ep) {
145 * Has "pcap_breakloop()" been called?
146 * If so, return immediately - if we haven't read any
147 * packets, clear the flag and return -2 to indicate
148 * that we were told to break out of the loop, otherwise
149 * leave the flag set, so that the *next* call will break
150 * out of the loop without having read any packets, and
151 * return the number of packets we've processed so far.
153 if (p->break_loop) {
154 if (n == 0) {
155 p->break_loop = 0;
156 return (-2);
157 } else {
158 p->bp = bp;
159 p->cc = ep - bp;
160 return (n);
164 ++p->md.stat.ps_recv;
165 cp = bp;
167 /* get past NIT buffer */
168 hdrp = (struct nit_bufhdr *)cp;
169 cp += sizeof(*hdrp);
171 /* get past NIT timer */
172 ntp = (struct nit_iftime *)cp;
173 cp += sizeof(*ntp);
175 ndp = (struct nit_ifdrops *)cp;
176 p->md.stat.ps_drop = ndp->nh_drops;
177 cp += sizeof *ndp;
179 /* get past packet len */
180 nlp = (struct nit_iflen *)cp;
181 cp += sizeof(*nlp);
183 /* next snapshot */
184 bp += hdrp->nhb_totlen;
186 caplen = nlp->nh_pktlen;
187 if (caplen > p->snapshot)
188 caplen = p->snapshot;
190 if (bpf_filter(fcode, cp, nlp->nh_pktlen, caplen)) {
191 struct pcap_pkthdr h;
192 h.ts = ntp->nh_timestamp;
193 h.len = nlp->nh_pktlen;
194 h.caplen = caplen;
195 (*callback)(user, &h, cp);
196 if (++n >= cnt && cnt >= 0) {
197 p->cc = ep - bp;
198 p->bp = bp;
199 return (n);
203 p->cc = 0;
204 return (n);
207 static int
208 pcap_inject_snit(pcap_t *p, const void *buf, size_t size)
210 struct strbuf ctl, data;
213 * XXX - can we just do
215 ret = write(pd->f, buf, size);
217 ctl.len = sizeof(*sa); /* XXX - what was this? */
218 ctl.buf = (char *)sa;
219 data.buf = buf;
220 data.len = size;
221 ret = putmsg(p->fd, &ctl, &data);
222 if (ret == -1) {
223 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s",
224 pcap_strerror(errno));
225 return (-1);
227 return (ret);
230 static int
231 nit_setflags(int fd, int promisc, int to_ms, char *ebuf)
233 bpf_u_int32 flags;
234 struct strioctl si;
235 struct timeval timeout;
237 si.ic_timout = INFTIM;
238 if (to_ms != 0) {
239 timeout.tv_sec = to_ms / 1000;
240 timeout.tv_usec = (to_ms * 1000) % 1000000;
241 si.ic_cmd = NIOCSTIME;
242 si.ic_len = sizeof(timeout);
243 si.ic_dp = (char *)&timeout;
244 if (ioctl(fd, I_STR, (char *)&si) < 0) {
245 snprintf(ebuf, PCAP_ERRBUF_SIZE, "NIOCSTIME: %s",
246 pcap_strerror(errno));
247 return (-1);
250 flags = NI_TIMESTAMP | NI_LEN | NI_DROPS;
251 if (promisc)
252 flags |= NI_PROMISC;
253 si.ic_cmd = NIOCSFLAGS;
254 si.ic_len = sizeof(flags);
255 si.ic_dp = (char *)&flags;
256 if (ioctl(fd, I_STR, (char *)&si) < 0) {
257 snprintf(ebuf, PCAP_ERRBUF_SIZE, "NIOCSFLAGS: %s",
258 pcap_strerror(errno));
259 return (-1);
261 return (0);
264 pcap_t *
265 pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
266 char *ebuf)
268 struct strioctl si; /* struct for ioctl() */
269 struct ifreq ifr; /* interface request struct */
270 int chunksize = CHUNKSIZE;
271 int fd;
272 static char dev[] = "/dev/nit";
273 register pcap_t *p;
275 p = (pcap_t *)malloc(sizeof(*p));
276 if (p == NULL) {
277 strlcpy(ebuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE);
278 return (NULL);
281 if (snaplen < 96)
283 * NIT requires a snapshot length of at least 96.
285 snaplen = 96;
287 memset(p, 0, sizeof(*p));
289 * Initially try a read/write open (to allow the inject
290 * method to work). If that fails due to permission
291 * issues, fall back to read-only. This allows a
292 * non-root user to be granted specific access to pcap
293 * capabilities via file permissions.
295 * XXX - we should have an API that has a flag that
296 * controls whether to open read-only or read-write,
297 * so that denial of permission to send (or inability
298 * to send, if sending packets isn't supported on
299 * the device in question) can be indicated at open
300 * time.
302 p->fd = fd = open(dev, O_RDWR);
303 if (fd < 0 && errno == EACCES)
304 p->fd = fd = open(dev, O_RDONLY);
305 if (fd < 0) {
306 snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s: %s", dev,
307 pcap_strerror(errno));
308 goto bad;
311 /* arrange to get discrete messages from the STREAM and use NIT_BUF */
312 if (ioctl(fd, I_SRDOPT, (char *)RMSGD) < 0) {
313 snprintf(ebuf, PCAP_ERRBUF_SIZE, "I_SRDOPT: %s",
314 pcap_strerror(errno));
315 goto bad;
317 if (ioctl(fd, I_PUSH, "nbuf") < 0) {
318 snprintf(ebuf, PCAP_ERRBUF_SIZE, "push nbuf: %s",
319 pcap_strerror(errno));
320 goto bad;
322 /* set the chunksize */
323 si.ic_cmd = NIOCSCHUNK;
324 si.ic_timout = INFTIM;
325 si.ic_len = sizeof(chunksize);
326 si.ic_dp = (char *)&chunksize;
327 if (ioctl(fd, I_STR, (char *)&si) < 0) {
328 snprintf(ebuf, PCAP_ERRBUF_SIZE, "NIOCSCHUNK: %s",
329 pcap_strerror(errno));
330 goto bad;
333 /* request the interface */
334 strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
335 ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
336 si.ic_cmd = NIOCBIND;
337 si.ic_len = sizeof(ifr);
338 si.ic_dp = (char *)&ifr;
339 if (ioctl(fd, I_STR, (char *)&si) < 0) {
340 snprintf(ebuf, PCAP_ERRBUF_SIZE, "NIOCBIND: %s: %s",
341 ifr.ifr_name, pcap_strerror(errno));
342 goto bad;
345 /* set the snapshot length */
346 si.ic_cmd = NIOCSSNAP;
347 si.ic_len = sizeof(snaplen);
348 si.ic_dp = (char *)&snaplen;
349 if (ioctl(fd, I_STR, (char *)&si) < 0) {
350 snprintf(ebuf, PCAP_ERRBUF_SIZE, "NIOCSSNAP: %s",
351 pcap_strerror(errno));
352 goto bad;
354 p->snapshot = snaplen;
355 if (nit_setflags(p->fd, promisc, to_ms, ebuf) < 0)
356 goto bad;
358 (void)ioctl(fd, I_FLUSH, (char *)FLUSHR);
360 * NIT supports only ethernets.
362 p->linktype = DLT_EN10MB;
364 p->bufsize = BUFSPACE;
365 p->buffer = (u_char *)malloc(p->bufsize);
366 if (p->buffer == NULL) {
367 strlcpy(ebuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE);
368 goto bad;
372 * "p->fd" is an FD for a STREAMS device, so "select()" and
373 * "poll()" should work on it.
375 p->selectable_fd = p->fd;
378 * This is (presumably) a real Ethernet capture; give it a
379 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
380 * that an application can let you choose it, in case you're
381 * capturing DOCSIS traffic that a Cisco Cable Modem
382 * Termination System is putting out onto an Ethernet (it
383 * doesn't put an Ethernet header onto the wire, it puts raw
384 * DOCSIS frames out on the wire inside the low-level
385 * Ethernet framing).
387 p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
389 * If that fails, just leave the list empty.
391 if (p->dlt_list != NULL) {
392 p->dlt_list[0] = DLT_EN10MB;
393 p->dlt_list[1] = DLT_DOCSIS;
394 p->dlt_count = 2;
397 p->read_op = pcap_read_snit;
398 p->inject_op = pcap_inject_snit;
399 p->setfilter_op = install_bpf_program; /* no kernel filtering */
400 p->setdirection_op = NULL; /* Not implemented. */
401 p->set_datalink_op = NULL; /* can't change data link type */
402 p->getnonblock_op = pcap_getnonblock_fd;
403 p->setnonblock_op = pcap_setnonblock_fd;
404 p->stats_op = pcap_stats_snit;
405 p->close_op = pcap_close_common;
407 return (p);
408 bad:
409 if (fd >= 0)
410 close(fd);
411 free(p);
412 return (NULL);
416 pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
418 return (0);