xref: /netbsd-src/external/bsd/ipf/dist/ipsend/sirix.c (revision bc4097aacfdd9307c19b7947c13c6ad6982527a9)
1*bc4097aaSchristos /*	$NetBSD: sirix.c,v 1.1.1.1 2012/03/23 21:20:07 christos Exp $	*/
2*bc4097aaSchristos 
3*bc4097aaSchristos /*
4*bc4097aaSchristos  * (C)opyright 1992-1998 Darren Reed.
5*bc4097aaSchristos  * (C)opyright 1997 Marc Boucher.
6*bc4097aaSchristos  *
7*bc4097aaSchristos  * See the IPFILTER.LICENCE file for details on licencing.
8*bc4097aaSchristos  *
9*bc4097aaSchristos  */
10*bc4097aaSchristos #include <stdio.h>
11*bc4097aaSchristos #include <sys/types.h>
12*bc4097aaSchristos #include <string.h>
13*bc4097aaSchristos #include <unistd.h>
14*bc4097aaSchristos #include <stdlib.h>
15*bc4097aaSchristos #include <errno.h>
16*bc4097aaSchristos #include <sys/socket.h>
17*bc4097aaSchristos #include <sys/ioctl.h>
18*bc4097aaSchristos 
19*bc4097aaSchristos #include <net/if.h>
20*bc4097aaSchristos #include <net/raw.h>
21*bc4097aaSchristos #include <netinet/in.h>
22*bc4097aaSchristos #include <netinet/in_systm.h>
23*bc4097aaSchristos #include <netinet/ip.h>
24*bc4097aaSchristos #include <netinet/if_ether.h>
25*bc4097aaSchristos #include <netinet/ip_var.h>
26*bc4097aaSchristos #include "ipsend.h"
27*bc4097aaSchristos #include <netinet/udp_var.h>
28*bc4097aaSchristos 
29*bc4097aaSchristos #if !defined(lint) && defined(LIBC_SCCS)
30*bc4097aaSchristos static	char	sirix[] = "@(#)sirix.c	1.0 10/9/97 (C)1997 Marc Boucher";
31*bc4097aaSchristos #endif
32*bc4097aaSchristos 
33*bc4097aaSchristos 
initdevice(char * device,int tout)34*bc4097aaSchristos int	initdevice(char *device, int tout)
35*bc4097aaSchristos {
36*bc4097aaSchristos 	int fd;
37*bc4097aaSchristos 	struct sockaddr_raw sr;
38*bc4097aaSchristos 
39*bc4097aaSchristos 	if ((fd = socket(PF_RAW, SOCK_RAW, RAWPROTO_DRAIN)) < 0)
40*bc4097aaSchristos 	    {
41*bc4097aaSchristos 		perror("socket(PF_RAW, SOCK_RAW, RAWPROTO_DRAIN)");
42*bc4097aaSchristos 		return -1;
43*bc4097aaSchristos 	    }
44*bc4097aaSchristos 
45*bc4097aaSchristos 	memset(&sr, 0, sizeof(sr));
46*bc4097aaSchristos 	sr.sr_family = AF_RAW;
47*bc4097aaSchristos 	sr.sr_port = ETHERTYPE_IP;
48*bc4097aaSchristos 	strncpy(sr.sr_ifname, device, sizeof(sr.sr_ifname));
49*bc4097aaSchristos 	if (bind(fd, &sr, sizeof(sr)) < 0)
50*bc4097aaSchristos 	    {
51*bc4097aaSchristos 		perror("bind AF_RAW");
52*bc4097aaSchristos 		close(fd);
53*bc4097aaSchristos 		return -1;
54*bc4097aaSchristos 	    }
55*bc4097aaSchristos 	return fd;
56*bc4097aaSchristos }
57*bc4097aaSchristos 
58*bc4097aaSchristos 
59*bc4097aaSchristos /*
60*bc4097aaSchristos  * output an IP packet
61*bc4097aaSchristos  */
sendip(int fd,char * pkt,int len)62*bc4097aaSchristos int	sendip(int fd, char *pkt, int len)
63*bc4097aaSchristos {
64*bc4097aaSchristos 	struct sockaddr_raw sr;
65*bc4097aaSchristos 	int srlen = sizeof(sr);
66*bc4097aaSchristos 	struct ifreq ifr;
67*bc4097aaSchristos 	struct ether_header *eh = (struct ether_header *)pkt;
68*bc4097aaSchristos 
69*bc4097aaSchristos 	if (getsockname(fd, &sr, &srlen) == -1)
70*bc4097aaSchristos 	    {
71*bc4097aaSchristos 		perror("getsockname");
72*bc4097aaSchristos 		return -1;
73*bc4097aaSchristos 	    }
74*bc4097aaSchristos 
75*bc4097aaSchristos 	memset(&ifr, 0, sizeof(ifr));
76*bc4097aaSchristos 	strncpy(ifr.ifr_name, sr.sr_ifname, sizeof ifr.ifr_name);
77*bc4097aaSchristos 
78*bc4097aaSchristos 	if (ioctl(fd, SIOCGIFADDR, &ifr) == -1)
79*bc4097aaSchristos 	    {
80*bc4097aaSchristos 		perror("ioctl SIOCGIFADDR");
81*bc4097aaSchristos 		return -1;
82*bc4097aaSchristos 	    }
83*bc4097aaSchristos 
84*bc4097aaSchristos 	memcpy(eh->ether_shost, ifr.ifr_addr.sa_data, sizeof(eh->ether_shost));
85*bc4097aaSchristos 
86*bc4097aaSchristos 	if (write(fd, pkt, len) == -1)
87*bc4097aaSchristos 	    {
88*bc4097aaSchristos 		perror("send");
89*bc4097aaSchristos 		return -1;
90*bc4097aaSchristos 	    }
91*bc4097aaSchristos 
92*bc4097aaSchristos 	return len;
93*bc4097aaSchristos }
94