forked from vmangos/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add IO::Networking::DNS::ResolveDomain (vmangos#2722)
- Loading branch information
Showing
8 changed files
with
123 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include "./DNS.h" | ||
#include "./Internal.h" | ||
#include "Log.h" | ||
#include "Errors.h" | ||
#include "IO/SystemErrorToString.h" | ||
|
||
#if defined(WIN32) | ||
#include <WinSock2.h> | ||
#include <ws2tcpip.h> | ||
#endif | ||
|
||
std::string IO::Networking::DNS::GetOwnHostname() | ||
{ | ||
char hostname[1024]; | ||
if (::gethostname(hostname, sizeof(hostname)) == -1) | ||
{ | ||
sLog.Out(LogType::LOG_NETWORK, LOG_LVL_ERROR, "IO ERROR: ::gethostname(...): %s", SystemErrorToString(errno).c_str()); | ||
MANGOS_ASSERT(false); | ||
} | ||
return hostname; | ||
} | ||
|
||
std::vector<IO::Networking::IpAddress> IO::Networking::DNS::ResolveDomain(std::string const& domainName, IO::Networking::IpAddress::Type type) | ||
{ | ||
MANGOS_ASSERT(type == IpAddress::Type::IPv4); | ||
|
||
addrinfo hints = {}; | ||
hints.ai_family = type == IpAddress::Type::IPv4 ? AF_INET : AF_INET6; | ||
hints.ai_socktype = SOCK_STREAM; | ||
hints.ai_protocol = IPPROTO_TCP; | ||
|
||
addrinfo* dnsResult = nullptr; | ||
if (::getaddrinfo(domainName.c_str(), nullptr, &hints, &dnsResult) != 0) | ||
{ | ||
sLog.Out(LogType::LOG_NETWORK, LOG_LVL_ERROR, "IO ERROR: ::getaddrinfo(...): %s", SystemErrorToString(errno).c_str()); | ||
MANGOS_ASSERT(false); | ||
} | ||
|
||
std::vector<IO::Networking::IpAddress> list; | ||
|
||
for (addrinfo* ptr = dnsResult; ptr != nullptr; ptr = ptr->ai_next) | ||
{ | ||
sockaddr_in* sockaddr_ipv4 = reinterpret_cast<sockaddr_in*>(ptr->ai_addr); | ||
IpAddress ip = IO::Networking::Internal::inet_ntop(&(sockaddr_ipv4->sin_addr)); | ||
list.emplace_back(ip); | ||
} | ||
|
||
freeaddrinfo(dnsResult); | ||
|
||
return list; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#ifndef MANGOS_IO_NETWORKING_DNS_H | ||
#define MANGOS_IO_NETWORKING_DNS_H | ||
|
||
#include <vector> | ||
#include <experimental/vector> | ||
#include "./IpAddress.h" | ||
|
||
namespace IO { namespace Networking { namespace DNS | ||
{ | ||
std::string GetOwnHostname(); | ||
std::vector<IO::Networking::IpAddress> ResolveDomain(std::string const& domainName, IO::Networking::IpAddress::Type type); | ||
}}} // namespace IO::Networking | ||
|
||
#endif // MANGOS_IO_NETWORKING_DNS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include "Errors.h" | ||
#include "./Internal.h" | ||
|
||
/// Converts a native IN_ADDR to a IO::Networking::IpAddress | ||
IO::Networking::IpAddress IO::Networking::Internal::inet_ntop(in_addr const* nativeAddress) | ||
{ | ||
#if defined(WIN32) | ||
// We cant use ::inet_ntoa(...) because it's not thread safe. We cant use ::inet_ntop(...) because it's not WinXP compatible, so we have to do it ourselves. | ||
int constexpr MAX_IPV4_LENGTH = 16; // "255.255.255.255" = length 15 + 1 for null-terminator | ||
char ipv4AddressString[MAX_IPV4_LENGTH]; | ||
{ // impl was taken from ACE, should be universal | ||
uint8_t const* p = reinterpret_cast<uint8_t const*>(nativeAddress); | ||
snprintf(ipv4AddressString, MAX_IPV4_LENGTH, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]); | ||
} | ||
auto ipAddress = IO::Networking::IpAddress::TryParseFromString(ipv4AddressString); | ||
#else | ||
char ipv4AddressString[INET_ADDRSTRLEN]; | ||
::inet_ntop(AF_INET, nativeAddress, ipv4AddressString, INET_ADDRSTRLEN); | ||
auto ipAddress = IO::Networking::IpAddress::TryParseFromString(ipv4AddressString); | ||
#endif | ||
MANGOS_ASSERT(ipAddress.has_value()); // this should never fail, since we got a valid IP from IN_ADDR | ||
return ipAddress.value(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#ifndef MANGOS_IO_NETWORKING_INTERNAL_H | ||
#define MANGOS_IO_NETWORKING_INTERNAL_H | ||
|
||
#include "./IpAddress.h" | ||
|
||
struct in_addr; | ||
|
||
namespace IO { namespace Networking { namespace Internal | ||
{ | ||
/// Converts a native IN_ADDR to a IO::Networking::IpAddress | ||
IO::Networking::IpAddress inet_ntop(in_addr const* nativeAddress); | ||
|
||
}}} // IO::Networking::Internal | ||
|
||
#endif // MANGOS_IO_NETWORKING_INTERNAL_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters