Documented and improved DNS::Resolve. Readme has been adjusted to changes.

This commit is contained in:
Arron David Nelson 2024-02-01 17:24:38 -08:00
parent beaa7cb034
commit e48a3fd750
4 changed files with 14 additions and 37 deletions

View File

@ -71,24 +71,6 @@ Wayland Window System. Wayland support is currently not fully supported yet, use
#include <ehs/EHS.h>
#include <ehs/io/Console.h>
void LogRaised(const ehs::Log& log)
{
ehs::Array<ehs::Str_8> tags = log.GetTags(); // Retrieves the tags from the log such as line number, function, etc...
ehs::Str_8 result = "{";
for (ehs::UInt_32 i = 0; i < tags.Size(); ++i)
{
result += tags[i];
if (i != tags.Size() - 1)
result += ", ";
}
result += "} (" + ehs::Str_8::FromNum(log.GetCode()) + "): " + log.GetMsg(); // Adds the error code and message from the log.
ehs::Console::Write_8(result);
}
ehs::Int_32 Main(ehs::Str_8* appName, ehs::Str_8* appVerId, ehs::Version* appVer)
{
// Simple identifying meta-data for the logger.
@ -96,10 +78,6 @@ ehs::Int_32 Main(ehs::Str_8* appName, ehs::Str_8* appVerId, ehs::Version* appVer
*appVerId = "Release"; // The app's version prefix; i.e. Alpha, Beta or Release as an example.
*appVer = {1, 0, 0}; // The app's version major, minor and patch number.
ehs::Console::Attach(); // Attach to the console.
ehs::Log::SetCallback(LogRaised); // Sets the log callback function for outputting the information to console.
ehs::Console::Write_8("How old are you?"); // Write to the console in UTF_8 character encoding.
ehs::Str_8 response = ehs::Console::Read_8(); // Read from the console in UTF_8 character encoding.

View File

@ -9,6 +9,9 @@ namespace ehs
class DNS
{
public:
static Str_8 Resolve(const AddrType addrType, const Str_8& hostName);
/// Resolves a hostname to an ip address.
/// @param [in] hostname The given hostname to resolve.
/// @returns The resulting ip address.
static Str_8 Resolve(const Str_8& hostname);
};
}

View File

@ -8,8 +8,6 @@ ehs::Int_32 Main(ehs::Str_8* appName, ehs::Str_8* appVerId, ehs::Version* appVer
*appVerId = "Release";
*appVer = {1, 0, 0};
ehs::Console::Attach();
ehs::Vector<ehs::Str_8> args = ehs::Console::GetArgs_8();
if (args.Size() > 1)

View File

@ -12,7 +12,7 @@
namespace ehs
{
Str_8 DNS::Resolve(const AddrType addrType, const Str_8& hostName)
Str_8 DNS::Resolve(const Str_8& hostname)
{
#if defined(EHS_OS_WINDOWS)
WSADATA data = {};
@ -25,15 +25,9 @@ namespace ehs
}
#endif
addrinfo hints = {};
if (addrType == AddrType::IPV6)
hints.ai_family = AF_INET6;
else if (addrType == AddrType::IPV4)
hints.ai_family = AF_INET;
addrinfo* result = nullptr;
Int_32 code = getaddrinfo(hostName, nullptr, &hints, &result);
Int_32 code = getaddrinfo(hostname, nullptr, nullptr, &result);
if (code)
{
EHS_LOG_INT("Error", 1, "Failed to resolve host with error #" + Str_8::FromNum(code) + ".");
@ -49,22 +43,26 @@ namespace ehs
#endif
if (addrType == AddrType::IPV6)
if (result->ai_family == AF_INET6)
{
Char_8 ipResult[INET6_ADDRSTRLEN];
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 (addrType == AddrType::IPV4)
else if (result->ai_family == AF_INET)
{
Char_8 ipResult[INET_ADDRSTRLEN];
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;