126 lines
2.7 KiB
C++
126 lines
2.7 KiB
C++
#include "ehs/io/socket/DNS_W32.h"
|
|
#include "ehs/Log.h"
|
|
|
|
#include <WinSock2.h>
|
|
#include <WS2tcpip.h>
|
|
|
|
namespace ehs
|
|
{
|
|
Str_8 DNS::Resolve(const IP 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 == IP::V6)
|
|
req.ai_family = AF_INET6;
|
|
else if (type == IP::V4)
|
|
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 {};
|
|
}
|
|
} |