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