From e48a3fd750ff64184a92270209228836a598d567 Mon Sep 17 00:00:00 2001 From: karutoh Date: Thu, 1 Feb 2024 17:24:38 -0800 Subject: [PATCH] Documented and improved DNS::Resolve. Readme has been adjusted to changes. --- README.md | 22 ---------------------- include/ehs/io/socket/DNS.h | 5 ++++- src/StrToHash.cpp | 2 -- src/io/socket/DNS.cpp | 22 ++++++++++------------ 4 files changed, 14 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index 683a3ba..d6c54d3 100644 --- a/README.md +++ b/README.md @@ -71,24 +71,6 @@ Wayland Window System. Wayland support is currently not fully supported yet, use #include #include -void LogRaised(const ehs::Log& log) -{ - ehs::Array 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. diff --git a/include/ehs/io/socket/DNS.h b/include/ehs/io/socket/DNS.h index 2e670e7..3f7d3e9 100644 --- a/include/ehs/io/socket/DNS.h +++ b/include/ehs/io/socket/DNS.h @@ -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); }; } \ No newline at end of file diff --git a/src/StrToHash.cpp b/src/StrToHash.cpp index b895485..adc9fee 100644 --- a/src/StrToHash.cpp +++ b/src/StrToHash.cpp @@ -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 args = ehs::Console::GetArgs_8(); if (args.Size() > 1) diff --git a/src/io/socket/DNS.cpp b/src/io/socket/DNS.cpp index d54d521..6834e07 100644 --- a/src/io/socket/DNS.cpp +++ b/src/io/socket/DNS.cpp @@ -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;