• 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 "src/core/lib/iomgr/sockaddr.h"
25 
26 #include "src/core/lib/iomgr/resolve_address.h"
27 
28 #include <string.h>
29 #include <sys/types.h>
30 
31 #include <grpc/support/alloc.h>
32 #include <grpc/support/log.h>
33 #include <grpc/support/string_util.h>
34 #include <grpc/support/time.h>
35 
36 #include "src/core/lib/gpr/string.h"
37 #include "src/core/lib/gpr/useful.h"
38 #include "src/core/lib/gprpp/host_port.h"
39 #include "src/core/lib/gprpp/thd.h"
40 #include "src/core/lib/iomgr/block_annotate.h"
41 #include "src/core/lib/iomgr/executor.h"
42 #include "src/core/lib/iomgr/iomgr_internal.h"
43 #include "src/core/lib/iomgr/unix_sockets_posix.h"
44 
posix_blocking_resolve_address(const char * name,const char * default_port,grpc_resolved_addresses ** addresses)45 static grpc_error* posix_blocking_resolve_address(
46     const char* name, const char* default_port,
47     grpc_resolved_addresses** addresses) {
48   grpc_core::ExecCtx exec_ctx;
49   struct addrinfo hints;
50   struct addrinfo *result = nullptr, *resp;
51   int s;
52   size_t i;
53   grpc_error* err;
54 
55   if (name[0] == 'u' && name[1] == 'n' && name[2] == 'i' && name[3] == 'x' &&
56       name[4] == ':' && name[5] != 0) {
57     return grpc_resolve_unix_domain_address(name + 5, addresses);
58   }
59 
60   std::string host;
61   std::string port;
62   /* parse name, splitting it into host and port parts */
63   grpc_core::SplitHostPort(name, &host, &port);
64   if (host.empty()) {
65     err = grpc_error_set_str(
66         GRPC_ERROR_CREATE_FROM_STATIC_STRING("unparseable host:port"),
67         GRPC_ERROR_STR_TARGET_ADDRESS, grpc_slice_from_copied_string(name));
68     goto done;
69   }
70   if (port.empty()) {
71     if (default_port == nullptr) {
72       err = grpc_error_set_str(
73           GRPC_ERROR_CREATE_FROM_STATIC_STRING("no port in name"),
74           GRPC_ERROR_STR_TARGET_ADDRESS, grpc_slice_from_copied_string(name));
75       goto done;
76     }
77     port = default_port;
78   }
79 
80   /* Call getaddrinfo */
81   memset(&hints, 0, sizeof(hints));
82   hints.ai_family = AF_UNSPEC;     /* ipv4 or ipv6 */
83   hints.ai_socktype = SOCK_STREAM; /* stream socket */
84   hints.ai_flags = AI_PASSIVE;     /* for wildcard IP address */
85 
86   GRPC_SCHEDULING_START_BLOCKING_REGION;
87   s = getaddrinfo(host.c_str(), port.c_str(), &hints, &result);
88   GRPC_SCHEDULING_END_BLOCKING_REGION;
89 
90   if (s != 0) {
91     /* Retry if well-known service name is recognized */
92     const char* svc[][2] = {{"http", "80"}, {"https", "443"}};
93     for (i = 0; i < GPR_ARRAY_SIZE(svc); i++) {
94       if (port == svc[i][0]) {
95         GRPC_SCHEDULING_START_BLOCKING_REGION;
96         s = getaddrinfo(host.c_str(), svc[i][1], &hints, &result);
97         GRPC_SCHEDULING_END_BLOCKING_REGION;
98         break;
99       }
100     }
101   }
102 
103   if (s != 0) {
104     err = grpc_error_set_str(
105         grpc_error_set_str(
106             grpc_error_set_str(
107                 grpc_error_set_int(
108                     GRPC_ERROR_CREATE_FROM_STATIC_STRING(gai_strerror(s)),
109                     GRPC_ERROR_INT_ERRNO, s),
110                 GRPC_ERROR_STR_OS_ERROR,
111                 grpc_slice_from_static_string(gai_strerror(s))),
112             GRPC_ERROR_STR_SYSCALL,
113             grpc_slice_from_static_string("getaddrinfo")),
114         GRPC_ERROR_STR_TARGET_ADDRESS, grpc_slice_from_copied_string(name));
115     goto done;
116   }
117 
118   /* Success path: set addrs non-NULL, fill it in */
119   *addresses = static_cast<grpc_resolved_addresses*>(
120       gpr_malloc(sizeof(grpc_resolved_addresses)));
121   (*addresses)->naddrs = 0;
122   for (resp = result; resp != nullptr; resp = resp->ai_next) {
123     (*addresses)->naddrs++;
124   }
125   (*addresses)->addrs = static_cast<grpc_resolved_address*>(
126       gpr_malloc(sizeof(grpc_resolved_address) * (*addresses)->naddrs));
127   i = 0;
128   for (resp = result; resp != nullptr; resp = resp->ai_next) {
129     memcpy(&(*addresses)->addrs[i].addr, resp->ai_addr, resp->ai_addrlen);
130     (*addresses)->addrs[i].len = resp->ai_addrlen;
131     i++;
132   }
133   err = GRPC_ERROR_NONE;
134 
135 done:
136   if (result) {
137     freeaddrinfo(result);
138   }
139   return err;
140 }
141 
142 struct request {
143   char* name;
144   char* default_port;
145   grpc_closure* on_done;
146   grpc_resolved_addresses** addrs_out;
147   grpc_closure request_closure;
148   void* arg;
149 };
150 /* Callback to be passed to grpc Executor to asynch-ify
151  * grpc_blocking_resolve_address */
do_request_thread(void * rp,grpc_error *)152 static void do_request_thread(void* rp, grpc_error* /*error*/) {
153   request* r = static_cast<request*>(rp);
154   grpc_core::ExecCtx::Run(
155       DEBUG_LOCATION, r->on_done,
156       grpc_blocking_resolve_address(r->name, r->default_port, r->addrs_out));
157   gpr_free(r->name);
158   gpr_free(r->default_port);
159   gpr_free(r);
160 }
161 
posix_resolve_address(const char * name,const char * default_port,grpc_pollset_set *,grpc_closure * on_done,grpc_resolved_addresses ** addrs)162 static void posix_resolve_address(const char* name, const char* default_port,
163                                   grpc_pollset_set* /*interested_parties*/,
164                                   grpc_closure* on_done,
165                                   grpc_resolved_addresses** addrs) {
166   request* r = static_cast<request*>(gpr_malloc(sizeof(request)));
167   GRPC_CLOSURE_INIT(&r->request_closure, do_request_thread, r, nullptr);
168   r->name = gpr_strdup(name);
169   r->default_port = gpr_strdup(default_port);
170   r->on_done = on_done;
171   r->addrs_out = addrs;
172   grpc_core::Executor::Run(&r->request_closure, GRPC_ERROR_NONE,
173                            grpc_core::ExecutorType::RESOLVER);
174 }
175 
176 grpc_address_resolver_vtable grpc_posix_resolver_vtable = {
177     posix_resolve_address, posix_blocking_resolve_address};
178 #endif
179