blob: 7a825643e126e28e20ce9ee98eea891f31cb3429 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080016 *
17 */
18
Alexander Polcyndb3e8982018-02-21 16:59:24 -080019#include <grpc/support/port_platform.h>
20
murgatroid9954070892016-08-08 17:01:18 -070021#include "src/core/lib/iomgr/port.h"
Muxi Yan67ff4052018-05-15 12:36:10 -070022#ifdef GRPC_POSIX_SOCKET_RESOLVE_ADDRESS
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080023
Craig Tiller9533d042016-03-25 17:11:06 -070024#include "src/core/lib/iomgr/sockaddr.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080025
murgatroid997871f732016-09-23 13:49:05 -070026#include "src/core/lib/iomgr/resolve_address.h"
27
Craig Tillere1e45592016-03-11 08:01:58 -080028#include <string.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080029#include <sys/types.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080030
31#include <grpc/support/alloc.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080032#include <grpc/support/log.h>
Masood Malekghassemi701af602015-06-03 15:01:17 -070033#include <grpc/support/string_util.h>
nnoble0c475f02014-12-05 15:37:39 -080034#include <grpc/support/time.h>
Vijay Paid4d0a302018-01-25 13:24:03 -080035
Vijay Paiae376bf2018-01-25 22:54:02 -080036#include "src/core/lib/gpr/host_port.h"
Mark D. Rothdbdf4952018-01-18 11:21:12 -080037#include "src/core/lib/gpr/string.h"
Vijay Paid4d0a302018-01-25 13:24:03 -080038#include "src/core/lib/gpr/useful.h"
Vijay Pai87e2e6a2018-02-19 22:45:47 -080039#include "src/core/lib/gprpp/thd.h"
Craig Tiller6b7c1fb2017-07-19 15:45:03 -070040#include "src/core/lib/iomgr/block_annotate.h"
Craig Tiller9533d042016-03-25 17:11:06 -070041#include "src/core/lib/iomgr/executor.h"
42#include "src/core/lib/iomgr/iomgr_internal.h"
Craig Tiller9533d042016-03-25 17:11:06 -070043#include "src/core/lib/iomgr/unix_sockets_posix.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080044
kpayson64539f5062018-03-12 19:16:30 -070045static grpc_error* posix_blocking_resolve_address(
Craig Tillerbaa14a92017-11-03 09:09:36 -070046 const char* name, const char* default_port,
47 grpc_resolved_addresses** addresses) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -080048 grpc_core::ExecCtx exec_ctx;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080049 struct addrinfo hints;
Craig Tiller4782d922017-11-10 09:53:21 -080050 struct addrinfo *result = nullptr, *resp;
Craig Tillerbaa14a92017-11-03 09:09:36 -070051 char* host;
52 char* port;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080053 int s;
54 size_t i;
Craig Tillerbaa14a92017-11-03 09:09:36 -070055 grpc_error* err;
Craig Tillerae7fe922015-02-13 23:16:32 -080056
Craig Tillera82950e2015-09-22 12:33:20 -070057 if (name[0] == 'u' && name[1] == 'n' && name[2] == 'i' && name[3] == 'x' &&
58 name[4] == ':' && name[5] != 0) {
Craig Tiller5b15afd2016-05-04 15:00:14 -070059 return grpc_resolve_unix_domain_address(name + 5, addresses);
Craig Tillera82950e2015-09-22 12:33:20 -070060 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080061
62 /* parse name, splitting it into host and port parts */
Craig Tillera82950e2015-09-22 12:33:20 -070063 gpr_split_host_port(name, &host, &port);
Craig Tiller4782d922017-11-10 09:53:21 -080064 if (host == nullptr) {
ncteisen4b36a3d2017-03-13 19:08:06 -070065 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));
Craig Tillera82950e2015-09-22 12:33:20 -070068 goto done;
69 }
Craig Tiller4782d922017-11-10 09:53:21 -080070 if (port == nullptr) {
71 if (default_port == nullptr) {
ncteisen4b36a3d2017-03-13 19:08:06 -070072 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));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080075 goto done;
76 }
Craig Tillera82950e2015-09-22 12:33:20 -070077 port = gpr_strdup(default_port);
78 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080079
80 /* Call getaddrinfo */
Craig Tillera82950e2015-09-22 12:33:20 -070081 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 */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080085
vjpai9839d282015-09-24 17:55:18 -070086 GRPC_SCHEDULING_START_BLOCKING_REGION;
Craig Tillera82950e2015-09-22 12:33:20 -070087 s = getaddrinfo(host, port, &hints, &result);
Yash Tibrewal8cf14702017-12-06 09:47:54 -080088 GRPC_SCHEDULING_END_BLOCKING_REGION;
Vijay Paiba130552015-09-24 13:43:01 -070089
Craig Tillera82950e2015-09-22 12:33:20 -070090 if (s != 0) {
91 /* Retry if well-known service name is recognized */
Craig Tillerbaa14a92017-11-03 09:09:36 -070092 const char* svc[][2] = {{"http", "80"}, {"https", "443"}};
Craig Tillerb9d35962015-09-11 13:31:16 -070093 for (i = 0; i < GPR_ARRAY_SIZE(svc); i++) {
Craig Tillera82950e2015-09-22 12:33:20 -070094 if (strcmp(port, svc[i][0]) == 0) {
vjpai9839d282015-09-24 17:55:18 -070095 GRPC_SCHEDULING_START_BLOCKING_REGION;
Craig Tillera82950e2015-09-22 12:33:20 -070096 s = getaddrinfo(host, svc[i][1], &hints, &result);
Yash Tibrewal8cf14702017-12-06 09:47:54 -080097 GRPC_SCHEDULING_END_BLOCKING_REGION;
Craig Tillera82950e2015-09-22 12:33:20 -070098 break;
99 }
Raul Silvera659be5a2015-03-04 11:51:11 -0800100 }
Craig Tillera82950e2015-09-22 12:33:20 -0700101 }
Raul Silvera659be5a2015-03-04 11:51:11 -0800102
Craig Tillera82950e2015-09-22 12:33:20 -0700103 if (s != 0) {
Craig Tiller5b15afd2016-05-04 15:00:14 -0700104 err = grpc_error_set_str(
105 grpc_error_set_str(
ncteisen4b36a3d2017-03-13 19:08:06 -0700106 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")),
ncteisenbbb38012017-03-10 14:58:43 -0800114 GRPC_ERROR_STR_TARGET_ADDRESS, grpc_slice_from_copied_string(name));
Craig Tillera82950e2015-09-22 12:33:20 -0700115 goto done;
116 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800117
118 /* Success path: set addrs non-NULL, fill it in */
Noah Eisen4d20a662018-02-09 09:34:04 -0800119 *addresses = static_cast<grpc_resolved_addresses*>(
120 gpr_malloc(sizeof(grpc_resolved_addresses)));
Craig Tiller5b15afd2016-05-04 15:00:14 -0700121 (*addresses)->naddrs = 0;
Craig Tiller4782d922017-11-10 09:53:21 -0800122 for (resp = result; resp != nullptr; resp = resp->ai_next) {
Craig Tiller5b15afd2016-05-04 15:00:14 -0700123 (*addresses)->naddrs++;
Craig Tillera82950e2015-09-22 12:33:20 -0700124 }
Noah Eisen4d20a662018-02-09 09:34:04 -0800125 (*addresses)->addrs = static_cast<grpc_resolved_address*>(
126 gpr_malloc(sizeof(grpc_resolved_address) * (*addresses)->naddrs));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800127 i = 0;
Craig Tiller4782d922017-11-10 09:53:21 -0800128 for (resp = result; resp != nullptr; resp = resp->ai_next) {
Craig Tiller5b15afd2016-05-04 15:00:14 -0700129 memcpy(&(*addresses)->addrs[i].addr, resp->ai_addr, resp->ai_addrlen);
130 (*addresses)->addrs[i].len = resp->ai_addrlen;
Craig Tillera82950e2015-09-22 12:33:20 -0700131 i++;
132 }
Craig Tiller5b15afd2016-05-04 15:00:14 -0700133 err = GRPC_ERROR_NONE;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800134
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800135done:
Craig Tillera82950e2015-09-22 12:33:20 -0700136 gpr_free(host);
137 gpr_free(port);
138 if (result) {
139 freeaddrinfo(result);
140 }
Craig Tiller5b15afd2016-05-04 15:00:14 -0700141 return err;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800142}
143
Craig Tiller5b15afd2016-05-04 15:00:14 -0700144typedef struct {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700145 char* name;
146 char* default_port;
147 grpc_closure* on_done;
148 grpc_resolved_addresses** addrs_out;
Craig Tiller5b15afd2016-05-04 15:00:14 -0700149 grpc_closure request_closure;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700150 void* arg;
Craig Tiller5b15afd2016-05-04 15:00:14 -0700151} request;
Craig Tillere1e45592016-03-11 08:01:58 -0800152
David Garcia Quintas4bc34632015-10-07 16:12:35 -0700153/* Callback to be passed to grpc_executor to asynch-ify
154 * grpc_blocking_resolve_address */
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800155static void do_request_thread(void* rp, grpc_error* error) {
Noah Eisenbe82e642018-02-09 09:16:55 -0800156 request* r = static_cast<request*>(rp);
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800157 GRPC_CLOSURE_SCHED(r->on_done, grpc_blocking_resolve_address(
158 r->name, r->default_port, r->addrs_out));
Craig Tillera82950e2015-09-22 12:33:20 -0700159 gpr_free(r->name);
160 gpr_free(r->default_port);
Craig Tillera82950e2015-09-22 12:33:20 -0700161 gpr_free(r);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800162}
163
kpayson64539f5062018-03-12 19:16:30 -0700164static 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) {
Noah Eisenbe82e642018-02-09 09:16:55 -0800168 request* r = static_cast<request*>(gpr_malloc(sizeof(request)));
ncteisen274bbbe2017-06-08 14:57:11 -0700169 GRPC_CLOSURE_INIT(&r->request_closure, do_request_thread, r,
Craig Tiller8ac417c2017-07-21 14:37:31 -0700170 grpc_executor_scheduler(GRPC_EXECUTOR_SHORT));
Craig Tillera82950e2015-09-22 12:33:20 -0700171 r->name = gpr_strdup(name);
172 r->default_port = gpr_strdup(default_port);
Craig Tiller5b15afd2016-05-04 15:00:14 -0700173 r->on_done = on_done;
174 r->addrs_out = addrs;
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800175 GRPC_CLOSURE_SCHED(&r->request_closure, GRPC_ERROR_NONE);
Craig Tiller190d3602015-02-18 09:23:38 -0800176}
Nicolas "Pixel" Noble94964fd2015-02-21 07:19:19 +0100177
kpayson64539f5062018-03-12 19:16:30 -0700178grpc_address_resolver_vtable grpc_posix_resolver_vtable = {
179 posix_resolve_address, posix_blocking_resolve_address};
Nicolas "Pixel" Noble94964fd2015-02-21 07:19:19 +0100180#endif