Debian Codegraph @

src/core/lib/iomgr/resolve_address_posix.cc

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