#include "NetworkAddresses.hh" #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; uint32_t resolve_address(const char* address) { struct addrinfo *res0; if (getaddrinfo(address, NULL, NULL, &res0)) { auto e = string_for_error(errno); throw runtime_error(string_printf("can\'t resolve hostname %s: %s", address, e.c_str())); } std::unique_ptr res0_unique( res0, freeaddrinfo); struct addrinfo *res4 = NULL; for (struct addrinfo* res = res0; res; res = res->ai_next) { if (res->ai_family == AF_INET) { res4 = res; } } if (!res4) { throw runtime_error(string_printf( "can\'t resolve hostname %s: no usable data", address)); } struct sockaddr_in* res_sin = (struct sockaddr_in*)res4->ai_addr; return bswap32(res_sin->sin_addr.s_addr); } map get_local_addresses() { struct ifaddrs* ifa_raw; if (getifaddrs(&ifa_raw)) { auto s = string_for_error(errno); throw runtime_error(string_printf("failed to get interface addresses: %s", s.c_str())); } unique_ptr ifa(ifa_raw, freeifaddrs); map ret; for (struct ifaddrs* i = ifa.get(); i; i = i->ifa_next) { if (!i->ifa_addr) { continue; } auto* sin = reinterpret_cast(i->ifa_addr); if (sin->sin_family != AF_INET) { continue; } ret.emplace(i->ifa_name, bswap32(sin->sin_addr.s_addr)); } return ret; } bool is_local_address(uint32_t addr) { uint8_t net = (addr >> 24) & 0xFF; if ((net != 127) && (net != 172) && (net != 10) && (net != 192)) { return false; } return true; } bool is_local_address(const sockaddr_storage& daddr) { if (daddr.ss_family != AF_INET) { return false; } const sockaddr_in* sin = reinterpret_cast(&daddr); return is_local_address(bswap32(sin->sin_addr.s_addr)); } string string_for_address(uint32_t address) { return string_printf("%hhu.%hhu.%hhu.%hhu", static_cast(address >> 24), static_cast(address >> 16), static_cast(address >> 8), static_cast(address)); } uint32_t address_for_string(const char* address) { return bswap32(inet_addr(address)); }