summaryrefslogtreecommitdiffstats
path: root/src/OSSupport/HostnameLookup.cpp
diff options
context:
space:
mode:
authorpeterbell10 <peterbell10@live.co.uk>2017-06-15 11:03:49 +0200
committerLukas Pioch <lukas@zgow.de>2017-06-15 11:03:49 +0200
commit035ecdc9e285ad2fd9ccf51e4ed2ac01b53dc3d1 (patch)
treeb6caede9a35093d3e59c57c8c8c7295dc1c8fbaf /src/OSSupport/HostnameLookup.cpp
parentAdded missing 1.12 packet changes (diff)
downloadcuberite-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/HostnameLookup.cpp')
-rw-r--r--src/OSSupport/HostnameLookup.cpp68
1 files changed, 34 insertions, 34 deletions
diff --git a/src/OSSupport/HostnameLookup.cpp b/src/OSSupport/HostnameLookup.cpp
index 0944153be..867472e8c 100644
--- a/src/OSSupport/HostnameLookup.cpp
+++ b/src/OSSupport/HostnameLookup.cpp
@@ -5,8 +5,8 @@
#include "Globals.h"
#include "HostnameLookup.h"
-#include <event2/dns.h>
#include "NetworkSingleton.h"
+#include "GetAddressInfoError.h"
@@ -15,8 +15,9 @@
////////////////////////////////////////////////////////////////////////////////
// cHostnameLookup:
-cHostnameLookup::cHostnameLookup(cNetwork::cResolveNameCallbacksPtr a_Callbacks):
- m_Callbacks(a_Callbacks)
+cHostnameLookup::cHostnameLookup(const AString & a_Hostname, cNetwork::cResolveNameCallbacksPtr a_Callbacks):
+ m_Callbacks(a_Callbacks),
+ m_Hostname(a_Hostname)
{
}
@@ -24,43 +25,45 @@ cHostnameLookup::cHostnameLookup(cNetwork::cResolveNameCallbacksPtr a_Callbacks)
-void cHostnameLookup::Lookup(const AString & a_Hostname)
+void cHostnameLookup::Lookup(const AString & a_Hostname, cNetwork::cResolveNameCallbacksPtr a_Callbacks)
{
- // Store the hostname for the callback:
- m_Hostname = a_Hostname;
-
- // Start the lookup:
- // Note that we don't have to store the LibEvent lookup handle, LibEvent will free it on its own.
- evutil_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 = EVUTIL_AI_CANONNAME;
- evdns_getaddrinfo(cNetworkSingleton::Get().GetDNSBase(), a_Hostname.c_str(), nullptr, &hints, Callback, this);
+ // 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().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);
+ });
}
-void cHostnameLookup::Callback(int a_ErrCode, evutil_addrinfo * a_Addr, void * a_Self)
+void cHostnameLookup::Callback(int a_ErrCode, addrinfo * a_Addr)
{
- // Get the Self class:
- cHostnameLookup * Self = reinterpret_cast<cHostnameLookup *>(a_Self);
- ASSERT(Self != nullptr);
-
// If an error has occurred, notify the error callback:
if (a_ErrCode != 0)
{
- Self->m_Callbacks->OnError(a_ErrCode, evutil_socket_error_to_string(a_ErrCode));
- cNetworkSingleton::Get().RemoveHostnameLookup(Self);
+ m_Callbacks->OnError(a_ErrCode, ErrorString(a_ErrCode));
return;
}
// Call the success handler for each entry received:
bool HasResolved = false;
- evutil_addrinfo * OrigAddr = a_Addr;
+ addrinfo * OrigAddr = a_Addr;
for (;a_Addr != nullptr; a_Addr = a_Addr->ai_next)
{
char IP[128];
@@ -69,7 +72,7 @@ void cHostnameLookup::Callback(int a_ErrCode, evutil_addrinfo * a_Addr, void * a
case AF_INET: // IPv4
{
sockaddr_in * sin = reinterpret_cast<sockaddr_in *>(a_Addr->ai_addr);
- if (!Self->m_Callbacks->OnNameResolvedV4(Self->m_Hostname, sin))
+ 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;
@@ -81,7 +84,7 @@ void cHostnameLookup::Callback(int a_ErrCode, evutil_addrinfo * a_Addr, void * a
case AF_INET6: // IPv6
{
sockaddr_in6 * sin = reinterpret_cast<sockaddr_in6 *>(a_Addr->ai_addr);
- if (!Self->m_Callbacks->OnNameResolvedV6(Self->m_Hostname, sin))
+ 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;
@@ -96,21 +99,20 @@ void cHostnameLookup::Callback(int a_ErrCode, evutil_addrinfo * a_Addr, void * a
continue; // for (a_Addr)
}
}
- Self->m_Callbacks->OnNameResolved(Self->m_Hostname, IP);
+ m_Callbacks->OnNameResolved(m_Hostname, IP);
HasResolved = true;
} // for (a_Addr)
// If only unsupported families were reported, call the Error handler:
if (!HasResolved)
{
- Self->m_Callbacks->OnError(DNS_ERR_NODATA, "The name does not resolve to any known address.");
+ m_Callbacks->OnError(EAI_NODATA, ErrorString(EAI_NODATA));
}
else
{
- Self->m_Callbacks->OnFinished();
+ m_Callbacks->OnFinished();
}
- evutil_freeaddrinfo(OrigAddr);
- cNetworkSingleton::Get().RemoveHostnameLookup(Self);
+ freeaddrinfo(OrigAddr);
}
@@ -125,9 +127,7 @@ bool cNetwork::HostnameToIP(
cNetwork::cResolveNameCallbacksPtr a_Callbacks
)
{
- auto Lookup = std::make_shared<cHostnameLookup>(a_Callbacks);
- cNetworkSingleton::Get().AddHostnameLookup(Lookup);
- Lookup->Lookup(a_Hostname);
+ cHostnameLookup::Lookup(a_Hostname, std::move(a_Callbacks));
return true;
}