// These addresses are used as the targets to find out the default local address// on a multi-homed endpoint. They are actually DNS servers.constchar kPublicIPv4Host[]="8.8.8.8";constchar kPublicIPv6Host[]="2001:4860:4860::8888";constint kPublicPort =53;// DNS port.
IPAddress BasicNetworkManager::QueryDefaultLocalAddress(int family)const{RTC_DCHECK(thread_ ==Thread::Current());RTC_DCHECK(thread_->socketserver()!=nullptr);RTC_DCHECK(family == AF_INET || family == AF_INET6);
std::unique_ptr<AsyncSocket>socket(thread_->socketserver()->CreateAsyncSocket(family, SOCK_DGRAM));if(!socket){LOG_ERR(LERROR)<<"Socket creation failed";returnIPAddress();}if(socket->Connect(SocketAddress(family == AF_INET ? kPublicIPv4Host : kPublicIPv6Host, kPublicPort))<0){if(socket->GetError()!= ENETUNREACH && socket->GetError()!= EHOSTUNREACH){// Ignore the expected case of "host/net unreachable" - which happens if the network is V4- or V6-only.LOG(LS_INFO)<<"Connect failed with "<< socket->GetError();}returnIPAddress();}return socket->GetLocalAddress().ipaddr();}
SocketAddress PhysicalSocket::GetLocalAddress()const{
sockaddr_storage addr_storage ={0};
socklen_t addrlen =sizeof(addr_storage);
sockaddr* addr =reinterpret_cast<sockaddr*>(&addr_storage);int result =::getsockname(s_, addr,&addrlen);
SocketAddress address;if(result >=0){SocketAddressFromSockAddrStorage(addr_storage,&address);}else{LOG(LS_WARNING)<<"GetLocalAddress: unable to get local addr, socket="<< s_;}return address;}