summaryrefslogtreecommitdiffstats
path: root/src/OSSupport/HostnameLookup.cpp
diff options
context:
space:
mode:
authorMattes D <github@xoft.cz>2015-01-18 11:57:16 +0100
committerMattes D <github@xoft.cz>2015-01-22 20:13:02 +0100
commitc0cb787c101725a649d26de68fca2632c82830ba (patch)
treec5efafa98e7ccfa628f5873e5611b49ff0b44d21 /src/OSSupport/HostnameLookup.cpp
parentcNetwork: Fixed WinXP dualstack listening. (diff)
downloadcuberite-c0cb787c101725a649d26de68fca2632c82830ba.tar
cuberite-c0cb787c101725a649d26de68fca2632c82830ba.tar.gz
cuberite-c0cb787c101725a649d26de68fca2632c82830ba.tar.bz2
cuberite-c0cb787c101725a649d26de68fca2632c82830ba.tar.lz
cuberite-c0cb787c101725a649d26de68fca2632c82830ba.tar.xz
cuberite-c0cb787c101725a649d26de68fca2632c82830ba.tar.zst
cuberite-c0cb787c101725a649d26de68fca2632c82830ba.zip
Diffstat (limited to '')
-rw-r--r--src/OSSupport/HostnameLookup.cpp109
1 files changed, 109 insertions, 0 deletions
diff --git a/src/OSSupport/HostnameLookup.cpp b/src/OSSupport/HostnameLookup.cpp
new file mode 100644
index 000000000..9e35f7163
--- /dev/null
+++ b/src/OSSupport/HostnameLookup.cpp
@@ -0,0 +1,109 @@
+
+// HostnameLookup.cpp
+
+// Implements the cHostnameLookup class representing an in-progress hostname-to-IP lookup
+
+#include "Globals.h"
+#include "HostnameLookup.h"
+#include <event2/dns.h>
+#include "NetworkSingleton.h"
+
+
+
+
+
+////////////////////////////////////////////////////////////////////////////////
+// cHostnameLookup:
+
+cHostnameLookup::cHostnameLookup(const AString & a_Hostname, cNetwork::cResolveNameCallbacksPtr a_Callbacks):
+ m_Callbacks(a_Callbacks),
+ m_Hostname(a_Hostname)
+{
+ 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);
+}
+
+
+
+
+
+void cHostnameLookup::Callback(int a_ErrCode, evutil_addrinfo * a_Addr, void * a_Self)
+{
+ // 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);
+ cNetworkSingleton::Get().RemoveHostnameLookup(Self);
+ return;
+ }
+
+ // Call the success handler for each entry received:
+ bool HasResolved = false;
+ evutil_addrinfo * OrigAddr = a_Addr;
+ for (;a_Addr != nullptr; a_Addr = a_Addr->ai_next)
+ {
+ char IP[128];
+ switch (a_Addr->ai_family)
+ {
+ case AF_INET: // IPv4
+ {
+ sockaddr_in * sin = reinterpret_cast<sockaddr_in *>(a_Addr->ai_addr);
+ evutil_inet_ntop(AF_INET, &(sin->sin_addr), IP, sizeof(IP));
+ break;
+ }
+ case AF_INET6: // IPv6
+ {
+ sockaddr_in6 * sin = reinterpret_cast<sockaddr_in6 *>(a_Addr->ai_addr);
+ evutil_inet_ntop(AF_INET6, &(sin->sin6_addr), IP, sizeof(IP));
+ break;
+ }
+ default:
+ {
+ // Unknown address family, handle as if this entry wasn't received
+ continue; // for (a_Addr)
+ }
+ }
+ Self->m_Callbacks->OnNameResolved(Self->m_Hostname, IP);
+ HasResolved = true;
+ } // for (a_Addr)
+
+ // If only unsupported families were reported, call the Error handler:
+ if (!HasResolved)
+ {
+ Self->m_Callbacks->OnError(1);
+ }
+ else
+ {
+ Self->m_Callbacks->OnFinished();
+ }
+ evutil_freeaddrinfo(OrigAddr);
+ cNetworkSingleton::Get().RemoveHostnameLookup(Self);
+}
+
+
+
+
+
+////////////////////////////////////////////////////////////////////////////////
+// cNetwork API:
+
+bool cNetwork::HostnameToIP(
+ const AString & a_Hostname,
+ cNetwork::cResolveNameCallbacksPtr a_Callbacks
+)
+{
+ return cNetworkSingleton::Get().HostnameToIP(a_Hostname, a_Callbacks);
+}
+
+
+
+