Skip to content

Commit

Permalink
Fix compilation on systems that do not define HOST_NAME_MAX
Browse files Browse the repository at this point in the history
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!).
  • Loading branch information
rgacogne committed Jun 2, 2020
1 parent 7c2b319 commit 64d3823
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 28 deletions.
15 changes: 6 additions & 9 deletions pdns/auth-carbon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"];

Expand Down
15 changes: 6 additions & 9 deletions pdns/dnsdist-carbon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
32 changes: 32 additions & 0 deletions pdns/misc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include <limits.h>
#ifdef __FreeBSD__
# include <pthread_np.h>
#endif
Expand Down Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions pdns/misc.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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();
16 changes: 6 additions & 10 deletions pdns/rec-carbon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit 64d3823

Please sign in to comment.