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