Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc / deps / grpc / 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 "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 <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/gpr/string.h"
39 #include "src/core/lib/gprpp/host_port.h"
40 #include "src/core/lib/gprpp/thd.h"
41 #include "src/core/lib/iomgr/block_annotate.h"
42 #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"
45
46 typedef struct {
47   char* name;
48   char* default_port;
49   grpc_closure request_closure;
50   grpc_closure* on_done;
51   grpc_resolved_addresses** addresses;
52 } request;
53
54 static grpc_error* windows_blocking_resolve_address(
55     const char* name, const char* default_port,
56     grpc_resolved_addresses** addresses) {
57   grpc_core::ExecCtx exec_ctx;
58   struct addrinfo hints;
59   struct addrinfo *result = NULL, *resp;
60   int s;
61   size_t i;
62   grpc_error* error = GRPC_ERROR_NONE;
63
64   /* parse name, splitting it into host and port parts */
65   grpc_core::UniquePtr<char> host;
66   grpc_core::UniquePtr<char> port;
67   grpc_core::SplitHostPort(name, &host, &port);
68   if (host == NULL) {
69     char* msg;
70     gpr_asprintf(&msg, "unparseable host:port: '%s'", name);
71     error = GRPC_ERROR_CREATE_FROM_COPIED_STRING(msg);
72     gpr_free(msg);
73     goto done;
74   }
75   if (port == NULL) {
76     if (default_port == NULL) {
77       char* msg;
78       gpr_asprintf(&msg, "no port in name '%s'", name);
79       error = GRPC_ERROR_CREATE_FROM_COPIED_STRING(msg);
80       gpr_free(msg);
81       goto done;
82     }
83     port.reset(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
92   GRPC_SCHEDULING_START_BLOCKING_REGION;
93   s = getaddrinfo(host.get(), port.get(), &hints, &result);
94   GRPC_SCHEDULING_END_BLOCKING_REGION;
95   if (s != 0) {
96     error = GRPC_WSA_ERROR(WSAGetLastError(), "getaddrinfo");
97     goto done;
98   }
99
100   /* Success path: set addrs non-NULL, fill it in */
101   (*addresses) =
102       (grpc_resolved_addresses*)gpr_malloc(sizeof(grpc_resolved_addresses));
103   (*addresses)->naddrs = 0;
104   for (resp = result; resp != NULL; resp = resp->ai_next) {
105     (*addresses)->naddrs++;
106   }
107   (*addresses)->addrs = (grpc_resolved_address*)gpr_malloc(
108       sizeof(grpc_resolved_address) * (*addresses)->naddrs);
109   i = 0;
110   for (resp = result; resp != NULL; resp = resp->ai_next) {
111     memcpy(&(*addresses)->addrs[i].addr, resp->ai_addr, resp->ai_addrlen);
112     (*addresses)->addrs[i].len = resp->ai_addrlen;
113     i++;
114   }
115
116   {
117     for (i = 0; i < (*addresses)->naddrs; i++) {
118       char* buf;
119       grpc_sockaddr_to_string(&buf, &(*addresses)->addrs[i], 0);
120       gpr_free(buf);
121     }
122   }
123
124 done:
125   if (result) {
126     freeaddrinfo(result);
127   }
128   return error;
129 }
130
131 /* Callback to be passed to grpc_executor to asynch-ify
132  * grpc_blocking_resolve_address */
133 static void do_request_thread(void* rp, grpc_error* error) {
134   request* r = (request*)rp;
135   if (error == GRPC_ERROR_NONE) {
136     error =
137         grpc_blocking_resolve_address(r->name, r->default_port, r->addresses);
138   } else {
139     GRPC_ERROR_REF(error);
140   }
141   GRPC_CLOSURE_SCHED(r->on_done, error);
142   gpr_free(r->name);
143   gpr_free(r->default_port);
144   gpr_free(r);
145 }
146
147 static void windows_resolve_address(const char* name, const char* default_port,
148                                     grpc_pollset_set* interested_parties,
149                                     grpc_closure* on_done,
150                                     grpc_resolved_addresses** addresses) {
151   request* r = (request*)gpr_malloc(sizeof(request));
152   GRPC_CLOSURE_INIT(
153       &r->request_closure, do_request_thread, r,
154       grpc_core::Executor::Scheduler(grpc_core::ExecutorType::RESOLVER,
155                                      grpc_core::ExecutorJobType::SHORT));
156   r->name = gpr_strdup(name);
157   r->default_port = gpr_strdup(default_port);
158   r->on_done = on_done;
159   r->addresses = addresses;
160   GRPC_CLOSURE_SCHED(&r->request_closure, GRPC_ERROR_NONE);
161 }
162
163 grpc_address_resolver_vtable grpc_windows_resolver_vtable = {
164     windows_resolve_address, windows_blocking_resolve_address};
165 #endif