Disable SRV and TXT lookups for localhost

pull/17470/head
Alexander Polcyn 6 years ago
parent 311ee1eda1
commit 7dc330f298
  1. 9
      src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.cc
  2. 29
      src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.cc
  3. 5
      test/core/client_channel/resolvers/dns_resolver_connectivity_test.cc
  4. 5
      test/core/end2end/fuzzers/api_fuzzer.cc
  5. 9
      test/core/iomgr/resolve_address_posix_test.cc

@ -465,11 +465,16 @@ static grpc_error* blocking_resolve_address_ares(
static grpc_address_resolver_vtable ares_resolver = {
grpc_resolve_address_ares, blocking_resolve_address_ares};
static bool should_use_ares(const char* resolver_env) {
return resolver_env != nullptr && gpr_stricmp(resolver_env, "ares") == 0;
}
void grpc_resolver_dns_ares_init() {
char* resolver_env = gpr_getenv("GRPC_DNS_RESOLVER");
/* TODO(zyc): Turn on c-ares based resolver by default after the address
sorter and the CNAME support are added. */
if (resolver_env != nullptr && gpr_stricmp(resolver_env, "ares") == 0) {
if (should_use_ares(resolver_env)) {
gpr_log(GPR_DEBUG, "Using ares dns resolver");
address_sorting_init();
grpc_error* error = grpc_ares_init();
if (error != GRPC_ERROR_NONE) {
@ -489,7 +494,7 @@ void grpc_resolver_dns_ares_init() {
void grpc_resolver_dns_ares_shutdown() {
char* resolver_env = gpr_getenv("GRPC_DNS_RESOLVER");
if (resolver_env != nullptr && gpr_stricmp(resolver_env, "ares") == 0) {
if (should_use_ares(resolver_env)) {
address_sorting_shutdown();
grpc_ares_cleanup();
}

@ -510,6 +510,28 @@ static bool resolve_as_ip_literal_locked(
return out;
}
static bool target_matches_localhost_inner(const char* name, char** host,
char** port) {
if (!gpr_split_host_port(name, host, port)) {
gpr_log(GPR_INFO, "Unable to split host and port for name: %s", name);
return false;
}
if (gpr_stricmp(*host, "localhost") == 0) {
return true;
} else {
return false;
}
}
static bool target_matches_localhost(const char* name) {
char* host = nullptr;
char* port = nullptr;
bool out = target_matches_localhost_inner(name, &host, &port);
gpr_free(host);
gpr_free(port);
return out;
}
static grpc_ares_request* grpc_dns_lookup_ares_locked_impl(
const char* dns_server, const char* name, const char* default_port,
grpc_pollset_set* interested_parties, grpc_closure* on_done,
@ -536,6 +558,13 @@ static grpc_ares_request* grpc_dns_lookup_ares_locked_impl(
GRPC_CLOSURE_SCHED(on_done, GRPC_ERROR_NONE);
return r;
}
// Don't query for SRV and TXT records if the target is "localhost", so
// as to cut down on lookups over the network, especially in tests:
// https://github.com/grpc/proposal/pull/79
if (target_matches_localhost(name)) {
check_grpclb = false;
r->service_config_json_out = nullptr;
}
// Look up name using c-ares lib.
grpc_dns_lookup_ares_continue_after_check_localhost_and_ip_literals_locked(
r, dns_server, name, default_port, interested_parties, check_grpclb,

@ -76,7 +76,10 @@ static grpc_ares_request* my_dns_lookup_ares_locked(
} else {
gpr_mu_unlock(&g_mu);
*addresses = grpc_core::MakeUnique<grpc_core::ServerAddressList>();
(*addresses)->emplace_back(nullptr, 0, nullptr);
grpc_resolved_address dummy_resolved_address;
memset(&dummy_resolved_address, 0, sizeof(dummy_resolved_address));
dummy_resolved_address.len = 123;
(*addresses)->emplace_back(dummy_resolved_address, nullptr);
}
GRPC_CLOSURE_SCHED(on_done, error);
return nullptr;

@ -342,7 +342,10 @@ static void finish_resolve(void* arg, grpc_error* error) {
*r->addrs = addrs;
} else if (r->addresses != nullptr) {
*r->addresses = grpc_core::MakeUnique<grpc_core::ServerAddressList>();
(*r->addresses)->emplace_back(nullptr, 0, nullptr);
grpc_resolved_address dummy_resolved_address;
memset(&dummy_resolved_address, 0, sizeof(dummy_resolved_address));
dummy_resolved_address.len = 0;
(*r->addresses)->emplace_back(dummy_resolved_address, nullptr);
}
GRPC_CLOSURE_SCHED(r->on_done, GRPC_ERROR_NONE);
} else {

@ -27,6 +27,8 @@
#include <grpc/support/sync.h>
#include <grpc/support/time.h>
#include "src/core/lib/gpr/env.h"
#include "src/core/lib/gpr/string.h"
#include "src/core/lib/gpr/useful.h"
#include "src/core/lib/gprpp/thd.h"
#include "src/core/lib/iomgr/executor.h"
@ -163,9 +165,16 @@ int main(int argc, char** argv) {
{
grpc_core::ExecCtx exec_ctx;
char* resolver_env = gpr_getenv("GRPC_DNS_RESOLVER");
// c-ares resolver doesn't support UDS (ability for native DNS resolver
// to handle this is only expected to be used by servers, which
// unconditionally use the native DNS resolver).
if (resolver_env == nullptr || gpr_stricmp(resolver_env, "native") == 0) {
test_unix_socket();
test_unix_socket_path_name_too_long();
}
gpr_free(resolver_env);
}
grpc_shutdown();
return 0;

Loading…
Cancel
Save