sdlpi.c revision 53024
1/*
2 * (C)opyright 1992-1998 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
9#include <stdio.h>
10#include <netdb.h>
11#include <ctype.h>
12#include <fcntl.h>
13#include <signal.h>
14#include <errno.h>
15#include <sys/types.h>
16#include <sys/time.h>
17#include <sys/timeb.h>
18#include <sys/socket.h>
19#include <sys/file.h>
20#include <sys/ioctl.h>
21#include <sys/stropts.h>
22
23#ifdef sun
24#include <sys/pfmod.h>
25#include <sys/bufmod.h>
26#endif
27#include <sys/dlpi.h>
28
29#include <net/if.h>
30#include <netinet/in.h>
31#include <netinet/in_systm.h>
32#include <netinet/ip.h>
33#include <netinet/if_ether.h>
34#include <netinet/ip_var.h>
35#include <netinet/udp.h>
36#include <netinet/udp_var.h>
37#include <netinet/tcp.h>
38
39#include "ipsend.h"
40
41#if !defined(lint)
42static const char sccsid[] = "@(#)sdlpi.c	1.3 10/30/95 (C)1995 Darren Reed";
43static const char rcsid[] = "@(#)$Id: sdlpi.c,v 2.1 1999/08/04 17:31:13 darrenr Exp $";
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#ifdef sun /* we require RAW DLPI mode, which is a Sun extension */
94	if (strioctl(fd, DLIOCRAW, -1, 0, NULL) == -1)
95	    {
96		fprintf(stderr, "DLIOCRAW error\n");
97		exit(-1);
98	    }
99#else
100you lose
101#endif
102	return fd;
103}
104
105
106/*
107 * output an IP packet onto a fd opened for /dev/nit
108 */
109int	sendip(fd, pkt, len)
110int	fd, len;
111char	*pkt;
112{
113	struct	strbuf	dbuf, *dp = &dbuf;
114
115	/*
116	 * construct NIT STREAMS messages, first control then data.
117	 */
118	dp->buf = pkt;
119	dp->len = len;
120	dp->maxlen = dp->len;
121
122	if (putmsg(fd, NULL, dp, 0) == -1)
123	    {
124		perror("putmsg");
125		return -1;
126	    }
127	if (ioctl(fd, I_FLUSH, FLUSHW) == -1)
128	    {
129		perror("I_FLUSHW");
130		return -1;
131	    }
132	return len;
133}
134