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/main.c') 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/main.c') 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