]> The Tcpdump Group git mirrors - libpcap/blob - pcap-nit.c
Add comments to "pcap_stats()" indicating what the counters mean on the
[libpcap] / pcap-nit.c
1 /*
2 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996
3 * The Regents of the University of California. All rights reserved.
4 *
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.
20 */
21 #ifndef lint
22 static const char rcsid[] =
23 "@(#) $Header: /tcpdump/master/libpcap/pcap-nit.c,v 1.40 2001-07-29 01:22:41 guy Exp $ (LBL)";
24 #endif
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <sys/types.h>
31 #include <sys/time.h>
32 #include <sys/timeb.h>
33 #include <sys/file.h>
34 #include <sys/ioctl.h>
35 #include <sys/socket.h>
36
37 #include <net/if.h>
38 #include <net/nit.h>
39
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>
49
50 #include <ctype.h>
51 #include <errno.h>
52 #include <stdio.h>
53
54 #include "pcap-int.h"
55
56 #ifdef HAVE_OS_PROTO_H
57 #include "os-proto.h"
58 #endif
59
60 /*
61 * The chunk size for NIT. This is the amount of buffering
62 * done for read calls.
63 */
64 #define CHUNKSIZE (2*1024)
65
66 /*
67 * The total buffer space used by NIT.
68 */
69 #define BUFSPACE (4*CHUNKSIZE)
70
71 /* Forwards */
72 static int nit_setflags(int, int, int, char *);
73
74 int
75 pcap_stats(pcap_t *p, struct pcap_stat *ps)
76 {
77
78 /*
79 * "ps_recv" counts packets handed to the filter, not packets
80 * that passed the filter.
81 *
82 * "ps_drop" presumably counts packets dropped by the socket
83 * because of flow control requirements or resource exhaustion;
84 * it doesn't count packets dropped by the interface driver.
85 * As filtering is done in userland, it counts packets regardless
86 * of whether they would've passed the filter.
87 */
88 *ps = p->md.stat;
89 return (0);
90 }
91
92 int
93 pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
94 {
95 register int cc, n;
96 register struct bpf_insn *fcode = p->fcode.bf_insns;
97 register u_char *bp, *cp, *ep;
98 register struct nit_hdr *nh;
99 register int caplen;
100
101 cc = p->cc;
102 if (cc == 0) {
103 cc = read(p->fd, (char *)p->buffer, p->bufsize);
104 if (cc < 0) {
105 if (errno == EWOULDBLOCK)
106 return (0);
107 snprintf(p->errbuf, sizeof(p->errbuf), "pcap_read: %s",
108 pcap_strerror(errno));
109 return (-1);
110 }
111 bp = p->buffer;
112 } else
113 bp = p->bp;
114
115 /*
116 * Loop through each packet. The increment expression
117 * rounds up to the next int boundary past the end of
118 * the previous packet.
119 */
120 n = 0;
121 ep = bp + cc;
122 while (bp < ep) {
123 nh = (struct nit_hdr *)bp;
124 cp = bp + sizeof(*nh);
125
126 switch (nh->nh_state) {
127
128 case NIT_CATCH:
129 break;
130
131 case NIT_NOMBUF:
132 case NIT_NOCLUSTER:
133 case NIT_NOSPACE:
134 p->md.stat.ps_drop = nh->nh_dropped;
135 continue;
136
137 case NIT_SEQNO:
138 continue;
139
140 default:
141 snprintf(p->errbuf, sizeof(p->errbuf),
142 "bad nit state %d", nh->nh_state);
143 return (-1);
144 }
145 ++p->md.stat.ps_recv;
146 bp += ((sizeof(struct nit_hdr) + nh->nh_datalen +
147 sizeof(int) - 1) & ~(sizeof(int) - 1));
148
149 caplen = nh->nh_wirelen;
150 if (caplen > p->snapshot)
151 caplen = p->snapshot;
152 if (bpf_filter(fcode, cp, nh->nh_wirelen, caplen)) {
153 struct pcap_pkthdr h;
154 h.ts = nh->nh_timestamp;
155 h.len = nh->nh_wirelen;
156 h.caplen = caplen;
157 (*callback)(user, &h, cp);
158 if (++n >= cnt && cnt >= 0) {
159 p->cc = ep - bp;
160 p->bp = bp;
161 return (n);
162 }
163 }
164 }
165 p->cc = 0;
166 return (n);
167 }
168
169 static int
170 nit_setflags(int fd, int promisc, int to_ms, char *ebuf)
171 {
172 struct nit_ioc nioc;
173
174 memset(&nioc, 0, sizeof(nioc));
175 nioc.nioc_bufspace = BUFSPACE;
176 nioc.nioc_chunksize = CHUNKSIZE;
177 nioc.nioc_typetomatch = NT_ALLTYPES;
178 nioc.nioc_snaplen = p->snapshot;
179 nioc.nioc_bufalign = sizeof(int);
180 nioc.nioc_bufoffset = 0;
181
182 if (to_ms != 0) {
183 nioc.nioc_flags |= NF_TIMEOUT;
184 nioc.nioc_timeout.tv_sec = to_ms / 1000;
185 nioc.nioc_timeout.tv_usec = (to_ms * 1000) % 1000000;
186 }
187 if (promisc)
188 nioc.nioc_flags |= NF_PROMISC;
189
190 if (ioctl(fd, SIOCSNIT, &nioc) < 0) {
191 snprintf(ebuf, PCAP_ERRBUF_SIZE, "SIOCSNIT: %s",
192 pcap_strerror(errno));
193 return (-1);
194 }
195 return (0);
196 }
197
198 pcap_t *
199 pcap_open_live(char *device, int snaplen, int promisc, int to_ms, char *ebuf)
200 {
201 int fd;
202 struct sockaddr_nit snit;
203 register pcap_t *p;
204
205 p = (pcap_t *)malloc(sizeof(*p));
206 if (p == NULL) {
207 strlcpy(ebuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE);
208 return (NULL);
209 }
210
211 if (snaplen < 96)
212 /*
213 * NIT requires a snapshot length of at least 96.
214 */
215 snaplen = 96;
216
217 memset(p, 0, sizeof(*p));
218 p->fd = fd = socket(AF_NIT, SOCK_RAW, NITPROTO_RAW);
219 if (fd < 0) {
220 snprintf(ebuf, PCAP_ERRBUF_SIZE,
221 "socket: %s", pcap_strerror(errno));
222 goto bad;
223 }
224 snit.snit_family = AF_NIT;
225 (void)strncpy(snit.snit_ifname, device, NITIFSIZ);
226
227 if (bind(fd, (struct sockaddr *)&snit, sizeof(snit))) {
228 snprintf(ebuf, PCAP_ERRBUF_SIZE,
229 "bind: %s: %s", snit.snit_ifname, pcap_strerror(errno));
230 goto bad;
231 }
232 p->snapshot = snaplen;
233 nit_setflags(p->fd, promisc, to_ms, ebuf);
234
235 /*
236 * NIT supports only ethernets.
237 */
238 p->linktype = DLT_EN10MB;
239
240 p->bufsize = BUFSPACE;
241 p->buffer = (u_char *)malloc(p->bufsize);
242 if (p->buffer == NULL) {
243 strlcpy(ebuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE);
244 goto bad;
245 }
246 return (p);
247 bad:
248 if (fd >= 0)
249 close(fd);
250 free(p);
251 return (NULL);
252 }
253
254 int
255 pcap_setfilter(pcap_t *p, struct bpf_program *fp)
256 {
257
258 if (install_bpf_program(p, fp) < 0)
259 return (-1);
260 return (0);
261 }