Merge pull request #19584 from bigfacebear/add_IsValidTarget_api_to_ResolverRegistry

add IsValidTarget API to ResolverRegistry
pull/19521/head^2
Qiancheng Zhao 5 years ago committed by GitHub
commit 0da6fcfa05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.cc
  2. 11
      src/core/ext/filters/client_channel/resolver/dns/native/dns_resolver.cc
  3. 2
      src/core/ext/filters/client_channel/resolver/fake/fake_resolver.cc
  4. 44
      src/core/ext/filters/client_channel/resolver/sockaddr/sockaddr_resolver.cc
  5. 4
      src/core/ext/filters/client_channel/resolver_factory.h
  6. 11
      src/core/ext/filters/client_channel/resolver_registry.cc
  7. 3
      src/core/ext/filters/client_channel/resolver_registry.h

@ -433,6 +433,8 @@ void AresDnsResolver::StartResolvingLocked() {
class AresDnsResolverFactory : public ResolverFactory { class AresDnsResolverFactory : public ResolverFactory {
public: public:
bool IsValidUri(const grpc_uri* uri) const override { return true; }
OrphanablePtr<Resolver> CreateResolver(ResolverArgs args) const override { OrphanablePtr<Resolver> CreateResolver(ResolverArgs args) const override {
return OrphanablePtr<Resolver>(New<AresDnsResolver>(std::move(args))); return OrphanablePtr<Resolver>(New<AresDnsResolver>(std::move(args)));
} }

@ -258,11 +258,16 @@ void NativeDnsResolver::StartResolvingLocked() {
class NativeDnsResolverFactory : public ResolverFactory { class NativeDnsResolverFactory : public ResolverFactory {
public: public:
OrphanablePtr<Resolver> CreateResolver(ResolverArgs args) const override { bool IsValidUri(const grpc_uri* uri) const override {
if (GPR_UNLIKELY(0 != strcmp(args.uri->authority, ""))) { if (GPR_UNLIKELY(0 != strcmp(uri->authority, ""))) {
gpr_log(GPR_ERROR, "authority based dns uri's not supported"); gpr_log(GPR_ERROR, "authority based dns uri's not supported");
return OrphanablePtr<Resolver>(nullptr); return false;
} }
return true;
}
OrphanablePtr<Resolver> CreateResolver(ResolverArgs args) const override {
if (!IsValidUri(args.uri)) return nullptr;
return OrphanablePtr<Resolver>(New<NativeDnsResolver>(std::move(args))); return OrphanablePtr<Resolver>(New<NativeDnsResolver>(std::move(args)));
} }

@ -321,6 +321,8 @@ namespace {
class FakeResolverFactory : public ResolverFactory { class FakeResolverFactory : public ResolverFactory {
public: public:
bool IsValidUri(const grpc_uri* uri) const override { return true; }
OrphanablePtr<Resolver> CreateResolver(ResolverArgs args) const override { OrphanablePtr<Resolver> CreateResolver(ResolverArgs args) const override {
return OrphanablePtr<Resolver>(New<FakeResolver>(std::move(args))); return OrphanablePtr<Resolver>(New<FakeResolver>(std::move(args)));
} }

@ -80,24 +80,23 @@ void SockaddrResolver::StartLocked() {
void DoNothing(void* ignored) {} void DoNothing(void* ignored) {}
OrphanablePtr<Resolver> CreateSockaddrResolver( bool ParseUri(const grpc_uri* uri,
ResolverArgs args, bool parse(const grpc_uri* uri, grpc_resolved_address* dst),
bool parse(const grpc_uri* uri, grpc_resolved_address* dst)) { ServerAddressList* addresses) {
if (0 != strcmp(args.uri->authority, "")) { if (0 != strcmp(uri->authority, "")) {
gpr_log(GPR_ERROR, "authority-based URIs not supported by the %s scheme", gpr_log(GPR_ERROR, "authority-based URIs not supported by the %s scheme",
args.uri->scheme); uri->scheme);
return nullptr; return false;
} }
// Construct addresses. // Construct addresses.
grpc_slice path_slice = grpc_slice path_slice =
grpc_slice_new(args.uri->path, strlen(args.uri->path), DoNothing); grpc_slice_new(uri->path, strlen(uri->path), DoNothing);
grpc_slice_buffer path_parts; grpc_slice_buffer path_parts;
grpc_slice_buffer_init(&path_parts); grpc_slice_buffer_init(&path_parts);
grpc_slice_split(path_slice, ",", &path_parts); grpc_slice_split(path_slice, ",", &path_parts);
ServerAddressList addresses;
bool errors_found = false; bool errors_found = false;
for (size_t i = 0; i < path_parts.count; i++) { for (size_t i = 0; i < path_parts.count; i++) {
grpc_uri ith_uri = *args.uri; grpc_uri ith_uri = *uri;
UniquePtr<char> part_str(grpc_slice_to_c_string(path_parts.slices[i])); UniquePtr<char> part_str(grpc_slice_to_c_string(path_parts.slices[i]));
ith_uri.path = part_str.get(); ith_uri.path = part_str.get();
grpc_resolved_address addr; grpc_resolved_address addr;
@ -105,13 +104,20 @@ OrphanablePtr<Resolver> CreateSockaddrResolver(
errors_found = true; errors_found = true;
break; break;
} }
addresses.emplace_back(addr, nullptr /* args */); if (addresses != nullptr) {
addresses->emplace_back(addr, nullptr /* args */);
}
} }
grpc_slice_buffer_destroy_internal(&path_parts); grpc_slice_buffer_destroy_internal(&path_parts);
grpc_slice_unref_internal(path_slice); grpc_slice_unref_internal(path_slice);
if (errors_found) { return !errors_found;
return OrphanablePtr<Resolver>(nullptr); }
}
OrphanablePtr<Resolver> CreateSockaddrResolver(
ResolverArgs args,
bool parse(const grpc_uri* uri, grpc_resolved_address* dst)) {
ServerAddressList addresses;
if (!ParseUri(args.uri, parse, &addresses)) return nullptr;
// Instantiate resolver. // Instantiate resolver.
return OrphanablePtr<Resolver>( return OrphanablePtr<Resolver>(
New<SockaddrResolver>(std::move(addresses), std::move(args))); New<SockaddrResolver>(std::move(addresses), std::move(args)));
@ -119,6 +125,10 @@ OrphanablePtr<Resolver> CreateSockaddrResolver(
class IPv4ResolverFactory : public ResolverFactory { class IPv4ResolverFactory : public ResolverFactory {
public: public:
bool IsValidUri(const grpc_uri* uri) const override {
return ParseUri(uri, grpc_parse_ipv4, nullptr);
}
OrphanablePtr<Resolver> CreateResolver(ResolverArgs args) const override { OrphanablePtr<Resolver> CreateResolver(ResolverArgs args) const override {
return CreateSockaddrResolver(std::move(args), grpc_parse_ipv4); return CreateSockaddrResolver(std::move(args), grpc_parse_ipv4);
} }
@ -128,6 +138,10 @@ class IPv4ResolverFactory : public ResolverFactory {
class IPv6ResolverFactory : public ResolverFactory { class IPv6ResolverFactory : public ResolverFactory {
public: public:
bool IsValidUri(const grpc_uri* uri) const override {
return ParseUri(uri, grpc_parse_ipv6, nullptr);
}
OrphanablePtr<Resolver> CreateResolver(ResolverArgs args) const override { OrphanablePtr<Resolver> CreateResolver(ResolverArgs args) const override {
return CreateSockaddrResolver(std::move(args), grpc_parse_ipv6); return CreateSockaddrResolver(std::move(args), grpc_parse_ipv6);
} }
@ -138,6 +152,10 @@ class IPv6ResolverFactory : public ResolverFactory {
#ifdef GRPC_HAVE_UNIX_SOCKET #ifdef GRPC_HAVE_UNIX_SOCKET
class UnixResolverFactory : public ResolverFactory { class UnixResolverFactory : public ResolverFactory {
public: public:
bool IsValidUri(const grpc_uri* uri) const override {
return ParseUri(uri, grpc_parse_unix, nullptr);
}
OrphanablePtr<Resolver> CreateResolver(ResolverArgs args) const override { OrphanablePtr<Resolver> CreateResolver(ResolverArgs args) const override {
return CreateSockaddrResolver(std::move(args), grpc_parse_unix); return CreateSockaddrResolver(std::move(args), grpc_parse_unix);
} }

@ -47,6 +47,10 @@ struct ResolverArgs {
class ResolverFactory { class ResolverFactory {
public: public:
/// Returns a bool indicating whether the input uri is valid to create a
/// resolver.
virtual bool IsValidUri(const grpc_uri* uri) const GRPC_ABSTRACT;
/// Returns a new resolver instance. /// Returns a new resolver instance.
virtual OrphanablePtr<Resolver> CreateResolver(ResolverArgs args) const virtual OrphanablePtr<Resolver> CreateResolver(ResolverArgs args) const
GRPC_ABSTRACT; GRPC_ABSTRACT;

@ -132,6 +132,17 @@ ResolverFactory* ResolverRegistry::LookupResolverFactory(const char* scheme) {
return g_state->LookupResolverFactory(scheme); return g_state->LookupResolverFactory(scheme);
} }
bool ResolverRegistry::IsValidTarget(const char* target) {
grpc_uri* uri = nullptr;
char* canonical_target = nullptr;
ResolverFactory* factory =
g_state->FindResolverFactory(target, &uri, &canonical_target);
bool result = factory == nullptr ? false : factory->IsValidUri(uri);
grpc_uri_destroy(uri);
gpr_free(canonical_target);
return result;
}
OrphanablePtr<Resolver> ResolverRegistry::CreateResolver( OrphanablePtr<Resolver> ResolverRegistry::CreateResolver(
const char* target, const grpc_channel_args* args, const char* target, const grpc_channel_args* args,
grpc_pollset_set* pollset_set, grpc_combiner* combiner, grpc_pollset_set* pollset_set, grpc_combiner* combiner,

@ -50,6 +50,9 @@ class ResolverRegistry {
static void RegisterResolverFactory(UniquePtr<ResolverFactory> factory); static void RegisterResolverFactory(UniquePtr<ResolverFactory> factory);
}; };
/// Checks whether the user input \a target is valid to create a resolver.
static bool IsValidTarget(const char* target);
/// Creates a resolver given \a target. /// Creates a resolver given \a target.
/// First tries to parse \a target as a URI. If this succeeds, tries /// First tries to parse \a target as a URI. If this succeeds, tries
/// to locate a registered resolver factory based on the URI scheme. /// to locate a registered resolver factory based on the URI scheme.

Loading…
Cancel
Save