Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / ipf / ipsend / snit.c
bloba4b19b9b83ee4b4071230e4db86042862a18bdd0
1 /* $NetBSD$ */
3 /*
4 * (C)opyright 1992-1998 Darren Reed. (from tcplog)
6 * See the IPFILTER.LICENCE file for details on licencing.
8 */
10 #include <stdio.h>
11 #include <netdb.h>
12 #include <ctype.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 <net/nit.h>
22 #include <sys/fcntlcom.h>
23 #include <sys/dir.h>
24 #include <net/nit_if.h>
25 #include <net/nit_pf.h>
26 #include <net/nit_buf.h>
27 #include <net/packetfilt.h>
28 #include <sys/stropts.h>
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>
40 #include "ipsend.h"
42 #if !defined(lint)
43 static const char sccsid[] = "@(#)snit.c 1.5 1/11/96 (C)1995 Darren Reed";
44 static const char rcsid[] = "@(#)Id: snit.c,v 2.3 2001/06/09 17:09:26 darrenr Exp";
45 #endif
47 #define CHUNKSIZE 8192
48 #define BUFSPACE (4*CHUNKSIZE)
51 * Be careful to only include those defined in the flags option for the
52 * interface are included in the header size.
54 #define BUFHDR_SIZE (sizeof(struct nit_bufhdr))
55 #define NIT_HDRSIZE (BUFHDR_SIZE)
57 static int timeout;
60 int initdevice(device, tout)
61 char *device;
62 int tout;
64 struct strioctl si;
65 struct timeval to;
66 struct ifreq ifr;
67 int fd;
69 if ((fd = open("/dev/nit", O_RDWR)) < 0)
71 perror("/dev/nit");
72 exit(-1);
76 * arrange to get messages from the NIT STREAM and use NIT_BUF option
78 ioctl(fd, I_SRDOPT, (char*)RMSGD);
79 ioctl(fd, I_PUSH, "nbuf");
82 * set the timeout
84 timeout = tout;
85 si.ic_timout = 1;
86 to.tv_sec = 1;
87 to.tv_usec = 0;
88 si.ic_cmd = NIOCSTIME;
89 si.ic_len = sizeof(to);
90 si.ic_dp = (char*)&to;
91 if (ioctl(fd, I_STR, (char*)&si) == -1)
93 perror("ioctl: NIT timeout");
94 exit(-1);
98 * request the interface
100 strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
101 ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = ' ';
102 si.ic_cmd = NIOCBIND;
103 si.ic_len = sizeof(ifr);
104 si.ic_dp = (char*)&ifr;
105 if (ioctl(fd, I_STR, (char*)&si) == -1)
107 perror(ifr.ifr_name);
108 exit(1);
110 return fd;
115 * output an IP packet onto a fd opened for /dev/nit
117 int sendip(fd, pkt, len)
118 int fd, len;
119 char *pkt;
121 struct sockaddr sk, *sa = &sk;
122 struct strbuf cbuf, *cp = &cbuf, dbuf, *dp = &dbuf;
125 * For ethernet, need at least 802.3 header and IP header.
127 if (len < (sizeof(sa->sa_data) + sizeof(struct ip)))
128 return -1;
130 * to avoid any output processing for IP, say we're not.
132 sa->sa_family = AF_UNSPEC;
133 bcopy(pkt, sa->sa_data, sizeof(sa->sa_data));
134 pkt += sizeof(sa->sa_data);
135 len -= sizeof(sa->sa_data);
138 * construct NIT STREAMS messages, first control then data.
140 cp->len = sizeof(*sa);
141 cp->maxlen = sizeof(*sa);
142 cp->buf = (char *)sa;
144 dp->buf = pkt;
145 dp->len = len;
146 dp->maxlen = dp->len;
148 if (putmsg(fd, cp, dp, 0) == -1)
150 perror("putmsg");
151 return -1;
154 if (ioctl(fd, I_FLUSH, FLUSHW) == -1)
156 perror("I_FLUSH");
157 return -1;
159 return len;