Debian Codegraph @

src/core/lib/iomgr/tcp_client_windows.cc

raw
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 <inttypes.h>
22
23 #include "src/core/lib/iomgr/port.h"
24
25 #ifdef GRPC_WINSOCK_SOCKET
26
27 #include <grpc/event_engine/endpoint_config.h>
28 #include <grpc/slice_buffer.h>
29 #include <grpc/support/alloc.h>
30 #include <grpc/support/log.h>
31 #include <grpc/support/log_windows.h>
32
33 #include "src/core/lib/address_utils/sockaddr_utils.h"
34 #include "src/core/lib/iomgr/iocp_windows.h"
35 #include "src/core/lib/iomgr/sockaddr.h"
36 #include "src/core/lib/iomgr/sockaddr_windows.h"
37 #include "src/core/lib/iomgr/socket_windows.h"
38 #include "src/core/lib/iomgr/tcp_client.h"
39 #include "src/core/lib/iomgr/tcp_windows.h"
40 #include "src/core/lib/iomgr/timer.h"
41 #include "src/core/lib/resource_quota/api.h"
42 #include "src/core/lib/slice/slice_internal.h"
43
44 using ::grpc_event_engine::experimental::EndpointConfig;
45
46 struct async_connect {
47 grpc_closure* on_done;
48 gpr_mu mu;
49 grpc_winsocket* socket;
50 grpc_timer alarm;
51 grpc_closure on_alarm;
52 std::string addr_name;
53 int refs;
54 grpc_closure on_connect;
55 grpc_endpoint** endpoint;
56 };
57
58 static void async_connect_unlock_and_cleanup(async_connect* ac,
59 grpc_winsocket* socket) {
60 int done = (--ac->refs == 0);
61 gpr_mu_unlock(&ac->mu);
62 if (done) {
63 gpr_mu_destroy(&ac->mu);
64 delete ac;
65 }
66 if (socket != NULL) grpc_winsocket_destroy(socket);
67 }
68
69 static void on_alarm(void* acp, grpc_error_handle error) {
70 async_connect* ac = (async_connect*)acp;
71 gpr_mu_lock(&ac->mu);
72 grpc_winsocket* socket = ac->socket;
73 ac->socket = NULL;
74 if (socket != NULL) {
75 grpc_winsocket_shutdown(socket);
76 }
77 async_connect_unlock_and_cleanup(ac, socket);
78 }
79
80 static void on_connect(void* acp, grpc_error_handle error) {
81 async_connect* ac = (async_connect*)acp;
82 grpc_endpoint** ep = ac->endpoint;
83 GPR_ASSERT(*ep == NULL);
84 grpc_closure* on_done = ac->on_done;
85
86 gpr_mu_lock(&ac->mu);
87 grpc_winsocket* socket = ac->socket;
88 ac->socket = NULL;
89 gpr_mu_unlock(&ac->mu);
90
91 grpc_timer_cancel(&ac->alarm);
92
93 gpr_mu_lock(&ac->mu);
94
95 if (error.ok()) {
96 if (socket != NULL) {
97 DWORD transfered_bytes = 0;
98 DWORD flags;
99 BOOL wsa_success =
100 WSAGetOverlappedResult(socket->socket, &socket->write_info.overlapped,
101 &transfered_bytes, FALSE, &flags);
102 GPR_ASSERT(transfered_bytes == 0);
103 if (!wsa_success) {
104 error = GRPC_WSA_ERROR(WSAGetLastError(), "ConnectEx");
105 closesocket(socket->socket);
106 } else {
107 *ep = grpc_tcp_create(socket, ac->addr_name);
108 socket = nullptr;
109 }
110 } else {
111 error = GRPC_ERROR_CREATE("socket is null");
112 }
113 }
114
115 async_connect_unlock_and_cleanup(ac, socket);
116 /* If the connection was aborted, the callback was already called when
117 the deadline was met. */
118 grpc_core::ExecCtx::Run(DEBUG_LOCATION, on_done, error);
119 }
120
121 /* Tries to issue one async connection, then schedules both an IOCP
122 notification request for the connection, and one timeout alert. */
123 static int64_t tcp_connect(grpc_closure* on_done, grpc_endpoint** endpoint,
124 grpc_pollset_set* interested_parties,
125 const EndpointConfig& config,
126 const grpc_resolved_address* addr,
127 grpc_core::Timestamp deadline) {
128 SOCKET sock = INVALID_SOCKET;
129 BOOL success;
130 int status;
131 grpc_resolved_address addr6_v4mapped;
132 grpc_resolved_address local_address;
133 grpc_winsocket* socket = NULL;
134 LPFN_CONNECTEX ConnectEx;
135 GUID guid = WSAID_CONNECTEX;
136 DWORD ioctl_num_bytes;
137 grpc_winsocket_callback_info* info;
138 grpc_error_handle error;
139 async_connect* ac = NULL;
140 absl::StatusOr<std::string> addr_uri;
141
142 addr_uri = grpc_sockaddr_to_uri(addr);
143 if (!addr_uri.ok()) {
144 error = GRPC_ERROR_CREATE(addr_uri.status().ToString());
145 goto failure;
146 }
147
148 *endpoint = NULL;
149
150 /* Use dualstack sockets where available. */
151 if (grpc_sockaddr_to_v4mapped(addr, &addr6_v4mapped)) {
152 addr = &addr6_v4mapped;
153 }
154
155 sock = WSASocket(AF_INET6, SOCK_STREAM, IPPROTO_TCP, NULL, 0,
156 grpc_get_default_wsa_socket_flags());
157 if (sock == INVALID_SOCKET) {
158 error = GRPC_WSA_ERROR(WSAGetLastError(), "WSASocket");
159 goto failure;
160 }
161
162 error = grpc_tcp_prepare_socket(sock);
163 if (!error.ok()) {
164 goto failure;
165 }
166
167 /* Grab the function pointer for ConnectEx for that specific socket.
168 It may change depending on the interface. */
169 status =
170 WSAIoctl(sock, SIO_GET_EXTENSION_FUNCTION_POINTER, &guid, sizeof(guid),
171 &ConnectEx, sizeof(ConnectEx), &ioctl_num_bytes, NULL, NULL);
172
173 if (status != 0) {
174 error = GRPC_WSA_ERROR(WSAGetLastError(),
175 "WSAIoctl(SIO_GET_EXTENSION_FUNCTION_POINTER)");
176 goto failure;
177 }
178
179 grpc_sockaddr_make_wildcard6(0, &local_address);
180
181 status =
182 bind(sock, (grpc_sockaddr*)&local_address.addr, (int)local_address.len);
183 if (status != 0) {
184 error = GRPC_WSA_ERROR(WSAGetLastError(), "bind");
185 goto failure;
186 }
187
188 socket = grpc_winsocket_create(sock, "client");
189 info = &socket->write_info;
190 success = ConnectEx(sock, (grpc_sockaddr*)&addr->addr, (int)addr->len, NULL,
191 0, NULL, &info->overlapped);
192
193 /* It wouldn't be unusual to get a success immediately. But we'll still get
194 an IOCP notification, so let's ignore it. */
195 if (!success) {
196 int last_error = WSAGetLastError();
197 if (last_error != ERROR_IO_PENDING) {
198 error = GRPC_WSA_ERROR(last_error, "ConnectEx");
199 goto failure;
200 }
201 }
202
203 ac = new async_connect();
204 ac->on_done = on_done;
205 ac->socket = socket;
206 gpr_mu_init(&ac->mu);
207 ac->refs = 2;
208 ac->addr_name = addr_uri.value();
209 ac->endpoint = endpoint;
210 GRPC_CLOSURE_INIT(&ac->on_connect, on_connect, ac, grpc_schedule_on_exec_ctx);
211
212 GRPC_CLOSURE_INIT(&ac->on_alarm, on_alarm, ac, grpc_schedule_on_exec_ctx);
213 gpr_mu_lock(&ac->mu);
214 grpc_timer_init(&ac->alarm, deadline, &ac->on_alarm);
215 grpc_socket_notify_on_write(socket, &ac->on_connect);
216 gpr_mu_unlock(&ac->mu);
217 return 0;
218
219 failure:
220 GPR_ASSERT(!error.ok());
221 grpc_error_handle final_error = grpc_error_set_str(
222 GRPC_ERROR_CREATE_REFERENCING("Failed to connect", &error, 1),
223 grpc_core::StatusStrProperty::kTargetAddress,
224 addr_uri.ok() ? *addr_uri : addr_uri.status().ToString());
225 if (socket != NULL) {
226 grpc_winsocket_destroy(socket);
227 } else if (sock != INVALID_SOCKET) {
228 closesocket(sock);
229 }
230 grpc_core::ExecCtx::Run(DEBUG_LOCATION, on_done, final_error);
231 return 0;
232 }
233
234 static bool tcp_cancel_connect(int64_t /*connection_handle*/) { return false; }
235
236 grpc_tcp_client_vtable grpc_windows_tcp_client_vtable = {tcp_connect,
237 tcp_cancel_connect};
238
239 #endif /* GRPC_WINSOCK_SOCKET */