diff options
Diffstat (limited to 'src/lib/sockets.c')
| -rw-r--r-- | src/lib/sockets.c | 79 |
1 files changed, 54 insertions, 25 deletions
diff --git a/src/lib/sockets.c b/src/lib/sockets.c index 6e7d4ad5..13219db0 100644 --- a/src/lib/sockets.c +++ b/src/lib/sockets.c @@ -1,10 +1,10 @@ /* - * Ouroboros - Copyright (C) 2016 - 2018 + * Ouroboros - Copyright (C) 2016 - 2024 * * The sockets layer to communicate between daemons * - * Dimitri Staessens <dimitri.staessens@ugent.be> - * Sander Vrijders <sander.vrijders@ugent.be> + * Dimitri Staessens <dimitri@ouroboros.rocks> + * Sander Vrijders <sander@ouroboros.rocks> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License @@ -21,6 +21,7 @@ */ #include <ouroboros/errno.h> +#include <ouroboros/pthread.h> #include <ouroboros/sockets.h> #include <ouroboros/utils.h> @@ -29,24 +30,33 @@ #include <string.h> #include <stdio.h> #include <stdlib.h> -#include <pthread.h> #include <stdbool.h> -#include <sys/time.h> + +/* Apple doesn't support SEQPACKET. */ +#ifdef __APPLE__ +#define SOCK_TYPE SOCK_STREAM +#else +#define SOCK_TYPE SOCK_SEQPACKET +#endif + +void __cleanup_close_ptr(void * fd) +{ + close(*(int *) fd); +} int client_socket_open(char * file_name) { int sockfd; struct sockaddr_un serv_addr; - sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0); + sockfd = socket(AF_UNIX, SOCK_TYPE, 0); if (sockfd < 0) return -1; serv_addr.sun_family = AF_UNIX; sprintf(serv_addr.sun_path, "%s", file_name); - if (connect(sockfd, - (struct sockaddr *) &serv_addr, + if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr))) { close(sockfd); return -1; @@ -66,7 +76,7 @@ int server_socket_open(char * file_name) return -1; } - sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0); + sockfd = socket(AF_UNIX, SOCK_TYPE, 0); if (sockfd < 0) return -1; @@ -88,17 +98,11 @@ int server_socket_open(char * file_name) return sockfd; } -static void close_ptr(void * o) -{ - close(*(int *) o); -} - irm_msg_t * send_recv_irm_msg(irm_msg_t * msg) { - int sockfd; - uint8_t buf[IRM_MSG_BUF_SIZE]; - ssize_t len; - irm_msg_t * recv_msg = NULL; + int sockfd; + uint8_t buf[SOCK_BUF_SIZE]; + ssize_t len; sockfd = client_socket_open(IRM_SOCK_PATH); if (sockfd < 0) @@ -110,19 +114,44 @@ irm_msg_t * send_recv_irm_msg(irm_msg_t * msg) return NULL; } - pthread_cleanup_push(close_ptr, &sockfd); - irm_msg__pack(msg, buf); - if (write(sockfd, buf, len) != -1) - len = read(sockfd, buf, IRM_MSG_BUF_SIZE); + pthread_cleanup_push(__cleanup_close_ptr, &sockfd); + + len = write(sockfd, buf, len); + if (len >= 0) + len = read(sockfd, buf, SOCK_BUF_SIZE); + + pthread_cleanup_pop(true); + + if (len < 0) + goto fail; + + return irm_msg__unpack(NULL, len, buf); + fail: + return NULL; +} - if (len > 0) - recv_msg = irm_msg__unpack(NULL, len, buf); +int send_recv_msg(buffer_t * msg) +{ + int sockfd; + ssize_t len = 0; + + sockfd = client_socket_open(IRM_SOCK_PATH); + if (sockfd < 0) + return -1; + + pthread_cleanup_push(__cleanup_close_ptr, &sockfd); + + len = write(sockfd, msg->data, msg->len); + if (len >= 0) + len = read(sockfd, msg->data, SOCK_BUF_SIZE); pthread_cleanup_pop(true); - return recv_msg; + msg->len = (size_t) len; + + return len < 0 ? -1 : 0; } char * ipcp_sock_path(pid_t pid) |
