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/support/port_platform.h> 23 24 #include <stddef.h> 25 26 #include "absl/container/flat_hash_set.h" 27 #include "absl/status/statusor.h" 28 29 #include <grpc/event_engine/event_engine.h> 30 31 #include "src/core/lib/event_engine/handle_containers.h" 32 #include "src/core/lib/gprpp/orphanable.h" 33 #include "src/core/lib/gprpp/time.h" 34 #include "src/core/lib/iomgr/pollset_set.h" 35 #include "src/core/lib/iomgr/port.h" 36 #include "src/core/lib/iomgr/resolved_address.h" 37 38 #define GRPC_MAX_SOCKADDR_SIZE 128 39 40 namespace grpc_core { 41 extern const char* kDefaultSecurePort; 42 constexpr int kDefaultSecurePortInt = 443; 43 constexpr Duration kDefaultDNSRequestTimeout = Duration::Minutes(2); 44 45 // A singleton class used for async and blocking DNS resolution 46 class DNSResolver { 47 public: 48 /// Task handle for DNS Resolution requests. 49 struct LookupTaskHandle { 50 intptr_t keys[2]; 51 static const LookupTaskHandle kInvalid; 52 friend bool operator==(const LookupTaskHandle& lhs, 53 const LookupTaskHandle& rhs); 54 friend bool operator!=(const LookupTaskHandle& lhs, 55 const LookupTaskHandle& rhs); 56 }; 57 using TaskHandle = LookupTaskHandle; 58 using TaskHandleSet = absl::flat_hash_set< 59 TaskHandle, 60 grpc_event_engine::experimental::TaskHandleComparator<TaskHandle>::Hash>; 61 62 static const TaskHandle kNullHandle; 63 ~DNSResolver()64 virtual ~DNSResolver() {} 65 66 static std::string HandleToString(TaskHandle handle); 67 68 // Asynchronously resolve name. Use \a default_port if a port isn't designated 69 // in \a name, otherwise use the port in \a name. On completion, \a on_done is 70 // invoked with the result. 71 // 72 // Note for implementations: calls may acquire locks in \a on_done which 73 // were previously held while starting the request. Therefore, 74 // implementations must not invoke \a on_done inline from the call site that 75 // starts the request. The DNSCallbackExecCtxScheduler utility may help 76 // address this. 77 // 78 // \a interested_parties may be deleted after a request is cancelled. 79 virtual TaskHandle LookupHostname( 80 std::function<void(absl::StatusOr<std::vector<grpc_resolved_address>>)> 81 on_resolved, 82 absl::string_view name, absl::string_view default_port, Duration timeout, 83 grpc_pollset_set* interested_parties, absl::string_view name_server) = 0; 84 85 // Resolve name in a blocking fashion. Use \a default_port if a port isn't 86 // designated in \a name, otherwise use the port in \a name. 87 virtual absl::StatusOr<std::vector<grpc_resolved_address>> 88 LookupHostnameBlocking(absl::string_view name, 89 absl::string_view default_port) = 0; 90 91 // Asynchronously resolve an SRV Record to Hostnames. 92 // On completion, \a on_done is invoked with the result. 93 // 94 // The same caveats in \a LookupHostname apply here as well. 95 // 96 // TODO(hork): return std::vector<SRVRecord> and ask the client to do the 97 // subsequent hostname lookups. 98 virtual TaskHandle LookupSRV( 99 std::function<void(absl::StatusOr<std::vector<grpc_resolved_address>>)> 100 on_resolved, 101 absl::string_view name, Duration timeout, 102 grpc_pollset_set* interested_parties, absl::string_view name_server) = 0; 103 104 // Asynchronously resolve a TXT Record. On completion, \a on_done is invoked 105 // with the resulting string. 106 // 107 // The same caveats in \a LookupHostname apply here. 108 virtual TaskHandle LookupTXT( 109 std::function<void(absl::StatusOr<std::string>)> on_resolved, 110 absl::string_view name, Duration timeout, 111 grpc_pollset_set* interested_parties, absl::string_view name_server) = 0; 112 113 // This shares the same semantics with \a EventEngine::Cancel: successfully 114 // cancelled lookups will not have their callbacks executed, and this 115 // method returns true. If a TaskHandle is unknown, this method should return 116 // false. 117 virtual bool Cancel(TaskHandle handle) = 0; 118 }; 119 120 // Override the active DNS resolver which should be used for all DNS 121 // resolution in gRPC. 122 void ResetDNSResolver(std::shared_ptr<DNSResolver> resolver); 123 124 // Get the singleton DNS resolver instance which should be used for all 125 // DNS resolution in gRPC. 126 std::shared_ptr<DNSResolver> GetDNSResolver(); 127 128 } // namespace grpc_core 129 130 #endif // GRPC_SRC_CORE_LIB_IOMGR_RESOLVE_ADDRESS_H 131