sdlpi.c revision 24583
1/*
2 * (C)opyright October 1992 Darren Reed. (from tcplog)
3 *
4 *   This software may be freely distributed as long as it is not altered
5 * in any way and that this messagge always accompanies it.
6 *
7 *   The author of this software makes no garuntee about the
8 * performance of this package or its suitability to fulfill any purpose.
9 *
10 */
11
12#include <stdio.h>
13#include <netdb.h>
14#include <ctype.h>
15#include <fcntl.h>
16#include <signal.h>
17#include <errno.h>
18#include <sys/types.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#include <sys/stropts.h>
25
26#include <sys/pfmod.h>
27#include <sys/bufmod.h>
28#include <sys/dlpi.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/if_ether.h>
35#include <netinet/ip_var.h>
36#include <netinet/udp.h>
37#include <netinet/udp_var.h>
38#include <netinet/tcp.h>
39
40#include "ipsend.h"
41
42#if !defined(lint) && defined(LIBC_SCCS)
43static	char	snitid[] = "@(#)sdlpi.c	1.3 10/30/95 (C)1995 Darren Reed";
44#endif
45
46#define	CHUNKSIZE	8192
47#define BUFSPACE	(4*CHUNKSIZE)
48
49
50/*
51 * Be careful to only include those defined in the flags option for the
52 * interface are included in the header size.
53 */
54int	initdevice(device, sport, tout)
55char	*device;
56int	sport, tout;
57{
58	char	devname[16], *s, buf[256];
59	int	i, fd;
60
61	(void) sprintf(devname, "/dev/%s", device);
62
63	s = devname + 5;
64	while (*s && !isdigit(*s))
65		s++;
66	if (!*s)
67	    {
68		fprintf(stderr, "bad device name %s\n", devname);
69		exit(-1);
70	    }
71	i = atoi(s);
72	*s = '\0';
73	/*
74	 * For writing
75	 */
76	if ((fd = open(devname, O_RDWR)) < 0)
77	    {
78		fprintf(stderr, "O_RDWR(1) ");
79		perror(devname);
80		exit(-1);
81	    }
82
83	if (dlattachreq(fd, i) == -1 || dlokack(fd, buf) == -1)
84	    {
85		fprintf(stderr, "DLPI error\n");
86		exit(-1);
87	    }
88	dlbindreq(fd, ETHERTYPE_IP, 0, DL_CLDLS, 0, 0);
89	dlbindack(fd, buf);
90	/*
91	 * write full headers
92	 */
93	if (strioctl(fd, DLIOCRAW, -1, 0, NULL) == -1)
94	    {
95		fprintf(stderr, "DLIOCRAW error\n");
96		exit(-1);
97	    }
98	return fd;
99}
100
101
102/*
103 * output an IP packet onto a fd opened for /dev/nit
104 */
105int	sendip(fd, pkt, len)
106int	fd, len;
107char	*pkt;
108{
109	struct	strbuf	dbuf, *dp = &dbuf;
110
111	/*
112	 * construct NIT STREAMS messages, first control then data.
113	 */
114	dp->buf = pkt;
115	dp->len = len;
116	dp->maxlen = dp->len;
117
118	if (putmsg(fd, NULL, dp, 0) == -1)
119	    {
120		perror("putmsg");
121		return -1;
122	    }
123	if (ioctl(fd, I_FLUSH, FLUSHW) == -1)
124	    {
125		perror("I_FLUSHW");
126		return -1;
127	    }
128	return len;
129}
130