blob: 3e977dca2da25d877592e27a4d81f9feaf8c3550 [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"
murgatroid99623dd4f2016-08-08 17:31:27 -070022#ifdef GRPC_WINSOCK_SOCKET
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
Yash Tibrewalfcd26bc2017-09-25 15:08:28 -070028#include <inttypes.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080029#include <string.h>
Craig Tillere1e45592016-03-11 08:01:58 -080030#include <sys/types.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080031
32#include <grpc/support/alloc.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080033#include <grpc/support/log.h>
Yuchen Zeng12dfdc32016-04-26 22:05:41 -070034#include <grpc/support/log_windows.h>
Masood Malekghassemi701af602015-06-03 15:01:17 -070035#include <grpc/support/string_util.h>
nnoble0c475f02014-12-05 15:37:39 -080036#include <grpc/support/time.h>
Vijay Paiae376bf2018-01-25 22:54:02 -080037
38#include "src/core/lib/gpr/host_port.h"
Mark D. Rothdbdf4952018-01-18 11:21:12 -080039#include "src/core/lib/gpr/string.h"
Vijay Paida693552018-02-16 22:59:03 -080040#include "src/core/lib/gprpp/thd.h"
Craig Tiller6b7c1fb2017-07-19 15:45:03 -070041#include "src/core/lib/iomgr/block_annotate.h"
Craig Tiller9533d042016-03-25 17:11:06 -070042#include "src/core/lib/iomgr/executor.h"
43#include "src/core/lib/iomgr/iomgr_internal.h"
44#include "src/core/lib/iomgr/sockaddr_utils.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080045
46typedef struct {
Craig Tillerbaa14a92017-11-03 09:09:36 -070047 char* name;
48 char* default_port;
David Garcia Quintas706b70f2015-10-13 11:19:49 -070049 grpc_closure request_closure;
Craig Tillerbaa14a92017-11-03 09:09:36 -070050 grpc_closure* on_done;
51 grpc_resolved_addresses** addresses;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080052} request;
53
kpayson64539f5062018-03-12 19:16:30 -070054static grpc_error* windows_blocking_resolve_address(
Craig Tillerbaa14a92017-11-03 09:09:36 -070055 const char* name, const char* default_port,
56 grpc_resolved_addresses** addresses) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -080057 grpc_core::ExecCtx exec_ctx;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080058 struct addrinfo hints;
59 struct addrinfo *result = NULL, *resp;
Craig Tillerbaa14a92017-11-03 09:09:36 -070060 char* host;
61 char* port;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080062 int s;
63 size_t i;
Craig Tillerbaa14a92017-11-03 09:09:36 -070064 grpc_error* error = GRPC_ERROR_NONE;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080065
66 /* parse name, splitting it into host and port parts */
Nicolas "Pixel" Noble589cba22015-02-20 22:58:54 -080067 gpr_split_host_port(name, &host, &port);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080068 if (host == NULL) {
Craig Tillerbaa14a92017-11-03 09:09:36 -070069 char* msg;
Craig Tillera41ac572016-05-17 16:08:17 -070070 gpr_asprintf(&msg, "unparseable host:port: '%s'", name);
ncteisen4b36a3d2017-03-13 19:08:06 -070071 error = GRPC_ERROR_CREATE_FROM_COPIED_STRING(msg);
Craig Tillera41ac572016-05-17 16:08:17 -070072 gpr_free(msg);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080073 goto done;
74 }
75 if (port == NULL) {
76 if (default_port == NULL) {
Craig Tillerbaa14a92017-11-03 09:09:36 -070077 char* msg;
Craig Tillera41ac572016-05-17 16:08:17 -070078 gpr_asprintf(&msg, "no port in name '%s'", name);
ncteisen4b36a3d2017-03-13 19:08:06 -070079 error = GRPC_ERROR_CREATE_FROM_COPIED_STRING(msg);
Craig Tillera41ac572016-05-17 16:08:17 -070080 gpr_free(msg);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080081 goto done;
82 }
83 port = gpr_strdup(default_port);
84 }
85
86 /* Call getaddrinfo */
87 memset(&hints, 0, sizeof(hints));
88 hints.ai_family = AF_UNSPEC; /* ipv4 or ipv6 */
89 hints.ai_socktype = SOCK_STREAM; /* stream socket */
90 hints.ai_flags = AI_PASSIVE; /* for wildcard IP address */
91
vjpai9839d282015-09-24 17:55:18 -070092 GRPC_SCHEDULING_START_BLOCKING_REGION;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080093 s = getaddrinfo(host, port, &hints, &result);
Yash Tibrewal8cf14702017-12-06 09:47:54 -080094 GRPC_SCHEDULING_END_BLOCKING_REGION;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080095 if (s != 0) {
Craig Tillere4512252016-05-17 16:30:33 -070096 error = GRPC_WSA_ERROR(WSAGetLastError(), "getaddrinfo");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080097 goto done;
98 }
99
100 /* Success path: set addrs non-NULL, fill it in */
Yash Tibrewald8b84a22017-09-25 13:38:03 -0700101 (*addresses) =
Craig Tillerbaa14a92017-11-03 09:09:36 -0700102 (grpc_resolved_addresses*)gpr_malloc(sizeof(grpc_resolved_addresses));
Craig Tillera41ac572016-05-17 16:08:17 -0700103 (*addresses)->naddrs = 0;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800104 for (resp = result; resp != NULL; resp = resp->ai_next) {
Craig Tillera41ac572016-05-17 16:08:17 -0700105 (*addresses)->naddrs++;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800106 }
Craig Tillerbaa14a92017-11-03 09:09:36 -0700107 (*addresses)->addrs = (grpc_resolved_address*)gpr_malloc(
Yash Tibrewald8b84a22017-09-25 13:38:03 -0700108 sizeof(grpc_resolved_address) * (*addresses)->naddrs);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800109 i = 0;
110 for (resp = result; resp != NULL; resp = resp->ai_next) {
Craig Tillera41ac572016-05-17 16:08:17 -0700111 memcpy(&(*addresses)->addrs[i].addr, resp->ai_addr, resp->ai_addrlen);
112 (*addresses)->addrs[i].len = resp->ai_addrlen;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800113 i++;
114 }
115
nnoble0c475f02014-12-05 15:37:39 -0800116 {
Craig Tillera41ac572016-05-17 16:08:17 -0700117 for (i = 0; i < (*addresses)->naddrs; i++) {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700118 char* buf;
Michael Lumish77c6cce2016-09-26 13:51:57 -0700119 grpc_sockaddr_to_string(&buf, &(*addresses)->addrs[i], 0);
nnoble0c475f02014-12-05 15:37:39 -0800120 gpr_free(buf);
121 }
122 }
123
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800124done:
125 gpr_free(host);
126 gpr_free(port);
127 if (result) {
128 freeaddrinfo(result);
129 }
Craig Tillera41ac572016-05-17 16:08:17 -0700130 return error;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800131}
132
David Garcia Quintas706b70f2015-10-13 11:19:49 -0700133/* Callback to be passed to grpc_executor to asynch-ify
134 * grpc_blocking_resolve_address */
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800135static void do_request_thread(void* rp, grpc_error* error) {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700136 request* r = (request*)rp;
Craig Tillera41ac572016-05-17 16:08:17 -0700137 if (error == GRPC_ERROR_NONE) {
Craig Tiller3e149f32016-05-17 16:11:04 -0700138 error =
139 grpc_blocking_resolve_address(r->name, r->default_port, r->addresses);
Craig Tillera41ac572016-05-17 16:08:17 -0700140 } else {
141 GRPC_ERROR_REF(error);
142 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800143 GRPC_CLOSURE_SCHED(r->on_done, error);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800144 gpr_free(r->name);
145 gpr_free(r->default_port);
146 gpr_free(r);
147}
148
kpayson64539f5062018-03-12 19:16:30 -0700149static void windows_resolve_address(const char* name, const char* default_port,
150 grpc_pollset_set* interested_parties,
151 grpc_closure* on_done,
152 grpc_resolved_addresses** addresses) {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700153 request* r = (request*)gpr_malloc(sizeof(request));
Sree Kuchibhotla37d8bbc2018-07-10 13:30:57 -0700154 GRPC_CLOSURE_INIT(
155 &r->request_closure, do_request_thread, r,
156 grpc_executor_scheduler(GRPC_RESOLVER_EXECUTOR, GRPC_EXECUTOR_SHORT));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800157 r->name = gpr_strdup(name);
158 r->default_port = gpr_strdup(default_port);
Craig Tillera41ac572016-05-17 16:08:17 -0700159 r->on_done = on_done;
160 r->addresses = addresses;
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800161 GRPC_CLOSURE_SCHED(&r->request_closure, GRPC_ERROR_NONE);
Craig Tiller190d3602015-02-18 09:23:38 -0800162}
Nicolas "Pixel" Noble94964fd2015-02-21 07:19:19 +0100163
kpayson64539f5062018-03-12 19:16:30 -0700164grpc_address_resolver_vtable grpc_windows_resolver_vtable = {
165 windows_resolve_address, windows_blocking_resolve_address};
Nicolas "Pixel" Noble94964fd2015-02-21 07:19:19 +0100166#endif