xref: /freebsd/sbin/ipf/ipsend/sockraw.c (revision 9768746b)
1 /*	$FreeBSD$	*/
2 
3 /*
4  * (C)opyright 2000 Darren Reed.
5  *
6  * See the IPFILTER.LICENCE file for details on licencing.
7  *
8  * WARNING: Attempting to use this .c file on HP-UX 11.00 will cause the
9  *          system to crash.
10  */
11 #include <sys/param.h>
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <sys/ioctl.h>
15 
16 #include <net/if.h>
17 #include <netinet/in.h>
18 #include <netinet/in_systm.h>
19 #include <netinet/ip.h>
20 #include <netinet/if_ether.h>
21 #include <netinet/ip_var.h>
22 #include <netinet/udp.h>
23 #include <netinet/udp_var.h>
24 #include <netinet/tcp.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include "ipsend.h"
31 
32 #if !defined(lint) && defined(LIBC_SCCS)
33 static	char	sirix[] = "@(#)sirix.c	1.0 10/9/97 (C)1997 Marc Boucher";
34 #endif
35 
36 
37 int
38 initdevice(char *device, int tout)
39 {
40 	struct sockaddr s;
41 	struct ifreq ifr;
42 	int fd;
43 
44 	memset(&ifr, 0, sizeof(ifr));
45 	strncpy(ifr.ifr_name, device, sizeof ifr.ifr_name);
46 
47 	if ((fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
48 	    {
49 		perror("socket(AF_INET, SOCK_RAW, IPPROTO_RAW)");
50 		return (-1);
51 	    }
52 
53 	if (ioctl(fd, SIOCGIFADDR, &ifr) == -1)
54 	    {
55 		perror("ioctl SIOCGIFADDR");
56 		return (-1);
57 	    }
58 
59 	bzero((char *)&s, sizeof(s));
60 	s.sa_family = AF_INET;
61 	bcopy(&ifr.ifr_addr, s.sa_data, 4);
62 	if (bind(fd, &s, sizeof(s)) == -1)
63 		perror("bind");
64 	return (fd);
65 }
66 
67 
68 /*
69  * output an IP packet
70  */
71 int	sendip(int fd, char *pkt, int len)
72 {
73 	struct ether_header *eh;
74 	struct sockaddr_in sin;
75 
76 	eh = (struct ether_header *)pkt;
77 	bzero((char *)&sin, sizeof(sin));
78 	sin.sin_family = AF_INET;
79 	pkt += 14;
80 	len -= 14;
81 	bcopy(pkt + 12, (char *)&sin.sin_addr, 4);
82 
83 	if (sendto(fd, pkt, len, 0, &sin, sizeof(sin)) == -1)
84 	    {
85 		perror("send");
86 		return (-1);
87 	    }
88 
89 	return (len);
90 }
91