From a6813ec786636957bd57648ee471362e68082c97 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Fri, 15 Feb 2019 18:19:56 +0100 Subject: [PATCH] ApiListener: restore previous bind(2) behavior --- lib/remote/apilistener.cpp | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/remote/apilistener.cpp b/lib/remote/apilistener.cpp index 339ab83bf..6e9cc8b7b 100644 --- a/lib/remote/apilistener.cpp +++ b/lib/remote/apilistener.cpp @@ -362,12 +362,28 @@ bool ApiListener::AddListener(const String& node, const String& service) try { tcp::resolver resolver (io); tcp::resolver::query query (node, service, tcp::resolver::query::passive); - auto endpoint (resolver.resolve(query)->endpoint()); - acceptor->open(endpoint.protocol()); - acceptor->set_option(ip::v6_only(false)); - acceptor->set_option(tcp::acceptor::reuse_address(true)); - acceptor->bind(endpoint); + auto result (resolver.resolve(query)); + auto current (result.begin()); + + for (;;) { + try { + acceptor->open(current->endpoint().protocol()); + acceptor->set_option(ip::v6_only(false)); + acceptor->set_option(tcp::acceptor::reuse_address(true)); + acceptor->bind(current->endpoint()); + + break; + } catch (const std::exception&) { + if (++current == result.end()) { + throw; + } + + if (acceptor->is_open()) { + acceptor->close(); + } + } + } } catch (const std::exception&) { Log(LogCritical, "ApiListener") << "Cannot bind TCP socket for host '" << node << "' on port '" << service << "'."; -- 2.40.0