]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/ipfilter/ipsend/sbpf.c
This commit was generated by cvs2svn to compensate for changes in r50760,
[FreeBSD/FreeBSD.git] / contrib / ipfilter / ipsend / sbpf.c
1 /*
2  * (C)opyright 1995-1997 Darren Reed. (from tcplog)
3  *
4  * Redistribution and use in source and binary forms are permitted
5  * provided that this notice is preserved and due credit is given
6  * to the original author and the contributors.
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 #include <sys/dir.h>
27 #endif
28 #include <net/bpf.h>
29
30 #include <net/if.h>
31 #include <netinet/in.h>
32 #include <netinet/in_systm.h>
33 #include <netinet/ip.h>
34 #include <netinet/ip_var.h>
35 #include <netinet/udp.h>
36 #include <netinet/udp_var.h>
37 #include <netinet/tcp.h>
38 #include "ipsend.h"
39
40 #if !defined(lint)
41 static const char sccsid[] = "@(#)sbpf.c        1.3 8/25/95 (C)1995 Darren Reed";
42 static const char rcsid[] = "@(#)$Id: sbpf.c,v 2.0.2.7 1997/10/23 11:42:47 darrenr Exp $";
43 #endif
44
45 /*
46  * the code herein is dervied from libpcap.
47  */
48 static  u_char  *buf = NULL;
49 static  int     bufsize = 0, timeout = 1;
50
51
52 int     initdevice(device, sport, tout)
53 char    *device;
54 int     sport, tout;
55 {
56         struct  bpf_version bv;
57         struct  timeval to;
58         struct  ifreq ifr;
59         char    bpfname[16];
60         int     fd, i;
61
62         for (i = 0; i < 16; i++)
63             {
64                 (void) sprintf(bpfname, "/dev/bpf%d", i);
65                 if ((fd = open(bpfname, O_RDWR)) >= 0)
66                         break;
67             }
68         if (i == 16)
69             {
70                 fprintf(stderr, "no bpf devices available as /dev/bpfxx\n");
71                 return -1;
72             }
73
74         if (ioctl(fd, BIOCVERSION, (caddr_t)&bv) < 0)
75             {
76                 perror("BIOCVERSION");
77                 return -1;
78             }
79         if (bv.bv_major != BPF_MAJOR_VERSION ||
80             bv.bv_minor < BPF_MINOR_VERSION)
81             {
82                 fprintf(stderr, "kernel bpf (v%d.%d) filter out of date:\n",
83                         bv.bv_major, bv.bv_minor);
84                 fprintf(stderr, "current version: %d.%d\n",
85                         BPF_MAJOR_VERSION, BPF_MINOR_VERSION);
86                 return -1;
87             }
88
89         (void) strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
90         if (ioctl(fd, BIOCSETIF, &ifr) == -1)
91             {
92                 fprintf(stderr, "%s(%d):", ifr.ifr_name, fd);
93                 perror("BIOCSETIF");
94                 exit(1);
95             }
96         /*
97          * get kernel buffer size
98          */
99         if (ioctl(fd, BIOCGBLEN, &bufsize) == -1)
100             {
101                 perror("BIOCSBLEN");
102                 exit(-1);
103             }
104         buf = (u_char*)malloc(bufsize);
105         /*
106          * set the timeout
107          */
108         timeout = tout;
109         to.tv_sec = 1;
110         to.tv_usec = 0;
111         if (ioctl(fd, BIOCSRTIMEOUT, (caddr_t)&to) == -1)
112             {
113                 perror("BIOCSRTIMEOUT");
114                 exit(-1);
115             }
116
117         (void) ioctl(fd, BIOCFLUSH, 0);
118         return fd;
119 }
120
121
122 /*
123  * output an IP packet onto a fd opened for /dev/bpf
124  */
125 int     sendip(fd, pkt, len)
126 int     fd, len;
127 char    *pkt;
128 {                       
129         if (write(fd, pkt, len) == -1)
130             {
131                 perror("send");
132                 return -1;
133             }
134
135         return len;
136 }