diff options
author | Mattes D <github@xoft.cz> | 2015-01-18 11:57:16 +0100 |
---|---|---|
committer | Mattes D <github@xoft.cz> | 2015-01-22 20:13:02 +0100 |
commit | c0cb787c101725a649d26de68fca2632c82830ba (patch) | |
tree | c5efafa98e7ccfa628f5873e5611b49ff0b44d21 /src/OSSupport/IPLookup.cpp | |
parent | cNetwork: Fixed WinXP dualstack listening. (diff) | |
download | cuberite-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 'src/OSSupport/IPLookup.cpp')
-rw-r--r-- | src/OSSupport/IPLookup.cpp | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/src/OSSupport/IPLookup.cpp b/src/OSSupport/IPLookup.cpp new file mode 100644 index 000000000..bbcfbfe40 --- /dev/null +++ b/src/OSSupport/IPLookup.cpp @@ -0,0 +1,91 @@ + +// 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(const AString & a_IP, cNetwork::cResolveNameCallbacksPtr a_Callbacks): + m_Callbacks(a_Callbacks), + m_IP(a_IP) +{ + sockaddr_storage sa; + int salen = static_cast<int>(sizeof(sa)); + memset(&sa, 0, sizeof(sa)); + evutil_parse_sockaddr_port(a_IP.c_str(), reinterpret_cast<sockaddr *>(&sa), &salen); + 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"); + break; + } + } // switch (address family) +} + + + + + +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); + } + 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 +) +{ + return cNetworkSingleton::Get().IPToHostName(a_IP, a_Callbacks); +} + + + + |