Imported Upstream version 1.41.0
[platform/upstream/grpc.git] / src / core / lib / iomgr / resolve_address_windows.cc
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 <inttypes.h>
25 #include <string.h>
26 #include <sys/types.h>
27
28 #include <string>
29
30 #include "absl/strings/str_format.h"
31
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>
37
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"
47
48 struct request {
49   char* name;
50   char* default_port;
51   grpc_closure request_closure;
52   grpc_closure* on_done;
53   grpc_resolved_addresses** addresses;
54 };
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;
61   int s;
62   size_t i;
63   grpc_error_handle error = GRPC_ERROR_NONE;
64
65   /* parse name, splitting it into host and port parts */
66   std::string host;
67   std::string port;
68   grpc_core::SplitHostPort(name, &host, &port);
69   if (host.empty()) {
70     error = GRPC_ERROR_CREATE_FROM_CPP_STRING(
71         absl::StrFormat("unparseable host:port: '%s'", name));
72     goto done;
73   }
74   if (port.empty()) {
75     if (default_port == NULL) {
76       error = GRPC_ERROR_CREATE_FROM_CPP_STRING(
77           absl::StrFormat("no port in name '%s'", name));
78       goto done;
79     }
80     port = default_port;
81   }
82
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 */
88
89   GRPC_SCHEDULING_START_BLOCKING_REGION;
90   s = getaddrinfo(host.c_str(), port.c_str(), &hints, &result);
91   GRPC_SCHEDULING_END_BLOCKING_REGION;
92   if (s != 0) {
93     error = GRPC_WSA_ERROR(WSAGetLastError(), "getaddrinfo");
94     goto done;
95   }
96
97   /* Success path: set addrs non-NULL, fill it in */
98   (*addresses) =
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++;
103   }
104   (*addresses)->addrs = (grpc_resolved_address*)gpr_malloc(
105       sizeof(grpc_resolved_address) * (*addresses)->naddrs);
106   i = 0;
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;
110     i++;
111   }
112
113 done:
114   if (result) {
115     freeaddrinfo(result);
116   }
117   return error;
118 }
119
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) {
125     error =
126         grpc_blocking_resolve_address(r->name, r->default_port, r->addresses);
127   } else {
128     GRPC_ERROR_REF(error);
129   }
130   grpc_core::ExecCtx::Run(DEBUG_LOCATION, r->on_done, error);
131   gpr_free(r->name);
132   gpr_free(r->default_port);
133   gpr_free(r);
134 }
135
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);
148 }
149
150 grpc_address_resolver_vtable grpc_windows_resolver_vtable = {
151     windows_resolve_address, windows_blocking_resolve_address};
152 #endif