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