| 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 <stddef.h> |
| 6 |
#include <stdio.h> |
| 7 |
#include <string.h> |
| 8 |
#include "azure_c_shared_utility/socketio.h" |
| 9 |
#include "azure_c_shared_utility/singlylinkedlist.h" |
| 10 |
#include "azure_c_shared_utility/tcpsocketconnection_c.h" |
| 11 |
#include "azure_c_shared_utility/optimize_size.h" |
| 12 |
#include "azure_c_shared_utility/xlogging.h" |
| 13 |
|
| 14 |
#define UNABLE_TO_COMPLETE -2 |
| 15 |
#define MBED_XIO_RECEIVE_BUFFER_SIZE 128 |
| 16 |
|
| 17 |
typedef enum IO_STATE_TAG |
| 18 |
{ |
| 19 |
IO_STATE_CLOSED, |
| 20 |
IO_STATE_OPENING, |
| 21 |
IO_STATE_OPEN, |
| 22 |
IO_STATE_CLOSING, |
| 23 |
IO_STATE_ERROR |
| 24 |
} IO_STATE; |
| 25 |
|
| 26 |
typedef struct PENDING_SOCKET_IO_TAG |
| 27 |
{ |
| 28 |
unsigned char* bytes; |
| 29 |
size_t size; |
| 30 |
ON_SEND_COMPLETE on_send_complete; |
| 31 |
void* callback_context; |
| 32 |
SINGLYLINKEDLIST_HANDLE pending_io_list; |
| 33 |
} PENDING_SOCKET_IO; |
| 34 |
|
| 35 |
typedef struct SOCKET_IO_INSTANCE_TAG |
| 36 |
{ |
| 37 |
TCPSOCKETCONNECTION_HANDLE tcp_socket_connection; |
| 38 |
ON_BYTES_RECEIVED on_bytes_received; |
| 39 |
ON_IO_ERROR on_io_error; |
| 40 |
void* on_bytes_received_context; |
| 41 |
void* on_io_error_context; |
| 42 |
char* hostname; |
| 43 |
int port; |
| 44 |
IO_STATE io_state; |
| 45 |
SINGLYLINKEDLIST_HANDLE pending_io_list; |
| 46 |
} SOCKET_IO_INSTANCE; |
| 47 |
|
| 48 |
/*this function will clone an option given by name and value*/ |
| 49 |
static void* socketio_CloneOption(const char* name, const void* value) |
| 50 |
{ |
| 51 |
(void)name; |
| 52 |
(void)value; |
| 53 |
return NULL; |
| 54 |
} |
| 55 |
|
| 56 |
/*this function destroys an option previously created*/ |
| 57 |
static void socketio_DestroyOption(const char* name, const void* value) |
| 58 |
{ |
| 59 |
(void)name; |
| 60 |
(void)value; |
| 61 |
} |
| 62 |
|
| 63 |
static OPTIONHANDLER_HANDLE socketio_retrieveoptions(CONCRETE_IO_HANDLE socket_io) |
| 64 |
{ |
| 65 |
OPTIONHANDLER_HANDLE result; |
| 66 |
(void)socket_io; |
| 67 |
result = OptionHandler_Create(socketio_CloneOption, socketio_DestroyOption, socketio_setoption); |
| 68 |
if (result == NULL) |
| 69 |
{ |
| 70 |
/*return as is*/ |
| 71 |
} |
| 72 |
else |
| 73 |
{ |
| 74 |
/*insert here work to add the options to "result" handle*/ |
| 75 |
} |
| 76 |
return result; |
| 77 |
} |
| 78 |
|
| 79 |
static const IO_INTERFACE_DESCRIPTION socket_io_interface_description = |
| 80 |
{ |
| 81 |
socketio_retrieveoptions, |
| 82 |
socketio_create, |
| 83 |
socketio_destroy, |
| 84 |
socketio_open, |
| 85 |
socketio_close, |
| 86 |
socketio_send, |
| 87 |
socketio_dowork, |
| 88 |
socketio_setoption |
| 89 |
}; |
| 90 |
|
| 91 |
static void indicate_error(SOCKET_IO_INSTANCE* socket_io_instance) |
| 92 |
{ |
| 93 |
if (socket_io_instance->on_io_error != NULL) |
| 94 |
{ |
| 95 |
socket_io_instance->on_io_error(socket_io_instance->on_io_error_context); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
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) |
| 100 |
{ |
| 101 |
int result; |
| 102 |
PENDING_SOCKET_IO* pending_socket_io = (PENDING_SOCKET_IO*)malloc(sizeof(PENDING_SOCKET_IO)); |
| 103 |
if (pending_socket_io == NULL) |
| 104 |
{ |
| 105 |
result = MU_FAILURE; |
| 106 |
} |
| 107 |
else |
| 108 |
{ |
| 109 |
pending_socket_io->bytes = (unsigned char*)malloc(size); |
| 110 |
if (pending_socket_io->bytes == NULL) |
| 111 |
{ |
| 112 |
free(pending_socket_io); |
| 113 |
result = MU_FAILURE; |
| 114 |
} |
| 115 |
else |
| 116 |
{ |
| 117 |
pending_socket_io->size = size; |
| 118 |
pending_socket_io->on_send_complete = on_send_complete; |
| 119 |
pending_socket_io->callback_context = callback_context; |
| 120 |
pending_socket_io->pending_io_list = socket_io_instance->pending_io_list; |
| 121 |
(void)memcpy(pending_socket_io->bytes, buffer, size); |
| 122 |
|
| 123 |
if (singlylinkedlist_add(socket_io_instance->pending_io_list, pending_socket_io) == NULL) |
| 124 |
{ |
| 125 |
free(pending_socket_io->bytes); |
| 126 |
free(pending_socket_io); |
| 127 |
result = MU_FAILURE; |
| 128 |
} |
| 129 |
else |
| 130 |
{ |
| 131 |
result = 0; |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
return result; |
| 137 |
} |
| 138 |
|
| 139 |
CONCRETE_IO_HANDLE socketio_create(void* io_create_parameters) |
| 140 |
{ |
| 141 |
SOCKETIO_CONFIG* socket_io_config = io_create_parameters; |
| 142 |
SOCKET_IO_INSTANCE* result; |
| 143 |
|
| 144 |
if (socket_io_config == NULL) |
| 145 |
{ |
| 146 |
result = NULL; |
| 147 |
} |
| 148 |
else |
| 149 |
{ |
| 150 |
result = malloc(sizeof(SOCKET_IO_INSTANCE)); |
| 151 |
if (result != NULL) |
| 152 |
{ |
| 153 |
result->pending_io_list = singlylinkedlist_create(); |
| 154 |
if (result->pending_io_list == NULL) |
| 155 |
{ |
| 156 |
free(result); |
| 157 |
result = NULL; |
| 158 |
} |
| 159 |
else |
| 160 |
{ |
| 161 |
result->hostname = (char*)malloc(strlen(socket_io_config->hostname) + 1); |
| 162 |
if (result->hostname == NULL) |
| 163 |
{ |
| 164 |
singlylinkedlist_destroy(result->pending_io_list); |
| 165 |
free(result); |
| 166 |
result = NULL; |
| 167 |
} |
| 168 |
else |
| 169 |
{ |
| 170 |
strcpy(result->hostname, socket_io_config->hostname); |
| 171 |
result->port = socket_io_config->port; |
| 172 |
result->on_bytes_received = NULL; |
| 173 |
result->on_io_error = NULL; |
| 174 |
result->on_bytes_received_context = NULL; |
| 175 |
result->on_io_error_context = NULL; |
| 176 |
result->io_state = IO_STATE_CLOSED; |
| 177 |
result->tcp_socket_connection = NULL; |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
return result; |
| 184 |
} |
| 185 |
|
| 186 |
void socketio_destroy(CONCRETE_IO_HANDLE socket_io) |
| 187 |
{ |
| 188 |
if (socket_io != NULL) |
| 189 |
{ |
| 190 |
SOCKET_IO_INSTANCE* socket_io_instance = (SOCKET_IO_INSTANCE*)socket_io; |
| 191 |
|
| 192 |
if (socket_io_instance->tcp_socket_connection != NULL) |
| 193 |
{ |
| 194 |
tcpsocketconnection_destroy(socket_io_instance->tcp_socket_connection); |
| 195 |
} |
| 196 |
|
| 197 |
/* clear all pending IOs */ |
| 198 |
LIST_ITEM_HANDLE first_pending_io = singlylinkedlist_get_head_item(socket_io_instance->pending_io_list); |
| 199 |
while (first_pending_io != NULL) |
| 200 |
{ |
| 201 |
PENDING_SOCKET_IO* pending_socket_io = (PENDING_SOCKET_IO*)singlylinkedlist_item_get_value(first_pending_io); |
| 202 |
if (pending_socket_io != NULL) |
| 203 |
{ |
| 204 |
free(pending_socket_io->bytes); |
| 205 |
free(pending_socket_io); |
| 206 |
} |
| 207 |
|
| 208 |
(void)singlylinkedlist_remove(socket_io_instance->pending_io_list, first_pending_io); |
| 209 |
first_pending_io = singlylinkedlist_get_head_item(socket_io_instance->pending_io_list); |
| 210 |
} |
| 211 |
|
| 212 |
singlylinkedlist_destroy(socket_io_instance->pending_io_list); |
| 213 |
free(socket_io_instance->hostname); |
| 214 |
free(socket_io); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
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) |
| 219 |
{ |
| 220 |
int result; |
| 221 |
|
| 222 |
SOCKET_IO_INSTANCE* socket_io_instance = (SOCKET_IO_INSTANCE*)socket_io; |
| 223 |
if (socket_io == NULL) |
| 224 |
{ |
| 225 |
result = MU_FAILURE; |
| 226 |
} |
| 227 |
else |
| 228 |
{ |
| 229 |
if (socket_io_instance->tcp_socket_connection != NULL) |
| 230 |
{ |
| 231 |
tcpsocketconnection_close(socket_io_instance->tcp_socket_connection); |
| 232 |
tcpsocketconnection_destroy(socket_io_instance->tcp_socket_connection); |
| 233 |
} |
| 234 |
|
| 235 |
socket_io_instance->tcp_socket_connection = tcpsocketconnection_create(); |
| 236 |
if (socket_io_instance->tcp_socket_connection == NULL) |
| 237 |
{ |
| 238 |
result = MU_FAILURE; |
| 239 |
} |
| 240 |
else |
| 241 |
{ |
| 242 |
if (tcpsocketconnection_connect(socket_io_instance->tcp_socket_connection, socket_io_instance->hostname, socket_io_instance->port) != 0) |
| 243 |
{ |
| 244 |
tcpsocketconnection_destroy(socket_io_instance->tcp_socket_connection); |
| 245 |
socket_io_instance->tcp_socket_connection = NULL; |
| 246 |
result = MU_FAILURE; |
| 247 |
} |
| 248 |
else |
| 249 |
{ |
| 250 |
tcpsocketconnection_set_blocking(socket_io_instance->tcp_socket_connection, false, 0); |
| 251 |
|
| 252 |
socket_io_instance->on_bytes_received = on_bytes_received; |
| 253 |
socket_io_instance->on_bytes_received_context = on_bytes_received_context; |
| 254 |
|
| 255 |
socket_io_instance->on_io_error = on_io_error; |
| 256 |
socket_io_instance->on_io_error_context = on_io_error_context; |
| 257 |
|
| 258 |
socket_io_instance->io_state = IO_STATE_OPEN; |
| 259 |
|
| 260 |
result = 0; |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
if (on_io_open_complete != NULL) |
| 266 |
{ |
| 267 |
on_io_open_complete(on_io_open_complete_context, result == 0 ? IO_OPEN_OK : IO_OPEN_ERROR); |
| 268 |
} |
| 269 |
|
| 270 |
return result; |
| 271 |
} |
| 272 |
|
| 273 |
int socketio_close(CONCRETE_IO_HANDLE socket_io, ON_IO_CLOSE_COMPLETE on_io_close_complete, void* callback_context) |
| 274 |
{ |
| 275 |
int result = 0; |
| 276 |
|
| 277 |
if (socket_io == NULL) |
| 278 |
{ |
| 279 |
result = MU_FAILURE; |
| 280 |
} |
| 281 |
else |
| 282 |
{ |
| 283 |
SOCKET_IO_INSTANCE* socket_io_instance = (SOCKET_IO_INSTANCE*)socket_io; |
| 284 |
|
| 285 |
if ((socket_io_instance->io_state == IO_STATE_CLOSED) || |
| 286 |
(socket_io_instance->io_state == IO_STATE_CLOSING)) |
| 287 |
{ |
| 288 |
result = MU_FAILURE; |
| 289 |
} |
| 290 |
else |
| 291 |
{ |
| 292 |
tcpsocketconnection_close(socket_io_instance->tcp_socket_connection); |
| 293 |
socket_io_instance->io_state = IO_STATE_CLOSED; |
| 294 |
|
| 295 |
if (on_io_close_complete != NULL) |
| 296 |
{ |
| 297 |
on_io_close_complete(callback_context); |
| 298 |
} |
| 299 |
|
| 300 |
result = 0; |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
return result; |
| 305 |
} |
| 306 |
|
| 307 |
int socketio_send(CONCRETE_IO_HANDLE socket_io, const void* buffer, size_t size, ON_SEND_COMPLETE on_send_complete, void* callback_context) |
| 308 |
{ |
| 309 |
int result; |
| 310 |
|
| 311 |
if ((socket_io == NULL) || |
| 312 |
(buffer == NULL) || |
| 313 |
(size == 0)) |
| 314 |
{ |
| 315 |
/* Invalid arguments */ |
| 316 |
result = MU_FAILURE; |
| 317 |
} |
| 318 |
else |
| 319 |
{ |
| 320 |
SOCKET_IO_INSTANCE* socket_io_instance = (SOCKET_IO_INSTANCE*)socket_io; |
| 321 |
if (socket_io_instance->io_state != IO_STATE_OPEN) |
| 322 |
{ |
| 323 |
result = MU_FAILURE; |
| 324 |
} |
| 325 |
else |
| 326 |
{ |
| 327 |
LIST_ITEM_HANDLE first_pending_io = singlylinkedlist_get_head_item(socket_io_instance->pending_io_list); |
| 328 |
if (first_pending_io != NULL) |
| 329 |
{ |
| 330 |
if (add_pending_io(socket_io_instance, buffer, size, on_send_complete, callback_context) != 0) |
| 331 |
{ |
| 332 |
result = MU_FAILURE; |
| 333 |
} |
| 334 |
else |
| 335 |
{ |
| 336 |
result = 0; |
| 337 |
} |
| 338 |
} |
| 339 |
else |
| 340 |
{ |
| 341 |
int send_result = tcpsocketconnection_send(socket_io_instance->tcp_socket_connection, buffer, size); |
| 342 |
if ((size_t)send_result != size) |
| 343 |
{ |
| 344 |
if (send_result < 0) |
| 345 |
{ |
| 346 |
send_result = 0; |
| 347 |
} |
| 348 |
|
| 349 |
/* queue data */ |
| 350 |
if (add_pending_io(socket_io_instance, (unsigned char*)buffer + send_result, size - send_result, on_send_complete, callback_context) != 0) |
| 351 |
{ |
| 352 |
result = MU_FAILURE; |
| 353 |
} |
| 354 |
else |
| 355 |
{ |
| 356 |
result = 0; |
| 357 |
} |
| 358 |
} |
| 359 |
else |
| 360 |
{ |
| 361 |
if (on_send_complete != NULL) |
| 362 |
{ |
| 363 |
on_send_complete(callback_context, IO_SEND_OK); |
| 364 |
} |
| 365 |
|
| 366 |
result = 0; |
| 367 |
} |
| 368 |
} |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
return result; |
| 373 |
} |
| 374 |
|
| 375 |
void socketio_dowork(CONCRETE_IO_HANDLE socket_io) |
| 376 |
{ |
| 377 |
if (socket_io != NULL) |
| 378 |
{ |
| 379 |
SOCKET_IO_INSTANCE* socket_io_instance = (SOCKET_IO_INSTANCE*)socket_io; |
| 380 |
if (socket_io_instance->io_state == IO_STATE_OPEN) |
| 381 |
{ |
| 382 |
int received = 1; |
| 383 |
|
| 384 |
LIST_ITEM_HANDLE first_pending_io = singlylinkedlist_get_head_item(socket_io_instance->pending_io_list); |
| 385 |
while (first_pending_io != NULL) |
| 386 |
{ |
| 387 |
PENDING_SOCKET_IO* pending_socket_io = (PENDING_SOCKET_IO*)singlylinkedlist_item_get_value(first_pending_io); |
| 388 |
if (pending_socket_io == NULL) |
| 389 |
{ |
| 390 |
socket_io_instance->io_state = IO_STATE_ERROR; |
| 391 |
indicate_error(socket_io_instance); |
| 392 |
break; |
| 393 |
} |
| 394 |
|
| 395 |
int send_result = tcpsocketconnection_send(socket_io_instance->tcp_socket_connection, (const char*)pending_socket_io->bytes, pending_socket_io->size); |
| 396 |
if ((size_t)send_result != pending_socket_io->size) |
| 397 |
{ |
| 398 |
if (send_result < 0) |
| 399 |
{ |
| 400 |
if (send_result < UNABLE_TO_COMPLETE) |
| 401 |
{ |
| 402 |
// Bad error. Indicate as much. |
| 403 |
socket_io_instance->io_state = IO_STATE_ERROR; |
| 404 |
indicate_error(socket_io_instance); |
| 405 |
} |
| 406 |
break; |
| 407 |
} |
| 408 |
else |
| 409 |
{ |
| 410 |
/* send something, wait for the rest */ |
| 411 |
(void)memmove(pending_socket_io->bytes, pending_socket_io->bytes + send_result, pending_socket_io->size - send_result); |
| 412 |
} |
| 413 |
} |
| 414 |
else |
| 415 |
{ |
| 416 |
if (pending_socket_io->on_send_complete != NULL) |
| 417 |
{ |
| 418 |
pending_socket_io->on_send_complete(pending_socket_io->callback_context, IO_SEND_OK); |
| 419 |
} |
| 420 |
|
| 421 |
free(pending_socket_io->bytes); |
| 422 |
free(pending_socket_io); |
| 423 |
if (singlylinkedlist_remove(socket_io_instance->pending_io_list, first_pending_io) != 0) |
| 424 |
{ |
| 425 |
socket_io_instance->io_state = IO_STATE_ERROR; |
| 426 |
indicate_error(socket_io_instance); |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
first_pending_io = singlylinkedlist_get_head_item(socket_io_instance->pending_io_list); |
| 431 |
} |
| 432 |
|
| 433 |
while (received > 0) |
| 434 |
{ |
| 435 |
unsigned char* recv_bytes = malloc(MBED_XIO_RECEIVE_BUFFER_SIZE); |
| 436 |
if (recv_bytes == NULL) |
| 437 |
{ |
| 438 |
LogError("Socketio_Failure: NULL allocating input buffer."); |
| 439 |
indicate_error(socket_io_instance); |
| 440 |
} |
| 441 |
else |
| 442 |
{ |
| 443 |
received = tcpsocketconnection_receive(socket_io_instance->tcp_socket_connection, (char*)recv_bytes, MBED_XIO_RECEIVE_BUFFER_SIZE); |
| 444 |
if (received > 0) |
| 445 |
{ |
| 446 |
if (socket_io_instance->on_bytes_received != NULL) |
| 447 |
{ |
| 448 |
/* explictly ignoring here the result of the callback */ |
| 449 |
(void)socket_io_instance->on_bytes_received(socket_io_instance->on_bytes_received_context, recv_bytes, received); |
| 450 |
} |
| 451 |
} |
| 452 |
free(recv_bytes); |
| 453 |
} |
| 454 |
} |
| 455 |
} |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
int socketio_setoption(CONCRETE_IO_HANDLE socket_io, const char* optionName, const void* value) |
| 460 |
{ |
| 461 |
/* Not implementing any options */ |
| 462 |
(void)socket_io; |
| 463 |
(void)optionName; |
| 464 |
(void)value; |
| 465 |
return MU_FAILURE; |
| 466 |
} |
| 467 |
|
| 468 |
const IO_INTERFACE_DESCRIPTION* socketio_get_interface_description(void) |
| 469 |
{ |
| 470 |
return &socket_io_interface_description; |
| 471 |
} |