From 64d382318f6eacfd3dd96d470cad353d31457752 Mon Sep 17 00:00:00 2001 From: Remi Gacogne Date: Tue, 2 Jun 2020 12:24:34 +0200 Subject: [PATCH] Fix compilation on systems that do not define HOST_NAME_MAX On FreeBSD at least, HOST_NAME_MAX is not defined and we need to use sysconf() to get the value at runtime instead. Based on a work done by @RvdE to make the recursor compile on FreeBSD (many thanks!). --- pdns/auth-carbon.cc | 15 ++++++--------- pdns/dnsdist-carbon.cc | 15 ++++++--------- pdns/misc.cc | 32 ++++++++++++++++++++++++++++++++ pdns/misc.hh | 2 ++ pdns/rec-carbon.cc | 16 ++++++---------- 5 files changed, 52 insertions(+), 28 deletions(-) diff --git a/pdns/auth-carbon.cc b/pdns/auth-carbon.cc index 5fd968c82c..3f839d2b6b 100644 --- a/pdns/auth-carbon.cc +++ b/pdns/auth-carbon.cc @@ -40,16 +40,13 @@ try string namespace_name=arg()["carbon-namespace"]; string hostname=arg()["carbon-ourname"]; - if(hostname.empty()) { - char tmp[HOST_NAME_MAX+1]; - memset(tmp, 0, sizeof(tmp)); - if (gethostname(tmp, sizeof(tmp)) != 0) { - throw std::runtime_error("The carbon-ourname setting has not been set and we are unable to determine the system's hostname: " + stringerror()); + if (hostname.empty()) { + try { + hostname = getCarbonHostName(); + } + catch(const std::exception& e) { + throw std::runtime_error(std::string("The 'carbon-ourname' setting has not been set and we are unable to determine the system's hostname: ") + e.what()); } - char *p = strchr(tmp, '.'); - if(p) *p=0; - hostname=tmp; - boost::replace_all(hostname, ".", "_"); } string instance_name=arg()["carbon-instance"]; diff --git a/pdns/dnsdist-carbon.cc b/pdns/dnsdist-carbon.cc index c5c1381d0b..6f8bc64a55 100644 --- a/pdns/dnsdist-carbon.cc +++ b/pdns/dnsdist-carbon.cc @@ -58,16 +58,13 @@ try const auto& server = conf.server; const std::string& namespace_name = conf.namespace_name; std::string hostname = conf.ourname; - if(hostname.empty()) { - char tmp[HOST_NAME_MAX+1]; - memset(tmp, 0, sizeof(tmp)); - if (gethostname(tmp, sizeof(tmp)) != 0) { - throw std::runtime_error("The 'ourname' setting in 'carbonServer()' has not been set and we are unable to determine the system's hostname: " + stringerror()); + if (hostname.empty()) { + try { + hostname = getCarbonHostName(); + } + catch(const std::exception& e) { + throw std::runtime_error(std::string("The 'ourname' setting in 'carbonServer()' has not been set and we are unable to determine the system's hostname: ") + e.what()); } - char *p = strchr(tmp, '.'); - if(p) *p=0; - hostname=tmp; - boost::replace_all(hostname, ".", "_"); } const std::string& instance_name = conf.instance_name; diff --git a/pdns/misc.cc b/pdns/misc.cc index fb253e5f1c..0215e8e42c 100644 --- a/pdns/misc.cc +++ b/pdns/misc.cc @@ -57,6 +57,7 @@ #include #include #include +#include #ifdef __FreeBSD__ # include #endif @@ -1560,3 +1561,34 @@ DNSName reverseNameFromIP(const ComboAddress& ip) throw std::runtime_error("Calling reverseNameFromIP() for an address which is neither an IPv4 nor an IPv6"); } + +static size_t getMaxHostNameSize() +{ +#if defined(HOST_NAME_MAX) + return HOST_NAME_MAX; +#endif + +#if defined(_SC_HOST_NAME_MAX) + auto tmp = sysconf(_SC_HOST_NAME_MAX); + if (tmp != -1) { + return tmp; + } +#endif + + /* _POSIX_HOST_NAME_MAX */ + return 255; +} + +std::string getCarbonHostName() +{ + std::string hostname; + hostname.resize(getMaxHostNameSize() + 1, 0); + + if (gethostname(const_cast(hostname.c_str()), hostname.size()) != 0) { + throw std::runtime_error(stringerror()); + } + + boost::replace_all(hostname, ".", "_"); + + return hostname; +} diff --git a/pdns/misc.hh b/pdns/misc.hh index 1db0f5ce31..a1bc98ba29 100644 --- a/pdns/misc.hh +++ b/pdns/misc.hh @@ -605,3 +605,5 @@ int mapThreadToCPUList(pthread_t tid, const std::set& cpus); std::vector getResolvers(const std::string& resolvConfPath); DNSName reverseNameFromIP(const ComboAddress& ip); + +std::string getCarbonHostName(); diff --git a/pdns/rec-carbon.cc b/pdns/rec-carbon.cc index ef1204d6ec..c03bae3082 100644 --- a/pdns/rec-carbon.cc +++ b/pdns/rec-carbon.cc @@ -32,17 +32,13 @@ try if(namespace_name.empty()) { namespace_name="pdns"; } - if(hostname.empty()) { - char tmp[HOST_NAME_MAX+1]; - memset(tmp, 0, sizeof(tmp)); - if (gethostname(tmp, sizeof(tmp)) != 0) { - throw std::runtime_error("The 'carbon-ourname' setting has not been set and we are unable to determine the system's hostname: " + stringerror()); + if (hostname.empty()) { + try { + hostname = getCarbonHostName(); + } + catch(const std::exception& e) { + throw std::runtime_error(std::string("The 'carbon-ourname' setting has not been set and we are unable to determine the system's hostname: ") + e.what()); } - char *p = strchr(tmp, '.'); - if(p) *p=0; - - hostname=tmp; - boost::replace_all(hostname, ".", "_"); } if(instance_name.empty()) { instance_name="recursor"; -- 2.39.2