Go to the documentation of this file.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
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "net_utils.h"
00037
00038 #include <string.h>
00039 #include <ifaddrs.h>
00040 #include <arpa/inet.h>
00041 #include <unistd.h>
00042
00043 namespace rc {
00044
00045 using namespace std;
00046
00047 uint32_t IPToUInt(const std::string ip) {
00048 int a, b, c, d;
00049 uint32_t addr = 0;
00050
00051 if (sscanf(ip.c_str(), "%d.%d.%d.%d", &a, &b, &c, &d) != 4)
00052 return 0;
00053
00054 addr = a << 24;
00055 addr |= b << 16;
00056 addr |= c << 8;
00057 addr |= d;
00058 return addr;
00059 }
00060
00061 bool isIPInRange(const std::string ip, const std::string network, const std::string mask) {
00062 uint32_t ip_addr = IPToUInt(ip);
00063 uint32_t network_addr = IPToUInt(network);
00064 uint32_t mask_addr = IPToUInt(mask);
00065
00066 uint32_t net_lower = (network_addr & mask_addr);
00067 uint32_t net_upper = (net_lower | (~mask_addr));
00068
00069 if (ip_addr >= net_lower &&
00070 ip_addr <= net_upper)
00071 return true;
00072 return false;
00073 }
00074
00075
00076 bool getThisHostsIP(string &thisHostsIP,
00077 const string &otherHostsIP,
00078 const string &networkInterface)
00079 {
00080
00081 struct ifaddrs *ifAddrStruct = NULL;
00082 struct ifaddrs *ifa = NULL;
00083 void *tmpAddrPtr = NULL;
00084 getifaddrs(&ifAddrStruct);
00085 bool foundValid = false;
00086 char addressBuffer[INET_ADDRSTRLEN], netmaskBuffer[INET_ADDRSTRLEN];
00087 for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next)
00088 {
00089
00090 if (!ifa->ifa_addr || ifa->ifa_addr->sa_family != AF_INET)
00091 continue;
00092
00093 tmpAddrPtr = &((struct sockaddr_in *) ifa->ifa_addr)->sin_addr;
00094 inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
00095
00096
00097 if (networkInterface != "")
00098 {
00099
00100 if (strcmp(networkInterface.c_str(), ifa->ifa_name) == 0)
00101 {
00102 foundValid = true;
00103 break;
00104 }
00105 }
00106
00107
00108 else if (otherHostsIP != "")
00109 {
00110 tmpAddrPtr = &((struct sockaddr_in *) ifa->ifa_netmask)->sin_addr;
00111 inet_ntop(AF_INET, tmpAddrPtr, netmaskBuffer, INET_ADDRSTRLEN);
00112 if (isIPInRange(addressBuffer, otherHostsIP, netmaskBuffer))
00113 {
00114 foundValid = true;
00115 break;
00116 }
00117 }
00118
00119
00120
00121
00122 else {
00123 if ( (strncmp("eth", ifa->ifa_name, 3) == 0) ||
00124 (strncmp("en", ifa->ifa_name, 2) == 0) ||
00125 (strncmp("wl", ifa->ifa_name, 2) == 0) )
00126 {
00127 foundValid = true;
00128 break;
00129 }
00130 }
00131 }
00132
00133 if (foundValid)
00134 thisHostsIP = string(addressBuffer);
00135 return foundValid;
00136 }
00137
00138 bool isValidIPAddress(const std::string &ip)
00139 {
00140
00141 static struct sockaddr_in sa;
00142 return TEMP_FAILURE_RETRY(inet_pton(AF_INET, ip.c_str(), &(sa.sin_addr))) ==
00143 1;
00144 }
00145
00146 }