4 * (C)opyright 1992-1998 Darren Reed. (from tcplog)
6 * See the IPFILTER.LICENCE file for details on licencing.
15 #include <sys/types.h>
17 #include <sys/timeb.h>
18 #include <sys/socket.h>
20 #include <sys/ioctl.h>
22 #include <sys/fcntlcom.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>
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>
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";
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)
60 int initdevice(device
, tout
)
69 if ((fd
= open("/dev/nit", O_RDWR
)) < 0)
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");
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");
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
);
115 * output an IP packet onto a fd opened for /dev/nit
117 int sendip(fd
, pkt
, len
)
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
)))
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
;
146 dp
->maxlen
= dp
->len
;
148 if (putmsg(fd
, cp
, dp
, 0) == -1)
154 if (ioctl(fd
, I_FLUSH
, FLUSHW
) == -1)