diff options
Diffstat (limited to 'src/OSSupport/HostnameLookup.cpp')
-rw-r--r-- | src/OSSupport/HostnameLookup.cpp | 121 |
1 files changed, 51 insertions, 70 deletions
diff --git a/src/OSSupport/HostnameLookup.cpp b/src/OSSupport/HostnameLookup.cpp index d86430d83..ee9610f63 100644 --- a/src/OSSupport/HostnameLookup.cpp +++ b/src/OSSupport/HostnameLookup.cpp @@ -15,104 +15,85 @@ //////////////////////////////////////////////////////////////////////////////// // cHostnameLookup: -cHostnameLookup::cHostnameLookup(const AString & a_Hostname, cNetwork::cResolveNameCallbacksPtr a_Callbacks): - m_Callbacks(std::move(a_Callbacks)), - m_Hostname(a_Hostname) +void cHostnameLookup::Lookup( + const AString & a_Hostname, cNetwork::cResolveNameCallbacksPtr a_Callbacks) { -} - - - - - -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)) }; + // Note the Lookup object is owned solely by this lambda which is destroyed + // after it runs + cNetworkSingleton::Get().GetLookupThread().async_resolve( + a_Hostname, "", + [Callbacks = std::move(a_Callbacks)](const auto & a_Error, const auto & a_Results) + { + // If an error has occurred, notify the error callback: + if (a_Error) + { + Callbacks->OnError(a_Error.value(), a_Error.message()); + return; + } - // 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); - }); + Callback(*Callbacks.get(), a_Results); + } + ); } -void cHostnameLookup::Callback(int a_ErrCode, addrinfo * a_Addr) +void cHostnameLookup::Callback(cNetwork::cResolveNameCallbacks & a_Callbacks, const asio::ip::tcp::resolver::results_type & a_Addr) { - // If an error has occurred, notify the error callback: - if (a_ErrCode != 0) - { - m_Callbacks->OnError(a_ErrCode, ErrorString(a_ErrCode)); - return; - } - // 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 (const auto & Addr : a_Addr) { - char IP[128]; - switch (a_Addr->ai_family) + const auto & Endpoint = Addr.endpoint(); + const auto & Address = Endpoint.address(); + const auto & Hostname = Addr.host_name(); + + if (Address.is_v4()) { - case AF_INET: // IPv4 + const auto sin = + reinterpret_cast<const sockaddr_in *>(Endpoint.data()); + if (!a_Callbacks.OnNameResolvedV4(Hostname, sin)) { - 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: - HasResolved = true; - continue; - } - evutil_inet_ntop(AF_INET, &(sin->sin_addr), IP, sizeof(IP)); - break; + // Callback indicated that the IP shouldn't be serialized to + // a string, just continue with the next address: + HasResolved = true; + continue; } - case AF_INET6: // IPv6 - { - 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: - HasResolved = true; - continue; - } - evutil_inet_ntop(AF_INET6, &(sin->sin6_addr), IP, sizeof(IP)); - break; - } - default: + } + else if (Address.is_v6()) + { + const auto sin = + reinterpret_cast<const sockaddr_in6 *>(Endpoint.data()); + if (!a_Callbacks.OnNameResolvedV6(Hostname, sin)) { - // Unknown address family, handle as if this entry wasn't received - continue; // for (a_Addr) + // Callback indicated that the IP shouldn't be serialized to + // a string, just continue with the next address: + HasResolved = true; + continue; } } - m_Callbacks->OnNameResolved(m_Hostname, IP); + else + { + // Unknown address family, handle as if this entry wasn't + // received + continue; // for (a_Addr) + } + a_Callbacks.OnNameResolved(Hostname, Address.to_string()); HasResolved = true; } // for (a_Addr) // If only unsupported families were reported, call the Error handler: if (!HasResolved) { - m_Callbacks->OnError(EAI_NONAME, ErrorString(EAI_NONAME)); + a_Callbacks.OnError(EAI_NONAME, ErrorString(EAI_NONAME)); } else { - m_Callbacks->OnFinished(); + a_Callbacks.OnFinished(); } - freeaddrinfo(OrigAddr); } |