xref: /freebsd/sbin/ipf/ipsend/sockraw.c (revision 97759ccc715c4b365432c16d763c50eecfcb1100)
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 
32 
33 int
34 initdevice(char *device, int tout)
35 {
36 	struct sockaddr s;
37 	struct ifreq ifr;
38 	int fd;
39 
40 	memset(&ifr, 0, sizeof(ifr));
41 	strncpy(ifr.ifr_name, device, sizeof ifr.ifr_name);
42 
43 	if ((fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
44 	    {
45 		perror("socket(AF_INET, SOCK_RAW, IPPROTO_RAW)");
46 		return (-1);
47 	    }
48 
49 	if (ioctl(fd, SIOCGIFADDR, &ifr) == -1)
50 	    {
51 		perror("ioctl SIOCGIFADDR");
52 		return (-1);
53 	    }
54 
55 	bzero((char *)&s, sizeof(s));
56 	s.sa_family = AF_INET;
57 	bcopy(&ifr.ifr_addr, s.sa_data, 4);
58 	if (bind(fd, &s, sizeof(s)) == -1)
59 		perror("bind");
60 	return (fd);
61 }
62 
63 
64 /*
65  * output an IP packet
66  */
67 int	sendip(int fd, char *pkt, int len)
68 {
69 	struct ether_header *eh;
70 	struct sockaddr_in sin;
71 
72 	eh = (struct ether_header *)pkt;
73 	bzero((char *)&sin, sizeof(sin));
74 	sin.sin_family = AF_INET;
75 	pkt += 14;
76 	len -= 14;
77 	bcopy(pkt + 12, (char *)&sin.sin_addr, 4);
78 
79 	if (sendto(fd, pkt, len, 0, &sin, sizeof(sin)) == -1)
80 	    {
81 		perror("send");
82 		return (-1);
83 	    }
84 
85 	return (len);
86 }
87