Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / ipfilter / 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 #include <stdio.h>
8 #include <netdb.h>
9 #include <ctype.h>
10 #include <fcntl.h>
11 #include <signal.h>
12 #include <errno.h>
13 #include <sys/types.h>
14 #include <sys/time.h>
15 #include <sys/timeb.h>
16 #include <sys/socket.h>
17 #include <sys/file.h>
18 #include <sys/ioctl.h>
19 #include <sys/stropts.h>
20
21 #ifdef sun
22 #include <sys/pfmod.h>
23 #include <sys/bufmod.h>
24 #endif
25 #include <sys/dlpi.h>
26
27 #include <net/if.h>
28 #include <netinet/in.h>
29 #include <netinet/in_systm.h>
30 #include <netinet/ip.h>
31 #include <netinet/if_ether.h>
32 #include <netinet/ip_var.h>
33 #include <netinet/udp.h>
34 #include <netinet/udp_var.h>
35 #include <netinet/tcp.h>
36
37 #include "ipsend.h"
38
39 #if !defined(lint)
40 static const char sccsid[] = "@(#)sdlpi.c       1.3 10/30/95 (C)1995 Darren Reed";
41 static const char rcsid[] = "@(#)$Id: sdlpi.c,v 2.1.4.2 2001/06/26 10:43:22 darrenr Exp $";
42 #endif
43
44 #define CHUNKSIZE       8192
45 #define BUFSPACE        (4*CHUNKSIZE)
46
47
48 /*
49  * Be careful to only include those defined in the flags option for the
50  * interface are included in the header size.
51  */
52 int     initdevice(device, sport, tout)
53 char    *device;
54 int     sport, tout;
55 {
56         char    devname[16], *s, buf[256];
57         int     i, fd;
58
59         (void) strcpy(devname, "/dev/");
60         (void) strncat(devname, device, sizeof(devname) - strlen(devname));
61
62         s = devname + 5;
63         while (*s && !isdigit(*s))
64                 s++;
65         if (!*s)
66             {
67                 fprintf(stderr, "bad device name %s\n", devname);
68                 exit(-1);
69             }
70         i = atoi(s);
71         *s = '\0';
72         /*
73          * For writing
74          */
75         if ((fd = open(devname, O_RDWR)) < 0)
76             {
77                 fprintf(stderr, "O_RDWR(1) ");
78                 perror(devname);
79                 exit(-1);
80             }
81
82         if (dlattachreq(fd, i) == -1 || dlokack(fd, buf) == -1)
83             {
84                 fprintf(stderr, "DLPI error\n");
85                 exit(-1);
86             }
87         dlbindreq(fd, ETHERTYPE_IP, 0, DL_CLDLS, 0, 0);
88         dlbindack(fd, buf);
89         /*
90          * write full headers
91          */
92 #ifdef sun /* we require RAW DLPI mode, which is a Sun extension */
93         if (strioctl(fd, DLIOCRAW, -1, 0, NULL) == -1)
94             {
95                 fprintf(stderr, "DLIOCRAW error\n");
96                 exit(-1);
97             }
98 #else
99 you lose
100 #endif
101         return fd;
102 }
103
104
105 /*
106  * output an IP packet onto a fd opened for /dev/nit
107  */
108 int     sendip(fd, pkt, len)
109 int     fd, len;
110 char    *pkt;
111 {                       
112         struct  strbuf  dbuf, *dp = &dbuf;
113
114         /*
115          * construct NIT STREAMS messages, first control then data.
116          */
117         dp->buf = pkt;
118         dp->len = len;
119         dp->maxlen = dp->len;
120
121         if (putmsg(fd, NULL, dp, 0) == -1)
122             {
123                 perror("putmsg");
124                 return -1;
125             }
126         if (ioctl(fd, I_FLUSH, FLUSHW) == -1)
127             {
128                 perror("I_FLUSHW");
129                 return -1;
130             }
131         return len;
132 }