diff options
Diffstat (limited to 'src/OSSupport/HostnameLookup.cpp')
-rw-r--r-- | src/OSSupport/HostnameLookup.cpp | 56 |
1 files changed, 26 insertions, 30 deletions
diff --git a/src/OSSupport/HostnameLookup.cpp b/src/OSSupport/HostnameLookup.cpp index d86430d83..a1489c389 100644 --- a/src/OSSupport/HostnameLookup.cpp +++ b/src/OSSupport/HostnameLookup.cpp @@ -15,9 +15,8 @@ //////////////////////////////////////////////////////////////////////////////// // cHostnameLookup: -cHostnameLookup::cHostnameLookup(const AString & a_Hostname, cNetwork::cResolveNameCallbacksPtr a_Callbacks): - m_Callbacks(std::move(a_Callbacks)), - m_Hostname(a_Hostname) +cHostnameLookup::cHostnameLookup(const AString & a_Hostname, cNetwork::cResolveNameCallbacksPtr a_Callbacks) : + m_Callbacks(std::move(a_Callbacks)), m_Hostname(a_Hostname) { } @@ -28,24 +27,26 @@ cHostnameLookup::cHostnameLookup(const AString & a_Hostname, cNetwork::cResolveN void cHostnameLookup::Lookup(const AString & a_Hostname, cNetwork::cResolveNameCallbacksPtr a_Callbacks) { // Cannot use std::make_shared here, constructor is not accessible - cHostnameLookupPtr Lookup{ new cHostnameLookup(a_Hostname, std::move(a_Callbacks)) }; + cHostnameLookupPtr Lookup {new cHostnameLookup(a_Hostname, std::move(a_Callbacks))}; // Note the Lookup object is owned solely by this lambda which is destroyed after it runs - cNetworkSingleton::Get().GetLookupThread().ScheduleLookup([=]() - { - // Start the lookup: - addrinfo hints; - memset(&hints, 0, sizeof(hints)); - hints.ai_protocol = IPPROTO_TCP; - hints.ai_socktype = SOCK_STREAM; - hints.ai_family = AF_UNSPEC; - hints.ai_flags = AI_CANONNAME; - - addrinfo * Result; - int ErrCode = getaddrinfo(Lookup->m_Hostname.c_str(), nullptr, &hints, &Result); - - Lookup->Callback(ErrCode, Result); - }); + cNetworkSingleton::Get().GetLookupThread().ScheduleLookup( + [=]() + { + // Start the lookup: + addrinfo hints; + memset(&hints, 0, sizeof(hints)); + hints.ai_protocol = IPPROTO_TCP; + hints.ai_socktype = SOCK_STREAM; + hints.ai_family = AF_UNSPEC; + hints.ai_flags = AI_CANONNAME; + + addrinfo * Result; + int ErrCode = getaddrinfo(Lookup->m_Hostname.c_str(), nullptr, &hints, &Result); + + Lookup->Callback(ErrCode, Result); + } + ); } @@ -64,7 +65,7 @@ void cHostnameLookup::Callback(int a_ErrCode, addrinfo * a_Addr) // Call the success handler for each entry received: bool HasResolved = false; addrinfo * OrigAddr = a_Addr; - for (;a_Addr != nullptr; a_Addr = a_Addr->ai_next) + for (; a_Addr != nullptr; a_Addr = a_Addr->ai_next) { char IP[128]; switch (a_Addr->ai_family) @@ -74,7 +75,8 @@ void cHostnameLookup::Callback(int a_ErrCode, addrinfo * a_Addr) sockaddr_in * sin = reinterpret_cast<sockaddr_in *>(a_Addr->ai_addr); if (!m_Callbacks->OnNameResolvedV4(m_Hostname, sin)) { - // Callback indicated that the IP shouldn't be serialized to a string, just continue with the next address: + // Callback indicated that the IP shouldn't be serialized to a string, just continue with the next + // address: HasResolved = true; continue; } @@ -86,7 +88,8 @@ void cHostnameLookup::Callback(int a_ErrCode, addrinfo * a_Addr) sockaddr_in6 * sin = reinterpret_cast<sockaddr_in6 *>(a_Addr->ai_addr); if (!m_Callbacks->OnNameResolvedV6(m_Hostname, sin)) { - // Callback indicated that the IP shouldn't be serialized to a string, just continue with the next address: + // Callback indicated that the IP shouldn't be serialized to a string, just continue with the next + // address: HasResolved = true; continue; } @@ -122,15 +125,8 @@ void cHostnameLookup::Callback(int a_ErrCode, addrinfo * a_Addr) //////////////////////////////////////////////////////////////////////////////// // cNetwork API: -bool cNetwork::HostnameToIP( - const AString & a_Hostname, - cNetwork::cResolveNameCallbacksPtr a_Callbacks -) +bool cNetwork::HostnameToIP(const AString & a_Hostname, cNetwork::cResolveNameCallbacksPtr a_Callbacks) { cHostnameLookup::Lookup(a_Hostname, std::move(a_Callbacks)); return true; } - - - - |