diff options
author | peterbell10 <peterbell10@live.co.uk> | 2017-06-15 11:03:49 +0200 |
---|---|---|
committer | Lukas Pioch <lukas@zgow.de> | 2017-06-15 11:03:49 +0200 |
commit | 035ecdc9e285ad2fd9ccf51e4ed2ac01b53dc3d1 (patch) | |
tree | b6caede9a35093d3e59c57c8c8c7295dc1c8fbaf /src/OSSupport/IPLookup.cpp | |
parent | Added missing 1.12 packet changes (diff) | |
download | cuberite-035ecdc9e285ad2fd9ccf51e4ed2ac01b53dc3d1.tar cuberite-035ecdc9e285ad2fd9ccf51e4ed2ac01b53dc3d1.tar.gz cuberite-035ecdc9e285ad2fd9ccf51e4ed2ac01b53dc3d1.tar.bz2 cuberite-035ecdc9e285ad2fd9ccf51e4ed2ac01b53dc3d1.tar.lz cuberite-035ecdc9e285ad2fd9ccf51e4ed2ac01b53dc3d1.tar.xz cuberite-035ecdc9e285ad2fd9ccf51e4ed2ac01b53dc3d1.tar.zst cuberite-035ecdc9e285ad2fd9ccf51e4ed2ac01b53dc3d1.zip |
Diffstat (limited to 'src/OSSupport/IPLookup.cpp')
-rw-r--r-- | src/OSSupport/IPLookup.cpp | 91 |
1 files changed, 38 insertions, 53 deletions
diff --git a/src/OSSupport/IPLookup.cpp b/src/OSSupport/IPLookup.cpp index 2722d4722..7b543793d 100644 --- a/src/OSSupport/IPLookup.cpp +++ b/src/OSSupport/IPLookup.cpp @@ -5,8 +5,9 @@ #include "Globals.h" #include "IPLookup.h" -#include <event2/dns.h> +#include <event2/util.h> #include "NetworkSingleton.h" +#include "GetAddressInfoError.h" @@ -15,8 +16,9 @@ //////////////////////////////////////////////////////////////////////////////// // cIPLookup: -cIPLookup::cIPLookup(cNetwork::cResolveNameCallbacksPtr a_Callbacks): - m_Callbacks(a_Callbacks) +cIPLookup::cIPLookup(const AString & a_IP, cNetwork::cResolveNameCallbacksPtr a_Callbacks): + m_Callbacks(a_Callbacks), + m_IP(a_IP) { ASSERT(a_Callbacks != nullptr); } @@ -25,68 +27,58 @@ cIPLookup::cIPLookup(cNetwork::cResolveNameCallbacksPtr a_Callbacks): -bool cIPLookup::Lookup(const AString & a_IP) +void cIPLookup::Lookup(const AString & a_IP, cNetwork::cResolveNameCallbacksPtr a_Callbacks) { - // Parse the IP address string into a sockaddr structure: - m_IP = a_IP; - sockaddr_storage sa; - int salen = static_cast<int>(sizeof(sa)); - memset(&sa, 0, sizeof(sa)); - if (evutil_parse_sockaddr_port(a_IP.c_str(), reinterpret_cast<sockaddr *>(&sa), &salen) != 0) - { - LOGD("Failed to parse IP address \"%s\".", a_IP.c_str()); - return false; - } + cIPLookupPtr Lookup{ new cIPLookup(a_IP, std::move(a_Callbacks)) }; // Cannot use std::make_shared here, constructor is not accessible - // Call the proper resolver based on the address family: - // Note that there's no need to store the evdns_request handle returned, LibEvent frees it on its own. - switch (sa.ss_family) + // Note the Lookup object is owned solely by this lambda which is destroyed after it runs + cNetworkSingleton::Get().GetLookupThread().ScheduleLookup([=]() { - case AF_INET: - { - sockaddr_in * sa4 = reinterpret_cast<sockaddr_in *>(&sa); - evdns_base_resolve_reverse(cNetworkSingleton::Get().GetDNSBase(), &(sa4->sin_addr), 0, Callback, this); - break; - } - case AF_INET6: - { - sockaddr_in6 * sa6 = reinterpret_cast<sockaddr_in6 *>(&sa); - evdns_base_resolve_reverse_ipv6(cNetworkSingleton::Get().GetDNSBase(), &(sa6->sin6_addr), 0, Callback, this); - break; - } - default: + sockaddr_storage sa; + int salen = sizeof(sa); + memset(&sa, 0, sizeof(sa)); + + int ErrCode = evutil_parse_sockaddr_port(Lookup->m_IP.c_str(), reinterpret_cast<sockaddr *>(&sa), &salen); + + if (ErrCode != 0) { - LOGWARNING("%s: Unknown address family: %d", __FUNCTION__, sa.ss_family); - ASSERT(!"Unknown address family"); - return false; + LOGD("Failed to parse IP address \"%s\".", Lookup->m_IP.c_str()); + Lookup->Callback(ErrCode, nullptr); + return; } - } // switch (address family) - return true; + + char Hostname[NI_MAXHOST]; + char ServInfo[NI_MAXSERV]; + + ErrCode = getnameinfo( + reinterpret_cast<sockaddr *>(&sa), + static_cast<socklen_t>(salen), + Hostname, sizeof(Hostname), + ServInfo, sizeof(ServInfo), + 0 + ); + Lookup->Callback(ErrCode, Hostname); + }); } -void cIPLookup::Callback(int a_Result, char a_Type, int a_Count, int a_Ttl, void * a_Addresses, void * a_Self) +void cIPLookup::Callback(int a_Result, const char * a_Address) { - // Get the Self class: - cIPLookup * Self = reinterpret_cast<cIPLookup *>(a_Self); - ASSERT(Self != nullptr); - // Call the proper callback based on the event received: - if ((a_Result != 0) || (a_Addresses == nullptr)) + if ((a_Result != 0) || (a_Address == nullptr)) { // An error has occurred, notify the error callback: - Self->m_Callbacks->OnError(a_Result, evutil_socket_error_to_string(a_Result)); + m_Callbacks->OnError(a_Result, ErrorString(a_Result)); } else { // Call the success handler: - Self->m_Callbacks->OnNameResolved(*(reinterpret_cast<char **>(a_Addresses)), Self->m_IP); - Self->m_Callbacks->OnFinished(); + m_Callbacks->OnNameResolved(a_Address, m_IP); + m_Callbacks->OnFinished(); } - cNetworkSingleton::Get().RemoveIPLookup(Self); } @@ -101,14 +93,7 @@ bool cNetwork::IPToHostName( cNetwork::cResolveNameCallbacksPtr a_Callbacks ) { - auto res = std::make_shared<cIPLookup>(a_Callbacks); - cNetworkSingleton::Get().AddIPLookup(res); - if (!res->Lookup(a_IP)) - { - // Lookup failed early on, remove the object completely: - cNetworkSingleton::Get().RemoveIPLookup(res.get()); - return false; - } + cIPLookup::Lookup(a_IP, std::move(a_Callbacks)); return true; } |