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/host_port.h"
37 #include "src/core/lib/gpr/string.h"
38 #include "src/core/lib/gpr/useful.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 char* host;
52 char* port;
53 int s;
54 size_t i;
55 grpc_error* err;
56
57 if (name[0] == 'u' && name[1] == 'n' && name[2] == 'i' && name[3] == 'x' &&
58 name[4] == ':' && name[5] != 0) {
59 return grpc_resolve_unix_domain_address(name + 5, addresses);
60 }
61
62 /* parse name, splitting it into host and port parts */
63 gpr_split_host_port(name, &host, &port);
64 if (host == nullptr) {
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 == nullptr) {
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 = gpr_strdup(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, port, &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 (strcmp(port, svc[i][0]) == 0) {
95 GRPC_SCHEDULING_START_BLOCKING_REGION;
96 s = getaddrinfo(host, 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("OS Error"),
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 gpr_free(host);
137 gpr_free(port);
138 if (result) {
139 freeaddrinfo(result);
140 }
141 return err;
142 }
143
144 typedef struct {
145 char* name;
146 char* default_port;
147 grpc_closure* on_done;
148 grpc_resolved_addresses** addrs_out;
149 grpc_closure request_closure;
150 void* arg;
151 } request;
152
153 /* Callback to be passed to grpc_executor to asynch-ify
154 * grpc_blocking_resolve_address */
do_request_thread(void * rp,grpc_error * error)155 static void do_request_thread(void* rp, grpc_error* error) {
156 request* r = static_cast<request*>(rp);
157 GRPC_CLOSURE_SCHED(r->on_done, grpc_blocking_resolve_address(
158 r->name, r->default_port, r->addrs_out));
159 gpr_free(r->name);
160 gpr_free(r->default_port);
161 gpr_free(r);
162 }
163
posix_resolve_address(const char * name,const char * default_port,grpc_pollset_set * interested_parties,grpc_closure * on_done,grpc_resolved_addresses ** addrs)164 static void posix_resolve_address(const char* name, const char* default_port,
165 grpc_pollset_set* interested_parties,
166 grpc_closure* on_done,
167 grpc_resolved_addresses** addrs) {
168 request* r = static_cast<request*>(gpr_malloc(sizeof(request)));
169 GRPC_CLOSURE_INIT(
170 &r->request_closure, do_request_thread, r,
171 grpc_executor_scheduler(GRPC_RESOLVER_EXECUTOR, GRPC_EXECUTOR_SHORT));
172 r->name = gpr_strdup(name);
173 r->default_port = gpr_strdup(default_port);
174 r->on_done = on_done;
175 r->addrs_out = addrs;
176 GRPC_CLOSURE_SCHED(&r->request_closure, GRPC_ERROR_NONE);
177 }
178
179 grpc_address_resolver_vtable grpc_posix_resolver_vtable = {
180 posix_resolve_address, posix_blocking_resolve_address};
181 #endif
182