3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <grpc/support/port_platform.h>
21 #include "src/core/lib/iomgr/port.h"
22 #ifdef GRPC_WINSOCK_SOCKET
26 #include <sys/types.h>
30 #include "absl/strings/str_format.h"
32 #include <grpc/support/alloc.h>
33 #include <grpc/support/log.h>
34 #include <grpc/support/log_windows.h>
35 #include <grpc/support/string_util.h>
36 #include <grpc/support/time.h>
38 #include "src/core/lib/address_utils/sockaddr_utils.h"
39 #include "src/core/lib/gpr/string.h"
40 #include "src/core/lib/gprpp/host_port.h"
41 #include "src/core/lib/gprpp/thd.h"
42 #include "src/core/lib/iomgr/block_annotate.h"
43 #include "src/core/lib/iomgr/executor.h"
44 #include "src/core/lib/iomgr/iomgr_internal.h"
45 #include "src/core/lib/iomgr/resolve_address.h"
46 #include "src/core/lib/iomgr/sockaddr.h"
51 grpc_closure request_closure;
52 grpc_closure* on_done;
53 grpc_resolved_addresses** addresses;
55 static grpc_error_handle windows_blocking_resolve_address(
56 const char* name, const char* default_port,
57 grpc_resolved_addresses** addresses) {
58 grpc_core::ExecCtx exec_ctx;
59 struct addrinfo hints;
60 struct addrinfo *result = NULL, *resp;
63 grpc_error_handle error = GRPC_ERROR_NONE;
65 /* parse name, splitting it into host and port parts */
68 grpc_core::SplitHostPort(name, &host, &port);
70 error = GRPC_ERROR_CREATE_FROM_CPP_STRING(
71 absl::StrFormat("unparseable host:port: '%s'", name));
75 if (default_port == NULL) {
76 error = GRPC_ERROR_CREATE_FROM_CPP_STRING(
77 absl::StrFormat("no port in name '%s'", name));
83 /* Call getaddrinfo */
84 memset(&hints, 0, sizeof(hints));
85 hints.ai_family = AF_UNSPEC; /* ipv4 or ipv6 */
86 hints.ai_socktype = SOCK_STREAM; /* stream socket */
87 hints.ai_flags = AI_PASSIVE; /* for wildcard IP address */
89 GRPC_SCHEDULING_START_BLOCKING_REGION;
90 s = getaddrinfo(host.c_str(), port.c_str(), &hints, &result);
91 GRPC_SCHEDULING_END_BLOCKING_REGION;
93 error = GRPC_WSA_ERROR(WSAGetLastError(), "getaddrinfo");
97 /* Success path: set addrs non-NULL, fill it in */
99 (grpc_resolved_addresses*)gpr_malloc(sizeof(grpc_resolved_addresses));
100 (*addresses)->naddrs = 0;
101 for (resp = result; resp != NULL; resp = resp->ai_next) {
102 (*addresses)->naddrs++;
104 (*addresses)->addrs = (grpc_resolved_address*)gpr_malloc(
105 sizeof(grpc_resolved_address) * (*addresses)->naddrs);
107 for (resp = result; resp != NULL; resp = resp->ai_next) {
108 memcpy(&(*addresses)->addrs[i].addr, resp->ai_addr, resp->ai_addrlen);
109 (*addresses)->addrs[i].len = resp->ai_addrlen;
115 freeaddrinfo(result);
120 /* Callback to be passed to grpc_executor to asynch-ify
121 * grpc_blocking_resolve_address */
122 static void do_request_thread(void* rp, grpc_error_handle error) {
123 request* r = (request*)rp;
124 if (error == GRPC_ERROR_NONE) {
126 grpc_blocking_resolve_address(r->name, r->default_port, r->addresses);
128 GRPC_ERROR_REF(error);
130 grpc_core::ExecCtx::Run(DEBUG_LOCATION, r->on_done, error);
132 gpr_free(r->default_port);
136 static void windows_resolve_address(const char* name, const char* default_port,
137 grpc_pollset_set* interested_parties,
138 grpc_closure* on_done,
139 grpc_resolved_addresses** addresses) {
140 request* r = (request*)gpr_malloc(sizeof(request));
141 GRPC_CLOSURE_INIT(&r->request_closure, do_request_thread, r, nullptr);
142 r->name = gpr_strdup(name);
143 r->default_port = gpr_strdup(default_port);
144 r->on_done = on_done;
145 r->addresses = addresses;
146 grpc_core::Executor::Run(&r->request_closure, GRPC_ERROR_NONE,
147 grpc_core::ExecutorType::RESOLVER);
150 grpc_address_resolver_vtable grpc_windows_resolver_vtable = {
151 windows_resolve_address, windows_blocking_resolve_address};