141edb306SCy Schubert
241edb306SCy Schubert /*
341edb306SCy Schubert * (C)opyright 2000 Darren Reed.
441edb306SCy Schubert *
541edb306SCy Schubert * See the IPFILTER.LICENCE file for details on licencing.
641edb306SCy Schubert *
741edb306SCy Schubert * WARNING: Attempting to use this .c file on HP-UX 11.00 will cause the
841edb306SCy Schubert * system to crash.
941edb306SCy Schubert */
1041edb306SCy Schubert #include <sys/param.h>
1141edb306SCy Schubert #include <sys/types.h>
1241edb306SCy Schubert #include <sys/socket.h>
1341edb306SCy Schubert #include <sys/ioctl.h>
1441edb306SCy Schubert
1541edb306SCy Schubert #include <net/if.h>
1641edb306SCy Schubert #include <netinet/in.h>
1741edb306SCy Schubert #include <netinet/in_systm.h>
1841edb306SCy Schubert #include <netinet/ip.h>
1941edb306SCy Schubert #include <netinet/if_ether.h>
2041edb306SCy Schubert #include <netinet/ip_var.h>
2141edb306SCy Schubert #include <netinet/udp.h>
2241edb306SCy Schubert #include <netinet/udp_var.h>
2341edb306SCy Schubert #include <netinet/tcp.h>
2441edb306SCy Schubert #include <stdio.h>
2541edb306SCy Schubert #include <string.h>
2641edb306SCy Schubert #include <unistd.h>
2741edb306SCy Schubert #include <stdlib.h>
2841edb306SCy Schubert #include <errno.h>
2941edb306SCy Schubert #include "ipsend.h"
3041edb306SCy Schubert
3141edb306SCy Schubert
3241edb306SCy Schubert
33efeb8bffSCy Schubert int
initdevice(char * device,int tout)34efeb8bffSCy Schubert initdevice(char *device, int tout)
3541edb306SCy Schubert {
3641edb306SCy Schubert struct sockaddr s;
3741edb306SCy Schubert struct ifreq ifr;
3841edb306SCy Schubert int fd;
3941edb306SCy Schubert
4041edb306SCy Schubert memset(&ifr, 0, sizeof(ifr));
4141edb306SCy Schubert strncpy(ifr.ifr_name, device, sizeof ifr.ifr_name);
4241edb306SCy Schubert
4341edb306SCy Schubert if ((fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
4441edb306SCy Schubert {
4541edb306SCy Schubert perror("socket(AF_INET, SOCK_RAW, IPPROTO_RAW)");
46*2582ae57SCy Schubert return (-1);
4741edb306SCy Schubert }
4841edb306SCy Schubert
4941edb306SCy Schubert if (ioctl(fd, SIOCGIFADDR, &ifr) == -1)
5041edb306SCy Schubert {
5141edb306SCy Schubert perror("ioctl SIOCGIFADDR");
52*2582ae57SCy Schubert return (-1);
5341edb306SCy Schubert }
5441edb306SCy Schubert
5541edb306SCy Schubert bzero((char *)&s, sizeof(s));
5641edb306SCy Schubert s.sa_family = AF_INET;
5741edb306SCy Schubert bcopy(&ifr.ifr_addr, s.sa_data, 4);
5841edb306SCy Schubert if (bind(fd, &s, sizeof(s)) == -1)
5941edb306SCy Schubert perror("bind");
60*2582ae57SCy Schubert return (fd);
6141edb306SCy Schubert }
6241edb306SCy Schubert
6341edb306SCy Schubert
6441edb306SCy Schubert /*
6541edb306SCy Schubert * output an IP packet
6641edb306SCy Schubert */
sendip(int fd,char * pkt,int len)6741edb306SCy Schubert int sendip(int fd, char *pkt, int len)
6841edb306SCy Schubert {
6941edb306SCy Schubert struct ether_header *eh;
7041edb306SCy Schubert struct sockaddr_in sin;
7141edb306SCy Schubert
7241edb306SCy Schubert eh = (struct ether_header *)pkt;
7341edb306SCy Schubert bzero((char *)&sin, sizeof(sin));
7441edb306SCy Schubert sin.sin_family = AF_INET;
7541edb306SCy Schubert pkt += 14;
7641edb306SCy Schubert len -= 14;
7741edb306SCy Schubert bcopy(pkt + 12, (char *)&sin.sin_addr, 4);
7841edb306SCy Schubert
7941edb306SCy Schubert if (sendto(fd, pkt, len, 0, &sin, sizeof(sin)) == -1)
8041edb306SCy Schubert {
8141edb306SCy Schubert perror("send");
82*2582ae57SCy Schubert return (-1);
8341edb306SCy Schubert }
8441edb306SCy Schubert
85*2582ae57SCy Schubert return (len);
8641edb306SCy Schubert }
87