]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Fix compilation on systems that do not define HOST_NAME_MAX 9127/head
authorRemi Gacogne <remi.gacogne@powerdns.com>
Tue, 2 Jun 2020 10:24:34 +0000 (12:24 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Tue, 2 Jun 2020 10:24:34 +0000 (12:24 +0200)
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
pdns/dnsdist-carbon.cc
pdns/misc.cc
pdns/misc.hh
pdns/rec-carbon.cc

index 5fd968c82cb55095b9516d3009988d3226a07803..3f839d2b6bd2a7c907264de4291b7b4c11b83635 100644 (file)
@@ -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"];
 
index c5c1381d0b5ac09dcfe3052a32663919304c25a1..6f8bc64a554b1dad88ab7e4d93767c47cf5a3b3f 100644 (file)
@@ -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;
 
index fb253e5f1c40d7e5563e36b827c2f201818c664b..0215e8e42c5f9500446ed8e45cbf953ada6fe2eb 100644 (file)
@@ -57,6 +57,7 @@
 #include <sys/types.h>
 #include <pwd.h>
 #include <grp.h>
+#include <limits.h>
 #ifdef __FreeBSD__
 #  include <pthread_np.h>
 #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<char*>(hostname.c_str()), hostname.size()) != 0) {
+    throw std::runtime_error(stringerror());
+  }
+
+  boost::replace_all(hostname, ".", "_");
+
+  return hostname;
+}
index 1db0f5ce31c081005a4cc51ca15ad3b912a3af9c..a1bc98ba29ec5b506ad4e6d3db2a4220c3527087 100644 (file)
@@ -605,3 +605,5 @@ int mapThreadToCPUList(pthread_t tid, const std::set<int>& cpus);
 std::vector<ComboAddress> getResolvers(const std::string& resolvConfPath);
 
 DNSName reverseNameFromIP(const ComboAddress& ip);
+
+std::string getCarbonHostName();
index ef1204d6ec1d3231742bf7df74f6835143a64b8e..c03bae308214a1f64a6734782ccc7c2d1a6eaa6e 100644 (file)
@@ -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";