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