Added new DNS::Resolve static method and organized it per OS.

This commit is contained in:
2024-07-09 16:41:43 -07:00
parent 126fc92fae
commit 4e887b1ac2
23 changed files with 545 additions and 96 deletions

122
src/io/socket/DNS_W32.cpp Normal file
View File

@@ -0,0 +1,122 @@
#include "ehs/io/socket/DNS_W32.h"
namespace ehs
{
Str_8 DNS::Resolve(const AddrType type, const Str_8 &hostname)
{
WSADATA data = {};
Int_32 wsaCode = WSAStartup(MAKEWORD(2, 2), &data);
if (wsaCode)
{
EHS_LOG_INT(LogType::ERR, 0, "Failed to start WSA with error #" + Str_8::FromNum(wsaCode) + ".");
return {};
}
addrinfo* result = nullptr;
addrinfo req = {};
if (type == AddrType::IPV6)
req.ai_family = AF_INET6;
else if (type == AddrType::IPV4)
req.ai_family = AF_INET;
Int_32 code = getaddrinfo(hostname, nullptr, &req, &result);
if (code)
{
EHS_LOG_INT(LogType::ERR, 1, "Failed to resolve host with error #" + Str_8::FromNum(code) + ".");
return {};
}
if (WSACleanup() == SOCKET_ERROR)
{
EHS_LOG_INT(LogType::ERR, 2, "Failed to shutdown WSA with error #" + Str_8::FromNum(WSAGetLastError()) + ".");
return {};
}
if (result->ai_family == AF_INET6)
{
Str_8 ipResult(INET6_ADDRSTRLEN);
inet_ntop(result->ai_family, &((sockaddr_in6*)result->ai_addr)->sin6_addr, ipResult, INET6_ADDRSTRLEN);
ipResult.ExactSize();
freeaddrinfo(result);
EHS_LOG_SUCCESS();
return ipResult;
}
else if (result->ai_family == AF_INET)
{
Str_8 ipResult(INET_ADDRSTRLEN);
inet_ntop(result->ai_family, &((sockaddr_in*)result->ai_addr)->sin_addr, ipResult, INET_ADDRSTRLEN);
ipResult.ExactSize();
freeaddrinfo(result);
EHS_LOG_SUCCESS();
return ipResult;
}
return {};
}
Str_8 DNS::Resolve(const Str_8 &hostname)
{
WSADATA data = {};
Int_32 wsaCode = WSAStartup(MAKEWORD(2, 2), &data);
if (wsaCode)
{
EHS_LOG_INT(LogType::ERR, 0, "Failed to start WSA with error #" + Str_8::FromNum(wsaCode) + ".");
return {};
}
addrinfo* result = nullptr;
Int_32 code = getaddrinfo(hostname, nullptr, nullptr, &result);
if (code)
{
EHS_LOG_INT(LogType::ERR, 1, "Failed to resolve host with error #" + Str_8::FromNum(code) + ".");
return {};
}
if (WSACleanup() == SOCKET_ERROR)
{
EHS_LOG_INT(LogType::ERR, 2, "Failed to shutdown WSA with error #" + Str_8::FromNum(WSAGetLastError()) + ".");
return {};
}
if (result->ai_family == AF_INET6)
{
Str_8 ipResult(INET6_ADDRSTRLEN);
inet_ntop(result->ai_family, &((sockaddr_in6*)result->ai_addr)->sin6_addr, ipResult, INET6_ADDRSTRLEN);
ipResult.ExactSize();
freeaddrinfo(result);
return ipResult;
}
else if (result->ai_family == AF_INET)
{
Str_8 ipResult(INET_ADDRSTRLEN);
inet_ntop(result->ai_family, &((sockaddr_in*)result->ai_addr)->sin_addr, ipResult, INET_ADDRSTRLEN);
ipResult.ExactSize();
freeaddrinfo(result);
return ipResult;
}
return {};
}
}