Initial import from FreeBSD RELENG_4:
[games.git] / contrib / ipfilter / ipsend / sbpf.c
1 /* $FreeBSD: src/contrib/ipfilter/ipsend/sbpf.c,v 1.3.2.2 2002/04/27 17:30:57 darrenr Exp $ */
2 /*
3  * (C)opyright 1995-1998 Darren Reed. (from tcplog)
4  *
5  * See the IPFILTER.LICENCE file for details on licencing.
6  */
7 #include <stdio.h>
8 #include <netdb.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <stdlib.h>
12 #include <ctype.h>
13 #include <signal.h>
14 #include <errno.h>
15 #include <sys/types.h>
16 #include <sys/param.h>
17 #include <sys/mbuf.h>
18 #include <sys/time.h>
19 #include <sys/timeb.h>
20 #include <sys/socket.h>
21 #include <sys/file.h>
22 #include <sys/ioctl.h>
23 #if BSD < 199103
24 #include <sys/fcntlcom.h>
25 #endif
26 #if (__FreeBSD_version >= 300000)
27 # include <sys/dirent.h>
28 #else
29 # include <sys/dir.h>
30 #endif
31 #include <net/bpf.h>
32
33 #include <net/if.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>
41 #include "ipsend.h"
42
43 #if !defined(lint)
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 $";
46 #endif
47
48 /*
49  * the code herein is dervied from libpcap.
50  */
51 static  u_char  *buf = NULL;
52 static  int     bufsize = 0, timeout = 1;
53
54
55 int     initdevice(device, sport, tout)
56 char    *device;
57 int     sport, tout;
58 {
59         struct  bpf_version bv;
60         struct  timeval to;
61         struct  ifreq ifr;
62         char    bpfname[16];
63         int     fd, i;
64
65         fd = -1;
66
67         for (i = 0; i < 16; i++)
68             {
69                 (void) sprintf(bpfname, "/dev/bpf%d", i);
70                 if ((fd = open(bpfname, O_RDWR)) >= 0)
71                         break;
72             }
73         if (i == 16)
74             {
75                 fprintf(stderr, "no bpf devices available as /dev/bpfxx\n");
76                 return -1;
77             }
78
79         if (ioctl(fd, BIOCVERSION, (caddr_t)&bv) < 0)
80             {
81                 perror("BIOCVERSION");
82                 return -1;
83             }
84         if (bv.bv_major != BPF_MAJOR_VERSION ||
85             bv.bv_minor < BPF_MINOR_VERSION)
86             {
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);
91                 return -1;
92             }
93
94         (void) strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
95         if (ioctl(fd, BIOCSETIF, &ifr) == -1)
96             {
97                 fprintf(stderr, "%s(%d):", ifr.ifr_name, fd);
98                 perror("BIOCSETIF");
99                 exit(1);
100             }
101         /*
102          * get kernel buffer size
103          */
104         if (ioctl(fd, BIOCGBLEN, &bufsize) == -1)
105             {
106                 perror("BIOCSBLEN");
107                 exit(-1);
108             }
109         buf = (u_char*)malloc(bufsize);
110         /*
111          * set the timeout
112          */
113         timeout = tout;
114         to.tv_sec = 1;
115         to.tv_usec = 0;
116         if (ioctl(fd, BIOCSRTIMEOUT, (caddr_t)&to) == -1)
117             {
118                 perror("BIOCSRTIMEOUT");
119                 exit(-1);
120             }
121
122         (void) ioctl(fd, BIOCFLUSH, 0);
123         return fd;
124 }
125
126
127 /*
128  * output an IP packet onto a fd opened for /dev/bpf
129  */
130 int     sendip(fd, pkt, len)
131 int     fd, len;
132 char    *pkt;
133 {                       
134         if (write(fd, pkt, len) == -1)
135             {
136                 perror("send");
137                 return -1;
138             }
139
140         return len;
141 }