4 * (C)opyright 1995-1998 Darren Reed. (from tcplog)
6 * See the IPFILTER.LICENCE file for details on licencing.
10 #include <sys/types.h>
13 #include <sys/timeb.h>
14 #include <sys/socket.h>
16 #include <sys/ioctl.h>
18 #include <sys/fcntlcom.h>
20 #if (__FreeBSD_version >= 300000)
21 # include <sys/dirent.h>
28 #include <netinet/in.h>
29 #include <netinet/in_systm.h>
30 #include <netinet/ip.h>
31 #include <netinet/ip_var.h>
32 #include <netinet/udp.h>
33 #include <netinet/udp_var.h>
34 #include <netinet/tcp.h>
48 static const char sccsid[] = "@(#)sbpf.c 1.3 8/25/95 (C)1995 Darren Reed";
49 static const char rcsid[] = "@(#)Id: sbpf.c,v 2.5 2002/02/24 07:30:03 darrenr Exp";
53 * the code herein is dervied from libpcap.
55 static u_char *buf = NULL;
56 static int bufsize = 0, timeout = 1;
59 int initdevice(device, tout)
63 struct bpf_version bv;
69 for (i = 0; i < 16; i++)
71 (void) sprintf(bpfname, "/dev/bpf%d", i);
72 if ((fd = open(bpfname, O_RDWR)) >= 0)
77 fprintf(stderr, "no bpf devices available as /dev/bpfxx\n");
81 if (ioctl(fd, BIOCVERSION, (caddr_t)&bv) < 0)
83 perror("BIOCVERSION");
86 if (bv.bv_major != BPF_MAJOR_VERSION ||
87 bv.bv_minor < BPF_MINOR_VERSION)
89 fprintf(stderr, "kernel bpf (v%d.%d) filter out of date:\n",
90 bv.bv_major, bv.bv_minor);
91 fprintf(stderr, "current version: %d.%d\n",
92 BPF_MAJOR_VERSION, BPF_MINOR_VERSION);
96 (void) strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
97 if (ioctl(fd, BIOCSETIF, &ifr) == -1)
99 fprintf(stderr, "%s(%d):", ifr.ifr_name, fd);
104 * get kernel buffer size
106 if (ioctl(fd, BIOCGBLEN, &bufsize) == -1)
111 buf = (u_char*)malloc(bufsize);
118 if (ioctl(fd, BIOCSRTIMEOUT, (caddr_t)&to) == -1)
120 perror("BIOCSRTIMEOUT");
124 (void) ioctl(fd, BIOCFLUSH, 0);
130 * output an IP packet onto a fd opened for /dev/bpf
132 int sendip(fd, pkt, len)
136 if (write(fd, pkt, len) == -1)