44 #pragma comment(lib, "IPHLPAPI.lib") 45 #pragma comment(lib, "ntdll.lib") 48 #include <arpa/inet.h> 61 if (sscanf(ip.c_str(),
"%d.%d.%d.%d", &a, &b, &c, &d) != 4)
73 bool isIPInRange(
const std::string& ip,
const std::string& network,
const std::string& mask)
76 uint32_t network_addr =
ipToUInt(network);
79 uint32_t net_lower = (network_addr & mask_addr);
80 uint32_t net_upper = (net_lower | (~mask_addr));
82 if (ip_addr >= net_lower && ip_addr <= net_upper)
92 bool getThisHostsIP(
string& this_hosts_ip,
const string& other_hosts_ip,
const string& network_interface)
98 DWORD dwOtherHostsIP = 0;
99 if (other_hosts_ip.size() > 0)
101 dwOtherHostsIP = htonl(
ipToUInt(other_hosts_ip));
106 DWORD ifindex = 0xffff;
108 if (network_interface.size() > 0)
110 PIP_ADAPTER_ADDRESSES addr = 0;
113 for (
int i = 0; i < 3; i++)
117 addr =
reinterpret_cast<PIP_ADAPTER_ADDRESSES
>(malloc(addr_size));
125 ULONG ret = GetAdaptersAddresses(AF_INET, 0, 0, addr, &addr_size);
127 if (ret == ERROR_SUCCESS)
136 PIP_ADAPTER_ADDRESSES p = addr;
138 std::wstring wNetworkInterface(network_interface.begin(), network_interface.end());
142 if (network_interface.compare(p->AdapterName) == 0 || wNetworkInterface.compare(p->FriendlyName) == 0)
144 ifindex = p->IfIndex;
153 if (ifindex == 0xffff)
161 PMIB_IPADDRTABLE table = 0;
162 ULONG table_size = 0;
164 for (
int i = 0; i < 5; i++)
166 int result = GetIpAddrTable(table, &table_size,
false);
168 if (result == NO_ERROR)
172 else if (result == ERROR_INSUFFICIENT_BUFFER)
175 table =
static_cast<PMIB_IPADDRTABLE
>(malloc(table_size));
186 bool found_valid =
false;
187 for (
unsigned int i = 0; i < table->dwNumEntries; i++)
189 PMIB_IPADDRROW row = &table->table[i];
193 if (row->dwAddr == htonl(INADDR_LOOPBACK))
200 if (ifindex == 0xffff || ifindex == row->dwIndex)
204 if ((row->dwAddr & row->dwMask) == (dwOtherHostsIP & row->dwMask))
208 addr.S_un.S_addr = row->dwAddr;
209 RtlIpv4AddressToStringA(&addr, tmp);
210 this_hosts_ip = string(tmp);
231 return RtlIpv4StringToAddressA(ip.c_str(), TRUE, &tp, &addr) == 0;
236 bool getThisHostsIP(
string& this_hosts_ip,
const string& other_hosts_ip,
const string& network_interface)
239 struct ifaddrs* if_addr_struct = NULL;
240 struct ifaddrs* ifa = NULL;
241 void* tmp_addr_ptr = NULL;
242 getifaddrs(&if_addr_struct);
243 bool found_valid =
false;
244 char address_buffer[INET_ADDRSTRLEN], netmask_buffer[INET_ADDRSTRLEN];
245 for (ifa = if_addr_struct; ifa != NULL; ifa = ifa->ifa_next)
249 if (!ifa->ifa_addr || ifa->ifa_addr->sa_family != AF_INET)
252 tmp_addr_ptr = &((
struct sockaddr_in*)ifa->ifa_addr)->sin_addr;
253 inet_ntop(AF_INET, tmp_addr_ptr, address_buffer, INET_ADDRSTRLEN);
257 if (network_interface.size() == 0 || strcmp(network_interface.c_str(), ifa->ifa_name) == 0)
261 tmp_addr_ptr = &((
struct sockaddr_in*)ifa->ifa_netmask)->sin_addr;
262 inet_ntop(AF_INET, tmp_addr_ptr, netmask_buffer, INET_ADDRSTRLEN);
263 if (
isIPInRange(address_buffer, other_hosts_ip, netmask_buffer))
273 this_hosts_ip = string(address_buffer);
282 static struct sockaddr_in sa;
283 return TEMP_FAILURE_RETRY(inet_pton(AF_INET, ip.c_str(), &(sa.sin_addr))) == 1;
bool getThisHostsIP(string &this_hosts_ip, const string &other_hosts_ip, const string &network_interface)
bool isValidIPAddress(const std::string &ip)
Checks if given string is a valid IP address.
uint32_t ipToUInt(const std::string &ip)
Converts a string-represented ip into uint (e.g.
bool isIPInRange(const std::string &ip, const std::string &network, const std::string &mask)
Checks if a given ip is in range of a network defined by ip/subnet taken from: https://www.stev.org/post/ccheckanipaddressisinaipmask.