00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <sys/types.h>
00026 #include <sys/socket.h>
00027 #include <sys/ioctl.h>
00028 #include <netinet/in.h>
00029 #include <unistd.h>
00030 #include <string.h>
00031 #include <arpa/inet.h>
00032 #include <net/if.h>
00033 #include <errno.h>
00034 #include <features.h>
00035 #if __GLIBC__ >=2 && __GLIBC_MINOR >= 1
00036 #include <netpacket/packet.h>
00037 #include <net/ethernet.h>
00038 #else
00039 #include <asm/types.h>
00040 #include <linux/if_packet.h>
00041 #include <linux/if_ether.h>
00042 #endif
00043
00044 #include "debug.h"
00045
00046 int read_interface(char *interface, int *ifindex, u_int32_t *addr, unsigned char *arp)
00047 {
00048 int fd;
00049 struct ifreq ifr;
00050 struct sockaddr_in *our_ip;
00051
00052 memset(&ifr, 0, sizeof(struct ifreq));
00053 if((fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) >= 0) {
00054 ifr.ifr_addr.sa_family = AF_INET;
00055 strcpy(ifr.ifr_name, interface);
00056
00057 if (addr) {
00058 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
00059 our_ip = (struct sockaddr_in *) &ifr.ifr_addr;
00060 *addr = our_ip->sin_addr.s_addr;
00061 DEBUG(LOG_INFO, "%s (our ip) = %s", ifr.ifr_name, inet_ntoa(our_ip->sin_addr));
00062 } else {
00063 LOG(LOG_ERR, "SIOCGIFADDR failed, is the interface up and configured?: %s",
00064 strerror(errno));
00065 return -1;
00066 }
00067 }
00068
00069 if (ioctl(fd, SIOCGIFINDEX, &ifr) == 0) {
00070 DEBUG(LOG_INFO, "adapter index %d", ifr.ifr_ifindex);
00071 *ifindex = ifr.ifr_ifindex;
00072 } else {
00073 LOG(LOG_ERR, "SIOCGIFINDEX failed!: %s", strerror(errno));
00074 return -1;
00075 }
00076 if (ioctl(fd, SIOCGIFHWADDR, &ifr) == 0) {
00077 memcpy(arp, ifr.ifr_hwaddr.sa_data, 6);
00078 DEBUG(LOG_INFO, "adapter hardware address %02x:%02x:%02x:%02x:%02x:%02x",
00079 arp[0], arp[1], arp[2], arp[3], arp[4], arp[5]);
00080 } else {
00081 LOG(LOG_ERR, "SIOCGIFHWADDR failed!: %s", strerror(errno));
00082 return -1;
00083 }
00084 } else {
00085 LOG(LOG_ERR, "socket failed!: %s", strerror(errno));
00086 return -1;
00087 }
00088 close(fd);
00089 return 0;
00090 }
00091
00092
00093 int listen_socket(unsigned int ip, int port, char *inf)
00094 {
00095 struct ifreq interface;
00096 int fd;
00097 struct sockaddr_in addr;
00098 int n = 1;
00099
00100 DEBUG(LOG_INFO, "Opening listen socket on 0x%08x:%d %s\n", ip, port, inf);
00101 if ((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
00102 DEBUG(LOG_ERR, "socket call failed: %s", strerror(errno));
00103 return -1;
00104 }
00105
00106 memset(&addr, 0, sizeof(addr));
00107 addr.sin_family = AF_INET;
00108 addr.sin_port = htons(port);
00109 addr.sin_addr.s_addr = ip;
00110
00111 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &n, sizeof(n)) == -1) {
00112 close(fd);
00113 return -1;
00114 }
00115 if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (char *) &n, sizeof(n)) == -1) {
00116 close(fd);
00117 return -1;
00118 }
00119
00120 strncpy(interface.ifr_ifrn.ifrn_name, inf, IFNAMSIZ);
00121 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,(char *)&interface, sizeof(interface)) < 0) {
00122 close(fd);
00123 return -1;
00124 }
00125
00126 if (bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr)) == -1) {
00127 close(fd);
00128 return -1;
00129 }
00130
00131 return fd;
00132 }
00133
00134
00135 int raw_socket(int ifindex)
00136 {
00137 int fd;
00138 struct sockaddr_ll sock;
00139
00140 DEBUG(LOG_INFO, "Opening raw socket on ifindex %d\n", ifindex);
00141 if ((fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) {
00142 DEBUG(LOG_ERR, "socket call failed: %s", strerror(errno));
00143 return -1;
00144 }
00145
00146 sock.sll_family = AF_PACKET;
00147 sock.sll_protocol = htons(ETH_P_IP);
00148 sock.sll_ifindex = ifindex;
00149 if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) < 0) {
00150 DEBUG(LOG_ERR, "bind call failed: %s", strerror(errno));
00151 close(fd);
00152 return -1;
00153 }
00154
00155 return fd;
00156 }
00157