]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/ipfilter/ipsend/sdlpi.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / ipfilter / ipsend / sdlpi.c
1 /*      $FreeBSD$       */
2
3 /*
4  * (C)opyright 1992-1998 Darren Reed. (from tcplog)
5  *
6  * See the IPFILTER.LICENCE file for details on licencing.
7  *
8  */
9
10 #include <stdio.h>
11 #include <netdb.h>
12 #include <ctype.h>
13 #include <fcntl.h>
14 #include <signal.h>
15 #include <errno.h>
16 #include <sys/types.h>
17 #include <sys/time.h>
18 #include <sys/timeb.h>
19 #include <sys/socket.h>
20 #include <sys/file.h>
21 #include <sys/ioctl.h>
22 #include <sys/stropts.h>
23
24 #ifdef sun
25 # include <sys/pfmod.h>
26 # include <sys/bufmod.h>
27 #endif
28 #ifdef __osf__
29 # include <sys/dlpihdr.h>
30 # include "radix_ipf_local.h"
31 #else
32 # include <sys/dlpi.h>
33 #endif
34 #ifdef __hpux
35 # include <sys/dlpi_ext.h>
36 #endif
37
38 #include <net/if.h>
39 #include <netinet/in.h>
40 #include <netinet/in_systm.h>
41 #include <netinet/ip.h>
42 #include <netinet/if_ether.h>
43 #include <netinet/ip_var.h>
44 #include <netinet/udp.h>
45 #include <netinet/udp_var.h>
46 #include <netinet/tcp.h>
47
48 #include "ipsend.h"
49
50 #if !defined(lint)
51 static const char sccsid[] = "@(#)sdlpi.c       1.3 10/30/95 (C)1995 Darren Reed";
52 static const char rcsid[] = "@(#)$Id: sdlpi.c,v 2.8.2.2 2007/02/17 12:41:51 darrenr Exp $";
53 #endif
54
55 #define CHUNKSIZE       8192
56 #define BUFSPACE        (4*CHUNKSIZE)
57
58
59 /*
60  * Be careful to only include those defined in the flags option for the
61  * interface are included in the header size.
62  */
63 int     initdevice(device, tout)
64 char    *device;
65 int     tout;
66 {
67         char    devname[16], *s, buf[256];
68         int     i, fd;
69
70         (void) strcpy(devname, "/dev/");
71         (void) strncat(devname, device, sizeof(devname) - strlen(devname));
72
73         s = devname + 5;
74         while (*s && !ISDIGIT(*s))
75                 s++;
76         if (!*s)
77             {
78                 fprintf(stderr, "bad device name %s\n", devname);
79                 exit(-1);
80             }
81         i = atoi(s);
82         *s = '\0';
83         /*
84          * For writing
85          */
86         if ((fd = open(devname, O_RDWR)) < 0)
87             {
88                 fprintf(stderr, "O_RDWR(1) ");
89                 perror(devname);
90                 exit(-1);
91             }
92
93         if (dlattachreq(fd, i) == -1)
94             {
95                 fprintf(stderr, "dlattachreq: DLPI error\n");
96                 exit(-1);
97             }
98         else if (dlokack(fd, buf) == -1)
99             {
100                 fprintf(stderr, "dlokack(attach): DLPI error\n");
101                 exit(-1);
102             }
103 #ifdef DL_HP_RAWDLS
104         if (dlpromisconreq(fd, DL_PROMISC_SAP) < 0)
105             {
106                 fprintf(stderr, "dlpromisconreq: DL_PROMISC_PHYS error\n");
107                 exit(-1);
108             }
109         else if (dlokack(fd, buf) < 0)
110             {
111                 fprintf(stderr, "dlokack(promisc): DLPI error\n");
112                 exit(-1);
113             }
114         /* 22 is INSAP as per the HP-UX DLPI Programmer's Guide */
115
116         dlbindreq(fd, 22, 1, DL_HP_RAWDLS, 0, 0);
117 #else
118         dlbindreq(fd, ETHERTYPE_IP, 0, DL_CLDLS, 0, 0);
119 #endif
120         dlbindack(fd, buf);
121         /*
122          * write full headers
123          */
124 #ifdef DLIOCRAW /* we require RAW DLPI mode, which is a Sun extension */
125         if (strioctl(fd, DLIOCRAW, -1, 0, NULL) == -1)
126             {
127                 fprintf(stderr, "DLIOCRAW error\n");
128                 exit(-1);
129             }
130 #endif
131         return fd;
132 }
133
134
135 /*
136  * output an IP packet onto a fd opened for /dev/nit
137  */
138 int     sendip(fd, pkt, len)
139 int     fd, len;
140 char    *pkt;
141 {                       
142         struct strbuf dbuf, *dp = &dbuf, *cp = NULL;
143         int pri = 0;
144 #ifdef DL_HP_RAWDLS
145         struct strbuf cbuf;
146         dl_hp_rawdata_req_t raw;
147
148         cp = &cbuf;
149         raw.dl_primitive = DL_HP_RAWDATA_REQ;
150         cp->len = sizeof(raw);
151         cp->buf = (char *)&raw;
152         cp->maxlen = cp->len;
153         pri = MSG_HIPRI;
154 #endif
155         /*
156          * construct NIT STREAMS messages, first control then data.
157          */
158         dp->buf = pkt;
159         dp->len = len;
160         dp->maxlen = dp->len;
161
162         if (putmsg(fd, cp, dp, pri) == -1)
163             {
164                 perror("putmsg");
165                 return -1;
166             }
167         if (ioctl(fd, I_FLUSH, FLUSHW) == -1)
168             {
169                 perror("I_FLUSHW");
170                 return -1;
171             }
172         return len;
173 }
174