summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSpencer Jackson <spencer.jackson@mongodb.com>2017-04-25 15:02:13 -0400
committerSpencer Jackson <spencer.jackson@mongodb.com>2017-04-28 19:26:16 -0400
commit60636b4d3ae60a24c080c7250459814eef5e7c87 (patch)
tree046999f85ed571550d88285e484286c5927d26b6
parentb1054a04df0182fa6e4ccef53eefff90616aeea3 (diff)
downloadmongo-60636b4d3ae60a24c080c7250459814eef5e7c87.tar.gz
SERVER-28229: Bind to localhost by default
-rw-r--r--buildscripts/resmokeconfig/suites/aggregation_read_concern_majority_passthrough.yml1
-rw-r--r--buildscripts/resmokeconfig/suites/aggregation_sharded_collections_passthrough.yml2
-rw-r--r--buildscripts/resmokeconfig/suites/core.yml1
-rw-r--r--buildscripts/resmokeconfig/suites/integration_tests_replset.yml1
-rw-r--r--buildscripts/resmokeconfig/suites/integration_tests_sharded.yml2
-rw-r--r--buildscripts/resmokeconfig/suites/integration_tests_standalone.yml1
-rw-r--r--buildscripts/resmokeconfig/suites/jstestfuzz_replication.yml1
-rw-r--r--buildscripts/resmokeconfig/suites/jstestfuzz_replication_initsync.yml1
-rw-r--r--buildscripts/resmokeconfig/suites/jstestfuzz_replication_resync.yml1
-rw-r--r--buildscripts/resmokeconfig/suites/jstestfuzz_sharded.yml2
-rw-r--r--buildscripts/resmokeconfig/suites/master_slave_jscore_passthrough.yml1
-rw-r--r--buildscripts/resmokeconfig/suites/read_concern_linearizable_passthrough.yml1
-rw-r--r--buildscripts/resmokeconfig/suites/read_concern_majority_passthrough.yml1
-rw-r--r--buildscripts/resmokeconfig/suites/replica_sets_initsync_jscore_passthrough.yml1
-rw-r--r--buildscripts/resmokeconfig/suites/replica_sets_initsync_static_jscore_passthrough.yml1
-rw-r--r--buildscripts/resmokeconfig/suites/replica_sets_jscore_passthrough.yml1
-rw-r--r--buildscripts/resmokeconfig/suites/replica_sets_kill_secondaries_jscore_passthrough.yml1
-rw-r--r--buildscripts/resmokeconfig/suites/replica_sets_resync_static_jscore_passthrough.yml1
-rw-r--r--buildscripts/resmokeconfig/suites/sharded_collections_jscore_passthrough.yml1
-rw-r--r--buildscripts/resmokeconfig/suites/sharding_gle_auth_basics_passthrough.yml2
-rw-r--r--buildscripts/resmokeconfig/suites/sharding_jscore_passthrough.yml2
-rw-r--r--jstests/noPassthrough/command_line_parsing.js4
-rw-r--r--src/mongo/db/repl/repl_set_commands.cpp2
-rw-r--r--src/mongo/db/server_options_helpers.cpp35
-rw-r--r--src/mongo/db/startup_warnings_common.cpp15
-rw-r--r--src/mongo/shell/servers.js4
-rw-r--r--src/mongo/tools/bridge.cpp3
-rw-r--r--src/mongo/util/net/hostandport.cpp17
-rw-r--r--src/mongo/util/net/hostandport.h5
-rw-r--r--src/mongo/util/net/hostandport_test.cpp10
-rw-r--r--src/mongo/util/net/listen.cpp4
31 files changed, 106 insertions, 19 deletions
diff --git a/buildscripts/resmokeconfig/suites/aggregation_read_concern_majority_passthrough.yml b/buildscripts/resmokeconfig/suites/aggregation_read_concern_majority_passthrough.yml
index 41e144644ab..695ac071892 100644
--- a/buildscripts/resmokeconfig/suites/aggregation_read_concern_majority_passthrough.yml
+++ b/buildscripts/resmokeconfig/suites/aggregation_read_concern_majority_passthrough.yml
@@ -29,6 +29,7 @@ executor:
fixture:
class: ReplicaSetFixture
mongod_options:
+ bind_ip_all: ''
enableMajorityReadConcern: ''
set_parameters:
enableTestCommands: 1
diff --git a/buildscripts/resmokeconfig/suites/aggregation_sharded_collections_passthrough.yml b/buildscripts/resmokeconfig/suites/aggregation_sharded_collections_passthrough.yml
index 53003452733..39aca0fab22 100644
--- a/buildscripts/resmokeconfig/suites/aggregation_sharded_collections_passthrough.yml
+++ b/buildscripts/resmokeconfig/suites/aggregation_sharded_collections_passthrough.yml
@@ -46,9 +46,11 @@ executor:
fixture:
class: ShardedClusterFixture
mongos_options:
+ bind_ip_all: ''
set_parameters:
enableTestCommands: 1
mongod_options:
+ bind_ip_all: ''
nopreallocj: ''
set_parameters:
enableTestCommands: 1
diff --git a/buildscripts/resmokeconfig/suites/core.yml b/buildscripts/resmokeconfig/suites/core.yml
index 04a6f97cb7d..7532b70635d 100644
--- a/buildscripts/resmokeconfig/suites/core.yml
+++ b/buildscripts/resmokeconfig/suites/core.yml
@@ -15,5 +15,6 @@ executor:
fixture:
class: MongoDFixture
mongod_options:
+ bind_ip_all: ''
set_parameters:
enableTestCommands: 1
diff --git a/buildscripts/resmokeconfig/suites/integration_tests_replset.yml b/buildscripts/resmokeconfig/suites/integration_tests_replset.yml
index 88498f0733d..cc59aba19d0 100644
--- a/buildscripts/resmokeconfig/suites/integration_tests_replset.yml
+++ b/buildscripts/resmokeconfig/suites/integration_tests_replset.yml
@@ -12,6 +12,7 @@ executor:
fixture:
class: ReplicaSetFixture
mongod_options:
+ bind_ip_all: ''
set_parameters:
enableTestCommands: 1
numInitialSyncAttempts: 1
diff --git a/buildscripts/resmokeconfig/suites/integration_tests_sharded.yml b/buildscripts/resmokeconfig/suites/integration_tests_sharded.yml
index db7bc2b0c15..c7e60faaf31 100644
--- a/buildscripts/resmokeconfig/suites/integration_tests_sharded.yml
+++ b/buildscripts/resmokeconfig/suites/integration_tests_sharded.yml
@@ -12,10 +12,12 @@ executor:
fixture:
class: ShardedClusterFixture
mongod_options:
+ bind_ip_all: ''
set_parameters:
enableTestCommands: 1
numInitialSyncAttempts: 1
mongos_options:
+ bind_ip_all: ''
set_parameters:
enableTestCommands: 1
enable_sharding:
diff --git a/buildscripts/resmokeconfig/suites/integration_tests_standalone.yml b/buildscripts/resmokeconfig/suites/integration_tests_standalone.yml
index c594fc6fae1..938526d48c1 100644
--- a/buildscripts/resmokeconfig/suites/integration_tests_standalone.yml
+++ b/buildscripts/resmokeconfig/suites/integration_tests_standalone.yml
@@ -12,5 +12,6 @@ executor:
fixture:
class: MongoDFixture
mongod_options:
+ bind_ip_all: ''
set_parameters:
enableTestCommands: 1
diff --git a/buildscripts/resmokeconfig/suites/jstestfuzz_replication.yml b/buildscripts/resmokeconfig/suites/jstestfuzz_replication.yml
index 248eca9cd98..5331ec7e6fe 100644
--- a/buildscripts/resmokeconfig/suites/jstestfuzz_replication.yml
+++ b/buildscripts/resmokeconfig/suites/jstestfuzz_replication.yml
@@ -24,6 +24,7 @@ executor:
fixture:
class: ReplicaSetFixture
mongod_options:
+ bind_ip_all: ''
set_parameters:
enableTestCommands: 1
numInitialSyncAttempts: 1
diff --git a/buildscripts/resmokeconfig/suites/jstestfuzz_replication_initsync.yml b/buildscripts/resmokeconfig/suites/jstestfuzz_replication_initsync.yml
index 034f254494f..a969ded4d38 100644
--- a/buildscripts/resmokeconfig/suites/jstestfuzz_replication_initsync.yml
+++ b/buildscripts/resmokeconfig/suites/jstestfuzz_replication_initsync.yml
@@ -23,6 +23,7 @@ executor:
class: ReplicaSetFixture
mongod_options:
oplogSize: 511
+ bind_ip_all: ''
verbose: ''
set_parameters:
logComponentVerbosity:
diff --git a/buildscripts/resmokeconfig/suites/jstestfuzz_replication_resync.yml b/buildscripts/resmokeconfig/suites/jstestfuzz_replication_resync.yml
index 1038b99a10c..d2a6dbccc85 100644
--- a/buildscripts/resmokeconfig/suites/jstestfuzz_replication_resync.yml
+++ b/buildscripts/resmokeconfig/suites/jstestfuzz_replication_resync.yml
@@ -24,6 +24,7 @@ executor:
class: ReplicaSetFixture
mongod_options:
oplogSize: 511
+ bind_ip_all: ''
verbose: ''
set_parameters:
logComponentVerbosity:
diff --git a/buildscripts/resmokeconfig/suites/jstestfuzz_sharded.yml b/buildscripts/resmokeconfig/suites/jstestfuzz_sharded.yml
index 8eb912aee14..c7bdd2a80bf 100644
--- a/buildscripts/resmokeconfig/suites/jstestfuzz_sharded.yml
+++ b/buildscripts/resmokeconfig/suites/jstestfuzz_sharded.yml
@@ -20,10 +20,12 @@ executor:
mongos_options:
set_parameters:
enableTestCommands: 1
+ bind_ip_all: ''
verbose: ''
mongod_options:
set_parameters:
enableTestCommands: 1
numInitialSyncAttempts: 1
verbose: ''
+ bind_ip_all: ''
num_shards: 2
diff --git a/buildscripts/resmokeconfig/suites/master_slave_jscore_passthrough.yml b/buildscripts/resmokeconfig/suites/master_slave_jscore_passthrough.yml
index 4af7ba65c02..9201e6d7071 100644
--- a/buildscripts/resmokeconfig/suites/master_slave_jscore_passthrough.yml
+++ b/buildscripts/resmokeconfig/suites/master_slave_jscore_passthrough.yml
@@ -22,6 +22,7 @@ executor:
fixture:
class: MasterSlaveFixture
mongod_options:
+ bind_ip_all: ''
oplogSize: 511
set_parameters:
enableTestCommands: 1
diff --git a/buildscripts/resmokeconfig/suites/read_concern_linearizable_passthrough.yml b/buildscripts/resmokeconfig/suites/read_concern_linearizable_passthrough.yml
index c4e760278a2..58406452df8 100644
--- a/buildscripts/resmokeconfig/suites/read_concern_linearizable_passthrough.yml
+++ b/buildscripts/resmokeconfig/suites/read_concern_linearizable_passthrough.yml
@@ -92,6 +92,7 @@ executor:
fixture:
class: ReplicaSetFixture
mongod_options:
+ bind_ip_all: ''
oplogSize: 511
set_parameters:
enableTestCommands: 1
diff --git a/buildscripts/resmokeconfig/suites/read_concern_majority_passthrough.yml b/buildscripts/resmokeconfig/suites/read_concern_majority_passthrough.yml
index deeef8cb1c8..e00fc4cb75b 100644
--- a/buildscripts/resmokeconfig/suites/read_concern_majority_passthrough.yml
+++ b/buildscripts/resmokeconfig/suites/read_concern_majority_passthrough.yml
@@ -89,6 +89,7 @@ executor:
fixture:
class: ReplicaSetFixture
mongod_options:
+ bind_ip_all: ''
set_parameters:
enableTestCommands: 1
numInitialSyncAttempts: 1
diff --git a/buildscripts/resmokeconfig/suites/replica_sets_initsync_jscore_passthrough.yml b/buildscripts/resmokeconfig/suites/replica_sets_initsync_jscore_passthrough.yml
index 68e0a1f11c2..bbcffe401cb 100644
--- a/buildscripts/resmokeconfig/suites/replica_sets_initsync_jscore_passthrough.yml
+++ b/buildscripts/resmokeconfig/suites/replica_sets_initsync_jscore_passthrough.yml
@@ -92,6 +92,7 @@ executor:
fixture:
class: ReplicaSetFixture
mongod_options:
+ bind_ip_all: ''
oplogSize: 511
set_parameters:
enableTestCommands: 1
diff --git a/buildscripts/resmokeconfig/suites/replica_sets_initsync_static_jscore_passthrough.yml b/buildscripts/resmokeconfig/suites/replica_sets_initsync_static_jscore_passthrough.yml
index 924f92b5f7c..66c253516a3 100644
--- a/buildscripts/resmokeconfig/suites/replica_sets_initsync_static_jscore_passthrough.yml
+++ b/buildscripts/resmokeconfig/suites/replica_sets_initsync_static_jscore_passthrough.yml
@@ -26,6 +26,7 @@ executor:
fixture:
class: ReplicaSetFixture
mongod_options:
+ bind_ip_all: ''
oplogSize: 511
set_parameters:
enableTestCommands: 1
diff --git a/buildscripts/resmokeconfig/suites/replica_sets_jscore_passthrough.yml b/buildscripts/resmokeconfig/suites/replica_sets_jscore_passthrough.yml
index c89d0bd1020..86fec456a72 100644
--- a/buildscripts/resmokeconfig/suites/replica_sets_jscore_passthrough.yml
+++ b/buildscripts/resmokeconfig/suites/replica_sets_jscore_passthrough.yml
@@ -24,6 +24,7 @@ executor:
fixture:
class: ReplicaSetFixture
mongod_options:
+ bind_ip_all: ''
oplogSize: 511
set_parameters:
enableTestCommands: 1
diff --git a/buildscripts/resmokeconfig/suites/replica_sets_kill_secondaries_jscore_passthrough.yml b/buildscripts/resmokeconfig/suites/replica_sets_kill_secondaries_jscore_passthrough.yml
index a4b6c5a5feb..e8d8662a642 100644
--- a/buildscripts/resmokeconfig/suites/replica_sets_kill_secondaries_jscore_passthrough.yml
+++ b/buildscripts/resmokeconfig/suites/replica_sets_kill_secondaries_jscore_passthrough.yml
@@ -24,6 +24,7 @@ executor:
fixture:
class: ReplicaSetFixture
mongod_options:
+ bind_ip_all: ''
oplogSize: 511
set_parameters:
enableTestCommands: 1
diff --git a/buildscripts/resmokeconfig/suites/replica_sets_resync_static_jscore_passthrough.yml b/buildscripts/resmokeconfig/suites/replica_sets_resync_static_jscore_passthrough.yml
index e9bbff75581..86cb7bfc439 100644
--- a/buildscripts/resmokeconfig/suites/replica_sets_resync_static_jscore_passthrough.yml
+++ b/buildscripts/resmokeconfig/suites/replica_sets_resync_static_jscore_passthrough.yml
@@ -27,6 +27,7 @@ executor:
fixture:
class: ReplicaSetFixture
mongod_options:
+ bind_ip_all: ''
oplogSize: 511
set_parameters:
enableTestCommands: 1
diff --git a/buildscripts/resmokeconfig/suites/sharded_collections_jscore_passthrough.yml b/buildscripts/resmokeconfig/suites/sharded_collections_jscore_passthrough.yml
index 9aa7fb56f7c..edb142f14cc 100644
--- a/buildscripts/resmokeconfig/suites/sharded_collections_jscore_passthrough.yml
+++ b/buildscripts/resmokeconfig/suites/sharded_collections_jscore_passthrough.yml
@@ -96,6 +96,7 @@ executor:
set_parameters:
enableTestCommands: 1
mongod_options:
+ bind_ip_all: ''
nopreallocj: ''
set_parameters:
enableTestCommands: 1
diff --git a/buildscripts/resmokeconfig/suites/sharding_gle_auth_basics_passthrough.yml b/buildscripts/resmokeconfig/suites/sharding_gle_auth_basics_passthrough.yml
index 56b4854cde9..e8798af8f70 100644
--- a/buildscripts/resmokeconfig/suites/sharding_gle_auth_basics_passthrough.yml
+++ b/buildscripts/resmokeconfig/suites/sharding_gle_auth_basics_passthrough.yml
@@ -32,11 +32,13 @@ executor:
fixture:
class: ShardedClusterFixture
mongos_options:
+ bind_ip_all: ''
keyFile: *keyFile
set_parameters:
enableTestCommands: 1
enableLocalhostAuthBypass: false
mongod_options:
+ bind_ip_all: ''
auth: ''
keyFile: *keyFile
set_parameters:
diff --git a/buildscripts/resmokeconfig/suites/sharding_jscore_passthrough.yml b/buildscripts/resmokeconfig/suites/sharding_jscore_passthrough.yml
index 9791e68b1ad..f41157f8d03 100644
--- a/buildscripts/resmokeconfig/suites/sharding_jscore_passthrough.yml
+++ b/buildscripts/resmokeconfig/suites/sharding_jscore_passthrough.yml
@@ -62,9 +62,11 @@ executor:
fixture:
class: ShardedClusterFixture
mongos_options:
+ bind_ip_all: ''
set_parameters:
enableTestCommands: 1
mongod_options:
+ bind_ip_all: ''
set_parameters:
enableTestCommands: 1
numInitialSyncAttempts: 1
diff --git a/jstests/noPassthrough/command_line_parsing.js b/jstests/noPassthrough/command_line_parsing.js
index 06246e72084..cda7e8ab36e 100644
--- a/jstests/noPassthrough/command_line_parsing.js
+++ b/jstests/noPassthrough/command_line_parsing.js
@@ -16,7 +16,7 @@ var m2expected = {
"parsed": {
"config": "jstests/libs/testconfig",
"storage": {"dbPath": m2.dbpath},
- "net": {"port": m2.port},
+ "net": {"bindIp": "0.0.0.0", "port": m2.port},
"help": false,
"version": false,
"sysinfo": false
@@ -40,7 +40,7 @@ var m3expected = {
"parsed": {
"config": "jstests/libs/testconfig",
"storage": {"dbPath": m3.dbpath},
- "net": {"port": m3.port},
+ "net": {"bindIp": "0.0.0.0", "port": m3.port},
"help": false,
"version": false,
"sysinfo": false
diff --git a/src/mongo/db/repl/repl_set_commands.cpp b/src/mongo/db/repl/repl_set_commands.cpp
index 09226ba0237..86da2bec9db 100644
--- a/src/mongo/db/repl/repl_set_commands.cpp
+++ b/src/mongo/db/repl/repl_set_commands.cpp
@@ -256,7 +256,7 @@ HostAndPort someHostAndPortForMe() {
ips = "";
}
HostAndPort h = HostAndPort(ip, serverGlobalParams.port);
- if (!h.isLocalHost()) {
+ if (!h.isLocalHost() && !h.isDefaultRoute()) {
return h;
}
}
diff --git a/src/mongo/db/server_options_helpers.cpp b/src/mongo/db/server_options_helpers.cpp
index cd08324cfa2..326345cb87d 100644
--- a/src/mongo/db/server_options_helpers.cpp
+++ b/src/mongo/db/server_options_helpers.cpp
@@ -184,11 +184,17 @@ Status addGeneralServerOptions(moe::OptionSection* options) {
options->addOptionChaining("net.port", "port", moe::Int, portInfoBuilder.str().c_str());
- options->addOptionChaining(
- "net.bindIp",
- "bind_ip",
- moe::String,
- "comma separated list of ip addresses to listen on - all local ips by default");
+ options
+ ->addOptionChaining(
+ "net.bindIp",
+ "bind_ip",
+ moe::String,
+ "comma separated list of ip addresses to listen on - localhost by default")
+ .incompatibleWith("bind_ip_all");
+
+ options
+ ->addOptionChaining("net.bindIpAll", "bind_ip_all", moe::Switch, "bind to all ip addresses")
+ .incompatibleWith("bind_ip");
options->addOptionChaining(
"net.ipv6", "ipv6", moe::Switch, "enable IPv6 support (disabled by default)");
@@ -775,10 +781,6 @@ Status storeServerOptions(const moe::Environment& params) {
serverGlobalParams.port = params["net.port"].as<int>();
}
- if (params.count("net.bindIp")) {
- serverGlobalParams.bind_ip = params["net.bindIp"].as<std::string>();
- }
-
if (params.count("net.ipv6") && params["net.ipv6"].as<bool>() == true) {
enableIPv6();
}
@@ -828,11 +830,18 @@ Status storeServerOptions(const moe::Environment& params) {
serverGlobalParams.objcheck = params["net.wireObjectCheck"].as<bool>();
}
- if (params.count("net.bindIp")) {
- // passing in wildcard is the same as default behavior; remove for SERVER-3350
- if (serverGlobalParams.bind_ip == "0.0.0.0") {
- serverGlobalParams.bind_ip = "";
+ if (params.count("net.bindIpAll") && params["net.bindIpAll"].as<bool>()) {
+ // Bind to all IP addresses
+ serverGlobalParams.bind_ip = "0.0.0.0";
+ if (params.count("net.ipv6") && params["net.ipv6"].as<bool>()) {
+ serverGlobalParams.bind_ip += ",::";
}
+ } else if (params.count("net.bindIp")) {
+ // Bind to enumerated IP addresses
+ serverGlobalParams.bind_ip = params["net.bindIp"].as<std::string>();
+ } else {
+ // Bind to localhost
+ serverGlobalParams.bind_ip = "";
}
#ifndef _WIN32
diff --git a/src/mongo/db/startup_warnings_common.cpp b/src/mongo/db/startup_warnings_common.cpp
index 510de06ca04..4259ded0d23 100644
--- a/src/mongo/db/startup_warnings_common.cpp
+++ b/src/mongo/db/startup_warnings_common.cpp
@@ -112,6 +112,21 @@ void logCommonStartupWarnings(const ServerGlobalParams& serverParams) {
}
#endif
+ if (serverParams.bind_ip.empty()) {
+ log() << startupWarningsLog;
+ log() << "** ATTENTION: The server is bound to localhost." << startupWarningsLog;
+ log() << "** Remote systems will be unable to connect to this server. "
+ << startupWarningsLog;
+ log() << "** Start the server with --bind_ip <address> to specify which IP "
+ "addresses it"
+ << startupWarningsLog;
+ log() << "** should serve responses from, or with --bind_ip_all to bind to all "
+ "interfaces."
+ << startupWarningsLog;
+ warned = true;
+ }
+
+
if (warned) {
log() << startupWarningsLog;
}
diff --git a/src/mongo/shell/servers.js b/src/mongo/shell/servers.js
index 8f4ae13ba3f..a0f58c5b415 100644
--- a/src/mongo/shell/servers.js
+++ b/src/mongo/shell/servers.js
@@ -468,6 +468,10 @@ var MongoRunner, _startMongod, startMongoProgram, runMongoProgram, startMongoPro
opts.networkMessageCompressors = jsTestOptions().networkMessageCompressors;
}
+ if (!opts.bind_ip) {
+ opts.bind_ip = "0.0.0.0";
+ }
+
return opts;
};
diff --git a/src/mongo/tools/bridge.cpp b/src/mongo/tools/bridge.cpp
index b515ad7ca87..6cae2ee2cf0 100644
--- a/src/mongo/tools/bridge.cpp
+++ b/src/mongo/tools/bridge.cpp
@@ -341,7 +341,8 @@ private:
class BridgeListener final : public Listener {
public:
BridgeListener()
- : Listener("bridge", "", mongoBridgeGlobalParams.port, getGlobalServiceContext(), false),
+ : Listener(
+ "bridge", "0.0.0.0", mongoBridgeGlobalParams.port, getGlobalServiceContext(), false),
_seedSource(mongoBridgeGlobalParams.seed) {
log() << "Setting random seed: " << mongoBridgeGlobalParams.seed;
}
diff --git a/src/mongo/util/net/hostandport.cpp b/src/mongo/util/net/hostandport.cpp
index abd8f2d6ade..ad94315e2f7 100644
--- a/src/mongo/util/net/hostandport.cpp
+++ b/src/mongo/util/net/hostandport.cpp
@@ -82,6 +82,23 @@ bool HostAndPort::isLocalHost() const {
);
}
+bool HostAndPort::isDefaultRoute() const {
+ if (_host == "0.0.0.0") {
+ return true;
+ }
+
+ // There are multiple ways to write IPv6 addresses.
+ // We're looking for any representation of the address "0:0:0:0:0:0:0:0".
+ // A single sequence of "0" bytes in an IPv6 address may be represented as "::",
+ // so we must also match addresses like "::" or "0::0:0".
+ // Return false if a character other than ':' or '0' is contained in the address.
+ auto firstNonDefaultIPv6Char =
+ std::find_if(std::begin(_host), std::end(_host), [](const char& c) {
+ return c != ':' && c != '0' && c != '[' && c != ']';
+ });
+ return firstNonDefaultIPv6Char == std::end(_host);
+}
+
std::string HostAndPort::toString() const {
StringBuilder ss;
append(ss);
diff --git a/src/mongo/util/net/hostandport.h b/src/mongo/util/net/hostandport.h
index 53209bbaa04..74f9fa578a1 100644
--- a/src/mongo/util/net/hostandport.h
+++ b/src/mongo/util/net/hostandport.h
@@ -97,6 +97,11 @@ struct HostAndPort {
bool isLocalHost() const;
/**
+ * Returns true if the hostname is an IP matching the default route.
+ */
+ bool isDefaultRoute() const;
+
+ /**
* Returns a string representation of "host:port".
*/
std::string toString() const;
diff --git a/src/mongo/util/net/hostandport_test.cpp b/src/mongo/util/net/hostandport_test.cpp
index 932c0d159bf..52015690ac7 100644
--- a/src/mongo/util/net/hostandport_test.cpp
+++ b/src/mongo/util/net/hostandport_test.cpp
@@ -111,5 +111,15 @@ TEST(HostAndPort, RoundTripAbility) {
ASSERT_EQUALS(HostAndPort("[124d:]"), HostAndPort(HostAndPort("[124d:]").toString()));
}
+TEST(HostAndPort, CanIdentifyDefaultRoutes) {
+ ASSERT_TRUE(HostAndPort("0.0.0.0").isDefaultRoute());
+ ASSERT_FALSE(HostAndPort("127.0.0.1").isDefaultRoute());
+ ASSERT_TRUE(HostAndPort("[::]").isDefaultRoute());
+ ASSERT_FALSE(HostAndPort("[::1]").isDefaultRoute());
+ ASSERT_TRUE(HostAndPort("[0:0:0:0:0:0:0:0]").isDefaultRoute());
+ ASSERT_TRUE(HostAndPort("[0:0:0::0:0:0]").isDefaultRoute());
+ ASSERT_TRUE(HostAndPort("[0:0:0::00:0:0]").isDefaultRoute());
+}
+
} // namespace
} // namespace mongo
diff --git a/src/mongo/util/net/listen.cpp b/src/mongo/util/net/listen.cpp
index 67e9f394308..9d04059a743 100644
--- a/src/mongo/util/net/listen.cpp
+++ b/src/mongo/util/net/listen.cpp
@@ -96,10 +96,10 @@ using std::vector;
vector<SockAddr> ipToAddrs(const char* ips, int port, bool useUnixSockets) {
vector<SockAddr> out;
if (*ips == '\0') {
- out.push_back(SockAddr("0.0.0.0", port)); // IPv4 all
+ out.push_back(SockAddr("127.0.0.1", port)); // IPv4 localhost
if (IPv6Enabled())
- out.push_back(SockAddr("::", port)); // IPv6 all
+ out.push_back(SockAddr("::1", port)); // IPv6 localhost
#ifndef _WIN32
if (useUnixSockets)
out.push_back(SockAddr(makeUnixSockPath(port), port)); // Unix socket