Debian Codegraph @

src/core/lib/iomgr/resolve_address_windows.cc

raw
1 //
2 // Copyright 2015 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include <grpc/support/port_platform.h>
18
19 #include "src/core/lib/iomgr/port.h"
20 #ifdef GRPC_WINSOCK_SOCKET
21
22 #include <inttypes.h>
23 #include <string.h>
24 #include <sys/types.h>
25
26 #include <string>
27
28 #include "absl/strings/str_format.h"
29
30 #include <grpc/support/alloc.h>
31 #include <grpc/support/log.h>
32 #include <grpc/support/log_windows.h>
33 #include <grpc/support/string_util.h>
34 #include <grpc/support/time.h>
35
36 #include "src/core/lib/address_utils/sockaddr_utils.h"
37 #include "src/core/lib/event_engine/default_event_engine.h"
38 #include "src/core/lib/gpr/string.h"
39 #include "src/core/lib/gprpp/host_port.h"
40 #include "src/core/lib/gprpp/thd.h"
41 #include "src/core/lib/iomgr/block_annotate.h"
42 #include "src/core/lib/iomgr/exec_ctx.h"
43 #include "src/core/lib/iomgr/executor.h"
44 #include "src/core/lib/iomgr/iomgr_internal.h"
45 #include "src/core/lib/iomgr/resolve_address.h"
46 #include "src/core/lib/iomgr/resolve_address_windows.h"
47 #include "src/core/lib/iomgr/sockaddr.h"
48 #include "src/core/lib/transport/error_utils.h"
49
50 namespace grpc_core {
51 namespace {
52
53 class NativeDNSRequest {
54 public:
55 NativeDNSRequest(
56 absl::string_view name, absl::string_view default_port,
57 std::function<void(absl::StatusOr<std::vector<grpc_resolved_address>>)>
58 on_done)
59 : name_(name), default_port_(default_port), on_done_(std::move(on_done)) {
60 GRPC_CLOSURE_INIT(&request_closure_, DoRequestThread, this, nullptr);
61 Executor::Run(&request_closure_, absl::OkStatus(), ExecutorType::RESOLVER);
62 }
63
64 private:
65 // Callback to be passed to grpc Executor to asynch-ify
66 // LookupHostnameBlocking
67 static void DoRequestThread(void* rp, grpc_error_handle /*error*/) {
68 NativeDNSRequest* r = static_cast<NativeDNSRequest*>(rp);
69 auto result =
70 GetDNSResolver()->LookupHostnameBlocking(r->name_, r->default_port_);
71 // running inline is safe since we've already been scheduled on the executor
72 r->on_done_(std::move(result));
73 delete r;
74 }
75
76 const std::string name_;
77 const std::string default_port_;
78 const std::function<void(absl::StatusOr<std::vector<grpc_resolved_address>>)>
79 on_done_;
80 grpc_closure request_closure_;
81 };
82
83 } // namespace
84
85 NativeDNSResolver::NativeDNSResolver() {}
86
87 DNSResolver::TaskHandle NativeDNSResolver::LookupHostname(
88 std::function<void(absl::StatusOr<std::vector<grpc_resolved_address>>)>
89 on_resolved,
90 absl::string_view name, absl::string_view default_port,
91 Duration /* timeout */, grpc_pollset_set* /* interested_parties */,
92 absl::string_view /* name_server */) {
93 new NativeDNSRequest(name, default_port, std::move(on_resolved));
94 return kNullHandle;
95 }
96
97 absl::StatusOr<std::vector<grpc_resolved_address>>
98 NativeDNSResolver::LookupHostnameBlocking(absl::string_view name,
99 absl::string_view default_port) {
100 ExecCtx exec_ctx;
101 struct addrinfo hints;
102 struct addrinfo *result = NULL, *resp;
103 int s;
104 grpc_error_handle error;
105 std::vector<grpc_resolved_address> addresses;
106
107 // parse name, splitting it into host and port parts
108 std::string host;
109 std::string port;
110 SplitHostPort(name, &host, &port);
111 if (host.empty()) {
112 error =
113 GRPC_ERROR_CREATE(absl::StrFormat("unparseable host:port: '%s'", name));
114 goto done;
115 }
116 if (port.empty()) {
117 if (default_port.empty()) {
118 error = GRPC_ERROR_CREATE(absl::StrFormat("no port in name '%s'", name));
119 goto done;
120 }
121 port = std::string(default_port);
122 }
123
124 // Call getaddrinfo
125 memset(&hints, 0, sizeof(hints));
126 hints.ai_family = AF_UNSPEC; /* ipv4 or ipv6 */
127 hints.ai_socktype = SOCK_STREAM; /* stream socket */
128 hints.ai_flags = AI_PASSIVE; /* for wildcard IP address */
129
130 GRPC_SCHEDULING_START_BLOCKING_REGION;
131 s = getaddrinfo(host.c_str(), port.c_str(), &hints, &result);
132 GRPC_SCHEDULING_END_BLOCKING_REGION;
133 if (s != 0) {
134 error = GRPC_WSA_ERROR(WSAGetLastError(), "getaddrinfo");
135 goto done;
136 }
137
138 // Success path: set addrs non-NULL, fill it in
139 for (resp = result; resp != NULL; resp = resp->ai_next) {
140 grpc_resolved_address addr;
141 memcpy(&addr.addr, resp->ai_addr, resp->ai_addrlen);
142 addr.len = resp->ai_addrlen;
143 addresses.push_back(addr);
144 }
145
146 done:
147 if (result) {
148 freeaddrinfo(result);
149 }
150 if (error.ok()) {
151 return addresses;
152 }
153 auto error_result = grpc_error_to_absl_status(error);
154 return error_result;
155 }
156
157 void RunCallbackOnDefaultEventEngine(absl::AnyInvocable<void()> f) {
158 auto engine = grpc_event_engine::experimental::GetDefaultEventEngine();
159 engine->Run([f = std::move(f), engine]() mutable { f(); });
160 }
161
162 DNSResolver::TaskHandle NativeDNSResolver::LookupSRV(
163 std::function<void(absl::StatusOr<std::vector<grpc_resolved_address>>)>
164 on_resolved,
165 absl::string_view /* name */, Duration /* deadline */,
166 grpc_pollset_set* /* interested_parties */,
167 absl::string_view /* name_server */) {
168 RunCallbackOnDefaultEventEngine([on_resolved] {
169 ApplicationCallbackExecCtx app_exec_ctx;
170 ExecCtx exec_ctx;
171 on_resolved(absl::UnimplementedError(
172 "The Native resolver does not support looking up SRV records"));
173 });
174 return {-1, -1};
175 };
176
177 DNSResolver::TaskHandle NativeDNSResolver::LookupTXT(
178 std::function<void(absl::StatusOr<std::string>)> on_resolved,
179 absl::string_view /* name */, Duration /* timeout */,
180 grpc_pollset_set* /* interested_parties */,
181 absl::string_view /* name_server */) {
182 // Not supported
183 RunCallbackOnDefaultEventEngine([on_resolved] {
184 ApplicationCallbackExecCtx app_exec_ctx;
185 ExecCtx exec_ctx;
186 on_resolved(absl::UnimplementedError(
187 "The Native resolver does not support looking up TXT records"));
188 });
189 return {-1, -1};
190 };
191
192 bool NativeDNSResolver::Cancel(TaskHandle /*handle*/) { return false; }
193
194 } // namespace grpc_core
195
196 #endif