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>
51 static const char sccsid
[] = "@(#)sbpf.c 1.3 8/25/95 (C)1995 Darren Reed";
52 static const char rcsid
[] = "@(#)Id: sbpf.c,v 2.5.4.1 2006/03/21 16:32:58 darrenr Exp";
56 * the code herein is dervied from libpcap.
58 static u_char
*buf
= NULL
;
59 static int bufsize
= 0, timeout
= 1;
62 int initdevice(device
, tout
)
66 struct bpf_version bv
;
70 char *bpfname
= _PATH_BPF
;
73 if ((fd
= open(bpfname
, O_RDWR
)) < 0)
75 fprintf(stderr
, "no bpf devices available as /dev/bpfxx\n");
82 for (i
= 0; i
< 16; i
++)
84 (void) sprintf(bpfname
, "/dev/bpf%d", i
);
85 if ((fd
= open(bpfname
, O_RDWR
)) >= 0)
90 fprintf(stderr
, "no bpf devices available as /dev/bpfxx\n");
95 if (ioctl(fd
, BIOCVERSION
, (caddr_t
)&bv
) < 0)
97 perror("BIOCVERSION");
100 if (bv
.bv_major
!= BPF_MAJOR_VERSION
||
101 bv
.bv_minor
< BPF_MINOR_VERSION
)
103 fprintf(stderr
, "kernel bpf (v%d.%d) filter out of date:\n",
104 bv
.bv_major
, bv
.bv_minor
);
105 fprintf(stderr
, "current version: %d.%d\n",
106 BPF_MAJOR_VERSION
, BPF_MINOR_VERSION
);
110 (void) strncpy(ifr
.ifr_name
, device
, sizeof(ifr
.ifr_name
));
111 if (ioctl(fd
, BIOCSETIF
, &ifr
) == -1)
113 fprintf(stderr
, "%s(%d):", ifr
.ifr_name
, fd
);
118 * get kernel buffer size
120 if (ioctl(fd
, BIOCGBLEN
, &bufsize
) == -1)
125 buf
= (u_char
*)malloc(bufsize
);
132 if (ioctl(fd
, BIOCSRTIMEOUT
, (caddr_t
)&to
) == -1)
134 perror("BIOCSRTIMEOUT");
138 (void) ioctl(fd
, BIOCFLUSH
, 0);
144 * output an IP packet onto a fd opened for /dev/bpf
146 int sendip(fd
, pkt
, len
)
150 if (write(fd
, pkt
, len
) == -1)