boolIPFromAddrInfo(structaddrinfo* info, IPAddress* out){if(!info ||!info->ai_addr){returnfalse;}if(info->ai_addr->sa_family == AF_INET){
sockaddr_in* addr =reinterpret_cast<sockaddr_in*>(info->ai_addr);*out =IPAddress(addr->sin_addr);returntrue;}elseif(info->ai_addr->sa_family == AF_INET6){
sockaddr_in6* addr =reinterpret_cast<sockaddr_in6*>(info->ai_addr);*out =IPAddress(addr->sin6_addr);returntrue;}returnfalse;}intResolveHostname(const std::string& hostname,int family,
std::vector<IPAddress>* addresses){#ifdef__native_client__RTC_NOTREACHED();LOG(LS_WARNING)<<"ResolveHostname() is not implemented for NaCl";return-1;#else// __native_client__if(!addresses){return-1;}
addresses->clear();structaddrinfo* result =nullptr;structaddrinfo hints ={0};
hints.ai_family = family;// |family| here will almost always be AF_UNSPEC, because |family| comes from// AsyncResolver::addr_.family(), which comes from a SocketAddress constructed// with a hostname. When a SocketAddress is constructed with a hostname, its// family is AF_UNSPEC. However, if someday in the future we construct// a SocketAddress with both a hostname and a family other than AF_UNSPEC,// then it would be possible to get a specific family value here.// The behavior of AF_UNSPEC is roughly "get both ipv4 and ipv6", as// documented by the various operating systems:// Linux: http://man7.org/linux/man-pages/man3/getaddrinfo.3.html// Windows: https://msdn.microsoft.com/en-us/library/windows/desktop/// ms738520(v=vs.85).aspx// Mac: https://developer.apple.com/legacy/library/documentation/Darwin/// Reference/ManPages/man3/getaddrinfo.3.html// Android (source code, not documentation):// https://android.googlesource.com/platform/bionic/+/// 7e0bfb511e85834d7c6cb9631206b62f82701d60/libc/netbsd/net/getaddrinfo.c#1657
hints.ai_flags = AI_ADDRCONFIG;int ret =getaddrinfo(hostname.c_str(),nullptr,&hints,&result);if(ret !=0){return ret;}structaddrinfo* cursor = result;for(; cursor; cursor = cursor->ai_next){if(family == AF_UNSPEC || cursor->ai_family == family){
IPAddress ip;if(IPFromAddrInfo(cursor,&ip)){
addresses->push_back(ip);}}}freeaddrinfo(result);return0;#endif// !__native_client__}