Commit 10d09d55 authored by aligungr's avatar aligungr

GetIp4 improvements for domain improvements

parent dd821784
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include <netdb.h>
#include <utils/constants.hpp> #include <utils/constants.hpp>
#include <utils/libc_error.hpp> #include <utils/libc_error.hpp>
...@@ -218,4 +219,38 @@ std::string GetIp4OfInterface(const std::string &ifName) ...@@ -218,4 +219,38 @@ std::string GetIp4OfInterface(const std::string &ifName)
return std::string{str}; return std::string{str};
} }
std::string GetHostByName(const std::string &name)
{
struct addrinfo hints = {};
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags |= AI_CANONNAME;
auto* res = gethostbyname(name.c_str());
if (res == nullptr)
return "";
if (res->h_addr_list == nullptr)
return "";
if (res->h_addrtype == AF_INET)
{
char str[INET_ADDRSTRLEN] = {0};
if (inet_ntop(AF_INET, res->h_addr_list[0], str, INET_ADDRSTRLEN) == nullptr)
return "";
return std::string{str};
}
else if (res->h_addrtype == AF_INET)
{
char str[INET6_ADDRSTRLEN] = {0};
if (inet_ntop(AF_INET6, res->h_addr_list[0], str, INET6_ADDRSTRLEN) == nullptr)
return "";
return std::string{str};
}
else
{
return "";
}
}
} // namespace io } // namespace io
...@@ -42,4 +42,6 @@ void AppendPath(std::string &source, const std::string &target); ...@@ -42,4 +42,6 @@ void AppendPath(std::string &source, const std::string &target);
std::string GetIp4OfInterface(const std::string &ifName); std::string GetIp4OfInterface(const std::string &ifName);
std::string GetHostByName(const std::string& name);
} // namespace io } // namespace io
...@@ -156,7 +156,10 @@ std::string GetIp4(const YAML::Node &node, const std::string &name) ...@@ -156,7 +156,10 @@ std::string GetIp4(const YAML::Node &node, const std::string &name)
if (ipFromIf.empty()) if (ipFromIf.empty())
FieldError(name, "must be a valid IPv4 address or a valid network interface with a IPv4 address"); FieldError(name, "must be a valid IPv4 address or a valid network interface with a IPv4 address");
return ipFromIf; auto n = io::GetHostByName(name);
if (utils::GetIpVersion(n) == 4)
return n;
return "";
} }
void AssertHasBool(const YAML::Node &node, const std::string &name) void AssertHasBool(const YAML::Node &node, const std::string &name)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment