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 #include <grpc/support/port_platform.h>
20
21 #include "src/core/lib/iomgr/port.h"
22 #ifdef GRPC_POSIX_SOCKET_RESOLVE_ADDRESS
23
24 #include <string.h>
25 #include <sys/types.h>
26
27 #include <grpc/support/alloc.h>
28 #include <grpc/support/log.h>
29 #include <grpc/support/string_util.h>
30 #include <grpc/support/time.h>
31
32 #include "src/core/lib/event_engine/default_event_engine.h"
33 #include "src/core/lib/gpr/string.h"
34 #include "src/core/lib/gpr/useful.h"
35 #include "src/core/lib/gprpp/crash.h"
36 #include "src/core/lib/gprpp/host_port.h"
37 #include "src/core/lib/gprpp/thd.h"
38 #include "src/core/lib/iomgr/block_annotate.h"
39 #include "src/core/lib/iomgr/exec_ctx.h"
40 #include "src/core/lib/iomgr/executor.h"
41 #include "src/core/lib/iomgr/iomgr_internal.h"
42 #include "src/core/lib/iomgr/resolve_address.h"
43 #include "src/core/lib/iomgr/resolve_address_posix.h"
44 #include "src/core/lib/iomgr/sockaddr.h"
45 #include "src/core/lib/iomgr/unix_sockets_posix.h"
46 #include "src/core/lib/transport/error_utils.h"
47
48 namespace grpc_core {
49 namespace {
50
51 class NativeDNSRequest {
52 public:
NativeDNSRequest(absl::string_view name,absl::string_view default_port,std::function<void (absl::StatusOr<std::vector<grpc_resolved_address>>)> on_done)53 NativeDNSRequest(
54 absl::string_view name, absl::string_view default_port,
55 std::function<void(absl::StatusOr<std::vector<grpc_resolved_address>>)>
56 on_done)
57 : name_(name), default_port_(default_port), on_done_(std::move(on_done)) {
58 GRPC_CLOSURE_INIT(&request_closure_, DoRequestThread, this, nullptr);
59 Executor::Run(&request_closure_, absl::OkStatus(), ExecutorType::RESOLVER);
60 }
61
62 private:
63 // Callback to be passed to grpc Executor to asynch-ify
64 // LookupHostnameBlocking
DoRequestThread(void * rp,grpc_error_handle)65 static void DoRequestThread(void* rp, grpc_error_handle /*error*/) {
66 NativeDNSRequest* r = static_cast<NativeDNSRequest*>(rp);
67 auto result =
68 GetDNSResolver()->LookupHostnameBlocking(r->name_, r->default_port_);
69 // running inline is safe since we've already been scheduled on the executor
70 r->on_done_(std::move(result));
71 delete r;
72 }
73
74 const std::string name_;
75 const std::string default_port_;
76 const std::function<void(absl::StatusOr<std::vector<grpc_resolved_address>>)>
77 on_done_;
78 grpc_closure request_closure_;
79 };
80
81 } // namespace
82
LookupHostname(std::function<void (absl::StatusOr<std::vector<grpc_resolved_address>>)> on_done,absl::string_view name,absl::string_view default_port,Duration,grpc_pollset_set *,absl::string_view)83 DNSResolver::TaskHandle NativeDNSResolver::LookupHostname(
84 std::function<void(absl::StatusOr<std::vector<grpc_resolved_address>>)>
85 on_done,
86 absl::string_view name, absl::string_view default_port,
87 Duration /* timeout */, grpc_pollset_set* /* interested_parties */,
88 absl::string_view /* name_server */) {
89 // self-deleting class
90 new NativeDNSRequest(name, default_port, std::move(on_done));
91 return kNullHandle;
92 }
93
94 absl::StatusOr<std::vector<grpc_resolved_address>>
LookupHostnameBlocking(absl::string_view name,absl::string_view default_port)95 NativeDNSResolver::LookupHostnameBlocking(absl::string_view name,
96 absl::string_view default_port) {
97 ExecCtx exec_ctx;
98 struct addrinfo hints;
99 struct addrinfo *result = nullptr, *resp;
100 int s;
101 size_t i;
102 grpc_error_handle err;
103 std::vector<grpc_resolved_address> addresses;
104 std::string host;
105 std::string port;
106 // parse name, splitting it into host and port parts
107 SplitHostPort(name, &host, &port);
108 if (host.empty()) {
109 err = grpc_error_set_str(GRPC_ERROR_CREATE("unparseable host:port"),
110 StatusStrProperty::kTargetAddress, name);
111 goto done;
112 }
113 if (port.empty()) {
114 if (default_port.empty()) {
115 err = grpc_error_set_str(GRPC_ERROR_CREATE("no port in name"),
116 StatusStrProperty::kTargetAddress, name);
117 goto done;
118 }
119 port = std::string(default_port);
120 }
121 // Call getaddrinfo
122 memset(&hints, 0, sizeof(hints));
123 hints.ai_family = AF_UNSPEC; // ipv4 or ipv6
124 hints.ai_socktype = SOCK_STREAM; // stream socket
125 hints.ai_flags = AI_PASSIVE; // for wildcard IP address
126 GRPC_SCHEDULING_START_BLOCKING_REGION;
127 s = getaddrinfo(host.c_str(), port.c_str(), &hints, &result);
128 GRPC_SCHEDULING_END_BLOCKING_REGION;
129 if (s != 0) {
130 // Retry if well-known service name is recognized
131 const char* svc[][2] = {{"http", "80"}, {"https", "443"}};
132 for (i = 0; i < GPR_ARRAY_SIZE(svc); i++) {
133 if (port == svc[i][0]) {
134 GRPC_SCHEDULING_START_BLOCKING_REGION;
135 s = getaddrinfo(host.c_str(), svc[i][1], &hints, &result);
136 GRPC_SCHEDULING_END_BLOCKING_REGION;
137 break;
138 }
139 }
140 }
141 if (s != 0) {
142 err = grpc_error_set_str(
143 grpc_error_set_str(
144 grpc_error_set_str(
145 grpc_error_set_int(GRPC_ERROR_CREATE(gai_strerror(s)),
146 StatusIntProperty::kErrorNo, s),
147 StatusStrProperty::kOsError, gai_strerror(s)),
148 StatusStrProperty::kSyscall, "getaddrinfo"),
149 StatusStrProperty::kTargetAddress, name);
150 goto done;
151 }
152 // Success path: fill in addrs
153 for (resp = result; resp != nullptr; resp = resp->ai_next) {
154 grpc_resolved_address addr;
155 memcpy(&addr.addr, resp->ai_addr, resp->ai_addrlen);
156 addr.len = resp->ai_addrlen;
157 addresses.push_back(addr);
158 }
159 err = absl::OkStatus();
160 done:
161 if (result) {
162 freeaddrinfo(result);
163 }
164 if (err.ok()) {
165 return addresses;
166 }
167 auto error_result = grpc_error_to_absl_status(err);
168 return error_result;
169 }
170
LookupSRV(std::function<void (absl::StatusOr<std::vector<grpc_resolved_address>>)> on_resolved,absl::string_view,Duration,grpc_pollset_set *,absl::string_view)171 DNSResolver::TaskHandle NativeDNSResolver::LookupSRV(
172 std::function<void(absl::StatusOr<std::vector<grpc_resolved_address>>)>
173 on_resolved,
174 absl::string_view /* name */, Duration /* timeout */,
175 grpc_pollset_set* /* interested_parties */,
176 absl::string_view /* name_server */) {
177 grpc_event_engine::experimental::GetDefaultEventEngine()->Run([on_resolved] {
178 ApplicationCallbackExecCtx app_exec_ctx;
179 ExecCtx exec_ctx;
180 on_resolved(absl::UnimplementedError(
181 "The Native resolver does not support looking up SRV records"));
182 });
183 return {-1, -1};
184 };
185
LookupTXT(std::function<void (absl::StatusOr<std::string>)> on_resolved,absl::string_view,Duration,grpc_pollset_set *,absl::string_view)186 DNSResolver::TaskHandle NativeDNSResolver::LookupTXT(
187 std::function<void(absl::StatusOr<std::string>)> on_resolved,
188 absl::string_view /* name */, Duration /* timeout */,
189 grpc_pollset_set* /* interested_parties */,
190 absl::string_view /* name_server */) {
191 // Not supported
192 grpc_event_engine::experimental::GetDefaultEventEngine()->Run([on_resolved] {
193 ApplicationCallbackExecCtx app_exec_ctx;
194 ExecCtx exec_ctx;
195 on_resolved(absl::UnimplementedError(
196 "The Native resolver does not support looking up TXT records"));
197 });
198 return {-1, -1};
199 };
200
Cancel(TaskHandle)201 bool NativeDNSResolver::Cancel(TaskHandle /*handle*/) { return false; }
202
203 } // namespace grpc_core
204
205 #endif
206