| 1 |
// Copyright (c) Microsoft. All rights reserved. |
| 2 |
// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 |
|
| 4 |
#include <stdlib.h> |
| 5 |
#include <stdio.h> |
| 6 |
#include <winsock2.h> |
| 7 |
#include <ws2tcpip.h> |
| 8 |
#include <windows.h> |
| 9 |
#include <mstcpip.h> |
| 10 |
#ifdef AF_UNIX_ON_WINDOWS |
| 11 |
#include <afunix.h> |
| 12 |
#endif |
| 13 |
#include "azure_c_shared_utility/socketio.h" |
| 14 |
#include "azure_c_shared_utility/singlylinkedlist.h" |
| 15 |
#include "azure_c_shared_utility/gballoc.h" |
| 16 |
#include "azure_c_shared_utility/gbnetwork.h" |
| 17 |
#include "azure_c_shared_utility/optimize_size.h" |
| 18 |
#include "azure_c_shared_utility/shared_util_options.h" |
| 19 |
#include "azure_c_shared_utility/xlogging.h" |
| 20 |
#include "azure_c_shared_utility/dns_resolver.h" |
| 21 |
#include "azure_c_shared_utility/safe_math.h" |
| 22 |
|
| 23 |
typedef enum IO_STATE_TAG |
| 24 |
{ |
| 25 |
IO_STATE_CLOSED, |
| 26 |
IO_STATE_OPENING, |
| 27 |
IO_STATE_OPEN, |
| 28 |
IO_STATE_CLOSING |
| 29 |
} IO_STATE; |
| 30 |
|
| 31 |
typedef struct PENDING_SOCKET_IO_TAG |
| 32 |
{ |
| 33 |
unsigned char* bytes; |
| 34 |
size_t size; |
| 35 |
ON_SEND_COMPLETE on_send_complete; |
| 36 |
void* callback_context; |
| 37 |
SINGLYLINKEDLIST_HANDLE pending_io_list; |
| 38 |
} PENDING_SOCKET_IO; |
| 39 |
|
| 40 |
typedef struct SOCKET_IO_INSTANCE_TAG |
| 41 |
{ |
| 42 |
SOCKET socket; |
| 43 |
SOCKETIO_ADDRESS_TYPE address_type; |
| 44 |
ON_BYTES_RECEIVED on_bytes_received; |
| 45 |
ON_IO_ERROR on_io_error; |
| 46 |
ON_IO_OPEN_COMPLETE on_io_open_complete; |
| 47 |
void* on_bytes_received_context; |
| 48 |
void* on_io_error_context; |
| 49 |
void* on_io_open_complete_context; |
| 50 |
char* hostname; |
| 51 |
int port; |
| 52 |
IO_STATE io_state; |
| 53 |
SINGLYLINKEDLIST_HANDLE pending_io_list; |
| 54 |
struct tcp_keepalive keep_alive; |
| 55 |
unsigned char recv_bytes[XIO_RECEIVE_BUFFER_SIZE]; |
| 56 |
DNSRESOLVER_HANDLE dns_resolver; |
| 57 |
struct addrinfo* addrInfo; |
| 58 |
} SOCKET_IO_INSTANCE; |
| 59 |
|
| 60 |
/*this function will clone an option given by name and value*/ |
| 61 |
static void* socketio_CloneOption(const char* name, const void* value) |
| 62 |
{ |
| 63 |
(void)name; |
| 64 |
(void)value; |
| 65 |
return NULL; |
| 66 |
} |
| 67 |
|
| 68 |
/*this function destroys an option previously created*/ |
| 69 |
static void socketio_DestroyOption(const char* name, const void* value) |
| 70 |
{ |
| 71 |
(void)name; |
| 72 |
(void)value; |
| 73 |
} |
| 74 |
|
| 75 |
static OPTIONHANDLER_HANDLE socketio_retrieveoptions(CONCRETE_IO_HANDLE handle) |
| 76 |
{ |
| 77 |
OPTIONHANDLER_HANDLE result; |
| 78 |
(void)handle; |
| 79 |
result = OptionHandler_Create(socketio_CloneOption, socketio_DestroyOption, socketio_setoption); |
| 80 |
if (result == NULL) |
| 81 |
{ |
| 82 |
LogError("unable to OptionHandler_Create"); |
| 83 |
/*return as is*/ |
| 84 |
} |
| 85 |
else |
| 86 |
{ |
| 87 |
/*insert here work to add the options to "result" handle*/ |
| 88 |
} |
| 89 |
return result; |
| 90 |
} |
| 91 |
|
| 92 |
static const IO_INTERFACE_DESCRIPTION socket_io_interface_description = |
| 93 |
{ |
| 94 |
socketio_retrieveoptions, |
| 95 |
socketio_create, |
| 96 |
socketio_destroy, |
| 97 |
socketio_open, |
| 98 |
socketio_close, |
| 99 |
socketio_send, |
| 100 |
socketio_dowork, |
| 101 |
socketio_setoption |
| 102 |
}; |
| 103 |
|
| 104 |
static void indicate_error(SOCKET_IO_INSTANCE* socket_io_instance) |
| 105 |
{ |
| 106 |
if (socket_io_instance->on_io_error != NULL) |
| 107 |
{ |
| 108 |
socket_io_instance->on_io_error(socket_io_instance->on_io_error_context); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
static int add_pending_io(SOCKET_IO_INSTANCE* socket_io_instance, const unsigned char* buffer, size_t size, ON_SEND_COMPLETE on_send_complete, void* callback_context) |
| 113 |
{ |
| 114 |
int result; |
| 115 |
PENDING_SOCKET_IO* pending_socket_io = (PENDING_SOCKET_IO*)calloc(1, sizeof(PENDING_SOCKET_IO)); |
| 116 |
if (pending_socket_io == NULL) |
| 117 |
{ |
| 118 |
result = MU_FAILURE; |
| 119 |
} |
| 120 |
else |
| 121 |
{ |
| 122 |
pending_socket_io->bytes = (unsigned char*)malloc(size); |
| 123 |
if (pending_socket_io->bytes == NULL) |
| 124 |
{ |
| 125 |
LogError("Allocation Failure: Unable to allocate pending list."); |
| 126 |
free(pending_socket_io); |
| 127 |
pending_socket_io = NULL; |
| 128 |
result = MU_FAILURE; |
| 129 |
} |
| 130 |
else |
| 131 |
{ |
| 132 |
pending_socket_io->size = size; |
| 133 |
pending_socket_io->on_send_complete = on_send_complete; |
| 134 |
pending_socket_io->callback_context = callback_context; |
| 135 |
pending_socket_io->pending_io_list = socket_io_instance->pending_io_list; |
| 136 |
(void)memcpy(pending_socket_io->bytes, buffer, size); |
| 137 |
|
| 138 |
if (singlylinkedlist_add(socket_io_instance->pending_io_list, pending_socket_io) == NULL) |
| 139 |
{ |
| 140 |
LogError("Failure: Unable to add socket to pending list."); |
| 141 |
free(pending_socket_io->bytes); |
| 142 |
free(pending_socket_io); |
| 143 |
pending_socket_io = NULL; |
| 144 |
result = MU_FAILURE; |
| 145 |
} |
| 146 |
else |
| 147 |
{ |
| 148 |
result = 0; |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
return result; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
static int lookup_address(SOCKET_IO_INSTANCE* socket_io_instance) |
| 157 |
{ |
| 158 |
int result = 0; |
| 159 |
|
| 160 |
if (socket_io_instance->address_type == ADDRESS_TYPE_IP) |
| 161 |
{ |
| 162 |
if (!dns_resolver_is_lookup_complete(socket_io_instance->dns_resolver)) |
| 163 |
{ |
| 164 |
socket_io_instance->io_state = IO_STATE_OPENING; |
| 165 |
} |
| 166 |
else if (dns_resolver_get_ipv4(socket_io_instance->dns_resolver) == 0) |
| 167 |
{ |
| 168 |
LogError("DNS resolution failure %d.", WSAGetLastError()); |
| 169 |
result = MU_FAILURE; |
| 170 |
} |
| 171 |
else |
| 172 |
{ |
| 173 |
//The hostname IP has been returned. |
| 174 |
//So, the socket is ready to open because currently socket_io_instance->socket is INVALID_SOCKET. |
| 175 |
socket_io_instance->io_state = IO_STATE_OPEN; |
| 176 |
} |
| 177 |
} |
| 178 |
else //ADDRESS_TYPE_DOMAIN_SOCKET |
| 179 |
{ |
| 180 |
socket_io_instance->io_state = IO_STATE_OPEN; |
| 181 |
} |
| 182 |
|
| 183 |
return result; |
| 184 |
} |
| 185 |
|
| 186 |
static int connect_socket(SOCKET socket, struct sockaddr* addr, size_t len) |
| 187 |
{ |
| 188 |
int result; |
| 189 |
u_long iMode = 1; |
| 190 |
|
| 191 |
if (connect(socket, addr, (int)len) != 0) |
| 192 |
{ |
| 193 |
LogError("Failure: connect failure %d.", WSAGetLastError()); |
| 194 |
result = MU_FAILURE; |
| 195 |
} |
| 196 |
else if (ioctlsocket(socket, FIONBIO, &iMode) != 0) |
| 197 |
{ |
| 198 |
LogError("Failure: ioctlsocket failure %d.", WSAGetLastError()); |
| 199 |
result = MU_FAILURE; |
| 200 |
} |
| 201 |
else |
| 202 |
{ |
| 203 |
result = 0; |
| 204 |
} |
| 205 |
|
| 206 |
return result; |
| 207 |
} |
| 208 |
|
| 209 |
static int initiate_socket_connection(SOCKET_IO_INSTANCE* socket_io_instance) |
| 210 |
{ |
| 211 |
int result; |
| 212 |
|
| 213 |
#ifdef AF_UNIX_ON_WINDOWS |
| 214 |
if (socket_io_instance->address_type == ADDRESS_TYPE_IP) |
| 215 |
{ |
| 216 |
#endif |
| 217 |
struct addrinfo* addr = dns_resolver_get_addrInfo(socket_io_instance->dns_resolver); |
| 218 |
|
| 219 |
if (addr == NULL) |
| 220 |
{ |
| 221 |
LogError("DNS resolution failed"); |
| 222 |
result = MU_FAILURE; |
| 223 |
} |
| 224 |
else |
| 225 |
{ |
| 226 |
(void)memcpy((socket_io_instance->addrInfo), addr, sizeof(*(socket_io_instance->addrInfo))); |
| 227 |
|
| 228 |
result = connect_socket(socket_io_instance->socket, (socket_io_instance->addrInfo)->ai_addr, sizeof(*((socket_io_instance->addrInfo)->ai_addr))); |
| 229 |
if (result != 0) |
| 230 |
{ |
| 231 |
LogError("connect_socket failed"); |
| 232 |
} |
| 233 |
else |
| 234 |
{ |
| 235 |
if (socket_io_instance->on_io_open_complete != NULL) |
| 236 |
{ |
| 237 |
socket_io_instance->on_io_open_complete(socket_io_instance->on_io_open_complete_context, IO_OPEN_OK /*: IO_OPEN_ERROR*/); |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
#ifdef AF_UNIX_ON_WINDOWS |
| 242 |
} |
| 243 |
else // ADDRESS_TYPE_DOMAIN_SOCKET |
| 244 |
{ |
| 245 |
SOCKADDR_UN addr_un; |
| 246 |
const char* path = socket_io_instance->hostname; |
| 247 |
size_t path_len = strlen(path); |
| 248 |
|
| 249 |
// If the value of hostname was parsed out of a 'unix://' URL, it might have a |
| 250 |
// leading forward slash ('/'). That's because the domain socket path is found |
| 251 |
// in the path portion of the URL, not the hostname portion. The hostname is |
| 252 |
// empty and the forward slash delimits the start of the path. For Unix domain |
| 253 |
// socket paths on Windows, discard the leading forward slash. |
| 254 |
if (path[0] == '/') |
| 255 |
{ |
| 256 |
++path; |
| 257 |
--path_len; |
| 258 |
} |
| 259 |
|
| 260 |
if (path_len + 1 > sizeof(addr_un.sun_path)) |
| 261 |
{ |
| 262 |
LogError("Path '%s' is too long for a unix socket (max len = %lu)", path, (unsigned long)sizeof(addr_un.sun_path)); |
| 263 |
result = MU_FAILURE; |
| 264 |
} |
| 265 |
else |
| 266 |
{ |
| 267 |
(void)memset(&addr_un, 0, sizeof(addr_un)); |
| 268 |
addr_un.sun_family = AF_UNIX; |
| 269 |
// No need to add NULL terminator due to the above memset |
| 270 |
(void)memcpy(addr_un.sun_path, path, path_len); |
| 271 |
|
| 272 |
result = connect_socket(socket_io_instance->socket, (struct sockaddr*)&addr_un, sizeof(addr_un)); |
| 273 |
if (result != 0) |
| 274 |
{ |
| 275 |
LogError("connect_socket failed"); |
| 276 |
result = MU_FAILURE; |
| 277 |
} |
| 278 |
else |
| 279 |
{ |
| 280 |
socket_io_instance->io_state = IO_STATE_OPEN; |
| 281 |
if (socket_io_instance->on_io_open_complete != NULL) |
| 282 |
{ |
| 283 |
socket_io_instance->on_io_open_complete(socket_io_instance->on_io_open_complete_context, IO_OPEN_OK); |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
} |
| 288 |
#endif |
| 289 |
|
| 290 |
return result; |
| 291 |
} |
| 292 |
|
| 293 |
static void destroy_socket_io_instance(SOCKET_IO_INSTANCE* instance) |
| 294 |
{ |
| 295 |
if (instance->dns_resolver != NULL) |
| 296 |
{ |
| 297 |
dns_resolver_destroy(instance->dns_resolver); |
| 298 |
} |
| 299 |
|
| 300 |
free(instance->hostname); |
| 301 |
free(instance->addrInfo); |
| 302 |
|
| 303 |
if (instance->pending_io_list != NULL) |
| 304 |
{ |
| 305 |
singlylinkedlist_destroy(instance->pending_io_list); |
| 306 |
} |
| 307 |
|
| 308 |
free(instance); |
| 309 |
} |
| 310 |
|
| 311 |
CONCRETE_IO_HANDLE socketio_create(void* io_create_parameters) |
| 312 |
{ |
| 313 |
SOCKETIO_CONFIG* socket_io_config = (SOCKETIO_CONFIG*)io_create_parameters; |
| 314 |
SOCKET_IO_INSTANCE* result; |
| 315 |
struct tcp_keepalive tcp_keepalive = { 0, 0, 0 }; |
| 316 |
|
| 317 |
if (socket_io_config == NULL) |
| 318 |
{ |
| 319 |
LogError("Invalid argument: socket_io_config is NULL"); |
| 320 |
result = NULL; |
| 321 |
} |
| 322 |
else |
| 323 |
{ |
| 324 |
result = (SOCKET_IO_INSTANCE*)malloc(sizeof(SOCKET_IO_INSTANCE)); |
| 325 |
|
| 326 |
if (result != NULL) |
| 327 |
{ |
| 328 |
(void)memset(result, 0, sizeof(SOCKET_IO_INSTANCE)); |
| 329 |
|
| 330 |
result->address_type = ADDRESS_TYPE_IP; |
| 331 |
result->pending_io_list = singlylinkedlist_create(); |
| 332 |
if (result->pending_io_list == NULL) |
| 333 |
{ |
| 334 |
LogError("Failure: singlylinkedlist_create unable to create pending list."); |
| 335 |
destroy_socket_io_instance(result); |
| 336 |
result = NULL; |
| 337 |
} |
| 338 |
else |
| 339 |
{ |
| 340 |
if (socket_io_config->hostname != NULL) |
| 341 |
{ |
| 342 |
size_t malloc_size = safe_add_size_t(strlen(socket_io_config->hostname), 1); |
| 343 |
if (malloc_size == SIZE_MAX) |
| 344 |
{ |
| 345 |
LogError("Invalid malloc size"); |
| 346 |
result->hostname = NULL; |
| 347 |
} |
| 348 |
else |
| 349 |
{ |
| 350 |
result->hostname = (char*)malloc(malloc_size); |
| 351 |
if (result->hostname != NULL) |
| 352 |
{ |
| 353 |
(void)strcpy(result->hostname, socket_io_config->hostname); |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
result->socket = INVALID_SOCKET; |
| 358 |
} |
| 359 |
else |
| 360 |
{ |
| 361 |
result->hostname = NULL; |
| 362 |
result->socket = *((SOCKET*)socket_io_config->accepted_socket); |
| 363 |
} |
| 364 |
|
| 365 |
if ((result->hostname == NULL) && (result->socket == INVALID_SOCKET)) |
| 366 |
{ |
| 367 |
LogError("Failure: hostname == NULL and socket is invalid."); |
| 368 |
destroy_socket_io_instance(result); |
| 369 |
result = NULL; |
| 370 |
} |
| 371 |
else if ((result->addrInfo = calloc(1, sizeof(struct addrinfo))) == NULL) |
| 372 |
{ |
| 373 |
LogError("Failure: addrInfo == NULL."); |
| 374 |
destroy_socket_io_instance(result); |
| 375 |
result = NULL; |
| 376 |
} |
| 377 |
else |
| 378 |
{ |
| 379 |
result->port = socket_io_config->port; |
| 380 |
result->on_io_open_complete = NULL; |
| 381 |
result->dns_resolver = dns_resolver_create(result->hostname, socket_io_config->port, NULL); |
| 382 |
result->on_bytes_received = NULL; |
| 383 |
result->on_io_error = NULL; |
| 384 |
result->on_bytes_received_context = NULL; |
| 385 |
result->on_io_error_context = NULL; |
| 386 |
result->io_state = IO_STATE_CLOSED; |
| 387 |
result->keep_alive = tcp_keepalive; |
| 388 |
} |
| 389 |
} |
| 390 |
} |
| 391 |
else |
| 392 |
{ |
| 393 |
LogError("Allocation Failure: SOCKET_IO_INSTANCE"); |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
return (XIO_HANDLE)result; |
| 398 |
} |
| 399 |
|
| 400 |
void socketio_destroy(CONCRETE_IO_HANDLE socket_io) |
| 401 |
{ |
| 402 |
LIST_ITEM_HANDLE first_pending_io; |
| 403 |
|
| 404 |
if (socket_io != NULL) |
| 405 |
{ |
| 406 |
SOCKET_IO_INSTANCE* socket_io_instance = (SOCKET_IO_INSTANCE*)socket_io; |
| 407 |
/* we cannot do much if the close fails, so just ignore the result */ |
| 408 |
(void)closesocket(socket_io_instance->socket); |
| 409 |
|
| 410 |
/* clear allpending IOs */ |
| 411 |
|
| 412 |
while ((first_pending_io = singlylinkedlist_get_head_item(socket_io_instance->pending_io_list)) != NULL) |
| 413 |
{ |
| 414 |
PENDING_SOCKET_IO* pending_socket_io = (PENDING_SOCKET_IO*)singlylinkedlist_item_get_value(first_pending_io); |
| 415 |
if (pending_socket_io != NULL) |
| 416 |
{ |
| 417 |
free(pending_socket_io->bytes); |
| 418 |
free(pending_socket_io); |
| 419 |
} |
| 420 |
|
| 421 |
singlylinkedlist_remove(socket_io_instance->pending_io_list, first_pending_io); |
| 422 |
} |
| 423 |
|
| 424 |
destroy_socket_io_instance(socket_io); |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
int socketio_open(CONCRETE_IO_HANDLE socket_io, ON_IO_OPEN_COMPLETE on_io_open_complete, void* on_io_open_complete_context, ON_BYTES_RECEIVED on_bytes_received, void* on_bytes_received_context, ON_IO_ERROR on_io_error, void* on_io_error_context) |
| 429 |
{ |
| 430 |
int result; |
| 431 |
|
| 432 |
SOCKET_IO_INSTANCE* socket_io_instance = (SOCKET_IO_INSTANCE*)socket_io; |
| 433 |
if (socket_io_instance == NULL) |
| 434 |
{ |
| 435 |
LogError("Invalid argument: SOCKET_IO_INSTANCE is NULL"); |
| 436 |
result = MU_FAILURE; |
| 437 |
} |
| 438 |
else |
| 439 |
{ |
| 440 |
if (socket_io_instance->io_state != IO_STATE_CLOSED) |
| 441 |
{ |
| 442 |
LogError("Failure: socket state is not closed."); |
| 443 |
result = MU_FAILURE; |
| 444 |
} |
| 445 |
else if (socket_io_instance->socket != INVALID_SOCKET) |
| 446 |
{ |
| 447 |
// Opening an accepted socket |
| 448 |
socket_io_instance->on_bytes_received_context = on_bytes_received_context; |
| 449 |
socket_io_instance->on_bytes_received = on_bytes_received; |
| 450 |
socket_io_instance->on_io_error = on_io_error; |
| 451 |
socket_io_instance->on_io_error_context = on_io_error_context; |
| 452 |
socket_io_instance->on_io_open_complete = on_io_open_complete; |
| 453 |
socket_io_instance->on_io_open_complete_context = on_io_open_complete_context; |
| 454 |
|
| 455 |
socket_io_instance->io_state = IO_STATE_OPEN; |
| 456 |
|
| 457 |
result = 0; |
| 458 |
} |
| 459 |
else |
| 460 |
{ |
| 461 |
#ifdef AF_UNIX_ON_WINDOWS |
| 462 |
int addr_family = socket_io_instance->address_type == ADDRESS_TYPE_IP ? AF_INET : AF_UNIX; |
| 463 |
#else |
| 464 |
int addr_family = AF_INET; |
| 465 |
#endif |
| 466 |
socket_io_instance->socket = socket(addr_family, SOCK_STREAM, 0); |
| 467 |
if (socket_io_instance->socket == INVALID_SOCKET) |
| 468 |
{ |
| 469 |
LogError("Failure: socket create failure %d.", WSAGetLastError()); |
| 470 |
result = MU_FAILURE; |
| 471 |
} |
| 472 |
else if (lookup_address(socket_io_instance) != 0) |
| 473 |
{ |
| 474 |
LogError("lookup_address failed"); |
| 475 |
(void)closesocket(socket_io_instance->socket); |
| 476 |
socket_io_instance->socket = INVALID_SOCKET; |
| 477 |
result = MU_FAILURE; |
| 478 |
} |
| 479 |
else if (socket_io_instance->io_state == IO_STATE_OPEN && initiate_socket_connection(socket_io_instance) != 0) |
| 480 |
{ |
| 481 |
LogError("initiate_socket_connection failed"); |
| 482 |
(void)closesocket(socket_io_instance->socket); |
| 483 |
socket_io_instance->socket = INVALID_SOCKET; |
| 484 |
result = MU_FAILURE; |
| 485 |
} |
| 486 |
else |
| 487 |
{ |
| 488 |
socket_io_instance->on_bytes_received_context = on_bytes_received_context; |
| 489 |
socket_io_instance->on_bytes_received = on_bytes_received; |
| 490 |
socket_io_instance->on_io_error = on_io_error; |
| 491 |
socket_io_instance->on_io_error_context = on_io_error_context; |
| 492 |
socket_io_instance->on_io_open_complete = on_io_open_complete; |
| 493 |
socket_io_instance->on_io_open_complete_context = on_io_open_complete_context; |
| 494 |
|
| 495 |
result = 0; |
| 496 |
} |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
if (socket_io_instance != NULL && socket_io_instance->io_state != IO_STATE_OPENING) |
| 501 |
{ |
| 502 |
if (on_io_open_complete != NULL) |
| 503 |
{ |
| 504 |
on_io_open_complete(on_io_open_complete_context, result == 0 ? IO_OPEN_OK : IO_OPEN_ERROR); |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
return result; |
| 509 |
} |
| 510 |
|
| 511 |
int socketio_close(CONCRETE_IO_HANDLE socket_io, ON_IO_CLOSE_COMPLETE on_io_close_complete, void* callback_context) |
| 512 |
{ |
| 513 |
int result; |
| 514 |
|
| 515 |
if (socket_io == NULL) |
| 516 |
{ |
| 517 |
LogError("Invalid argument: socket_io is NULL"); |
| 518 |
result = MU_FAILURE; |
| 519 |
} |
| 520 |
else |
| 521 |
{ |
| 522 |
SOCKET_IO_INSTANCE* socket_io_instance = (SOCKET_IO_INSTANCE*)socket_io; |
| 523 |
|
| 524 |
if ((socket_io_instance->io_state != IO_STATE_CLOSING) && |
| 525 |
(socket_io_instance->io_state != IO_STATE_CLOSED)) |
| 526 |
{ |
| 527 |
(void)closesocket(socket_io_instance->socket); |
| 528 |
socket_io_instance->socket = INVALID_SOCKET; |
| 529 |
socket_io_instance->io_state = IO_STATE_CLOSED; |
| 530 |
} |
| 531 |
|
| 532 |
if (on_io_close_complete != NULL) |
| 533 |
{ |
| 534 |
on_io_close_complete(callback_context); |
| 535 |
} |
| 536 |
result = 0; |
| 537 |
} |
| 538 |
|
| 539 |
return result; |
| 540 |
} |
| 541 |
|
| 542 |
int socketio_send(CONCRETE_IO_HANDLE socket_io, const void* buffer, size_t size, ON_SEND_COMPLETE on_send_complete, void* callback_context) |
| 543 |
{ |
| 544 |
int result; |
| 545 |
|
| 546 |
if ((socket_io == NULL) || |
| 547 |
(buffer == NULL) || |
| 548 |
(size == 0)) |
| 549 |
{ |
| 550 |
/* Invalid arguments */ |
| 551 |
LogError("Invalid argument: send given invalid parameter"); |
| 552 |
result = MU_FAILURE; |
| 553 |
} |
| 554 |
else |
| 555 |
{ |
| 556 |
SOCKET_IO_INSTANCE* socket_io_instance = (SOCKET_IO_INSTANCE*)socket_io; |
| 557 |
if (socket_io_instance->io_state != IO_STATE_OPEN) |
| 558 |
{ |
| 559 |
LogError("Failure: socket state is not opened."); |
| 560 |
result = MU_FAILURE; |
| 561 |
} |
| 562 |
else |
| 563 |
{ |
| 564 |
LIST_ITEM_HANDLE first_pending_io = singlylinkedlist_get_head_item(socket_io_instance->pending_io_list); |
| 565 |
if (first_pending_io != NULL) |
| 566 |
{ |
| 567 |
if (add_pending_io(socket_io_instance, (const unsigned char*)buffer, size, on_send_complete, callback_context) != 0) |
| 568 |
{ |
| 569 |
LogError("Failure: add_pending_io failed."); |
| 570 |
result = MU_FAILURE; |
| 571 |
} |
| 572 |
else |
| 573 |
{ |
| 574 |
result = 0; |
| 575 |
} |
| 576 |
} |
| 577 |
else |
| 578 |
{ |
| 579 |
int send_result = send(socket_io_instance->socket, (const char*)buffer, (int)size, 0); |
| 580 |
if ((size_t)send_result != size) |
| 581 |
{ |
| 582 |
int last_error = WSAGetLastError(); |
| 583 |
|
| 584 |
if (send_result == SOCKET_ERROR && last_error != WSAEWOULDBLOCK) |
| 585 |
{ |
| 586 |
LogError("Failure: sending socket failed %d.", last_error); |
| 587 |
result = MU_FAILURE; |
| 588 |
} |
| 589 |
else |
| 590 |
{ |
| 591 |
size_t bytes_sent = (send_result == SOCKET_ERROR ? 0 : send_result); |
| 592 |
|
| 593 |
/* queue data */ |
| 594 |
if (add_pending_io(socket_io_instance, ((const unsigned char*)buffer) + bytes_sent, size - bytes_sent, on_send_complete, callback_context) != 0) |
| 595 |
{ |
| 596 |
LogError("Failure: add_pending_io failed."); |
| 597 |
result = MU_FAILURE; |
| 598 |
} |
| 599 |
else |
| 600 |
{ |
| 601 |
result = 0; |
| 602 |
} |
| 603 |
} |
| 604 |
} |
| 605 |
else |
| 606 |
{ |
| 607 |
if (on_send_complete != NULL) |
| 608 |
{ |
| 609 |
on_send_complete(callback_context, IO_SEND_OK); |
| 610 |
} |
| 611 |
|
| 612 |
result = 0; |
| 613 |
} |
| 614 |
} |
| 615 |
} |
| 616 |
} |
| 617 |
|
| 618 |
return result; |
| 619 |
} |
| 620 |
|
| 621 |
void socketio_dowork(CONCRETE_IO_HANDLE socket_io) |
| 622 |
{ |
| 623 |
int send_result; |
| 624 |
if (socket_io != NULL) |
| 625 |
{ |
| 626 |
SOCKET_IO_INSTANCE* socket_io_instance = (SOCKET_IO_INSTANCE*)socket_io; |
| 627 |
if (socket_io_instance->io_state == IO_STATE_OPEN) |
| 628 |
{ |
| 629 |
LIST_ITEM_HANDLE first_pending_io = singlylinkedlist_get_head_item(socket_io_instance->pending_io_list); |
| 630 |
while (first_pending_io != NULL) |
| 631 |
{ |
| 632 |
PENDING_SOCKET_IO* pending_socket_io = (PENDING_SOCKET_IO*)singlylinkedlist_item_get_value(first_pending_io); |
| 633 |
if (pending_socket_io == NULL) |
| 634 |
{ |
| 635 |
LogError("Failure: retrieving socket from list"); |
| 636 |
indicate_error(socket_io_instance); |
| 637 |
break; |
| 638 |
} |
| 639 |
|
| 640 |
/* TODO: we need to do more than a cast here to be 100% clean |
| 641 |
The following bug was filed: [WarnL4] socketio_win32 does not account for already sent bytes and there is a truncation of size from size_t to int */ |
| 642 |
send_result = send(socket_io_instance->socket, (const char*)pending_socket_io->bytes, (int)pending_socket_io->size, 0); |
| 643 |
if ((size_t)send_result != pending_socket_io->size) |
| 644 |
{ |
| 645 |
int last_error = WSAGetLastError(); |
| 646 |
if (last_error != WSAEWOULDBLOCK) |
| 647 |
{ |
| 648 |
free(pending_socket_io->bytes); |
| 649 |
free(pending_socket_io); |
| 650 |
pending_socket_io = NULL; |
| 651 |
(void)singlylinkedlist_remove(socket_io_instance->pending_io_list, first_pending_io); |
| 652 |
} |
| 653 |
else |
| 654 |
{ |
| 655 |
/* try again */ |
| 656 |
} |
| 657 |
} |
| 658 |
else |
| 659 |
{ |
| 660 |
if (pending_socket_io->on_send_complete != NULL) |
| 661 |
{ |
| 662 |
pending_socket_io->on_send_complete(pending_socket_io->callback_context, IO_SEND_OK); |
| 663 |
} |
| 664 |
|
| 665 |
free(pending_socket_io->bytes); |
| 666 |
free(pending_socket_io); |
| 667 |
pending_socket_io = NULL; |
| 668 |
if (singlylinkedlist_remove(socket_io_instance->pending_io_list, first_pending_io) != 0) |
| 669 |
{ |
| 670 |
LogError("Failure: removing socket from list"); |
| 671 |
indicate_error(socket_io_instance); |
| 672 |
} |
| 673 |
} |
| 674 |
|
| 675 |
first_pending_io = singlylinkedlist_get_head_item(socket_io_instance->pending_io_list); |
| 676 |
} |
| 677 |
|
| 678 |
if (socket_io_instance->io_state == IO_STATE_OPEN) |
| 679 |
{ |
| 680 |
int received = 0; |
| 681 |
do |
| 682 |
{ |
| 683 |
received = recv(socket_io_instance->socket, (char*)socket_io_instance->recv_bytes, XIO_RECEIVE_BUFFER_SIZE, 0); |
| 684 |
if ((received > 0)) |
| 685 |
{ |
| 686 |
if (socket_io_instance->on_bytes_received != NULL) |
| 687 |
{ |
| 688 |
/* Explicitly ignoring here the result of the callback */ |
| 689 |
(void)socket_io_instance->on_bytes_received(socket_io_instance->on_bytes_received_context, socket_io_instance->recv_bytes, received); |
| 690 |
} |
| 691 |
} |
| 692 |
else if (received == 0) |
| 693 |
{ |
| 694 |
indicate_error(socket_io_instance); |
| 695 |
} |
| 696 |
else |
| 697 |
{ |
| 698 |
int last_error = WSAGetLastError(); |
| 699 |
if (last_error != WSAEWOULDBLOCK && last_error != ERROR_SUCCESS) |
| 700 |
{ |
| 701 |
LogError("Socketio_Failure: Receiving data from endpoint: %d.", last_error); |
| 702 |
indicate_error(socket_io_instance); |
| 703 |
} |
| 704 |
} |
| 705 |
} while (received > 0 && socket_io_instance->io_state == IO_STATE_OPEN); |
| 706 |
} |
| 707 |
} |
| 708 |
else |
| 709 |
{ |
| 710 |
//Handle async socket_open operation within socket_dowork |
| 711 |
if (socket_io_instance->io_state == IO_STATE_OPENING) |
| 712 |
{ |
| 713 |
if (lookup_address(socket_io_instance) != 0) |
| 714 |
{ |
| 715 |
LogError("lookup_address failed"); |
| 716 |
(void)closesocket(socket_io_instance->socket); |
| 717 |
socket_io_instance->socket = INVALID_SOCKET; |
| 718 |
socket_io_instance->io_state = IO_STATE_CLOSED; |
| 719 |
} |
| 720 |
else if (socket_io_instance->io_state == IO_STATE_OPEN && initiate_socket_connection(socket_io_instance) != 0) |
| 721 |
{ |
| 722 |
LogError("initialize_socket_connection failed"); |
| 723 |
(void)closesocket(socket_io_instance->socket); |
| 724 |
socket_io_instance->socket = INVALID_SOCKET; |
| 725 |
socket_io_instance->io_state = IO_STATE_CLOSED; |
| 726 |
} |
| 727 |
} |
| 728 |
} |
| 729 |
} |
| 730 |
} |
| 731 |
|
| 732 |
static int set_keepalive(SOCKET_IO_INSTANCE* socket_io, struct tcp_keepalive* keepAlive) |
| 733 |
{ |
| 734 |
int result; |
| 735 |
DWORD bytesReturned; |
| 736 |
|
| 737 |
int err = WSAIoctl(socket_io->socket, SIO_KEEPALIVE_VALS, keepAlive, |
| 738 |
sizeof(struct tcp_keepalive), NULL, 0, &bytesReturned, NULL, NULL); |
| 739 |
if (err != 0) |
| 740 |
{ |
| 741 |
LogError("Failure: setting keep-alive on the socket: %d.", err == SOCKET_ERROR ? WSAGetLastError() : err); |
| 742 |
result = MU_FAILURE; |
| 743 |
} |
| 744 |
else |
| 745 |
{ |
| 746 |
socket_io->keep_alive = *keepAlive; |
| 747 |
result = 0; |
| 748 |
} |
| 749 |
|
| 750 |
return result; |
| 751 |
} |
| 752 |
|
| 753 |
static int socketio_setaddresstype_option(SOCKET_IO_INSTANCE* socket_io_instance, const char* addressType) |
| 754 |
{ |
| 755 |
int result; |
| 756 |
|
| 757 |
if (socket_io_instance->io_state != IO_STATE_CLOSED) |
| 758 |
{ |
| 759 |
LogError("Socket's type can only be changed when in state 'IO_STATE_CLOSED'. Current state=%d", socket_io_instance->io_state); |
| 760 |
result = MU_FAILURE; |
| 761 |
} |
| 762 |
#ifdef AF_UNIX_ON_WINDOWS |
| 763 |
else if (strcmp(addressType, OPTION_ADDRESS_TYPE_DOMAIN_SOCKET) == 0) |
| 764 |
{ |
| 765 |
socket_io_instance->address_type = ADDRESS_TYPE_DOMAIN_SOCKET; |
| 766 |
result = 0; |
| 767 |
} |
| 768 |
#endif |
| 769 |
else if (strcmp(addressType, OPTION_ADDRESS_TYPE_IP_SOCKET) == 0) |
| 770 |
{ |
| 771 |
socket_io_instance->address_type = ADDRESS_TYPE_IP; |
| 772 |
result = 0; |
| 773 |
} |
| 774 |
else |
| 775 |
{ |
| 776 |
LogError("Address type %s is not supported", addressType); |
| 777 |
result = MU_FAILURE; |
| 778 |
} |
| 779 |
|
| 780 |
return result; |
| 781 |
} |
| 782 |
|
| 783 |
int socketio_setoption(CONCRETE_IO_HANDLE socket_io, const char* optionName, const void* value) |
| 784 |
{ |
| 785 |
int result; |
| 786 |
|
| 787 |
if (socket_io == NULL || |
| 788 |
optionName == NULL || |
| 789 |
value == NULL) |
| 790 |
{ |
| 791 |
result = MU_FAILURE; |
| 792 |
} |
| 793 |
else |
| 794 |
{ |
| 795 |
SOCKET_IO_INSTANCE* socket_io_instance = (SOCKET_IO_INSTANCE*)socket_io; |
| 796 |
|
| 797 |
if (strcmp(optionName, "tcp_keepalive") == 0) |
| 798 |
{ |
| 799 |
struct tcp_keepalive keepAlive = socket_io_instance->keep_alive; |
| 800 |
keepAlive.onoff = *(int *)value; |
| 801 |
|
| 802 |
result = set_keepalive(socket_io_instance, &keepAlive); |
| 803 |
} |
| 804 |
else if (strcmp(optionName, "tcp_keepalive_time") == 0) |
| 805 |
{ |
| 806 |
unsigned long kaTime = *(int *)value * 1000; // convert to ms |
| 807 |
struct tcp_keepalive keepAlive = socket_io_instance->keep_alive; |
| 808 |
keepAlive.keepalivetime = kaTime; |
| 809 |
|
| 810 |
result = set_keepalive(socket_io_instance, &keepAlive); |
| 811 |
} |
| 812 |
else if (strcmp(optionName, "tcp_keepalive_interval") == 0) |
| 813 |
{ |
| 814 |
unsigned long kaInterval = *(int *)value * 1000; // convert to ms |
| 815 |
struct tcp_keepalive keepAlive = socket_io_instance->keep_alive; |
| 816 |
keepAlive.keepaliveinterval = kaInterval; |
| 817 |
|
| 818 |
result = set_keepalive(socket_io_instance, &keepAlive); |
| 819 |
} |
| 820 |
else if (strcmp(optionName, OPTION_ADDRESS_TYPE) == 0) |
| 821 |
{ |
| 822 |
result = socketio_setaddresstype_option(socket_io_instance, (const char*)value); |
| 823 |
} |
| 824 |
else |
| 825 |
{ |
| 826 |
result = MU_FAILURE; |
| 827 |
} |
| 828 |
} |
| 829 |
|
| 830 |
return result; |
| 831 |
} |
| 832 |
|
| 833 |
const IO_INTERFACE_DESCRIPTION* socketio_get_interface_description(void) |
| 834 |
{ |
| 835 |
return &socket_io_interface_description; |
| 836 |
} |