diff options
author | Mattes D <github@xoft.cz> | 2015-01-18 12:35:02 +0100 |
---|---|---|
committer | Mattes D <github@xoft.cz> | 2015-01-22 20:13:02 +0100 |
commit | d4682463a1d503c349ac95e275b11d67d402268c (patch) | |
tree | de3167d2b81eb909a419c1355021353e00e71074 /src/OSSupport/IPLookup.cpp | |
parent | cNetwork: Split the main cpp file into several files. (diff) | |
download | cuberite-d4682463a1d503c349ac95e275b11d67d402268c.tar cuberite-d4682463a1d503c349ac95e275b11d67d402268c.tar.gz cuberite-d4682463a1d503c349ac95e275b11d67d402268c.tar.bz2 cuberite-d4682463a1d503c349ac95e275b11d67d402268c.tar.lz cuberite-d4682463a1d503c349ac95e275b11d67d402268c.tar.xz cuberite-d4682463a1d503c349ac95e275b11d67d402268c.tar.zst cuberite-d4682463a1d503c349ac95e275b11d67d402268c.zip |
Diffstat (limited to '')
-rw-r--r-- | src/OSSupport/IPLookup.cpp | 32 |
1 files changed, 26 insertions, 6 deletions
diff --git a/src/OSSupport/IPLookup.cpp b/src/OSSupport/IPLookup.cpp index bbcfbfe40..a52b6480b 100644 --- a/src/OSSupport/IPLookup.cpp +++ b/src/OSSupport/IPLookup.cpp @@ -15,14 +15,31 @@ //////////////////////////////////////////////////////////////////////////////// // cIPLookup: -cIPLookup::cIPLookup(const AString & a_IP, cNetwork::cResolveNameCallbacksPtr a_Callbacks): - m_Callbacks(a_Callbacks), - m_IP(a_IP) +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)); - evutil_parse_sockaddr_port(a_IP.c_str(), reinterpret_cast<sockaddr *>(&sa), &salen); + 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: @@ -41,9 +58,10 @@ cIPLookup::cIPLookup(const AString & a_IP, cNetwork::cResolveNameCallbacksPtr a_ { LOGWARNING("%s: Unknown address family: %d", __FUNCTION__, sa.ss_family); ASSERT(!"Unknown address family"); - break; + return false; } } // switch (address family) + return true; } @@ -83,7 +101,9 @@ bool cNetwork::IPToHostName( cNetwork::cResolveNameCallbacksPtr a_Callbacks ) { - return cNetworkSingleton::Get().IPToHostName(a_IP, a_Callbacks); + auto res = std::make_shared<cIPLookup>(a_Callbacks); + cNetworkSingleton::Get().AddIPLookup(res); + return res->Lookup(a_IP); } |