diff options
author | William Schultz <william.schultz@mongodb.com> | 2020-04-16 11:00:53 -0400 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-04-20 16:10:32 +0000 |
commit | a7dcecf3da26fa73a033170283431c6607f7ec21 (patch) | |
tree | e64ef23976bd0ad7e1ac79f792bd8a1c61272d71 | |
parent | ce0f3f551dd83dbd844beb8779a462b1df1fcbf6 (diff) | |
download | mongo-a7dcecf3da26fa73a033170283431c6607f7ec21.tar.gz |
SERVER-47545 Allow isSelf to consider any IP address in the 127.0.0.1/8 range as pointing to the loopback
(cherry picked from commit 5d27d869ef502efc0e6bb27f028676ed479efaa3)
-rw-r--r-- | src/mongo/db/repl/isself.cpp | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/mongo/db/repl/isself.cpp b/src/mongo/db/repl/isself.cpp index f2bcae326b2..6ba5264398a 100644 --- a/src/mongo/db/repl/isself.cpp +++ b/src/mongo/db/repl/isself.cpp @@ -45,6 +45,7 @@ #include "mongo/db/commands.h" #include "mongo/db/service_context.h" #include "mongo/logv2/log.h" +#include "mongo/util/net/cidr.h" #include "mongo/util/net/socket_utils.h" #include "mongo/util/scopeguard.h" @@ -170,11 +171,13 @@ bool isSelf(const HostAndPort& hostAndPort, ServiceContext* const ctx) { // If any of the bound addresses is the default route (0.0.0.0 on IPv4) it means we are // listening on all network interfaces and need to check against any of them. + auto defaultRoute = false; if (myAddrs.empty() || std::any_of(myAddrs.cbegin(), myAddrs.cend(), [](std::string const& addrStr) { return HostAndPort(addrStr, serverGlobalParams.port).isDefaultRoute(); })) { myAddrs = getBoundAddrs(IPv6Enabled()); + defaultRoute = true; } const std::vector<std::string> hostAddrs = @@ -185,6 +188,26 @@ bool isSelf(const HostAndPort& hostAndPort, ServiceContext* const ctx) { for (std::vector<std::string>::const_iterator j = hostAddrs.begin(); j != hostAddrs.end(); ++j) { + + // If we are listening on the default route and the host address is in the range of + // addresses that correspond to the loopback interface, then we consider the host as + // ourself. + // + // Note that this logic is included to account for the fact that Debian systems add + // the "127.0.1.1" address for the local host name (as opposed to "127.0.0.1"). + // Debian does not do this for IPv6 so we don't need to check that here. + try { + CIDR loopbackAddrs("127.0.0.1/8"); + if (defaultRoute && loopbackAddrs.contains(CIDR(*j))) { + return true; + } + } catch (const std::exception& e) { + LOGV2_WARNING(4754500, + "Error checking host against loopback addresses", + "host"_attr = *j, + "error"_attr = e.what()); + } + if (*i == *j) { return true; } |