Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / ipf / ipsend / sbpf.c
blob7e30369d359541533e113c9242e2d6e5b82c8157
1 /* $NetBSD$ */
3 /*
4 * (C)opyright 1995-1998 Darren Reed. (from tcplog)
6 * See the IPFILTER.LICENCE file for details on licencing.
8 */
9 #include <sys/param.h>
10 #include <sys/types.h>
11 #include <sys/mbuf.h>
12 #include <sys/time.h>
13 #include <sys/timeb.h>
14 #include <sys/socket.h>
15 #include <sys/file.h>
16 #include <sys/ioctl.h>
17 #if BSD < 199103
18 #include <sys/fcntlcom.h>
19 #endif
20 #if (__FreeBSD_version >= 300000)
21 # include <sys/dirent.h>
22 #else
23 # include <sys/dir.h>
24 #endif
25 #include <net/bpf.h>
27 #include <net/if.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>
36 #include <stdio.h>
37 #include <netdb.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <stdlib.h>
41 #ifdef __NetBSD__
42 # include <paths.h>
43 #endif
44 #include <ctype.h>
45 #include <signal.h>
46 #include <errno.h>
48 #include "ipsend.h"
50 #if !defined(lint)
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";
53 #endif
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)
63 char *device;
64 int tout;
66 struct bpf_version bv;
67 struct timeval to;
68 struct ifreq ifr;
69 #ifdef _PATH_BPF
70 char *bpfname = _PATH_BPF;
71 int fd;
73 if ((fd = open(bpfname, O_RDWR)) < 0)
75 fprintf(stderr, "no bpf devices available as /dev/bpfxx\n");
76 return -1;
78 #else
79 char bpfname[16];
80 int fd = 0, i;
82 for (i = 0; i < 16; i++)
84 (void) sprintf(bpfname, "/dev/bpf%d", i);
85 if ((fd = open(bpfname, O_RDWR)) >= 0)
86 break;
88 if (i == 16)
90 fprintf(stderr, "no bpf devices available as /dev/bpfxx\n");
91 return -1;
93 #endif
95 if (ioctl(fd, BIOCVERSION, (caddr_t)&bv) < 0)
97 perror("BIOCVERSION");
98 return -1;
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);
107 return -1;
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);
114 perror("BIOCSETIF");
115 exit(1);
118 * get kernel buffer size
120 if (ioctl(fd, BIOCGBLEN, &bufsize) == -1)
122 perror("BIOCSBLEN");
123 exit(-1);
125 buf = (u_char*)malloc(bufsize);
127 * set the timeout
129 timeout = tout;
130 to.tv_sec = 1;
131 to.tv_usec = 0;
132 if (ioctl(fd, BIOCSRTIMEOUT, (caddr_t)&to) == -1)
134 perror("BIOCSRTIMEOUT");
135 exit(-1);
138 (void) ioctl(fd, BIOCFLUSH, 0);
139 return fd;
144 * output an IP packet onto a fd opened for /dev/bpf
146 int sendip(fd, pkt, len)
147 int fd, len;
148 char *pkt;
150 if (write(fd, pkt, len) == -1)
152 perror("send");
153 return -1;
156 return len;