Busybox: Upgrade to 1.21.1 (stable). lsof active.
[tomato.git] / release / src / router / libpcap / pcap-enet.c
blob5ad921c252f9d1a54c1b94a925f3189a16f034da
1 /*
2 * Stanford Enetfilter subroutines for tcpdump
4 * Based on the MERIT NNstat etherifrt.c and the Ultrix pcap-pf.c
5 * subroutines.
7 * Rayan Zachariassen, CA*Net
8 */
9 #ifndef lint
10 static const char rcsid[] _U_ =
11 "@(#) $Header: /tcpdump/master/libpcap/pcap-enet.c,v 1.9 2006-10-04 18:09:22 guy Exp $";
12 #endif
14 #ifdef HAVE_CONFIG_H
15 #include "config.h"
16 #endif
18 #include <sys/types.h>
19 #include <sys/time.h>
20 #include <sys/file.h>
21 #include <sys/ioctl.h>
22 #include <sys/socket.h>
24 #include <net/if.h>
25 #include <pcap/bpf.h>
26 #include <net/enet.h>
28 #include <netinet/in.h>
29 #include <netinet/if_ether.h>
31 #include <stdio.h>
32 #include <errno.h>
34 #include "interface.h"
36 struct packet_header {
37 #ifdef IBMRTPC
38 struct LengthWords length;
39 struct tap_header tap;
40 #endif /* IBMRTPC */
41 u_char packet[8]
44 extern int errno;
46 #define BUFSPACE (4*1024)
48 /* Forwards */
49 static void efReadError(int, char *);
51 void
52 readloop(int cnt, int if_fd, struct bpf_program *fp, printfunc printit)
54 #ifdef IBMRTPC
55 register struct packet_header *ph;
56 register u_char *bp;
57 register int inc;
58 #else /* !IBMRTPC */
59 static struct timeval tv = { 0 };
60 #endif /* IBMRTPC */
61 register int cc, caplen;
62 register struct bpf_insn *fcode = fp->bf_insns;
63 union {
64 struct packet_header hdr;
65 u_char p[BUFSPACE];
66 u_short s;
67 } buf;
69 while (1) {
70 if ((cc = read(if_fd, (char *)buf.p, sizeof(buf))) < 0)
71 efReadError(if_fd, "reader");
73 #ifdef IBMRTPC
75 * Loop through each packet.
77 bp = buf.p;
78 while (cc > 0) {
79 ph = (struct packet_header *)bp;
80 caplen = ph->tap.th_wirelen > snaplen ? snaplen : ph->tap
81 .th_wirelen ;
82 if (bpf_filter(fcode, (char *)ph->packet,
83 ph->tap.th_wirelen, caplen)) {
84 if (cnt >= 0 && --cnt < 0)
85 goto out;
86 (*printit)((char *)ph->packet,
87 (struct timeval *)ph->tap.th_timestamp,
88 ph->tap.th_wirelen, caplen);
90 inc = ph->length.PacketOffset;
91 cc -= inc;
92 bp += inc;
94 #else /* !IBMRTPC */
95 caplen = cc > snaplen ? snaplen : cc ;
96 if (bpf_filter(fcode, buf.hdr.packet, cc, caplen)) {
97 if (cnt >= 0 && --cnt < 0)
98 goto out;
99 (*printit)(buf.hdr.packet, &tv, cc, caplen);
101 #endif /* IBMRTPC */
103 out:
104 wrapup(if_fd);
107 /* Call ONLY if read() has returned an error on packet filter */
108 static void
109 efReadError(int fid, char *msg)
111 if (errno == EINVAL) { /* read MAXINT bytes already! */
112 if (lseek(fid, 0, 0) < 0) {
113 perror("tcpdump: efReadError/lseek");
114 exit(-1);
116 else
117 return;
119 else {
120 (void) fprintf(stderr, "tcpdump: ");
121 perror(msg);
122 exit(-1);
126 void
127 wrapup(int fd)
129 #ifdef IBMRTPC
130 struct enstats es;
132 if (ioctl(fd, EIOSTATS, &es) == -1) {
133 perror("tcpdump: enet ioctl EIOSTATS error");
134 exit(-1);
137 fprintf(stderr, "%d packets queued", es.enStat_Rcnt);
138 if (es.enStat_Rdrops > 0)
139 fprintf(stderr, ", %d dropped", es.enStat_Rdrops);
140 if (es.enStat_Reads > 0)
141 fprintf(stderr, ", %d tcpdump %s", es.enStat_Reads,
142 es.enStat_Reads > 1 ? "reads" : "read");
143 if (es.enStat_MaxRead > 1)
144 fprintf(stderr, ", %d packets in largest read",
145 es.enStat_MaxRead);
146 putc('\n', stderr);
147 #endif /* IBMRTPC */
148 close(fd);
152 initdevice(char *device, int pflag, int *linktype)
154 struct eniocb ctl;
155 struct enfilter filter;
156 u_int maxwaiting;
157 int if_fd;
159 #ifdef IBMRTPC
160 GETENETDEVICE(0, O_RDONLY, &if_fd);
161 #else /* !IBMRTPC */
162 if_fd = open("/dev/enet", O_RDONLY, 0);
163 #endif /* IBMRTPC */
165 if (if_fd == -1) {
166 perror("tcpdump: enet open error");
167 error(
168 "your system may not be properly configured; see \"man enet(4)\"");
169 exit(-1);
172 /* Get operating parameters. */
174 if (ioctl(if_fd, EIOCGETP, (char *)&ctl) == -1) {
175 perror("tcpdump: enet ioctl EIOCGETP error");
176 exit(-1);
179 /* Set operating parameters. */
181 #ifdef IBMRTPC
182 ctl.en_rtout = 1 * ctl.en_hz;
183 ctl.en_tr_etherhead = 1;
184 ctl.en_tap_network = 1;
185 ctl.en_multi_packet = 1;
186 ctl.en_maxlen = BUFSPACE;
187 #else /* !IBMRTPC */
188 ctl.en_rtout = 64; /* randomly picked value for HZ */
189 #endif /* IBMRTPC */
190 if (ioctl(if_fd, EIOCSETP, &ctl) == -1) {
191 perror("tcpdump: enet ioctl EIOCSETP error");
192 exit(-1);
195 /* Flush the receive queue, since we've changed
196 the operating parameters and we otherwise might
197 receive data without headers. */
199 if (ioctl(if_fd, EIOCFLUSH) == -1) {
200 perror("tcpdump: enet ioctl EIOCFLUSH error");
201 exit(-1);
204 /* Set the receive queue depth to its maximum. */
206 maxwaiting = ctl.en_maxwaiting;
207 if (ioctl(if_fd, EIOCSETW, &maxwaiting) == -1) {
208 perror("tcpdump: enet ioctl EIOCSETW error");
209 exit(-1);
212 #ifdef IBMRTPC
213 /* Clear statistics. */
215 if (ioctl(if_fd, EIOCLRSTAT, 0) == -1) {
216 perror("tcpdump: enet ioctl EIOCLRSTAT error");
217 exit(-1);
219 #endif /* IBMRTPC */
221 /* Set the filter (accept all packets). */
223 filter.enf_Priority = 3;
224 filter.enf_FilterLen = 0;
225 if (ioctl(if_fd, EIOCSETF, &filter) == -1) {
226 perror("tcpdump: enet ioctl EIOCSETF error");
227 exit(-1);
230 * "enetfilter" supports only ethernets.
232 *linktype = DLT_EN10MB;
234 return(if_fd);