From f8cf24a61c7d76ef082e083865b7606336e96589 Mon Sep 17 00:00:00 2001
From: George Sedov <george.sedov@desy.de>
Date: Fri, 21 Oct 2022 15:33:56 +0200
Subject: [PATCH] change the way the IB IP is found

---
 receiver/src/receiver_config.cpp | 30 +++++++++++++++++++-----------
 1 file changed, 19 insertions(+), 11 deletions(-)

diff --git a/receiver/src/receiver_config.cpp b/receiver/src/receiver_config.cpp
index db98730a4..11fa68359 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";
-- 
GitLab