From 12968ba98642bd7057e8e7f41e184ab2d61671ed Mon Sep 17 00:00:00 2001 From: Sander Vrijders Date: Thu, 3 May 2018 12:10:02 +0200 Subject: lib: Make UNIX socket buffer size configurable This makes the buffer size used by the UNIX sockets configurable. In case of a lot of IPCPs in the system it might become too small with the default value, resulting in irm command failures. The user can now easily configure it with an adequate value. Signed-off-by: Sander Vrijders Signed-off-by: Dimitri Staessens --- CMakeLists.txt | 2 +- include/ouroboros/CMakeLists.txt | 6 +++++ include/ouroboros/sockets.h | 57 ---------------------------------------- include/ouroboros/sockets.h.in | 56 +++++++++++++++++++++++++++++++++++++++ src/ipcpd/ipcp.c | 4 +-- src/irmd/ipcp.c | 4 +-- src/irmd/main.c | 4 +-- src/lib/sockets.c | 15 ++++++++--- 8 files changed, 80 insertions(+), 68 deletions(-) delete mode 100644 include/ouroboros/sockets.h create mode 100644 include/ouroboros/sockets.h.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 1f888d1e..062d92b2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,7 @@ include(GNUInstallDirs) set(PACKAGE_VERSION_MAJOR 0) set(PACKAGE_VERSION_MINOR 11) -set(PACKAGE_VERSION_PATCH 2) +set(PACKAGE_VERSION_PATCH 3) set(PACKAGE_NAME "${CMAKE_PROJECT_NAME}") set(PACKAGE_DESCRIPTION "The Ouroboros prototype") diff --git a/include/ouroboros/CMakeLists.txt b/include/ouroboros/CMakeLists.txt index c0a83030..fe0ccf5d 100644 --- a/include/ouroboros/CMakeLists.txt +++ b/include/ouroboros/CMakeLists.txt @@ -1,6 +1,12 @@ configure_file("${CMAKE_CURRENT_SOURCE_DIR}/version.h.in" "${CMAKE_CURRENT_BINARY_DIR}/version.h" @ONLY) +set(SOCK_BUF_SIZE 10240 CACHE STRING + "Size of the buffer used by the UNIX sockets for local IPC") + +configure_file("${CMAKE_CURRENT_SOURCE_DIR}/sockets.h.in" + "${CMAKE_CURRENT_BINARY_DIR}/sockets.h" @ONLY) + set(HEADER_FILES cacep.h cdefs.h diff --git a/include/ouroboros/sockets.h b/include/ouroboros/sockets.h deleted file mode 100644 index 36ea4da9..00000000 --- a/include/ouroboros/sockets.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Ouroboros - Copyright (C) 2016 - 2018 - * - * The sockets layer to communicate between daemons - * - * Dimitri Staessens - * Sander Vrijders - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License - * version 2.1 as published by the Free Software Foundation. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., http://www.fsf.org/about/contact/. - */ - -#ifndef OUROBOROS_SOCKETS_H -#define OUROBOROS_SOCKETS_H - -#include - -#include "ipcp_config.pb-c.h" -typedef IpcpConfigMsg ipcp_config_msg_t; -typedef LayerInfoMsg layer_info_msg_t; - -#include "irmd_messages.pb-c.h" -typedef IrmMsg irm_msg_t; -typedef IpcpInfoMsg ipcp_info_msg_t; - -#include "ipcpd_messages.pb-c.h" -typedef IpcpMsg ipcp_msg_t; - -#define SOCK_PATH "/var/run/ouroboros/" -#define SOCK_PATH_SUFFIX ".sock" - -#define IRM_SOCK_PATH SOCK_PATH "irm" SOCK_PATH_SUFFIX -#define IRM_MSG_BUF_SIZE 2048 - -#define IPCP_SOCK_PATH_PREFIX SOCK_PATH "ipcp" -#define IPCP_MSG_BUF_SIZE IRM_MSG_BUF_SIZE - -/* Returns the full socket path of an IPCP */ -char * ipcp_sock_path(pid_t pid); - -int server_socket_open(char * file_name); - -int client_socket_open(char * file_name); - -irm_msg_t * send_recv_irm_msg(irm_msg_t * msg); - -#endif diff --git a/include/ouroboros/sockets.h.in b/include/ouroboros/sockets.h.in new file mode 100644 index 00000000..4557a9ef --- /dev/null +++ b/include/ouroboros/sockets.h.in @@ -0,0 +1,56 @@ +/* + * Ouroboros - Copyright (C) 2016 - 2018 + * + * The sockets layer to communicate between daemons + * + * Dimitri Staessens + * Sander Vrijders + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * version 2.1 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., http://www.fsf.org/about/contact/. + */ + +#ifndef OUROBOROS_SOCKETS_H +#define OUROBOROS_SOCKETS_H + +#include + +#include "ipcp_config.pb-c.h" +typedef IpcpConfigMsg ipcp_config_msg_t; +typedef LayerInfoMsg layer_info_msg_t; + +#include "irmd_messages.pb-c.h" +typedef IrmMsg irm_msg_t; +typedef IpcpInfoMsg ipcp_info_msg_t; + +#include "ipcpd_messages.pb-c.h" +typedef IpcpMsg ipcp_msg_t; + +#define SOCK_PATH "/var/run/ouroboros/" +#define SOCK_PATH_SUFFIX ".sock" + +#define IRM_SOCK_PATH SOCK_PATH "irm" SOCK_PATH_SUFFIX +#define IPCP_SOCK_PATH_PREFIX SOCK_PATH "ipcp" + +#define SOCK_BUF_SIZE @SOCK_BUF_SIZE@ + +/* Returns the full socket path of an IPCP */ +char * ipcp_sock_path(pid_t pid); + +int server_socket_open(char * file_name); + +int client_socket_open(char * file_name); + +irm_msg_t * send_recv_irm_msg(irm_msg_t * msg); + +#endif diff --git a/src/ipcpd/ipcp.c b/src/ipcpd/ipcp.c index dfb88057..8dc7751a 100644 --- a/src/ipcpd/ipcp.c +++ b/src/ipcpd/ipcp.c @@ -55,7 +55,7 @@ struct cmd { struct list_head next; - uint8_t cbuf[IPCP_MSG_BUF_SIZE]; + uint8_t cbuf[SOCK_BUF_SIZE]; size_t len; int fd; }; @@ -146,7 +146,7 @@ static void * acceptloop(void * o) break; } - cmd->len = read(csockfd, cmd->cbuf, IPCP_MSG_BUF_SIZE); + cmd->len = read(csockfd, cmd->cbuf, SOCK_BUF_SIZE); if (cmd->len <= 0) { log_err("Failed to read from socket."); close(csockfd); diff --git a/src/irmd/ipcp.c b/src/irmd/ipcp.c index 49bf13c9..dc8f1c6e 100644 --- a/src/irmd/ipcp.c +++ b/src/irmd/ipcp.c @@ -53,7 +53,7 @@ ipcp_msg_t * send_recv_ipcp_msg(pid_t pid, ipcp_msg_t * msg) { int sockfd = 0; - uint8_t buf[IPCP_MSG_BUF_SIZE]; + uint8_t buf[SOCK_BUF_SIZE]; char * sock_path = NULL; ssize_t len; ipcp_msg_t * recv_msg = NULL; @@ -116,7 +116,7 @@ ipcp_msg_t * send_recv_ipcp_msg(pid_t pid, ipcp_msg__pack(msg, buf); if (write(sockfd, buf, len) != -1) - len = read(sockfd, buf, IPCP_MSG_BUF_SIZE); + len = read(sockfd, buf, SOCK_BUF_SIZE); if (len > 0) recv_msg = ipcp_msg__unpack(NULL, len, buf); diff --git a/src/irmd/main.c b/src/irmd/main.c index db9d7bbd..b2a521d5 100644 --- a/src/irmd/main.c +++ b/src/irmd/main.c @@ -68,7 +68,7 @@ #define IRMD_CLEANUP_TIMER ((IRMD_FLOW_TIMEOUT / 20) * MILLION) /* ns */ #define SHM_SAN_HOLDOFF 1000 /* ms */ #define IPCP_HASH_LEN(e) hash_len(e->dir_hash_algo) -#define IB_LEN IRM_MSG_BUF_SIZE +#define IB_LEN SOCK_BUF_SIZE enum init_state { IPCP_NULL = 0, @@ -1862,7 +1862,7 @@ static void * acceptloop(void * o) break; } - cmd->len = read(csockfd, cmd->cbuf, IRM_MSG_BUF_SIZE); + cmd->len = read(csockfd, cmd->cbuf, SOCK_BUF_SIZE); if (cmd->len <= 0) { log_err("Failed to read from socket."); close(csockfd); diff --git a/src/lib/sockets.c b/src/lib/sockets.c index 6e7d4ad5..b148b7ca 100644 --- a/src/lib/sockets.c +++ b/src/lib/sockets.c @@ -33,12 +33,19 @@ #include #include +/* Apple doesn't support SEQPACKET. */ +#ifdef __APPLE__ +#define SOCK_TYPE SOCK_STREAM +#else +#define SOCK_TYPE SOCK_SEQPACKET +#endif + 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; @@ -66,7 +73,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; @@ -96,7 +103,7 @@ static void close_ptr(void * o) irm_msg_t * send_recv_irm_msg(irm_msg_t * msg) { int sockfd; - uint8_t buf[IRM_MSG_BUF_SIZE]; + uint8_t buf[SOCK_BUF_SIZE]; ssize_t len; irm_msg_t * recv_msg = NULL; @@ -115,7 +122,7 @@ irm_msg_t * send_recv_irm_msg(irm_msg_t * msg) irm_msg__pack(msg, buf); if (write(sockfd, buf, len) != -1) - len = read(sockfd, buf, IRM_MSG_BUF_SIZE); + len = read(sockfd, buf, SOCK_BUF_SIZE); if (len > 0) recv_msg = irm_msg__unpack(NULL, len, buf); -- cgit v1.2.3