• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <grpc/support/alloc.h>
23 #include <grpc/support/log_windows.h>
24 #include <grpc/support/string_util.h>
25 #include <grpc/support/time.h>
26 #include <inttypes.h>
27 #include <string.h>
28 #include <sys/types.h>
29 
30 #include <string>
31 
32 #include "absl/strings/str_format.h"
33 #include "src/core/lib/address_utils/sockaddr_utils.h"
34 #include "src/core/lib/event_engine/default_event_engine.h"
35 #include "src/core/lib/iomgr/block_annotate.h"
36 #include "src/core/lib/iomgr/exec_ctx.h"
37 #include "src/core/lib/iomgr/executor.h"
38 #include "src/core/lib/iomgr/iomgr_internal.h"
39 #include "src/core/lib/iomgr/resolve_address.h"
40 #include "src/core/lib/iomgr/resolve_address_windows.h"
41 #include "src/core/lib/iomgr/sockaddr.h"
42 #include "src/core/lib/transport/error_utils.h"
43 #include "src/core/util/crash.h"
44 #include "src/core/util/host_port.h"
45 #include "src/core/util/string.h"
46 #include "src/core/util/thd.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 
NativeDNSResolver()83 NativeDNSResolver::NativeDNSResolver() {}
84 
LookupHostname(std::function<void (absl::StatusOr<std::vector<grpc_resolved_address>>)> on_resolved,absl::string_view name,absl::string_view default_port,Duration,grpc_pollset_set *,absl::string_view)85 DNSResolver::TaskHandle NativeDNSResolver::LookupHostname(
86     std::function<void(absl::StatusOr<std::vector<grpc_resolved_address>>)>
87         on_resolved,
88     absl::string_view name, absl::string_view default_port,
89     Duration /* timeout */, grpc_pollset_set* /* interested_parties */,
90     absl::string_view /* name_server */) {
91   new NativeDNSRequest(name, default_port, std::move(on_resolved));
92   return kNullHandle;
93 }
94 
95 absl::StatusOr<std::vector<grpc_resolved_address>>
LookupHostnameBlocking(absl::string_view name,absl::string_view default_port)96 NativeDNSResolver::LookupHostnameBlocking(absl::string_view name,
97                                           absl::string_view default_port) {
98   ExecCtx exec_ctx;
99   struct addrinfo hints;
100   struct addrinfo *result = NULL, *resp;
101   int s;
102   grpc_error_handle error;
103   std::vector<grpc_resolved_address> addresses;
104 
105   // parse name, splitting it into host and port parts
106   std::string host;
107   std::string port;
108   SplitHostPort(name, &host, &port);
109   if (host.empty()) {
110     error =
111         GRPC_ERROR_CREATE(absl::StrFormat("unparsable host:port: '%s'", name));
112     goto done;
113   }
114   if (port.empty()) {
115     if (default_port.empty()) {
116       error = GRPC_ERROR_CREATE(absl::StrFormat("no port in name '%s'", name));
117       goto done;
118     }
119     port = std::string(default_port);
120   }
121 
122   // Call getaddrinfo
123   memset(&hints, 0, sizeof(hints));
124   hints.ai_family = AF_UNSPEC;      // ipv4 or ipv6
125   hints.ai_socktype = SOCK_STREAM;  // stream socket
126   hints.ai_flags = AI_PASSIVE;      // for wildcard IP address
127 
128   GRPC_SCHEDULING_START_BLOCKING_REGION;
129   s = getaddrinfo(host.c_str(), port.c_str(), &hints, &result);
130   GRPC_SCHEDULING_END_BLOCKING_REGION;
131   if (s != 0) {
132     error = GRPC_WSA_ERROR(WSAGetLastError(), "getaddrinfo");
133     goto done;
134   }
135 
136   // Success path: set addrs non-NULL, fill it in
137   for (resp = result; resp != NULL; resp = resp->ai_next) {
138     grpc_resolved_address addr;
139     memcpy(&addr.addr, resp->ai_addr, resp->ai_addrlen);
140     addr.len = resp->ai_addrlen;
141     addresses.push_back(addr);
142   }
143 
144 done:
145   if (result) {
146     freeaddrinfo(result);
147   }
148   if (error.ok()) {
149     return addresses;
150   }
151   auto error_result = grpc_error_to_absl_status(error);
152   return error_result;
153 }
154 
RunCallbackOnDefaultEventEngine(absl::AnyInvocable<void ()> f)155 void RunCallbackOnDefaultEventEngine(absl::AnyInvocable<void()> f) {
156   auto engine = grpc_event_engine::experimental::GetDefaultEventEngine();
157   engine->Run([f = std::move(f), engine]() mutable { f(); });
158 }
159 
LookupSRV(std::function<void (absl::StatusOr<std::vector<grpc_resolved_address>>)> on_resolved,absl::string_view,Duration,grpc_pollset_set *,absl::string_view)160 DNSResolver::TaskHandle NativeDNSResolver::LookupSRV(
161     std::function<void(absl::StatusOr<std::vector<grpc_resolved_address>>)>
162         on_resolved,
163     absl::string_view /* name */, Duration /* deadline */,
164     grpc_pollset_set* /* interested_parties */,
165     absl::string_view /* name_server */) {
166   RunCallbackOnDefaultEventEngine([on_resolved] {
167     ApplicationCallbackExecCtx app_exec_ctx;
168     ExecCtx exec_ctx;
169     on_resolved(absl::UnimplementedError(
170         "The Native resolver does not support looking up SRV records"));
171   });
172   return {-1, -1};
173 };
174 
LookupTXT(std::function<void (absl::StatusOr<std::string>)> on_resolved,absl::string_view,Duration,grpc_pollset_set *,absl::string_view)175 DNSResolver::TaskHandle NativeDNSResolver::LookupTXT(
176     std::function<void(absl::StatusOr<std::string>)> on_resolved,
177     absl::string_view /* name */, Duration /* timeout */,
178     grpc_pollset_set* /* interested_parties */,
179     absl::string_view /* name_server */) {
180   // Not supported
181   RunCallbackOnDefaultEventEngine([on_resolved] {
182     ApplicationCallbackExecCtx app_exec_ctx;
183     ExecCtx exec_ctx;
184     on_resolved(absl::UnimplementedError(
185         "The Native resolver does not support looking up TXT records"));
186   });
187   return {-1, -1};
188 };
189 
Cancel(TaskHandle)190 bool NativeDNSResolver::Cancel(TaskHandle /*handle*/) { return false; }
191 
192 }  // namespace grpc_core
193 
194 #endif
195