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