| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
#include <ws2tcpip.h> |
| 13 |
#include <mswsock.h> |
| 14 |
#include <mstcpip.h> |
| 15 |
|
| 16 |
|
| 17 |
#include <aws/io/private/socket_impl.h> |
| 18 |
#include <aws/io/socket.h> |
| 19 |
|
| 20 |
#include <aws/common/byte_buf.h> |
| 21 |
#include <aws/common/clock.h> |
| 22 |
#include <aws/common/condition_variable.h> |
| 23 |
#include <aws/common/mutex.h> |
| 24 |
#include <aws/common/task_scheduler.h> |
| 25 |
|
| 26 |
#include <aws/io/event_loop.h> |
| 27 |
#include <aws/io/logging.h> |
| 28 |
#include <aws/io/pipe.h> |
| 29 |
#include <aws/io/private/event_loop_impl.h> |
| 30 |
|
| 31 |
#include <aws/io/io.h> |
| 32 |
#include <errno.h> |
| 33 |
#include <fcntl.h> |
| 34 |
#include <signal.h> |
| 35 |
#include <stdio.h> |
| 36 |
#include <stdlib.h> |
| 37 |
#include <string.h> |
| 38 |
|
| 39 |
#ifdef _MSC_VER |
| 40 |
# pragma warning(disable : 4221) |
| 41 |
# pragma warning(disable : 4204) |
| 42 |
#endif |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
#define IO_OPERATION_CANCELLED 0xC0000120 |
| 48 |
#define IO_STATUS_CONNECTION_REFUSED 0xC0000236 |
| 49 |
#define IO_STATUS_TIMEOUT 0x00000102 |
| 50 |
#define IO_NETWORK_UNREACHABLE 0xC000023C |
| 51 |
#define IO_HOST_UNREACHABLE 0xC000023D |
| 52 |
#define IO_CONNECTION_ABORTED 0xC0000241 |
| 53 |
#define IO_PIPE_BROKEN 0xC000014B |
| 54 |
#define SOME_ERROR_CODE_THAT_MEANS_INVALID_PATH 0x00000003 |
| 55 |
#define IO_STATUS_BUFFER_OVERFLOW 0x80000005 |
| 56 |
#define STATUS_INVALID_ADDRESS_COMPONENT 0xC0000207 |
| 57 |
|
| 58 |
#define PIPE_BUFFER_SIZE 512 |
| 59 |
|
| 60 |
struct winsock_vtable { |
| 61 |
int (*connection_success)(struct aws_socket *socket); |
| 62 |
void (*connection_error)(struct aws_socket *socket, int error_code); |
| 63 |
int (*close)(struct aws_socket *socket); |
| 64 |
int (*connect)( |
| 65 |
struct aws_socket *socket, |
| 66 |
const struct aws_socket_endpoint *remote_endpoint, |
| 67 |
struct aws_event_loop *connect_loop, |
| 68 |
aws_socket_on_connection_result_fn *on_connection_result, |
| 69 |
void *user_data); |
| 70 |
int (*start_accept)( |
| 71 |
struct aws_socket *socket, |
| 72 |
struct aws_event_loop *accept_loop, |
| 73 |
struct aws_socket_listener_options options); |
| 74 |
int (*stop_accept)(struct aws_socket *socket); |
| 75 |
int (*bind)(struct aws_socket *socket, const struct aws_socket_endpoint *local_endpoint); |
| 76 |
int (*listen)(struct aws_socket *socket, int backlog_size); |
| 77 |
int (*read)(struct aws_socket *socket, struct aws_byte_buf *buffer, size_t *amount_read); |
| 78 |
int (*subscribe_to_read)(struct aws_socket *socket, aws_socket_on_readable_fn *on_readable, void *user_data); |
| 79 |
}; |
| 80 |
|
| 81 |
static int s_ipv4_stream_connection_success(struct aws_socket *socket); |
| 82 |
static int s_ipv6_stream_connection_success(struct aws_socket *socket); |
| 83 |
static void s_connection_error(struct aws_socket *socket, int error_code); |
| 84 |
static int s_local_and_udp_connection_success(struct aws_socket *socket); |
| 85 |
static int s_ipv4_stream_connect( |
| 86 |
struct aws_socket *socket, |
| 87 |
const struct aws_socket_endpoint *remote_endpoint, |
| 88 |
struct aws_event_loop *connect_loop, |
| 89 |
aws_socket_on_connection_result_fn *on_connection_result, |
| 90 |
void *user_data); |
| 91 |
static int s_ipv4_dgram_connect( |
| 92 |
struct aws_socket *socket, |
| 93 |
const struct aws_socket_endpoint *remote_endpoint, |
| 94 |
struct aws_event_loop *connect_loop, |
| 95 |
aws_socket_on_connection_result_fn *on_connection_result, |
| 96 |
void *user_data); |
| 97 |
static int s_ipv6_stream_connect( |
| 98 |
struct aws_socket *socket, |
| 99 |
const struct aws_socket_endpoint *remote_endpoint, |
| 100 |
struct aws_event_loop *connect_loop, |
| 101 |
aws_socket_on_connection_result_fn *on_connection_result, |
| 102 |
void *user_data); |
| 103 |
static int s_ipv6_dgram_connect( |
| 104 |
struct aws_socket *socket, |
| 105 |
const struct aws_socket_endpoint *remote_endpoint, |
| 106 |
struct aws_event_loop *connect_loop, |
| 107 |
aws_socket_on_connection_result_fn *on_connection_result, |
| 108 |
void *user_data); |
| 109 |
static int s_local_connect( |
| 110 |
struct aws_socket *socket, |
| 111 |
const struct aws_socket_endpoint *remote_endpoint, |
| 112 |
struct aws_event_loop *connect_loop, |
| 113 |
aws_socket_on_connection_result_fn *on_connection_result, |
| 114 |
void *user_data); |
| 115 |
static int s_tcp_start_accept( |
| 116 |
struct aws_socket *socket, |
| 117 |
struct aws_event_loop *accept_loop, |
| 118 |
struct aws_socket_listener_options options); |
| 119 |
static int s_local_start_accept( |
| 120 |
struct aws_socket *socket, |
| 121 |
struct aws_event_loop *accept_loop, |
| 122 |
struct aws_socket_listener_options options); |
| 123 |
static int s_stream_stop_accept(struct aws_socket *socket); |
| 124 |
static int s_dgram_start_accept( |
| 125 |
struct aws_socket *socket, |
| 126 |
struct aws_event_loop *accept_loop, |
| 127 |
struct aws_socket_listener_options options); |
| 128 |
static int s_dgram_stop_accept(struct aws_socket *socket); |
| 129 |
|
| 130 |
static int s_tcp_listen(struct aws_socket *socket, int backlog_size); |
| 131 |
static int s_udp_listen(struct aws_socket *socket, int backlog_size); |
| 132 |
static int s_local_listen(struct aws_socket *socket, int backlog_size); |
| 133 |
static int s_tcp_read(struct aws_socket *socket, struct aws_byte_buf *buffer, size_t *amount_read); |
| 134 |
static int s_local_read(struct aws_socket *socket, struct aws_byte_buf *buffer, size_t *amount_read); |
| 135 |
static int s_dgram_read(struct aws_socket *socket, struct aws_byte_buf *buffer, size_t *amount_read); |
| 136 |
static int s_protocol_socket_close(struct aws_socket *socket); |
| 137 |
static int s_local_close(struct aws_socket *socket); |
| 138 |
static int s_ipv4_stream_bind(struct aws_socket *socket, const struct aws_socket_endpoint *local_endpoint); |
| 139 |
static int s_ipv4_dgram_bind(struct aws_socket *socket, const struct aws_socket_endpoint *local_endpoint); |
| 140 |
static int s_ipv6_stream_bind(struct aws_socket *socket, const struct aws_socket_endpoint *local_endpoint); |
| 141 |
static int s_ipv6_dgram_bind(struct aws_socket *socket, const struct aws_socket_endpoint *local_endpoint); |
| 142 |
static int s_local_bind(struct aws_socket *socket, const struct aws_socket_endpoint *local_endpoint); |
| 143 |
|
| 144 |
static void s_socket_clean_up(struct aws_socket *socket); |
| 145 |
static int s_socket_connect(struct aws_socket *socket, struct aws_socket_connect_options *socket_connect_options); |
| 146 |
static int s_socket_bind(struct aws_socket *socket, struct aws_socket_bind_options *socket_bind_options); |
| 147 |
static int s_socket_listen(struct aws_socket *socket, int backlog_size); |
| 148 |
static int s_socket_start_accept( |
| 149 |
struct aws_socket *socket, |
| 150 |
struct aws_event_loop *accept_loop, |
| 151 |
struct aws_socket_listener_options options); |
| 152 |
static int s_socket_stop_accept(struct aws_socket *socket); |
| 153 |
static int s_socket_set_options(struct aws_socket *socket, const struct aws_socket_options *options); |
| 154 |
static int s_socket_close(struct aws_socket *socket); |
| 155 |
static int s_socket_shutdown_dir(struct aws_socket *socket, enum aws_channel_direction dir); |
| 156 |
static int s_socket_assign_to_event_loop(struct aws_socket *socket, struct aws_event_loop *event_loop); |
| 157 |
static int s_socket_subscribe_to_readable_events( |
| 158 |
struct aws_socket *socket, |
| 159 |
aws_socket_on_readable_fn *on_readable, |
| 160 |
void *user_data); |
| 161 |
static int s_socket_read(struct aws_socket *socket, struct aws_byte_buf *buffer, size_t *amount_read); |
| 162 |
static int s_socket_write( |
| 163 |
struct aws_socket *socket, |
| 164 |
const struct aws_byte_cursor *cursor, |
| 165 |
aws_socket_on_write_completed_fn *written_fn, |
| 166 |
void *user_data); |
| 167 |
static int s_socket_get_error(struct aws_socket *socket); |
| 168 |
static bool s_socket_is_open(struct aws_socket *socket); |
| 169 |
static int s_set_close_callback(struct aws_socket *socket, aws_socket_on_shutdown_complete_fn fn, void *user_data); |
| 170 |
static int s_set_cleanup_callback(struct aws_socket *socket, aws_socket_on_shutdown_complete_fn fn, void *user_data); |
| 171 |
static struct aws_byte_buf s_socket_get_protocol_fn(const struct aws_socket *socket); |
| 172 |
static struct aws_string *s_socket_get_server_name_fn(const struct aws_socket *socket); |
| 173 |
|
| 174 |
static int s_stream_subscribe_to_read( |
| 175 |
struct aws_socket *socket, |
| 176 |
aws_socket_on_readable_fn *on_readable, |
| 177 |
void *user_data); |
| 178 |
static int s_dgram_subscribe_to_read( |
| 179 |
struct aws_socket *socket, |
| 180 |
aws_socket_on_readable_fn *on_readable, |
| 181 |
void *user_data); |
| 182 |
|
| 183 |
static int s_determine_socket_error(int error); |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
static struct winsock_vtable s_winsock_vtables[3][2] = { |
| 191 |
[AWS_SOCKET_IPV4] = |
| 192 |
{ |
| 193 |
[AWS_SOCKET_STREAM] = |
| 194 |
{ |
| 195 |
.connection_success = s_ipv4_stream_connection_success, |
| 196 |
.connection_error = s_connection_error, |
| 197 |
.connect = s_ipv4_stream_connect, |
| 198 |
.start_accept = s_tcp_start_accept, |
| 199 |
.stop_accept = s_stream_stop_accept, |
| 200 |
.bind = s_ipv4_stream_bind, |
| 201 |
.listen = s_tcp_listen, |
| 202 |
.read = s_tcp_read, |
| 203 |
.close = s_protocol_socket_close, |
| 204 |
.subscribe_to_read = s_stream_subscribe_to_read, |
| 205 |
}, |
| 206 |
[AWS_SOCKET_DGRAM] = |
| 207 |
{ |
| 208 |
.connection_success = s_local_and_udp_connection_success, |
| 209 |
.connection_error = s_connection_error, |
| 210 |
.connect = s_ipv4_dgram_connect, |
| 211 |
.start_accept = s_dgram_start_accept, |
| 212 |
.stop_accept = s_dgram_stop_accept, |
| 213 |
.bind = s_ipv4_dgram_bind, |
| 214 |
.listen = s_udp_listen, |
| 215 |
.read = s_dgram_read, |
| 216 |
.close = s_protocol_socket_close, |
| 217 |
.subscribe_to_read = s_dgram_subscribe_to_read, |
| 218 |
}, |
| 219 |
}, |
| 220 |
[AWS_SOCKET_IPV6] = |
| 221 |
{ |
| 222 |
[AWS_SOCKET_STREAM] = |
| 223 |
{ |
| 224 |
.connection_success = s_ipv6_stream_connection_success, |
| 225 |
.connection_error = s_connection_error, |
| 226 |
.connect = s_ipv6_stream_connect, |
| 227 |
.start_accept = s_tcp_start_accept, |
| 228 |
.stop_accept = s_stream_stop_accept, |
| 229 |
.bind = s_ipv6_stream_bind, |
| 230 |
.listen = s_tcp_listen, |
| 231 |
.read = s_tcp_read, |
| 232 |
.close = s_protocol_socket_close, |
| 233 |
.subscribe_to_read = s_stream_subscribe_to_read, |
| 234 |
}, |
| 235 |
[AWS_SOCKET_DGRAM] = |
| 236 |
{ |
| 237 |
.connection_success = s_local_and_udp_connection_success, |
| 238 |
.connection_error = s_connection_error, |
| 239 |
.connect = s_ipv6_dgram_connect, |
| 240 |
.start_accept = s_dgram_start_accept, |
| 241 |
.stop_accept = s_dgram_stop_accept, |
| 242 |
.bind = s_ipv6_dgram_bind, |
| 243 |
.listen = s_udp_listen, |
| 244 |
.read = s_dgram_read, |
| 245 |
.close = s_protocol_socket_close, |
| 246 |
.subscribe_to_read = s_dgram_subscribe_to_read, |
| 247 |
}, |
| 248 |
}, |
| 249 |
[AWS_SOCKET_LOCAL] = |
| 250 |
{ |
| 251 |
[AWS_SOCKET_STREAM] = |
| 252 |
{ |
| 253 |
.connection_success = s_local_and_udp_connection_success, |
| 254 |
.connection_error = s_connection_error, |
| 255 |
.connect = s_local_connect, |
| 256 |
.start_accept = s_local_start_accept, |
| 257 |
.stop_accept = s_stream_stop_accept, |
| 258 |
.bind = s_local_bind, |
| 259 |
.listen = s_local_listen, |
| 260 |
.read = s_local_read, |
| 261 |
.close = s_local_close, |
| 262 |
.subscribe_to_read = s_stream_subscribe_to_read, |
| 263 |
}, |
| 264 |
[AWS_SOCKET_DGRAM] = {0}, |
| 265 |
}, |
| 266 |
}; |
| 267 |
|
| 268 |
struct aws_socket_vtable s_winsock_vtable = { |
| 269 |
.socket_cleanup_fn = s_socket_clean_up, |
| 270 |
.socket_connect_fn = s_socket_connect, |
| 271 |
.socket_bind_fn = s_socket_bind, |
| 272 |
.socket_listen_fn = s_socket_listen, |
| 273 |
.socket_start_accept_fn = s_socket_start_accept, |
| 274 |
.socket_stop_accept_fn = s_socket_stop_accept, |
| 275 |
.socket_set_options_fn = s_socket_set_options, |
| 276 |
.socket_close_fn = s_socket_close, |
| 277 |
.socket_shutdown_dir_fn = s_socket_shutdown_dir, |
| 278 |
.socket_assign_to_event_loop_fn = s_socket_assign_to_event_loop, |
| 279 |
.socket_subscribe_to_readable_events_fn = s_socket_subscribe_to_readable_events, |
| 280 |
.socket_read_fn = s_socket_read, |
| 281 |
.socket_write_fn = s_socket_write, |
| 282 |
.socket_get_error_fn = s_socket_get_error, |
| 283 |
.socket_is_open_fn = s_socket_is_open, |
| 284 |
.socket_set_close_callback = s_set_close_callback, |
| 285 |
.socket_set_cleanup_callback = s_set_cleanup_callback, |
| 286 |
.socket_get_protocol_fn = s_socket_get_protocol_fn, |
| 287 |
.socket_get_server_name_fn = s_socket_get_server_name_fn, |
| 288 |
}; |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
enum socket_state { |
| 293 |
INIT = 0x01, |
| 294 |
CONNECTING = 0x02, |
| 295 |
CONNECTED_READ = 0x04, |
| 296 |
CONNECTED_WRITE = 0x08, |
| 297 |
CONNECTED_WAITING_ON_READABLE = 0x10, |
| 298 |
BOUND = 0x20, |
| 299 |
LISTENING = 0x40, |
| 300 |
TIMEDOUT = 0x80, |
| 301 |
CLOSED = 0x0100, |
| 302 |
ERRORED = 0x0200, |
| 303 |
}; |
| 304 |
|
| 305 |
static int s_convert_domain(enum aws_socket_domain domain) { |
| 306 |
switch (domain) { |
| 307 |
case AWS_SOCKET_IPV4: |
| 308 |
return AF_INET; |
| 309 |
case AWS_SOCKET_IPV6: |
| 310 |
return AF_INET6; |
| 311 |
case AWS_SOCKET_LOCAL: |
| 312 |
return AF_UNIX; |
| 313 |
default: |
| 314 |
AWS_ASSERT(0); |
| 315 |
return AF_INET; |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
static int s_convert_type(enum aws_socket_type type) { |
| 320 |
switch (type) { |
| 321 |
case AWS_SOCKET_STREAM: |
| 322 |
return SOCK_STREAM; |
| 323 |
case AWS_SOCKET_DGRAM: |
| 324 |
return SOCK_DGRAM; |
| 325 |
default: |
| 326 |
AWS_ASSERT(0); |
| 327 |
return SOCK_STREAM; |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
#define SOCK_STORAGE_SIZE (sizeof(struct sockaddr_storage) + 16) |
| 332 |
|
| 333 |
struct socket_connect_args { |
| 334 |
struct aws_allocator *allocator; |
| 335 |
struct aws_socket *socket; |
| 336 |
struct aws_task timeout_task; |
| 337 |
}; |
| 338 |
|
| 339 |
struct io_operation_data { |
| 340 |
struct aws_allocator *allocator; |
| 341 |
struct aws_socket *socket; |
| 342 |
struct aws_overlapped signal; |
| 343 |
struct aws_linked_list_node node; |
| 344 |
struct aws_task sequential_task_storage; |
| 345 |
bool in_use; |
| 346 |
}; |
| 347 |
|
| 348 |
struct iocp_socket { |
| 349 |
struct winsock_vtable *winsock_vtable; |
| 350 |
struct io_operation_data *read_io_data; |
| 351 |
struct aws_socket *incoming_socket; |
| 352 |
uint8_t accept_buffer[SOCK_STORAGE_SIZE * 2]; |
| 353 |
struct socket_connect_args *connect_args; |
| 354 |
struct aws_linked_list pending_io_operations; |
| 355 |
bool stop_accept; |
| 356 |
aws_socket_on_shutdown_complete_fn *on_close_complete; |
| 357 |
void *close_user_data; |
| 358 |
aws_socket_on_shutdown_complete_fn *on_cleanup_complete; |
| 359 |
void *cleanup_user_data; |
| 360 |
}; |
| 361 |
|
| 362 |
static int s_set_close_callback(struct aws_socket *socket, aws_socket_on_shutdown_complete_fn fn, void *user_data) { |
| 363 |
struct iocp_socket *socket_impl = socket->impl; |
| 364 |
socket_impl->close_user_data = user_data; |
| 365 |
socket_impl->on_close_complete = fn; |
| 366 |
return 0; |
| 367 |
} |
| 368 |
|
| 369 |
static int s_set_cleanup_callback(struct aws_socket *socket, aws_socket_on_shutdown_complete_fn fn, void *user_data) { |
| 370 |
struct iocp_socket *socket_impl = socket->impl; |
| 371 |
socket_impl->cleanup_user_data = user_data; |
| 372 |
socket_impl->on_cleanup_complete = fn; |
| 373 |
return 0; |
| 374 |
} |
| 375 |
|
| 376 |
static int s_create_socket(struct aws_socket *sock, const struct aws_socket_options *options) { |
| 377 |
SOCKET handle = socket(s_convert_domain(options->domain), s_convert_type(options->type), 0); |
| 378 |
if (handle == INVALID_SOCKET) { |
| 379 |
int wsa_err = WSAGetLastError(); |
| 380 |
AWS_LOGF_ERROR(AWS_LS_IO_SOCKET, "id=static: socket() call failed with WSAError %d", wsa_err); |
| 381 |
return aws_raise_error(s_determine_socket_error(wsa_err)); |
| 382 |
} |
| 383 |
|
| 384 |
AWS_LOGF_DEBUG( |
| 385 |
AWS_LS_IO_SOCKET, |
| 386 |
"id=%p handle=%p: initializing with domain %d and type %d", |
| 387 |
(void *)sock, |
| 388 |
(void *)handle, |
| 389 |
options->domain, |
| 390 |
options->type); |
| 391 |
u_long non_blocking = 1; |
| 392 |
if (ioctlsocket(handle, FIONBIO, &non_blocking) != 0) { |
| 393 |
int wsa_err = WSAGetLastError(); |
| 394 |
AWS_LOGF_ERROR(AWS_LS_IO_SOCKET, "id=static: ioctlsocket() call failed with WSAError %d", wsa_err); |
| 395 |
aws_raise_error(s_determine_socket_error(wsa_err)); |
| 396 |
goto error; |
| 397 |
} |
| 398 |
|
| 399 |
sock->io_handle.data.handle = (HANDLE)handle; |
| 400 |
sock->io_handle.additional_data = NULL; |
| 401 |
if (aws_socket_set_options(sock, options)) { |
| 402 |
goto error; |
| 403 |
} |
| 404 |
return AWS_OP_SUCCESS; |
| 405 |
|
| 406 |
error: |
| 407 |
closesocket(handle); |
| 408 |
sock->io_handle.data.handle = (HANDLE)INVALID_SOCKET; |
| 409 |
return AWS_OP_ERR; |
| 410 |
} |
| 411 |
|
| 412 |
static int s_socket_init( |
| 413 |
struct aws_socket *socket, |
| 414 |
struct aws_allocator *alloc, |
| 415 |
const struct aws_socket_options *options, |
| 416 |
bool create_underlying_socket) { |
| 417 |
AWS_FATAL_ASSERT(options->domain <= AWS_SOCKET_LOCAL); |
| 418 |
AWS_FATAL_ASSERT(options->type <= AWS_SOCKET_DGRAM); |
| 419 |
AWS_ZERO_STRUCT(*socket); |
| 420 |
|
| 421 |
struct iocp_socket *impl = aws_mem_calloc(alloc, 1, sizeof(struct iocp_socket)); |
| 422 |
if (!impl) { |
| 423 |
return AWS_OP_ERR; |
| 424 |
} |
| 425 |
|
| 426 |
socket->vtable = &s_winsock_vtable; |
| 427 |
|
| 428 |
impl->winsock_vtable = &s_winsock_vtables[options->domain][options->type]; |
| 429 |
if (!impl->winsock_vtable || !impl->winsock_vtable->connection_success) { |
| 430 |
aws_mem_release(alloc, impl); |
| 431 |
socket->impl = NULL; |
| 432 |
return aws_raise_error(AWS_IO_SOCKET_INVALID_OPTIONS); |
| 433 |
} |
| 434 |
|
| 435 |
impl->read_io_data = aws_mem_calloc(alloc, 1, sizeof(struct io_operation_data)); |
| 436 |
if (!impl->read_io_data) { |
| 437 |
aws_mem_release(alloc, impl); |
| 438 |
socket->impl = NULL; |
| 439 |
return AWS_OP_ERR; |
| 440 |
} |
| 441 |
|
| 442 |
impl->read_io_data->allocator = alloc; |
| 443 |
impl->read_io_data->socket = socket; |
| 444 |
impl->read_io_data->in_use = false; |
| 445 |
aws_linked_list_init(&impl->pending_io_operations); |
| 446 |
|
| 447 |
socket->allocator = alloc; |
| 448 |
socket->io_handle.data.handle = INVALID_HANDLE_VALUE; |
| 449 |
socket->state = INIT; |
| 450 |
socket->impl = impl; |
| 451 |
socket->options = *options; |
| 452 |
|
| 453 |
if (options->domain != AWS_SOCKET_LOCAL && create_underlying_socket) { |
| 454 |
if (s_create_socket(socket, options)) { |
| 455 |
aws_mem_release(alloc, impl->read_io_data); |
| 456 |
aws_mem_release(alloc, impl); |
| 457 |
socket->impl = NULL; |
| 458 |
return AWS_OP_ERR; |
| 459 |
} |
| 460 |
} |
| 461 |
return AWS_OP_SUCCESS; |
| 462 |
} |
| 463 |
|
| 464 |
int aws_socket_init_winsock( |
| 465 |
struct aws_socket *socket, |
| 466 |
struct aws_allocator *alloc, |
| 467 |
const struct aws_socket_options *options) { |
| 468 |
AWS_ASSERT(options); |
| 469 |
|
| 470 |
aws_check_and_init_winsock(); |
| 471 |
|
| 472 |
int err = s_socket_init(socket, alloc, options, true); |
| 473 |
|
| 474 |
return err; |
| 475 |
} |
| 476 |
|
| 477 |
static void s_socket_clean_up(struct aws_socket *socket) { |
| 478 |
if (!socket->impl) { |
| 479 |
|
| 480 |
return; |
| 481 |
} |
| 482 |
AWS_LOGF_DEBUG( |
| 483 |
AWS_LS_IO_SOCKET, |
| 484 |
"id=%p, handle=%p: cleaning up socket.", |
| 485 |
(void *)socket, |
| 486 |
(void *)socket->io_handle.data.handle); |
| 487 |
struct iocp_socket *socket_impl = socket->impl; |
| 488 |
socket_impl->winsock_vtable->close(socket); |
| 489 |
|
| 490 |
if (socket_impl->incoming_socket) { |
| 491 |
aws_socket_clean_up(socket_impl->incoming_socket); |
| 492 |
aws_mem_release(socket->allocator, socket_impl->incoming_socket); |
| 493 |
} |
| 494 |
|
| 495 |
if (socket_impl->read_io_data) { |
| 496 |
aws_mem_release(socket->allocator, socket_impl->read_io_data); |
| 497 |
} |
| 498 |
|
| 499 |
aws_socket_on_shutdown_complete_fn *on_cleanup_complete = socket_impl->on_cleanup_complete; |
| 500 |
void *cleanup_user_data = socket_impl->cleanup_user_data; |
| 501 |
|
| 502 |
aws_mem_release(socket->allocator, socket->impl); |
| 503 |
AWS_ZERO_STRUCT(*socket); |
| 504 |
socket->io_handle.data.handle = INVALID_HANDLE_VALUE; |
| 505 |
|
| 506 |
if (on_cleanup_complete) { |
| 507 |
on_cleanup_complete(cleanup_user_data); |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
static int s_socket_connect(struct aws_socket *socket, struct aws_socket_connect_options *socket_connect_options) { |
| 512 |
|
| 513 |
const struct aws_socket_endpoint *remote_endpoint = socket_connect_options->remote_endpoint; |
| 514 |
struct aws_event_loop *event_loop = socket_connect_options->event_loop; |
| 515 |
aws_socket_on_connection_result_fn *on_connection_result = socket_connect_options->on_connection_result; |
| 516 |
void *user_data = socket_connect_options->user_data; |
| 517 |
|
| 518 |
struct iocp_socket *socket_impl = socket->impl; |
| 519 |
if (socket->options.type != AWS_SOCKET_DGRAM) { |
| 520 |
AWS_ASSERT(on_connection_result); |
| 521 |
if (socket->state != INIT) { |
| 522 |
socket->state = ERRORED; |
| 523 |
return aws_raise_error(AWS_IO_SOCKET_ILLEGAL_OPERATION_FOR_STATE); |
| 524 |
} |
| 525 |
} else { |
| 526 |
|
| 527 |
if (socket->state != CONNECTED_READ && socket->state != INIT) { |
| 528 |
socket->state = ERRORED; |
| 529 |
return aws_raise_error(AWS_IO_SOCKET_ILLEGAL_OPERATION_FOR_STATE); |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
if (aws_socket_validate_port_for_connect(remote_endpoint->port, socket->options.domain)) { |
| 534 |
return AWS_OP_ERR; |
| 535 |
} |
| 536 |
|
| 537 |
return socket_impl->winsock_vtable->connect(socket, remote_endpoint, event_loop, on_connection_result, user_data); |
| 538 |
} |
| 539 |
|
| 540 |
static int s_socket_bind(struct aws_socket *socket, struct aws_socket_bind_options *socket_bind_options) { |
| 541 |
const struct aws_socket_endpoint *local_endpoint = socket_bind_options->local_endpoint; |
| 542 |
if (socket->state != INIT) { |
| 543 |
socket->state = ERRORED; |
| 544 |
return aws_raise_error(AWS_IO_SOCKET_ILLEGAL_OPERATION_FOR_STATE); |
| 545 |
} |
| 546 |
|
| 547 |
if (aws_socket_validate_port_for_bind(local_endpoint->port, socket->options.domain)) { |
| 548 |
return AWS_OP_ERR; |
| 549 |
} |
| 550 |
|
| 551 |
struct iocp_socket *socket_impl = socket->impl; |
| 552 |
return socket_impl->winsock_vtable->bind(socket, local_endpoint); |
| 553 |
} |
| 554 |
|
| 555 |
|
| 556 |
static int s_update_local_endpoint_ipv4_ipv6(struct aws_socket *socket) { |
| 557 |
struct aws_socket_endpoint tmp_endpoint; |
| 558 |
AWS_ZERO_STRUCT(tmp_endpoint); |
| 559 |
|
| 560 |
struct sockaddr_storage address; |
| 561 |
AWS_ZERO_STRUCT(address); |
| 562 |
socklen_t address_size = sizeof(address); |
| 563 |
if (getsockname((SOCKET)socket->io_handle.data.handle, (struct sockaddr *)&address, &address_size) != 0) { |
| 564 |
int wsa_err = WSAGetLastError(); |
| 565 |
AWS_LOGF_ERROR( |
| 566 |
AWS_LS_IO_SOCKET, |
| 567 |
"id=%p handle=%p: getsockname() failed with error %d", |
| 568 |
(void *)socket, |
| 569 |
(void *)socket->io_handle.data.handle, |
| 570 |
wsa_err); |
| 571 |
return aws_raise_error(s_determine_socket_error(wsa_err)); |
| 572 |
} |
| 573 |
|
| 574 |
if (address.ss_family == AF_INET) { |
| 575 |
struct sockaddr_in *s = (struct sockaddr_in *)&address; |
| 576 |
tmp_endpoint.port = ntohs(s->sin_port); |
| 577 |
if (InetNtopA(AF_INET, &s->sin_addr, tmp_endpoint.address, sizeof(tmp_endpoint.address)) == NULL) { |
| 578 |
int wsa_err = WSAGetLastError(); |
| 579 |
AWS_LOGF_ERROR( |
| 580 |
AWS_LS_IO_SOCKET, |
| 581 |
"id=%p handle=%p: determining local endpoint failed", |
| 582 |
(void *)socket, |
| 583 |
(void *)socket->io_handle.data.handle); |
| 584 |
return aws_raise_error(s_determine_socket_error(wsa_err)); |
| 585 |
} |
| 586 |
} else if (address.ss_family == AF_INET6) { |
| 587 |
struct sockaddr_in6 *s = (struct sockaddr_in6 *)&address; |
| 588 |
tmp_endpoint.port = ntohs(s->sin6_port); |
| 589 |
if (InetNtopA(AF_INET6, &s->sin6_addr, tmp_endpoint.address, sizeof(tmp_endpoint.address)) == NULL) { |
| 590 |
int wsa_err = WSAGetLastError(); |
| 591 |
AWS_LOGF_ERROR( |
| 592 |
AWS_LS_IO_SOCKET, |
| 593 |
"id=%p handle=%p: determining local endpoint failed", |
| 594 |
(void *)socket, |
| 595 |
(void *)socket->io_handle.data.handle); |
| 596 |
return aws_raise_error(s_determine_socket_error(wsa_err)); |
| 597 |
} |
| 598 |
} else { |
| 599 |
AWS_LOGF_ERROR( |
| 600 |
AWS_LS_IO_SOCKET, |
| 601 |
"id=%p handle=%p: unknown ADDRESS_FAMILY %d", |
| 602 |
(void *)socket, |
| 603 |
(void *)socket->io_handle.data.handle, |
| 604 |
address.ss_family); |
| 605 |
return aws_raise_error(AWS_IO_SOCKET_UNSUPPORTED_ADDRESS_FAMILY); |
| 606 |
} |
| 607 |
|
| 608 |
socket->local_endpoint = tmp_endpoint; |
| 609 |
return AWS_OP_SUCCESS; |
| 610 |
} |
| 611 |
|
| 612 |
static int s_socket_listen(struct aws_socket *socket, int backlog_size) { |
| 613 |
struct iocp_socket *socket_impl = socket->impl; |
| 614 |
return socket_impl->winsock_vtable->listen(socket, backlog_size); |
| 615 |
} |
| 616 |
|
| 617 |
static int s_socket_start_accept( |
| 618 |
struct aws_socket *socket, |
| 619 |
struct aws_event_loop *accept_loop, |
| 620 |
struct aws_socket_listener_options options) { |
| 621 |
struct iocp_socket *socket_impl = socket->impl; |
| 622 |
return socket_impl->winsock_vtable->start_accept(socket, accept_loop, options); |
| 623 |
} |
| 624 |
|
| 625 |
static int s_socket_stop_accept(struct aws_socket *socket) { |
| 626 |
struct iocp_socket *socket_impl = socket->impl; |
| 627 |
return socket_impl->winsock_vtable->stop_accept(socket); |
| 628 |
} |
| 629 |
|
| 630 |
static int s_socket_close(struct aws_socket *socket) { |
| 631 |
struct iocp_socket *socket_impl = socket->impl; |
| 632 |
int result = socket_impl->winsock_vtable->close(socket); |
| 633 |
if (socket_impl->on_close_complete) { |
| 634 |
socket_impl->on_close_complete(socket_impl->close_user_data); |
| 635 |
} |
| 636 |
return result; |
| 637 |
} |
| 638 |
|
| 639 |
static int s_socket_shutdown_dir(struct aws_socket *socket, enum aws_channel_direction dir) { |
| 640 |
int how = dir == AWS_CHANNEL_DIR_READ ? 0 : 1; |
| 641 |
|
| 642 |
if (shutdown((SOCKET)socket->io_handle.data.handle, how)) { |
| 643 |
int aws_error = s_determine_socket_error(WSAGetLastError()); |
| 644 |
return aws_raise_error(aws_error); |
| 645 |
} |
| 646 |
|
| 647 |
if (dir == AWS_CHANNEL_DIR_READ) { |
| 648 |
socket->state &= ~CONNECTED_READ; |
| 649 |
} else { |
| 650 |
socket->state &= ~CONNECTED_WRITE; |
| 651 |
} |
| 652 |
|
| 653 |
return AWS_OP_SUCCESS; |
| 654 |
} |
| 655 |
|
| 656 |
static int s_socket_read(struct aws_socket *socket, struct aws_byte_buf *buffer, size_t *amount_read) { |
| 657 |
struct iocp_socket *socket_impl = socket->impl; |
| 658 |
AWS_ASSERT(socket->readable_fn); |
| 659 |
|
| 660 |
if (!aws_event_loop_thread_is_callers_thread(socket->event_loop)) { |
| 661 |
AWS_LOGF_ERROR( |
| 662 |
AWS_LS_IO_SOCKET, |
| 663 |
"id=%p handle=%p: Read can only be called from the owning event-loop's thread.", |
| 664 |
(void *)socket, |
| 665 |
(void *)socket->io_handle.data.handle); |
| 666 |
return aws_raise_error(AWS_ERROR_IO_EVENT_LOOP_THREAD_ONLY); |
| 667 |
} |
| 668 |
|
| 669 |
if (!(socket->state & CONNECTED_READ)) { |
| 670 |
AWS_LOGF_ERROR( |
| 671 |
AWS_LS_IO_SOCKET, |
| 672 |
"id=%p handle=%p: Attempt to read from an unconnected socket.", |
| 673 |
(void *)socket, |
| 674 |
(void *)socket->io_handle.data.handle); |
| 675 |
return aws_raise_error(AWS_IO_SOCKET_NOT_CONNECTED); |
| 676 |
} |
| 677 |
|
| 678 |
return socket_impl->winsock_vtable->read(socket, buffer, amount_read); |
| 679 |
} |
| 680 |
|
| 681 |
static int s_socket_subscribe_to_readable_events( |
| 682 |
struct aws_socket *socket, |
| 683 |
aws_socket_on_readable_fn *on_readable, |
| 684 |
void *user_data) { |
| 685 |
struct iocp_socket *socket_impl = socket->impl; |
| 686 |
AWS_ASSERT(socket->event_loop); |
| 687 |
AWS_ASSERT(!socket->readable_fn); |
| 688 |
|
| 689 |
if (!(socket->state & CONNECTED_READ)) { |
| 690 |
AWS_LOGF_ERROR( |
| 691 |
AWS_LS_IO_SOCKET, |
| 692 |
"id=%p handle=%p: Attempt to subscribe to an unconnected socket.", |
| 693 |
(void *)socket, |
| 694 |
(void *)socket->io_handle.data.handle); |
| 695 |
return aws_raise_error(AWS_IO_SOCKET_NOT_CONNECTED); |
| 696 |
} |
| 697 |
|
| 698 |
return socket_impl->winsock_vtable->subscribe_to_read(socket, on_readable, user_data); |
| 699 |
} |
| 700 |
|
| 701 |
static int s_determine_socket_error(int error) { |
| 702 |
switch (error) { |
| 703 |
case WSAECONNREFUSED: |
| 704 |
case IO_STATUS_CONNECTION_REFUSED: |
| 705 |
return AWS_IO_SOCKET_CONNECTION_REFUSED; |
| 706 |
case WSAETIMEDOUT: |
| 707 |
case IO_STATUS_TIMEOUT: |
| 708 |
return AWS_IO_SOCKET_TIMEOUT; |
| 709 |
case IO_PIPE_BROKEN: |
| 710 |
case ERROR_BROKEN_PIPE: |
| 711 |
return AWS_IO_SOCKET_CLOSED; |
| 712 |
case STATUS_INVALID_ADDRESS_COMPONENT: |
| 713 |
case WSAEADDRNOTAVAIL: |
| 714 |
return AWS_IO_SOCKET_INVALID_ADDRESS; |
| 715 |
case WSAEADDRINUSE: |
| 716 |
return AWS_IO_SOCKET_ADDRESS_IN_USE; |
| 717 |
case WSAENETUNREACH: |
| 718 |
case IO_NETWORK_UNREACHABLE: |
| 719 |
case IO_HOST_UNREACHABLE: |
| 720 |
return AWS_IO_SOCKET_NO_ROUTE_TO_HOST; |
| 721 |
case WSAENETDOWN: |
| 722 |
return AWS_IO_SOCKET_NETWORK_DOWN; |
| 723 |
case WSAECONNABORTED: |
| 724 |
case IO_CONNECTION_ABORTED: |
| 725 |
return AWS_IO_SOCKET_CONNECT_ABORTED; |
| 726 |
case WSAENOBUFS: |
| 727 |
return AWS_ERROR_OOM; |
| 728 |
case WSAEMFILE: |
| 729 |
return AWS_ERROR_MAX_FDS_EXCEEDED; |
| 730 |
case WSAENAMETOOLONG: |
| 731 |
case WSA_INVALID_PARAMETER: |
| 732 |
case SOME_ERROR_CODE_THAT_MEANS_INVALID_PATH: |
| 733 |
return AWS_ERROR_FILE_INVALID_PATH; |
| 734 |
case WSAEAFNOSUPPORT: |
| 735 |
return AWS_IO_SOCKET_UNSUPPORTED_ADDRESS_FAMILY; |
| 736 |
case WSAEACCES: |
| 737 |
return AWS_ERROR_NO_PERMISSION; |
| 738 |
default: |
| 739 |
return AWS_IO_SOCKET_NOT_CONNECTED; |
| 740 |
} |
| 741 |
} |
| 742 |
|
| 743 |
static inline int s_process_tcp_sock_options(struct aws_socket *socket) { |
| 744 |
if (aws_socket_set_options(socket, &socket->options)) { |
| 745 |
aws_raise_error(AWS_IO_SOCKET_INVALID_OPTIONS); |
| 746 |
return AWS_OP_ERR; |
| 747 |
} |
| 748 |
return AWS_OP_SUCCESS; |
| 749 |
} |
| 750 |
|
| 751 |
|
| 752 |
static int s_ipv4_stream_connection_success(struct aws_socket *socket) { |
| 753 |
struct iocp_socket *socket_impl = socket->impl; |
| 754 |
|
| 755 |
if (s_process_tcp_sock_options(socket)) { |
| 756 |
goto error; |
| 757 |
} |
| 758 |
|
| 759 |
int connect_result = 0; |
| 760 |
socklen_t result_length = sizeof(connect_result); |
| 761 |
if (getsockopt( |
| 762 |
(SOCKET)socket->io_handle.data.handle, SOL_SOCKET, SO_ERROR, (char *)&connect_result, &result_length) < 0) { |
| 763 |
int wsa_err = WSAGetLastError(); |
| 764 |
AWS_LOGF_DEBUG( |
| 765 |
AWS_LS_IO_SOCKET, |
| 766 |
"id=%p handle=%p: failed to determine connection error %d", |
| 767 |
(void *)socket, |
| 768 |
(void *)socket->io_handle.data.handle, |
| 769 |
wsa_err); |
| 770 |
aws_raise_error(s_determine_socket_error(wsa_err)); |
| 771 |
goto error; |
| 772 |
} |
| 773 |
|
| 774 |
if (connect_result) { |
| 775 |
AWS_LOGF_DEBUG( |
| 776 |
AWS_LS_IO_SOCKET, |
| 777 |
"id=%p handle=%p: connection error %d", |
| 778 |
(void *)socket, |
| 779 |
(void *)socket->io_handle.data.handle, |
| 780 |
connect_result); |
| 781 |
aws_raise_error(s_determine_socket_error(connect_result)); |
| 782 |
goto error; |
| 783 |
} |
| 784 |
|
| 785 |
AWS_LOGF_DEBUG( |
| 786 |
AWS_LS_IO_SOCKET, "id=%p handle=%p: connection success", (void *)socket, (void *)socket->io_handle.data.handle); |
| 787 |
|
| 788 |
if (s_update_local_endpoint_ipv4_ipv6(socket)) { |
| 789 |
goto error; |
| 790 |
} |
| 791 |
|
| 792 |
AWS_LOGF_DEBUG( |
| 793 |
AWS_LS_IO_SOCKET, |
| 794 |
"id=%p handle=%p: local endpoint %s:%u", |
| 795 |
(void *)socket, |
| 796 |
(void *)socket->io_handle.data.handle, |
| 797 |
socket->local_endpoint.address, |
| 798 |
socket->local_endpoint.port); |
| 799 |
|
| 800 |
setsockopt((SOCKET)socket->io_handle.data.handle, SOL_SOCKET, SO_UPDATE_CONNECT_CONTEXT, NULL, 0); |
| 801 |
socket->state = CONNECTED_WRITE | CONNECTED_READ; |
| 802 |
|
| 803 |
socket->connection_result_fn(socket, AWS_ERROR_SUCCESS, socket->connect_accept_user_data); |
| 804 |
|
| 805 |
return AWS_OP_SUCCESS; |
| 806 |
error: |
| 807 |
socket->state = ERRORED; |
| 808 |
socket_impl->winsock_vtable->connection_error(socket, aws_last_error()); |
| 809 |
return AWS_OP_ERR; |
| 810 |
} |
| 811 |
|
| 812 |
|
| 813 |
static int s_ipv6_stream_connection_success(struct aws_socket *socket) { |
| 814 |
struct iocp_socket *socket_impl = socket->impl; |
| 815 |
|
| 816 |
if (s_process_tcp_sock_options(socket)) { |
| 817 |
goto error; |
| 818 |
} |
| 819 |
|
| 820 |
int connect_result = 0; |
| 821 |
socklen_t result_length = sizeof(connect_result); |
| 822 |
if (getsockopt( |
| 823 |
(SOCKET)socket->io_handle.data.handle, SOL_SOCKET, SO_ERROR, (char *)&connect_result, &result_length) < 0) { |
| 824 |
int wsa_err = WSAGetLastError(); |
| 825 |
AWS_LOGF_DEBUG( |
| 826 |
AWS_LS_IO_SOCKET, |
| 827 |
"id=%p handle=%p: failed to determine connection error %d", |
| 828 |
(void *)socket, |
| 829 |
(void *)socket->io_handle.data.handle, |
| 830 |
wsa_err); |
| 831 |
aws_raise_error(s_determine_socket_error(wsa_err)); |
| 832 |
goto error; |
| 833 |
} |
| 834 |
|
| 835 |
if (connect_result) { |
| 836 |
AWS_LOGF_DEBUG( |
| 837 |
AWS_LS_IO_SOCKET, |
| 838 |
"id=%p handle=%p: connection error %d", |
| 839 |
(void *)socket, |
| 840 |
(void *)socket->io_handle.data.handle, |
| 841 |
connect_result); |
| 842 |
aws_raise_error(s_determine_socket_error(connect_result)); |
| 843 |
goto error; |
| 844 |
} |
| 845 |
|
| 846 |
AWS_LOGF_DEBUG( |
| 847 |
AWS_LS_IO_SOCKET, "id=%p handle=%p: connection success", (void *)socket, (void *)socket->io_handle.data.handle); |
| 848 |
|
| 849 |
if (s_update_local_endpoint_ipv4_ipv6(socket)) { |
| 850 |
goto error; |
| 851 |
} |
| 852 |
|
| 853 |
AWS_LOGF_DEBUG( |
| 854 |
AWS_LS_IO_SOCKET, |
| 855 |
"id=%p handle=%p: local endpoint %s:%u", |
| 856 |
(void *)socket, |
| 857 |
(void *)socket->io_handle.data.handle, |
| 858 |
socket->local_endpoint.address, |
| 859 |
socket->local_endpoint.port); |
| 860 |
|
| 861 |
setsockopt((SOCKET)socket->io_handle.data.handle, SOL_SOCKET, SO_UPDATE_CONNECT_CONTEXT, NULL, 0); |
| 862 |
|
| 863 |
socket->state = CONNECTED_WRITE | CONNECTED_READ; |
| 864 |
|
| 865 |
socket->connection_result_fn(socket, AWS_ERROR_SUCCESS, socket->connect_accept_user_data); |
| 866 |
|
| 867 |
return AWS_OP_SUCCESS; |
| 868 |
|
| 869 |
error: |
| 870 |
socket->state = ERRORED; |
| 871 |
socket_impl->winsock_vtable->connection_error(socket, aws_last_error()); |
| 872 |
return AWS_OP_ERR; |
| 873 |
} |
| 874 |
|
| 875 |
|
| 876 |
static int s_local_and_udp_connection_success(struct aws_socket *socket) { |
| 877 |
socket->state = CONNECTED_WRITE | CONNECTED_READ; |
| 878 |
|
| 879 |
AWS_LOGF_DEBUG( |
| 880 |
AWS_LS_IO_SOCKET, "id=%p handle=%p: connection success", (void *)socket, (void *)socket->io_handle.data.handle); |
| 881 |
if (socket->connection_result_fn) { |
| 882 |
socket->connection_result_fn(socket, AWS_ERROR_SUCCESS, socket->connect_accept_user_data); |
| 883 |
} |
| 884 |
|
| 885 |
return AWS_OP_SUCCESS; |
| 886 |
} |
| 887 |
|
| 888 |
static void s_connection_error(struct aws_socket *socket, int error) { |
| 889 |
socket->state = ERRORED; |
| 890 |
|
| 891 |
AWS_LOGF_DEBUG( |
| 892 |
AWS_LS_IO_SOCKET, |
| 893 |
"id=%p handle=%p: connection error with code %d", |
| 894 |
(void *)socket, |
| 895 |
(void *)socket->io_handle.data.handle, |
| 896 |
error); |
| 897 |
|
| 898 |
if (socket->connection_result_fn) { |
| 899 |
socket->connection_result_fn(socket, error, socket->connect_accept_user_data); |
| 900 |
} else if (socket->accept_result_fn) { |
| 901 |
socket->accept_result_fn(socket, error, NULL, socket->connect_accept_user_data); |
| 902 |
} |
| 903 |
} |
| 904 |
|
| 905 |
|
| 906 |
void s_socket_connection_completion( |
| 907 |
struct aws_event_loop *event_loop, |
| 908 |
struct aws_overlapped *overlapped, |
| 909 |
int status_code, |
| 910 |
size_t num_bytes_transferred) { |
| 911 |
(void)num_bytes_transferred; |
| 912 |
(void)event_loop; |
| 913 |
|
| 914 |
struct io_operation_data *operation_data = AWS_CONTAINER_OF(overlapped, struct io_operation_data, signal); |
| 915 |
struct socket_connect_args *socket_args = overlapped->user_data; |
| 916 |
|
| 917 |
AWS_LOGF_TRACE(AWS_LS_IO_SOCKET, "static: connect completion triggered on event-loop %p", (void *)event_loop); |
| 918 |
|
| 919 |
if (!operation_data->socket) { |
| 920 |
aws_mem_release(operation_data->allocator, operation_data); |
| 921 |
return; |
| 922 |
} |
| 923 |
|
| 924 |
if (status_code == IO_OPERATION_CANCELLED) { |
| 925 |
operation_data->in_use = false; |
| 926 |
return; |
| 927 |
} |
| 928 |
|
| 929 |
if (socket_args->socket) { |
| 930 |
AWS_LOGF_TRACE( |
| 931 |
AWS_LS_IO_SOCKET, |
| 932 |
"id=%p handle=%p: connect completion triggered. Socket has not timed out yet: proceeding with connection", |
| 933 |
(void *)socket_args->socket, |
| 934 |
(void *)socket_args->socket->io_handle.data.handle); |
| 935 |
struct iocp_socket *socket_impl = socket_args->socket->impl; |
| 936 |
|
| 937 |
struct aws_socket *socket = socket_args->socket; |
| 938 |
socket->readable_fn = NULL; |
| 939 |
socket->readable_user_data = NULL; |
| 940 |
socket_impl->connect_args = NULL; |
| 941 |
socket_args->socket = NULL; |
| 942 |
|
| 943 |
if (!status_code) { |
| 944 |
socket_impl->winsock_vtable->connection_success(socket); |
| 945 |
} else { |
| 946 |
AWS_LOGF_ERROR( |
| 947 |
AWS_LS_IO_SOCKET, |
| 948 |
"id=%p handle=%p: connect completion triggered with error %d", |
| 949 |
(void *)socket, |
| 950 |
(void *)socket->io_handle.data.handle, |
| 951 |
status_code); |
| 952 |
int error = s_determine_socket_error(status_code); |
| 953 |
socket_impl->winsock_vtable->connection_error(socket, error); |
| 954 |
} |
| 955 |
} |
| 956 |
|
| 957 |
if (operation_data->socket) { |
| 958 |
operation_data->in_use = false; |
| 959 |
} else { |
| 960 |
aws_mem_release(operation_data->allocator, operation_data); |
| 961 |
} |
| 962 |
} |
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
static void s_handle_socket_timeout(struct aws_task *task, void *args, aws_task_status status) { |
| 967 |
(void)task; |
| 968 |
(void)status; |
| 969 |
struct socket_connect_args *socket_args = args; |
| 970 |
|
| 971 |
AWS_LOGF_TRACE(AWS_LS_IO_SOCKET, "task_id=%p: timeout task triggered, evaluating timeouts.", (void *)task); |
| 972 |
if (socket_args->socket) { |
| 973 |
AWS_LOGF_ERROR( |
| 974 |
AWS_LS_IO_SOCKET, |
| 975 |
"id=%p handle=%p: timed out, shutting down.", |
| 976 |
(void *)socket_args->socket, |
| 977 |
(void *)socket_args->socket->io_handle.data.handle); |
| 978 |
socket_args->socket->state = TIMEDOUT; |
| 979 |
struct aws_socket *socket = socket_args->socket; |
| 980 |
int error_code = AWS_IO_SOCKET_TIMEOUT; |
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
if (status == AWS_TASK_STATUS_CANCELED) { |
| 985 |
struct iocp_socket *iocp_socket = socket->impl; |
| 986 |
iocp_socket->read_io_data->in_use = false; |
| 987 |
error_code = AWS_IO_EVENT_LOOP_SHUTDOWN; |
| 988 |
} |
| 989 |
|
| 990 |
aws_raise_error(error_code); |
| 991 |
|
| 992 |
|
| 993 |
aws_socket_close(socket); |
| 994 |
socket->connection_result_fn(socket, error_code, socket->connect_accept_user_data); |
| 995 |
} |
| 996 |
|
| 997 |
struct aws_allocator *allocator = socket_args->allocator; |
| 998 |
aws_mem_release(allocator, socket_args); |
| 999 |
} |
| 1000 |
|
| 1001 |
|
| 1002 |
static inline int s_tcp_connect( |
| 1003 |
struct aws_socket *socket, |
| 1004 |
const struct aws_socket_endpoint *remote_endpoint, |
| 1005 |
struct aws_event_loop *connect_loop, |
| 1006 |
struct sockaddr *bind_addr, |
| 1007 |
struct sockaddr *socket_addr, |
| 1008 |
size_t sock_size) { |
| 1009 |
struct iocp_socket *socket_impl = socket->impl; |
| 1010 |
socket->remote_endpoint = *remote_endpoint; |
| 1011 |
|
| 1012 |
int reuse = 1; |
| 1013 |
if (setsockopt((SOCKET)socket->io_handle.data.handle, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(int))) { |
| 1014 |
int wsa_err = WSAGetLastError(); |
| 1015 |
AWS_LOGF_WARN( |
| 1016 |
AWS_LS_IO_SOCKET, |
| 1017 |
"id=%p handle=%p: setsockopt() call for enabling SO_REUSEADDR failed with WSAError %d", |
| 1018 |
(void *)socket, |
| 1019 |
(void *)socket->io_handle.data.handle, |
| 1020 |
wsa_err); |
| 1021 |
return aws_raise_error(s_determine_socket_error(wsa_err)); |
| 1022 |
} |
| 1023 |
|
| 1024 |
struct socket_connect_args *connect_args = aws_mem_calloc(socket->allocator, 1, sizeof(struct socket_connect_args)); |
| 1025 |
if (!connect_args) { |
| 1026 |
socket->state = ERRORED; |
| 1027 |
return AWS_OP_ERR; |
| 1028 |
} |
| 1029 |
|
| 1030 |
connect_args->timeout_task.fn = s_handle_socket_timeout; |
| 1031 |
connect_args->timeout_task.arg = connect_args; |
| 1032 |
|
| 1033 |
LPFN_CONNECTEX connect_fn = NULL; |
| 1034 |
if (aws_socket_assign_to_event_loop(socket, connect_loop)) { |
| 1035 |
socket->state = ERRORED; |
| 1036 |
aws_mem_release(socket->allocator, connect_args); |
| 1037 |
return AWS_OP_ERR; |
| 1038 |
} |
| 1039 |
|
| 1040 |
connect_args->allocator = socket->allocator; |
| 1041 |
connect_args->socket = socket; |
| 1042 |
socket->state = CONNECTING; |
| 1043 |
connect_fn = (LPFN_CONNECTEX)aws_winsock_get_connectex_fn(); |
| 1044 |
socket_impl->read_io_data->in_use = true; |
| 1045 |
aws_overlapped_init(&socket_impl->read_io_data->signal, s_socket_connection_completion, connect_args); |
| 1046 |
int fake_buffer = 0; |
| 1047 |
socket_impl->connect_args = connect_args; |
| 1048 |
BOOL connect_res = false; |
| 1049 |
bind((SOCKET)socket->io_handle.data.handle, bind_addr, (int)sock_size); |
| 1050 |
|
| 1051 |
|
| 1052 |
struct aws_allocator *allocator = socket->allocator; |
| 1053 |
uint32_t connect_timeout_ms = socket->options.connect_timeout_ms; |
| 1054 |
connect_res = connect_fn( |
| 1055 |
(SOCKET)socket->io_handle.data.handle, |
| 1056 |
socket_addr, |
| 1057 |
(int)sock_size, |
| 1058 |
&fake_buffer, |
| 1059 |
0, |
| 1060 |
NULL, |
| 1061 |
aws_overlapped_to_windows_overlapped(&socket_impl->read_io_data->signal)); |
| 1062 |
|
| 1063 |
uint64_t time_to_run = 0; |
| 1064 |
|
| 1065 |
|
| 1066 |
aws_event_loop_current_clock_time(connect_loop, &time_to_run); |
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
if (!connect_res) { |
| 1071 |
int error_code = WSAGetLastError(); |
| 1072 |
if (error_code != ERROR_IO_PENDING) { |
| 1073 |
AWS_LOGF_DEBUG( |
| 1074 |
AWS_LS_IO_TLS, |
| 1075 |
"id=%p handle=%p: connection error %d", |
| 1076 |
(void *)socket, |
| 1077 |
(void *)socket->io_handle.data.handle, |
| 1078 |
error_code); |
| 1079 |
socket_impl->connect_args = NULL; |
| 1080 |
socket_impl->read_io_data->in_use = false; |
| 1081 |
aws_mem_release(allocator, connect_args); |
| 1082 |
int aws_err = s_determine_socket_error(error_code); |
| 1083 |
return aws_raise_error(aws_err); |
| 1084 |
} |
| 1085 |
|
| 1086 |
time_to_run += aws_timestamp_convert(connect_timeout_ms, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL); |
| 1087 |
} else { |
| 1088 |
|
| 1089 |
time_to_run += aws_timestamp_convert(500, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL); |
| 1090 |
} |
| 1091 |
|
| 1092 |
AWS_LOGF_TRACE( |
| 1093 |
AWS_LS_IO_SOCKET, |
| 1094 |
"id=%p handle=%p: connection pending, scheduling timeout task", |
| 1095 |
(void *)socket, |
| 1096 |
(void *)socket->io_handle.data.handle); |
| 1097 |
aws_event_loop_schedule_task_future(connect_loop, &connect_args->timeout_task, time_to_run); |
| 1098 |
|
| 1099 |
return AWS_OP_SUCCESS; |
| 1100 |
} |
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
static inline int s_convert_pton_error(int pton_err) { |
| 1105 |
if (pton_err == 0) { |
| 1106 |
return AWS_IO_SOCKET_INVALID_ADDRESS; |
| 1107 |
} |
| 1108 |
|
| 1109 |
return s_determine_socket_error(WSAGetLastError()); |
| 1110 |
} |
| 1111 |
|
| 1112 |
|
| 1113 |
static int s_ipv4_stream_connect( |
| 1114 |
struct aws_socket *socket, |
| 1115 |
const struct aws_socket_endpoint *remote_endpoint, |
| 1116 |
struct aws_event_loop *connect_loop, |
| 1117 |
aws_socket_on_connection_result_fn *on_connection_result, |
| 1118 |
void *user_data) { |
| 1119 |
AWS_ASSERT(connect_loop); |
| 1120 |
AWS_ASSERT(on_connection_result); |
| 1121 |
|
| 1122 |
AWS_LOGF_DEBUG( |
| 1123 |
AWS_LS_IO_SOCKET, "id=%p handle=%p: beginning connect.", (void *)socket, (void *)socket->io_handle.data.handle); |
| 1124 |
|
| 1125 |
socket->connection_result_fn = on_connection_result; |
| 1126 |
socket->connect_accept_user_data = user_data; |
| 1127 |
struct sockaddr_in addr_in; |
| 1128 |
AWS_ZERO_STRUCT(addr_in); |
| 1129 |
int err = inet_pton(AF_INET, remote_endpoint->address, &(addr_in.sin_addr)); |
| 1130 |
|
| 1131 |
if (err != 1) { |
| 1132 |
int aws_err = s_convert_pton_error(err); |
| 1133 |
AWS_LOGF_ERROR( |
| 1134 |
AWS_LS_IO_SOCKET, |
| 1135 |
"id=%p handle=%p: failed to parse address %s:%u.", |
| 1136 |
(void *)socket, |
| 1137 |
(void *)socket->io_handle.data.handle, |
| 1138 |
remote_endpoint->address, |
| 1139 |
remote_endpoint->port); |
| 1140 |
return aws_raise_error(aws_err); |
| 1141 |
} |
| 1142 |
|
| 1143 |
AWS_LOGF_DEBUG( |
| 1144 |
AWS_LS_IO_SOCKET, |
| 1145 |
"id=%p handle=%p: connecting to endpoint %s:%u.", |
| 1146 |
(void *)socket, |
| 1147 |
(void *)socket->io_handle.data.handle, |
| 1148 |
remote_endpoint->address, |
| 1149 |
remote_endpoint->port); |
| 1150 |
|
| 1151 |
addr_in.sin_port = htons((uint16_t)remote_endpoint->port); |
| 1152 |
addr_in.sin_family = AF_INET; |
| 1153 |
|
| 1154 |
|
| 1155 |
struct sockaddr_in in_bind_addr; |
| 1156 |
AWS_ZERO_STRUCT(in_bind_addr); |
| 1157 |
in_bind_addr.sin_family = AF_INET; |
| 1158 |
in_bind_addr.sin_addr.s_addr = INADDR_ANY; |
| 1159 |
in_bind_addr.sin_port = 0; |
| 1160 |
|
| 1161 |
return s_tcp_connect( |
| 1162 |
socket, |
| 1163 |
remote_endpoint, |
| 1164 |
connect_loop, |
| 1165 |
(struct sockaddr *)&in_bind_addr, |
| 1166 |
(struct sockaddr *)&addr_in, |
| 1167 |
sizeof(addr_in)); |
| 1168 |
} |
| 1169 |
|
| 1170 |
|
| 1171 |
static int s_ipv6_stream_connect( |
| 1172 |
struct aws_socket *socket, |
| 1173 |
const struct aws_socket_endpoint *remote_endpoint, |
| 1174 |
struct aws_event_loop *connect_loop, |
| 1175 |
aws_socket_on_connection_result_fn *on_connection_result, |
| 1176 |
void *user_data) { |
| 1177 |
AWS_ASSERT(connect_loop); |
| 1178 |
AWS_ASSERT(on_connection_result); |
| 1179 |
|
| 1180 |
AWS_LOGF_DEBUG( |
| 1181 |
AWS_LS_IO_SOCKET, "id=%p handle=%p: beginning connect.", (void *)socket, (void *)socket->io_handle.data.handle); |
| 1182 |
|
| 1183 |
if (socket->state != INIT) { |
| 1184 |
socket->state = ERRORED; |
| 1185 |
return aws_raise_error(AWS_IO_SOCKET_ILLEGAL_OPERATION_FOR_STATE); |
| 1186 |
} |
| 1187 |
|
| 1188 |
socket->connection_result_fn = on_connection_result; |
| 1189 |
socket->connect_accept_user_data = user_data; |
| 1190 |
struct sockaddr_in6 bind_addr; |
| 1191 |
AWS_ZERO_STRUCT(bind_addr); |
| 1192 |
bind_addr.sin6_family = AF_INET6; |
| 1193 |
bind_addr.sin6_port = 0; |
| 1194 |
|
| 1195 |
struct sockaddr_in6 addr_in6; |
| 1196 |
AWS_ZERO_STRUCT(addr_in6); |
| 1197 |
int pton_err = inet_pton(AF_INET6, remote_endpoint->address, &(addr_in6.sin6_addr)); |
| 1198 |
if (pton_err != 1) { |
| 1199 |
int aws_err = s_convert_pton_error(pton_err); |
| 1200 |
AWS_LOGF_ERROR( |
| 1201 |
AWS_LS_IO_SOCKET, |
| 1202 |
"id=%p handle=%p: failed to parse address %s:%u.", |
| 1203 |
(void *)socket, |
| 1204 |
(void *)socket->io_handle.data.handle, |
| 1205 |
remote_endpoint->address, |
| 1206 |
remote_endpoint->port); |
| 1207 |
return aws_raise_error(aws_err); |
| 1208 |
} |
| 1209 |
|
| 1210 |
AWS_LOGF_DEBUG( |
| 1211 |
AWS_LS_IO_SOCKET, |
| 1212 |
"id=%p handle=%p: connecting to endpoint %s:%u.", |
| 1213 |
(void *)socket, |
| 1214 |
(void *)socket->io_handle.data.handle, |
| 1215 |
remote_endpoint->address, |
| 1216 |
remote_endpoint->port); |
| 1217 |
|
| 1218 |
addr_in6.sin6_port = htons((uint16_t)remote_endpoint->port); |
| 1219 |
addr_in6.sin6_family = AF_INET6; |
| 1220 |
|
| 1221 |
return s_tcp_connect( |
| 1222 |
socket, |
| 1223 |
remote_endpoint, |
| 1224 |
connect_loop, |
| 1225 |
(struct sockaddr *)&bind_addr, |
| 1226 |
(struct sockaddr *)&addr_in6, |
| 1227 |
sizeof(addr_in6)); |
| 1228 |
} |
| 1229 |
|
| 1230 |
|
| 1231 |
static void s_connection_success_task(struct aws_task *task, void *arg, enum aws_task_status task_status) { |
| 1232 |
(void)task; |
| 1233 |
(void)task_status; |
| 1234 |
|
| 1235 |
struct io_operation_data *io_data = arg; |
| 1236 |
|
| 1237 |
if (!io_data->socket) { |
| 1238 |
aws_mem_release(io_data->allocator, io_data); |
| 1239 |
return; |
| 1240 |
} |
| 1241 |
|
| 1242 |
io_data->sequential_task_storage.fn = NULL; |
| 1243 |
io_data->sequential_task_storage.arg = NULL; |
| 1244 |
io_data->in_use = false; |
| 1245 |
|
| 1246 |
struct aws_socket *socket = io_data->socket; |
| 1247 |
struct iocp_socket *socket_impl = socket->impl; |
| 1248 |
socket_impl->winsock_vtable->connection_success(socket); |
| 1249 |
} |
| 1250 |
|
| 1251 |
|
| 1252 |
static int s_local_connect( |
| 1253 |
struct aws_socket *socket, |
| 1254 |
const struct aws_socket_endpoint *remote_endpoint, |
| 1255 |
struct aws_event_loop *connect_loop, |
| 1256 |
aws_socket_on_connection_result_fn *on_connection_result, |
| 1257 |
void *user_data) { |
| 1258 |
AWS_ASSERT(connect_loop); |
| 1259 |
AWS_ASSERT(on_connection_result); |
| 1260 |
|
| 1261 |
AWS_LOGF_DEBUG( |
| 1262 |
AWS_LS_IO_SOCKET, "id=%p handle=%p: beginning connect.", (void *)socket, (void *)socket->io_handle.data.handle); |
| 1263 |
|
| 1264 |
socket->connection_result_fn = on_connection_result; |
| 1265 |
socket->connect_accept_user_data = user_data; |
| 1266 |
|
| 1267 |
if (s_process_tcp_sock_options(socket)) { |
| 1268 |
socket->state = ERRORED; |
| 1269 |
return AWS_OP_ERR; |
| 1270 |
} |
| 1271 |
|
| 1272 |
struct iocp_socket *socket_impl = socket->impl; |
| 1273 |
socket->remote_endpoint = *remote_endpoint; |
| 1274 |
|
| 1275 |
socket->io_handle.data.handle = CreateFileA( |
| 1276 |
remote_endpoint->address, |
| 1277 |
GENERIC_READ | GENERIC_WRITE, |
| 1278 |
0, |
| 1279 |
NULL, |
| 1280 |
OPEN_EXISTING, |
| 1281 |
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, |
| 1282 |
NULL); |
| 1283 |
|
| 1284 |
if (socket->io_handle.data.handle != INVALID_HANDLE_VALUE) { |
| 1285 |
AWS_LOGF_DEBUG( |
| 1286 |
AWS_LS_IO_SOCKET, |
| 1287 |
"id=%p handle=%p: Successfully connected to named pipe %s.", |
| 1288 |
(void *)socket, |
| 1289 |
(void *)socket->io_handle.data.handle, |
| 1290 |
remote_endpoint->address); |
| 1291 |
|
| 1292 |
if (aws_socket_assign_to_event_loop(socket, connect_loop)) { |
| 1293 |
goto error; |
| 1294 |
} |
| 1295 |
|
| 1296 |
socket_impl->read_io_data->sequential_task_storage.fn = s_connection_success_task; |
| 1297 |
socket_impl->read_io_data->sequential_task_storage.arg = socket_impl->read_io_data; |
| 1298 |
socket_impl->read_io_data->in_use = true; |
| 1299 |
aws_event_loop_schedule_task_now(connect_loop, &socket_impl->read_io_data->sequential_task_storage); |
| 1300 |
return AWS_OP_SUCCESS; |
| 1301 |
} |
| 1302 |
|
| 1303 |
error:; |
| 1304 |
int win_error = GetLastError(); |
| 1305 |
AWS_LOGF_DEBUG( |
| 1306 |
AWS_LS_IO_SOCKET, |
| 1307 |
"id=%p handle=%p: failed to connect to named pipe %s.", |
| 1308 |
(void *)socket, |
| 1309 |
(void *)socket->io_handle.data.handle, |
| 1310 |
remote_endpoint->address); |
| 1311 |
socket->state = ERRORED; |
| 1312 |
int aws_error = s_determine_socket_error(win_error); |
| 1313 |
aws_raise_error(aws_error); |
| 1314 |
return AWS_OP_ERR; |
| 1315 |
} |
| 1316 |
|
| 1317 |
|
| 1318 |
static inline int s_dgram_connect( |
| 1319 |
struct aws_socket *socket, |
| 1320 |
const struct aws_socket_endpoint *remote_endpoint, |
| 1321 |
struct aws_event_loop *connect_loop, |
| 1322 |
struct sockaddr *socket_addr, |
| 1323 |
size_t sock_size) { |
| 1324 |
struct iocp_socket *socket_impl = socket->impl; |
| 1325 |
socket->remote_endpoint = *remote_endpoint; |
| 1326 |
|
| 1327 |
AWS_LOGF_DEBUG( |
| 1328 |
AWS_LS_IO_SOCKET, |
| 1329 |
"id=%p handle=%p: connecting to to %s:%u", |
| 1330 |
(void *)socket, |
| 1331 |
(void *)socket->io_handle.data.handle, |
| 1332 |
remote_endpoint->address, |
| 1333 |
remote_endpoint->port); |
| 1334 |
|
| 1335 |
int reuse = 1; |
| 1336 |
if (setsockopt((SOCKET)socket->io_handle.data.handle, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(int))) { |
| 1337 |
int wsa_err = WSAGetLastError(); |
| 1338 |
AWS_LOGF_WARN( |
| 1339 |
AWS_LS_IO_SOCKET, |
| 1340 |
"id=%p handle=%p: setsockopt() call for enabling SO_REUSEADDR failed with WSAError %d", |
| 1341 |
(void *)socket, |
| 1342 |
(void *)socket->io_handle.data.handle, |
| 1343 |
wsa_err); |
| 1344 |
aws_raise_error(s_determine_socket_error(wsa_err)); |
| 1345 |
goto error; |
| 1346 |
} |
| 1347 |
|
| 1348 |
int connect_err = connect((SOCKET)socket->io_handle.data.handle, socket_addr, (int)sock_size); |
| 1349 |
|
| 1350 |
if (connect_err) { |
| 1351 |
int wsa_err = WSAGetLastError(); |
| 1352 |
AWS_LOGF_DEBUG( |
| 1353 |
AWS_LS_IO_SOCKET, |
| 1354 |
"id=%p handle=%p: Failed to connect to %s:%u with error %d.", |
| 1355 |
(void *)socket, |
| 1356 |
(void *)socket->io_handle.data.handle, |
| 1357 |
remote_endpoint->address, |
| 1358 |
remote_endpoint->port, |
| 1359 |
wsa_err); |
| 1360 |
aws_raise_error(s_determine_socket_error(wsa_err)); |
| 1361 |
goto error; |
| 1362 |
} |
| 1363 |
|
| 1364 |
if (s_update_local_endpoint_ipv4_ipv6(socket)) { |
| 1365 |
goto error; |
| 1366 |
} |
| 1367 |
|
| 1368 |
AWS_LOGF_DEBUG( |
| 1369 |
AWS_LS_IO_SOCKET, |
| 1370 |
"id=%p handle=%p: local endpoint %s:%u", |
| 1371 |
(void *)socket, |
| 1372 |
(void *)socket->io_handle.data.handle, |
| 1373 |
socket->local_endpoint.address, |
| 1374 |
socket->local_endpoint.port); |
| 1375 |
|
| 1376 |
if (s_process_tcp_sock_options(socket)) { |
| 1377 |
goto error; |
| 1378 |
} |
| 1379 |
socket->state = CONNECTED_READ | CONNECTED_WRITE; |
| 1380 |
|
| 1381 |
if (connect_loop) { |
| 1382 |
if (aws_socket_assign_to_event_loop(socket, connect_loop)) { |
| 1383 |
goto error; |
| 1384 |
} |
| 1385 |
|
| 1386 |
socket_impl->read_io_data->sequential_task_storage.fn = s_connection_success_task; |
| 1387 |
socket_impl->read_io_data->sequential_task_storage.arg = socket_impl->read_io_data; |
| 1388 |
socket_impl->read_io_data->in_use = true; |
| 1389 |
aws_event_loop_schedule_task_now(connect_loop, &socket_impl->read_io_data->sequential_task_storage); |
| 1390 |
} |
| 1391 |
|
| 1392 |
return AWS_OP_SUCCESS; |
| 1393 |
|
| 1394 |
error: |
| 1395 |
socket->state = ERRORED; |
| 1396 |
return AWS_OP_ERR; |
| 1397 |
} |
| 1398 |
|
| 1399 |
static int s_ipv4_dgram_connect( |
| 1400 |
struct aws_socket *socket, |
| 1401 |
const struct aws_socket_endpoint *remote_endpoint, |
| 1402 |
struct aws_event_loop *connect_loop, |
| 1403 |
aws_socket_on_connection_result_fn *on_connection_result, |
| 1404 |
void *user_data) { |
| 1405 |
(void)user_data; |
| 1406 |
|
| 1407 |
|
| 1408 |
socket->connection_result_fn = on_connection_result; |
| 1409 |
socket->connect_accept_user_data = user_data; |
| 1410 |
struct sockaddr_in addr_in; |
| 1411 |
AWS_ZERO_STRUCT(addr_in); |
| 1412 |
int pton_err = inet_pton(AF_INET, remote_endpoint->address, &(addr_in.sin_addr)); |
| 1413 |
if (pton_err != 1) { |
| 1414 |
int aws_err = s_convert_pton_error(pton_err); |
| 1415 |
socket->state = ERRORED; |
| 1416 |
return aws_raise_error(aws_err); |
| 1417 |
} |
| 1418 |
|
| 1419 |
addr_in.sin_port = htons((uint16_t)remote_endpoint->port); |
| 1420 |
addr_in.sin_family = AF_INET; |
| 1421 |
|
| 1422 |
return s_dgram_connect(socket, remote_endpoint, connect_loop, (struct sockaddr *)&addr_in, sizeof(addr_in)); |
| 1423 |
} |
| 1424 |
|
| 1425 |
static int s_ipv6_dgram_connect( |
| 1426 |
struct aws_socket *socket, |
| 1427 |
const struct aws_socket_endpoint *remote_endpoint, |
| 1428 |
struct aws_event_loop *connect_loop, |
| 1429 |
aws_socket_on_connection_result_fn *on_connection_result, |
| 1430 |
void *user_data) { |
| 1431 |
(void)user_data; |
| 1432 |
|
| 1433 |
|
| 1434 |
socket->connection_result_fn = on_connection_result; |
| 1435 |
socket->connect_accept_user_data = user_data; |
| 1436 |
struct sockaddr_in6 addr_in6; |
| 1437 |
AWS_ZERO_STRUCT(addr_in6); |
| 1438 |
int pton_err = inet_pton(AF_INET6, remote_endpoint->address, &(addr_in6.sin6_addr)); |
| 1439 |
|
| 1440 |
if (pton_err != 1) { |
| 1441 |
int aws_err = s_convert_pton_error(pton_err); |
| 1442 |
socket->state = ERRORED; |
| 1443 |
return aws_raise_error(aws_err); |
| 1444 |
} |
| 1445 |
|
| 1446 |
addr_in6.sin6_port = htons((uint16_t)remote_endpoint->port); |
| 1447 |
addr_in6.sin6_family = AF_INET6; |
| 1448 |
|
| 1449 |
return s_dgram_connect(socket, remote_endpoint, connect_loop, (struct sockaddr *)&addr_in6, sizeof(addr_in6)); |
| 1450 |
} |
| 1451 |
|
| 1452 |
static inline int s_tcp_bind(struct aws_socket *socket, struct sockaddr *sock_addr, size_t sock_size) { |
| 1453 |
|
| 1454 |
|
| 1455 |
int exclusive_use_val = 1; |
| 1456 |
if (setsockopt( |
| 1457 |
(SOCKET)socket->io_handle.data.handle, |
| 1458 |
SOL_SOCKET, |
| 1459 |
SO_EXCLUSIVEADDRUSE, |
| 1460 |
(char *)&exclusive_use_val, |
| 1461 |
sizeof(int))) { |
| 1462 |
int wsa_err = WSAGetLastError(); |
| 1463 |
AWS_LOGF_WARN( |
| 1464 |
AWS_LS_IO_SOCKET, |
| 1465 |
"id=%p handle=%p: setsockopt() call for enabling SO_EXCLUSIVEADDRUSE failed with WSAError %d", |
| 1466 |
(void *)socket, |
| 1467 |
(void *)socket->io_handle.data.handle, |
| 1468 |
wsa_err); |
| 1469 |
aws_raise_error(s_determine_socket_error(wsa_err)); |
| 1470 |
goto error; |
| 1471 |
} |
| 1472 |
|
| 1473 |
if (bind((SOCKET)socket->io_handle.data.handle, sock_addr, (int)sock_size) != 0) { |
| 1474 |
int wsa_err = WSAGetLastError(); |
| 1475 |
AWS_LOGF_ERROR( |
| 1476 |
AWS_LS_IO_SOCKET, |
| 1477 |
"id=%p handle=%p: error binding. error %d", |
| 1478 |
(void *)socket, |
| 1479 |
(void *)socket->io_handle.data.handle, |
| 1480 |
wsa_err); |
| 1481 |
aws_raise_error(s_determine_socket_error(wsa_err)); |
| 1482 |
goto error; |
| 1483 |
} |
| 1484 |
|
| 1485 |
if (s_update_local_endpoint_ipv4_ipv6(socket)) { |
| 1486 |
goto error; |
| 1487 |
} |
| 1488 |
|
| 1489 |
AWS_LOGF_INFO( |
| 1490 |
AWS_LS_IO_SOCKET, |
| 1491 |
"id=%p handle=%p: binding to tcp %s:%u", |
| 1492 |
(void *)socket, |
| 1493 |
(void *)socket->io_handle.data.handle, |
| 1494 |
socket->local_endpoint.address, |
| 1495 |
socket->local_endpoint.port); |
| 1496 |
|
| 1497 |
socket->state = BOUND; |
| 1498 |
return AWS_OP_SUCCESS; |
| 1499 |
|
| 1500 |
error: |
| 1501 |
socket->state = ERRORED; |
| 1502 |
return AWS_OP_ERR; |
| 1503 |
} |
| 1504 |
|
| 1505 |
static int s_ipv4_stream_bind(struct aws_socket *socket, const struct aws_socket_endpoint *local_endpoint) { |
| 1506 |
struct sockaddr_in addr_in; |
| 1507 |
AWS_ZERO_STRUCT(addr_in); |
| 1508 |
int pton_err = inet_pton(AF_INET, local_endpoint->address, &(addr_in.sin_addr)); |
| 1509 |
|
| 1510 |
if (pton_err != 1) { |
| 1511 |
int aws_err = s_convert_pton_error(pton_err); |
| 1512 |
socket->state = ERRORED; |
| 1513 |
return aws_raise_error(aws_err); |
| 1514 |
} |
| 1515 |
|
| 1516 |
addr_in.sin_port = htons((uint16_t)local_endpoint->port); |
| 1517 |
addr_in.sin_family = AF_INET; |
| 1518 |
|
| 1519 |
return s_tcp_bind(socket, (struct sockaddr *)&addr_in, sizeof(addr_in)); |
| 1520 |
} |
| 1521 |
|
| 1522 |
static int s_ipv6_stream_bind(struct aws_socket *socket, const struct aws_socket_endpoint *local_endpoint) { |
| 1523 |
struct sockaddr_in6 addr_in6; |
| 1524 |
AWS_ZERO_STRUCT(addr_in6); |
| 1525 |
int pton_err = inet_pton(AF_INET6, local_endpoint->address, &(addr_in6.sin6_addr)); |
| 1526 |
|
| 1527 |
if (pton_err != 1) { |
| 1528 |
int aws_err = s_convert_pton_error(pton_err); |
| 1529 |
socket->state = ERRORED; |
| 1530 |
return aws_raise_error(aws_err); |
| 1531 |
} |
| 1532 |
|
| 1533 |
addr_in6.sin6_port = htons((uint16_t)local_endpoint->port); |
| 1534 |
addr_in6.sin6_family = AF_INET6; |
| 1535 |
|
| 1536 |
return s_tcp_bind(socket, (struct sockaddr *)&addr_in6, sizeof(addr_in6)); |
| 1537 |
} |
| 1538 |
|
| 1539 |
static inline int s_udp_bind(struct aws_socket *socket, struct sockaddr *sock_addr, size_t sock_size) { |
| 1540 |
|
| 1541 |
if (bind((SOCKET)socket->io_handle.data.handle, sock_addr, (int)sock_size) != 0) { |
| 1542 |
int wsa_err = WSAGetLastError(); |
| 1543 |
AWS_LOGF_ERROR( |
| 1544 |
AWS_LS_IO_SOCKET, |
| 1545 |
"id=%p handle=%p: error binding. error %d", |
| 1546 |
(void *)socket, |
| 1547 |
(void *)socket->io_handle.data.handle, |
| 1548 |
wsa_err); |
| 1549 |
aws_raise_error(s_determine_socket_error(wsa_err)); |
| 1550 |
goto error; |
| 1551 |
} |
| 1552 |
|
| 1553 |
if (s_update_local_endpoint_ipv4_ipv6(socket)) { |
| 1554 |
goto error; |
| 1555 |
} |
| 1556 |
|
| 1557 |
AWS_LOGF_INFO( |
| 1558 |
AWS_LS_IO_SOCKET, |
| 1559 |
"id=%p handle=%p: binding to udp %s:%u", |
| 1560 |
(void *)socket, |
| 1561 |
(void *)socket->io_handle.data.handle, |
| 1562 |
socket->local_endpoint.address, |
| 1563 |
socket->local_endpoint.port); |
| 1564 |
|
| 1565 |
socket->state = CONNECTED_READ; |
| 1566 |
return AWS_OP_SUCCESS; |
| 1567 |
|
| 1568 |
error: |
| 1569 |
socket->state = ERRORED; |
| 1570 |
return AWS_OP_ERR; |
| 1571 |
} |
| 1572 |
|
| 1573 |
static int s_ipv4_dgram_bind(struct aws_socket *socket, const struct aws_socket_endpoint *local_endpoint) { |
| 1574 |
struct sockaddr_in addr_in; |
| 1575 |
AWS_ZERO_STRUCT(addr_in); |
| 1576 |
int pton_err = inet_pton(AF_INET, local_endpoint->address, &(addr_in.sin_addr)); |
| 1577 |
|
| 1578 |
if (pton_err != 1) { |
| 1579 |
int aws_err = s_convert_pton_error(pton_err); |
| 1580 |
socket->state = ERRORED; |
| 1581 |
return aws_raise_error(aws_err); |
| 1582 |
} |
| 1583 |
|
| 1584 |
addr_in.sin_port = htons((uint16_t)local_endpoint->port); |
| 1585 |
addr_in.sin_family = AF_INET; |
| 1586 |
|
| 1587 |
return s_udp_bind(socket, (struct sockaddr *)&addr_in, sizeof(addr_in)); |
| 1588 |
} |
| 1589 |
|
| 1590 |
static int s_ipv6_dgram_bind(struct aws_socket *socket, const struct aws_socket_endpoint *local_endpoint) { |
| 1591 |
struct sockaddr_in6 addr_in6; |
| 1592 |
AWS_ZERO_STRUCT(addr_in6); |
| 1593 |
int pton_err = inet_pton(AF_INET6, local_endpoint->address, &(addr_in6.sin6_addr)); |
| 1594 |
|
| 1595 |
if (pton_err != 1) { |
| 1596 |
int aws_err = s_convert_pton_error(pton_err); |
| 1597 |
socket->state = ERRORED; |
| 1598 |
return aws_raise_error(aws_err); |
| 1599 |
} |
| 1600 |
|
| 1601 |
addr_in6.sin6_port = htons((uint16_t)local_endpoint->port); |
| 1602 |
addr_in6.sin6_family = AF_INET6; |
| 1603 |
|
| 1604 |
return s_udp_bind(socket, (struct sockaddr *)&addr_in6, sizeof(addr_in6)); |
| 1605 |
} |
| 1606 |
|
| 1607 |
static int s_local_bind(struct aws_socket *socket, const struct aws_socket_endpoint *local_endpoint) { |
| 1608 |
AWS_LOGF_INFO( |
| 1609 |
AWS_LS_IO_SOCKET, |
| 1610 |
"id=%p handle=%p: binding to named pipe %s", |
| 1611 |
(void *)socket, |
| 1612 |
(void *)socket->io_handle.data.handle, |
| 1613 |
local_endpoint->address); |
| 1614 |
|
| 1615 |
socket->local_endpoint = *local_endpoint; |
| 1616 |
socket->io_handle.data.handle = CreateNamedPipeA( |
| 1617 |
local_endpoint->address, |
| 1618 |
PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, |
| 1619 |
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT | PIPE_ACCEPT_REMOTE_CLIENTS, |
| 1620 |
PIPE_UNLIMITED_INSTANCES, |
| 1621 |
PIPE_BUFFER_SIZE, |
| 1622 |
PIPE_BUFFER_SIZE, |
| 1623 |
0, |
| 1624 |
NULL); |
| 1625 |
|
| 1626 |
if (socket->io_handle.data.handle != INVALID_HANDLE_VALUE) { |
| 1627 |
socket->state = BOUND; |
| 1628 |
return AWS_OP_SUCCESS; |
| 1629 |
} else { |
| 1630 |
int error_code = GetLastError(); |
| 1631 |
AWS_LOGF_ERROR( |
| 1632 |
AWS_LS_IO_SOCKET, |
| 1633 |
"id=%p handle=%p: failed to open named pipe %s with error %d", |
| 1634 |
(void *)socket, |
| 1635 |
(void *)socket->io_handle.data.handle, |
| 1636 |
local_endpoint->address, |
| 1637 |
error_code); |
| 1638 |
|
| 1639 |
socket->state = ERRORED; |
| 1640 |
int aws_error = s_determine_socket_error(error_code); |
| 1641 |
return aws_raise_error(aws_error); |
| 1642 |
} |
| 1643 |
} |
| 1644 |
|
| 1645 |
static int s_tcp_listen(struct aws_socket *socket, int backlog_size) { |
| 1646 |
AWS_LOGF_INFO( |
| 1647 |
AWS_LS_IO_SOCKET, |
| 1648 |
"id=%p handle=%p: starting listen with backlog %d", |
| 1649 |
(void *)socket, |
| 1650 |
(void *)socket->io_handle.data.handle, |
| 1651 |
backlog_size); |
| 1652 |
int error_code = listen((SOCKET)socket->io_handle.data.handle, backlog_size); |
| 1653 |
|
| 1654 |
if (!error_code) { |
| 1655 |
socket->state = LISTENING; |
| 1656 |
return AWS_OP_SUCCESS; |
| 1657 |
} |
| 1658 |
|
| 1659 |
error_code = GetLastError(); |
| 1660 |
AWS_LOGF_ERROR( |
| 1661 |
AWS_LS_IO_SOCKET, |
| 1662 |
"id=%p handle=%p: listen failed with error code %d", |
| 1663 |
(void *)socket, |
| 1664 |
(void *)socket->io_handle.data.handle, |
| 1665 |
error_code); |
| 1666 |
int aws_error = s_determine_socket_error(error_code); |
| 1667 |
return aws_raise_error(aws_error); |
| 1668 |
} |
| 1669 |
|
| 1670 |
static int s_udp_listen(struct aws_socket *socket, int backlog_size) { |
| 1671 |
(void)socket; |
| 1672 |
(void)backlog_size; |
| 1673 |
return aws_raise_error(AWS_IO_SOCKET_INVALID_OPERATION_FOR_TYPE); |
| 1674 |
} |
| 1675 |
|
| 1676 |
static int s_local_listen(struct aws_socket *socket, int backlog_size) { |
| 1677 |
(void)socket; |
| 1678 |
(void)backlog_size; |
| 1679 |
if (AWS_UNLIKELY(socket->state != BOUND)) { |
| 1680 |
return aws_raise_error(AWS_IO_SOCKET_ILLEGAL_OPERATION_FOR_STATE); |
| 1681 |
} |
| 1682 |
|
| 1683 |
socket->state = LISTENING; |
| 1684 |
return AWS_OP_SUCCESS; |
| 1685 |
} |
| 1686 |
|
| 1687 |
|
| 1688 |
static void s_incoming_pipe_connection_event( |
| 1689 |
struct aws_event_loop *event_loop, |
| 1690 |
struct aws_overlapped *overlapped, |
| 1691 |
int status_code, |
| 1692 |
size_t num_bytes_transferred) { |
| 1693 |
(void)event_loop; |
| 1694 |
(void)num_bytes_transferred; |
| 1695 |
|
| 1696 |
struct io_operation_data *operation_data = AWS_CONTAINER_OF(overlapped, struct io_operation_data, signal); |
| 1697 |
struct aws_socket *socket = overlapped->user_data; |
| 1698 |
|
| 1699 |
if (!operation_data->socket) { |
| 1700 |
aws_mem_release(operation_data->allocator, operation_data); |
| 1701 |
return; |
| 1702 |
} |
| 1703 |
|
| 1704 |
if (status_code == IO_OPERATION_CANCELLED) { |
| 1705 |
operation_data->in_use = false; |
| 1706 |
return; |
| 1707 |
} |
| 1708 |
|
| 1709 |
AWS_LOGF_DEBUG( |
| 1710 |
AWS_LS_IO_SOCKET, |
| 1711 |
"id=%p handle=%p: named-pipe listening event received", |
| 1712 |
(void *)socket, |
| 1713 |
(void *)socket->io_handle.data.handle); |
| 1714 |
struct iocp_socket *socket_impl = socket->impl; |
| 1715 |
|
| 1716 |
if (status_code) { |
| 1717 |
if (status_code == IO_PIPE_BROKEN) { |
| 1718 |
AWS_LOGF_ERROR( |
| 1719 |
AWS_LS_IO_SOCKET, |
| 1720 |
"id=%p handle=%p: named-pipe is closed", |
| 1721 |
(void *)socket, |
| 1722 |
(void *)socket->io_handle.data.handle); |
| 1723 |
aws_raise_error(AWS_IO_SOCKET_CLOSED); |
| 1724 |
socket->state = CLOSED; |
| 1725 |
} else { |
| 1726 |
AWS_LOGF_ERROR( |
| 1727 |
AWS_LS_IO_SOCKET, |
| 1728 |
"id=%p handle=%p: named-pipe error %d", |
| 1729 |
(void *)socket, |
| 1730 |
(void *)socket->io_handle.data.handle, |
| 1731 |
(int)GetLastError()); |
| 1732 |
aws_raise_error(s_determine_socket_error(status_code)); |
| 1733 |
socket->state = ERRORED; |
| 1734 |
} |
| 1735 |
|
| 1736 |
socket_impl->winsock_vtable->connection_error(socket, aws_last_error()); |
| 1737 |
operation_data->in_use = false; |
| 1738 |
return; |
| 1739 |
} |
| 1740 |
|
| 1741 |
bool continue_accept_loop = !socket_impl->stop_accept; |
| 1742 |
|
| 1743 |
do { |
| 1744 |
AWS_LOGF_DEBUG( |
| 1745 |
AWS_LS_IO_SOCKET, |
| 1746 |
"id=%p handle=%p: incoming connection", |
| 1747 |
(void *)socket, |
| 1748 |
(void *)socket->io_handle.data.handle); |
| 1749 |
struct aws_socket *new_socket = aws_mem_acquire(socket->allocator, sizeof(struct aws_socket)); |
| 1750 |
|
| 1751 |
if (!new_socket) { |
| 1752 |
socket->state = ERRORED; |
| 1753 |
operation_data->in_use = false; |
| 1754 |
socket_impl->winsock_vtable->connection_error(socket, AWS_ERROR_OOM); |
| 1755 |
return; |
| 1756 |
} |
| 1757 |
|
| 1758 |
if (s_socket_init(new_socket, socket->allocator, &socket->options, false)) { |
| 1759 |
aws_mem_release(socket->allocator, new_socket); |
| 1760 |
socket->state = ERRORED; |
| 1761 |
operation_data->in_use = false; |
| 1762 |
socket_impl->winsock_vtable->connection_error(socket, aws_last_error()); |
| 1763 |
return; |
| 1764 |
} |
| 1765 |
|
| 1766 |
new_socket->state = CONNECTED_WRITE | CONNECTED_READ; |
| 1767 |
|
| 1768 |
|
| 1769 |
|
| 1770 |
|
| 1771 |
new_socket->io_handle = socket->io_handle; |
| 1772 |
aws_event_loop_unsubscribe_from_io_events(event_loop, &new_socket->io_handle); |
| 1773 |
new_socket->event_loop = NULL; |
| 1774 |
|
| 1775 |
socket->io_handle.data.handle = CreateNamedPipeA( |
| 1776 |
socket->local_endpoint.address, |
| 1777 |
PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, |
| 1778 |
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT | PIPE_ACCEPT_REMOTE_CLIENTS, |
| 1779 |
PIPE_UNLIMITED_INSTANCES, |
| 1780 |
PIPE_BUFFER_SIZE, |
| 1781 |
PIPE_BUFFER_SIZE, |
| 1782 |
0, |
| 1783 |
NULL); |
| 1784 |
|
| 1785 |
if (socket->io_handle.data.handle == INVALID_HANDLE_VALUE) { |
| 1786 |
AWS_LOGF_ERROR( |
| 1787 |
AWS_LS_IO_SOCKET, |
| 1788 |
"id=%p handle=%p: error rebinding named pipe with error %d", |
| 1789 |
(void *)socket, |
| 1790 |
(void *)socket->io_handle.data.handle, |
| 1791 |
(int)GetLastError()); |
| 1792 |
socket->state = ERRORED; |
| 1793 |
operation_data->in_use = false; |
| 1794 |
socket_impl->winsock_vtable->connection_error(socket, aws_last_error()); |
| 1795 |
return; |
| 1796 |
} |
| 1797 |
|
| 1798 |
aws_overlapped_init(&socket_impl->read_io_data->signal, s_incoming_pipe_connection_event, socket); |
| 1799 |
socket->event_loop = NULL; |
| 1800 |
if (aws_socket_assign_to_event_loop(socket, event_loop)) { |
| 1801 |
socket->state = ERRORED; |
| 1802 |
operation_data->in_use = false; |
| 1803 |
aws_socket_clean_up(new_socket); |
| 1804 |
socket_impl->winsock_vtable->connection_error(socket, aws_last_error()); |
| 1805 |
return; |
| 1806 |
} |
| 1807 |
|
| 1808 |
socket->accept_result_fn(socket, AWS_ERROR_SUCCESS, new_socket, socket->connect_accept_user_data); |
| 1809 |
|
| 1810 |
if (!operation_data->socket) { |
| 1811 |
socket->state = ERRORED; |
| 1812 |
operation_data->in_use = false; |
| 1813 |
aws_mem_release(operation_data->allocator, operation_data); |
| 1814 |
return; |
| 1815 |
} |
| 1816 |
|
| 1817 |
socket_impl->read_io_data->in_use = true; |
| 1818 |
BOOL res = ConnectNamedPipe( |
| 1819 |
socket->io_handle.data.handle, aws_overlapped_to_windows_overlapped(&socket_impl->read_io_data->signal)); |
| 1820 |
|
| 1821 |
continue_accept_loop = false; |
| 1822 |
|
| 1823 |
if (!res) { |
| 1824 |
int error_code = GetLastError(); |
| 1825 |
if (error_code != ERROR_IO_PENDING && error_code != ERROR_PIPE_CONNECTED) { |
| 1826 |
AWS_LOGF_ERROR( |
| 1827 |
AWS_LS_IO_SOCKET, |
| 1828 |
"id=%p handle=%p: named-pipe connect failed with error %d", |
| 1829 |
(void *)socket, |
| 1830 |
(void *)socket->io_handle.data.handle, |
| 1831 |
error_code); |
| 1832 |
socket->state = ERRORED; |
| 1833 |
socket_impl->read_io_data->in_use = false; |
| 1834 |
int aws_err = s_determine_socket_error(error_code); |
| 1835 |
socket_impl->winsock_vtable->connection_error(socket, aws_err); |
| 1836 |
return; |
| 1837 |
} else if (error_code == ERROR_PIPE_CONNECTED) { |
| 1838 |
continue_accept_loop = true; |
| 1839 |
} else { |
| 1840 |
AWS_LOGF_TRACE( |
| 1841 |
AWS_LS_IO_SOCKET, |
| 1842 |
"id=%p handle=%p: no pending connections exiting accept loop.", |
| 1843 |
(void *)socket, |
| 1844 |
(void *)socket->io_handle.data.handle); |
| 1845 |
} |
| 1846 |
} |
| 1847 |
} while (continue_accept_loop && !socket_impl->stop_accept); |
| 1848 |
} |
| 1849 |
|
| 1850 |
static void s_tcp_accept_event( |
| 1851 |
struct aws_event_loop *event_loop, |
| 1852 |
struct aws_overlapped *overlapped, |
| 1853 |
int status_code, |
| 1854 |
size_t num_bytes_transferred); |
| 1855 |
|
| 1856 |
static int s_socket_setup_accept(struct aws_socket *socket, struct aws_event_loop *accept_loop) { |
| 1857 |
struct iocp_socket *socket_impl = socket->impl; |
| 1858 |
socket_impl->incoming_socket = aws_mem_acquire(socket->allocator, sizeof(struct aws_socket)); |
| 1859 |
|
| 1860 |
if (!socket_impl->incoming_socket) { |
| 1861 |
return AWS_OP_ERR; |
| 1862 |
} |
| 1863 |
|
| 1864 |
int err = s_socket_init(socket_impl->incoming_socket, socket->allocator, &socket->options, true); |
| 1865 |
|
| 1866 |
if (err) { |
| 1867 |
socket->state = ERRORED; |
| 1868 |
aws_socket_clean_up(socket_impl->incoming_socket); |
| 1869 |
aws_mem_release(socket->allocator, socket_impl->incoming_socket); |
| 1870 |
socket_impl->incoming_socket = NULL; |
| 1871 |
return AWS_OP_ERR; |
| 1872 |
} |
| 1873 |
|
| 1874 |
socket_impl->incoming_socket->local_endpoint = socket->local_endpoint; |
| 1875 |
socket_impl->incoming_socket->state = INIT; |
| 1876 |
|
| 1877 |
if (accept_loop && aws_socket_assign_to_event_loop(socket, accept_loop)) { |
| 1878 |
socket->state = ERRORED; |
| 1879 |
aws_socket_clean_up(socket_impl->incoming_socket); |
| 1880 |
aws_mem_release(socket->allocator, socket_impl->incoming_socket); |
| 1881 |
socket_impl->incoming_socket = NULL; |
| 1882 |
return AWS_OP_ERR; |
| 1883 |
} |
| 1884 |
|
| 1885 |
aws_overlapped_init(&socket_impl->read_io_data->signal, s_tcp_accept_event, socket); |
| 1886 |
|
| 1887 |
LPFN_ACCEPTEX accept_fn = (LPFN_ACCEPTEX)aws_winsock_get_acceptex_fn(); |
| 1888 |
socket_impl->read_io_data->in_use = true; |
| 1889 |
|
| 1890 |
while (true) { |
| 1891 |
AWS_LOGF_TRACE( |
| 1892 |
AWS_LS_IO_SOCKET, |
| 1893 |
"id=%p handle=%p: performing non-blocking accept", |
| 1894 |
(void *)socket, |
| 1895 |
(void *)socket->io_handle.data.handle); |
| 1896 |
BOOL res = accept_fn( |
| 1897 |
(SOCKET)socket->io_handle.data.handle, |
| 1898 |
(SOCKET)socket_impl->incoming_socket->io_handle.data.handle, |
| 1899 |
socket_impl->accept_buffer, |
| 1900 |
0, |
| 1901 |
SOCK_STORAGE_SIZE, |
| 1902 |
SOCK_STORAGE_SIZE, |
| 1903 |
NULL, |
| 1904 |
aws_overlapped_to_windows_overlapped(&socket_impl->read_io_data->signal)); |
| 1905 |
|
| 1906 |
if (!res) { |
| 1907 |
int win_err = WSAGetLastError(); |
| 1908 |
if (win_err == ERROR_IO_PENDING) { |
| 1909 |
AWS_LOGF_TRACE( |
| 1910 |
AWS_LS_IO_SOCKET, |
| 1911 |
"id=%p handle=%p: no pending incoming connections, exiting loop.", |
| 1912 |
(void *)socket, |
| 1913 |
(void *)socket->io_handle.data.handle); |
| 1914 |
return aws_raise_error(AWS_IO_READ_WOULD_BLOCK); |
| 1915 |
} else if (AWS_UNLIKELY(win_err == WSAECONNRESET)) { |
| 1916 |
continue; |
| 1917 |
} |
| 1918 |
|
| 1919 |
AWS_LOGF_ERROR( |
| 1920 |
AWS_LS_IO_SOCKET, |
| 1921 |
"id=%p handle=%p: accept failed with error %d", |
| 1922 |
(void *)socket, |
| 1923 |
(void *)socket->io_handle.data.handle, |
| 1924 |
(int)win_err); |
| 1925 |
socket->state = ERRORED; |
| 1926 |
socket_impl->read_io_data->in_use = false; |
| 1927 |
aws_mem_release(socket->allocator, socket_impl->incoming_socket); |
| 1928 |
socket_impl->incoming_socket = NULL; |
| 1929 |
int aws_err = s_determine_socket_error(win_err); |
| 1930 |
return aws_raise_error(aws_err); |
| 1931 |
} |
| 1932 |
|
| 1933 |
return AWS_OP_SUCCESS; |
| 1934 |
} |
| 1935 |
} |
| 1936 |
|
| 1937 |
|
| 1938 |
static void s_tcp_accept_event( |
| 1939 |
struct aws_event_loop *event_loop, |
| 1940 |
struct aws_overlapped *overlapped, |
| 1941 |
int status_code, |
| 1942 |
size_t num_bytes_transferred) { |
| 1943 |
|
| 1944 |
(void)event_loop; |
| 1945 |
(void)num_bytes_transferred; |
| 1946 |
|
| 1947 |
struct io_operation_data *operation_data = AWS_CONTAINER_OF(overlapped, struct io_operation_data, signal); |
| 1948 |
struct aws_socket *socket = overlapped->user_data; |
| 1949 |
|
| 1950 |
if (!operation_data->socket) { |
| 1951 |
aws_mem_release(operation_data->allocator, operation_data); |
| 1952 |
return; |
| 1953 |
} |
| 1954 |
|
| 1955 |
if (status_code == IO_OPERATION_CANCELLED || status_code == WSAECONNRESET) { |
| 1956 |
operation_data->in_use = false; |
| 1957 |
return; |
| 1958 |
} |
| 1959 |
|
| 1960 |
AWS_LOGF_TRACE( |
| 1961 |
AWS_LS_IO_SOCKET, |
| 1962 |
"id=%p handle=%p: accept event triggered.", |
| 1963 |
(void *)socket, |
| 1964 |
(void *)socket->io_handle.data.handle); |
| 1965 |
struct iocp_socket *socket_impl = socket->impl; |
| 1966 |
|
| 1967 |
if (!status_code && !socket_impl->stop_accept) { |
| 1968 |
int err = AWS_OP_SUCCESS; |
| 1969 |
|
| 1970 |
do { |
| 1971 |
socket_impl->incoming_socket->state = CONNECTED_WRITE | CONNECTED_READ; |
| 1972 |
|
| 1973 |
uint32_t port = 0; |
| 1974 |
|
| 1975 |
struct sockaddr_storage *in_addr = (struct sockaddr_storage *)socket_impl->accept_buffer; |
| 1976 |
|
| 1977 |
if (in_addr->ss_family == AF_INET) { |
| 1978 |
struct sockaddr_in *s = (struct sockaddr_in *)in_addr; |
| 1979 |
port = ntohs(s->sin_port); |
| 1980 |
|
| 1981 |
InetNtopA( |
| 1982 |
AF_INET, |
| 1983 |
&s->sin_addr, |
| 1984 |
socket_impl->incoming_socket->remote_endpoint.address, |
| 1985 |
sizeof(socket_impl->incoming_socket->remote_endpoint.address)); |
| 1986 |
socket_impl->incoming_socket->options.domain = AWS_SOCKET_IPV4; |
| 1987 |
} else if (in_addr->ss_family == AF_INET6) { |
| 1988 |
struct sockaddr_in6 *s = (struct sockaddr_in6 *)in_addr; |
| 1989 |
port = ntohs(s->sin6_port); |
| 1990 |
|
| 1991 |
InetNtopA( |
| 1992 |
AF_INET6, |
| 1993 |
&s->sin6_addr, |
| 1994 |
socket_impl->incoming_socket->remote_endpoint.address, |
| 1995 |
sizeof(socket_impl->incoming_socket->remote_endpoint.address)); |
| 1996 |
socket_impl->incoming_socket->options.domain = AWS_SOCKET_IPV6; |
| 1997 |
} |
| 1998 |
|
| 1999 |
socket_impl->incoming_socket->remote_endpoint.port = port; |
| 2000 |
AWS_LOGF_INFO( |
| 2001 |
AWS_LS_IO_SOCKET, |
| 2002 |
"id=%p handle=%p: incoming connection accepted from %s:%u.", |
| 2003 |
(void *)socket, |
| 2004 |
(void *)socket->io_handle.data.handle, |
| 2005 |
socket_impl->incoming_socket->remote_endpoint.address, |
| 2006 |
port); |
| 2007 |
|
| 2008 |
u_long non_blocking = 1; |
| 2009 |
ioctlsocket((SOCKET)socket_impl->incoming_socket->io_handle.data.handle, FIONBIO, &non_blocking); |
| 2010 |
aws_socket_set_options(socket_impl->incoming_socket, &socket->options); |
| 2011 |
|
| 2012 |
struct aws_socket *incoming_socket = socket_impl->incoming_socket; |
| 2013 |
socket_impl->incoming_socket = NULL; |
| 2014 |
socket->accept_result_fn(socket, AWS_ERROR_SUCCESS, incoming_socket, socket->connect_accept_user_data); |
| 2015 |
if (!operation_data->socket) { |
| 2016 |
aws_mem_release(operation_data->allocator, operation_data); |
| 2017 |
return; |
| 2018 |
} |
| 2019 |
|
| 2020 |
socket_impl->incoming_socket = NULL; |
| 2021 |
err = s_socket_setup_accept(socket, NULL); |
| 2022 |
|
| 2023 |
if (err) { |
| 2024 |
if (aws_last_error() != AWS_IO_READ_WOULD_BLOCK) { |
| 2025 |
socket->state = ERRORED; |
| 2026 |
socket_impl->winsock_vtable->connection_error(socket, aws_last_error()); |
| 2027 |
} |
| 2028 |
return; |
| 2029 |
} |
| 2030 |
} while (!err && !socket_impl->stop_accept); |
| 2031 |
} else if (status_code) { |
| 2032 |
AWS_LOGF_ERROR( |
| 2033 |
AWS_LS_IO_SOCKET, |
| 2034 |
"id=%p handle=%p: error occurred %d.", |
| 2035 |
(void *)socket, |
| 2036 |
(void *)socket->io_handle.data.handle, |
| 2037 |
status_code); |
| 2038 |
socket->state = ERRORED; |
| 2039 |
int aws_error = s_determine_socket_error(status_code); |
| 2040 |
aws_raise_error(aws_error); |
| 2041 |
socket_impl->winsock_vtable->connection_error(socket, aws_error); |
| 2042 |
operation_data->in_use = false; |
| 2043 |
} |
| 2044 |
} |
| 2045 |
|
| 2046 |
static void s_process_invoke_on_accept_start(struct aws_task *task, void *args, enum aws_task_status status) { |
| 2047 |
(void)task; |
| 2048 |
struct on_start_accept_result_args *on_accept_args = args; |
| 2049 |
if (status == AWS_TASK_STATUS_RUN_READY) { |
| 2050 |
struct aws_socket *socket = on_accept_args->socket; |
| 2051 |
|
| 2052 |
if (on_accept_args->on_accept_start) { |
| 2053 |
|
| 2054 |
AWS_FATAL_ASSERT(socket); |
| 2055 |
on_accept_args->on_accept_start(socket, on_accept_args->error, on_accept_args->on_accept_start_user_data); |
| 2056 |
} |
| 2057 |
} |
| 2058 |
aws_mem_release(on_accept_args->allocator, args); |
| 2059 |
} |
| 2060 |
|
| 2061 |
static void s_invoke_on_accept_start( |
| 2062 |
struct aws_allocator *allocator, |
| 2063 |
struct aws_event_loop *loop, |
| 2064 |
struct aws_socket *socket, |
| 2065 |
int error, |
| 2066 |
aws_socket_on_accept_started_fn *on_accept_start, |
| 2067 |
void *on_accept_start_user_data) { |
| 2068 |
struct on_start_accept_result_args *args = aws_mem_calloc(allocator, 1, sizeof(struct on_start_accept_result_args)); |
| 2069 |
|
| 2070 |
args->allocator = allocator; |
| 2071 |
args->socket = socket; |
| 2072 |
args->error = error; |
| 2073 |
args->on_accept_start = on_accept_start; |
| 2074 |
args->on_accept_start_user_data = on_accept_start_user_data; |
| 2075 |
|
| 2076 |
aws_task_init(&args->task, s_process_invoke_on_accept_start, args, "SocketOnAcceptStartResultTask"); |
| 2077 |
aws_event_loop_schedule_task_now(loop, &args->task); |
| 2078 |
} |
| 2079 |
|
| 2080 |
static int s_tcp_start_accept( |
| 2081 |
struct aws_socket *socket, |
| 2082 |
struct aws_event_loop *accept_loop, |
| 2083 |
struct aws_socket_listener_options options) { |
| 2084 |
AWS_ASSERT(accept_loop); |
| 2085 |
AWS_ASSERT(options.on_accept_result); |
| 2086 |
|
| 2087 |
if (AWS_UNLIKELY(socket->state != LISTENING)) { |
| 2088 |
AWS_LOGF_ERROR( |
| 2089 |
AWS_LS_IO_SOCKET, |
| 2090 |
"id=%p handle=%p: invalid state for start_accept operation. You must call listen first.", |
| 2091 |
(void *)socket, |
| 2092 |
(void *)socket->io_handle.data.handle); |
| 2093 |
return aws_raise_error(AWS_IO_SOCKET_ILLEGAL_OPERATION_FOR_STATE); |
| 2094 |
} |
| 2095 |
|
| 2096 |
if (AWS_UNLIKELY(socket->event_loop && socket->event_loop != accept_loop)) { |
| 2097 |
AWS_LOGF_ERROR( |
| 2098 |
AWS_LS_IO_SOCKET, |
| 2099 |
"id=%p handle=%p: is already assigned to event-loop %p.", |
| 2100 |
(void *)socket, |
| 2101 |
(void *)socket->io_handle.data.handle, |
| 2102 |
(void *)socket->event_loop); |
| 2103 |
return aws_raise_error(AWS_IO_EVENT_LOOP_ALREADY_ASSIGNED); |
| 2104 |
} |
| 2105 |
|
| 2106 |
struct iocp_socket *socket_impl = socket->impl; |
| 2107 |
|
| 2108 |
if (!socket_impl->read_io_data) { |
| 2109 |
socket_impl->read_io_data = aws_mem_calloc(socket->allocator, 1, sizeof(struct io_operation_data)); |
| 2110 |
if (!socket_impl->read_io_data) { |
| 2111 |
socket->state = ERRORED; |
| 2112 |
return AWS_OP_ERR; |
| 2113 |
} |
| 2114 |
socket_impl->read_io_data->allocator = socket->allocator; |
| 2115 |
socket_impl->read_io_data->in_use = false; |
| 2116 |
socket_impl->read_io_data->socket = socket; |
| 2117 |
} |
| 2118 |
|
| 2119 |
socket->accept_result_fn = options.on_accept_result; |
| 2120 |
socket->connect_accept_user_data = options.on_accept_result_user_data; |
| 2121 |
socket_impl->stop_accept = false; |
| 2122 |
|
| 2123 |
struct aws_event_loop *el_to_use = !socket->event_loop ? accept_loop : NULL; |
| 2124 |
int err = s_socket_setup_accept(socket, el_to_use); |
| 2125 |
|
| 2126 |
if (!err || aws_last_error() == AWS_IO_READ_WOULD_BLOCK) { |
| 2127 |
s_invoke_on_accept_start( |
| 2128 |
socket->allocator, |
| 2129 |
accept_loop, |
| 2130 |
socket, |
| 2131 |
AWS_OP_SUCCESS, |
| 2132 |
options.on_accept_start, |
| 2133 |
options.on_accept_start_user_data); |
| 2134 |
return AWS_OP_SUCCESS; |
| 2135 |
} |
| 2136 |
|
| 2137 |
socket->state = ERRORED; |
| 2138 |
return AWS_OP_ERR; |
| 2139 |
} |
| 2140 |
|
| 2141 |
struct stop_accept_args { |
| 2142 |
struct aws_mutex mutex; |
| 2143 |
struct aws_condition_variable condition_var; |
| 2144 |
struct aws_socket *socket; |
| 2145 |
bool invoked; |
| 2146 |
int ret_code; |
| 2147 |
}; |
| 2148 |
|
| 2149 |
static bool s_stop_accept_predicate(void *arg) { |
| 2150 |
struct stop_accept_args *stop_accept_args = arg; |
| 2151 |
return stop_accept_args->invoked; |
| 2152 |
} |
| 2153 |
|
| 2154 |
static int s_stream_stop_accept(struct aws_socket *socket); |
| 2155 |
|
| 2156 |
static void s_stop_accept_task(struct aws_task *task, void *arg, enum aws_task_status status) { |
| 2157 |
(void)task; |
| 2158 |
(void)status; |
| 2159 |
|
| 2160 |
struct stop_accept_args *stop_accept_args = arg; |
| 2161 |
aws_mutex_lock(&stop_accept_args->mutex); |
| 2162 |
stop_accept_args->ret_code = AWS_OP_SUCCESS; |
| 2163 |
|
| 2164 |
if (aws_socket_stop_accept(stop_accept_args->socket)) { |
| 2165 |
stop_accept_args->ret_code = aws_last_error(); |
| 2166 |
} |
| 2167 |
stop_accept_args->invoked = true; |
| 2168 |
aws_condition_variable_notify_one(&stop_accept_args->condition_var); |
| 2169 |
aws_mutex_unlock(&stop_accept_args->mutex); |
| 2170 |
} |
| 2171 |
|
| 2172 |
static int s_stream_stop_accept(struct aws_socket *socket) { |
| 2173 |
AWS_LOGF_INFO( |
| 2174 |
AWS_LS_IO_SOCKET, |
| 2175 |
"id=%p handle=%p: shutting down accept.", |
| 2176 |
(void *)socket, |
| 2177 |
(void *)socket->io_handle.data.handle); |
| 2178 |
AWS_ASSERT(socket->event_loop); |
| 2179 |
if (!aws_event_loop_thread_is_callers_thread(socket->event_loop)) { |
| 2180 |
struct stop_accept_args args = { |
| 2181 |
.mutex = AWS_MUTEX_INIT, |
| 2182 |
.condition_var = AWS_CONDITION_VARIABLE_INIT, |
| 2183 |
.socket = socket, |
| 2184 |
.ret_code = AWS_OP_SUCCESS, |
| 2185 |
}; |
| 2186 |
|
| 2187 |
struct aws_task stop_accept_task = { |
| 2188 |
.fn = s_stop_accept_task, |
| 2189 |
.arg = &args, |
| 2190 |
}; |
| 2191 |
|
| 2192 |
AWS_LOGF_DEBUG( |
| 2193 |
AWS_LS_IO_SOCKET, |
| 2194 |
"id=%p handle=%p: accept is shutting down, but it was called outside the " |
| 2195 |
" event-loop thread. Blocking waiting on shutdown", |
| 2196 |
(void *)socket, |
| 2197 |
(void *)socket->io_handle.data.handle); |
| 2198 |
aws_mutex_lock(&args.mutex); |
| 2199 |
aws_event_loop_schedule_task_now(socket->event_loop, &stop_accept_task); |
| 2200 |
aws_condition_variable_wait_pred(&args.condition_var, &args.mutex, s_stop_accept_predicate, &args); |
| 2201 |
aws_mutex_unlock(&args.mutex); |
| 2202 |
AWS_LOGF_DEBUG( |
| 2203 |
AWS_LS_IO_SOCKET, |
| 2204 |
"id=%p handle=%p: accept shutdown completed", |
| 2205 |
(void *)socket, |
| 2206 |
(void *)socket->io_handle.data.handle); |
| 2207 |
|
| 2208 |
if (args.ret_code) { |
| 2209 |
socket->state = ERRORED; |
| 2210 |
return aws_raise_error(args.ret_code); |
| 2211 |
} |
| 2212 |
|
| 2213 |
return AWS_OP_SUCCESS; |
| 2214 |
} |
| 2215 |
|
| 2216 |
struct iocp_socket *socket_impl = socket->impl; |
| 2217 |
socket_impl->stop_accept = true; |
| 2218 |
CancelIo(socket->io_handle.data.handle); |
| 2219 |
|
| 2220 |
if (!socket_impl->read_io_data && socket_impl->incoming_socket) { |
| 2221 |
aws_socket_clean_up(socket_impl->incoming_socket); |
| 2222 |
aws_mem_release(socket->allocator, socket_impl->incoming_socket); |
| 2223 |
socket_impl->incoming_socket = NULL; |
| 2224 |
} |
| 2225 |
|
| 2226 |
return AWS_OP_SUCCESS; |
| 2227 |
} |
| 2228 |
|
| 2229 |
static void s_named_pipe_is_ridiculous_task(struct aws_task *task, void *args, enum aws_task_status status) { |
| 2230 |
(void)task; |
| 2231 |
struct io_operation_data *io_data = args; |
| 2232 |
|
| 2233 |
if (!io_data->socket) { |
| 2234 |
aws_mem_release(io_data->allocator, io_data); |
| 2235 |
return; |
| 2236 |
} |
| 2237 |
|
| 2238 |
if (status == AWS_TASK_STATUS_RUN_READY) { |
| 2239 |
io_data->sequential_task_storage.fn = NULL; |
| 2240 |
io_data->sequential_task_storage.arg = NULL; |
| 2241 |
s_incoming_pipe_connection_event(io_data->socket->event_loop, &io_data->signal, AWS_OP_SUCCESS, 0); |
| 2242 |
} else { |
| 2243 |
io_data->in_use = false; |
| 2244 |
} |
| 2245 |
} |
| 2246 |
|
| 2247 |
static int s_local_start_accept( |
| 2248 |
struct aws_socket *socket, |
| 2249 |
struct aws_event_loop *accept_loop, |
| 2250 |
struct aws_socket_listener_options options) { |
| 2251 |
AWS_ASSERT(accept_loop); |
| 2252 |
AWS_ASSERT(options.on_accept_result); |
| 2253 |
|
| 2254 |
if (AWS_UNLIKELY(socket->state != LISTENING)) { |
| 2255 |
AWS_LOGF_ERROR( |
| 2256 |
AWS_LS_IO_SOCKET, |
| 2257 |
"id=%p handle=%p: invalid state for start_accept operation. You must call listen first.", |
| 2258 |
(void *)socket, |
| 2259 |
(void *)socket->io_handle.data.handle); |
| 2260 |
return aws_raise_error(AWS_IO_SOCKET_ILLEGAL_OPERATION_FOR_STATE); |
| 2261 |
} |
| 2262 |
|
| 2263 |
if (AWS_UNLIKELY(socket->event_loop && socket->event_loop != accept_loop)) { |
| 2264 |
AWS_LOGF_ERROR( |
| 2265 |
AWS_LS_IO_SOCKET, |
| 2266 |
"id=%p handle=%p: is already assigned to event-loop %p.", |
| 2267 |
(void *)socket, |
| 2268 |
(void *)socket->io_handle.data.handle, |
| 2269 |
(void *)socket->event_loop); |
| 2270 |
return aws_raise_error(AWS_IO_EVENT_LOOP_ALREADY_ASSIGNED); |
| 2271 |
} |
| 2272 |
|
| 2273 |
AWS_LOGF_INFO( |
| 2274 |
AWS_LS_IO_SOCKET, |
| 2275 |
"id=%p handle=%p: is starting to accept incoming connections", |
| 2276 |
(void *)socket, |
| 2277 |
(void *)socket->io_handle.data.handle); |
| 2278 |
struct iocp_socket *socket_impl = socket->impl; |
| 2279 |
|
| 2280 |
if (!socket_impl->read_io_data) { |
| 2281 |
socket_impl->read_io_data = aws_mem_calloc(socket->allocator, 1, sizeof(struct io_operation_data)); |
| 2282 |
if (!socket_impl->read_io_data) { |
| 2283 |
socket->state = ERRORED; |
| 2284 |
return AWS_OP_ERR; |
| 2285 |
} |
| 2286 |
socket_impl->read_io_data->allocator = socket->allocator; |
| 2287 |
socket_impl->read_io_data->in_use = false; |
| 2288 |
socket_impl->read_io_data->socket = socket; |
| 2289 |
} |
| 2290 |
|
| 2291 |
socket->accept_result_fn = options.on_accept_result; |
| 2292 |
socket->connect_accept_user_data = options.on_accept_result_user_data; |
| 2293 |
socket_impl->stop_accept = false; |
| 2294 |
aws_overlapped_init(&socket_impl->read_io_data->signal, s_incoming_pipe_connection_event, socket); |
| 2295 |
socket_impl->read_io_data->in_use = true; |
| 2296 |
|
| 2297 |
if (!socket->event_loop && aws_socket_assign_to_event_loop(socket, accept_loop)) { |
| 2298 |
socket_impl->read_io_data->in_use = false; |
| 2299 |
socket->state = ERRORED; |
| 2300 |
return AWS_OP_ERR; |
| 2301 |
} |
| 2302 |
|
| 2303 |
BOOL res = ConnectNamedPipe( |
| 2304 |
socket->io_handle.data.handle, aws_overlapped_to_windows_overlapped(&socket_impl->read_io_data->signal)); |
| 2305 |
|
| 2306 |
if (!res) { |
| 2307 |
int error_code = GetLastError(); |
| 2308 |
if (error_code != ERROR_IO_PENDING && error_code != ERROR_PIPE_CONNECTED) { |
| 2309 |
AWS_LOGF_ERROR( |
| 2310 |
AWS_LS_IO_SOCKET, |
| 2311 |
"id=%p handle=%p: ConnectNamedPipe() failed with error %d.", |
| 2312 |
(void *)socket, |
| 2313 |
(void *)socket->io_handle.data.handle, |
| 2314 |
error_code); |
| 2315 |
socket->state = ERRORED; |
| 2316 |
socket_impl->read_io_data->in_use = false; |
| 2317 |
int aws_err = s_determine_socket_error(error_code); |
| 2318 |
return aws_raise_error(aws_err); |
| 2319 |
} else if (error_code == ERROR_PIPE_CONNECTED) { |
| 2320 |
AWS_LOGF_TRACE( |
| 2321 |
AWS_LS_IO_SOCKET, |
| 2322 |
"id=%p handle=%p: Pipe connected immediately, scheduling task for setup.", |
| 2323 |
(void *)socket, |
| 2324 |
(void *)socket->io_handle.data.handle, |
| 2325 |
error_code); |
| 2326 |
|
| 2327 |
|
| 2328 |
socket_impl->read_io_data->sequential_task_storage.fn = s_named_pipe_is_ridiculous_task; |
| 2329 |
socket_impl->read_io_data->sequential_task_storage.arg = socket_impl->read_io_data; |
| 2330 |
aws_event_loop_schedule_task_now(socket->event_loop, &socket_impl->read_io_data->sequential_task_storage); |
| 2331 |
} |
| 2332 |
} |
| 2333 |
|
| 2334 |
s_invoke_on_accept_start( |
| 2335 |
socket->allocator, |
| 2336 |
accept_loop, |
| 2337 |
socket, |
| 2338 |
AWS_OP_SUCCESS, |
| 2339 |
options.on_accept_start, |
| 2340 |
options.on_accept_start_user_data); |
| 2341 |
|
| 2342 |
return AWS_OP_SUCCESS; |
| 2343 |
} |
| 2344 |
|
| 2345 |
static int s_dgram_start_accept( |
| 2346 |
struct aws_socket *socket, |
| 2347 |
struct aws_event_loop *accept_loop, |
| 2348 |
struct aws_socket_listener_options options) { |
| 2349 |
(void)socket; |
| 2350 |
(void)accept_loop; |
| 2351 |
(void)options; |
| 2352 |
return aws_raise_error(AWS_IO_SOCKET_ILLEGAL_OPERATION_FOR_STATE); |
| 2353 |
} |
| 2354 |
|
| 2355 |
static int s_dgram_stop_accept(struct aws_socket *socket) { |
| 2356 |
(void)socket; |
| 2357 |
return aws_raise_error(AWS_IO_SOCKET_ILLEGAL_OPERATION_FOR_STATE); |
| 2358 |
} |
| 2359 |
|
| 2360 |
static int s_socket_set_options(struct aws_socket *socket, const struct aws_socket_options *options) { |
| 2361 |
if (socket->options.domain != options->domain || socket->options.type != options->type) { |
| 2362 |
return aws_raise_error(AWS_IO_SOCKET_INVALID_OPTIONS); |
| 2363 |
} |
| 2364 |
|
| 2365 |
AWS_LOGF_DEBUG( |
| 2366 |
AWS_LS_IO_SOCKET, |
| 2367 |
"id=%p handle=%p: setting socket options to: keep-alive %d, keep idle %d, keep-alive interval %d, max failed " |
| 2368 |
"probe count %d", |
| 2369 |
(void *)socket, |
| 2370 |
(void *)socket->io_handle.data.handle, |
| 2371 |
(int)options->keepalive, |
| 2372 |
(int)options->keep_alive_timeout_sec, |
| 2373 |
(int)options->keep_alive_interval_sec, |
| 2374 |
(int)options->keep_alive_max_failed_probes); |
| 2375 |
|
| 2376 |
socket->options = *options; |
| 2377 |
|
| 2378 |
if (socket->options.domain != AWS_SOCKET_LOCAL && socket->options.type == AWS_SOCKET_STREAM) { |
| 2379 |
if (socket->options.keepalive && |
| 2380 |
!(socket->options.keep_alive_interval_sec && socket->options.keep_alive_timeout_sec)) { |
| 2381 |
int keep_alive = 1; |
| 2382 |
if (setsockopt( |
| 2383 |
(SOCKET)socket->io_handle.data.handle, |
| 2384 |
SOL_SOCKET, |
| 2385 |
SO_KEEPALIVE, |
| 2386 |
(char *)&keep_alive, |
| 2387 |
sizeof(int))) { |
| 2388 |
int wsa_err = WSAGetLastError(); |
| 2389 |
AWS_LOGF_WARN( |
| 2390 |
AWS_LS_IO_SOCKET, |
| 2391 |
"id=%p handle=%p: setsockopt() call for enabling keep-alive failed with WSAError %d", |
| 2392 |
(void *)socket, |
| 2393 |
(void *)socket->io_handle.data.handle, |
| 2394 |
wsa_err); |
| 2395 |
} |
| 2396 |
} else if (socket->options.keepalive) { |
| 2397 |
ULONG keep_alive_timeout = (ULONG)aws_timestamp_convert( |
| 2398 |
socket->options.keep_alive_timeout_sec, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_MILLIS, NULL); |
| 2399 |
ULONG keep_alive_interval = (ULONG)aws_timestamp_convert( |
| 2400 |
socket->options.keep_alive_interval_sec, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_MILLIS, NULL); |
| 2401 |
struct tcp_keepalive keepalive_args = { |
| 2402 |
.onoff = 1, |
| 2403 |
.keepalivetime = keep_alive_timeout, |
| 2404 |
.keepaliveinterval = keep_alive_interval, |
| 2405 |
}; |
| 2406 |
DWORD bytes_returned = 0; |
| 2407 |
if (WSAIoctl( |
| 2408 |
(SOCKET)socket->io_handle.data.handle, |
| 2409 |
SIO_KEEPALIVE_VALS, |
| 2410 |
&keepalive_args, |
| 2411 |
sizeof(keepalive_args), |
| 2412 |
NULL, |
| 2413 |
0, |
| 2414 |
&bytes_returned, |
| 2415 |
NULL, |
| 2416 |
NULL)) { |
| 2417 |
int wsa_err = WSAGetLastError(); |
| 2418 |
AWS_LOGF_WARN( |
| 2419 |
AWS_LS_IO_SOCKET, |
| 2420 |
"id=%p handle=%p: WSAIoctl() call for setting keep-alive values failed with WSAError %d", |
| 2421 |
(void *)socket, |
| 2422 |
(void *)socket->io_handle.data.handle, |
| 2423 |
wsa_err); |
| 2424 |
} |
| 2425 |
} |
| 2426 |
|
| 2427 |
|
| 2428 |
#ifdef TCP_KEEPCNT |
| 2429 |
if (socket->options.keep_alive_max_failed_probes) { |
| 2430 |
DWORD max_probes = socket->options.keep_alive_max_failed_probes; |
| 2431 |
if (setsockopt( |
| 2432 |
(SOCKET)socket->io_handle.data.handle, |
| 2433 |
IPPROTO_TCP, |
| 2434 |
TCP_KEEPCNT, |
| 2435 |
(char *)&max_probes, |
| 2436 |
sizeof(max_probes))) { |
| 2437 |
int wsa_err = WSAGetLastError(); |
| 2438 |
AWS_LOGF_WARN( |
| 2439 |
AWS_LS_IO_SOCKET, |
| 2440 |
"id=%p handle=%p: setsockopt() call for setting keep-alive probe count value failed with WSAError " |
| 2441 |
"%d. This likely" |
| 2442 |
" isn't a problem. It's more likely you're on an old version of windows. This feature was added in " |
| 2443 |
"Windows 10 1703", |
| 2444 |
(void *)socket, |
| 2445 |
(void *)socket->io_handle.data.handle, |
| 2446 |
wsa_err); |
| 2447 |
} |
| 2448 |
} |
| 2449 |
#endif |
| 2450 |
} |
| 2451 |
|
| 2452 |
size_t network_interface_length = 0; |
| 2453 |
if (aws_secure_strlen(options->network_interface_name, AWS_NETWORK_INTERFACE_NAME_MAX, &network_interface_length)) { |
| 2454 |
AWS_LOGF_ERROR( |
| 2455 |
AWS_LS_IO_SOCKET, |
| 2456 |
"id=%p fd=%d: network_interface_name max length must be less or equal than %d bytes including NULL " |
| 2457 |
"terminated", |
| 2458 |
(void *)socket, |
| 2459 |
socket->io_handle.data.fd, |
| 2460 |
AWS_NETWORK_INTERFACE_NAME_MAX); |
| 2461 |
return aws_raise_error(AWS_IO_SOCKET_INVALID_OPTIONS); |
| 2462 |
} |
| 2463 |
if (network_interface_length != 0) { |
| 2464 |
AWS_LOGF_ERROR( |
| 2465 |
AWS_LS_IO_SOCKET, |
| 2466 |
"id=%p fd=%d: network_interface_name is not supported on this platform.", |
| 2467 |
(void *)socket, |
| 2468 |
socket->io_handle.data.fd); |
| 2469 |
return aws_raise_error(AWS_ERROR_PLATFORM_NOT_SUPPORTED); |
| 2470 |
} |
| 2471 |
|
| 2472 |
return AWS_OP_SUCCESS; |
| 2473 |
} |
| 2474 |
|
| 2475 |
struct close_args { |
| 2476 |
struct aws_mutex mutex; |
| 2477 |
struct aws_condition_variable condition_var; |
| 2478 |
struct aws_socket *socket; |
| 2479 |
bool invoked; |
| 2480 |
int ret_code; |
| 2481 |
}; |
| 2482 |
|
| 2483 |
static bool s_close_predicate(void *arg) { |
| 2484 |
struct close_args *close_args = arg; |
| 2485 |
return close_args->invoked; |
| 2486 |
} |
| 2487 |
|
| 2488 |
static void s_close_task(struct aws_task *task, void *arg, enum aws_task_status status) { |
| 2489 |
(void)task; |
| 2490 |
|
| 2491 |
struct close_args *close_args = arg; |
| 2492 |
aws_mutex_lock(&close_args->mutex); |
| 2493 |
close_args->ret_code = AWS_OP_SUCCESS; |
| 2494 |
|
| 2495 |
|
| 2496 |
|
| 2497 |
if (status == AWS_TASK_STATUS_CANCELED) { |
| 2498 |
struct iocp_socket *iocp_socket = close_args->socket->impl; |
| 2499 |
iocp_socket->read_io_data->in_use = false; |
| 2500 |
} |
| 2501 |
|
| 2502 |
if (aws_socket_close(close_args->socket)) { |
| 2503 |
close_args->ret_code = aws_last_error(); |
| 2504 |
} |
| 2505 |
close_args->invoked = true; |
| 2506 |
aws_condition_variable_notify_one(&close_args->condition_var); |
| 2507 |
aws_mutex_unlock(&close_args->mutex); |
| 2508 |
} |
| 2509 |
|
| 2510 |
static int s_wait_on_close(struct aws_socket *socket) { |
| 2511 |
AWS_ASSERT(socket->event_loop); |
| 2512 |
|
| 2513 |
|
| 2514 |
|
| 2515 |
|
| 2516 |
if (socket->state != LISTENING) { |
| 2517 |
return aws_raise_error(AWS_IO_SOCKET_ILLEGAL_OPERATION_FOR_STATE); |
| 2518 |
} |
| 2519 |
|
| 2520 |
void *handle_for_logging = socket->io_handle.data.handle; |
| 2521 |
(void)handle_for_logging; |
| 2522 |
|
| 2523 |
AWS_LOGF_INFO( |
| 2524 |
AWS_LS_IO_SOCKET, |
| 2525 |
"id=%p handle=%p: closing from a different thread than " |
| 2526 |
"the socket is running from. Blocking until it closes down.", |
| 2527 |
(void *)socket, |
| 2528 |
handle_for_logging); |
| 2529 |
|
| 2530 |
struct close_args args = { |
| 2531 |
.mutex = AWS_MUTEX_INIT, |
| 2532 |
.condition_var = AWS_CONDITION_VARIABLE_INIT, |
| 2533 |
.socket = socket, |
| 2534 |
.ret_code = AWS_OP_SUCCESS, |
| 2535 |
}; |
| 2536 |
|
| 2537 |
struct aws_task close_task = { |
| 2538 |
.fn = s_close_task, |
| 2539 |
.arg = &args, |
| 2540 |
}; |
| 2541 |
|
| 2542 |
aws_mutex_lock(&args.mutex); |
| 2543 |
aws_event_loop_schedule_task_now(socket->event_loop, &close_task); |
| 2544 |
aws_condition_variable_wait_pred(&args.condition_var, &args.mutex, s_close_predicate, &args); |
| 2545 |
aws_mutex_unlock(&args.mutex); |
| 2546 |
AWS_LOGF_INFO(AWS_LS_IO_SOCKET, "id=%p handle=%p: close task completed.", (void *)socket, handle_for_logging); |
| 2547 |
|
| 2548 |
if (args.ret_code) { |
| 2549 |
return aws_raise_error(args.ret_code); |
| 2550 |
} |
| 2551 |
|
| 2552 |
return AWS_OP_SUCCESS; |
| 2553 |
} |
| 2554 |
|
| 2555 |
static int s_protocol_socket_close(struct aws_socket *socket) { |
| 2556 |
struct iocp_socket *socket_impl = socket->impl; |
| 2557 |
AWS_LOGF_DEBUG(AWS_LS_IO_SOCKET, "id=%p handle=%p: closing", (void *)socket, (void *)socket->io_handle.data.handle); |
| 2558 |
|
| 2559 |
if (socket->event_loop) { |
| 2560 |
if (!aws_event_loop_thread_is_callers_thread(socket->event_loop)) { |
| 2561 |
return s_wait_on_close(socket); |
| 2562 |
} |
| 2563 |
|
| 2564 |
if (socket->state & LISTENING && !socket_impl->stop_accept) { |
| 2565 |
aws_socket_stop_accept(socket); |
| 2566 |
} |
| 2567 |
} |
| 2568 |
|
| 2569 |
if (socket_impl->connect_args) { |
| 2570 |
socket_impl->connect_args->socket = NULL; |
| 2571 |
socket_impl->connect_args = NULL; |
| 2572 |
} |
| 2573 |
|
| 2574 |
if (socket_impl->read_io_data && socket_impl->read_io_data->in_use) { |
| 2575 |
socket_impl->read_io_data->socket = NULL; |
| 2576 |
socket_impl->read_io_data = NULL; |
| 2577 |
} else if (socket_impl->read_io_data) { |
| 2578 |
aws_mem_release(socket->allocator, socket_impl->read_io_data); |
| 2579 |
socket_impl->read_io_data = NULL; |
| 2580 |
} |
| 2581 |
|
| 2582 |
if (socket->io_handle.data.handle != INVALID_HANDLE_VALUE) { |
| 2583 |
shutdown((SOCKET)socket->io_handle.data.handle, SD_BOTH); |
| 2584 |
closesocket((SOCKET)socket->io_handle.data.handle); |
| 2585 |
socket->io_handle.data.handle = INVALID_HANDLE_VALUE; |
| 2586 |
} |
| 2587 |
|
| 2588 |
socket->state = CLOSED; |
| 2589 |
|
| 2590 |
while (!aws_linked_list_empty(&socket_impl->pending_io_operations)) { |
| 2591 |
struct aws_linked_list_node *node = aws_linked_list_front(&socket_impl->pending_io_operations); |
| 2592 |
struct io_operation_data *op_data = AWS_CONTAINER_OF(node, struct io_operation_data, node); |
| 2593 |
op_data->socket = NULL; |
| 2594 |
aws_linked_list_pop_front(&socket_impl->pending_io_operations); |
| 2595 |
} |
| 2596 |
|
| 2597 |
socket->event_loop = NULL; |
| 2598 |
|
| 2599 |
return AWS_OP_SUCCESS; |
| 2600 |
} |
| 2601 |
|
| 2602 |
static int s_local_close(struct aws_socket *socket) { |
| 2603 |
struct iocp_socket *socket_impl = socket->impl; |
| 2604 |
AWS_LOGF_DEBUG(AWS_LS_IO_SOCKET, "id=%p handle=%p: closing", (void *)socket, (void *)socket->io_handle.data.handle); |
| 2605 |
|
| 2606 |
if (socket->event_loop) { |
| 2607 |
if (!aws_event_loop_thread_is_callers_thread(socket->event_loop)) { |
| 2608 |
return s_wait_on_close(socket); |
| 2609 |
} |
| 2610 |
} |
| 2611 |
|
| 2612 |
if (socket_impl->connect_args) { |
| 2613 |
socket_impl->connect_args->socket = NULL; |
| 2614 |
socket_impl->connect_args = NULL; |
| 2615 |
} |
| 2616 |
|
| 2617 |
if (socket_impl->read_io_data && socket_impl->read_io_data->in_use) { |
| 2618 |
socket_impl->read_io_data->socket = NULL; |
| 2619 |
socket_impl->read_io_data = NULL; |
| 2620 |
} else if (socket_impl->read_io_data) { |
| 2621 |
aws_mem_release(socket->allocator, socket_impl->read_io_data); |
| 2622 |
socket_impl->read_io_data = NULL; |
| 2623 |
} |
| 2624 |
|
| 2625 |
if (socket->io_handle.data.handle != INVALID_HANDLE_VALUE) { |
| 2626 |
CloseHandle(socket->io_handle.data.handle); |
| 2627 |
socket->io_handle.data.handle = INVALID_HANDLE_VALUE; |
| 2628 |
} |
| 2629 |
|
| 2630 |
socket->state = CLOSED; |
| 2631 |
|
| 2632 |
while (!aws_linked_list_empty(&socket_impl->pending_io_operations)) { |
| 2633 |
struct aws_linked_list_node *node = aws_linked_list_pop_front(&socket_impl->pending_io_operations); |
| 2634 |
struct io_operation_data *op_data = AWS_CONTAINER_OF(node, struct io_operation_data, node); |
| 2635 |
|
| 2636 |
op_data->socket = NULL; |
| 2637 |
} |
| 2638 |
|
| 2639 |
return AWS_OP_SUCCESS; |
| 2640 |
} |
| 2641 |
|
| 2642 |
int aws_socket_half_close(struct aws_socket *socket, enum aws_channel_direction dir) { |
| 2643 |
int how = dir == AWS_CHANNEL_DIR_READ ? 0 : 1; |
| 2644 |
|
| 2645 |
struct iocp_socket *socket_impl = socket->impl; |
| 2646 |
AWS_LOGF_DEBUG( |
| 2647 |
AWS_LS_IO_SOCKET, |
| 2648 |
"id=%p handle=%p: shutting down in direction %d", |
| 2649 |
(void *)socket, |
| 2650 |
(void *)socket->io_handle.data.handle, |
| 2651 |
dir); |
| 2652 |
if (shutdown((SOCKET)socket->io_handle.data.handle, how)) { |
| 2653 |
int error = WSAGetLastError(); |
| 2654 |
int aws_error = s_determine_socket_error(error); |
| 2655 |
aws_raise_error(aws_error); |
| 2656 |
socket_impl->winsock_vtable->connection_error(socket, aws_error); |
| 2657 |
return AWS_OP_ERR; |
| 2658 |
} |
| 2659 |
|
| 2660 |
return AWS_OP_SUCCESS; |
| 2661 |
} |
| 2662 |
|
| 2663 |
struct aws_io_handle *aws_socket_get_io_handle(struct aws_socket *socket) { |
| 2664 |
return &socket->io_handle; |
| 2665 |
} |
| 2666 |
|
| 2667 |
static int s_socket_assign_to_event_loop(struct aws_socket *socket, struct aws_event_loop *event_loop) { |
| 2668 |
if (socket->event_loop) { |
| 2669 |
return aws_raise_error(AWS_IO_EVENT_LOOP_ALREADY_ASSIGNED); |
| 2670 |
} |
| 2671 |
|
| 2672 |
socket->event_loop = event_loop; |
| 2673 |
return aws_event_loop_connect_handle_to_io_completion_port(event_loop, &socket->io_handle); |
| 2674 |
} |
| 2675 |
|
| 2676 |
struct read_cb_args { |
| 2677 |
struct aws_socket *socket; |
| 2678 |
aws_socket_on_readable_fn *user_callback; |
| 2679 |
void *user_data; |
| 2680 |
}; |
| 2681 |
|
| 2682 |
|
| 2683 |
static void s_stream_readable_event( |
| 2684 |
struct aws_event_loop *event_loop, |
| 2685 |
struct aws_overlapped *overlapped, |
| 2686 |
int status_code, |
| 2687 |
size_t num_bytes_transferred) { |
| 2688 |
(void)num_bytes_transferred; |
| 2689 |
(void)event_loop; |
| 2690 |
|
| 2691 |
struct io_operation_data *operation_data = AWS_CONTAINER_OF(overlapped, struct io_operation_data, signal); |
| 2692 |
struct aws_socket *socket = overlapped->user_data; |
| 2693 |
|
| 2694 |
if (!operation_data->socket) { |
| 2695 |
aws_mem_release(operation_data->allocator, operation_data); |
| 2696 |
return; |
| 2697 |
} |
| 2698 |
|
| 2699 |
if (status_code == WSA_OPERATION_ABORTED || status_code == IO_OPERATION_CANCELLED) { |
| 2700 |
return; |
| 2701 |
} |
| 2702 |
|
| 2703 |
AWS_LOGF_TRACE( |
| 2704 |
AWS_LS_IO_SOCKET, |
| 2705 |
"id=%p handle=%p: socket readable event triggered", |
| 2706 |
(void *)socket, |
| 2707 |
(void *)socket->io_handle.data.handle); |
| 2708 |
struct iocp_socket *socket_impl = socket->impl; |
| 2709 |
socket->state = socket->state & ~CONNECTED_WAITING_ON_READABLE; |
| 2710 |
|
| 2711 |
int err_code = AWS_OP_SUCCESS; |
| 2712 |
if (status_code && status_code != ERROR_IO_PENDING) { |
| 2713 |
AWS_LOGF_ERROR( |
| 2714 |
AWS_LS_IO_SOCKET, |
| 2715 |
"id=%p handle=%p: socket status error %d", |
| 2716 |
(void *)socket, |
| 2717 |
(void *)socket->io_handle.data.handle, |
| 2718 |
status_code); |
| 2719 |
err_code = s_determine_socket_error(status_code); |
| 2720 |
if (err_code == AWS_IO_SOCKET_CLOSED) { |
| 2721 |
socket->state = CLOSED; |
| 2722 |
} else { |
| 2723 |
socket->state = ERRORED; |
| 2724 |
} |
| 2725 |
} |
| 2726 |
|
| 2727 |
socket->readable_fn(socket, err_code, socket->readable_user_data); |
| 2728 |
|
| 2729 |
if (operation_data->socket && socket_impl->read_io_data) { |
| 2730 |
|
| 2731 |
|
| 2732 |
|
| 2733 |
if (!(socket->state & CONNECTED_WAITING_ON_READABLE)) { |
| 2734 |
socket_impl->read_io_data->in_use = false; |
| 2735 |
} |
| 2736 |
} |
| 2737 |
|
| 2738 |
if (!operation_data->socket) { |
| 2739 |
aws_mem_release(operation_data->allocator, operation_data); |
| 2740 |
return; |
| 2741 |
} |
| 2742 |
} |
| 2743 |
|
| 2744 |
|
| 2745 |
static void s_dgram_readable_event( |
| 2746 |
struct aws_event_loop *event_loop, |
| 2747 |
struct aws_overlapped *overlapped, |
| 2748 |
int status_code, |
| 2749 |
size_t num_bytes_transferred) { |
| 2750 |
(void)num_bytes_transferred; |
| 2751 |
(void)event_loop; |
| 2752 |
|
| 2753 |
struct io_operation_data *operation_data = AWS_CONTAINER_OF(overlapped, struct io_operation_data, signal); |
| 2754 |
struct aws_socket *socket = overlapped->user_data; |
| 2755 |
|
| 2756 |
if (!operation_data->socket) { |
| 2757 |
aws_mem_release(operation_data->allocator, operation_data); |
| 2758 |
return; |
| 2759 |
} |
| 2760 |
|
| 2761 |
if (status_code == WSA_OPERATION_ABORTED || status_code == IO_OPERATION_CANCELLED) { |
| 2762 |
return; |
| 2763 |
} |
| 2764 |
|
| 2765 |
AWS_LOGF_TRACE( |
| 2766 |
AWS_LS_IO_SOCKET, |
| 2767 |
"id=%p handle=%p: socket readable event triggered", |
| 2768 |
(void *)socket, |
| 2769 |
(void *)socket->io_handle.data.handle); |
| 2770 |
struct iocp_socket *socket_impl = socket->impl; |
| 2771 |
socket->state = socket->state & ~CONNECTED_WAITING_ON_READABLE; |
| 2772 |
|
| 2773 |
int err_code = AWS_OP_SUCCESS; |
| 2774 |
|
| 2775 |
|
| 2776 |
if (status_code != ERROR_IO_PENDING && status_code != IO_STATUS_BUFFER_OVERFLOW) { |
| 2777 |
AWS_LOGF_ERROR( |
| 2778 |
AWS_LS_IO_SOCKET, |
| 2779 |
"id=%p handle=%p: socket status error %d", |
| 2780 |
(void *)socket, |
| 2781 |
(void *)socket->io_handle.data.handle, |
| 2782 |
status_code); |
| 2783 |
err_code = s_determine_socket_error(status_code); |
| 2784 |
if (err_code == AWS_IO_SOCKET_CLOSED) { |
| 2785 |
socket->state = CLOSED; |
| 2786 |
} else { |
| 2787 |
socket->state = ERRORED; |
| 2788 |
} |
| 2789 |
} |
| 2790 |
|
| 2791 |
socket->readable_fn(socket, err_code, socket->readable_user_data); |
| 2792 |
|
| 2793 |
if (operation_data->socket && socket_impl->read_io_data) { |
| 2794 |
|
| 2795 |
|
| 2796 |
|
| 2797 |
if (!(socket->state & CONNECTED_WAITING_ON_READABLE)) { |
| 2798 |
socket_impl->read_io_data->in_use = false; |
| 2799 |
} |
| 2800 |
} |
| 2801 |
|
| 2802 |
if (!operation_data->socket) { |
| 2803 |
aws_mem_release(operation_data->allocator, operation_data); |
| 2804 |
return; |
| 2805 |
} |
| 2806 |
} |
| 2807 |
|
| 2808 |
static int s_stream_subscribe_to_read( |
| 2809 |
struct aws_socket *socket, |
| 2810 |
aws_socket_on_readable_fn *on_readable, |
| 2811 |
void *user_data) { |
| 2812 |
socket->readable_fn = on_readable; |
| 2813 |
socket->readable_user_data = user_data; |
| 2814 |
|
| 2815 |
AWS_LOGF_TRACE( |
| 2816 |
AWS_LS_IO_SOCKET, |
| 2817 |
"id=%p handle=%p: subscribing to readable event", |
| 2818 |
(void *)socket, |
| 2819 |
(void *)socket->io_handle.data.handle); |
| 2820 |
|
| 2821 |
struct iocp_socket *iocp_socket = socket->impl; |
| 2822 |
iocp_socket->read_io_data->in_use = true; |
| 2823 |
aws_overlapped_init(&iocp_socket->read_io_data->signal, s_stream_readable_event, socket); |
| 2824 |
|
| 2825 |
int fake_buffer = 0; |
| 2826 |
socket->state |= CONNECTED_WAITING_ON_READABLE; |
| 2827 |
BOOL success = ReadFile( |
| 2828 |
socket->io_handle.data.handle, |
| 2829 |
&fake_buffer, |
| 2830 |
0, |
| 2831 |
NULL, |
| 2832 |
aws_overlapped_to_windows_overlapped(&iocp_socket->read_io_data->signal)); |
| 2833 |
if (!success) { |
| 2834 |
int win_err = GetLastError(); |
| 2835 |
if (win_err != ERROR_IO_PENDING) { |
| 2836 |
AWS_LOGF_ERROR( |
| 2837 |
AWS_LS_IO_SOCKET, |
| 2838 |
"id=%p handle=%p: socket ReadFile() failed with error %d", |
| 2839 |
(void *)socket, |
| 2840 |
(void *)socket->io_handle.data.handle, |
| 2841 |
win_err); |
| 2842 |
iocp_socket->read_io_data->in_use = false; |
| 2843 |
socket->state &= ~CONNECTED_WAITING_ON_READABLE; |
| 2844 |
|
| 2845 |
int aws_error = s_determine_socket_error(win_err); |
| 2846 |
if (aws_error == AWS_IO_SOCKET_CLOSED) { |
| 2847 |
socket->state = CLOSED; |
| 2848 |
} else { |
| 2849 |
socket->state = ERRORED; |
| 2850 |
} |
| 2851 |
return aws_raise_error(aws_error); |
| 2852 |
} |
| 2853 |
} |
| 2854 |
return AWS_OP_SUCCESS; |
| 2855 |
} |
| 2856 |
|
| 2857 |
static int s_dgram_subscribe_to_read( |
| 2858 |
struct aws_socket *socket, |
| 2859 |
aws_socket_on_readable_fn *on_readable, |
| 2860 |
void *user_data) { |
| 2861 |
AWS_LOGF_TRACE( |
| 2862 |
AWS_LS_IO_SOCKET, |
| 2863 |
"id=%p handle=%p: subscribing to readable event", |
| 2864 |
(void *)socket, |
| 2865 |
(void *)socket->io_handle.data.handle); |
| 2866 |
socket->readable_fn = on_readable; |
| 2867 |
socket->readable_user_data = user_data; |
| 2868 |
|
| 2869 |
struct iocp_socket *iocp_socket = socket->impl; |
| 2870 |
iocp_socket->read_io_data->in_use = true; |
| 2871 |
aws_overlapped_init(&iocp_socket->read_io_data->signal, s_dgram_readable_event, socket); |
| 2872 |
|
| 2873 |
socket->state |= CONNECTED_WAITING_ON_READABLE; |
| 2874 |
|
| 2875 |
|
| 2876 |
|
| 2877 |
WSABUF buf = { |
| 2878 |
.len = 0, |
| 2879 |
.buf = NULL, |
| 2880 |
}; |
| 2881 |
DWORD flags = MSG_PEEK; |
| 2882 |
int err = WSARecv( |
| 2883 |
(SOCKET)socket->io_handle.data.handle, |
| 2884 |
&buf, |
| 2885 |
1, |
| 2886 |
NULL, |
| 2887 |
&flags, |
| 2888 |
aws_overlapped_to_windows_overlapped(&iocp_socket->read_io_data->signal), |
| 2889 |
NULL); |
| 2890 |
|
| 2891 |
if (err) { |
| 2892 |
int wsa_err = WSAGetLastError(); |
| 2893 |
if (wsa_err != ERROR_IO_PENDING) { |
| 2894 |
AWS_LOGF_ERROR( |
| 2895 |
AWS_LS_IO_SOCKET, |
| 2896 |
"id=%p handle=%p: socket WSARecv() failed with error %d", |
| 2897 |
(void *)socket, |
| 2898 |
(void *)socket->io_handle.data.handle, |
| 2899 |
wsa_err); |
| 2900 |
iocp_socket->read_io_data->in_use = false; |
| 2901 |
int aws_error = s_determine_socket_error(wsa_err); |
| 2902 |
if (aws_error == AWS_IO_SOCKET_CLOSED) { |
| 2903 |
socket->state = CLOSED; |
| 2904 |
} else { |
| 2905 |
socket->state = ERRORED; |
| 2906 |
} |
| 2907 |
return aws_raise_error(aws_error); |
| 2908 |
} |
| 2909 |
} |
| 2910 |
return AWS_OP_SUCCESS; |
| 2911 |
} |
| 2912 |
|
| 2913 |
static int s_local_read(struct aws_socket *socket, struct aws_byte_buf *buffer, size_t *amount_read) { |
| 2914 |
AWS_LOGF_TRACE( |
| 2915 |
AWS_LS_IO_SOCKET, |
| 2916 |
"id=%p handle=%p: reading from named pipe", |
| 2917 |
(void *)socket, |
| 2918 |
(void *)socket->io_handle.data.handle); |
| 2919 |
|
| 2920 |
DWORD bytes_available = 0; |
| 2921 |
BOOL peek_success = PeekNamedPipe(socket->io_handle.data.handle, NULL, 0, NULL, &bytes_available, NULL); |
| 2922 |
|
| 2923 |
if (!peek_success) { |
| 2924 |
int error_code = GetLastError(); |
| 2925 |
AWS_LOGF_ERROR( |
| 2926 |
AWS_LS_IO_SOCKET, |
| 2927 |
"id=%p handle=%p: PeekNamedPipe() failed with error %d", |
| 2928 |
(void *)socket, |
| 2929 |
(void *)socket->io_handle.data.handle, |
| 2930 |
error_code); |
| 2931 |
return aws_raise_error(s_determine_socket_error(error_code)); |
| 2932 |
} |
| 2933 |
|
| 2934 |
if (!bytes_available) { |
| 2935 |
if (!(socket->state & CONNECTED_WAITING_ON_READABLE)) { |
| 2936 |
struct iocp_socket *iocp_socket = socket->impl; |
| 2937 |
socket->state |= CONNECTED_WAITING_ON_READABLE; |
| 2938 |
iocp_socket->read_io_data->in_use = true; |
| 2939 |
aws_overlapped_init(&iocp_socket->read_io_data->signal, s_stream_readable_event, socket); |
| 2940 |
int fake_buffer = 0; |
| 2941 |
BOOL success = ReadFile( |
| 2942 |
socket->io_handle.data.handle, |
| 2943 |
&fake_buffer, |
| 2944 |
0, |
| 2945 |
NULL, |
| 2946 |
aws_overlapped_to_windows_overlapped(&iocp_socket->read_io_data->signal)); |
| 2947 |
if (!success) { |
| 2948 |
int win_err = GetLastError(); |
| 2949 |
if (win_err != ERROR_IO_PENDING) { |
| 2950 |
AWS_LOGF_ERROR( |
| 2951 |
AWS_LS_IO_SOCKET, |
| 2952 |
"id=%p handle=%p: ReadFile() failed with error %d", |
| 2953 |
(void *)socket, |
| 2954 |
(void *)socket->io_handle.data.handle, |
| 2955 |
win_err); |
| 2956 |
iocp_socket->read_io_data->in_use = false; |
| 2957 |
int aws_error = s_determine_socket_error(win_err); |
| 2958 |
if (aws_error == AWS_IO_SOCKET_CLOSED) { |
| 2959 |
socket->state = CLOSED; |
| 2960 |
} else { |
| 2961 |
socket->state = ERRORED; |
| 2962 |
} |
| 2963 |
return aws_raise_error(aws_error); |
| 2964 |
} |
| 2965 |
} |
| 2966 |
} |
| 2967 |
|
| 2968 |
AWS_LOGF_TRACE( |
| 2969 |
AWS_LS_IO_SOCKET, |
| 2970 |
"id=%p handle=%p: read would block, returning", |
| 2971 |
(void *)socket, |
| 2972 |
(void *)socket->io_handle.data.handle); |
| 2973 |
return aws_raise_error(AWS_IO_READ_WOULD_BLOCK); |
| 2974 |
} |
| 2975 |
|
| 2976 |
AWS_LOGF_TRACE( |
| 2977 |
AWS_LS_IO_SOCKET, |
| 2978 |
"id=%p handle=%p: %u bytes available for read.", |
| 2979 |
(void *)socket, |
| 2980 |
(void *)socket->io_handle.data.handle, |
| 2981 |
bytes_available); |
| 2982 |
DWORD bytes_read = 0; |
| 2983 |
size_t read_capacity = buffer->capacity - buffer->len; |
| 2984 |
DWORD bytes_to_read = (DWORD)(bytes_available > read_capacity ? read_capacity : bytes_available); |
| 2985 |
BOOL read_success = |
| 2986 |
ReadFile(socket->io_handle.data.handle, buffer->buffer + buffer->len, bytes_to_read, &bytes_read, NULL); |
| 2987 |
|
| 2988 |
if (!read_success) { |
| 2989 |
int error_code = GetLastError(); |
| 2990 |
int aws_error = s_determine_socket_error(error_code); |
| 2991 |
if (aws_error == AWS_IO_SOCKET_CLOSED) { |
| 2992 |
socket->state = CLOSED; |
| 2993 |
} else { |
| 2994 |
socket->state = ERRORED; |
| 2995 |
} |
| 2996 |
AWS_LOGF_ERROR( |
| 2997 |
AWS_LS_IO_SOCKET, |
| 2998 |
"id=%p handle=%p: socket ReadFile() failed with error %d", |
| 2999 |
(void *)socket, |
| 3000 |
(void *)socket->io_handle.data.handle, |
| 3001 |
error_code); |
| 3002 |
return aws_raise_error(aws_error); |
| 3003 |
} |
| 3004 |
|
| 3005 |
*amount_read = bytes_read; |
| 3006 |
AWS_LOGF_TRACE( |
| 3007 |
AWS_LS_IO_SOCKET, |
| 3008 |
"id=%p handle=%p: successfully read %u bytes.", |
| 3009 |
(void *)socket, |
| 3010 |
(void *)socket->io_handle.data.handle, |
| 3011 |
bytes_read); |
| 3012 |
buffer->len += bytes_read; |
| 3013 |
return AWS_OP_SUCCESS; |
| 3014 |
} |
| 3015 |
|
| 3016 |
static int s_tcp_read(struct aws_socket *socket, struct aws_byte_buf *buffer, size_t *amount_read) { |
| 3017 |
AWS_LOGF_TRACE( |
| 3018 |
AWS_LS_IO_SOCKET, |
| 3019 |
"id=%p handle=%p: reading from socket", |
| 3020 |
(void *)socket, |
| 3021 |
(void *)socket->io_handle.data.handle); |
| 3022 |
|
| 3023 |
int read_val = recv( |
| 3024 |
(SOCKET)socket->io_handle.data.handle, |
| 3025 |
(char *)buffer->buffer + buffer->len, |
| 3026 |
(int)(buffer->capacity - buffer->len), |
| 3027 |
0); |
| 3028 |
|
| 3029 |
if (read_val > 0) { |
| 3030 |
AWS_LOGF_TRACE( |
| 3031 |
AWS_LS_IO_SOCKET, |
| 3032 |
"id=%p handle=%p: read %d bytes from socket", |
| 3033 |
(void *)socket, |
| 3034 |
(void *)socket->io_handle.data.handle, |
| 3035 |
read_val); |
| 3036 |
*amount_read = (size_t)read_val; |
| 3037 |
buffer->len += *amount_read; |
| 3038 |
return AWS_OP_SUCCESS; |
| 3039 |
} |
| 3040 |
|
| 3041 |
if (read_val == 0) { |
| 3042 |
AWS_LOGF_DEBUG( |
| 3043 |
AWS_LS_IO_SOCKET, |
| 3044 |
"id=%p handle=%p: socket closed gracefully", |
| 3045 |
(void *)socket, |
| 3046 |
(void *)socket->io_handle.data.handle); |
| 3047 |
socket->state = CLOSED; |
| 3048 |
return aws_raise_error(AWS_IO_SOCKET_CLOSED); |
| 3049 |
} |
| 3050 |
|
| 3051 |
int error = WSAGetLastError(); |
| 3052 |
|
| 3053 |
if (error == WSAEWOULDBLOCK) { |
| 3054 |
AWS_LOGF_TRACE( |
| 3055 |
AWS_LS_IO_SOCKET, |
| 3056 |
"id=%p handle=%p: read would block, scheduling 0 byte read and returning", |
| 3057 |
(void *)socket, |
| 3058 |
(void *)socket->io_handle.data.handle); |
| 3059 |
if (!(socket->state & CONNECTED_WAITING_ON_READABLE)) { |
| 3060 |
struct iocp_socket *iocp_socket = socket->impl; |
| 3061 |
socket->state |= CONNECTED_WAITING_ON_READABLE; |
| 3062 |
iocp_socket->read_io_data->in_use = true; |
| 3063 |
aws_overlapped_init(&iocp_socket->read_io_data->signal, s_stream_readable_event, socket); |
| 3064 |
int fake_buffer = 0; |
| 3065 |
BOOL success = ReadFile( |
| 3066 |
socket->io_handle.data.handle, |
| 3067 |
&fake_buffer, |
| 3068 |
0, |
| 3069 |
NULL, |
| 3070 |
aws_overlapped_to_windows_overlapped(&iocp_socket->read_io_data->signal)); |
| 3071 |
if (!success) { |
| 3072 |
int win_err = GetLastError(); |
| 3073 |
if (win_err != ERROR_IO_PENDING) { |
| 3074 |
AWS_LOGF_ERROR( |
| 3075 |
AWS_LS_IO_SOCKET, |
| 3076 |
"id=%p handle=%p: ReadFile() for 0 byte read failed with error %d", |
| 3077 |
(void *)socket, |
| 3078 |
(void *)socket->io_handle.data.handle, |
| 3079 |
win_err); |
| 3080 |
iocp_socket->read_io_data->in_use = false; |
| 3081 |
int aws_error = s_determine_socket_error(win_err); |
| 3082 |
if (aws_error == AWS_IO_SOCKET_CLOSED) { |
| 3083 |
socket->state = CLOSED; |
| 3084 |
} else { |
| 3085 |
socket->state = ERRORED; |
| 3086 |
} |
| 3087 |
return aws_raise_error(aws_error); |
| 3088 |
} |
| 3089 |
} |
| 3090 |
} |
| 3091 |
|
| 3092 |
return aws_raise_error(AWS_IO_READ_WOULD_BLOCK); |
| 3093 |
} |
| 3094 |
|
| 3095 |
if (error == EPIPE) { |
| 3096 |
AWS_LOGF_ERROR( |
| 3097 |
AWS_LS_IO_SOCKET, "id=%p handle=%p: socket closed", (void *)socket, (void *)socket->io_handle.data.handle); |
| 3098 |
socket->state = CLOSED; |
| 3099 |
return aws_raise_error(AWS_IO_BROKEN_PIPE); |
| 3100 |
} |
| 3101 |
|
| 3102 |
AWS_LOGF_ERROR( |
| 3103 |
AWS_LS_IO_SOCKET, |
| 3104 |
"id=%p handle=%p: recv() failed with error %d", |
| 3105 |
(void *)socket, |
| 3106 |
(void *)socket->io_handle.data.handle, |
| 3107 |
error); |
| 3108 |
return aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE); |
| 3109 |
} |
| 3110 |
|
| 3111 |
static int s_dgram_read(struct aws_socket *socket, struct aws_byte_buf *buffer, size_t *amount_read) { |
| 3112 |
AWS_LOGF_TRACE( |
| 3113 |
AWS_LS_IO_SOCKET, |
| 3114 |
"id=%p handle=%p: reading from socket", |
| 3115 |
(void *)socket, |
| 3116 |
(void *)socket->io_handle.data.handle); |
| 3117 |
int read_val = recv( |
| 3118 |
(SOCKET)socket->io_handle.data.handle, |
| 3119 |
(char *)buffer->buffer + buffer->len, |
| 3120 |
(int)(buffer->capacity - buffer->len), |
| 3121 |
0); |
| 3122 |
|
| 3123 |
if (read_val > 0) { |
| 3124 |
AWS_LOGF_TRACE( |
| 3125 |
AWS_LS_IO_SOCKET, |
| 3126 |
"id=%p handle=%p: read %d bytes from socket", |
| 3127 |
(void *)socket, |
| 3128 |
(void *)socket->io_handle.data.handle, |
| 3129 |
read_val); |
| 3130 |
*amount_read = (size_t)read_val; |
| 3131 |
buffer->len += *amount_read; |
| 3132 |
return AWS_OP_SUCCESS; |
| 3133 |
} |
| 3134 |
|
| 3135 |
if (read_val == 0) { |
| 3136 |
AWS_LOGF_DEBUG( |
| 3137 |
AWS_LS_IO_SOCKET, |
| 3138 |
"id=%p handle=%p: socket closed gracefully", |
| 3139 |
(void *)socket, |
| 3140 |
(void *)socket->io_handle.data.handle); |
| 3141 |
socket->state = CLOSED; |
| 3142 |
return aws_raise_error(AWS_IO_SOCKET_CLOSED); |
| 3143 |
} |
| 3144 |
|
| 3145 |
int error = WSAGetLastError(); |
| 3146 |
|
| 3147 |
if (error == WSAEWOULDBLOCK) { |
| 3148 |
AWS_LOGF_TRACE( |
| 3149 |
AWS_LS_IO_SOCKET, |
| 3150 |
"id=%p handle=%p: read would block, scheduling 0 byte read and returning", |
| 3151 |
(void *)socket, |
| 3152 |
(void *)socket->io_handle.data.handle); |
| 3153 |
if (!(socket->state & CONNECTED_WAITING_ON_READABLE)) { |
| 3154 |
struct iocp_socket *iocp_socket = socket->impl; |
| 3155 |
socket->state |= CONNECTED_WAITING_ON_READABLE; |
| 3156 |
iocp_socket->read_io_data->in_use = true; |
| 3157 |
aws_overlapped_init(&iocp_socket->read_io_data->signal, s_stream_readable_event, socket); |
| 3158 |
|
| 3159 |
|
| 3160 |
|
| 3161 |
WSABUF buf = { |
| 3162 |
.len = 0, |
| 3163 |
.buf = NULL, |
| 3164 |
}; |
| 3165 |
|
| 3166 |
DWORD flags = MSG_PEEK; |
| 3167 |
int err = WSARecv( |
| 3168 |
(SOCKET)socket->io_handle.data.handle, |
| 3169 |
&buf, |
| 3170 |
1, |
| 3171 |
NULL, |
| 3172 |
&flags, |
| 3173 |
aws_overlapped_to_windows_overlapped(&iocp_socket->read_io_data->signal), |
| 3174 |
NULL); |
| 3175 |
if (err) { |
| 3176 |
int wsa_err = WSAGetLastError(); |
| 3177 |
if (wsa_err != ERROR_IO_PENDING) { |
| 3178 |
AWS_LOGF_ERROR( |
| 3179 |
AWS_LS_IO_SOCKET, |
| 3180 |
"id=%p handle=%p: WSARecv() for 0 byte read failed with error %d", |
| 3181 |
(void *)socket, |
| 3182 |
(void *)socket->io_handle.data.handle, |
| 3183 |
wsa_err); |
| 3184 |
iocp_socket->read_io_data->in_use = false; |
| 3185 |
int aws_error = s_determine_socket_error(wsa_err); |
| 3186 |
if (aws_error == AWS_IO_SOCKET_CLOSED) { |
| 3187 |
socket->state = CLOSED; |
| 3188 |
} else { |
| 3189 |
socket->state = ERRORED; |
| 3190 |
} |
| 3191 |
|
| 3192 |
return aws_raise_error(aws_error); |
| 3193 |
} |
| 3194 |
} |
| 3195 |
} |
| 3196 |
|
| 3197 |
return aws_raise_error(AWS_IO_READ_WOULD_BLOCK); |
| 3198 |
} |
| 3199 |
|
| 3200 |
if (error == EPIPE) { |
| 3201 |
AWS_LOGF_ERROR( |
| 3202 |
AWS_LS_IO_SOCKET, "id=%p handle=%p: socket closed", (void *)socket, (void *)socket->io_handle.data.handle); |
| 3203 |
socket->state = CLOSED; |
| 3204 |
return aws_raise_error(AWS_IO_BROKEN_PIPE); |
| 3205 |
} |
| 3206 |
|
| 3207 |
AWS_LOGF_ERROR( |
| 3208 |
AWS_LS_IO_SOCKET, |
| 3209 |
"id=%p handle=%p: recv() failed with error %d", |
| 3210 |
(void *)socket, |
| 3211 |
(void *)socket->io_handle.data.handle, |
| 3212 |
error); |
| 3213 |
return aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE); |
| 3214 |
} |
| 3215 |
|
| 3216 |
struct write_cb_args { |
| 3217 |
struct io_operation_data io_data; |
| 3218 |
size_t original_buffer_len; |
| 3219 |
aws_socket_on_write_completed_fn *user_callback; |
| 3220 |
void *user_data; |
| 3221 |
}; |
| 3222 |
|
| 3223 |
|
| 3224 |
static void s_socket_written_event( |
| 3225 |
struct aws_event_loop *event_loop, |
| 3226 |
struct aws_overlapped *overlapped, |
| 3227 |
int status_code, |
| 3228 |
size_t num_bytes_transferred) { |
| 3229 |
(void)event_loop; |
| 3230 |
(void)num_bytes_transferred; |
| 3231 |
|
| 3232 |
struct io_operation_data *operation_data = AWS_CONTAINER_OF(overlapped, struct io_operation_data, signal); |
| 3233 |
struct write_cb_args *write_cb_args = overlapped->user_data; |
| 3234 |
struct aws_socket *socket = operation_data->socket; |
| 3235 |
int aws_error_code = status_code ? s_determine_socket_error(status_code) : AWS_OP_SUCCESS; |
| 3236 |
|
| 3237 |
if (aws_error_code) { |
| 3238 |
aws_raise_error(aws_error_code); |
| 3239 |
} |
| 3240 |
|
| 3241 |
if (!socket) { |
| 3242 |
void *user_data = write_cb_args->user_data; |
| 3243 |
aws_socket_on_write_completed_fn *callback = write_cb_args->user_callback; |
| 3244 |
callback(NULL, aws_error_code, num_bytes_transferred, user_data); |
| 3245 |
aws_mem_release(operation_data->allocator, write_cb_args); |
| 3246 |
return; |
| 3247 |
} |
| 3248 |
|
| 3249 |
AWS_LOGF_TRACE( |
| 3250 |
AWS_LS_IO_SOCKET, |
| 3251 |
"id=%p handle=%p: Write Completion callback triggered", |
| 3252 |
(void *)socket, |
| 3253 |
(void *)socket->io_handle.data.handle); |
| 3254 |
|
| 3255 |
if (status_code) { |
| 3256 |
if (aws_error_code == AWS_IO_SOCKET_CLOSED) { |
| 3257 |
socket->state = CLOSED; |
| 3258 |
} else { |
| 3259 |
socket->state = ERRORED; |
| 3260 |
} |
| 3261 |
} else { |
| 3262 |
AWS_LOGF_TRACE( |
| 3263 |
AWS_LS_IO_SOCKET, |
| 3264 |
"id=%p handle=%p: Write of size %llu completed", |
| 3265 |
(void *)socket, |
| 3266 |
(void *)socket->io_handle.data.handle, |
| 3267 |
(unsigned long long)num_bytes_transferred); |
| 3268 |
AWS_ASSERT(num_bytes_transferred == write_cb_args->original_buffer_len); |
| 3269 |
} |
| 3270 |
|
| 3271 |
aws_linked_list_remove(&operation_data->node); |
| 3272 |
|
| 3273 |
void *user_data = write_cb_args->user_data; |
| 3274 |
aws_socket_on_write_completed_fn *callback = write_cb_args->user_callback; |
| 3275 |
callback(operation_data->socket, aws_error_code, num_bytes_transferred, user_data); |
| 3276 |
|
| 3277 |
aws_mem_release(operation_data->allocator, write_cb_args); |
| 3278 |
} |
| 3279 |
|
| 3280 |
static int s_socket_write( |
| 3281 |
struct aws_socket *socket, |
| 3282 |
const struct aws_byte_cursor *cursor, |
| 3283 |
aws_socket_on_write_completed_fn *written_fn, |
| 3284 |
void *user_data) { |
| 3285 |
if (!aws_event_loop_thread_is_callers_thread(socket->event_loop)) { |
| 3286 |
return aws_raise_error(AWS_ERROR_IO_EVENT_LOOP_THREAD_ONLY); |
| 3287 |
} |
| 3288 |
|
| 3289 |
if (!(socket->state & CONNECTED_WRITE)) { |
| 3290 |
AWS_LOGF_ERROR( |
| 3291 |
AWS_LS_IO_SOCKET, |
| 3292 |
"id=%p handle=%p: cannot write to because it is not connected", |
| 3293 |
(void *)socket, |
| 3294 |
(void *)socket->io_handle.data.handle); |
| 3295 |
return aws_raise_error(AWS_IO_SOCKET_NOT_CONNECTED); |
| 3296 |
} |
| 3297 |
|
| 3298 |
struct write_cb_args *write_cb_data = aws_mem_calloc(socket->allocator, 1, sizeof(struct write_cb_args)); |
| 3299 |
if (!write_cb_data) { |
| 3300 |
socket->state = ERRORED; |
| 3301 |
return AWS_OP_ERR; |
| 3302 |
} |
| 3303 |
|
| 3304 |
write_cb_data->user_callback = written_fn; |
| 3305 |
write_cb_data->user_data = user_data; |
| 3306 |
write_cb_data->original_buffer_len = cursor->len; |
| 3307 |
write_cb_data->io_data.allocator = socket->allocator; |
| 3308 |
write_cb_data->io_data.in_use = true; |
| 3309 |
write_cb_data->io_data.socket = socket; |
| 3310 |
|
| 3311 |
aws_overlapped_init(&write_cb_data->io_data.signal, s_socket_written_event, write_cb_data); |
| 3312 |
struct iocp_socket *socket_impl = socket->impl; |
| 3313 |
|
| 3314 |
aws_linked_list_push_back(&socket_impl->pending_io_operations, &write_cb_data->io_data.node); |
| 3315 |
AWS_LOGF_TRACE( |
| 3316 |
AWS_LS_IO_SOCKET, |
| 3317 |
"id=%p handle=%p: queueing write of %llu bytes", |
| 3318 |
(void *)socket, |
| 3319 |
(void *)socket->io_handle.data.handle, |
| 3320 |
(unsigned long long)cursor->len); |
| 3321 |
|
| 3322 |
BOOL res = WriteFile( |
| 3323 |
socket->io_handle.data.handle, |
| 3324 |
cursor->ptr, |
| 3325 |
(DWORD)cursor->len, |
| 3326 |
NULL, |
| 3327 |
aws_overlapped_to_windows_overlapped(&write_cb_data->io_data.signal)); |
| 3328 |
|
| 3329 |
if (!res) { |
| 3330 |
int error_code = GetLastError(); |
| 3331 |
if (error_code != ERROR_IO_PENDING) { |
| 3332 |
AWS_LOGF_ERROR( |
| 3333 |
AWS_LS_IO_SOCKET, |
| 3334 |
"id=%p handle=%p: WriteFile() failed with error %d", |
| 3335 |
(void *)socket, |
| 3336 |
(void *)socket->io_handle.data.handle, |
| 3337 |
error_code); |
| 3338 |
|
| 3339 |
aws_linked_list_remove(&write_cb_data->io_data.node); |
| 3340 |
aws_mem_release(socket->allocator, write_cb_data); |
| 3341 |
int aws_error = s_determine_socket_error(error_code); |
| 3342 |
if (aws_error == AWS_IO_SOCKET_CLOSED) { |
| 3343 |
socket->state = CLOSED; |
| 3344 |
} else { |
| 3345 |
socket->state = ERRORED; |
| 3346 |
} |
| 3347 |
return aws_raise_error(aws_error); |
| 3348 |
} |
| 3349 |
} |
| 3350 |
|
| 3351 |
return AWS_OP_SUCCESS; |
| 3352 |
} |
| 3353 |
|
| 3354 |
static int s_socket_get_error(struct aws_socket *socket) { |
| 3355 |
if (socket->options.domain != AWS_SOCKET_LOCAL) { |
| 3356 |
int connect_result; |
| 3357 |
socklen_t result_length = sizeof(connect_result); |
| 3358 |
if (getsockopt( |
| 3359 |
(SOCKET)socket->io_handle.data.handle, SOL_SOCKET, SO_ERROR, (char *)&connect_result, &result_length) < |
| 3360 |
0) { |
| 3361 |
return s_determine_socket_error(WSAGetLastError()); |
| 3362 |
} |
| 3363 |
|
| 3364 |
if (connect_result) { |
| 3365 |
return s_determine_socket_error(connect_result); |
| 3366 |
} |
| 3367 |
} else { |
| 3368 |
return s_determine_socket_error(WSAGetLastError()); |
| 3369 |
} |
| 3370 |
|
| 3371 |
return AWS_OP_SUCCESS; |
| 3372 |
} |
| 3373 |
|
| 3374 |
static bool s_socket_is_open(struct aws_socket *socket) { |
| 3375 |
return socket->io_handle.data.handle != INVALID_HANDLE_VALUE; |
| 3376 |
} |
| 3377 |
|
| 3378 |
static struct aws_byte_buf s_socket_get_protocol_fn(const struct aws_socket *socket) { |
| 3379 |
struct aws_byte_buf empty; |
| 3380 |
AWS_ZERO_STRUCT(empty); |
| 3381 |
AWS_LOGF_ERROR( |
| 3382 |
AWS_LS_IO_SOCKET, |
| 3383 |
"id=%p socket_get_protocol_fn should only be called on a socket using secitem.", |
| 3384 |
(void *)socket); |
| 3385 |
return empty; |
| 3386 |
} |
| 3387 |
|
| 3388 |
static struct aws_string *s_socket_get_server_name_fn(const struct aws_socket *socket) { |
| 3389 |
AWS_LOGF_ERROR( |
| 3390 |
AWS_LS_IO_SOCKET, |
| 3391 |
"id=%p socket_get_server_name_fn should only be called on a socket using secitem.", |
| 3392 |
(void *)socket); |
| 3393 |
return NULL; |
| 3394 |
} |
| 3395 |
|
| 3396 |
bool aws_is_network_interface_name_valid(const char *interface_name) { |
| 3397 |
(void)interface_name; |
| 3398 |
AWS_LOGF_ERROR(AWS_LS_IO_SOCKET, "network_interface_names are not supported on Windows"); |
| 3399 |
return false; |
| 3400 |
} |
| 3401 |
|
| 3402 |
int aws_parse_ipv4_address(const struct aws_string *src, uint32_t *dst) { |
| 3403 |
int result = inet_pton(AF_INET, aws_string_c_str(src), dst); |
| 3404 |
if (result != 1) { |
| 3405 |
return aws_raise_error(s_convert_pton_error(result)); |
| 3406 |
} |
| 3407 |
return AWS_OP_SUCCESS; |
| 3408 |
} |
| 3409 |
int aws_parse_ipv6_address(const struct aws_string *src, struct aws_byte_buf *dst) { |
| 3410 |
|
| 3411 |
size_t result_length = 16; |
| 3412 |
if (dst->capacity - dst->len < result_length) { |
| 3413 |
return aws_raise_error(AWS_ERROR_SHORT_BUFFER); |
| 3414 |
} |
| 3415 |
int result = inet_pton(AF_INET6, aws_string_c_str(src), dst->buffer + dst->len); |
| 3416 |
if (result != 1) { |
| 3417 |
return aws_raise_error(s_convert_pton_error(result)); |
| 3418 |
} |
| 3419 |
dst->len += result_length; |
| 3420 |
return AWS_OP_SUCCESS; |
| 3421 |
} |