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_WINSOCK_SOCKET
23
24 #include "src/core/lib/iomgr/sockaddr.h"
25
26 #include "src/core/lib/iomgr/resolve_address.h"
27
28 #include <inttypes.h>
29 #include <string.h>
30 #include <sys/types.h>
31
32 #include <string>
33
34 #include "absl/strings/str_format.h"
35
36 #include <grpc/support/alloc.h>
37 #include <grpc/support/log.h>
38 #include <grpc/support/log_windows.h>
39 #include <grpc/support/string_util.h>
40 #include <grpc/support/time.h>
41
42 #include "src/core/lib/gpr/string.h"
43 #include "src/core/lib/gprpp/host_port.h"
44 #include "src/core/lib/gprpp/thd.h"
45 #include "src/core/lib/iomgr/block_annotate.h"
46 #include "src/core/lib/iomgr/executor.h"
47 #include "src/core/lib/iomgr/iomgr_internal.h"
48 #include "src/core/lib/iomgr/sockaddr_utils.h"
49
50 struct request {
51 char* name;
52 char* default_port;
53 grpc_closure request_closure;
54 grpc_closure* on_done;
55 grpc_resolved_addresses** addresses;
56 };
windows_blocking_resolve_address(const char * name,const char * default_port,grpc_resolved_addresses ** addresses)57 static grpc_error* windows_blocking_resolve_address(
58 const char* name, const char* default_port,
59 grpc_resolved_addresses** addresses) {
60 grpc_core::ExecCtx exec_ctx;
61 struct addrinfo hints;
62 struct addrinfo *result = NULL, *resp;
63 int s;
64 size_t i;
65 grpc_error* error = GRPC_ERROR_NONE;
66
67 /* parse name, splitting it into host and port parts */
68 std::string host;
69 std::string port;
70 grpc_core::SplitHostPort(name, &host, &port);
71 if (host.empty()) {
72 error = GRPC_ERROR_CREATE_FROM_COPIED_STRING(
73 absl::StrFormat("unparseable host:port: '%s'", name).c_str());
74 goto done;
75 }
76 if (port.empty()) {
77 if (default_port == NULL) {
78 error = GRPC_ERROR_CREATE_FROM_COPIED_STRING(
79 absl::StrFormat("no port in name '%s'", name).c_str());
80 goto done;
81 }
82 port = default_port;
83 }
84
85 /* Call getaddrinfo */
86 memset(&hints, 0, sizeof(hints));
87 hints.ai_family = AF_UNSPEC; /* ipv4 or ipv6 */
88 hints.ai_socktype = SOCK_STREAM; /* stream socket */
89 hints.ai_flags = AI_PASSIVE; /* for wildcard IP address */
90
91 GRPC_SCHEDULING_START_BLOCKING_REGION;
92 s = getaddrinfo(host.c_str(), port.c_str(), &hints, &result);
93 GRPC_SCHEDULING_END_BLOCKING_REGION;
94 if (s != 0) {
95 error = GRPC_WSA_ERROR(WSAGetLastError(), "getaddrinfo");
96 goto done;
97 }
98
99 /* Success path: set addrs non-NULL, fill it in */
100 (*addresses) =
101 (grpc_resolved_addresses*)gpr_malloc(sizeof(grpc_resolved_addresses));
102 (*addresses)->naddrs = 0;
103 for (resp = result; resp != NULL; resp = resp->ai_next) {
104 (*addresses)->naddrs++;
105 }
106 (*addresses)->addrs = (grpc_resolved_address*)gpr_malloc(
107 sizeof(grpc_resolved_address) * (*addresses)->naddrs);
108 i = 0;
109 for (resp = result; resp != NULL; resp = resp->ai_next) {
110 memcpy(&(*addresses)->addrs[i].addr, resp->ai_addr, resp->ai_addrlen);
111 (*addresses)->addrs[i].len = resp->ai_addrlen;
112 i++;
113 }
114
115 done:
116 if (result) {
117 freeaddrinfo(result);
118 }
119 return error;
120 }
121
122 /* Callback to be passed to grpc_executor to asynch-ify
123 * grpc_blocking_resolve_address */
do_request_thread(void * rp,grpc_error * error)124 static void do_request_thread(void* rp, grpc_error* error) {
125 request* r = (request*)rp;
126 if (error == GRPC_ERROR_NONE) {
127 error =
128 grpc_blocking_resolve_address(r->name, r->default_port, r->addresses);
129 } else {
130 GRPC_ERROR_REF(error);
131 }
132 grpc_core::ExecCtx::Run(DEBUG_LOCATION, r->on_done, error);
133 gpr_free(r->name);
134 gpr_free(r->default_port);
135 gpr_free(r);
136 }
137
windows_resolve_address(const char * name,const char * default_port,grpc_pollset_set * interested_parties,grpc_closure * on_done,grpc_resolved_addresses ** addresses)138 static void windows_resolve_address(const char* name, const char* default_port,
139 grpc_pollset_set* interested_parties,
140 grpc_closure* on_done,
141 grpc_resolved_addresses** addresses) {
142 request* r = (request*)gpr_malloc(sizeof(request));
143 GRPC_CLOSURE_INIT(&r->request_closure, do_request_thread, r, nullptr);
144 r->name = gpr_strdup(name);
145 r->default_port = gpr_strdup(default_port);
146 r->on_done = on_done;
147 r->addresses = addresses;
148 grpc_core::Executor::Run(&r->request_closure, GRPC_ERROR_NONE,
149 grpc_core::ExecutorType::RESOLVER);
150 }
151
152 grpc_address_resolver_vtable grpc_windows_resolver_vtable = {
153 windows_resolve_address, windows_blocking_resolve_address};
154 #endif
155