From 8045c2cbf2e1099d71cf8c69819df2dac48053e6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Blin?=
 <sebastien.blin@savoirfairelinux.com>
Date: Mon, 18 Feb 2019 16:45:31 -0500
Subject: [PATCH] dhtrunner: avoid startNetwork failure if only one binding is
 failing

---
 src/dhtrunner.cpp | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)

diff --git a/src/dhtrunner.cpp b/src/dhtrunner.cpp
index b50bbb05..acc45504 100644
--- a/src/dhtrunner.cpp
+++ b/src/dhtrunner.cpp
@@ -521,7 +521,7 @@ DhtRunner::startNetwork(const SockAddr sin4, const SockAddr sin6)
 #ifndef _WIN32
     auto status = pipe(stopfds);
     if (status == -1) {
-        throw DhtException("Can't open pipe");
+        throw DhtException(std::string("Can't open pipe: ") + strerror(errno));
     }
 #else
     udpPipe(stopfds);
@@ -533,15 +533,29 @@ DhtRunner::startNetwork(const SockAddr sin4, const SockAddr sin6)
     s6 = -1;
 
     bound4 = {};
-    if (sin4)
-        s4 = bindSocket(sin4, bound4);
+    if (sin4) {
+        try {
+            s4 = bindSocket(sin4, bound4);
+        } catch (const DhtException& e) {
+            std::cerr << "Can't bind inet socket: " << e.what() << std::endl;
+        }
+    }
 
 #if 1
     bound6 = {};
-    if (sin6)
-        s6 = bindSocket(sin6, bound6);
+    if (sin6) {
+        try {
+            s6 = bindSocket(sin6, bound6);
+        } catch (const DhtException& e) {
+            std::cerr << "Can't bind inet6 socket: " << e.what() << std::endl;
+        }
+    }
 #endif
 
+    if (s4 == -1 && s6 == -1) {
+        throw DhtException("Can't bind socket");
+    }
+
     running_network = true;
     rcv_thread = std::thread([this, stop_readfd]() {
         try {
-- 
GitLab