summaryrefslogtreecommitdiff
path: root/src/lib/sockets.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/sockets.c')
-rw-r--r--src/lib/sockets.c89
1 files changed, 37 insertions, 52 deletions
diff --git a/src/lib/sockets.c b/src/lib/sockets.c
index 8179d2b3..13219db0 100644
--- a/src/lib/sockets.c
+++ b/src/lib/sockets.c
@@ -1,5 +1,5 @@
/*
- * Ouroboros - Copyright (C) 2016 - 2021
+ * Ouroboros - Copyright (C) 2016 - 2024
*
* The sockets layer to communicate between daemons
*
@@ -21,6 +21,7 @@
*/
#include <ouroboros/errno.h>
+#include <ouroboros/pthread.h>
#include <ouroboros/sockets.h>
#include <ouroboros/utils.h>
@@ -29,9 +30,7 @@
#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__
@@ -57,8 +56,7 @@ int client_socket_open(char * file_name)
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;
@@ -102,10 +100,9 @@ int server_socket_open(char * file_name)
irm_msg_t * send_recv_irm_msg(irm_msg_t * msg)
{
- int sockfd;
- uint8_t buf[SOCK_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)
@@ -117,19 +114,44 @@ irm_msg_t * send_recv_irm_msg(irm_msg_t * msg)
return NULL;
}
- pthread_cleanup_push(__cleanup_close_ptr, &sockfd);
-
irm_msg__pack(msg, buf);
- if (write(sockfd, buf, len) != -1)
+ pthread_cleanup_push(__cleanup_close_ptr, &sockfd);
+
+ len = write(sockfd, buf, len);
+ if (len >= 0)
len = read(sockfd, buf, SOCK_BUF_SIZE);
- if (len > 0)
- recv_msg = irm_msg__unpack(NULL, len, buf);
+ pthread_cleanup_pop(true);
+
+ if (len < 0)
+ goto fail;
+
+ return irm_msg__unpack(NULL, len, buf);
+ fail:
+ return NULL;
+}
+
+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)
@@ -165,40 +187,3 @@ char * ipcp_sock_path(pid_t pid)
return full_name;
}
-
-qosspec_msg_t spec_to_msg(const qosspec_t * qs)
-{
- qosspec_t spec;
- qosspec_msg_t msg = QOSSPEC_MSG__INIT;
-
- spec = (qs == NULL ? qos_raw : *qs);
-
- msg.delay = spec.delay;
- msg.bandwidth = spec.bandwidth;
- msg.availability = spec.availability;
- msg.loss = spec.loss;
- msg.ber = spec.ber;
- msg.in_order = spec.in_order;
- msg.max_gap = spec.max_gap;
- msg.cypher_s = spec.cypher_s;
-
- return msg;
-}
-
-qosspec_t msg_to_spec(const qosspec_msg_t * msg)
-{
- qosspec_t spec;
-
- assert(msg);
-
- spec.delay = msg->delay;
- spec.bandwidth = msg->bandwidth;
- spec.availability = msg->availability;
- spec.loss = msg->loss;
- spec.ber = msg->ber;
- spec.in_order = msg->in_order;
- spec.max_gap = msg->max_gap;
- spec.cypher_s = msg->cypher_s;
-
- return spec;
-}