Debian Codegraph @

src/vendor/azure-uamqp-c/deps/azure-c-shared-utility/adapters/socketio_berkeley.c

raw
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 #ifndef _BSD_SOURCE
5 #define _BSD_SOURCE
6 #define SOCKETIO_BERKELEY_UNDEF_BSD_SOURCE
7 #endif
8
9 #define _DEFAULT_SOURCE
10 #include <net/if.h>
11 #undef _DEFAULT_SOURCE
12
13 #ifdef SOCKETIO_BERKELEY_UNDEF_BSD_SOURCE
14 #undef _BSD_SOURCE
15 #undef SOCKETIO_BERKELEY_UNDEF_BSD_SOURCE
16 #endif
17
18 #ifndef MSG_NOSIGNAL
19 #define MSG_NOSIGNAL 0
20 #endif
21
22 #include <signal.h>
23 #include <stdlib.h>
24 #include <stddef.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include "azure_c_shared_utility/socketio.h"
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/select.h>
32 #ifdef TIZENRT
33 #include <net/lwip/tcp.h>
34 #else
35 #include <netinet/tcp.h>
36 #endif
37 #include <netdb.h>
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include <errno.h>
41 #include "azure_c_shared_utility/singlylinkedlist.h"
42 #include "azure_c_shared_utility/gballoc.h"
43 #include "azure_c_shared_utility/gbnetwork.h"
44 #include "azure_c_shared_utility/optimize_size.h"
45 #include "azure_c_shared_utility/optionhandler.h"
46 #include "azure_c_shared_utility/shared_util_options.h"
47 #include "azure_c_shared_utility/xlogging.h"
48 #include "azure_c_shared_utility/const_defines.h"
49 #include "azure_c_shared_utility/dns_resolver.h"
50 #include "azure_c_shared_utility/safe_math.h"
51 #include <sys/ioctl.h>
52 #include <netinet/in.h>
53 #include <arpa/inet.h>
54 #include <sys/un.h>
55
56 #define SOCKET_SUCCESS 0
57 #define INVALID_SOCKET -1
58 #define SOCKET_SEND_FAILURE -1
59 #define MAC_ADDRESS_STRING_LENGTH 18
60
61 #ifndef IFREQ_BUFFER_SIZE
62 #define IFREQ_BUFFER_SIZE 1024
63 #endif
64
65 // connect timeout in seconds
66 #define CONNECT_TIMEOUT 10
67
68 typedef enum IO_STATE_TAG
69 {
70 IO_STATE_CLOSED,
71 IO_STATE_OPENING,
72 IO_STATE_OPEN,
73 IO_STATE_CLOSING,
74 IO_STATE_ERROR
75 } IO_STATE;
76
77 typedef struct PENDING_SOCKET_IO_TAG
78 {
79 unsigned char* bytes;
80 size_t size;
81 ON_SEND_COMPLETE on_send_complete;
82 void* callback_context;
83 SINGLYLINKEDLIST_HANDLE pending_io_list;
84 } PENDING_SOCKET_IO;
85
86 typedef struct SOCKET_IO_INSTANCE_TAG
87 {
88 int socket;
89 SOCKETIO_ADDRESS_TYPE address_type;
90 ON_BYTES_RECEIVED on_bytes_received;
91 ON_IO_ERROR on_io_error;
92 ON_IO_OPEN_COMPLETE on_io_open_complete;
93 void* on_bytes_received_context;
94 void* on_io_error_context;
95 void* on_io_open_complete_context;
96 char* hostname;
97 int port;
98 char* target_mac_address;
99 IO_STATE io_state;
100 SINGLYLINKEDLIST_HANDLE pending_io_list;
101 unsigned char recv_bytes[XIO_RECEIVE_BUFFER_SIZE];
102 DNSRESOLVER_HANDLE dns_resolver;
103 } SOCKET_IO_INSTANCE;
104
105 typedef struct NETWORK_INTERFACE_DESCRIPTION_TAG
106 {
107 char* name;
108 char* mac_address;
109 char* ip_address;
110 struct NETWORK_INTERFACE_DESCRIPTION_TAG* next;
111 } NETWORK_INTERFACE_DESCRIPTION;
112
113 /*this function will clone an option given by name and value*/
114 static void* socketio_CloneOption(const char* name, const void* value)
115 {
116 void* result;
117
118 if (name != NULL)
119 {
120 result = NULL;
121
122 if (strcmp(name, OPTION_NET_INT_MAC_ADDRESS) == 0)
123 {
124 if (value == NULL)
125 {
126 LogError("Failed cloning option %s (value is NULL)", name);
127 }
128 else
129 {
130 size_t malloc_size = safe_add_size_t(strlen((char*)value), 1);
131 malloc_size = safe_multiply_size_t(malloc_size, sizeof(char));
132 if (malloc_size == SIZE_MAX)
133 {
134 LogError("Invalid malloc size");
135 }
136 else if ((result = malloc(malloc_size)) == NULL)
137 {
138 LogError("Failed cloning option %s (malloc failed)", name);
139 }
140 else
141 {
142 strcpy((char *)result, (char *)value);
143 }
144 }
145 }
146 else
147 {
148 LogError("Cannot clone option %s (not supported)", name);
149 }
150 }
151 else
152 {
153 result = NULL;
154 }
155 return result;
156 }
157
158 /*this function destroys an option previously created*/
159 static void socketio_DestroyOption(const char* name, const void* value)
160 {
161 if (name != NULL)
162 {
163 if (strcmp(name, OPTION_NET_INT_MAC_ADDRESS) == 0 && value != NULL)
164 {
165 free((void*)value);
166 }
167 }
168 }
169
170 static OPTIONHANDLER_HANDLE socketio_retrieveoptions(CONCRETE_IO_HANDLE handle)
171 {
172 OPTIONHANDLER_HANDLE result;
173
174 if (handle == NULL)
175 {
176 LogError("failed retrieving options (handle is NULL)");
177 result = NULL;
178 }
179 else
180 {
181 SOCKET_IO_INSTANCE* socket_io_instance = (SOCKET_IO_INSTANCE*)handle;
182
183 result = OptionHandler_Create(socketio_CloneOption, socketio_DestroyOption, socketio_setoption);
184 if (result == NULL)
185 {
186 LogError("unable to OptionHandler_Create");
187 }
188 else if (socket_io_instance->target_mac_address != NULL &&
189 OptionHandler_AddOption(result, OPTION_NET_INT_MAC_ADDRESS, socket_io_instance->target_mac_address) != OPTIONHANDLER_OK)
190 {
191 LogError("failed retrieving options (failed adding net_interface_mac_address)");
192 OptionHandler_Destroy(result);
193 result = NULL;
194 }
195 }
196
197 return result;
198 }
199
200 static const IO_INTERFACE_DESCRIPTION socket_io_interface_description =
201 {
202 socketio_retrieveoptions,
203 socketio_create,
204 socketio_destroy,
205 socketio_open,
206 socketio_close,
207 socketio_send,
208 socketio_dowork,
209 socketio_setoption
210 };
211
212 static void indicate_error(SOCKET_IO_INSTANCE* socket_io_instance)
213 {
214 socket_io_instance->io_state = IO_STATE_ERROR;
215 if (socket_io_instance->on_io_error != NULL)
216 {
217 socket_io_instance->on_io_error(socket_io_instance->on_io_error_context);
218 }
219 }
220
221 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)
222 {
223 int result;
224 PENDING_SOCKET_IO* pending_socket_io = (PENDING_SOCKET_IO*)malloc(sizeof(PENDING_SOCKET_IO));
225 if (pending_socket_io == NULL)
226 {
227 result = MU_FAILURE;
228 }
229 else
230 {
231 pending_socket_io->bytes = (unsigned char*)malloc(size);
232 if (pending_socket_io->bytes == NULL)
233 {
234 LogError("Allocation Failure: Unable to allocate pending list.");
235 free(pending_socket_io);
236 result = MU_FAILURE;
237 }
238 else
239 {
240 pending_socket_io->size = size;
241 pending_socket_io->on_send_complete = on_send_complete;
242 pending_socket_io->callback_context = callback_context;
243 pending_socket_io->pending_io_list = socket_io_instance->pending_io_list;
244 (void)memcpy(pending_socket_io->bytes, buffer, size);
245
246 if (singlylinkedlist_add(socket_io_instance->pending_io_list, pending_socket_io) == NULL)
247 {
248 LogError("Failure: Unable to add socket to pending list.");
249 free(pending_socket_io->bytes);
250 free(pending_socket_io);
251 result = MU_FAILURE;
252 }
253 else
254 {
255 result = 0;
256 }
257 }
258 }
259 return result;
260 }
261
262 static STATIC_VAR_UNUSED void signal_callback(int signum)
263 {
264 AZURE_UNREFERENCED_PARAMETER(signum);
265 LogError("Socket received signal %d.", signum);
266 }
267
268 static int lookup_address(SOCKET_IO_INSTANCE* socket_io_instance)
269 {
270 int result = 0;
271
272 if (socket_io_instance->address_type == ADDRESS_TYPE_IP)
273 {
274 if (!dns_resolver_is_lookup_complete(socket_io_instance->dns_resolver))
275 {
276 socket_io_instance->io_state = IO_STATE_OPENING;
277 }
278 else
279 {
280 socket_io_instance->io_state = IO_STATE_OPEN;
281 }
282 }
283 else //ADDRESS_TYPE_DOMAIN_SOCKET
284 {
285 socket_io_instance->io_state = IO_STATE_OPEN;
286 }
287
288 return result;
289 }
290
291 #ifndef __APPLE__
292 static void destroy_network_interface_descriptions(NETWORK_INTERFACE_DESCRIPTION* nid)
293 {
294 if (nid != NULL)
295 {
296 if (nid->next != NULL)
297 {
298 destroy_network_interface_descriptions(nid->next);
299 }
300
301 if (nid->name != NULL)
302 {
303 free(nid->name);
304 }
305
306 if (nid->mac_address != NULL)
307 {
308 free(nid->mac_address);
309 }
310
311 if (nid->ip_address != NULL)
312 {
313 free(nid->ip_address);
314 }
315
316 free(nid);
317 }
318 }
319
320 static NETWORK_INTERFACE_DESCRIPTION* create_network_interface_description(struct ifreq *ifr, NETWORK_INTERFACE_DESCRIPTION* previous_nid)
321 {
322 NETWORK_INTERFACE_DESCRIPTION* result;
323 size_t malloc_size;
324
325 if ((result = (NETWORK_INTERFACE_DESCRIPTION*)malloc(sizeof(NETWORK_INTERFACE_DESCRIPTION))) == NULL)
326 {
327 LogError("Failed allocating NETWORK_INTERFACE_DESCRIPTION");
328 }
329 else if ((malloc_size = safe_multiply_size_t(safe_add_size_t(strlen(ifr->ifr_name), 1), sizeof(char))) == SIZE_MAX)
330 {
331 LogError("invalid malloc size");
332 destroy_network_interface_descriptions(result);
333 result = NULL;
334 }
335 else if ((result->name = (char*)malloc(malloc_size)) == NULL)
336 {
337 LogError("failed setting interface description name (malloc failed)");
338 destroy_network_interface_descriptions(result);
339 result = NULL;
340 }
341 else
342 {
343 strcpy(result->name, ifr->ifr_name);
344
345 char* ip_address;
346 unsigned char* mac = (unsigned char*)ifr->ifr_hwaddr.sa_data;
347 size_t malloc_size = safe_multiply_size_t(sizeof(char), MAC_ADDRESS_STRING_LENGTH);
348
349 if (malloc_size == SIZE_MAX ||
350 (result->mac_address = (char*)malloc(malloc_size)) == NULL)
351 {
352 LogError("failed formatting mac address (malloc failed) size:%zu", malloc_size);
353 destroy_network_interface_descriptions(result);
354 result = NULL;
355 }
356 else if (sprintf(result->mac_address, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]) <= 0)
357 {
358 LogError("failed formatting mac address (sprintf failed)");
359 destroy_network_interface_descriptions(result);
360 result = NULL;
361 }
362 else if ((ip_address = inet_ntoa(((struct sockaddr_in*)&ifr->ifr_addr)->sin_addr)) == NULL)
363 {
364 LogError("failed setting the ip address (inet_ntoa failed)");
365 destroy_network_interface_descriptions(result);
366 result = NULL;
367 }
368 else if ((malloc_size = safe_multiply_size_t(safe_add_size_t(strlen(ip_address), 1), sizeof(char))) == SIZE_MAX)
369 {
370 LogError("invalid malloc size");
371 destroy_network_interface_descriptions(result);
372 result = NULL;
373 }
374 else if ((result->ip_address = (char*)malloc(malloc_size)) == NULL)
375 {
376 LogError("failed setting the ip address (malloc failed)");
377 destroy_network_interface_descriptions(result);
378 result = NULL;
379 }
380 else
381 {
382 strcpy(result->ip_address, ip_address);
383 result->next = NULL;
384
385 if (previous_nid != NULL)
386 {
387 previous_nid->next = result;
388 }
389 }
390 }
391
392 return result;
393 }
394
395 static int get_network_interface_descriptions(int socket, NETWORK_INTERFACE_DESCRIPTION** nid)
396 {
397 int result;
398
399 struct ifreq ifr;
400 struct ifconf ifc;
401 char buf[IFREQ_BUFFER_SIZE];
402
403 ifc.ifc_len = sizeof(buf);
404 ifc.ifc_buf = buf;
405
406 if (ioctl(socket, SIOCGIFCONF, &ifc) == -1)
407 {
408 LogError("ioctl failed querying socket (SIOCGIFCONF, errno=%d)", errno);
409 result = MU_FAILURE;
410 }
411 else
412 {
413 NETWORK_INTERFACE_DESCRIPTION* root_nid = NULL;
414 NETWORK_INTERFACE_DESCRIPTION* new_nid = NULL;
415
416 struct ifreq* it = ifc.ifc_req;
417 const struct ifreq* const end = it + (ifc.ifc_len / sizeof(struct ifreq));
418
419 result = 0;
420
421 for (; it != end; ++it)
422 {
423 strcpy(ifr.ifr_name, it->ifr_name);
424
425 if (ioctl(socket, SIOCGIFFLAGS, &ifr) != 0)
426 {
427 LogError("ioctl failed querying socket (SIOCGIFFLAGS, errno=%d)", errno);
428 result = MU_FAILURE;
429 break;
430 }
431 else if (ioctl(socket, SIOCGIFHWADDR, &ifr) != 0)
432 {
433 LogError("ioctl failed querying socket (SIOCGIFHWADDR, errno=%d)", errno);
434 result = MU_FAILURE;
435 break;
436 }
437 else if (ioctl(socket, SIOCGIFADDR, &ifr) != 0)
438 {
439 LogError("ioctl failed querying socket (SIOCGIFADDR, errno=%d)", errno);
440 result = MU_FAILURE;
441 break;
442 }
443 else if ((new_nid = create_network_interface_description(&ifr, new_nid)) == NULL)
444 {
445 LogError("Failed creating network interface description");
446 result = MU_FAILURE;
447 break;
448 }
449 else if (root_nid == NULL)
450 {
451 root_nid = new_nid;
452 }
453 }
454
455 if (result == 0)
456 {
457 *nid = root_nid;
458 }
459 else
460 {
461 destroy_network_interface_descriptions(root_nid);
462 }
463 }
464
465 return result;
466 }
467
468 static int set_target_network_interface(int socket, char* mac_address)
469 {
470 int result;
471 NETWORK_INTERFACE_DESCRIPTION* nid;
472
473 if (get_network_interface_descriptions(socket, &nid) != 0)
474 {
475 LogError("Failed getting network interface descriptions");
476 result = MU_FAILURE;
477 }
478 else
479 {
480 NETWORK_INTERFACE_DESCRIPTION* current_nid = nid;
481
482 while(current_nid != NULL)
483 {
484 if (strcmp(mac_address, current_nid->mac_address) == 0)
485 {
486 break;
487 }
488
489 current_nid = current_nid->next;
490 }
491
492 if (current_nid == NULL)
493 {
494 LogError("Did not find a network interface matching MAC ADDRESS");
495 result = MU_FAILURE;
496 }
497 else if (setsockopt(socket, SOL_SOCKET, SO_BINDTODEVICE, current_nid->name, strlen(current_nid->name)) != 0)
498 {
499 LogError("setsockopt failed (%d)", errno);
500 result = MU_FAILURE;
501 }
502 else
503 {
504 result = 0;
505 }
506
507 destroy_network_interface_descriptions(nid);
508 }
509
510 return result;
511 }
512 #endif //__APPLE__
513
514 static int initiate_socket_connection(SOCKET_IO_INSTANCE* socket_io_instance)
515 {
516 int result;
517 int flags;
518 struct addrinfo* addr = NULL;
519 struct sockaddr_un addrInfoUn;
520
521 if(socket_io_instance->address_type == ADDRESS_TYPE_IP)
522 {
523 if(!dns_resolver_is_lookup_complete(socket_io_instance->dns_resolver))
524 {
525 LogError("DNS did not resolve hostname IP address. Hostname:%s", socket_io_instance->hostname);
526 result = MU_FAILURE;
527 }
528 else
529 {
530 addr = dns_resolver_get_addrInfo(socket_io_instance->dns_resolver);
531
532 if (addr == NULL)
533 {
534 LogError("DNS resolution failed. Hostname:%s", socket_io_instance->hostname);
535 result = MU_FAILURE;
536 }
537 else
538 {
539 result = 0;
540 }
541 }
542 }
543 else
544 {
545 if (socket_io_instance->hostname != NULL)
546 {
547 size_t hostname_len = strlen(socket_io_instance->hostname);
548 if (hostname_len + 1 > sizeof(addrInfoUn.sun_path))
549 {
550 LogError("Hostname %s is too long for a unix socket (max len = %lu)", socket_io_instance->hostname, (unsigned long)sizeof(addrInfoUn.sun_path));
551 result = MU_FAILURE;
552 }
553 else
554 {
555 memset(&addrInfoUn, 0, sizeof(addrInfoUn));
556 addrInfoUn.sun_family = AF_UNIX;
557 // No need to add NULL terminator due to the above memset
558 (void)memcpy(addrInfoUn.sun_path, socket_io_instance->hostname, hostname_len);
559 result = 0;
560 }
561 }
562 else
563 {
564 LogError("Hostname is NULL");
565 result = MU_FAILURE;
566 }
567 }
568
569 if(result == 0)
570 {
571 if (socket_io_instance->address_type == ADDRESS_TYPE_IP)
572 {
573 socket_io_instance->socket = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
574 }
575 else
576 {
577 socket_io_instance->socket = socket(AF_UNIX, SOCK_STREAM, 0);
578 }
579
580 if (socket_io_instance->socket < SOCKET_SUCCESS)
581 {
582 LogError("Failure: socket create failure %d.", socket_io_instance->socket);
583 result = MU_FAILURE;
584 }
585 #ifndef __APPLE__
586 else if (socket_io_instance->target_mac_address != NULL &&
587 set_target_network_interface(socket_io_instance->socket, socket_io_instance->target_mac_address) != 0)
588 {
589 LogError("Failure: failed selecting target network interface (MACADDR=%s).", socket_io_instance->target_mac_address);
590 result = MU_FAILURE;
591 }
592 #endif //__APPLE__
593 else if ((-1 == (flags = fcntl(socket_io_instance->socket, F_GETFL, 0))) ||
594 (fcntl(socket_io_instance->socket, F_SETFL, flags | O_NONBLOCK) == -1))
595 {
596 LogError("Failure: fcntl failure.");
597 result = MU_FAILURE;
598 }
599 else
600 {
601 if (socket_io_instance->address_type == ADDRESS_TYPE_IP)
602 {
603 result = connect(socket_io_instance->socket, addr->ai_addr, addr->ai_addrlen);
604 }
605 else
606 {
607 result = connect(socket_io_instance->socket, (struct sockaddr *)&addrInfoUn, sizeof(addrInfoUn));
608 }
609
610 if ((result != 0) && (errno != EINPROGRESS))
611 {
612 LogError("Failure: connect failure %d.", errno);
613 result = MU_FAILURE;
614 }
615 else // result == 0 || errno == EINPROGRESS
616 {
617 // Async connect will return -1.
618 result = 0;
619 if (socket_io_instance->on_io_open_complete != NULL)
620 {
621 socket_io_instance->on_io_open_complete(socket_io_instance->on_io_open_complete_context, IO_OPEN_OK /*: IO_OPEN_ERROR*/);
622 }
623 }
624 }
625
626 if (result != 0)
627 {
628 if (socket_io_instance->socket >= SOCKET_SUCCESS)
629 {
630 close(socket_io_instance->socket);
631 }
632 socket_io_instance->socket = INVALID_SOCKET;
633 }
634 }
635
636 return result;
637 }
638
639 static int lookup_address_and_initiate_socket_connection(SOCKET_IO_INSTANCE* socket_io_instance)
640 {
641 int result;
642
643 result = lookup_address(socket_io_instance);
644
645 if(socket_io_instance->io_state == IO_STATE_OPEN)
646 {
647 if (result == 0)
648 {
649 result = initiate_socket_connection(socket_io_instance);
650 }
651 }
652
653 return result;
654 }
655
656 static int wait_for_socket_connection(SOCKET_IO_INSTANCE* socket_io_instance)
657 {
658 int result;
659 int err;
660 int retval;
661 int select_errno = 0;
662
663 fd_set fdset;
664 struct timeval tv;
665
666 FD_ZERO(&fdset);
667 FD_SET(socket_io_instance->socket, &fdset);
668 tv.tv_sec = CONNECT_TIMEOUT;
669 tv.tv_usec = 0;
670
671 do
672 {
673 retval = select(socket_io_instance->socket + 1, NULL, &fdset, NULL, &tv);
674
675 if (retval < 0)
676 {
677 select_errno = errno;
678 }
679 } while (retval < 0 && select_errno == EINTR);
680
681 if (retval != 1)
682 {
683 LogError("Failure: select failure.");
684 result = MU_FAILURE;
685 }
686 else
687 {
688 int so_error = 0;
689 socklen_t len = sizeof(so_error);
690 err = getsockopt(socket_io_instance->socket, SOL_SOCKET, SO_ERROR, &so_error, &len);
691 if (err != 0)
692 {
693 LogError("Failure: getsockopt failure %d.", errno);
694 result = MU_FAILURE;
695 }
696 else if (so_error != 0)
697 {
698 err = so_error;
699 LogError("Failure: connect failure %d.", so_error);
700 result = MU_FAILURE;
701 }
702 else
703 {
704 result = 0;
705 }
706 }
707
708 if (result != 0)
709 {
710 if (socket_io_instance->socket >= SOCKET_SUCCESS)
711 {
712 close(socket_io_instance->socket);
713 }
714 socket_io_instance->socket = INVALID_SOCKET;
715 }
716
717 return result;
718 }
719
720 static void destroy_socket_io_instance(SOCKET_IO_INSTANCE* instance)
721 {
722 if (instance->dns_resolver != NULL)
723 {
724 dns_resolver_destroy(instance->dns_resolver);
725 }
726
727 free(instance->hostname);
728 free(instance->target_mac_address);
729
730 if (instance->pending_io_list != NULL)
731 {
732 singlylinkedlist_destroy(instance->pending_io_list);
733 }
734
735 free(instance);
736 }
737
738 CONCRETE_IO_HANDLE socketio_create(void* io_create_parameters)
739 {
740 SOCKETIO_CONFIG* socket_io_config = io_create_parameters;
741 SOCKET_IO_INSTANCE* result;
742
743 if (socket_io_config == NULL)
744 {
745 LogError("Invalid argument: socket_io_config is NULL");
746 result = NULL;
747 }
748 else
749 {
750 result = malloc(sizeof(SOCKET_IO_INSTANCE));
751 if (result != NULL)
752 {
753 (void)memset(result, 0, sizeof(SOCKET_IO_INSTANCE));
754
755 result->address_type = ADDRESS_TYPE_IP;
756 result->pending_io_list = singlylinkedlist_create();
757 if (result->pending_io_list == NULL)
758 {
759 LogError("Failure: singlylinkedlist_create unable to create pending list.");
760 destroy_socket_io_instance(result);
761 result = NULL;
762 }
763 else
764 {
765 if (socket_io_config->hostname != NULL)
766 {
767 size_t malloc_size = safe_add_size_t(strlen(socket_io_config->hostname), 1);
768 if (malloc_size == SIZE_MAX)
769 {
770 LogError("invalid malloc size");
771 result->hostname = NULL;
772 }
773 else
774 {
775 result->hostname = (char*)malloc(malloc_size);
776 if (result->hostname != NULL)
777 {
778 (void)strcpy(result->hostname, socket_io_config->hostname);
779 }
780 }
781
782 result->socket = INVALID_SOCKET;
783 }
784 else
785 {
786 result->hostname = NULL;
787 result->socket = *((int*)socket_io_config->accepted_socket);
788 }
789
790 if ((result->hostname == NULL) && (result->socket == INVALID_SOCKET))
791 {
792 LogError("Failure: hostname == NULL and socket is invalid.");
793 destroy_socket_io_instance(result);
794 result = NULL;
795 }
796 else
797 {
798 result->port = socket_io_config->port;
799 result->on_io_open_complete = NULL;
800 result->dns_resolver = dns_resolver_create(result->hostname, socket_io_config->port, NULL);
801 result->target_mac_address = NULL;
802 result->on_bytes_received = NULL;
803 result->on_io_error = NULL;
804 result->on_bytes_received_context = NULL;
805 result->on_io_error_context = NULL;
806 result->io_state = IO_STATE_CLOSED;
807 }
808 }
809 }
810 else
811 {
812 LogError("Allocation Failure: SOCKET_IO_INSTANCE");
813 }
814 }
815
816 return result;
817 }
818
819 void socketio_destroy(CONCRETE_IO_HANDLE socket_io)
820 {
821 if (socket_io != NULL)
822 {
823 SOCKET_IO_INSTANCE* socket_io_instance = (SOCKET_IO_INSTANCE*)socket_io;
824 /* we cannot do much if the close fails, so just ignore the result */
825 if (socket_io_instance->socket != INVALID_SOCKET)
826 {
827 close(socket_io_instance->socket);
828 }
829
830 /* clear allpending IOs */
831 LIST_ITEM_HANDLE first_pending_io = singlylinkedlist_get_head_item(socket_io_instance->pending_io_list);
832 while (first_pending_io != NULL)
833 {
834 PENDING_SOCKET_IO* pending_socket_io = (PENDING_SOCKET_IO*)singlylinkedlist_item_get_value(first_pending_io);
835 if (pending_socket_io != NULL)
836 {
837 free(pending_socket_io->bytes);
838 free(pending_socket_io);
839 }
840
841 (void)singlylinkedlist_remove(socket_io_instance->pending_io_list, first_pending_io);
842 first_pending_io = singlylinkedlist_get_head_item(socket_io_instance->pending_io_list);
843 }
844
845 destroy_socket_io_instance(socket_io_instance);
846 }
847 }
848
849 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)
850 {
851 int result;
852
853 SOCKET_IO_INSTANCE* socket_io_instance = (SOCKET_IO_INSTANCE*)socket_io;
854 if (socket_io == NULL)
855 {
856 LogError("Invalid argument: SOCKET_IO_INSTANCE is NULL");
857 result = MU_FAILURE;
858 }
859 else
860 {
861 if (socket_io_instance->io_state != IO_STATE_CLOSED)
862 {
863 LogError("Failure: socket state is not closed.");
864 result = MU_FAILURE;
865 }
866 else if (socket_io_instance->socket != INVALID_SOCKET)
867 {
868 // Opening an accepted socket
869 socket_io_instance->on_bytes_received_context = on_bytes_received_context;
870 socket_io_instance->on_bytes_received = on_bytes_received;
871 socket_io_instance->on_io_error = on_io_error;
872 socket_io_instance->on_io_error_context = on_io_error_context;
873
874 socket_io_instance->io_state = IO_STATE_OPEN;
875
876 result = 0;
877 }
878 else
879 {
880 if ((result = lookup_address_and_initiate_socket_connection(socket_io_instance)) != 0)
881 {
882 LogError("lookup_address_and_connect_socket failed");
883 }
884 else if ((socket_io_instance->io_state == IO_STATE_OPEN) && (result = wait_for_socket_connection(socket_io_instance)) != 0)
885 {
886 LogError("wait_for_socket_connection failed");
887 }
888 else
889 {
890 socket_io_instance->on_bytes_received = on_bytes_received;
891 socket_io_instance->on_bytes_received_context = on_bytes_received_context;
892
893 socket_io_instance->on_io_error = on_io_error;
894 socket_io_instance->on_io_error_context = on_io_error_context;
895
896 socket_io_instance->on_io_open_complete = on_io_open_complete;
897 socket_io_instance->on_io_open_complete_context = on_io_open_complete_context;
898 }
899 }
900 }
901
902 if (socket_io_instance->io_state != IO_STATE_OPENING)
903 {
904 if (on_io_open_complete != NULL)
905 {
906 on_io_open_complete(on_io_open_complete_context, result == 0 ? IO_OPEN_OK : IO_OPEN_ERROR);
907 }
908 }
909
910 return result;
911 }
912
913 int socketio_close(CONCRETE_IO_HANDLE socket_io, ON_IO_CLOSE_COMPLETE on_io_close_complete, void* callback_context)
914 {
915 int result = 0;
916
917 if (socket_io == NULL)
918 {
919 result = MU_FAILURE;
920 }
921 else
922 {
923 SOCKET_IO_INSTANCE* socket_io_instance = (SOCKET_IO_INSTANCE*)socket_io;
924 if ((socket_io_instance->io_state != IO_STATE_CLOSED) && (socket_io_instance->io_state != IO_STATE_CLOSING))
925 {
926 // Only close if the socket isn't already in the closed or closing state
927 (void)shutdown(socket_io_instance->socket, SHUT_RDWR);
928 close(socket_io_instance->socket);
929 socket_io_instance->socket = INVALID_SOCKET;
930 socket_io_instance->io_state = IO_STATE_CLOSED;
931 }
932
933 if (on_io_close_complete != NULL)
934 {
935 on_io_close_complete(callback_context);
936 }
937
938 result = 0;
939 }
940
941 return result;
942 }
943
944 int socketio_send(CONCRETE_IO_HANDLE socket_io, const void* buffer, size_t size, ON_SEND_COMPLETE on_send_complete, void* callback_context)
945 {
946 int result;
947
948 if ((socket_io == NULL) ||
949 (buffer == NULL) ||
950 (size == 0))
951 {
952 /* Invalid arguments */
953 LogError("Invalid argument: send given invalid parameter");
954 result = MU_FAILURE;
955 }
956 else
957 {
958 SOCKET_IO_INSTANCE* socket_io_instance = (SOCKET_IO_INSTANCE*)socket_io;
959 if (socket_io_instance->io_state != IO_STATE_OPEN)
960 {
961 LogError("Failure: socket state is not opened.");
962 result = MU_FAILURE;
963 }
964 else
965 {
966 LIST_ITEM_HANDLE first_pending_io = singlylinkedlist_get_head_item(socket_io_instance->pending_io_list);
967 if (first_pending_io != NULL)
968 {
969 if (add_pending_io(socket_io_instance, buffer, size, on_send_complete, callback_context) != 0)
970 {
971 LogError("Failure: add_pending_io failed.");
972 result = MU_FAILURE;
973 }
974 else
975 {
976 result = 0;
977 }
978 }
979 else
980 {
981 signal(SIGPIPE, SIG_IGN);
982
983 ssize_t send_result = send(socket_io_instance->socket, buffer, size, MSG_NOSIGNAL);
984 if ((size_t)send_result != size)
985 {
986 if (send_result == SOCKET_SEND_FAILURE && errno != EAGAIN && errno != ENOBUFS)
987 {
988 LogError("Failure: sending socket failed. errno=%d (%s).", errno, strerror(errno));
989 result = MU_FAILURE;
990 }
991 else
992 {
993 /*send says "come back later" with EAGAIN, ENOBUFS - likely the socket buffer cannot accept more data*/
994 /* queue data */
995 size_t bytes_sent = (send_result < 0 ? 0 : send_result);
996
997 if (add_pending_io(socket_io_instance, (const unsigned char*)buffer + bytes_sent, size - bytes_sent, on_send_complete, callback_context) != 0)
998 {
999 LogError("Failure: add_pending_io failed.");
1000 result = MU_FAILURE;
1001 }
1002 else
1003 {
1004 result = 0;
1005 }
1006 }
1007 }
1008 else
1009 {
1010 if (on_send_complete != NULL)
1011 {
1012 on_send_complete(callback_context, IO_SEND_OK);
1013 }
1014
1015 result = 0;
1016 }
1017 }
1018 }
1019 }
1020
1021 return result;
1022 }
1023
1024 void socketio_dowork(CONCRETE_IO_HANDLE socket_io)
1025 {
1026 if (socket_io != NULL)
1027 {
1028 SOCKET_IO_INSTANCE* socket_io_instance = (SOCKET_IO_INSTANCE*)socket_io;
1029 signal(SIGPIPE, SIG_IGN);
1030
1031 if (socket_io_instance->io_state == IO_STATE_OPEN)
1032 {
1033 LIST_ITEM_HANDLE first_pending_io = singlylinkedlist_get_head_item(socket_io_instance->pending_io_list);
1034 while (first_pending_io != NULL)
1035 {
1036 PENDING_SOCKET_IO* pending_socket_io = (PENDING_SOCKET_IO*)singlylinkedlist_item_get_value(first_pending_io);
1037 if (pending_socket_io == NULL)
1038 {
1039 indicate_error(socket_io_instance);
1040 LogError("Failure: retrieving socket from list");
1041 break;
1042 }
1043
1044 ssize_t send_result = send(socket_io_instance->socket, pending_socket_io->bytes, pending_socket_io->size, MSG_NOSIGNAL);
1045 if ((send_result < 0) || ((size_t)send_result != pending_socket_io->size))
1046 {
1047 if (send_result == INVALID_SOCKET)
1048 {
1049 if (errno == EAGAIN || errno == ENOBUFS) /*send says "come back later" with EAGAIN, ENOBUFS - likely the socket buffer cannot accept more data*/
1050 {
1051 /*do nothing until next dowork */
1052 break;
1053 }
1054 else
1055 {
1056 free(pending_socket_io->bytes);
1057 free(pending_socket_io);
1058 pending_socket_io = NULL;
1059 (void)singlylinkedlist_remove(socket_io_instance->pending_io_list, first_pending_io);
1060
1061 LogError("Failure: sending Socket information. errno=%d (%s).", errno, strerror(errno));
1062 indicate_error(socket_io_instance);
1063 }
1064 }
1065 else
1066 {
1067 /* simply wait until next dowork */
1068 (void)memmove(pending_socket_io->bytes, pending_socket_io->bytes + send_result, pending_socket_io->size - send_result);
1069 pending_socket_io->size -= send_result;
1070 break;
1071 }
1072 }
1073 else
1074 {
1075 if (pending_socket_io->on_send_complete != NULL)
1076 {
1077 pending_socket_io->on_send_complete(pending_socket_io->callback_context, IO_SEND_OK);
1078 }
1079
1080 free(pending_socket_io->bytes);
1081 free(pending_socket_io);
1082 pending_socket_io = NULL;
1083 if (singlylinkedlist_remove(socket_io_instance->pending_io_list, first_pending_io) != 0)
1084 {
1085 indicate_error(socket_io_instance);
1086 LogError("Failure: unable to remove socket from list");
1087 }
1088 }
1089
1090 first_pending_io = singlylinkedlist_get_head_item(socket_io_instance->pending_io_list);
1091 }
1092
1093 if (socket_io_instance->io_state == IO_STATE_OPEN)
1094 {
1095 ssize_t received = 0;
1096 do
1097 {
1098 received = recv(socket_io_instance->socket, socket_io_instance->recv_bytes, XIO_RECEIVE_BUFFER_SIZE, MSG_NOSIGNAL);
1099 if (received > 0)
1100 {
1101 if (socket_io_instance->on_bytes_received != NULL)
1102 {
1103 /* Explicitly ignoring here the result of the callback */
1104 (void)socket_io_instance->on_bytes_received(socket_io_instance->on_bytes_received_context, socket_io_instance->recv_bytes, received);
1105 }
1106 }
1107 else if (received == 0)
1108 {
1109 // Do not log error here due to this is probably the socket being closed on the other end
1110 indicate_error(socket_io_instance);
1111 }
1112 else if (received < 0 && errno != EAGAIN)
1113 {
1114 LogError("Socketio_Failure: Receiving data from endpoint: errno=%d.", errno);
1115 indicate_error(socket_io_instance);
1116 }
1117
1118 } while (received > 0 && socket_io_instance->io_state == IO_STATE_OPEN);
1119 }
1120 }
1121 else
1122 {
1123 if (socket_io_instance->io_state == IO_STATE_OPENING)
1124 {
1125 if(lookup_address(socket_io_instance) != 0)
1126 {
1127 LogError("Socketio_Failure: lookup address failed");
1128 indicate_error(socket_io_instance);
1129 }
1130 else
1131 {
1132 if(socket_io_instance->io_state == IO_STATE_OPEN)
1133 {
1134 if (initiate_socket_connection(socket_io_instance) != 0)
1135 {
1136 LogError("Socketio_Failure: initiate_socket_connection failed");
1137 indicate_error(socket_io_instance);
1138 }
1139 else if (wait_for_socket_connection(socket_io_instance) != 0)
1140 {
1141 LogError("Socketio_Failure: wait_for_socket_connection failed");
1142 indicate_error(socket_io_instance);
1143 }
1144 }
1145 }
1146
1147 }
1148 }
1149 }
1150 }
1151
1152 // Edison is missing this from netinet/tcp.h, but this code still works if we manually define it.
1153 #ifndef SOL_TCP
1154 #define SOL_TCP 6
1155 #endif
1156
1157 #ifndef __APPLE__
1158 static void strtoup(char* str)
1159 {
1160 if (str != NULL)
1161 {
1162 while (*str != '\0')
1163 {
1164 if (isalpha((int)*str) && islower((int)*str))
1165 {
1166 *str = (char)toupper((int)*str);
1167 }
1168 str++;
1169 }
1170 }
1171 }
1172 #endif // __APPLE__
1173
1174 static int socketio_setaddresstype_option(SOCKET_IO_INSTANCE* socket_io_instance, const char* addressType)
1175 {
1176 int result;
1177
1178 if (socket_io_instance->io_state != IO_STATE_CLOSED)
1179 {
1180 LogError("Socket's type can only be changed when in state 'IO_STATE_CLOSED'. Current state=%d", socket_io_instance->io_state);
1181 result = MU_FAILURE;
1182 }
1183 else if (strcmp(addressType, OPTION_ADDRESS_TYPE_DOMAIN_SOCKET) == 0)
1184 {
1185 socket_io_instance->address_type = ADDRESS_TYPE_DOMAIN_SOCKET;
1186 result = 0;
1187 }
1188 else if (strcmp(addressType, OPTION_ADDRESS_TYPE_IP_SOCKET) == 0)
1189 {
1190 socket_io_instance->address_type = ADDRESS_TYPE_IP;
1191 result = 0;
1192 }
1193 else
1194 {
1195 LogError("Address type %s is not supported", addressType);
1196 result = MU_FAILURE;
1197 }
1198
1199 return result;
1200 }
1201
1202 int socketio_setoption(CONCRETE_IO_HANDLE socket_io, const char* optionName, const void* value)
1203 {
1204 int result;
1205
1206 if (socket_io == NULL ||
1207 optionName == NULL ||
1208 value == NULL)
1209 {
1210 result = MU_FAILURE;
1211 }
1212 else
1213 {
1214 SOCKET_IO_INSTANCE* socket_io_instance = (SOCKET_IO_INSTANCE*)socket_io;
1215
1216 if (strcmp(optionName, "tcp_keepalive") == 0)
1217 {
1218 result = setsockopt(socket_io_instance->socket, SOL_SOCKET, SO_KEEPALIVE, value, sizeof(int));
1219 if (result == -1) result = errno;
1220 }
1221 else if (strcmp(optionName, "tcp_keepalive_time") == 0)
1222 {
1223 #ifdef __APPLE__
1224 result = setsockopt(socket_io_instance->socket, IPPROTO_TCP, TCP_KEEPALIVE, value, sizeof(int));
1225 #else
1226 result = setsockopt(socket_io_instance->socket, SOL_TCP, TCP_KEEPIDLE, value, sizeof(int));
1227 #endif
1228 if (result == -1) result = errno;
1229 }
1230 else if (strcmp(optionName, "tcp_keepalive_interval") == 0)
1231 {
1232 result = setsockopt(socket_io_instance->socket, SOL_TCP, TCP_KEEPINTVL, value, sizeof(int));
1233 if (result == -1) result = errno;
1234 }
1235 else if (strcmp(optionName, OPTION_NET_INT_MAC_ADDRESS) == 0)
1236 {
1237 #ifdef __APPLE__
1238 LogError("option not supported.");
1239 result = MU_FAILURE;
1240 #else
1241 size_t malloc_size;
1242 if (strlen(value) == 0)
1243 {
1244 LogError("option value must be a valid mac address");
1245 result = MU_FAILURE;
1246 }
1247 else if ((malloc_size = safe_multiply_size_t(safe_add_size_t(strlen(value), 1), sizeof(char))) == SIZE_MAX)
1248 {
1249 LogError("invalid malloc size");
1250 result = MU_FAILURE;
1251 socket_io_instance->target_mac_address = NULL;
1252 }
1253 else if ((socket_io_instance->target_mac_address = (char*)malloc(malloc_size)) == NULL)
1254 {
1255 LogError("failed setting net_interface_mac_address option (malloc failed)");
1256 result = MU_FAILURE;
1257 }
1258 else
1259 {
1260 strcpy(socket_io_instance->target_mac_address, value);
1261 strtoup(socket_io_instance->target_mac_address);
1262 result = 0;
1263 }
1264 #endif
1265 }
1266 else if (strcmp(optionName, OPTION_ADDRESS_TYPE) == 0)
1267 {
1268 result = socketio_setaddresstype_option(socket_io_instance, (const char*)value);
1269 }
1270 else
1271 {
1272 result = MU_FAILURE;
1273 }
1274 }
1275
1276 return result;
1277 }
1278
1279 const IO_INTERFACE_DESCRIPTION* socketio_get_interface_description(void)
1280 {
1281 return &socket_io_interface_description;
1282 }
1283