3 * (C)opyright 1995-1998 Darren Reed. (from tcplog)
5 * See the IPFILTER.LICENCE file for details on licencing.
15 #include <sys/types.h>
16 #include <sys/param.h>
19 #include <sys/timeb.h>
20 #include <sys/socket.h>
22 #include <sys/ioctl.h>
24 #include <sys/fcntlcom.h>
26 #if (__FreeBSD_version >= 300000)
27 # include <sys/dirent.h>
34 #include <netinet/in.h>
35 #include <netinet/in_systm.h>
36 #include <netinet/ip.h>
37 #include <netinet/ip_var.h>
38 #include <netinet/udp.h>
39 #include <netinet/udp_var.h>
40 #include <netinet/tcp.h>
44 static const char sccsid[] = "@(#)sbpf.c 1.3 8/25/95 (C)1995 Darren Reed";
45 static const char rcsid[] = "@(#)$Id: sbpf.c,v 2.1.4.2 2001/09/30 04:04:28 darrenr Exp $";
49 * the code herein is dervied from libpcap.
51 static u_char *buf = NULL;
52 static int bufsize = 0, timeout = 1;
55 int initdevice(device, sport, tout)
59 struct bpf_version bv;
67 for (i = 0; i < 16; i++)
69 (void) sprintf(bpfname, "/dev/bpf%d", i);
70 if ((fd = open(bpfname, O_RDWR)) >= 0)
75 fprintf(stderr, "no bpf devices available as /dev/bpfxx\n");
79 if (ioctl(fd, BIOCVERSION, (caddr_t)&bv) < 0)
81 perror("BIOCVERSION");
84 if (bv.bv_major != BPF_MAJOR_VERSION ||
85 bv.bv_minor < BPF_MINOR_VERSION)
87 fprintf(stderr, "kernel bpf (v%d.%d) filter out of date:\n",
88 bv.bv_major, bv.bv_minor);
89 fprintf(stderr, "current version: %d.%d\n",
90 BPF_MAJOR_VERSION, BPF_MINOR_VERSION);
94 (void) strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
95 if (ioctl(fd, BIOCSETIF, &ifr) == -1)
97 fprintf(stderr, "%s(%d):", ifr.ifr_name, fd);
102 * get kernel buffer size
104 if (ioctl(fd, BIOCGBLEN, &bufsize) == -1)
109 buf = (u_char*)malloc(bufsize);
116 if (ioctl(fd, BIOCSRTIMEOUT, (caddr_t)&to) == -1)
118 perror("BIOCSRTIMEOUT");
122 (void) ioctl(fd, BIOCFLUSH, 0);
128 * output an IP packet onto a fd opened for /dev/bpf
130 int sendip(fd, pkt, len)
134 if (write(fd, pkt, len) == -1)