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