1 // 2 // 3 // Copyright 2015 gRPC authors. 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 // 17 // 18 19 #ifndef GRPC_SRC_CORE_LIB_IOMGR_RESOLVE_ADDRESS_H 20 #define GRPC_SRC_CORE_LIB_IOMGR_RESOLVE_ADDRESS_H 21 22 #include <grpc/event_engine/event_engine.h> 23 #include <grpc/support/port_platform.h> 24 #include <stddef.h> 25 26 #include "absl/container/flat_hash_set.h" 27 #include "absl/status/statusor.h" 28 #include "src/core/lib/event_engine/handle_containers.h" 29 #include "src/core/lib/iomgr/pollset_set.h" 30 #include "src/core/lib/iomgr/port.h" 31 #include "src/core/lib/iomgr/resolved_address.h" 32 #include "src/core/util/orphanable.h" 33 #include "src/core/util/time.h" 34 35 #define GRPC_MAX_SOCKADDR_SIZE 128 36 37 namespace grpc_core { 38 extern const char* kDefaultSecurePort; 39 constexpr int kDefaultSecurePortInt = 443; 40 constexpr Duration kDefaultDNSRequestTimeout = Duration::Minutes(2); 41 42 // A singleton class used for async and blocking DNS resolution 43 class DNSResolver { 44 public: 45 /// Task handle for DNS Resolution requests. 46 struct LookupTaskHandle { 47 intptr_t keys[2]; 48 static const LookupTaskHandle kInvalid; 49 friend bool operator==(const LookupTaskHandle& lhs, 50 const LookupTaskHandle& rhs); 51 friend bool operator!=(const LookupTaskHandle& lhs, 52 const LookupTaskHandle& rhs); 53 }; 54 using TaskHandle = LookupTaskHandle; 55 using TaskHandleSet = absl::flat_hash_set< 56 TaskHandle, 57 grpc_event_engine::experimental::TaskHandleComparator<TaskHandle>::Hash>; 58 59 static const TaskHandle kNullHandle; 60 ~DNSResolver()61 virtual ~DNSResolver() {} 62 63 static std::string HandleToString(TaskHandle handle); 64 65 // Asynchronously resolve name. Use \a default_port if a port isn't designated 66 // in \a name, otherwise use the port in \a name. On completion, \a on_done is 67 // invoked with the result. 68 // 69 // Note for implementations: calls may acquire locks in \a on_done which 70 // were previously held while starting the request. Therefore, 71 // implementations must not invoke \a on_done inline from the call site that 72 // starts the request. The DNSCallbackExecCtxScheduler utility may help 73 // address this. 74 // 75 // \a interested_parties may be deleted after a request is cancelled. 76 virtual TaskHandle LookupHostname( 77 std::function<void(absl::StatusOr<std::vector<grpc_resolved_address>>)> 78 on_resolved, 79 absl::string_view name, absl::string_view default_port, Duration timeout, 80 grpc_pollset_set* interested_parties, absl::string_view name_server) = 0; 81 82 // Resolve name in a blocking fashion. Use \a default_port if a port isn't 83 // designated in \a name, otherwise use the port in \a name. 84 virtual absl::StatusOr<std::vector<grpc_resolved_address>> 85 LookupHostnameBlocking(absl::string_view name, 86 absl::string_view default_port) = 0; 87 88 // Asynchronously resolve an SRV Record to Hostnames. 89 // On completion, \a on_done is invoked with the result. 90 // 91 // The same caveats in \a LookupHostname apply here as well. 92 // 93 // TODO(hork): return std::vector<SRVRecord> and ask the client to do the 94 // subsequent hostname lookups. 95 virtual TaskHandle LookupSRV( 96 std::function<void(absl::StatusOr<std::vector<grpc_resolved_address>>)> 97 on_resolved, 98 absl::string_view name, Duration timeout, 99 grpc_pollset_set* interested_parties, absl::string_view name_server) = 0; 100 101 // Asynchronously resolve a TXT Record. On completion, \a on_done is invoked 102 // with the resulting string. 103 // 104 // The same caveats in \a LookupHostname apply here. 105 virtual TaskHandle LookupTXT( 106 std::function<void(absl::StatusOr<std::string>)> on_resolved, 107 absl::string_view name, Duration timeout, 108 grpc_pollset_set* interested_parties, absl::string_view name_server) = 0; 109 110 // This shares the same semantics with \a EventEngine::Cancel: successfully 111 // cancelled lookups will not have their callbacks executed, and this 112 // method returns true. If a TaskHandle is unknown, this method should return 113 // false. 114 virtual bool Cancel(TaskHandle handle) = 0; 115 }; 116 117 // Override the active DNS resolver which should be used for all DNS 118 // resolution in gRPC. 119 void ResetDNSResolver(std::shared_ptr<DNSResolver> resolver); 120 121 // Get the singleton DNS resolver instance which should be used for all 122 // DNS resolution in gRPC. 123 std::shared_ptr<DNSResolver> GetDNSResolver(); 124 125 } // namespace grpc_core 126 127 #endif // GRPC_SRC_CORE_LIB_IOMGR_RESOLVE_ADDRESS_H 128