xref: /netbsd-src/external/bsd/ipf/dist/ipsend/slinux.c (revision 13885a665959c62f13a82b3caedf986eaa17aa31)
1*13885a66Sdarrenr /*	$NetBSD: slinux.c,v 1.2 2012/07/22 14:27:36 darrenr Exp $	*/
2bc4097aaSchristos 
3bc4097aaSchristos /*
4bc4097aaSchristos  * (C)opyright 1992-1998 Darren Reed. (from tcplog)
5bc4097aaSchristos  *
6bc4097aaSchristos  * See the IPFILTER.LICENCE file for details on licencing.
7bc4097aaSchristos  *
8bc4097aaSchristos  */
9bc4097aaSchristos 
10bc4097aaSchristos #include <stdio.h>
11bc4097aaSchristos #include <string.h>
12bc4097aaSchristos #include <netdb.h>
13bc4097aaSchristos #include <ctype.h>
14bc4097aaSchristos #include <signal.h>
15bc4097aaSchristos #include <errno.h>
16bc4097aaSchristos #include <sys/types.h>
17bc4097aaSchristos #include <sys/time.h>
18bc4097aaSchristos #include <sys/timeb.h>
19bc4097aaSchristos #include <sys/socket.h>
20bc4097aaSchristos #include <sys/file.h>
21bc4097aaSchristos #include <sys/ioctl.h>
22bc4097aaSchristos #include <sys/dir.h>
23bc4097aaSchristos #include <linux/netdevice.h>
24bc4097aaSchristos #include <net/if.h>
25bc4097aaSchristos #include <netinet/in.h>
26bc4097aaSchristos #include <netinet/in_systm.h>
27bc4097aaSchristos #include <netinet/ip.h>
28bc4097aaSchristos #include <netinet/tcp.h>
29bc4097aaSchristos #include "ipsend.h"
30bc4097aaSchristos 
31bc4097aaSchristos #if !defined(lint)
32bc4097aaSchristos static const char sccsid[] = "@(#)slinux.c	1.2 8/25/95";
33*13885a66Sdarrenr static const char rcsid[] = "@(#)Id: slinux.c,v 1.1.1.2 2012/07/22 13:44:37 darrenr Exp $";
34bc4097aaSchristos #endif
35bc4097aaSchristos 
36bc4097aaSchristos #define	CHUNKSIZE	8192
37bc4097aaSchristos #define BUFSPACE	(4*CHUNKSIZE)
38bc4097aaSchristos 
39bc4097aaSchristos /*
40bc4097aaSchristos  * Be careful to only include those defined in the flags option for the
41bc4097aaSchristos  * interface are included in the header size.
42bc4097aaSchristos  */
43bc4097aaSchristos 
44bc4097aaSchristos static	int	timeout;
45bc4097aaSchristos static	char	*eth_dev = NULL;
46bc4097aaSchristos 
47bc4097aaSchristos 
initdevice(dev,spare)48bc4097aaSchristos int	initdevice(dev, spare)
49bc4097aaSchristos 	char	*dev;
50bc4097aaSchristos 	int	spare;
51bc4097aaSchristos {
52bc4097aaSchristos 	int fd;
53bc4097aaSchristos 
54bc4097aaSchristos 	eth_dev = strdup(dev);
55bc4097aaSchristos 	if ((fd = socket(AF_INET, SOCK_PACKET, htons(ETHERTYPE_IP))) == -1)
56bc4097aaSchristos 	    {
57bc4097aaSchristos 		perror("socket(SOCK_PACKET)");
58bc4097aaSchristos 		exit(-1);
59bc4097aaSchristos 	    }
60bc4097aaSchristos 
61bc4097aaSchristos 	return fd;
62bc4097aaSchristos }
63bc4097aaSchristos 
64bc4097aaSchristos 
65bc4097aaSchristos /*
66bc4097aaSchristos  * output an IP packet onto a fd opened for /dev/nit
67bc4097aaSchristos  */
sendip(fd,pkt,len)68bc4097aaSchristos int	sendip(fd, pkt, len)
69bc4097aaSchristos 	int	fd, len;
70bc4097aaSchristos 	char	*pkt;
71bc4097aaSchristos {
72bc4097aaSchristos 	struct	sockaddr	s;
73bc4097aaSchristos 	struct	ifreq	ifr;
74bc4097aaSchristos 
75bc4097aaSchristos 	strncpy(ifr.ifr_name, eth_dev, sizeof(ifr.ifr_name));
76bc4097aaSchristos 	if (ioctl(fd, SIOCGIFHWADDR, &ifr) == -1)
77bc4097aaSchristos 	    {
78bc4097aaSchristos 		perror("SIOCGIFHWADDR");
79bc4097aaSchristos 		return -1;
80bc4097aaSchristos 	    }
81bc4097aaSchristos 	bcopy(ifr.ifr_hwaddr.sa_data, pkt + 6, 6);
82bc4097aaSchristos 	s.sa_family = ETHERTYPE_IP;
83bc4097aaSchristos 	strncpy(s.sa_data, eth_dev, sizeof(s.sa_data));
84bc4097aaSchristos 
85bc4097aaSchristos 	if (sendto(fd, pkt, len, 0, &s, sizeof(s)) == -1)
86bc4097aaSchristos 	    {
87bc4097aaSchristos 		perror("send");
88bc4097aaSchristos 		return -1;
89bc4097aaSchristos 	    }
90bc4097aaSchristos 
91bc4097aaSchristos 	return len;
92bc4097aaSchristos }
93