From 3c5aeccd53fdb776bbf814cf0af8f5df7a2cebf0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Thu, 27 May 2021 13:06:28 -0400
Subject: [PATCH] dhtrunner: don't read port config if already configured

---
 src/dhtrunner.cpp | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/src/dhtrunner.cpp b/src/dhtrunner.cpp
index 576d3304..c3d440d3 100644
--- a/src/dhtrunner.cpp
+++ b/src/dhtrunner.cpp
@@ -119,22 +119,24 @@ DhtRunner::run(const Config& config, Context&& context)
         local6.setFamily(AF_INET6);
     }
     auto state_path = config.dht_config.node_config.persist_path;
-    if (not state_path.empty()) {
+    if (not state_path.empty() && (local4.getPort() == 0 || local6.getPort() == 0)) {
         state_path += "_port.txt";
         std::ifstream inConfig(state_path);
         if (inConfig.is_open()) {
             in_port_t port;
             if (inConfig >> port) {
-                if (context.logger)
-                    context.logger->d("[runner %p] Using IPv4 port %hu from saved configuration", this, port);
-                if (local4.getPort() == 0)
+                if (local4.getPort() == 0) {
+                    if (context.logger)
+                        context.logger->d("[runner %p] Using IPv4 port %hu from saved configuration", this, port);
                     local4.setPort(port);
+                }
             }
             if (inConfig >> port) {
-                if (context.logger)
-                    context.logger->d("[runner %p] Using IPv6 port %hu from saved configuration", this, port);
-                if (local6.getPort() == 0)
+                if (local6.getPort() == 0) {
+                    if (context.logger)
+                        context.logger->d("[runner %p] Using IPv6 port %hu from saved configuration", this, port);
                     local6.setPort(port);
+                }
             }
         }
     }
-- 
GitLab