Unverified Commit b0aef086 authored by Ali Güngör's avatar Ali Güngör Committed by GitHub

Merge pull request #458 from abousselmi/master

Allow using FQDN along with IPv4 and IPv6
parents b8c86919 38a8b5e5
...@@ -252,33 +252,38 @@ std::string GetIp6OfInterface(const std::string &ifName) ...@@ -252,33 +252,38 @@ std::string GetIp6OfInterface(const std::string &ifName)
std::string GetHostByName(const std::string &name) std::string GetHostByName(const std::string &name)
{ {
struct addrinfo hints = {}; struct addrinfo hints = {};
struct addrinfo *res;
hints.ai_family = PF_UNSPEC; hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags |= AI_CANONNAME;
auto* res = gethostbyname(name.c_str()); if (getaddrinfo(name.c_str(), NULL, &hints, &res))
if (res == nullptr)
return "";
if (res->h_addr_list == nullptr)
return ""; return "";
if (res->h_addrtype == AF_INET) if (res->ai_family == AF_INET)
{ {
char str[INET_ADDRSTRLEN] = {0}; char str[INET_ADDRSTRLEN] = {0};
if (inet_ntop(AF_INET, res->h_addr_list[0], str, INET_ADDRSTRLEN) == nullptr) if (inet_ntop(AF_INET, &((struct sockaddr_in*)res->ai_addr)->sin_addr, str, INET_ADDRSTRLEN) == nullptr)
{
freeaddrinfo(res);
return ""; return "";
}
freeaddrinfo(res);
return std::string{str}; return std::string{str};
} }
else if (res->h_addrtype == AF_INET) else if (res->ai_family == AF_INET6)
{ {
char str[INET6_ADDRSTRLEN] = {0}; char str[INET6_ADDRSTRLEN] = {0};
if (inet_ntop(AF_INET6, res->h_addr_list[0], str, INET6_ADDRSTRLEN) == nullptr) if (inet_ntop(AF_INET6, &((struct sockaddr_in6*)res->ai_addr)->sin6_addr, str, INET6_ADDRSTRLEN) == nullptr)
{
freeaddrinfo(res);
return ""; return "";
}
freeaddrinfo(res);
return std::string{str}; return std::string{str};
} }
else else
{ {
freeaddrinfo(res);
return ""; return "";
} }
} }
......
...@@ -146,15 +146,17 @@ std::string GetIp4(const YAML::Node &node, const std::string &name) ...@@ -146,15 +146,17 @@ std::string GetIp4(const YAML::Node &node, const std::string &name)
{ {
std::string s = GetString(node, name); std::string s = GetString(node, name);
s = io::GetHostByName(s);
int version = utils::GetIpVersion(s); int version = utils::GetIpVersion(s);
if (version == 6) if (version == 6)
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, FQDN or a valid network interface with a IPv4 address");
if (version == 4) if (version == 4)
return s; return s;
auto ipFromIf = io::GetIp4OfInterface(s); auto ipFromIf = io::GetIp4OfInterface(s);
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, FQDN or a valid network interface with a IPv4 address");
return ipFromIf; return ipFromIf;
} }
...@@ -162,15 +164,17 @@ std::string GetIp6(const YAML::Node &node, const std::string &name) ...@@ -162,15 +164,17 @@ std::string GetIp6(const YAML::Node &node, const std::string &name)
{ {
std::string s = GetString(node, name); std::string s = GetString(node, name);
s = io::GetHostByName(s);
int version = utils::GetIpVersion(s); int version = utils::GetIpVersion(s);
if (version == 4) if (version == 4)
FieldError(name, "must be a valid IPv6 address or a valid network interface with a IPv6 address"); FieldError(name, "must be a valid IPv6 address, FQDN or a valid network interface with a IPv6 address");
if (version == 6) if (version == 6)
return s; return s;
auto ipFromIf = io::GetIp6OfInterface(s); auto ipFromIf = io::GetIp6OfInterface(s);
if (ipFromIf.empty()) if (ipFromIf.empty())
FieldError(name, "must be a valid IPv6 address or a valid network interface with a IPv6 address"); FieldError(name, "must be a valid IPv6 address, FQDN or a valid network interface with a IPv6 address");
return ipFromIf; return ipFromIf;
} }
...@@ -178,6 +182,8 @@ std::string GetIp(const YAML::Node &node, const std::string & name) ...@@ -178,6 +182,8 @@ std::string GetIp(const YAML::Node &node, const std::string & name)
{ {
std::string s = GetString(node, name); std::string s = GetString(node, name);
s = io::GetHostByName(s);
int version = utils::GetIpVersion(s); int version = utils::GetIpVersion(s);
if (version == 6 || version == 4) if (version == 6 || version == 4)
return s; return s;
...@@ -187,7 +193,7 @@ std::string GetIp(const YAML::Node &node, const std::string & name) ...@@ -187,7 +193,7 @@ std::string GetIp(const YAML::Node &node, const std::string & name)
auto ip6FromIf = io::GetIp6OfInterface(s); auto ip6FromIf = io::GetIp6OfInterface(s);
if (!ip6FromIf.empty()) if (!ip6FromIf.empty())
return ip6FromIf; return ip6FromIf;
FieldError(name, "must be a valid IP address or a valid network interface with an IP address"); FieldError(name, "must be a valid IP address, FQDN or a valid network interface with an IP address");
} }
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