From b144406dbb9cbdf83354590746097a93d6e88bc4 Mon Sep 17 00:00:00 2001 From: Sander Vrijders Date: Mon, 28 Mar 2016 16:04:34 +0200 Subject: lib, irmd: Use GPB for dev.c and IRMd IRMd and dev.c now also use GPB instead of our own ser/des. irm_msg struct has been dropped as well as the methods associated with it. --- src/irmd/main.c | 72 ++++++++++++++++++++++----------------------------------- 1 file changed, 28 insertions(+), 44 deletions(-) (limited to 'src/irmd') diff --git a/src/irmd/main.c b/src/irmd/main.c index 622b367d..d5e1fcd8 100644 --- a/src/irmd/main.c +++ b/src/irmd/main.c @@ -124,7 +124,7 @@ static void destroy_ipcp(struct irm * instance, static void bootstrap_ipcp(struct irm * instance, rina_name_t name, - struct dif_config conf) + struct dif_config * conf) { pid_t pid = 0; @@ -134,7 +134,7 @@ static void bootstrap_ipcp(struct irm * instance, return; } - if (ipcp_bootstrap(pid, conf)) + if (ipcp_bootstrap(pid, *conf)) LOG_ERR("Could not bootstrap IPCP"); } @@ -219,10 +219,9 @@ int main() while (true) { int cli_sockfd; - struct irm_msg * msg; + irm_msg_t * msg; ssize_t count; - buffer_t buffer; - int i; + rina_name_t name; cli_sockfd = accept(sockfd, 0, 0); if (cli_sockfd < 0) { @@ -232,59 +231,44 @@ int main() count = read(cli_sockfd, buf, IRM_MSG_BUF_SIZE); if (count > 0) { - buffer.size = count; - buffer.data = buf; - msg = deserialize_irm_msg(&buffer); + msg = irm_msg__unpack(NULL, count, buf); if (msg == NULL) continue; + name.ap_name = msg->ap_name; + name.api_id = msg->api_id; + switch (msg->code) { - case IRM_CREATE_IPCP: - create_ipcp(instance, - *(msg->name), - msg->ipcp_type); - free(msg->ipcp_type); + case IRM_MSG_CODE__IRM_CREATE_IPCP: + create_ipcp(instance, name, msg->ipcp_type); break; - case IRM_DESTROY_IPCP: - destroy_ipcp(instance, - *(msg->name)); + case IRM_MSG_CODE__IRM_DESTROY_IPCP: + destroy_ipcp(instance, name); break; - case IRM_BOOTSTRAP_IPCP: - bootstrap_ipcp(instance, - *(msg->name), - *(msg->conf)); - free(msg->conf); + case IRM_MSG_CODE__IRM_BOOTSTRAP_IPCP: + bootstrap_ipcp(instance, name, NULL); break; - case IRM_ENROLL_IPCP: - enroll_ipcp(instance, - *(msg->name), - msg->dif_name); - free(msg->dif_name); + case IRM_MSG_CODE__IRM_ENROLL_IPCP: + if (msg->n_dif_name != 1) + continue; + enroll_ipcp(instance, name, msg->dif_name[0]); break; - case IRM_REG_IPCP: - reg_ipcp(instance, - *(msg->name), - msg->difs, - msg->difs_size); - for (i = 0; i < msg->difs_size; i++) - free(msg->difs[i]); - free(msg->difs); + case IRM_MSG_CODE__IRM_REG_IPCP: + reg_ipcp(instance, name, + msg->dif_name, + msg->n_dif_name); break; - case IRM_UNREG_IPCP: - unreg_ipcp(instance, - *(msg->name), - msg->difs, - msg->difs_size); - for (i = 0; i < msg->difs_size; i++) - free(msg->difs[i]); - free(msg->difs); + case IRM_MSG_CODE__IRM_UNREG_IPCP: + unreg_ipcp(instance, name, + msg->dif_name, + msg->n_dif_name); break; default: LOG_ERR("Don't know that message code"); break; } - name_destroy(msg->name); - free(msg); + + irm_msg__free_unpacked(msg, NULL); } close(cli_sockfd); -- cgit v1.2.3 From c45be0dcbd123881cc33ce61571578dcb22c3b54 Mon Sep 17 00:00:00 2001 From: Sander Vrijders Date: Mon, 28 Mar 2016 16:35:40 +0200 Subject: lib: Add ipcp_messages proto file This removes the custom ser/des methods for communicating with the IPCP daemon and also uses GPB instead. --- include/ouroboros/ipcp.h | 20 ++-- include/ouroboros/sockets.h | 34 ++---- src/irmd/main.c | 2 +- src/lib/CMakeLists.txt | 52 ++++----- src/lib/ipcp.c | 94 ++++++++++------- src/lib/ipcpd_messages.proto | 14 +++ src/lib/sockets.c | 245 ------------------------------------------- 7 files changed, 115 insertions(+), 346 deletions(-) create mode 100644 src/lib/ipcpd_messages.proto (limited to 'src/irmd') diff --git a/include/ouroboros/ipcp.h b/include/ouroboros/ipcp.h index b8775fc0..cd4a3f51 100644 --- a/include/ouroboros/ipcp.h +++ b/include/ouroboros/ipcp.h @@ -32,21 +32,21 @@ struct ipcp; /* Returns the process id */ pid_t ipcp_create(rina_name_t name, - char * ipcp_type); + char * ipcp_type); int ipcp_destroy(pid_t pid); -int ipcp_reg(pid_t pid, +int ipcp_reg(pid_t pid, char ** difs, - size_t difs_size); -int ipcp_unreg(pid_t pid, + size_t difs_size); +int ipcp_unreg(pid_t pid, char ** difs, - size_t difs_size); + size_t difs_size); -int ipcp_bootstrap(pid_t pid, - struct dif_config conf); -int ipcp_enroll(pid_t pid, - char * dif_name, - char * member_name, +int ipcp_bootstrap(pid_t pid, + struct dif_config * conf); +int ipcp_enroll(pid_t pid, + char * dif_name, + char * member_name, char ** n_1_difs, ssize_t n_1_difs_size); diff --git a/include/ouroboros/sockets.h b/include/ouroboros/sockets.h index 0974ada0..bb8e6d84 100644 --- a/include/ouroboros/sockets.h +++ b/include/ouroboros/sockets.h @@ -31,40 +31,22 @@ #include "irmd_messages.pb-c.h" typedef IrmMsg irm_msg_t; +#include "ipcpd_messages.pb-c.h" +typedef IpcpMsg ipcp_msg_t; + #define IRM_SOCK_PATH "/tmp/irm_sock" #define IRM_MSG_BUF_SIZE 256 #define IPCP_SOCK_PATH_PREFIX "/tmp/ipcp_sock" #define IPCP_MSG_BUFS_SIZE IRM_MSG_BUF_SIZE -enum ipcp_msg_code { - IPCP_BOOTSTRAP, - IPCP_ENROLL, - IPCP_REG, - IPCP_UNREG -}; - -struct ipcp_msg { - enum ipcp_msg_code code; - struct dif_config * conf; - char * dif_name; - char * ap_name; - char ** difs; - size_t difs_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); +char * ipcp_sock_path(pid_t pid); -int send_irm_msg(irm_msg_t * msg); -irm_msg_t * send_recv_irm_msg(irm_msg_t * msg); +int server_socket_open(char * file_name); +int client_socket_open(char * file_name); -/* Caller has to free the buffer */ -buffer_t * serialize_ipcp_msg(struct ipcp_msg * msg); -/* Caller has to free all the allocated fields in the message */ -struct ipcp_msg * deserialize_ipcp_msg(buffer_t * data); +int send_irm_msg(irm_msg_t * msg); +irm_msg_t * send_recv_irm_msg(irm_msg_t * msg); #endif diff --git a/src/irmd/main.c b/src/irmd/main.c index d5e1fcd8..e4b6cebd 100644 --- a/src/irmd/main.c +++ b/src/irmd/main.c @@ -134,7 +134,7 @@ static void bootstrap_ipcp(struct irm * instance, return; } - if (ipcp_bootstrap(pid, *conf)) + if (ipcp_bootstrap(pid, conf)) LOG_ERR("Could not bootstrap IPCP"); } diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index 0427e236..52061645 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -3,48 +3,48 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR}) include_directories(${CMAKE_SOURCE_DIR}/include) include_directories(${CMAKE_BINARY_DIR}/include) -find_package(ProtobufC REQUIRED) +find_package(ProtobufC REQUIRED) include_directories(${PROTOBUF_INCLUDE_DIRS}) -include_directories(${CMAKE_CURRENT_BINARY_DIR}) - -protobuf_generate_c(PROTO_SRCS PROTO_HDRS irmd_messages.proto) +protobuf_generate_c(IRM_PROTO_SRCS IRM_PROTO_HDRS irmd_messages.proto) +protobuf_generate_c(IPCP_PROTO_SRCS IPCP_PROTO_HDRS ipcpd_messages.proto) find_library(LIBRT_LIBRARIES rt) if(NOT LIBRT_LIBRARIES) - message(FATAL_ERROR "librt not found") + message(FATAL_ERROR "librt not found") endif() find_library(LIBPTHREAD_LIBRARIES pthread) if(NOT LIBPTHREAD_LIBRARIES) - message(FATAL_ERROR "libpthread not found") + message(FATAL_ERROR "libpthread not found") endif() set(SOURCE_FILES - # Add source files here - bitmap.c - cdap.c - da.c - dev.c - du_buff.c - ipcp.c - irm.c - list.c - rina_name.c - shm_du_map.c - sockets.c - utils.c - ) - -install(FILES ${PROTO_HDRS} - DESTINATION include/ouroboros) - -add_library(ouroboros SHARED ${SOURCE_FILES} ${PROTO_SRCS}) + # Add source files here + bitmap.c + cdap.c + da.c + dev.c + du_buff.c + ipcp.c + irm.c + list.c + rina_name.c + shm_du_map.c + sockets.c + utils.c + ) + +install(FILES ${IRM_PROTO_HDRS} ${IPCP_PROTO_HDRS} + DESTINATION include/ouroboros) + +add_library(ouroboros SHARED ${SOURCE_FILES} + ${IRM_PROTO_SRCS} ${IPCP_PROTO_SRCS}) target_link_libraries(ouroboros rt pthread ${PROTOBUF_C_LIBRARY}) include(MacroAddCompileFlags) if (CMAKE_BUILD_TYPE MATCHES Debug) - MACRO_ADD_COMPILE_FLAGS(ouroboros -DCONFIG_OUROBOROS_DEBUG) + MACRO_ADD_COMPILE_FLAGS(ouroboros -DCONFIG_OUROBOROS_DEBUG) endif (CMAKE_BUILD_TYPE MATCHES Debug) install(TARGETS ouroboros LIBRARY DESTINATION lib) diff --git a/src/lib/ipcp.c b/src/lib/ipcp.c index 53d717ba..445160f0 100644 --- a/src/lib/ipcp.c +++ b/src/lib/ipcp.c @@ -40,10 +40,10 @@ #include static int send_ipcp_msg(pid_t pid, - struct ipcp_msg * msg) + ipcp_msg_t * msg) { int sockfd = 0; - buffer_t * buf = NULL; + buffer_t buf; char * sock_path; sock_path = ipcp_sock_path(pid); @@ -56,24 +56,31 @@ static int send_ipcp_msg(pid_t pid, return -1; } - buf = serialize_ipcp_msg(msg); - if (buf == NULL) { - free(sock_path); + buf.size = ipcp_msg__get_packed_size(msg); + if (buf.size == 0) { close(sockfd); + free(sock_path); return -1; } - if (write(sockfd, buf->data, buf->size) == -1) { + buf.data = malloc(buf.size); + if (buf.data == NULL) { + close(sockfd); + free(sock_path); + return -ENOMEM; + } + + ipcp_msg__pack(msg, buf.data); + + if (write(sockfd, buf.data, buf.size) == -1) { free(sock_path); - free(buf->data); - free(buf); + free(buf.data); close(sockfd); return -1; } - free(buf->data); - free(buf); - + free(buf.data); + free(sock_path); close(sockfd); return 0; } @@ -158,14 +165,16 @@ int ipcp_reg(pid_t pid, char ** difs, size_t difs_size) { - struct ipcp_msg msg; + ipcp_msg_t msg = IPCP_MSG__INIT; - if (difs == NULL) - return -1; + if (difs == NULL || + difs_size == 0 || + difs[0] == NULL) + return -EINVAL; - msg.code = IPCP_REG; - msg.difs = difs; - msg.difs_size = difs_size; + msg.code = IPCP_MSG_CODE__IPCP_REG; + msg.dif_name = difs; + msg.n_dif_name = difs_size; if (send_ipcp_msg(pid, &msg)) { LOG_ERR("Failed to send message to daemon"); @@ -179,14 +188,16 @@ int ipcp_unreg(pid_t pid, char ** difs, size_t difs_size) { - struct ipcp_msg msg; + ipcp_msg_t msg = IPCP_MSG__INIT; - if (difs == NULL) - return -1; + if (difs == NULL || + difs_size == 0 || + difs[0] == NULL) + return -EINVAL; - msg.code = IPCP_UNREG; - msg.difs = difs; - msg.difs_size = difs_size; + msg.code = IPCP_MSG_CODE__IPCP_UNREG; + msg.dif_name = difs; + msg.n_dif_name = difs_size; if (send_ipcp_msg(pid, &msg)) { LOG_ERR("Failed to send message to daemon"); @@ -197,12 +208,11 @@ int ipcp_unreg(pid_t pid, } int ipcp_bootstrap(pid_t pid, - struct dif_config conf) + struct dif_config * conf) { - struct ipcp_msg msg; + ipcp_msg_t msg = IPCP_MSG__INIT; - msg.code = IPCP_BOOTSTRAP; - msg.conf = &conf; + msg.code = IPCP_MSG_CODE__IPCP_BOOTSTRAP; if (send_ipcp_msg(pid, &msg)) { LOG_ERR("Failed to send message to daemon"); @@ -218,24 +228,32 @@ int ipcp_enroll(pid_t pid, char ** n_1_difs, ssize_t n_1_difs_size) { - struct ipcp_msg msg; - - if (n_1_difs == NULL) - return -1; - - if (dif_name == NULL) + ipcp_msg_t msg = IPCP_MSG__INIT; + + if (n_1_difs == NULL || + n_1_difs_size == 0 || + n_1_difs[0] == NULL || + dif_name == NULL || + member_name == NULL) + return -EINVAL; + + msg.code = IPCP_MSG_CODE__IPCP_ENROLL; + msg.dif_name = malloc(sizeof(*(msg.dif_name))); + if (msg.dif_name == NULL) { + LOG_ERR("Failed to malloc"); return -1; - - msg.code = IPCP_ENROLL; - msg.dif_name = dif_name; + } + msg.dif_name[0] = dif_name; msg.ap_name = member_name; - msg.difs = n_1_difs; - msg.difs_size = n_1_difs_size; + msg.n_1_dif_name = n_1_difs; + msg.n_n_1_dif_name = n_1_difs_size; if (send_ipcp_msg(pid, &msg)) { LOG_ERR("Failed to send message to daemon"); + free(msg.dif_name); return -1; } + free(msg.dif_name); return 0; } diff --git a/src/lib/ipcpd_messages.proto b/src/lib/ipcpd_messages.proto new file mode 100644 index 00000000..0715fbe0 --- /dev/null +++ b/src/lib/ipcpd_messages.proto @@ -0,0 +1,14 @@ +enum ipcp_msg_code { + IPCP_BOOTSTRAP = 1; + IPCP_ENROLL = 2; + IPCP_REG = 3; + IPCP_UNREG = 4; +}; + +message ipcp_msg { + required ipcp_msg_code code = 1; + optional string ap_name = 2; + // Missing dif_config field here + repeated string dif_name = 4; + repeated string n_1_dif_name = 5; +}; diff --git a/src/lib/sockets.c b/src/lib/sockets.c index fcd5d55a..a699206d 100644 --- a/src/lib/sockets.c +++ b/src/lib/sockets.c @@ -111,7 +111,6 @@ int send_irm_msg(irm_msg_t * msg) return -1; } - LOG_DBG("Size will be %lu", buf.size); buf.data = malloc(buf.size); if (buf.data == NULL) { close(sockfd); @@ -214,247 +213,3 @@ char * ipcp_sock_path(pid_t pid) return full_name; } - -static int serialized_string_len(uint8_t * data) -{ - uint8_t * seek = data; - - while (*seek != '\0') - seek++; - - return (seek - data) + 1; -} - -static void ser_copy_value(size_t flen, - void * dst, - void * src, - int * offset) -{ - memcpy(dst + *offset, src, flen); - *offset += flen; -} - -static void deser_copy_value(size_t flen, - void * dst, - void * src, - int * offset) -{ - memcpy(dst, src + *offset, flen); - *offset += flen; -} - -static int deser_copy_string(uint8_t * data, - char ** dst, - int * offset) -{ - size_t flen; - - flen = serialized_string_len(data + *offset); - *dst = malloc(sizeof(**dst) * (flen + 1)); - if (*dst == NULL) - return -1; - deser_copy_value(flen, *dst, data, offset); - return 0; -} - -static void deser_copy_size_t(uint8_t * data, - size_t * dst, - int * offset) -{ - *dst = 0; - deser_copy_value(sizeof(size_t), dst, data, offset); -} - -/* Move these to a separate file? */ -static buffer_t * buffer_create() -{ - buffer_t * buf; - - buf = malloc(sizeof(*buf)); - if (buf == NULL) - return NULL; - - buf->data = malloc(IRM_MSG_BUF_SIZE); - if (buf->data == NULL) { - free(buf); - return NULL; - } - - return buf; -} - -static void buffer_destroy(buffer_t * buf) -{ - if (buf->data != NULL) - free(buf->data); - - if (buf != NULL) - free(buf); -} - -buffer_t * serialize_ipcp_msg(struct ipcp_msg * msg) -{ - buffer_t * buf = NULL; - uint8_t * data = NULL; - int offset = 0; - char ** pos = NULL; - int i = 0; - - if (msg == NULL) - return NULL; - - buf = buffer_create(); - if (buf == NULL) - return NULL; - - data = buf->data; - - ser_copy_value(sizeof(enum ipcp_msg_code), - data, &msg->code, &offset); - - switch (msg->code) { - case IPCP_BOOTSTRAP: - break; - case IPCP_ENROLL: - if (msg->dif_name == NULL) { - buffer_destroy(buf); - return NULL; - } - - ser_copy_value(strlen(msg->dif_name) + 1, data, - msg->dif_name, &offset); - - if (msg->ap_name == NULL) { - LOG_ERR("Null pointer passed"); - buffer_destroy(buf); - return NULL; - } - ser_copy_value(strlen(msg->ap_name) + 1, data, - msg->ap_name, &offset); - - /* All these operations end with a list of DIFs */ - case IPCP_REG: - case IPCP_UNREG: - if (msg->difs == NULL || msg->difs[0] == NULL) { - buffer_destroy(buf); - return NULL; - } - - ser_copy_value(sizeof(size_t), data, &msg->difs_size, &offset); - - pos = msg->difs; - for (i = 0; i < msg->difs_size; i++) { - ser_copy_value(strlen(*pos) + 1, data, *pos, &offset); - pos++; - } - break; - default: - LOG_ERR("Don't know that code"); - buffer_destroy(buf); - return NULL; - } - - buf->size = offset; - - return buf; -} - -struct ipcp_msg * deserialize_ipcp_msg(buffer_t * data) -{ - struct ipcp_msg * msg; - int i, j; - int offset = 0; - size_t difs_size; - - if (data == NULL || data->data == NULL) { - LOG_ERR("Got a null pointer"); - return NULL; - } - - msg = malloc(sizeof(*msg)); - if (msg == NULL) { - LOG_ERR("Failed to allocate memory"); - return NULL; - } - - deser_copy_value(sizeof(enum ipcp_msg_code), - &msg->code, data->data, &offset); - - switch (msg->code) { - case IPCP_BOOTSTRAP: - break; - case IPCP_ENROLL: - if (deser_copy_string(data->data, - &msg->dif_name, - &offset)) { - free(msg); - return NULL; - } - - deser_copy_string(data->data, - &msg->ap_name, - &offset); - if (msg->ap_name == NULL) { - LOG_ERR("Failed to reconstruct name"); - free(msg->dif_name); - free(msg); - return NULL; - } - - deser_copy_size_t(data->data, &difs_size, &offset); - msg->difs_size = difs_size; - - msg->difs = malloc(sizeof(*(msg->difs)) * difs_size); - if (msg->difs == NULL) { - free(msg->ap_name); - free(msg->dif_name); - free(msg); - return NULL; - } - - for (i = 0; i < difs_size; i++) { - if (deser_copy_string(data->data, - &msg->difs[i], - &offset)) { - for (j = 0; j < i; j++) - free(msg->difs[j]); - free(msg->dif_name); - free(msg->difs); - free(msg->ap_name); - free(msg); - return NULL; - } - } - break; - case IPCP_REG: - case IPCP_UNREG: - deser_copy_size_t(data->data, &difs_size, &offset); - msg->difs_size = difs_size; - - msg->difs = malloc(sizeof(*(msg->difs)) * difs_size); - if (msg->difs == NULL) { - free(msg); - return NULL; - } - - for (i = 0; i < difs_size; i++) { - if (deser_copy_string(data->data, - &msg->difs[i], - &offset)) { - for (j = 0; j < i; j++) - free(msg->difs[j]); - free(msg->difs); - free(msg); - return NULL; - } - } - - break; - default: - LOG_ERR("Don't know that code"); - free(msg); - return NULL; - } - - return msg; -} -- cgit v1.2.3 From 2ed8914deed73a558c6fbac7f107f47dc22f22d2 Mon Sep 17 00:00:00 2001 From: dimitri staessens Date: Tue, 29 Mar 2016 23:32:04 +0200 Subject: lib: renamed rina_name_t to instance_name_t all functions taking a char * ap_name and uint id now take either a instance_name_t or instance_name_t * --- include/ouroboros/CMakeLists.txt | 2 +- include/ouroboros/da.h | 2 +- include/ouroboros/instance_name.h | 89 ++++++++++++ include/ouroboros/ipcp.h | 10 +- include/ouroboros/irm.h | 35 ++--- include/ouroboros/rina_name.h | 94 ------------ include/ouroboros/sockets.h | 1 - src/irmd/main.c | 96 ++++++------- src/lib/CMakeLists.txt | 2 +- src/lib/instance_name.c | 274 +++++++++++++++++++++++++++++++++++ src/lib/ipcp.c | 10 +- src/lib/irm.c | 84 ++++++----- src/lib/rina_name.c | 275 ------------------------------------ src/tools/echo/echo_client.c | 1 - src/tools/echo/echo_server.c | 1 - src/tools/irm/irm.c | 2 +- src/tools/irm/irm_bootstrap_ipcp.c | 11 +- src/tools/irm/irm_create_ipcp.c | 12 +- src/tools/irm/irm_destroy_ipcp.c | 11 +- src/tools/irm/irm_enroll_ipcp.c | 11 +- src/tools/irm/irm_register_ipcp.c | 11 +- src/tools/irm/irm_unregister_ipcp.c | 11 +- src/tools/irm/irm_utils.h | 4 - 23 files changed, 516 insertions(+), 533 deletions(-) create mode 100644 include/ouroboros/instance_name.h delete mode 100644 include/ouroboros/rina_name.h create mode 100644 src/lib/instance_name.c delete mode 100644 src/lib/rina_name.c (limited to 'src/irmd') diff --git a/include/ouroboros/CMakeLists.txt b/include/ouroboros/CMakeLists.txt index cc6b9103..324a85ad 100644 --- a/include/ouroboros/CMakeLists.txt +++ b/include/ouroboros/CMakeLists.txt @@ -9,11 +9,11 @@ set(HEADER_FILES da.h dev.h du_buff.h + instance_name.h ipcp.h irm.h list.h logs.h - rina_name.h shm_du_map.h sockets.h utils.h diff --git a/include/ouroboros/da.h b/include/ouroboros/da.h index f678007d..9ecd4bd8 100644 --- a/include/ouroboros/da.h +++ b/include/ouroboros/da.h @@ -24,7 +24,7 @@ #define OUROBOROS_DA_H #include "common.h" -#include "rina_name.h" +#include "instance_name.h" char * da_resolve_daf(char * daf_name); /* diff --git a/include/ouroboros/instance_name.h b/include/ouroboros/instance_name.h new file mode 100644 index 00000000..b3e528c0 --- /dev/null +++ b/include/ouroboros/instance_name.h @@ -0,0 +1,89 @@ +/* + * RINA naming related utilities + * + * Sander Vrijders + * Francesco Salvestrini + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#ifndef INSTANCE_NAME_H +#define INSTANCE_NAME_H + +#include "common.h" + +typedef struct { + char * name; + uint16_t id; +} instance_name_t; + +/* + * Allocates a new name, returning the allocated object. + * In case of an error, a NULL is returned. + */ +instance_name_t * instance_name_create(); + +/* + * Initializes a previously dynamically allocated name (i.e. name_create()) + * or a statically one (e.g. declared into a struct not as a pointer). + * Returns the passed object pointer in case everything is ok, a NULL + * otherwise. + * + * A call to name_destroy() is allowed in case of error, in order to + * release the associated resources. + * + * It is allowed to call name_init() over an already initialized object + */ +instance_name_t * instance_name_init_from(instance_name_t * dst, + const char * name, + uint16_t api_id); + +/* Takes ownership of the passed parameters */ +instance_name_t * instance_name_init_with(instance_name_t * dst, + char * name, + uint16_t id); + +/* + * Finalize a name object, releasing all the embedded resources (without + * releasing the object itself). After name_fini() execution the passed + * object will be in the same states as at the end of name_init(). + */ +void instance_name_fini(instance_name_t * dst); + +/* Releases all the associated resources bound to a name object */ +void instance_name_destroy(instance_name_t * ptr); + +/* Duplicates a name object, returning the pointer to the new object */ +instance_name_t * instance_name_dup(const instance_name_t * src); + +/* + * Copies the source object contents into the destination object, both must + * be previously allocated + */ +int instance_name_cpy(const instance_name_t * src, + instance_name_t * dst); + +int instance_name_cmp(const instance_name_t * a, + const instance_name_t * b); + +bool instance_name_is_valid(const instance_name_t * n); + +/* Returns a name as a (newly allocated) string */ +char * instance_name_to_string(const instance_name_t * n); + +/* Inverse of name_tostring() */ +instance_name_t * string_to_instance_name(const char * s); + +#endif /* INSTANCE_NAME_H */ diff --git a/include/ouroboros/ipcp.h b/include/ouroboros/ipcp.h index cd4a3f51..49b04908 100644 --- a/include/ouroboros/ipcp.h +++ b/include/ouroboros/ipcp.h @@ -26,14 +26,14 @@ #include #include "common.h" -#include "rina_name.h" +#include "instance_name.h" struct ipcp; /* Returns the process id */ -pid_t ipcp_create(rina_name_t name, - char * ipcp_type); -int ipcp_destroy(pid_t pid); +pid_t ipcp_create(instance_name_t * api, + char * ipcp_type); +int ipcp_destroy(pid_t pid); int ipcp_reg(pid_t pid, char ** difs, @@ -50,4 +50,4 @@ int ipcp_enroll(pid_t pid, char ** n_1_difs, ssize_t n_1_difs_size); -#endif +#endif /* OUROBOROS_IPCP_H */ diff --git a/include/ouroboros/irm.h b/include/ouroboros/irm.h index fe72aefe..780bf77b 100644 --- a/include/ouroboros/irm.h +++ b/include/ouroboros/irm.h @@ -24,28 +24,21 @@ #define OUROBOROS_IRM_H #include "common.h" -#include "rina_name.h" +#include "instance_name.h" -int irm_create_ipcp(char * ap_name, - int api_id, - char * ipcp_type); -int irm_destroy_ipcp(char * ap_name, - int api_id); +int irm_create_ipcp(instance_name_t * api, + char * ipcp_type); +int irm_destroy_ipcp(instance_name_t * api); -int irm_bootstrap_ipcp(char * ap_name, - int api_id, +int irm_bootstrap_ipcp(instance_name_t * api, struct dif_config * conf); -int irm_enroll_ipcp(char * ap_name, - int api_id, - char * dif_name); +int irm_enroll_ipcp(instance_name_t * api, + char * dif_name); -int irm_reg_ipcp(char * ap_name, - int api_id, - char ** difs, - size_t difs_size); -int irm_unreg_ipcp(char * ap_name, - int api_id, - char ** difs, - size_t difs_size); - -#endif +int irm_reg_ipcp(instance_name_t * api, + char ** difs, + size_t difs_size); +int irm_unreg_ipcp(const instance_name_t * api, + char ** difs, + size_t difs_size); +#endif /* OUROBOROS_IRM_H */ diff --git a/include/ouroboros/rina_name.h b/include/ouroboros/rina_name.h deleted file mode 100644 index f8af00c2..00000000 --- a/include/ouroboros/rina_name.h +++ /dev/null @@ -1,94 +0,0 @@ -/* - * RINA naming related utilities - * - * Sander Vrijders - * Francesco Salvestrini - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - */ - -#ifndef RINA_NAME_H -#define RINA_NAME_H - -#include "common.h" - -typedef struct { - char * ap_name; - unsigned int api_id; -} rina_name_t; - -/* - * Allocates a new name, returning the allocated object. - * In case of an error, a NULL is returned. - */ -rina_name_t * name_create(); - -/* - * Initializes a previously dynamically allocated name (i.e. name_create()) - * or a statically one (e.g. declared into a struct not as a pointer). - * Returns the passed object pointer in case everything is ok, a NULL - * otherwise. - * - * A call to name_destroy() is allowed in case of error, in order to - * release the associated resources. - * - * It is allowed to call name_init() over an already initialized object - */ -rina_name_t * name_init_from(rina_name_t * dst, - const char * ap_name, - unsigned int api_id); - -/* Takes ownership of the passed parameters */ -rina_name_t * name_init_with(rina_name_t * dst, - char * ap_name, - unsigned int api_id); - -/* - * Finalize a name object, releasing all the embedded resources (without - * releasing the object itself). After name_fini() execution the passed - * object will be in the same states as at the end of name_init(). - */ -void name_fini(rina_name_t * dst); - -/* Releases all the associated resources bound to a name object */ -void name_destroy(rina_name_t * ptr); - -/* Duplicates a name object, returning the pointer to the new object */ -rina_name_t * name_dup(const rina_name_t * src); - -/* - * Copies the source object contents into the destination object, both must - * be previously allocated - */ -int name_cpy(const rina_name_t * src, rina_name_t * dst); - -bool name_is_equal(const rina_name_t * a, const rina_name_t * b); -bool name_is_ok(const rina_name_t * n); - -#define NAME_CMP_APN 0x01 -#define NAME_CMP_API 0x02 -#define NAME_CMP_ALL (NAME_CMP_APN | NAME_CMP_API) - -bool name_cmp(uint8_t flags, - const rina_name_t * a, - const rina_name_t * b); - -/* Returns a name as a (newly allocated) string */ -char * name_to_string(const rina_name_t * n); - -/* Inverse of name_tostring() */ -rina_name_t * string_to_name(const char * s); - -#endif diff --git a/include/ouroboros/sockets.h b/include/ouroboros/sockets.h index bb8e6d84..4c736de2 100644 --- a/include/ouroboros/sockets.h +++ b/include/ouroboros/sockets.h @@ -24,7 +24,6 @@ #define OUROBOROS_SOCKETS_H #include -#include #include diff --git a/src/irmd/main.c b/src/irmd/main.c index e4b6cebd..547286e8 100644 --- a/src/irmd/main.c +++ b/src/irmd/main.c @@ -29,7 +29,7 @@ #include #include #include -#include +#include #include #include @@ -37,17 +37,17 @@ #include struct name_to_pid_entry { - struct list_head next; - pid_t pid; - rina_name_t * name; + struct list_head next; + pid_t pid; + instance_name_t * api; }; struct irm { struct list_head name_to_pid; }; -static pid_t find_pid_by_name(struct irm * instance, - rina_name_t * name) +static pid_t find_pid_by_name(struct irm * instance, + instance_name_t * api) { struct list_head * pos; @@ -55,23 +55,23 @@ static pid_t find_pid_by_name(struct irm * instance, struct name_to_pid_entry * tmp = list_entry(pos, struct name_to_pid_entry, next); - LOG_DBG("name is %s", name->ap_name); + LOG_DBG("name is %s", api->name); - if (name_is_equal(name, tmp->name)) + if (instance_name_cmp(api, tmp->api) == 0) return tmp->pid; } return 0; } -static void create_ipcp(struct irm * instance, - rina_name_t name, - char * ipcp_type) +static void create_ipcp(struct irm * instance, + instance_name_t * api, + char * ipcp_type) { pid_t pid; struct name_to_pid_entry * tmp; - pid = ipcp_create(name, ipcp_type); + pid = ipcp_create(api, ipcp_type); if (pid == -1) { LOG_ERR("Failed to create IPCP"); return; @@ -84,8 +84,8 @@ static void create_ipcp(struct irm * instance, INIT_LIST_HEAD(&tmp->next); tmp->pid = pid; - tmp->name = name_dup(&name); - if (tmp->name == NULL) { + tmp->api = instance_name_dup(api); + if (tmp->api == NULL) { free(tmp); return; } @@ -95,14 +95,14 @@ static void create_ipcp(struct irm * instance, list_add(&tmp->next, &instance->name_to_pid); } -static void destroy_ipcp(struct irm * instance, - rina_name_t name) +static void destroy_ipcp(struct irm * instance, + instance_name_t * api) { pid_t pid = 0; struct list_head * pos; struct list_head * n; - pid = find_pid_by_name(instance, &name); + pid = find_pid_by_name(instance, api); if (pid == 0) { LOG_ERR("No such IPCP"); return; @@ -117,18 +117,18 @@ static void destroy_ipcp(struct irm * instance, struct name_to_pid_entry * tmp = list_entry(pos, struct name_to_pid_entry, next); - if (name_is_equal(&name, tmp->name)) + if (instance_name_cmp(api, tmp->api) == 0) list_del(&tmp->next); } } -static void bootstrap_ipcp(struct irm * instance, - rina_name_t name, +static void bootstrap_ipcp(struct irm * instance, + instance_name_t * api, struct dif_config * conf) { pid_t pid = 0; - pid = find_pid_by_name(instance, &name); + pid = find_pid_by_name(instance, api); if (pid == 0) { LOG_ERR("No such IPCP"); return; @@ -138,16 +138,16 @@ static void bootstrap_ipcp(struct irm * instance, LOG_ERR("Could not bootstrap IPCP"); } -static void enroll_ipcp(struct irm * instance, - rina_name_t name, - char * dif_name) +static void enroll_ipcp(struct irm * instance, + instance_name_t * api, + char * dif_name) { - pid_t pid = 0; - char * member; + pid_t pid = 0; + char * member; char ** n_1_difs = NULL; ssize_t n_1_difs_size = 0; - pid = find_pid_by_name(instance, &name); + pid = find_pid_by_name(instance, api); if (pid == 0) { LOG_ERR("No such IPCP"); return; @@ -166,14 +166,14 @@ static void enroll_ipcp(struct irm * instance, LOG_ERR("Could not enroll IPCP"); } -static void reg_ipcp(struct irm * instance, - rina_name_t name, - char ** difs, - size_t difs_size) +static void reg_ipcp(struct irm * instance, + instance_name_t * api, + char ** difs, + size_t difs_size) { pid_t pid = 0; - pid = find_pid_by_name(instance, &name); + pid = find_pid_by_name(instance, api); if (pid == 0) { LOG_ERR("No such IPCP"); return; @@ -183,14 +183,14 @@ static void reg_ipcp(struct irm * instance, LOG_ERR("Could not register IPCP to N-1 DIF(s)"); } -static void unreg_ipcp(struct irm * instance, - rina_name_t name, - char ** difs, - size_t difs_size) +static void unreg_ipcp(struct irm * instance, + instance_name_t * api, + char ** difs, + size_t difs_size) { pid_t pid = 0; - pid = find_pid_by_name(instance, &name); + pid = find_pid_by_name(instance, api); if (pid == 0) { LOG_ERR("No such IPCP"); return; @@ -204,8 +204,8 @@ static void unreg_ipcp(struct irm * instance, int main() { struct irm * instance = NULL; - int sockfd; - uint8_t buf[IRM_MSG_BUF_SIZE]; + int sockfd; + uint8_t buf[IRM_MSG_BUF_SIZE]; instance = malloc(sizeof(*instance)); if (instance == NULL) @@ -221,7 +221,7 @@ int main() int cli_sockfd; irm_msg_t * msg; ssize_t count; - rina_name_t name; + instance_name_t api; cli_sockfd = accept(sockfd, 0, 0); if (cli_sockfd < 0) { @@ -235,31 +235,31 @@ int main() if (msg == NULL) continue; - name.ap_name = msg->ap_name; - name.api_id = msg->api_id; + api.name = msg->ap_name; + api.id = msg->api_id; switch (msg->code) { case IRM_MSG_CODE__IRM_CREATE_IPCP: - create_ipcp(instance, name, msg->ipcp_type); + create_ipcp(instance, &api, msg->ipcp_type); break; case IRM_MSG_CODE__IRM_DESTROY_IPCP: - destroy_ipcp(instance, name); + destroy_ipcp(instance, &api); break; case IRM_MSG_CODE__IRM_BOOTSTRAP_IPCP: - bootstrap_ipcp(instance, name, NULL); + bootstrap_ipcp(instance, &api, NULL); break; case IRM_MSG_CODE__IRM_ENROLL_IPCP: if (msg->n_dif_name != 1) continue; - enroll_ipcp(instance, name, msg->dif_name[0]); + enroll_ipcp(instance, &api, msg->dif_name[0]); break; case IRM_MSG_CODE__IRM_REG_IPCP: - reg_ipcp(instance, name, + reg_ipcp(instance, &api, msg->dif_name, msg->n_dif_name); break; case IRM_MSG_CODE__IRM_UNREG_IPCP: - unreg_ipcp(instance, name, + unreg_ipcp(instance, &api, msg->dif_name, msg->n_dif_name); break; diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index c986112e..f18b4d3b 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -26,10 +26,10 @@ set(SOURCE_FILES da.c dev.c du_buff.c + instance_name.c ipcp.c irm.c list.c - rina_name.c shm_du_map.c sockets.c utils.c diff --git a/src/lib/instance_name.c b/src/lib/instance_name.c new file mode 100644 index 00000000..1e61f790 --- /dev/null +++ b/src/lib/instance_name.c @@ -0,0 +1,274 @@ +/* + * RINA naming related utilities + * + * Sander Vrijders + * Francesco Salvestrini + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#define OUROBOROS_PREFIX "instance-name" + +#include +#include +#include +#include + +#include +#include +#include +#include + +#define instance_name_is_equal(a, b) (instance_name_cmp(a, b) == 0) + +static char * strdup(const char * src) +{ + int len = 0; + char * dst = NULL; + + if (src == NULL) + return NULL; + + len = strlen(src) + 1; + + dst = malloc(len); + if (dst == NULL) + return NULL; + + memcpy(dst, src, len); + + return dst; +} + +instance_name_t * instance_name_create() +{ + instance_name_t * tmp; + + tmp = malloc(sizeof *tmp); + + tmp->name = NULL; + tmp->id = 0; + + return tmp; +} + +instance_name_t * instance_name_init_from(instance_name_t * dst, + const char * name, + uint16_t id) +{ + if (dst == NULL) + return NULL; + + /* Clean up the destination, leftovers might be there ... */ + instance_name_fini(dst); + + dst->name = strdup(name); + dst->id = id; + + if (dst->name == NULL) { + instance_name_fini(dst); + return NULL; + } + + return dst; +} + +instance_name_t * instance_name_init_with(instance_name_t * dst, + char * name, + uint16_t id) +{ + if (dst == NULL) + return NULL; + + /* Clean up the destination, leftovers might be there ... */ + instance_name_fini(dst); + + dst->name = name; + dst->id = id; + + return dst; +} + +void instance_name_fini(instance_name_t * n) +{ + if (n == NULL) + return; + + if (n->name != NULL) { + free(n->name); + n->name = NULL; + } +} + +void instance_name_destroy(instance_name_t * ptr) +{ + if (ptr == NULL) + return; + + instance_name_fini(ptr); + + free(ptr); +} + +int instance_name_cpy(const instance_name_t * src, + instance_name_t * dst) +{ + instance_name_t * res; + + if (src == NULL || dst == NULL) + return -1; + + res = instance_name_init_from(dst, + src->name, + src->id); + if (res == NULL) + return -1; + + return 0; +} + +instance_name_t * instance_name_dup(const instance_name_t * src) +{ + instance_name_t * tmp; + + if (src == NULL) + return NULL; + + tmp = instance_name_create(); + if (tmp == NULL) + return NULL; + + if (instance_name_cpy(src, tmp)) { + instance_name_destroy(tmp); + return NULL; + } + + return tmp; +} + +bool instance_name_is_valid(const instance_name_t * n) +{ + return (n != NULL && + n->name != NULL && + strlen(n->name)); +} + +int instance_name_cmp(const instance_name_t * a, + const instance_name_t * b) +{ + + int ret = 0; + + if (a == NULL || b == NULL) { + LOG_DBGF("Won't compare NULL."); + return -2; + } + + if (a == b) + return 0; + + ret = strcmp(a->name, b->name); + + if (!ret) { + if (a->id == b-> id) + return 0; + else + return a->id < b->id ? -1 : 1; + } + + return ret; +} + + + +#define DELIMITER "/" + +char * instance_name_to_string(const instance_name_t * n) +{ + char * tmp; + size_t size; + const char * none = ""; + size_t none_len = strlen(none); + + if (n == NULL) + return NULL; + + size = 0; + + size += (n->name != NULL ? + strlen(n->name) : none_len); + size += strlen(DELIMITER); + + size += (n->id == 0 ? + 1 : n_digits(n->id)); + size += strlen(DELIMITER); + + tmp = malloc(size); + if (!tmp) + return NULL; + + if (sprintf(tmp, "%s%s%d", + (n->name != NULL ? n->name : none), + DELIMITER, n->id) + != size - 1) { + free(tmp); + return NULL; + } + + return tmp; +} + +instance_name_t * string_to_instance_name(const char * s) +{ + instance_name_t * name; + + char * tmp1 = NULL; + char * tmp_ap = NULL; + char * tmp_s_api = NULL; + unsigned int tmp_api = 0; + char * tmp2; + + if (s == NULL) + return NULL; + + tmp1 = strdup(s); + if (tmp1 == NULL) { + return NULL; + } + + tmp_ap = strtok(tmp1, DELIMITER); + tmp_s_api = strtok(NULL, DELIMITER); + if (tmp_s_api != NULL) + tmp_api = (unsigned int) strtol(tmp_s_api, &tmp2, 10); + + name = instance_name_create(); + if (name == NULL) { + if (tmp1 != NULL) + free(tmp1); + return NULL; + } + + if (!instance_name_init_from(name, tmp_ap, tmp_api)) { + instance_name_destroy(name); + if (tmp1 != NULL) + free(tmp1); + return NULL; + } + + if (tmp1 != NULL) + free(tmp1); + + return name; +} diff --git a/src/lib/ipcp.c b/src/lib/ipcp.c index 445160f0..60d5879e 100644 --- a/src/lib/ipcp.c +++ b/src/lib/ipcp.c @@ -85,8 +85,8 @@ static int send_ipcp_msg(pid_t pid, return 0; } -pid_t ipcp_create(rina_name_t name, - char * ipcp_type) +pid_t ipcp_create(instance_name_t * api, + char * ipcp_type) { pid_t pid = 0; char * api_id = NULL; @@ -107,12 +107,12 @@ pid_t ipcp_create(rina_name_t name, return pid; } - api_id = malloc(n_digits(name.api_id) + 1); + api_id = malloc(n_digits(api->id) + 1); if (!api_id) { LOG_ERR("Failed to malloc"); exit(EXIT_FAILURE); } - sprintf(api_id, "%d", name.api_id); + sprintf(api_id, "%d", api->id); len += strlen(INSTALL_DIR); len += strlen(ipcp_dir); @@ -129,7 +129,7 @@ pid_t ipcp_create(rina_name_t name, strcat(full_name, ipcp_dir); char * argv[] = {full_name, - name.ap_name, api_id, + api->name, api_id, ipcp_type, 0}; char * envp[] = {0}; diff --git a/src/lib/irm.c b/src/lib/irm.c index 92d8b3a5..af899d0a 100644 --- a/src/lib/irm.c +++ b/src/lib/irm.c @@ -26,22 +26,25 @@ #include #include #include +#include #include -int irm_create_ipcp(char * ap_name, - int api_id, - char * ipcp_type) +int irm_create_ipcp(instance_name_t * api, + char * ipcp_type) { irm_msg_t msg = IRM_MSG__INIT; - if (ipcp_type == NULL || ap_name == NULL) + if (api == NULL) + return -EINVAL; + + if (ipcp_type == NULL || api == NULL) return -EINVAL; msg.code = IRM_MSG_CODE__IRM_CREATE_IPCP; - msg.ap_name = ap_name; + msg.ap_name = api->name; msg.has_api_id = true; - msg.api_id = api_id; + msg.api_id = api->id; msg.ipcp_type = ipcp_type; if (send_irm_msg(&msg)) { @@ -52,19 +55,21 @@ int irm_create_ipcp(char * ap_name, return 0; } -int irm_destroy_ipcp(char * ap_name, - int api_id) +int irm_destroy_ipcp(instance_name_t * api) { irm_msg_t msg = IRM_MSG__INIT; - if (ap_name == NULL) { + if (api == NULL) + return -EINVAL; + + if (api->name == NULL) { return -EINVAL; } msg.code = IRM_MSG_CODE__IRM_DESTROY_IPCP; - msg.ap_name = ap_name; + msg.ap_name = api->name; msg.has_api_id = true; - msg.api_id = api_id; + msg.api_id = api->id; if (send_irm_msg(&msg)) { LOG_ERR("Failed to send message to daemon"); @@ -74,20 +79,21 @@ int irm_destroy_ipcp(char * ap_name, return 0; } -int irm_bootstrap_ipcp(char * ap_name, - int api_id, +int irm_bootstrap_ipcp(instance_name_t * api, struct dif_config * conf) { irm_msg_t msg = IRM_MSG__INIT; - if (ap_name == NULL || conf == NULL) { + if (api == NULL) + return -EINVAL; + + if (api->name == NULL || conf == NULL) return -EINVAL; - } msg.code = IRM_MSG_CODE__IRM_BOOTSTRAP_IPCP; - msg.ap_name = ap_name; + msg.ap_name = api->name; msg.has_api_id = true; - msg.api_id = api_id; + msg.api_id = api->id; if (send_irm_msg(&msg)) { LOG_ERR("Failed to send message to daemon"); @@ -97,20 +103,21 @@ int irm_bootstrap_ipcp(char * ap_name, return 0; } -int irm_enroll_ipcp(char * ap_name, - int api_id, - char * dif_name) +int irm_enroll_ipcp(instance_name_t * api, + char * dif_name) { irm_msg_t msg = IRM_MSG__INIT; - if (ap_name == NULL || dif_name == NULL) { + if (api == NULL) + return -EINVAL; + + if (api->name == NULL || dif_name == NULL) return -EINVAL; - } msg.code = IRM_MSG_CODE__IRM_ENROLL_IPCP; - msg.ap_name = ap_name; + msg.ap_name = api->name; msg.has_api_id = true; - msg.api_id = api_id; + msg.api_id = api->id; msg.n_dif_name = 1; msg.dif_name = malloc(sizeof(*(msg.dif_name))); if (msg.dif_name == NULL) { @@ -130,14 +137,13 @@ int irm_enroll_ipcp(char * ap_name, return 0; } -int irm_reg_ipcp(char * ap_name, - int api_id, - char ** difs, - size_t difs_size) +int irm_reg_ipcp(instance_name_t * api, + char ** difs, + size_t difs_size) { irm_msg_t msg = IRM_MSG__INIT; - if (ap_name == NULL || + if (api->name == NULL || difs == NULL || difs_size == 0 || difs[0] == NULL) { @@ -145,9 +151,9 @@ int irm_reg_ipcp(char * ap_name, } msg.code = IRM_MSG_CODE__IRM_REG_IPCP; - msg.ap_name = ap_name; + msg.ap_name = api->name; msg.has_api_id = true; - msg.api_id = api_id; + msg.api_id = api->id; msg.dif_name = difs; msg.n_dif_name = difs_size; @@ -159,14 +165,16 @@ int irm_reg_ipcp(char * ap_name, return 0; } -int irm_unreg_ipcp(char * ap_name, - int api_id, - char ** difs, - size_t difs_size) +int irm_unreg_ipcp(const instance_name_t * api, + char ** difs, + size_t difs_size) { irm_msg_t msg = IRM_MSG__INIT; - if (ap_name == NULL || + if (api == NULL) + return -EINVAL; + + if (api->name == NULL || difs == NULL || difs_size == 0 || difs[0] == NULL) { @@ -174,9 +182,9 @@ int irm_unreg_ipcp(char * ap_name, } msg.code = IRM_MSG_CODE__IRM_UNREG_IPCP; - msg.ap_name = ap_name; + msg.ap_name = api->name; msg.has_api_id = true; - msg.api_id = api_id; + msg.api_id = api->id; msg.dif_name = difs; msg.n_dif_name = difs_size; diff --git a/src/lib/rina_name.c b/src/lib/rina_name.c deleted file mode 100644 index 2dcfbb08..00000000 --- a/src/lib/rina_name.c +++ /dev/null @@ -1,275 +0,0 @@ -/* - * RINA naming related utilities - * - * Sander Vrijders - * Francesco Salvestrini - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - */ - -#define OUROBOROS_PREFIX "name-utils" - -#include -#include -#include -#include - -#include -#include -#include -#include - -static char * strdup(const char * src) -{ - int len = 0; - char * dst = NULL; - - if (src == NULL) - return NULL; - - len = strlen(src) + 1; - - dst = malloc(len); - if (dst == NULL) - return NULL; - - memcpy(dst, src, len); - - return dst; -} - -rina_name_t * name_create() -{ - rina_name_t * tmp; - - tmp = malloc(sizeof(rina_name_t)); - - tmp->ap_name = NULL; - tmp->api_id = 0; - - return tmp; -} - -rina_name_t * name_init_from(rina_name_t * dst, - const char * ap_name, - unsigned int api_id) -{ - if (dst == NULL) - return NULL; - - /* Clean up the destination, leftovers might be there ... */ - name_fini(dst); - - dst->ap_name = strdup(ap_name); - dst->api_id = api_id; - - if (dst->ap_name == NULL) { - name_fini(dst); - return NULL; - } - - return dst; -} - -rina_name_t * name_init_with(rina_name_t * dst, - char * ap_name, - unsigned int api_id) -{ - if (dst == NULL) - return NULL; - - /* Clean up the destination, leftovers might be there ... */ - name_fini(dst); - - dst->ap_name = ap_name; - dst->api_id = api_id; - - return dst; -} - -void name_fini(rina_name_t * n) -{ - if (n == NULL) - return; - - if (n->ap_name != NULL) { - free(n->ap_name); - n->ap_name = NULL; - } -} - -void name_destroy(rina_name_t * ptr) -{ - if (ptr == NULL) - return; - - name_fini(ptr); - - free(ptr); -} - -int name_cpy(const rina_name_t * src, - rina_name_t * dst) -{ - rina_name_t * res; - - if (src == NULL || dst == NULL) - return -1; - - res = name_init_from(dst, - src->ap_name, - src->api_id); - if (res == NULL) - return -1; - - return 0; -} - -rina_name_t * name_dup(const rina_name_t * src) -{ - rina_name_t * tmp; - - if (src == NULL) - return NULL; - - tmp = name_create(); - if (tmp == NULL) - return NULL; - - if (name_cpy(src, tmp)) { - name_destroy(tmp); - return NULL; - } - - return tmp; -} - -#define NAME_CMP_FIELD(X, Y, FIELD) \ - ((X->FIELD != NULL && Y->FIELD != NULL) ? \ - strcmp(X->FIELD, Y->FIELD) : \ - ((X->FIELD == NULL && Y->FIELD == NULL) ? 0 : -1)) - -bool name_is_ok(const rina_name_t * n) -{ return (n != NULL && - n->ap_name != NULL && - strlen(n->ap_name)); } - -bool name_cmp(uint8_t flags, - const rina_name_t * a, - const rina_name_t * b) -{ - if (a == b) - return true; - - if (a == NULL || b == NULL) - return false; - - if (!(flags & NAME_CMP_ALL)) - LOG_DBG("No flags, name comparison will be meaningless ..."); - - if (flags & NAME_CMP_APN) - if (NAME_CMP_FIELD(a, b, ap_name)) - return false; - - if (flags & NAME_CMP_API) - if (a->api_id != b->api_id) - return false; - - return true; -} - -bool name_is_equal(const rina_name_t * a, - const rina_name_t * b) -{ return name_cmp(NAME_CMP_ALL, a, b); } - -#define DELIMITER "/" - -char * name_to_string(const rina_name_t * n) -{ - char * tmp; - size_t size; - const char * none = ""; - size_t none_len = strlen(none); - - if (n == NULL) - return NULL; - - size = 0; - - size += (n->ap_name != NULL ? - strlen(n->ap_name) : none_len); - size += strlen(DELIMITER); - - size += (n->api_id == 0 ? - 1 : n_digits(n->api_id)); - size += strlen(DELIMITER); - - tmp = malloc(size); - if (!tmp) - return NULL; - - if (sprintf(tmp, "%s%s%d", - (n->ap_name != NULL ? n->ap_name : none), - DELIMITER, n->api_id) - != size - 1) { - free(tmp); - return NULL; - } - - return tmp; -} - -rina_name_t * string_to_name(const char * s) -{ - rina_name_t * name; - - char * tmp1 = NULL; - char * tmp_ap = NULL; - char * tmp_s_api = NULL; - unsigned int tmp_api = 0; - char * tmp2; - - if (s == NULL) - return NULL; - - tmp1 = strdup(s); - if (tmp1 == NULL) { - return NULL; - } - - tmp_ap = strtok(tmp1, DELIMITER); - tmp_s_api = strtok(NULL, DELIMITER); - if (tmp_s_api != NULL) - tmp_api = (unsigned int) strtol(tmp_s_api, &tmp2, 10); - - name = name_create(); - if (name == NULL) { - if (tmp1 != NULL) - free(tmp1); - return NULL; - } - - if (!name_init_from(name, tmp_ap, tmp_api)) { - name_destroy(name); - if (tmp1 != NULL) - free(tmp1); - return NULL; - } - - if (tmp1 != NULL) - free(tmp1); - - return name; -} diff --git a/src/tools/echo/echo_client.c b/src/tools/echo/echo_client.c index 196296f2..8d3fc322 100644 --- a/src/tools/echo/echo_client.c +++ b/src/tools/echo/echo_client.c @@ -23,7 +23,6 @@ #define CLIENT_AP_NAME "echo-client" #include -#include int client_main() { diff --git a/src/tools/echo/echo_server.c b/src/tools/echo/echo_server.c index 289f537a..b1547d8c 100644 --- a/src/tools/echo/echo_server.c +++ b/src/tools/echo/echo_server.c @@ -26,7 +26,6 @@ #include #include -#include #define DIF_NAME "*" diff --git a/src/tools/irm/irm.c b/src/tools/irm/irm.c index 895d52c2..d05e083e 100644 --- a/src/tools/irm/irm.c +++ b/src/tools/irm/irm.c @@ -21,7 +21,7 @@ */ #include -#include +#include #include #include #include diff --git a/src/tools/irm/irm_bootstrap_ipcp.c b/src/tools/irm/irm_bootstrap_ipcp.c index 0843083d..c7b82c4a 100644 --- a/src/tools/irm/irm_bootstrap_ipcp.c +++ b/src/tools/irm/irm_bootstrap_ipcp.c @@ -39,17 +39,16 @@ static void usage() int do_bootstrap_ipcp(int argc, char ** argv) { - char * ap_name = NULL; - int api_id = 0; + instance_name_t api = {NULL, 0}; struct dif_config conf; conf.qosspecs = NULL; while (argc > 0) { if (matches(*argv, "ap") == 0) { - ap_name = *(argv + 1); + api.name = *(argv + 1); } else if (matches(*argv, "api") == 0) { - api_id = atoi(*(argv + 1)); + api.id = atoi(*(argv + 1)); } else { printf("\"%s\" is unknown, try \"irm " "destroy_ipcp\".\n", *argv); @@ -61,10 +60,10 @@ int do_bootstrap_ipcp(int argc, char ** argv) argv += 2; } - if (ap_name == NULL) { + if (api.name == NULL) { usage(); return -1; } - return irm_bootstrap_ipcp(ap_name, api_id, &conf); + return irm_bootstrap_ipcp(&api, &conf); } diff --git a/src/tools/irm/irm_create_ipcp.c b/src/tools/irm/irm_create_ipcp.c index 3262bd5c..73d20dce 100644 --- a/src/tools/irm/irm_create_ipcp.c +++ b/src/tools/irm/irm_create_ipcp.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -41,16 +42,15 @@ static void usage() int do_create_ipcp(int argc, char ** argv) { char * ipcp_type = NULL; - char * ap_name = NULL; - int api_id = 0; + instance_name_t api = {NULL, 0}; while (argc > 0) { if (matches(*argv, "type") == 0) { ipcp_type = *(argv + 1); } else if (matches(*argv, "ap") == 0) { - ap_name = *(argv + 1); + api.name = *(argv + 1); } else if (matches(*argv, "api") == 0) { - api_id = atoi(*(argv + 1)); + api.id = atoi(*(argv + 1)); } else { printf("\"%s\" is unknown, try \"irm " "create_ipcp\".\n", *argv); @@ -61,10 +61,10 @@ int do_create_ipcp(int argc, char ** argv) argv += 2; } - if (ipcp_type == NULL || ap_name == NULL) { + if (ipcp_type == NULL || api.name == NULL) { usage(); return -1; } - return irm_create_ipcp(ap_name, api_id, ipcp_type); + return irm_create_ipcp(&api, ipcp_type); } diff --git a/src/tools/irm/irm_destroy_ipcp.c b/src/tools/irm/irm_destroy_ipcp.c index fbbeb5bd..fe6ef57e 100644 --- a/src/tools/irm/irm_destroy_ipcp.c +++ b/src/tools/irm/irm_destroy_ipcp.c @@ -37,14 +37,13 @@ static void usage() int do_destroy_ipcp(int argc, char ** argv) { - char * ap_name = NULL; - int api_id = 0; + instance_name_t api = {NULL, 0}; while (argc > 0) { if (matches(*argv, "ap") == 0) { - ap_name = *(argv + 1); + api.name = *(argv + 1); } else if (matches(*argv, "api") == 0) { - api_id = atoi(*(argv + 1)); + api.id = atoi(*(argv + 1)); } else { printf("\"%s\" is unknown, try \"irm " "destroy_ipcp\".\n", *argv); @@ -55,10 +54,10 @@ int do_destroy_ipcp(int argc, char ** argv) argv += 2; } - if (ap_name == NULL) { + if (api.name == NULL) { usage(); return -1; } - return irm_destroy_ipcp(ap_name, api_id); + return irm_destroy_ipcp(&api); } diff --git a/src/tools/irm/irm_enroll_ipcp.c b/src/tools/irm/irm_enroll_ipcp.c index 70798821..5c9572bf 100644 --- a/src/tools/irm/irm_enroll_ipcp.c +++ b/src/tools/irm/irm_enroll_ipcp.c @@ -38,15 +38,14 @@ static void usage() int do_enroll_ipcp(int argc, char ** argv) { - char * ap_name = NULL; - int api_id = 0; + instance_name_t api = {NULL, 0}; char * dif_name = NULL; while (argc > 0) { if (matches(*argv, "ap") == 0) { - ap_name = *(argv + 1); + api.name = *(argv + 1); } else if (matches(*argv, "api") == 0) { - api_id = atoi(*(argv + 1)); + api.id = atoi(*(argv + 1)); } else if (matches(*argv, "dif") == 0) { dif_name = *(argv + 1); } else { @@ -59,10 +58,10 @@ int do_enroll_ipcp(int argc, char ** argv) argv += 2; } - if (dif_name == NULL || ap_name == NULL) { + if (dif_name == NULL || api.name == NULL) { usage(); return -1; } - return irm_enroll_ipcp(ap_name, api_id, dif_name); + return irm_enroll_ipcp(&api, dif_name); } diff --git a/src/tools/irm/irm_register_ipcp.c b/src/tools/irm/irm_register_ipcp.c index b8808ecd..f0c1ccff 100644 --- a/src/tools/irm/irm_register_ipcp.c +++ b/src/tools/irm/irm_register_ipcp.c @@ -47,14 +47,13 @@ int do_register_ipcp(int argc, char ** argv) { char * difs[MAX_DIFS]; size_t difs_size = 0; - char * ap_name = NULL; - int api_id = 0; + instance_name_t api = {NULL, 0}; while (argc > 0) { if (matches(*argv, "ap") == 0) { - ap_name = *(argv + 1); + api.name = *(argv + 1); } else if (matches(*argv, "api") == 0) { - api_id = atoi(*(argv + 1)); + api.id = atoi(*(argv + 1)); } else if (matches(*argv, "dif") == 0) { difs[difs_size++] = *(argv + 1); if (difs_size > MAX_DIFS) { @@ -71,10 +70,10 @@ int do_register_ipcp(int argc, char ** argv) argv += 2; } - if (difs_size == 0 || ap_name == NULL) { + if (difs_size == 0 || api.name == NULL) { usage(); return -1; } - return irm_reg_ipcp(ap_name, api_id, difs, difs_size); + return irm_reg_ipcp(&api, difs, difs_size); } diff --git a/src/tools/irm/irm_unregister_ipcp.c b/src/tools/irm/irm_unregister_ipcp.c index 1321c263..3fd6f148 100644 --- a/src/tools/irm/irm_unregister_ipcp.c +++ b/src/tools/irm/irm_unregister_ipcp.c @@ -45,17 +45,16 @@ static void usage() int do_unregister_ipcp(int argc, char ** argv) { - char * ap_name = NULL; - int api_id = 0; + instance_name_t api = {NULL, 0}; char * difs[MAX_DIFS]; size_t difs_size = 0; while (argc > 0) { if (matches(*argv, "ap") == 0) { - ap_name = *(argv + 1); + api.name = *(argv + 1); } else if (matches(*argv, "api") == 0) { - api_id = atoi(*(argv + 1)); + api.id = atoi(*(argv + 1)); } else if (matches(*argv, "dif") == 0) { difs[difs_size++] = *(argv + 1); if (difs_size > MAX_DIFS) { @@ -72,10 +71,10 @@ int do_unregister_ipcp(int argc, char ** argv) argv += 2; } - if (difs_size == 0 || ap_name == NULL) { + if (difs_size == 0 || api.name == NULL) { usage(); return -1; } - return irm_unreg_ipcp(ap_name, api_id, difs, difs_size); + return irm_unreg_ipcp(&api, difs, difs_size); } diff --git a/src/tools/irm/irm_utils.h b/src/tools/irm/irm_utils.h index 3d328d95..da2259c6 100644 --- a/src/tools/irm/irm_utils.h +++ b/src/tools/irm/irm_utils.h @@ -20,8 +20,4 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -#include - -#include - int matches(const char * cmd, const char * pattern); -- cgit v1.2.3 From 08941177f030b77fb44238a7e589322d2e0fcaa2 Mon Sep 17 00:00:00 2001 From: Sander Vrijders Date: Wed, 30 Mar 2016 17:25:30 +0200 Subject: lib, irmd: Update communication with IRMd All messages sent to the IRMd now also get a reply back with the result of the operation. --- include/ouroboros/config.h.in | 9 +- include/ouroboros/shm_du_map.h | 4 - include/ouroboros/sockets.h | 1 - src/irmd/main.c | 311 +++++++++++++++++++++++++++++----------- src/lib/dev.c | 78 +++++++--- src/lib/ipcp.c | 8 +- src/lib/irm.c | 92 +++++++++--- src/lib/irmd_messages.proto | 23 ++- src/lib/sockets.c | 35 ----- src/lib/tests/shm_du_map_test.c | 1 + 10 files changed, 379 insertions(+), 183 deletions(-) (limited to 'src/irmd') diff --git a/include/ouroboros/config.h.in b/include/ouroboros/config.h.in index 0f5c2131..01d7888e 100644 --- a/include/ouroboros/config.h.in +++ b/include/ouroboros/config.h.in @@ -23,9 +23,10 @@ #ifndef OUROBOROS_CONFIG #define OUROBOROS_CONFIG -#define PROJECT_NAME "@CMAKE_PROJECT_NAME@" -#define PROJECT_VERSION "@PACKAGE_VERSION@" -#define INSTALL_DIR "@CMAKE_INSTALL_PREFIX@" -#define BUILD_TYPE "@CMAKE_BUILD_TYPE@" +#define PROJECT_NAME "@CMAKE_PROJECT_NAME@" +#define PROJECT_VERSION "@PACKAGE_VERSION@" +#define INSTALL_DIR "@CMAKE_INSTALL_PREFIX@" +#define BUILD_TYPE "@CMAKE_BUILD_TYPE@" +#define _POSIX_C_SOURCE 199506L #endif diff --git a/include/ouroboros/shm_du_map.h b/include/ouroboros/shm_du_map.h index 68492a91..fb51768d 100644 --- a/include/ouroboros/shm_du_map.h +++ b/include/ouroboros/shm_du_map.h @@ -24,10 +24,6 @@ #ifndef OUROBOROS_SHM_DU_MAP_H #define OUROBOROS_SHM_DU_MAP_H -#ifndef _POSIX_C_SOURCE -#define _POSIX_C_SOURCE 199506L -#endif - #ifndef SHM_DU_BUFF_BLOCK_SIZE #define SHM_DU_BUFF_BLOCK_SIZE sysconf(_SC_PAGESIZE) #endif diff --git a/include/ouroboros/sockets.h b/include/ouroboros/sockets.h index 4c736de2..9904eecd 100644 --- a/include/ouroboros/sockets.h +++ b/include/ouroboros/sockets.h @@ -45,7 +45,6 @@ char * ipcp_sock_path(pid_t pid); int server_socket_open(char * file_name); int client_socket_open(char * file_name); -int send_irm_msg(irm_msg_t * msg); irm_msg_t * send_recv_irm_msg(irm_msg_t * msg); #endif diff --git a/src/irmd/main.c b/src/irmd/main.c index 547286e8..af0f85e2 100644 --- a/src/irmd/main.c +++ b/src/irmd/main.c @@ -46,8 +46,9 @@ struct irm { struct list_head name_to_pid; }; -static pid_t find_pid_by_name(struct irm * instance, - instance_name_t * api) +struct irm * instance = NULL; + +static pid_t find_pid_by_name(instance_name_t * api) { struct list_head * pos; @@ -64,9 +65,8 @@ static pid_t find_pid_by_name(struct irm * instance, return 0; } -static void create_ipcp(struct irm * instance, - instance_name_t * api, - char * ipcp_type) +static int create_ipcp(instance_name_t * api, + char * ipcp_type) { pid_t pid; struct name_to_pid_entry * tmp; @@ -74,12 +74,12 @@ static void create_ipcp(struct irm * instance, pid = ipcp_create(api, ipcp_type); if (pid == -1) { LOG_ERR("Failed to create IPCP"); - return; + return -1; } tmp = malloc(sizeof(*tmp)); if (tmp == NULL) - return; + return -1; INIT_LIST_HEAD(&tmp->next); @@ -87,25 +87,25 @@ static void create_ipcp(struct irm * instance, tmp->api = instance_name_dup(api); if (tmp->api == NULL) { free(tmp); - return; + return -1; } LOG_DBG("Created IPC process with pid %d", pid); list_add(&tmp->next, &instance->name_to_pid); + return 0; } -static void destroy_ipcp(struct irm * instance, - instance_name_t * api) +static int destroy_ipcp(instance_name_t * api) { pid_t pid = 0; struct list_head * pos; struct list_head * n; - pid = find_pid_by_name(instance, api); + pid = find_pid_by_name(api); if (pid == 0) { LOG_ERR("No such IPCP"); - return; + return -1; } LOG_DBG("Destroying ipcp with pid %d", pid); @@ -120,92 +120,159 @@ static void destroy_ipcp(struct irm * instance, if (instance_name_cmp(api, tmp->api) == 0) list_del(&tmp->next); } + + return 0; } -static void bootstrap_ipcp(struct irm * instance, - instance_name_t * api, - struct dif_config * conf) +static int bootstrap_ipcp(instance_name_t * api, + struct dif_config * conf) { pid_t pid = 0; - pid = find_pid_by_name(instance, api); + pid = find_pid_by_name(api); if (pid == 0) { LOG_ERR("No such IPCP"); - return; + return -1; } - if (ipcp_bootstrap(pid, conf)) + if (ipcp_bootstrap(pid, conf)) { LOG_ERR("Could not bootstrap IPCP"); + return -1; + } + + return 0; } -static void enroll_ipcp(struct irm * instance, - instance_name_t * api, - char * dif_name) +static int enroll_ipcp(instance_name_t * api, + char * dif_name) { pid_t pid = 0; char * member; char ** n_1_difs = NULL; ssize_t n_1_difs_size = 0; - pid = find_pid_by_name(instance, api); + pid = find_pid_by_name(api); if (pid == 0) { LOG_ERR("No such IPCP"); - return; + return -1; } member = da_resolve_daf(dif_name); if (member == NULL) { LOG_ERR("Could not find a member of that DIF"); - return; + return -1; } n_1_difs_size = da_resolve_dap(member, n_1_difs); if (n_1_difs_size != 0) if (ipcp_enroll(pid, dif_name, member, - n_1_difs, n_1_difs_size)) + n_1_difs, n_1_difs_size)) { LOG_ERR("Could not enroll IPCP"); + return -1; + } + + return 0; } -static void reg_ipcp(struct irm * instance, - instance_name_t * api, - char ** difs, - size_t difs_size) +static int reg_ipcp(instance_name_t * api, + char ** difs, + size_t difs_size) { pid_t pid = 0; - pid = find_pid_by_name(instance, api); + pid = find_pid_by_name(api); if (pid == 0) { LOG_ERR("No such IPCP"); - return; + return -1; } - if (ipcp_reg(pid, difs, difs_size)) + if (ipcp_reg(pid, difs, difs_size)) { LOG_ERR("Could not register IPCP to N-1 DIF(s)"); + return -1; + } + + return 0; } -static void unreg_ipcp(struct irm * instance, - instance_name_t * api, - char ** difs, - size_t difs_size) +static int unreg_ipcp(instance_name_t * api, + char ** difs, + size_t difs_size) { pid_t pid = 0; - pid = find_pid_by_name(instance, api); + pid = find_pid_by_name(api); if (pid == 0) { LOG_ERR("No such IPCP"); - return; + return -1; } - if (ipcp_unreg(pid, difs, difs_size)) + if (ipcp_unreg(pid, difs, difs_size)) { LOG_ERR("Could not unregister IPCP from N-1 DIF(s)"); + return -1; + } + + return 0; +} + +static int ap_reg(char * ap_name, + char ** difs, + size_t difs_size) +{ + return -1; +} + +static int ap_unreg(char * ap_name, + char ** difs, + size_t difs_size) +{ + return -1; +} + +static int flow_accept(int fd, + char * ap_name, + char * ae_name) +{ + return -1; +} + +static int flow_alloc_resp(int fd, + int result) +{ + + return -1; +} + +static int flow_alloc(char * dst_ap_name, + char * src_ap_name, + char * src_ae_name, + struct qos_spec * qos, + int oflags) +{ + return -1; +} + +static int flow_alloc_res(int fd) +{ + + return -1; +} + +static int flow_dealloc(int fd) +{ + return -1; +} + +static int flow_cntl(int fd, + int oflags) +{ + return -1; } /* FIXME: Close sockfd on closing and release irm */ int main() { - struct irm * instance = NULL; - int sockfd; - uint8_t buf[IRM_MSG_BUF_SIZE]; + int sockfd; + uint8_t buf[IRM_MSG_BUF_SIZE]; instance = malloc(sizeof(*instance)); if (instance == NULL) @@ -222,6 +289,10 @@ int main() irm_msg_t * msg; ssize_t count; instance_name_t api; + buffer_t buffer; + irm_msg_t ret_msg = IRM_MSG__INIT; + + ret_msg.code = IRM_MSG_CODE__IRM_REPLY; cli_sockfd = accept(sockfd, 0, 0); if (cli_sockfd < 0) { @@ -230,47 +301,125 @@ int main() } count = read(cli_sockfd, buf, IRM_MSG_BUF_SIZE); - if (count > 0) { - msg = irm_msg__unpack(NULL, count, buf); - if (msg == NULL) - continue; - - api.name = msg->ap_name; - api.id = msg->api_id; - - switch (msg->code) { - case IRM_MSG_CODE__IRM_CREATE_IPCP: - create_ipcp(instance, &api, msg->ipcp_type); - break; - case IRM_MSG_CODE__IRM_DESTROY_IPCP: - destroy_ipcp(instance, &api); - break; - case IRM_MSG_CODE__IRM_BOOTSTRAP_IPCP: - bootstrap_ipcp(instance, &api, NULL); - break; - case IRM_MSG_CODE__IRM_ENROLL_IPCP: - if (msg->n_dif_name != 1) - continue; - enroll_ipcp(instance, &api, msg->dif_name[0]); - break; - case IRM_MSG_CODE__IRM_REG_IPCP: - reg_ipcp(instance, &api, - msg->dif_name, - msg->n_dif_name); - break; - case IRM_MSG_CODE__IRM_UNREG_IPCP: - unreg_ipcp(instance, &api, - msg->dif_name, - msg->n_dif_name); - break; - default: - LOG_ERR("Don't know that message code"); - break; - } - - irm_msg__free_unpacked(msg, NULL); + if (count <= 0) { + LOG_ERR("Failed to read from socket"); + close(cli_sockfd); + continue; + } + + msg = irm_msg__unpack(NULL, count, buf); + if (msg == NULL) { + close(cli_sockfd); + continue; + } + + api.name = msg->ap_name; + api.id = msg->api_id; + + switch (msg->code) { + case IRM_MSG_CODE__IRM_CREATE_IPCP: + ret_msg.has_result = true; + ret_msg.result = create_ipcp(&api, + msg->ipcp_type); + break; + case IRM_MSG_CODE__IRM_DESTROY_IPCP: + ret_msg.has_result = true; + ret_msg.result = destroy_ipcp(&api); + break; + case IRM_MSG_CODE__IRM_BOOTSTRAP_IPCP: + ret_msg.has_result = true; + ret_msg.result = bootstrap_ipcp(&api, NULL); + break; + case IRM_MSG_CODE__IRM_ENROLL_IPCP: + ret_msg.has_result = true; + ret_msg.result = enroll_ipcp(&api, + msg->dif_name[0]); + break; + case IRM_MSG_CODE__IRM_REG_IPCP: + ret_msg.has_result = true; + ret_msg.result = reg_ipcp(&api, + msg->dif_name, + msg->n_dif_name); + break; + case IRM_MSG_CODE__IRM_UNREG_IPCP: + ret_msg.has_result = true; + ret_msg.result = unreg_ipcp(&api, + msg->dif_name, + msg->n_dif_name); + break; + case IRM_MSG_CODE__IRM_AP_REG: + ret_msg.has_fd = true; + ret_msg.fd = ap_reg(msg->ap_name, + msg->dif_name, + msg->n_dif_name); + break; + case IRM_MSG_CODE__IRM_AP_UNREG: + ret_msg.has_result = true; + ret_msg.result = ap_unreg(msg->ap_name, + msg->dif_name, + msg->n_dif_name); + break; + case IRM_MSG_CODE__IRM_FLOW_ACCEPT: + ret_msg.has_fd = true; + ret_msg.fd = flow_accept(msg->fd, + ret_msg.ap_name, + ret_msg.ae_name); + break; + case IRM_MSG_CODE__IRM_FLOW_ALLOC_RESP: + ret_msg.has_result = true; + ret_msg.result = flow_alloc_resp(msg->fd, + msg->result); + break; + case IRM_MSG_CODE__IRM_FLOW_ALLOC: + ret_msg.has_fd = true; + ret_msg.fd = flow_alloc(msg->dst_ap_name, + msg->ap_name, + msg->ae_name, + NULL, + msg->oflags); + break; + case IRM_MSG_CODE__IRM_FLOW_ALLOC_RES: + ret_msg.has_result = true; + ret_msg.result = flow_alloc_res(msg->fd); + break; + case IRM_MSG_CODE__IRM_FLOW_DEALLOC: + ret_msg.has_result = true; + ret_msg.result = flow_dealloc(msg->fd); + break; + case IRM_MSG_CODE__IRM_FLOW_CONTROL: + ret_msg.has_result = true; + ret_msg.result = flow_cntl(msg->fd, + msg->oflags); + break; + default: + LOG_ERR("Don't know that message code"); + break; + } + + irm_msg__free_unpacked(msg, NULL); + + buffer.size = irm_msg__get_packed_size(&ret_msg); + if (buffer.size == 0) { + LOG_ERR("Failed to send reply message"); + close(cli_sockfd); + continue; + } + + buffer.data = malloc(buffer.size); + if (buffer.data == NULL) { + close(cli_sockfd); + continue; + } + + irm_msg__pack(&ret_msg, buffer.data); + + if (write(cli_sockfd, buffer.data, buffer.size) == -1) { + free(buffer.data); + close(cli_sockfd); + continue; } + free(buffer.data); close(cli_sockfd); } diff --git a/src/lib/dev.c b/src/lib/dev.c index 5c11d8bf..c138b009 100644 --- a/src/lib/dev.c +++ b/src/lib/dev.c @@ -49,7 +49,11 @@ int ap_reg(char * ap_name, msg.n_dif_name = difs_size; recv_msg = send_recv_irm_msg(&msg); - if (recv_msg == NULL) { + if (recv_msg == NULL) + return -1; + + if (recv_msg->has_fd == false) { + irm_msg__free_unpacked(recv_msg, NULL); return -1; } @@ -64,6 +68,8 @@ int ap_unreg(char * ap_name, size_t difs_size) { irm_msg_t msg = IRM_MSG__INIT; + irm_msg_t * recv_msg = NULL; + int ret = -1; if (ap_name == NULL || difs == NULL || @@ -77,12 +83,19 @@ int ap_unreg(char * ap_name, msg.dif_name = difs; msg.n_dif_name = difs_size; - if (send_irm_msg(&msg)) { - LOG_ERR("Failed to send message to daemon"); + recv_msg = send_recv_irm_msg(&msg); + if (recv_msg == NULL) + return -1; + + if (recv_msg->has_result == false) { + irm_msg__free_unpacked(recv_msg, NULL); return -1; } - return 0; + ret = recv_msg->result; + irm_msg__free_unpacked(recv_msg, NULL); + + return ret; } int flow_accept(int fd, @@ -102,9 +115,8 @@ int flow_accept(int fd, msg.fd = fd; recv_msg = send_recv_irm_msg(&msg); - if (recv_msg == NULL) { + if (recv_msg == NULL) return -1; - } if (recv_msg->has_fd == false) { irm_msg__free_unpacked(recv_msg, NULL); @@ -122,6 +134,8 @@ int flow_alloc_resp(int fd, int result) { irm_msg_t msg = IRM_MSG__INIT; + irm_msg_t * recv_msg = NULL; + int ret = -1; msg.code = IRM_MSG_CODE__IRM_FLOW_ALLOC_RESP; msg.has_fd = true; @@ -129,12 +143,19 @@ int flow_alloc_resp(int fd, msg.has_result = true; msg.result = result; - if (send_irm_msg(&msg)) { - LOG_ERR("Failed to send message to daemon"); + recv_msg = send_recv_irm_msg(&msg); + if (recv_msg == NULL) + return -1; + + if (recv_msg->has_result == false) { + irm_msg__free_unpacked(recv_msg, NULL); return -1; } - return 0; + ret = recv_msg->result; + irm_msg__free_unpacked(recv_msg, NULL); + + return ret; } int flow_alloc(char * dst_ap_name, @@ -150,8 +171,7 @@ int flow_alloc(char * dst_ap_name, if (dst_ap_name == NULL || src_ap_name == NULL || qos == NULL) { - LOG_ERR("Invalid arguments"); - return -1; + return -EINVAL; } msg.code = IRM_MSG_CODE__IRM_FLOW_ALLOC; @@ -162,9 +182,8 @@ int flow_alloc(char * dst_ap_name, msg.oflags = oflags; recv_msg = send_recv_irm_msg(&msg); - if (recv_msg == NULL) { + if (recv_msg == NULL) return -1; - } if (recv_msg->has_fd == false) { irm_msg__free_unpacked(recv_msg, NULL); @@ -187,9 +206,8 @@ int flow_alloc_res(int fd) msg.fd = fd; recv_msg = send_recv_irm_msg(&msg); - if (recv_msg == NULL) { + if (recv_msg == NULL) return -1; - } if (recv_msg->has_result == false) { irm_msg__free_unpacked(recv_msg, NULL); @@ -205,33 +223,51 @@ int flow_alloc_res(int fd) int flow_dealloc(int fd) { irm_msg_t msg = IRM_MSG__INIT; + irm_msg_t * recv_msg = NULL; + int ret = -1; msg.code = IRM_MSG_CODE__IRM_FLOW_DEALLOC; msg.has_fd = true; msg.fd = fd; - if (send_irm_msg(&msg)) { - LOG_ERR("Failed to send message to daemon"); + recv_msg = send_recv_irm_msg(&msg); + if (recv_msg == NULL) + return -1; + + if (recv_msg->has_result == false) { + irm_msg__free_unpacked(recv_msg, NULL); return -1; } - return 0; + ret = recv_msg->result; + irm_msg__free_unpacked(recv_msg, NULL); + + return ret; } int flow_cntl(int fd, int oflags) { irm_msg_t msg = IRM_MSG__INIT; + irm_msg_t * recv_msg = NULL; + int ret = -1; msg.has_fd = true; msg.fd = fd; msg.oflags = oflags; - if (send_irm_msg(&msg)) { - LOG_ERR("Failed to send message to daemon"); + recv_msg = send_recv_irm_msg(&msg); + if (recv_msg == NULL) + return -1; + + if (recv_msg->has_result == false) { + irm_msg__free_unpacked(recv_msg, NULL); return -1; } - return 0; + ret = recv_msg->result; + irm_msg__free_unpacked(recv_msg, NULL); + + return ret; } ssize_t flow_write(int fd, diff --git a/src/lib/ipcp.c b/src/lib/ipcp.c index 60d5879e..2caeaad3 100644 --- a/src/lib/ipcp.c +++ b/src/lib/ipcp.c @@ -22,14 +22,10 @@ #define OUROBOROS_PREFIX "lib-ipcp" -#ifndef _POSIX_C_SOURCE -#define _POSIX_C_SOURCE 199506L -#endif - +#include #include #include #include -#include #include #include @@ -97,6 +93,8 @@ pid_t ipcp_create(instance_name_t * api, if (ipcp_type == NULL) return -1; + LOG_DBG("%lu", _POSIX_C_SOURCE); + pid = fork(); if (pid == -1) { LOG_ERR("Failed to fork"); diff --git a/src/lib/irm.c b/src/lib/irm.c index 644e1113..7c187be1 100644 --- a/src/lib/irm.c +++ b/src/lib/irm.c @@ -34,6 +34,8 @@ int irm_create_ipcp(instance_name_t * api, char * ipcp_type) { irm_msg_t msg = IRM_MSG__INIT; + irm_msg_t * recv_msg = NULL; + int ret = -1; if (api == NULL || ipcp_type == NULL || api->name == NULL) return -EINVAL; @@ -44,17 +46,26 @@ int irm_create_ipcp(instance_name_t * api, msg.api_id = api->id; msg.ipcp_type = ipcp_type; - if (send_irm_msg(&msg)) { - LOG_ERR("Failed to send message to daemon"); + recv_msg = send_recv_irm_msg(&msg); + if (recv_msg == NULL) + return -1; + + if (recv_msg->has_result == false) { + irm_msg__free_unpacked(recv_msg, NULL); return -1; } - return 0; + ret = recv_msg->result; + irm_msg__free_unpacked(recv_msg, NULL); + + return ret; } int irm_destroy_ipcp(instance_name_t * api) { irm_msg_t msg = IRM_MSG__INIT; + irm_msg_t * recv_msg = NULL; + int ret = -1; if (api == NULL || api->name == NULL) return -EINVAL; @@ -64,18 +75,27 @@ int irm_destroy_ipcp(instance_name_t * api) msg.has_api_id = true; msg.api_id = api->id; - if (send_irm_msg(&msg)) { - LOG_ERR("Failed to send message to daemon"); + recv_msg = send_recv_irm_msg(&msg); + if (recv_msg == NULL) + return -1; + + if (recv_msg->has_result == false) { + irm_msg__free_unpacked(recv_msg, NULL); return -1; } - return 0; + ret = recv_msg->result; + irm_msg__free_unpacked(recv_msg, NULL); + + return ret; } int irm_bootstrap_ipcp(instance_name_t * api, struct dif_config * conf) { irm_msg_t msg = IRM_MSG__INIT; + irm_msg_t * recv_msg = NULL; + int ret = -1; if (api == NULL || api->name == NULL || conf == NULL) return -EINVAL; @@ -85,18 +105,27 @@ int irm_bootstrap_ipcp(instance_name_t * api, msg.has_api_id = true; msg.api_id = api->id; - if (send_irm_msg(&msg)) { - LOG_ERR("Failed to send message to daemon"); + recv_msg = send_recv_irm_msg(&msg); + if (recv_msg == NULL) + return -1; + + if (recv_msg->has_result == false) { + irm_msg__free_unpacked(recv_msg, NULL); return -1; } - return 0; + ret = recv_msg->result; + irm_msg__free_unpacked(recv_msg, NULL); + + return ret; } int irm_enroll_ipcp(instance_name_t * api, char * dif_name) { irm_msg_t msg = IRM_MSG__INIT; + irm_msg_t * recv_msg = NULL; + int ret = -1; if (api == NULL || api->name == NULL || dif_name == NULL) return -EINVAL; @@ -113,15 +142,22 @@ int irm_enroll_ipcp(instance_name_t * api, } msg.dif_name[0] = dif_name; - if (send_irm_msg(&msg)) { - LOG_ERR("Failed to send message to daemon"); + recv_msg = send_recv_irm_msg(&msg); + if (recv_msg == NULL) { free(msg.dif_name); return -1; } - free(msg.dif_name); + if (recv_msg->has_result == false) { + irm_msg__free_unpacked(recv_msg, NULL); + return -1; + } + + ret = recv_msg->result; + irm_msg__free_unpacked(recv_msg, NULL); - return 0; + free(msg.dif_name); + return ret; } int irm_reg_ipcp(instance_name_t * api, @@ -129,6 +165,8 @@ int irm_reg_ipcp(instance_name_t * api, size_t difs_size) { irm_msg_t msg = IRM_MSG__INIT; + irm_msg_t * recv_msg = NULL; + int ret = -1; if (api->name == NULL || difs == NULL || @@ -144,12 +182,19 @@ int irm_reg_ipcp(instance_name_t * api, msg.dif_name = difs; msg.n_dif_name = difs_size; - if (send_irm_msg(&msg)) { - LOG_ERR("Failed to send message to daemon"); + recv_msg = send_recv_irm_msg(&msg); + if (recv_msg == NULL) + return -1; + + if (recv_msg->has_result == false) { + irm_msg__free_unpacked(recv_msg, NULL); return -1; } - return 0; + ret = recv_msg->result; + irm_msg__free_unpacked(recv_msg, NULL); + + return ret; } int irm_unreg_ipcp(const instance_name_t * api, @@ -157,6 +202,8 @@ int irm_unreg_ipcp(const instance_name_t * api, size_t difs_size) { irm_msg_t msg = IRM_MSG__INIT; + irm_msg_t * recv_msg = NULL; + int ret = -1; if (api == NULL || api->name == NULL || @@ -173,10 +220,17 @@ int irm_unreg_ipcp(const instance_name_t * api, msg.dif_name = difs; msg.n_dif_name = difs_size; - if (send_irm_msg(&msg)) { - LOG_ERR("Failed to send message to daemon"); + recv_msg = send_recv_irm_msg(&msg); + if (recv_msg == NULL) + return -1; + + if (recv_msg->has_result == false) { + irm_msg__free_unpacked(recv_msg, NULL); return -1; } - return 0; + ret = recv_msg->result; + irm_msg__free_unpacked(recv_msg, NULL); + + return ret; } diff --git a/src/lib/irmd_messages.proto b/src/lib/irmd_messages.proto index c61d1b6d..a524a7fb 100644 --- a/src/lib/irmd_messages.proto +++ b/src/lib/irmd_messages.proto @@ -6,19 +6,16 @@ enum irm_msg_code { IRM_REG_IPCP = 5; IRM_UNREG_IPCP = 6; IRM_AP_REG = 7; - IRM_AP_REG_R = 8; - IRM_AP_UNREG = 9; - IRM_FLOW_ACCEPT = 10; - IRM_FLOW_ACCEPT_R = 11; - IRM_FLOW_ALLOC_RESP = 12; - IRM_FLOW_ALLOC = 13; - IRM_FLOW_ALLOC_R = 14; - IRM_FLOW_ALLOC_RES = 15; - IRM_FLOW_ALLOC_RES_R = 16; - IRM_FLOW_DEALLOC = 17; - IRM_FLOW_CONTROL = 18; - IRM_FLOW_WRITE = 19; - IRM_FLOW_READ = 20; + IRM_AP_UNREG = 8; + IRM_FLOW_ACCEPT = 9; + IRM_FLOW_ALLOC_RESP = 10; + IRM_FLOW_ALLOC = 11; + IRM_FLOW_ALLOC_RES = 12; + IRM_FLOW_DEALLOC = 13; + IRM_FLOW_CONTROL = 14; + IRM_FLOW_WRITE = 15; + IRM_FLOW_READ = 16; + IRM_REPLY = 17; }; message irm_msg { diff --git a/src/lib/sockets.c b/src/lib/sockets.c index a699206d..f9024f70 100644 --- a/src/lib/sockets.c +++ b/src/lib/sockets.c @@ -96,41 +96,6 @@ int server_socket_open(char * file_name) return sockfd; } -int send_irm_msg(irm_msg_t * msg) -{ - int sockfd; - buffer_t buf; - - sockfd = client_socket_open(IRM_SOCK_PATH); - if (sockfd < 0) - return -1; - - buf.size = irm_msg__get_packed_size(msg); - if (buf.size == 0) { - close(sockfd); - return -1; - } - - buf.data = malloc(buf.size); - if (buf.data == NULL) { - close(sockfd); - return -ENOMEM; - } - - irm_msg__pack(msg, buf.data); - - if (write(sockfd, buf.data, buf.size) == -1) { - free(buf.data); - close(sockfd); - return -1; - } - - free(buf.data); - - close(sockfd); - return 0; -} - irm_msg_t * send_recv_irm_msg(irm_msg_t * msg) { int sockfd; diff --git a/src/lib/tests/shm_du_map_test.c b/src/lib/tests/shm_du_map_test.c index f636c941..85a82e4d 100644 --- a/src/lib/tests/shm_du_map_test.c +++ b/src/lib/tests/shm_du_map_test.c @@ -20,6 +20,7 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ +#include #include #include #include -- cgit v1.2.3 From 151adbc851c9b2a218f2be9409199c1baa62bd8d Mon Sep 17 00:00:00 2001 From: Sander Vrijders Date: Thu, 31 Mar 2016 18:32:28 +0200 Subject: lib: Implementation of flow related ops This adds the messages that are sent to the IPCPs related to flows. Some messages are also sent to the IRMd (e.g. when a new flow arrives). --- include/ouroboros/ipcp.h | 18 +-- include/ouroboros/sockets.h | 2 +- src/irmd/main.c | 36 ++++- src/lib/ipcp.c | 349 +++++++++++++++++++++++++++++++++++++++---- src/lib/ipcpd_messages.proto | 11 +- src/lib/irmd_messages.proto | 7 +- 6 files changed, 376 insertions(+), 47 deletions(-) (limited to 'src/irmd') diff --git a/include/ouroboros/ipcp.h b/include/ouroboros/ipcp.h index b39a6b0d..fdaf5c4a 100644 --- a/include/ouroboros/ipcp.h +++ b/include/ouroboros/ipcp.h @@ -23,10 +23,10 @@ #ifndef OUROBOROS_IPCP_H #define OUROBOROS_IPCP_H -#include +#include +#include -#include "common.h" -#include "instance_name.h" +#include struct ipcp; @@ -44,19 +44,17 @@ int ipcp_unreg(pid_t pid, int ipcp_bootstrap(pid_t pid, struct dif_config * conf); -int ipcp_enroll(pid_t pid, - char * dif_name, - char * member_name, - char ** n_1_difs, - ssize_t n_1_difs_size); +int ipcp_enroll(pid_t pid, + char * member_name, + char * n_1_dif); /* Flow related ops, these go from IRMd to IPCP */ int ipcp_ap_reg(pid_t pid, uint32_t reg_api_id, char * ap_name); -int ipcp_ap_unreg(pid_t pid, - char * ap_name); +int ipcp_ap_unreg(pid_t pid, + uint32_t reg_api_id); int ipcp_flow_alloc(pid_t pid, uint32_t port_id, diff --git a/include/ouroboros/sockets.h b/include/ouroboros/sockets.h index 9904eecd..0c517bd4 100644 --- a/include/ouroboros/sockets.h +++ b/include/ouroboros/sockets.h @@ -37,7 +37,7 @@ typedef IpcpMsg ipcp_msg_t; #define IRM_MSG_BUF_SIZE 256 #define IPCP_SOCK_PATH_PREFIX "/tmp/ipcp_sock" -#define IPCP_MSG_BUFS_SIZE IRM_MSG_BUF_SIZE +#define IPCP_MSG_BUF_SIZE IRM_MSG_BUF_SIZE /* Returns the full socket path of an IPCP */ char * ipcp_sock_path(pid_t pid); diff --git a/src/irmd/main.c b/src/irmd/main.c index af0f85e2..0256248b 100644 --- a/src/irmd/main.c +++ b/src/irmd/main.c @@ -165,8 +165,7 @@ static int enroll_ipcp(instance_name_t * api, n_1_difs_size = da_resolve_dap(member, n_1_difs); if (n_1_difs_size != 0) - if (ipcp_enroll(pid, dif_name, member, - n_1_difs, n_1_difs_size)) { + if (ipcp_enroll(pid, member, n_1_difs[0])) { LOG_ERR("Could not enroll IPCP"); return -1; } @@ -268,6 +267,24 @@ static int flow_cntl(int fd, return -1; } +static int flow_req_arr(uint32_t reg_api_id, + char * ap_name, + char * ae_name) +{ + return -1; +} + +static int flow_alloc_reply(uint32_t port_id, + int result) +{ + return -1; +} + +static int flow_dealloc_ipcp(uint32_t port_id) +{ + return -1; +} + /* FIXME: Close sockfd on closing and release irm */ int main() { @@ -391,6 +408,21 @@ int main() ret_msg.result = flow_cntl(msg->fd, msg->oflags); break; + case IRM_MSG_CODE__IPCP_FLOW_REQ_ARR: + ret_msg.has_fd = true; + ret_msg.fd = flow_req_arr(msg->port_id, + msg->ap_name, + msg->ae_name); + break; + case IRM_MSG_CODE__IPCP_FLOW_ALLOC_REPLY: + ret_msg.has_result = true; + ret_msg.result = flow_alloc_reply(msg->port_id, + msg->result); + break; + case IRM_MSG_CODE__IPCP_FLOW_DEALLOC: + ret_msg.has_result = true; + ret_msg.result = flow_dealloc_ipcp(msg->port_id); + break; default: LOG_ERR("Don't know that message code"); break; diff --git a/src/lib/ipcp.c b/src/lib/ipcp.c index 2caeaad3..d61fcb50 100644 --- a/src/lib/ipcp.c +++ b/src/lib/ipcp.c @@ -35,35 +35,37 @@ #include #include -static int send_ipcp_msg(pid_t pid, - ipcp_msg_t * msg) +static ipcp_msg_t * send_recv_ipcp_msg(pid_t pid, + ipcp_msg_t * msg) { int sockfd = 0; buffer_t buf; - char * sock_path; + char * sock_path = NULL; + ssize_t count = 0; + ipcp_msg_t * recv_msg = NULL; sock_path = ipcp_sock_path(pid); if (sock_path == NULL) - return -1; + return NULL; sockfd = client_socket_open(sock_path); if (sockfd < 0) { free(sock_path); - return -1; + return NULL; } buf.size = ipcp_msg__get_packed_size(msg); if (buf.size == 0) { close(sockfd); free(sock_path); - return -1; + return NULL; } buf.data = malloc(buf.size); if (buf.data == NULL) { close(sockfd); free(sock_path); - return -ENOMEM; + return NULL; } ipcp_msg__pack(msg, buf.data); @@ -72,13 +74,29 @@ static int send_ipcp_msg(pid_t pid, free(sock_path); free(buf.data); close(sockfd); - return -1; + return NULL; + } + + count = read(sockfd, buf.data, IPCP_MSG_BUF_SIZE); + if (count <= 0) { + free(sock_path); + free(buf.data); + close(sockfd); + return NULL; + } + + recv_msg = ipcp_msg__unpack(NULL, count, buf.data); + if (recv_msg == NULL) { + free(sock_path); + free(buf.data); + close(sockfd); + return NULL; } free(buf.data); free(sock_path); close(sockfd); - return 0; + return recv_msg; } pid_t ipcp_create(instance_name_t * api, @@ -164,6 +182,8 @@ int ipcp_reg(pid_t pid, size_t difs_size) { ipcp_msg_t msg = IPCP_MSG__INIT; + ipcp_msg_t * recv_msg = NULL; + int ret = -1; if (difs == NULL || difs_size == 0 || @@ -174,12 +194,19 @@ int ipcp_reg(pid_t pid, msg.dif_name = difs; msg.n_dif_name = difs_size; - if (send_ipcp_msg(pid, &msg)) { - LOG_ERR("Failed to send message to daemon"); + recv_msg = send_recv_ipcp_msg(pid, &msg); + if (recv_msg == NULL) + return -1; + + if (recv_msg->has_result == false) { + ipcp_msg__free_unpacked(recv_msg, NULL); return -1; } - return 0; + ret = recv_msg->result; + ipcp_msg__free_unpacked(recv_msg, NULL); + + return ret; } int ipcp_unreg(pid_t pid, @@ -187,6 +214,8 @@ int ipcp_unreg(pid_t pid, size_t difs_size) { ipcp_msg_t msg = IPCP_MSG__INIT; + ipcp_msg_t * recv_msg = NULL; + int ret = -1; if (difs == NULL || difs_size == 0 || @@ -197,42 +226,54 @@ int ipcp_unreg(pid_t pid, msg.dif_name = difs; msg.n_dif_name = difs_size; - if (send_ipcp_msg(pid, &msg)) { - LOG_ERR("Failed to send message to daemon"); + recv_msg = send_recv_ipcp_msg(pid, &msg); + if (recv_msg == NULL) + return -1; + + if (recv_msg->has_result == false) { + ipcp_msg__free_unpacked(recv_msg, NULL); return -1; } - return 0; + ret = recv_msg->result; + ipcp_msg__free_unpacked(recv_msg, NULL); + + return ret; } int ipcp_bootstrap(pid_t pid, struct dif_config * conf) { ipcp_msg_t msg = IPCP_MSG__INIT; + ipcp_msg_t * recv_msg = NULL; + int ret = -1; msg.code = IPCP_MSG_CODE__IPCP_BOOTSTRAP; - if (send_ipcp_msg(pid, &msg)) { - LOG_ERR("Failed to send message to daemon"); + recv_msg = send_recv_ipcp_msg(pid, &msg); + if (recv_msg == NULL) + return -1; + + if (recv_msg->has_result == false) { + ipcp_msg__free_unpacked(recv_msg, NULL); return -1; } - return 0; + ret = recv_msg->result; + ipcp_msg__free_unpacked(recv_msg, NULL); + + return ret; } int ipcp_enroll(pid_t pid, - char * dif_name, char * member_name, - char ** n_1_difs, - ssize_t n_1_difs_size) + char * n_1_dif) { ipcp_msg_t msg = IPCP_MSG__INIT; + ipcp_msg_t * recv_msg = NULL; + int ret = -1; - if (n_1_difs == NULL || - n_1_difs_size == 0 || - n_1_difs[0] == NULL || - dif_name == NULL || - member_name == NULL) + if (n_1_dif == NULL || member_name == NULL) return -EINVAL; msg.code = IPCP_MSG_CODE__IPCP_ENROLL; @@ -241,17 +282,261 @@ int ipcp_enroll(pid_t pid, LOG_ERR("Failed to malloc"); return -1; } - msg.dif_name[0] = dif_name; + msg.dif_name[0] = n_1_dif; msg.ap_name = member_name; - msg.n_1_dif_name = n_1_difs; - msg.n_n_1_dif_name = n_1_difs_size; - if (send_ipcp_msg(pid, &msg)) { - LOG_ERR("Failed to send message to daemon"); + recv_msg = send_recv_ipcp_msg(pid, &msg); + if (recv_msg == NULL) { free(msg.dif_name); return -1; } + if (recv_msg->has_result == false) { + ipcp_msg__free_unpacked(recv_msg, NULL); + free(msg.dif_name); + return -1; + } + + ret = recv_msg->result; + ipcp_msg__free_unpacked(recv_msg, NULL); free(msg.dif_name); - return 0; + + return ret; +} + +int ipcp_ap_reg(pid_t pid, + uint32_t reg_api_id, + char * ap_name) +{ + ipcp_msg_t msg = IPCP_MSG__INIT; + ipcp_msg_t * recv_msg = NULL; + int ret = -1; + + if (ap_name == NULL) + return -1; + + msg.code = IPCP_MSG_CODE__IPCP_AP_REG; + msg.ap_name = ap_name; + msg.has_port_id = true; + msg.port_id = reg_api_id; + + recv_msg = send_recv_ipcp_msg(pid, &msg); + if (recv_msg == NULL) + return -1; + + if (recv_msg->has_result == false) { + ipcp_msg__free_unpacked(recv_msg, NULL); + return -1; + } + + ret = recv_msg->result; + ipcp_msg__free_unpacked(recv_msg, NULL); + + return ret; +} + +int ipcp_ap_unreg(pid_t pid, + uint32_t reg_api_id) +{ + ipcp_msg_t msg = IPCP_MSG__INIT; + ipcp_msg_t * recv_msg = NULL; + int ret = -1; + + msg.code = IPCP_MSG_CODE__IPCP_AP_UNREG; + msg.has_port_id = true; + msg.port_id = reg_api_id; + + recv_msg = send_recv_ipcp_msg(pid, &msg); + if (recv_msg == NULL) + return -1; + + if (recv_msg->has_result == false) { + ipcp_msg__free_unpacked(recv_msg, NULL); + return -1; + } + + ret = recv_msg->result; + ipcp_msg__free_unpacked(recv_msg, NULL); + + return ret; +} + +int ipcp_flow_alloc(pid_t pid, + uint32_t port_id, + char * dst_ap_name, + char * src_ap_name, + char * src_ae_name, + struct qos_spec * qos) +{ + ipcp_msg_t msg = IPCP_MSG__INIT; + ipcp_msg_t * recv_msg = NULL; + int ret = -1; + + if (dst_ap_name == NULL || src_ap_name == NULL || src_ae_name == NULL) + return -EINVAL; + + msg.code = IPCP_MSG_CODE__IPCP_FLOW_ALLOC; + msg.ap_name = src_ap_name; + msg.ae_name = src_ae_name; + msg.dst_ap_name = dst_ap_name; + msg.has_port_id = true; + msg.port_id = port_id; + + recv_msg = send_recv_ipcp_msg(pid, &msg); + if (recv_msg == NULL) + return -1; + + if (recv_msg->has_result == false) { + ipcp_msg__free_unpacked(recv_msg, NULL); + return -1; + } + + ret = recv_msg->result; + ipcp_msg__free_unpacked(recv_msg, NULL); + + return ret; +} + +int ipcp_flow_alloc_resp(pid_t pid, + uint32_t port_id, + int result) +{ + ipcp_msg_t msg = IPCP_MSG__INIT; + ipcp_msg_t * recv_msg = NULL; + int ret = -1; + + msg.code = IPCP_MSG_CODE__IPCP_FLOW_ALLOC_RESP; + msg.has_port_id = true; + msg.port_id = port_id; + msg.has_result = true; + msg.result = result; + + recv_msg = send_recv_ipcp_msg(pid, &msg); + if (recv_msg == NULL) + return -1; + + if (recv_msg->has_result == false) { + ipcp_msg__free_unpacked(recv_msg, NULL); + return -1; + } + + ret = recv_msg->result; + ipcp_msg__free_unpacked(recv_msg, NULL); + + return ret; +} + +int ipcp_flow_req_arr(pid_t pid, + uint32_t reg_api_id, + char * ap_name, + char * ae_name) +{ + irm_msg_t msg = IRM_MSG__INIT; + irm_msg_t * recv_msg = NULL; + int fd = -1; + + if (ap_name == NULL || ae_name == NULL) + return -EINVAL; + + msg.code = IRM_MSG_CODE__IPCP_FLOW_REQ_ARR; + msg.ap_name = ap_name; + msg.ae_name = ae_name; + msg.port_id = reg_api_id; + msg.has_port_id = true; + msg.pid = pid; + msg.has_pid = true; + + recv_msg = send_recv_irm_msg(&msg); + if (recv_msg == NULL) + return -1; + + if (recv_msg->has_fd == false) { + irm_msg__free_unpacked(recv_msg, NULL); + return -1; + } + + fd = recv_msg->fd; + irm_msg__free_unpacked(recv_msg, NULL); + + return fd; +} + +int ipcp_flow_alloc_reply(pid_t pid, + uint32_t port_id, + int result) +{ + irm_msg_t msg = IRM_MSG__INIT; + irm_msg_t * recv_msg = NULL; + int ret = -1; + + msg.code = IRM_MSG_CODE__IPCP_FLOW_ALLOC_REPLY; + msg.port_id = port_id; + msg.has_port_id = true; + msg.result = result; + msg.has_result = true; + + recv_msg = send_recv_irm_msg(&msg); + if (recv_msg == NULL) + return -1; + + if (recv_msg->has_result == false) { + irm_msg__free_unpacked(recv_msg, NULL); + return -1; + } + + ret = recv_msg->result; + irm_msg__free_unpacked(recv_msg, NULL); + + return ret; +} + + +int ipcp_flow_dealloc(pid_t pid, + uint32_t port_id) +{ + if (pid != 0) { + ipcp_msg_t msg = IPCP_MSG__INIT; + ipcp_msg_t * recv_msg = NULL; + int ret = -1; + + msg.code = IPCP_MSG_CODE__IPCP_FLOW_DEALLOC; + msg.has_port_id = true; + msg.port_id = port_id; + + recv_msg = send_recv_ipcp_msg(pid, &msg); + if (recv_msg == NULL) + return -1; + + if (recv_msg->has_result == false) { + ipcp_msg__free_unpacked(recv_msg, NULL); + return -1; + } + + ret = recv_msg->result; + ipcp_msg__free_unpacked(recv_msg, NULL); + + return ret; + } else { + irm_msg_t msg = IRM_MSG__INIT; + irm_msg_t * recv_msg = NULL; + int ret = -1; + + msg.code = IRM_MSG_CODE__IPCP_FLOW_DEALLOC; + msg.has_port_id = true; + msg.port_id = port_id; + + recv_msg = send_recv_irm_msg(&msg); + if (recv_msg == NULL) + return -1; + + if (recv_msg->has_result == false) { + irm_msg__free_unpacked(recv_msg, NULL); + return -1; + } + + ret = recv_msg->result; + irm_msg__free_unpacked(recv_msg, NULL); + + return ret; + } } diff --git a/src/lib/ipcpd_messages.proto b/src/lib/ipcpd_messages.proto index 0715fbe0..bcdd54ae 100644 --- a/src/lib/ipcpd_messages.proto +++ b/src/lib/ipcpd_messages.proto @@ -3,6 +3,12 @@ enum ipcp_msg_code { IPCP_ENROLL = 2; IPCP_REG = 3; IPCP_UNREG = 4; + IPCP_AP_REG = 5; + IPCP_AP_UNREG = 6; + IPCP_FLOW_ALLOC = 7; + IPCP_FLOW_ALLOC_RESP = 8; + IPCP_FLOW_DEALLOC = 9; + IPCP_REPLY = 10; }; message ipcp_msg { @@ -10,5 +16,8 @@ message ipcp_msg { optional string ap_name = 2; // Missing dif_config field here repeated string dif_name = 4; - repeated string n_1_dif_name = 5; + optional int32 result = 5; + optional uint32 port_id = 6; + optional string ae_name = 7; + optional string dst_ap_name = 8; }; diff --git a/src/lib/irmd_messages.proto b/src/lib/irmd_messages.proto index a524a7fb..44070755 100644 --- a/src/lib/irmd_messages.proto +++ b/src/lib/irmd_messages.proto @@ -15,7 +15,10 @@ enum irm_msg_code { IRM_FLOW_CONTROL = 14; IRM_FLOW_WRITE = 15; IRM_FLOW_READ = 16; - IRM_REPLY = 17; + IPCP_FLOW_REQ_ARR = 17; + IPCP_FLOW_ALLOC_REPLY = 18; + IPCP_FLOW_DEALLOC = 19; + IRM_REPLY = 20; }; message irm_msg { @@ -31,4 +34,6 @@ message irm_msg { // Missing qos_spec here optional int32 oflags = 10; optional string dst_ap_name = 11; + optional uint32 port_id = 12; + optional int32 pid = 13; }; -- cgit v1.2.3