]> granicus.if.org Git - icinga2/commitdiff
ApiListener: restore previous bind(2) behavior
authorAlexander A. Klimov <alexander.klimov@icinga.com>
Fri, 15 Feb 2019 17:19:56 +0000 (18:19 +0100)
committerAlexander A. Klimov <alexander.klimov@icinga.com>
Mon, 1 Apr 2019 09:40:14 +0000 (11:40 +0200)
lib/remote/apilistener.cpp

index 339ab83bf3ab44151f57007d7a807a5815fe9073..6e9cc8b7b4ee47b9b8ae17969694e02a057ddd4e 100644 (file)
@@ -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 << "'.";