]>
The Tcpdump Group git mirrors - libpcap/blob - pcap-snit.c
1601a9a93ec1d6365c6ab0a0fa9771d2e01ae58b
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
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.
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)";
31 #include <sys/types.h>
33 #include <sys/timeb.h>
35 #include <sys/fcntlcom.h>
37 #include <sys/ioctl.h>
38 #include <sys/socket.h>
39 #include <sys/stropts.h>
43 #include <net/nit_if.h>
44 #include <net/nit_pf.h>
45 #include <net/nit_buf.h>
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>
66 #ifdef HAVE_OS_PROTO_H
71 * The chunk size for NIT. This is the amount of buffering
72 * done for read calls.
74 #define CHUNKSIZE (2*1024)
77 * The total buffer space used by NIT.
79 #define BUFSPACE (4*CHUNKSIZE)
82 static int nit_setflags(int, int, int, char *);
85 pcap_stats(pcap_t
*p
, struct pcap_stat
*ps
)
93 pcap_read(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
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
;
106 cc
= read(p
->fd
, (char *)p
->buffer
, p
->bufsize
);
108 if (errno
== EWOULDBLOCK
)
110 snprintf(p
->errbuf
, sizeof(p
->errbuf
), "pcap_read: %s",
111 pcap_strerror(errno
));
119 * loop through each snapshot in the chunk
124 ++p
->md
.stat
.ps_recv
;
127 /* get past NIT buffer */
128 hdrp
= (struct nit_bufhdr
*)cp
;
131 /* get past NIT timer */
132 ntp
= (struct nit_iftime
*)cp
;
135 ndp
= (struct nit_ifdrops
*)cp
;
136 p
->md
.stat
.ps_drop
= ndp
->nh_drops
;
139 /* get past packet len */
140 nlp
= (struct nit_iflen
*)cp
;
144 bp
+= hdrp
->nhb_totlen
;
146 caplen
= nlp
->nh_pktlen
;
147 if (caplen
> p
->snapshot
)
148 caplen
= p
->snapshot
;
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
;
155 (*callback
)(user
, &h
, cp
);
156 if (++n
>= cnt
&& cnt
>= 0) {
168 nit_setflags(int fd
, int promisc
, int to_ms
, char *ebuf
)
172 struct timeval timeout
;
174 si
.ic_timout
= INFTIM
;
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
));
187 flags
= NI_TIMESTAMP
| NI_LEN
| NI_DROPS
;
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
));
202 pcap_open_live(char *device
, int snaplen
, int promisc
, int to_ms
, char *ebuf
)
204 struct strioctl si
; /* struct for ioctl() */
205 struct ifreq ifr
; /* interface request struct */
206 int chunksize
= CHUNKSIZE
;
208 static char dev
[] = "/dev/nit";
211 p
= (pcap_t
*)malloc(sizeof(*p
));
213 strlcpy(ebuf
, pcap_strerror(errno
), PCAP_ERRBUF_SIZE
);
219 * NIT requires a snapshot length of at least 96.
223 bzero(p
, sizeof(*p
));
224 p
->fd
= fd
= open(dev
, O_RDONLY
);
226 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "%s: %s", dev
,
227 pcap_strerror(errno
));
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
));
237 if (ioctl(fd
, I_PUSH
, "nbuf") < 0) {
238 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "push nbuf: %s",
239 pcap_strerror(errno
));
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
));
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
));
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
));
274 p
->snapshot
= snaplen
;
275 if (nit_setflags(p
->fd
, promisc
, to_ms
, ebuf
) < 0)
278 (void)ioctl(fd
, I_FLUSH
, (char *)FLUSHR
);
280 * NIT supports only ethernets.
282 p
->linktype
= DLT_EN10MB
;
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
);
299 pcap_setfilter(pcap_t
*p
, struct bpf_program
*fp
)