etc/protocols - sync with NetBSD-8
[minix.git] / external / bsd / libpcap / dist / pcap-enet.c
blob7aff4e5baf5e245d7dcbf196a25ec7c3dd74e00a
1 /* $NetBSD: pcap-enet.c,v 1.2 2014/11/19 19:33:30 christos Exp $ */
3 /*
4 * Stanford Enetfilter subroutines for tcpdump
6 * Based on the MERIT NNstat etherifrt.c and the Ultrix pcap-pf.c
7 * subroutines.
9 * Rayan Zachariassen, CA*Net
12 #include <sys/cdefs.h>
13 __RCSID("$NetBSD: pcap-enet.c,v 1.2 2014/11/19 19:33:30 christos Exp $");
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif
19 #include <sys/types.h>
20 #include <sys/time.h>
21 #include <sys/file.h>
22 #include <sys/ioctl.h>
23 #include <sys/socket.h>
25 #include <net/if.h>
26 #include <pcap/bpf.h>
27 #include <net/enet.h>
29 #include <netinet/in.h>
30 #include <netinet/if_ether.h>
32 #include <stdio.h>
33 #include <errno.h>
35 #include "interface.h"
37 struct packet_header {
38 #ifdef IBMRTPC
39 struct LengthWords length;
40 struct tap_header tap;
41 #endif /* IBMRTPC */
42 u_char packet[8]
45 extern int errno;
47 #define BUFSPACE (4*1024)
49 /* Forwards */
50 static void efReadError(int, char *);
52 void
53 readloop(int cnt, int if_fd, struct bpf_program *fp, printfunc printit)
55 #ifdef IBMRTPC
56 register struct packet_header *ph;
57 register u_char *bp;
58 register int inc;
59 #else /* !IBMRTPC */
60 static struct timeval tv = { 0 };
61 #endif /* IBMRTPC */
62 register int cc, caplen;
63 register struct bpf_insn *fcode = fp->bf_insns;
64 union {
65 struct packet_header hdr;
66 u_char p[BUFSPACE];
67 u_short s;
68 } buf;
70 while (1) {
71 if ((cc = read(if_fd, (char *)buf.p, sizeof(buf))) < 0)
72 efReadError(if_fd, "reader");
74 #ifdef IBMRTPC
76 * Loop through each packet.
78 bp = buf.p;
79 while (cc > 0) {
80 ph = (struct packet_header *)bp;
81 caplen = ph->tap.th_wirelen > snaplen ? snaplen : ph->tap
82 .th_wirelen ;
83 if (bpf_filter(fcode, (char *)ph->packet,
84 ph->tap.th_wirelen, caplen)) {
85 if (cnt >= 0 && --cnt < 0)
86 goto out;
87 (*printit)((char *)ph->packet,
88 (struct timeval *)ph->tap.th_timestamp,
89 ph->tap.th_wirelen, caplen);
91 inc = ph->length.PacketOffset;
92 cc -= inc;
93 bp += inc;
95 #else /* !IBMRTPC */
96 caplen = cc > snaplen ? snaplen : cc ;
97 if (bpf_filter(fcode, buf.hdr.packet, cc, caplen)) {
98 if (cnt >= 0 && --cnt < 0)
99 goto out;
100 (*printit)(buf.hdr.packet, &tv, cc, caplen);
102 #endif /* IBMRTPC */
104 out:
105 wrapup(if_fd);
108 /* Call ONLY if read() has returned an error on packet filter */
109 static void
110 efReadError(int fid, char *msg)
112 if (errno == EINVAL) { /* read MAXINT bytes already! */
113 if (lseek(fid, 0, 0) < 0) {
114 perror("tcpdump: efReadError/lseek");
115 exit(-1);
117 else
118 return;
120 else {
121 (void) fprintf(stderr, "tcpdump: ");
122 perror(msg);
123 exit(-1);
127 void
128 wrapup(int fd)
130 #ifdef IBMRTPC
131 struct enstats es;
133 if (ioctl(fd, EIOSTATS, &es) == -1) {
134 perror("tcpdump: enet ioctl EIOSTATS error");
135 exit(-1);
138 fprintf(stderr, "%d packets queued", es.enStat_Rcnt);
139 if (es.enStat_Rdrops > 0)
140 fprintf(stderr, ", %d dropped", es.enStat_Rdrops);
141 if (es.enStat_Reads > 0)
142 fprintf(stderr, ", %d tcpdump %s", es.enStat_Reads,
143 es.enStat_Reads > 1 ? "reads" : "read");
144 if (es.enStat_MaxRead > 1)
145 fprintf(stderr, ", %d packets in largest read",
146 es.enStat_MaxRead);
147 putc('\n', stderr);
148 #endif /* IBMRTPC */
149 close(fd);
153 initdevice(char *device, int pflag, int *linktype)
155 struct eniocb ctl;
156 struct enfilter filter;
157 u_int maxwaiting;
158 int if_fd;
160 #ifdef IBMRTPC
161 GETENETDEVICE(0, O_RDONLY, &if_fd);
162 #else /* !IBMRTPC */
163 if_fd = open("/dev/enet", O_RDONLY, 0);
164 #endif /* IBMRTPC */
166 if (if_fd == -1) {
167 perror("tcpdump: enet open error");
168 error(
169 "your system may not be properly configured; see \"man enet(4)\"");
170 exit(-1);
173 /* Get operating parameters. */
175 if (ioctl(if_fd, EIOCGETP, (char *)&ctl) == -1) {
176 perror("tcpdump: enet ioctl EIOCGETP error");
177 exit(-1);
180 /* Set operating parameters. */
182 #ifdef IBMRTPC
183 ctl.en_rtout = 1 * ctl.en_hz;
184 ctl.en_tr_etherhead = 1;
185 ctl.en_tap_network = 1;
186 ctl.en_multi_packet = 1;
187 ctl.en_maxlen = BUFSPACE;
188 #else /* !IBMRTPC */
189 ctl.en_rtout = 64; /* randomly picked value for HZ */
190 #endif /* IBMRTPC */
191 if (ioctl(if_fd, EIOCSETP, &ctl) == -1) {
192 perror("tcpdump: enet ioctl EIOCSETP error");
193 exit(-1);
196 /* Flush the receive queue, since we've changed
197 the operating parameters and we otherwise might
198 receive data without headers. */
200 if (ioctl(if_fd, EIOCFLUSH) == -1) {
201 perror("tcpdump: enet ioctl EIOCFLUSH error");
202 exit(-1);
205 /* Set the receive queue depth to its maximum. */
207 maxwaiting = ctl.en_maxwaiting;
208 if (ioctl(if_fd, EIOCSETW, &maxwaiting) == -1) {
209 perror("tcpdump: enet ioctl EIOCSETW error");
210 exit(-1);
213 #ifdef IBMRTPC
214 /* Clear statistics. */
216 if (ioctl(if_fd, EIOCLRSTAT, 0) == -1) {
217 perror("tcpdump: enet ioctl EIOCLRSTAT error");
218 exit(-1);
220 #endif /* IBMRTPC */
222 /* Set the filter (accept all packets). */
224 filter.enf_Priority = 3;
225 filter.enf_FilterLen = 0;
226 if (ioctl(if_fd, EIOCSETF, &filter) == -1) {
227 perror("tcpdump: enet ioctl EIOCSETF error");
228 exit(-1);
231 * "enetfilter" supports only ethernets.
233 *linktype = DLT_EN10MB;
235 return(if_fd);