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