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