Skip to content

Commit 5788feb

Browse files
jtc3161srh
andcommitted
Don't use DNS resolution when finding local ip addresses.
Removes use of DNS to find local ip addresses to bind to, except on Windows. Also removes mistaken "Ignore loopback addresses" logic, which happened to get its conditional flipped the wrong way. The flipped "Ignore loopback addresses" logic meant that we would remove all non-loopback addresses that got returned by the DNS lookup. So removing the DNS lookup entirely won't break things. The behavior on Windows never used getifaddrs, it just used DNS lookup. We keep the old behavior on Windows because limited manpower prevents us from using the Windows alternative to getifaddrs. Fixes #4600. Co-authored-by: Sam Hughes <[email protected]>
1 parent ed42bdb commit 5788feb

File tree

3 files changed

+10
-20
lines changed

3 files changed

+10
-20
lines changed

src/arch/address.cc

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -151,28 +151,19 @@ std::set<ip_address_t> hostname_to_ips(const std::string &host) {
151151
std::set<ip_address_t> get_local_ips(const std::set<ip_address_t> &filter,
152152
local_ip_filter_t filter_type) {
153153
std::set<ip_address_t> all_ips;
154-
std::set<ip_address_t> filtered_ips;
155-
154+
#ifdef _WIN32
155+
// NOTE: We used to use hostname_to_ips for all OSes, we stopped using it because
156+
// DNS resolution could be slow. Now we only use it for Windows because nobody's
157+
// fixed the Windows implementation to get the network interfaces more sensibly.
156158
try {
157159
all_ips = hostname_to_ips(str_gethostname());
158160
} catch (const host_lookup_exc_t &ex) {
159161
// Continue on, this probably means there's no DNS entry for this host
160162
}
161163

162-
#ifdef _WIN32
163164
// TODO WINDOWS: is this enough?
164165
all_ips.emplace("127.0.0.1");
165166
#else
166-
// Ignore loopback addresses - those will be returned by getifaddrs, and
167-
// getaddrinfo is not so trustworthy.
168-
// See https://github.com/rethinkdb/rethinkdb/issues/2405
169-
for (auto it = all_ips.begin(); it != all_ips.end();) {
170-
auto curr = it++;
171-
if (!curr->is_loopback()) {
172-
all_ips.erase(curr);
173-
}
174-
}
175-
176167
struct ifaddrs *addrs;
177168
int res = getifaddrs(&addrs);
178169
guarantee_err(res == 0,
@@ -191,6 +182,7 @@ std::set<ip_address_t> get_local_ips(const std::set<ip_address_t> &filter,
191182
freeifaddrs(addrs);
192183
#endif
193184

185+
std::set<ip_address_t> filtered_ips;
194186
// Remove any addresses that don't fit the filter
195187
for (auto const &ip : all_ips) {
196188
if (filter_type == local_ip_filter_t::ALL ||

src/clustering/administration/main/serve.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ peer_address_set_t look_up_peers_addresses(const std::vector<host_and_port_t> &n
5858
}
5959

6060
std::string service_address_ports_t::get_addresses_string(
61-
std::set<ip_address_t> actual_addresses) const {
61+
std::set<ip_address_t> actual_addresses) {
6262

6363
bool first = true;
6464
std::string result;
@@ -77,8 +77,7 @@ std::string service_address_ports_t::get_addresses_string(
7777
return result;
7878
}
7979

80-
bool service_address_ports_t::is_bind_all(
81-
std::set<ip_address_t> addresses) const {
80+
bool service_address_ports_t::is_bind_all(const std::set<ip_address_t> &addresses) {
8281
// If the set is empty, it means we're listening on all addresses.
8382
return addresses.empty();
8483
}
@@ -630,7 +629,7 @@ bool do_serve(io_backender_t *io_backender,
630629
serve_info.ports.local_addresses_http.size() == 1 ? "" : "es",
631630
addresses_string.c_str());
632631

633-
if (!serve_info.ports.is_bind_all(serve_info.ports.local_addresses)) {
632+
if (!service_address_ports_t::is_bind_all(serve_info.ports.local_addresses)) {
634633
if(serve_info.config_file) {
635634
logNTC("To fully expose RethinkDB on the network, bind to "
636635
"all addresses by adding `bind=all' to the config "

src/clustering/administration/main/serve.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,9 @@ struct service_address_ports_t {
7979
sanitize_port(reql_port, "reql_port", port_offset);
8080
}
8181

82-
std::string get_addresses_string(
83-
std::set<ip_address_t> actual_addresses) const;
82+
static std::string get_addresses_string(std::set<ip_address_t> actual_addresses);
8483

85-
bool is_bind_all(std::set<ip_address_t> addresses) const;
84+
static bool is_bind_all(const std::set<ip_address_t> &addresses);
8685

8786
std::set<ip_address_t> local_addresses;
8887
std::set<ip_address_t> local_addresses_cluster;

0 commit comments

Comments
 (0)