diff options
author | Sara Golemon <sara.golemon@mongodb.com> | 2017-08-10 11:44:13 -0400 |
---|---|---|
committer | Sara Golemon <sara.golemon@mongodb.com> | 2017-08-17 00:51:01 -0400 |
commit | 1158a6b9b1de13ef2dd809b4515d881422163b5e (patch) | |
tree | e7e9620a155db736c5078a63cedff2d780501c39 /src/mongo/transport | |
parent | 3c2e253dd0a2cb9b5cee72205eafd48c4d78b324 (diff) | |
download | mongo-1158a6b9b1de13ef2dd809b4515d881422163b5e.tar.gz |
SERVER-30588 Bind all addresses returned by getaddroinfo()
Diffstat (limited to 'src/mongo/transport')
-rw-r--r-- | src/mongo/transport/transport_layer_asio.cpp | 75 |
1 files changed, 42 insertions, 33 deletions
diff --git a/src/mongo/transport/transport_layer_asio.cpp b/src/mongo/transport/transport_layer_asio.cpp index 15220f3abc4..9751c49eddb 100644 --- a/src/mongo/transport/transport_layer_asio.cpp +++ b/src/mongo/transport/transport_layer_asio.cpp @@ -165,52 +165,61 @@ Status TransportLayerASIO::setup() { warning() << "Skipping empty bind address"; continue; } - SockAddr addr(StringData(ip), - _listenerOptions.port, - _listenerOptions.enableIPv6 ? AF_UNSPEC : AF_INET); - asio::generic::stream_protocol::endpoint endpoint(addr.raw(), addr.addressSize); + + const auto addrs = SockAddr::createAll( + ip, _listenerOptions.port, _listenerOptions.enableIPv6 ? AF_UNSPEC : AF_INET); + if (addrs.empty()) { + warning() << "Found no addresses for " << ip; + continue; + } + + for (const auto& addr : addrs) { + asio::generic::stream_protocol::endpoint endpoint(addr.raw(), addr.addressSize); #ifndef _WIN32 - if (addr.getType() == AF_UNIX) { - if (::unlink(ip.c_str()) == -1 && errno != ENOENT) { - error() << "Failed to unlink socket file " << ip << " " - << errnoWithDescription(errno); - fassertFailedNoTrace(40486); + if (addr.getType() == AF_UNIX) { + if (::unlink(ip.c_str()) == -1 && errno != ENOENT) { + error() << "Failed to unlink socket file " << ip << " " + << errnoWithDescription(errno); + fassertFailedNoTrace(40486); + } } - } #endif - if (addr.getType() == AF_INET6 && !_listenerOptions.enableIPv6) { - error() << "Specified ipv6 bind address, but ipv6 is disabled"; - fassertFailedNoTrace(40488); - } + if (addr.getType() == AF_INET6 && !_listenerOptions.enableIPv6) { + error() << "Specified ipv6 bind address, but ipv6 is disabled"; + fassertFailedNoTrace(40488); + } - GenericAcceptor acceptor(*_ioContext); - acceptor.open(endpoint.protocol()); - acceptor.set_option(GenericAcceptor::reuse_address(true)); + GenericAcceptor acceptor(*_ioContext); + acceptor.open(endpoint.protocol()); + acceptor.set_option(GenericAcceptor::reuse_address(true)); - acceptor.non_blocking(true, ec); - if (ec) { - return errorCodeToStatus(ec); - } + acceptor.non_blocking(true, ec); + if (ec) { + return errorCodeToStatus(ec); + } - acceptor.bind(endpoint, ec); - if (ec) { - return errorCodeToStatus(ec); - } + acceptor.bind(endpoint, ec); + if (ec) { + return errorCodeToStatus(ec); + } #ifndef _WIN32 - if (addr.getType() == AF_UNIX) { - if (::chmod(ip.c_str(), serverGlobalParams.unixSocketPermissions) == -1) { - error() << "Failed to chmod socket file " << ip << " " - << errnoWithDescription(errno); - fassertFailedNoTrace(40487); + if (addr.getType() == AF_UNIX) { + if (::chmod(ip.c_str(), serverGlobalParams.unixSocketPermissions) == -1) { + error() << "Failed to chmod socket file " << ip << " " + << errnoWithDescription(errno); + fassertFailedNoTrace(40487); + } } - } #endif - _acceptors.emplace_back(std::move(acceptor)); + _acceptors.emplace_back(std::move(acceptor)); + } } - invariant(!_acceptors.empty()); + if (_acceptors.empty()) { + return Status(ErrorCodes::SocketException, "No available addresses/ports to bind to"); + } #ifdef MONGO_CONFIG_SSL const auto& sslParams = getSSLGlobalParams(); |