]>
The Tcpdump Group git mirrors - libpcap/blob - pcap-nit.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.
22 static const char rcsid
[] =
23 "@(#) $Header: /tcpdump/master/libpcap/pcap-nit.c,v 1.39 2000-10-28 00:01:29 guy Exp $ (LBL)";
30 #include <sys/types.h>
32 #include <sys/timeb.h>
34 #include <sys/ioctl.h>
35 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <netinet/in_systm.h>
42 #include <netinet/ip.h>
43 #include <netinet/if_ether.h>
44 #include <netinet/ip_var.h>
45 #include <netinet/udp.h>
46 #include <netinet/udp_var.h>
47 #include <netinet/tcp.h>
48 #include <netinet/tcpip.h>
56 #ifdef HAVE_OS_PROTO_H
61 * The chunk size for NIT. This is the amount of buffering
62 * done for read calls.
64 #define CHUNKSIZE (2*1024)
67 * The total buffer space used by NIT.
69 #define BUFSPACE (4*CHUNKSIZE)
72 static int nit_setflags(int, int, int, char *);
75 pcap_stats(pcap_t
*p
, struct pcap_stat
*ps
)
83 pcap_read(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
86 register struct bpf_insn
*fcode
= p
->fcode
.bf_insns
;
87 register u_char
*bp
, *cp
, *ep
;
88 register struct nit_hdr
*nh
;
93 cc
= read(p
->fd
, (char *)p
->buffer
, p
->bufsize
);
95 if (errno
== EWOULDBLOCK
)
97 snprintf(p
->errbuf
, sizeof(p
->errbuf
), "pcap_read: %s",
98 pcap_strerror(errno
));
106 * Loop through each packet. The increment expression
107 * rounds up to the next int boundary past the end of
108 * the previous packet.
113 nh
= (struct nit_hdr
*)bp
;
114 cp
= bp
+ sizeof(*nh
);
116 switch (nh
->nh_state
) {
124 p
->md
.stat
.ps_drop
= nh
->nh_dropped
;
131 snprintf(p
->errbuf
, sizeof(p
->errbuf
),
132 "bad nit state %d", nh
->nh_state
);
135 ++p
->md
.stat
.ps_recv
;
136 bp
+= ((sizeof(struct nit_hdr
) + nh
->nh_datalen
+
137 sizeof(int) - 1) & ~(sizeof(int) - 1));
139 caplen
= nh
->nh_wirelen
;
140 if (caplen
> p
->snapshot
)
141 caplen
= p
->snapshot
;
142 if (bpf_filter(fcode
, cp
, nh
->nh_wirelen
, caplen
)) {
143 struct pcap_pkthdr h
;
144 h
.ts
= nh
->nh_timestamp
;
145 h
.len
= nh
->nh_wirelen
;
147 (*callback
)(user
, &h
, cp
);
148 if (++n
>= cnt
&& cnt
>= 0) {
160 nit_setflags(int fd
, int promisc
, int to_ms
, char *ebuf
)
164 memset(&nioc
, 0, sizeof(nioc
));
165 nioc
.nioc_bufspace
= BUFSPACE
;
166 nioc
.nioc_chunksize
= CHUNKSIZE
;
167 nioc
.nioc_typetomatch
= NT_ALLTYPES
;
168 nioc
.nioc_snaplen
= p
->snapshot
;
169 nioc
.nioc_bufalign
= sizeof(int);
170 nioc
.nioc_bufoffset
= 0;
173 nioc
.nioc_flags
|= NF_TIMEOUT
;
174 nioc
.nioc_timeout
.tv_sec
= to_ms
/ 1000;
175 nioc
.nioc_timeout
.tv_usec
= (to_ms
* 1000) % 1000000;
178 nioc
.nioc_flags
|= NF_PROMISC
;
180 if (ioctl(fd
, SIOCSNIT
, &nioc
) < 0) {
181 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "SIOCSNIT: %s",
182 pcap_strerror(errno
));
189 pcap_open_live(char *device
, int snaplen
, int promisc
, int to_ms
, char *ebuf
)
192 struct sockaddr_nit snit
;
195 p
= (pcap_t
*)malloc(sizeof(*p
));
197 strlcpy(ebuf
, pcap_strerror(errno
), PCAP_ERRBUF_SIZE
);
203 * NIT requires a snapshot length of at least 96.
207 memset(p
, 0, sizeof(*p
));
208 p
->fd
= fd
= socket(AF_NIT
, SOCK_RAW
, NITPROTO_RAW
);
210 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
211 "socket: %s", pcap_strerror(errno
));
214 snit
.snit_family
= AF_NIT
;
215 (void)strncpy(snit
.snit_ifname
, device
, NITIFSIZ
);
217 if (bind(fd
, (struct sockaddr
*)&snit
, sizeof(snit
))) {
218 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
219 "bind: %s: %s", snit
.snit_ifname
, pcap_strerror(errno
));
222 p
->snapshot
= snaplen
;
223 nit_setflags(p
->fd
, promisc
, to_ms
, ebuf
);
226 * NIT supports only ethernets.
228 p
->linktype
= DLT_EN10MB
;
230 p
->bufsize
= BUFSPACE
;
231 p
->buffer
= (u_char
*)malloc(p
->bufsize
);
232 if (p
->buffer
== NULL
) {
233 strlcpy(ebuf
, pcap_strerror(errno
), PCAP_ERRBUF_SIZE
);
245 pcap_setfilter(pcap_t
*p
, struct bpf_program
*fp
)
248 if (install_bpf_program(p
, fp
) < 0)