3 * (C)opyright 1995-1998 Darren Reed. (from tcplog)
5 * See the IPFILTER.LICENCE file for details on licencing.
12 #include <sys/timeb.h>
13 #include <sys/socket.h>
15 #include <sys/ioctl.h>
17 #include <sys/fcntlcom.h>
19 #if (__FreeBSD_version >= 300000)
20 # include <sys/dirent.h>
27 #include <netinet/in.h>
28 #include <netinet/in_systm.h>
29 #include <netinet/ip.h>
30 #include <netinet/ip_var.h>
47 static const char sccsid[] = "@(#)sbpf.c 1.3 8/25/95 (C)1995 Darren Reed";
48 static const char rcsid[] = "@(#)$Id: sbpf.c,v 2.5.4.1 2006/03/21 16:32:58 darrenr Exp $";
52 * the code herein is dervied from libpcap.
54 static u_char *buf = NULL;
55 static int bufsize = 0, timeout = 1;
58 int initdevice(device, tout)
62 struct bpf_version bv;
66 char *bpfname = _PATH_BPF;
69 if ((fd = open(bpfname, O_RDWR)) < 0)
71 fprintf(stderr, "no bpf devices available as /dev/bpfxx\n");
78 for (i = 0; i < 16; i++)
80 (void) sprintf(bpfname, "/dev/bpf%d", i);
81 if ((fd = open(bpfname, O_RDWR)) >= 0)
86 fprintf(stderr, "no bpf devices available as /dev/bpfxx\n");
91 if (ioctl(fd, BIOCVERSION, (caddr_t)&bv) < 0)
93 perror("BIOCVERSION");
96 if (bv.bv_major != BPF_MAJOR_VERSION ||
97 bv.bv_minor < BPF_MINOR_VERSION)
99 fprintf(stderr, "kernel bpf (v%d.%d) filter out of date:\n",
100 bv.bv_major, bv.bv_minor);
101 fprintf(stderr, "current version: %d.%d\n",
102 BPF_MAJOR_VERSION, BPF_MINOR_VERSION);
106 (void) strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
107 if (ioctl(fd, BIOCSETIF, &ifr) == -1)
109 fprintf(stderr, "%s(%d):", ifr.ifr_name, fd);
114 * get kernel buffer size
116 if (ioctl(fd, BIOCGBLEN, &bufsize) == -1)
121 buf = (u_char*)malloc(bufsize);
128 if (ioctl(fd, BIOCSRTIMEOUT, (caddr_t)&to) == -1)
130 perror("BIOCSRTIMEOUT");
134 (void) ioctl(fd, BIOCFLUSH, 0);
140 * output an IP packet onto a fd opened for /dev/bpf
142 int sendip(fd, pkt, len)
146 if (write(fd, pkt, len) == -1)