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 std::string host;
56 std::string port;
57 /* parse name, splitting it into host and port parts */
58 grpc_core::SplitHostPort(name, &host, &port);
59 if (host.empty()) {
60 err = grpc_error_set_str(
61 GRPC_ERROR_CREATE_FROM_STATIC_STRING("unparseable host:port"),
62 GRPC_ERROR_STR_TARGET_ADDRESS, grpc_slice_from_copied_string(name));
63 goto done;
64 }
65
66 if (port.empty()) {
67 if (default_port == nullptr) {
68 err = grpc_error_set_str(
69 GRPC_ERROR_CREATE_FROM_STATIC_STRING("no port in name"),
70 GRPC_ERROR_STR_TARGET_ADDRESS, grpc_slice_from_copied_string(name));
71 goto done;
72 }
73 port = default_port;
74 }
75
76 /* Call getaddrinfo */
77 memset(&hints, 0, sizeof(hints));
78 hints.ai_family = AF_UNSPEC; /* ipv4 or ipv6 */
79 hints.ai_socktype = SOCK_STREAM; /* stream socket */
80 hints.ai_flags = AI_PASSIVE; /* for wildcard IP address */
81
82 GRPC_SCHEDULING_START_BLOCKING_REGION;
83 s = getaddrinfo(host.c_str(), port.c_str(), &hints, &result);
84 GRPC_SCHEDULING_END_BLOCKING_REGION;
85
86 if (s != 0) {
87 /* Retry if well-known service name is recognized */
88 const char* svc[][2] = {{"http", "80"}, {"https", "443"}};
89 for (i = 0; i < GPR_ARRAY_SIZE(svc); i++) {
90 if (port == svc[i][0]) {
91 GRPC_SCHEDULING_START_BLOCKING_REGION;
92 s = getaddrinfo(host.c_str(), svc[i][1], &hints, &result);
93 GRPC_SCHEDULING_END_BLOCKING_REGION;
94 break;
95 }
96 }
97 }
98
99 if (s != 0) {
100 err = grpc_error_set_str(
101 grpc_error_set_str(
102 grpc_error_set_str(
103 grpc_error_set_int(
104 GRPC_ERROR_CREATE_FROM_STATIC_STRING(gai_strerror(s)),
105 GRPC_ERROR_INT_ERRNO, s),
106 GRPC_ERROR_STR_OS_ERROR,
107 grpc_slice_from_static_string(gai_strerror(s))),
108 GRPC_ERROR_STR_SYSCALL,
109 grpc_slice_from_static_string("getaddrinfo")),
110 GRPC_ERROR_STR_TARGET_ADDRESS, grpc_slice_from_copied_string(name));
111 goto done;
112 }
113
114 /* Success path: set addrs non-NULL, fill it in */
115 *addresses = static_cast<grpc_resolved_addresses*>(
116 gpr_malloc(sizeof(grpc_resolved_addresses)));
117 (*addresses)->naddrs = 0;
118 for (resp = result; resp != nullptr; resp = resp->ai_next) {
119 (*addresses)->naddrs++;
120 }
121 (*addresses)->addrs = static_cast<grpc_resolved_address*>(
122 gpr_malloc(sizeof(grpc_resolved_address) * (*addresses)->naddrs));
123 i = 0;
124 for (resp = result; resp != nullptr; resp = resp->ai_next) {
125 memcpy(&(*addresses)->addrs[i].addr, resp->ai_addr, resp->ai_addrlen);
126 (*addresses)->addrs[i].len = resp->ai_addrlen;
127 i++;
128 }
129 err = GRPC_ERROR_NONE;
130
131 done:
132 if (result) {
133 freeaddrinfo(result);
134 }
135 return err;
136 }
137
138 struct request {
139 char* name;
140 char* default_port;
141 grpc_closure* on_done;
142 grpc_resolved_addresses** addrs_out;
143 grpc_closure request_closure;
144 void* arg;
145 };
146 /* Callback to be passed to grpc Executor to asynch-ify
147 * grpc_blocking_resolve_address */
do_request_thread(void * rp,grpc_error *)148 static void do_request_thread(void* rp, grpc_error* /*error*/) {
149 request* r = static_cast<request*>(rp);
150 grpc_core::ExecCtx::Run(
151 DEBUG_LOCATION, r->on_done,
152 grpc_blocking_resolve_address(r->name, r->default_port, r->addrs_out));
153 gpr_free(r->name);
154 gpr_free(r->default_port);
155 gpr_free(r);
156 }
157
posix_resolve_address(const char * name,const char * default_port,grpc_pollset_set *,grpc_closure * on_done,grpc_resolved_addresses ** addrs)158 static void posix_resolve_address(const char* name, const char* default_port,
159 grpc_pollset_set* /*interested_parties*/,
160 grpc_closure* on_done,
161 grpc_resolved_addresses** addrs) {
162 request* r = static_cast<request*>(gpr_malloc(sizeof(request)));
163 GRPC_CLOSURE_INIT(&r->request_closure, do_request_thread, r, nullptr);
164 r->name = gpr_strdup(name);
165 r->default_port = gpr_strdup(default_port);
166 r->on_done = on_done;
167 r->addrs_out = addrs;
168 grpc_core::Executor::Run(&r->request_closure, GRPC_ERROR_NONE,
169 grpc_core::ExecutorType::RESOLVER);
170 }
171
172 grpc_address_resolver_vtable grpc_posix_resolver_vtable = {
173 posix_resolve_address, posix_blocking_resolve_address};
174 #endif
175