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/host_port.h"
39 #include "src/core/lib/gpr/string.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 
windows_blocking_resolve_address(const char * name,const char * default_port,grpc_resolved_addresses ** addresses)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   char* host;
61   char* port;
62   int s;
63   size_t i;
64   grpc_error* error = GRPC_ERROR_NONE;
65 
66   /* parse name, splitting it into host and port parts */
67   gpr_split_host_port(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 = 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, port, &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   gpr_free(host);
126   gpr_free(port);
127   if (result) {
128     freeaddrinfo(result);
129   }
130   return error;
131 }
132 
133 /* Callback to be passed to grpc_executor to asynch-ify
134  * grpc_blocking_resolve_address */
do_request_thread(void * rp,grpc_error * error)135 static void do_request_thread(void* rp, grpc_error* error) {
136   request* r = (request*)rp;
137   if (error == GRPC_ERROR_NONE) {
138     error =
139         grpc_blocking_resolve_address(r->name, r->default_port, r->addresses);
140   } else {
141     GRPC_ERROR_REF(error);
142   }
143   GRPC_CLOSURE_SCHED(r->on_done, error);
144   gpr_free(r->name);
145   gpr_free(r->default_port);
146   gpr_free(r);
147 }
148 
windows_resolve_address(const char * name,const char * default_port,grpc_pollset_set * interested_parties,grpc_closure * on_done,grpc_resolved_addresses ** addresses)149 static 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) {
153   request* r = (request*)gpr_malloc(sizeof(request));
154   GRPC_CLOSURE_INIT(
155       &r->request_closure, do_request_thread, r,
156       grpc_executor_scheduler(GRPC_RESOLVER_EXECUTOR, GRPC_EXECUTOR_SHORT));
157   r->name = gpr_strdup(name);
158   r->default_port = gpr_strdup(default_port);
159   r->on_done = on_done;
160   r->addresses = addresses;
161   GRPC_CLOSURE_SCHED(&r->request_closure, GRPC_ERROR_NONE);
162 }
163 
164 grpc_address_resolver_vtable grpc_windows_resolver_vtable = {
165     windows_resolve_address, windows_blocking_resolve_address};
166 #endif
167