diff --git a/receiver/src/receiver_config.cpp b/receiver/src/receiver_config.cpp index db98730a453b0d3857092e6536fd03b1b74f93d7..11fa68359f996773eb86a58fc620cf46cdd77c39 100644 --- a/receiver/src/receiver_config.cpp +++ b/receiver/src/receiver_config.cpp @@ -4,7 +4,10 @@ #include <iostream> #include <unistd.h> +#include <limits.h> #include <netdb.h> +#include <string.h> +#include <arpa/inet.h> namespace asapo { @@ -67,19 +70,24 @@ Error ReceiverConfigManager::ReadConfigFromFile(std::string file_name) { } // try to discover an IB address. It should be resolved by <hostname>-ib.desy.de, if exists - char hostname[250]; - gethostname(hostname, 250); - std::string hostnamestr {hostname}; - auto pos = hostnamestr.find('.'); - if (pos != std::string::npos) { - hostnamestr = hostnamestr.substr(0, pos) + "-ib" + hostnamestr.substr(pos); + char hostname[HOST_NAME_MAX + 1]; + char hostname_ib[HOST_NAME_MAX + 4]; + gethostname(hostname, HOST_NAME_MAX); + auto pos = strchr(hostname, '.'); + if (pos != NULL) { + memmove(pos+3, pos, strlen(pos)); + sprintf(hostname_ib, "%.*s-ib%s", (int) (pos - hostname), hostname, pos + 3); } else { - hostnamestr += "-ib"; + sprintf(hostname_ib, "%s-ib", hostname); } - auto record = gethostbyname(hostnamestr.c_str()); - if (record != nullptr && record->h_addr_list != nullptr) { - pos = config.dataserver.advertise_uri.find(':'); - config.dataserver.advertise_ib_uri = record->h_addr_list[0] + config.dataserver.advertise_uri.substr(pos); + + struct addrinfo *result; + if (getaddrinfo(hostname_ib, NULL, NULL, &result) == 0) { + inet_ntop(AF_INET, &((struct sockaddr_in *)result->ai_addr)->sin_addr, hostname_ib, INET_ADDRSTRLEN); + auto strpos = config.dataserver.advertise_uri.find(':'); + config.dataserver.advertise_ib_uri = hostname_ib + config.dataserver.advertise_uri.substr(strpos); + + freeaddrinfo(result); } config.dataserver.tag = config.tag + "_ds";