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: /usr/local/cvs/linux/tools/build/e100boot/libpcap-0.4/pcap-nit.c,v 1.1 1999/08/26 10:05:24 johana Exp $ (LBL)";
26 #include <sys/types.h>
28 #include <sys/timeb.h>
30 #include <sys/ioctl.h>
31 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <netinet/in_systm.h>
38 #include <netinet/ip.h>
39 #include <netinet/if_ether.h>
40 #include <netinet/ip_var.h>
41 #include <netinet/udp.h>
42 #include <netinet/udp_var.h>
43 #include <netinet/tcp.h>
44 #include <netinet/tcpip.h>
53 #ifdef HAVE_OS_PROTO_H
58 * The chunk size for NIT. This is the amount of buffering
59 * done for read calls.
61 #define CHUNKSIZE (2*1024)
64 * The total buffer space used by NIT.
66 #define BUFSPACE (4*CHUNKSIZE)
69 static int nit_setflags(int, int, int, char *);
72 pcap_stats(pcap_t *p, struct pcap_stat *ps)
80 pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
83 register struct bpf_insn *fcode = p->fcode.bf_insns;
84 register u_char *bp, *cp, *ep;
85 register struct nit_hdr *nh;
90 cc = read(p->fd, (char *)p->buffer, p->bufsize);
92 if (errno == EWOULDBLOCK)
94 sprintf(p->errbuf, "pcap_read: %s",
95 pcap_strerror(errno));
103 * Loop through each packet. The increment expression
104 * rounds up to the next int boundary past the end of
105 * the previous packet.
110 nh = (struct nit_hdr *)bp;
111 cp = bp + sizeof(*nh);
113 switch (nh->nh_state) {
121 p->md.stat.ps_drop = nh->nh_dropped;
128 sprintf(p->errbuf, "bad nit state %d", nh->nh_state);
131 ++p->md.stat.ps_recv;
132 bp += ((sizeof(struct nit_hdr) + nh->nh_datalen +
133 sizeof(int) - 1) & ~(sizeof(int) - 1));
135 caplen = nh->nh_wirelen;
136 if (caplen > p->snapshot)
137 caplen = p->snapshot;
138 if (bpf_filter(fcode, cp, nh->nh_wirelen, caplen)) {
139 struct pcap_pkthdr h;
140 h.ts = nh->nh_timestamp;
141 h.len = nh->nh_wirelen;
143 (*callback)(user, &h, cp);
144 if (++n >= cnt && cnt >= 0) {
156 nit_setflags(int fd, int promisc, int to_ms, char *ebuf)
160 bzero((char *)&nioc, sizeof(nioc));
161 nioc.nioc_bufspace = BUFSPACE;
162 nioc.nioc_chunksize = CHUNKSIZE;
163 nioc.nioc_typetomatch = NT_ALLTYPES;
164 nioc.nioc_snaplen = p->snapshot;
165 nioc.nioc_bufalign = sizeof(int);
166 nioc.nioc_bufoffset = 0;
169 nioc.nioc_flags |= NF_TIMEOUT;
170 nioc.nioc_timeout.tv_sec = to_ms / 1000;
171 nioc.nioc_timeout.tv_usec = (to_ms * 1000) % 1000000;
174 nioc.nioc_flags |= NF_PROMISC;
176 if (ioctl(fd, SIOCSNIT, &nioc) < 0) {
177 sprintf(ebuf, "SIOCSNIT: %s", pcap_strerror(errno));
184 pcap_open_live(char *device, int snaplen, int promisc, int to_ms, char *ebuf)
187 struct sockaddr_nit snit;
190 p = (pcap_t *)malloc(sizeof(*p));
192 strcpy(ebuf, pcap_strerror(errno));
198 * NIT requires a snapshot length of at least 96.
202 bzero(p, sizeof(*p));
203 p->fd = fd = socket(AF_NIT, SOCK_RAW, NITPROTO_RAW);
205 sprintf(ebuf, "socket: %s", pcap_strerror(errno));
208 snit.snit_family = AF_NIT;
209 (void)strncpy(snit.snit_ifname, device, NITIFSIZ);
211 if (bind(fd, (struct sockaddr *)&snit, sizeof(snit))) {
212 sprintf(ebuf, "bind: %s: %s", snit.snit_ifname,
213 pcap_strerror(errno));
216 p->snapshot = snaplen;
217 nit_setflags(p->fd, promisc, to_ms, ebuf);
220 * NIT supports only ethernets.
222 p->linktype = DLT_EN10MB;
224 p->bufsize = BUFSPACE;
225 p->buffer = (u_char *)malloc(p->bufsize);
226 if (p->buffer == NULL) {
227 strcpy(ebuf, pcap_strerror(errno));
239 pcap_setfilter(pcap_t *p, struct bpf_program *fp)