]>
The Tcpdump Group git mirrors - libpcap/blob - pcap-snit.c
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.50 2000-07-11 00:37:07 assar Exp $ (LBL)";
35 #include <sys/types.h>
37 #include <sys/timeb.h>
39 #include <sys/fcntlcom.h>
41 #include <sys/ioctl.h>
42 #include <sys/socket.h>
43 #include <sys/stropts.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>
69 #ifdef HAVE_OS_PROTO_H
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)
85 static int nit_setflags(int, int, int, char *);
88 pcap_stats(pcap_t
*p
, struct pcap_stat
*ps
)
96 pcap_read(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
99 register struct bpf_insn
*fcode
= p
->fcode
.bf_insns
;
100 register u_char
*bp
, *cp
, *ep
;
101 register struct nit_bufhdr
*hdrp
;
102 register struct nit_iftime
*ntp
;
103 register struct nit_iflen
*nlp
;
104 register struct nit_ifdrops
*ndp
;
109 cc
= read(p
->fd
, (char *)p
->buffer
, p
->bufsize
);
111 if (errno
== EWOULDBLOCK
)
113 snprintf(p
->errbuf
, sizeof(p
->errbuf
), "pcap_read: %s",
114 pcap_strerror(errno
));
122 * loop through each snapshot in the chunk
127 ++p
->md
.stat
.ps_recv
;
130 /* get past NIT buffer */
131 hdrp
= (struct nit_bufhdr
*)cp
;
134 /* get past NIT timer */
135 ntp
= (struct nit_iftime
*)cp
;
138 ndp
= (struct nit_ifdrops
*)cp
;
139 p
->md
.stat
.ps_drop
= ndp
->nh_drops
;
142 /* get past packet len */
143 nlp
= (struct nit_iflen
*)cp
;
147 bp
+= hdrp
->nhb_totlen
;
149 caplen
= nlp
->nh_pktlen
;
150 if (caplen
> p
->snapshot
)
151 caplen
= p
->snapshot
;
153 if (bpf_filter(fcode
, cp
, nlp
->nh_pktlen
, caplen
)) {
154 struct pcap_pkthdr h
;
155 h
.ts
= ntp
->nh_timestamp
;
156 h
.len
= nlp
->nh_pktlen
;
158 (*callback
)(user
, &h
, cp
);
159 if (++n
>= cnt
&& cnt
>= 0) {
171 nit_setflags(int fd
, int promisc
, int to_ms
, char *ebuf
)
175 struct timeval timeout
;
177 si
.ic_timout
= INFTIM
;
179 timeout
.tv_sec
= to_ms
/ 1000;
180 timeout
.tv_usec
= (to_ms
* 1000) % 1000000;
181 si
.ic_cmd
= NIOCSTIME
;
182 si
.ic_len
= sizeof(timeout
);
183 si
.ic_dp
= (char *)&timeout
;
184 if (ioctl(fd
, I_STR
, (char *)&si
) < 0) {
185 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "NIOCSTIME: %s",
186 pcap_strerror(errno
));
190 flags
= NI_TIMESTAMP
| NI_LEN
| NI_DROPS
;
193 si
.ic_cmd
= NIOCSFLAGS
;
194 si
.ic_len
= sizeof(flags
);
195 si
.ic_dp
= (char *)&flags
;
196 if (ioctl(fd
, I_STR
, (char *)&si
) < 0) {
197 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "NIOCSFLAGS: %s",
198 pcap_strerror(errno
));
205 pcap_open_live(char *device
, int snaplen
, int promisc
, int to_ms
, char *ebuf
)
207 struct strioctl si
; /* struct for ioctl() */
208 struct ifreq ifr
; /* interface request struct */
209 int chunksize
= CHUNKSIZE
;
211 static char dev
[] = "/dev/nit";
214 p
= (pcap_t
*)malloc(sizeof(*p
));
216 strlcpy(ebuf
, pcap_strerror(errno
), PCAP_ERRBUF_SIZE
);
222 * NIT requires a snapshot length of at least 96.
226 bzero(p
, sizeof(*p
));
227 p
->fd
= fd
= open(dev
, O_RDONLY
);
229 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "%s: %s", dev
,
230 pcap_strerror(errno
));
234 /* arrange to get discrete messages from the STREAM and use NIT_BUF */
235 if (ioctl(fd
, I_SRDOPT
, (char *)RMSGD
) < 0) {
236 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "I_SRDOPT: %s",
237 pcap_strerror(errno
));
240 if (ioctl(fd
, I_PUSH
, "nbuf") < 0) {
241 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "push nbuf: %s",
242 pcap_strerror(errno
));
245 /* set the chunksize */
246 si
.ic_cmd
= NIOCSCHUNK
;
247 si
.ic_timout
= INFTIM
;
248 si
.ic_len
= sizeof(chunksize
);
249 si
.ic_dp
= (char *)&chunksize
;
250 if (ioctl(fd
, I_STR
, (char *)&si
) < 0) {
251 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "NIOCSCHUNK: %s",
252 pcap_strerror(errno
));
256 /* request the interface */
257 strncpy(ifr
.ifr_name
, device
, sizeof(ifr
.ifr_name
));
258 ifr
.ifr_name
[sizeof(ifr
.ifr_name
) - 1] = ' ';
259 si
.ic_cmd
= NIOCBIND
;
260 si
.ic_len
= sizeof(ifr
);
261 si
.ic_dp
= (char *)&ifr
;
262 if (ioctl(fd
, I_STR
, (char *)&si
) < 0) {
263 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "NIOCBIND: %s: %s",
264 ifr
.ifr_name
, pcap_strerror(errno
));
268 /* set the snapshot length */
269 si
.ic_cmd
= NIOCSSNAP
;
270 si
.ic_len
= sizeof(snaplen
);
271 si
.ic_dp
= (char *)&snaplen
;
272 if (ioctl(fd
, I_STR
, (char *)&si
) < 0) {
273 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "NIOCSSNAP: %s",
274 pcap_strerror(errno
));
277 p
->snapshot
= snaplen
;
278 if (nit_setflags(p
->fd
, promisc
, to_ms
, ebuf
) < 0)
281 (void)ioctl(fd
, I_FLUSH
, (char *)FLUSHR
);
283 * NIT supports only ethernets.
285 p
->linktype
= DLT_EN10MB
;
287 p
->bufsize
= BUFSPACE
;
288 p
->buffer
= (u_char
*)malloc(p
->bufsize
);
289 if (p
->buffer
== NULL
) {
290 strlcpy(ebuf
, pcap_strerror(errno
), PCAP_ERRBUF_SIZE
);
302 pcap_setfilter(pcap_t
*p
, struct bpf_program
*fp
)