summaryrefslogtreecommitdiffstats
path: root/src/OSSupport/IPLookup.cpp
diff options
context:
space:
mode:
authorHowaner <franzi.moos@googlemail.com>2015-01-25 00:57:20 +0100
committerHowaner <franzi.moos@googlemail.com>2015-01-25 00:57:20 +0100
commit6678863f7f7c0fd29bc81e13144f0f054b0162eb (patch)
treea2ce3bb46e396f50f4742711864d9e0327df6457 /src/OSSupport/IPLookup.cpp
parentAdded sponge. (diff)
parentGamosocm support (diff)
downloadcuberite-6678863f7f7c0fd29bc81e13144f0f054b0162eb.tar
cuberite-6678863f7f7c0fd29bc81e13144f0f054b0162eb.tar.gz
cuberite-6678863f7f7c0fd29bc81e13144f0f054b0162eb.tar.bz2
cuberite-6678863f7f7c0fd29bc81e13144f0f054b0162eb.tar.lz
cuberite-6678863f7f7c0fd29bc81e13144f0f054b0162eb.tar.xz
cuberite-6678863f7f7c0fd29bc81e13144f0f054b0162eb.tar.zst
cuberite-6678863f7f7c0fd29bc81e13144f0f054b0162eb.zip
Diffstat (limited to 'src/OSSupport/IPLookup.cpp')
-rw-r--r--src/OSSupport/IPLookup.cpp111
1 files changed, 111 insertions, 0 deletions
diff --git a/src/OSSupport/IPLookup.cpp b/src/OSSupport/IPLookup.cpp
new file mode 100644
index 000000000..8cdc5132d
--- /dev/null
+++ b/src/OSSupport/IPLookup.cpp
@@ -0,0 +1,111 @@
+
+// IPLookup.cpp
+
+// Implements the cIPLookup class representing an IP-to-hostname lookup in progress.
+
+#include "Globals.h"
+#include "IPLookup.h"
+#include <event2/dns.h>
+#include "NetworkSingleton.h"
+
+
+
+
+
+////////////////////////////////////////////////////////////////////////////////
+// cIPLookup:
+
+cIPLookup::cIPLookup(cNetwork::cResolveNameCallbacksPtr a_Callbacks):
+ m_Callbacks(a_Callbacks)
+{
+ ASSERT(a_Callbacks != nullptr);
+}
+
+
+
+
+
+bool cIPLookup::Lookup(const AString & a_IP)
+{
+ // 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;
+ }
+
+ // 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)
+ {
+ 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:
+ {
+ LOGWARNING("%s: Unknown address family: %d", __FUNCTION__, sa.ss_family);
+ ASSERT(!"Unknown address family");
+ return false;
+ }
+ } // switch (address family)
+ return true;
+}
+
+
+
+
+
+void cIPLookup::Callback(int a_Result, char a_Type, int a_Count, int a_Ttl, void * a_Addresses, void * a_Self)
+{
+ // 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))
+ {
+ // An error has occurred, notify the error callback:
+ Self->m_Callbacks->OnError(a_Result, evutil_socket_error_to_string(a_Result));
+ }
+ else
+ {
+ // Call the success handler:
+ Self->m_Callbacks->OnNameResolved(*(reinterpret_cast<char **>(a_Addresses)), Self->m_IP);
+ Self->m_Callbacks->OnFinished();
+ }
+ cNetworkSingleton::Get().RemoveIPLookup(Self);
+}
+
+
+
+
+
+////////////////////////////////////////////////////////////////////////////////
+// cNetwork API:
+
+bool cNetwork::IPToHostName(
+ const AString & a_IP,
+ cNetwork::cResolveNameCallbacksPtr a_Callbacks
+)
+{
+ auto res = std::make_shared<cIPLookup>(a_Callbacks);
+ cNetworkSingleton::Get().AddIPLookup(res);
+ return res->Lookup(a_IP);
+}
+
+
+
+