diff options
author | dimitri staessens <dimitri.staessens@intec.ugent.be> | 2016-04-01 15:01:51 +0200 |
---|---|---|
committer | dimitri staessens <dimitri.staessens@intec.ugent.be> | 2016-04-01 15:01:51 +0200 |
commit | 65591e5211fb3c44450c52d7534918902e0760d5 (patch) | |
tree | b3f21e0bdb9cf0e773962db73be041bb86d0ee44 /src/lib | |
parent | 3d859bb9a75932a6cffdd37466d510b479c9aa71 (diff) | |
parent | 0e1aeba4f2d2de3ce827bfaeb1d9f2480e628529 (diff) | |
download | ouroboros-65591e5211fb3c44450c52d7534918902e0760d5.tar.gz ouroboros-65591e5211fb3c44450c52d7534918902e0760d5.zip |
Merge branch 'be' of bitbucket.org:ouroboros-rina/ouroboros into be-build
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/.gitignore | 1 | ||||
-rw-r--r-- | src/lib/CMakeLists.txt | 54 | ||||
-rw-r--r-- | src/lib/dev.c | 191 | ||||
-rw-r--r-- | src/lib/flow.c | 83 | ||||
-rw-r--r-- | src/lib/instance_name.c (renamed from src/lib/rina_name.c) | 155 | ||||
-rw-r--r-- | src/lib/ipcp.c | 431 | ||||
-rw-r--r-- | src/lib/ipcpd_messages.proto | 23 | ||||
-rw-r--r-- | src/lib/irm.c | 232 | ||||
-rw-r--r-- | src/lib/irmd_messages.proto | 39 | ||||
-rw-r--r-- | src/lib/sockets.c | 678 | ||||
-rw-r--r-- | src/lib/tests/shm_du_map_test.c | 1 |
11 files changed, 916 insertions, 972 deletions
diff --git a/src/lib/.gitignore b/src/lib/.gitignore new file mode 100644 index 00000000..8704469b --- /dev/null +++ b/src/lib/.gitignore @@ -0,0 +1 @@ +*.pb-c.[ch]
\ No newline at end of file diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index 42a4d5c0..1f7b0f55 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -4,40 +4,54 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR}) include_directories(${CMAKE_SOURCE_DIR}/include) include_directories(${CMAKE_BINARY_DIR}/include) +find_package(ProtobufC REQUIRED) +include_directories(${PROTOBUF_INCLUDE_DIRS}) +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 -) - -add_library(ouroboros SHARED ${SOURCE_FILES}) -target_link_libraries(ouroboros ${LIBRT_LIBRARIES} ${LIBPTHREAD_LIBRARIES}) + # Add source files here + bitmap.c + cdap.c + da.c + dev.c + du_buff.c + flow.c + instance_name.c + ipcp.c + irm.c + list.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 ${LIBRT_LIBRARIES} + ${LIBPTHREAD_LIBRARIES} ${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) +target_include_directories(ouroboros PUBLIC ${CMAKE_CURRENT_BINARY_DIR}) + add_subdirectory(tests) diff --git a/src/lib/dev.c b/src/lib/dev.c index 7c0c8a15..c138b009 100644 --- a/src/lib/dev.c +++ b/src/lib/dev.c @@ -32,30 +32,33 @@ int ap_reg(char * ap_name, char ** difs, size_t difs_size) { - struct irm_msg msg; - struct irm_msg * recv_msg = NULL; + irm_msg_t msg = IRM_MSG__INIT; + irm_msg_t * recv_msg = NULL; int fd = 0; if (ap_name == NULL || difs == NULL || - difs_size == 0) { - LOG_ERR("Invalid arguments"); - return -1; + difs_size == 0 || + difs[0] == NULL) { + return -EINVAL; } - msg.code = IRM_AP_REG; + msg.code = IRM_MSG_CODE__IRM_AP_REG; msg.ap_name = ap_name; - msg.difs = difs; - msg.difs_size = difs_size; + msg.dif_name = difs; + msg.n_dif_name = difs_size; + + recv_msg = send_recv_irm_msg(&msg); + if (recv_msg == NULL) + return -1; - recv_msg = send_recv_irmd_msg(&msg); - if (recv_msg == NULL) { - LOG_ERR("Failed to send and receive message"); + if (recv_msg->has_fd == false) { + irm_msg__free_unpacked(recv_msg, NULL); return -1; } fd = recv_msg->fd; - free(recv_msg); + irm_msg__free_unpacked(recv_msg, NULL); return fd; } @@ -64,76 +67,95 @@ int ap_unreg(char * ap_name, char ** difs, size_t difs_size) { - struct irm_msg msg; + irm_msg_t msg = IRM_MSG__INIT; + irm_msg_t * recv_msg = NULL; + int ret = -1; if (ap_name == NULL || difs == NULL || - difs_size == 0) { - LOG_ERR("Invalid arguments"); - return -1; + difs_size == 0 || + difs[0] == NULL) { + return -EINVAL; } - msg.code = IRM_AP_UNREG; + msg.code = IRM_MSG_CODE__IRM_AP_UNREG; msg.ap_name = ap_name; - msg.difs = difs; - msg.difs_size = difs_size; + msg.dif_name = difs; + msg.n_dif_name = difs_size; - if (send_irmd_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, char * ap_name, char * ae_name) { - struct irm_msg msg; - struct irm_msg * recv_msg = NULL; + irm_msg_t msg = IRM_MSG__INIT; + irm_msg_t * recv_msg = NULL; int cli_fd = 0; if (ap_name == NULL) { - LOG_ERR("Invalid arguments"); - return -1; + return -EINVAL; } - msg.code = IRM_FLOW_ACCEPT; + msg.code = IRM_MSG_CODE__IRM_FLOW_ACCEPT; + msg.has_fd = true; msg.fd = fd; - recv_msg = send_recv_irmd_msg(&msg); - if (recv_msg == NULL) { - LOG_ERR("Failed to send and receive message"); + 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; + } cli_fd = recv_msg->fd; ap_name = recv_msg->ap_name; - if (ae_name == NULL) - ae_name = ""; - else - ae_name = recv_msg->ae_name; - free(recv_msg); + ae_name = recv_msg->ae_name; + irm_msg__free_unpacked(recv_msg, NULL); return cli_fd; } int flow_alloc_resp(int fd, int result) { - struct irm_msg msg; + irm_msg_t msg = IRM_MSG__INIT; + irm_msg_t * recv_msg = NULL; + int ret = -1; - msg.code = IRM_FLOW_ALLOC_RESP; + msg.code = IRM_MSG_CODE__IRM_FLOW_ALLOC_RESP; + msg.has_fd = true; msg.fd = fd; + msg.has_result = true; msg.result = result; - if (send_irmd_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, @@ -142,87 +164,110 @@ int flow_alloc(char * dst_ap_name, struct qos_spec * qos, int oflags) { - struct irm_msg msg; - struct irm_msg * recv_msg = NULL; + irm_msg_t msg = IRM_MSG__INIT; + irm_msg_t * recv_msg = NULL; int fd = 0; if (dst_ap_name == NULL || - src_ap_name == NULL) { - LOG_ERR("Invalid arguments"); - return -1; + src_ap_name == NULL || + qos == NULL) { + return -EINVAL; } - msg.code = IRM_FLOW_ALLOC; + msg.code = IRM_MSG_CODE__IRM_FLOW_ALLOC; msg.dst_ap_name = dst_ap_name; msg.ap_name = src_ap_name; - if (src_ae_name == NULL) - msg.ae_name = ""; - else - msg.ae_name = src_ae_name; - msg.qos = qos; + msg.ae_name = src_ae_name; + msg.has_oflags = true; msg.oflags = oflags; - recv_msg = send_recv_irmd_msg(&msg); - if (recv_msg == NULL) { - LOG_ERR("Failed to send and receive message"); + 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; - free(recv_msg); - + irm_msg__free_unpacked(recv_msg, NULL); return fd; } int flow_alloc_res(int fd) { - struct irm_msg msg; - struct irm_msg * recv_msg = NULL; + irm_msg_t msg = IRM_MSG__INIT; + irm_msg_t * recv_msg = NULL; int result = 0; - msg.code = IRM_FLOW_ALLOC_RES; + msg.code = IRM_MSG_CODE__IRM_FLOW_ALLOC_RES; + msg.has_fd = true; msg.fd = fd; - recv_msg = send_recv_irmd_msg(&msg); - if (recv_msg == NULL) { - LOG_ERR("Failed to send and receive message"); + 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; } result = recv_msg->result; - free(recv_msg); + irm_msg__free_unpacked(recv_msg, NULL); return result; } int flow_dealloc(int fd) { - struct irm_msg msg; + irm_msg_t msg = IRM_MSG__INIT; + irm_msg_t * recv_msg = NULL; + int ret = -1; - msg.code = IRM_FLOW_DEALLOC; + msg.code = IRM_MSG_CODE__IRM_FLOW_DEALLOC; + msg.has_fd = true; msg.fd = fd; - if (send_irmd_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) { - struct irm_msg msg; + 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_irmd_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/flow.c b/src/lib/flow.c new file mode 100644 index 00000000..04166298 --- /dev/null +++ b/src/lib/flow.c @@ -0,0 +1,83 @@ +/* + * Ouroboros - Copyright (C) 2016 + * + * Flows + * + * Dimitri Staessens <dimitri.staessens@intec.ugent.be> + * + * 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. + */ + +#include <malloc.h> +#include <ouroboros/flow.h> + +#define OUROBOROS_PREFIX "ipcpd/flow" + +#include <ouroboros/logs.h> + +flow_t * flow_create(int32_t port_id) +{ + flow_t * flow = malloc(sizeof *flow); + if (flow == NULL) { + LOG_DBGF("Could not malloc flow."); + return NULL; + } + + flow->port_id = port_id; + flow->oflags = FLOW_O_DEFAULT; + flow->state = FLOW_INIT; + + pthread_mutex_init(&flow->lock, NULL); + + return flow; +} + +void flow_destroy(flow_t * flow) +{ + free(flow); +} + +int flow_set_opts(flow_t * flow, uint16_t opts) +{ + if (flow == NULL) { + LOG_DBGF("Non-existing flow."); + return -1; + } + + pthread_mutex_lock(&flow->lock); + + if ((opts & FLOW_O_ACCMODE) == FLOW_O_ACCMODE) { + flow->oflags = FLOW_O_DEFAULT; + pthread_mutex_unlock(&flow->lock); + LOG_WARN("Invalid flow options. Setting default."); + return -1; + } + + flow->oflags = opts; + + pthread_mutex_unlock(&flow->lock); + + return 0; +} + +uint16_t flow_get_opts(const flow_t * flow) +{ + if (flow == NULL) { + LOG_DBGF("Non-existing flow."); + return FLOW_O_INVALID; + } + + return flow->oflags; +} diff --git a/src/lib/rina_name.c b/src/lib/instance_name.c index 2dcfbb08..0f666211 100644 --- a/src/lib/rina_name.c +++ b/src/lib/instance_name.c @@ -19,11 +19,11 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -#define OUROBOROS_PREFIX "name-utils" +#define OUROBOROS_PREFIX "instance-name" #include <ouroboros/logs.h> #include <ouroboros/common.h> -#include <ouroboros/rina_name.h> +#include <ouroboros/instance_name.h> #include <ouroboros/utils.h> #include <string.h> @@ -31,6 +31,8 @@ #include <malloc.h> #include <stdlib.h> +#define instance_name_is_equal(a, b) (instance_name_cmp(a, b) == 0) + static char * strdup(const char * src) { int len = 0; @@ -50,153 +52,144 @@ static char * strdup(const char * src) return dst; } -rina_name_t * name_create() +instance_name_t * instance_name_create() { - rina_name_t * tmp; + instance_name_t * tmp; - tmp = malloc(sizeof(rina_name_t)); + tmp = malloc(sizeof *tmp); - tmp->ap_name = NULL; - tmp->api_id = 0; + tmp->name = NULL; + tmp->id = 0; return tmp; } -rina_name_t * name_init_from(rina_name_t * dst, - const char * ap_name, - unsigned int api_id) +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 ... */ - name_fini(dst); + instance_name_fini(dst); - dst->ap_name = strdup(ap_name); - dst->api_id = api_id; + dst->name = strdup(name); + dst->id = id; - if (dst->ap_name == NULL) { - name_fini(dst); + if (dst->name == NULL) { + instance_name_fini(dst); return NULL; } return dst; } -rina_name_t * name_init_with(rina_name_t * dst, - char * ap_name, - unsigned int api_id) +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 ... */ - name_fini(dst); + instance_name_fini(dst); - dst->ap_name = ap_name; - dst->api_id = api_id; + dst->name = name; + dst->id = id; return dst; } -void name_fini(rina_name_t * n) +void instance_name_fini(instance_name_t * n) { - if (n == NULL) + if (n == NULL || n->name == NULL) return; - if (n->ap_name != NULL) { - free(n->ap_name); - n->ap_name = NULL; - } + free(n->name); + n->name = NULL; } -void name_destroy(rina_name_t * ptr) +void instance_name_destroy(instance_name_t * ptr) { if (ptr == NULL) return; - name_fini(ptr); + instance_name_fini(ptr); free(ptr); } -int name_cpy(const rina_name_t * src, - rina_name_t * dst) +int instance_name_cpy(instance_name_t * dst, + const instance_name_t * src) { - rina_name_t * res; + instance_name_t * res; if (src == NULL || dst == NULL) return -1; - res = name_init_from(dst, - src->ap_name, - src->api_id); + res = instance_name_init_from(dst, src->name, src->id); if (res == NULL) return -1; return 0; } -rina_name_t * name_dup(const rina_name_t * src) +instance_name_t * instance_name_dup(const instance_name_t * src) { - rina_name_t * tmp; + instance_name_t * tmp; if (src == NULL) return NULL; - tmp = name_create(); + tmp = instance_name_create(); if (tmp == NULL) return NULL; - if (name_cpy(src, tmp)) { - name_destroy(tmp); + if (instance_name_cpy(tmp, src)) { + instance_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 instance_name_is_valid(const instance_name_t * n) +{ + return (n != NULL && n->name != NULL && strlen(n->name)); +} -bool name_cmp(uint8_t flags, - const rina_name_t * a, - const rina_name_t * b) +int instance_name_cmp(const instance_name_t * a, + const instance_name_t * b) { - if (a == b) - return true; - if (a == NULL || b == NULL) - return false; + int ret = 0; + + if (a == NULL || b == NULL) { + LOG_DBGF("Won't compare NULL."); + return -2; + } - if (!(flags & NAME_CMP_ALL)) - LOG_DBG("No flags, name comparison will be meaningless ..."); + if (a == b) + return 0; - if (flags & NAME_CMP_APN) - if (NAME_CMP_FIELD(a, b, ap_name)) - return false; + ret = strcmp(a->name, b->name); - if (flags & NAME_CMP_API) - if (a->api_id != b->api_id) - return false; + if (!ret) { + if (a->id == b-> id) + return 0; + else + return a->id < b->id ? -1 : 1; + } - return true; + return ret; } -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 * instance_name_to_string(const instance_name_t * n) { char * tmp; size_t size; @@ -206,14 +199,14 @@ char * name_to_string(const rina_name_t * n) if (n == NULL) return NULL; - size = 0; + size = 0; - size += (n->ap_name != NULL ? - strlen(n->ap_name) : none_len); + size += (n->name != NULL ? + strlen(n->name) : none_len); size += strlen(DELIMITER); - size += (n->api_id == 0 ? - 1 : n_digits(n->api_id)); + size += (n->id == 0 ? + 1 : n_digits(n->id)); size += strlen(DELIMITER); tmp = malloc(size); @@ -221,8 +214,8 @@ char * name_to_string(const rina_name_t * n) return NULL; if (sprintf(tmp, "%s%s%d", - (n->ap_name != NULL ? n->ap_name : none), - DELIMITER, n->api_id) + (n->name != NULL ? n->name : none), + DELIMITER, n->id) != size - 1) { free(tmp); return NULL; @@ -231,9 +224,9 @@ char * name_to_string(const rina_name_t * n) return tmp; } -rina_name_t * string_to_name(const char * s) +instance_name_t * string_to_instance_name(const char * s) { - rina_name_t * name; + instance_name_t * name; char * tmp1 = NULL; char * tmp_ap = NULL; @@ -254,15 +247,15 @@ rina_name_t * string_to_name(const char * s) if (tmp_s_api != NULL) tmp_api = (unsigned int) strtol(tmp_s_api, &tmp2, 10); - name = name_create(); + name = instance_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 (!instance_name_init_from(name, tmp_ap, tmp_api)) { + instance_name_destroy(name); if (tmp1 != NULL) free(tmp1); return NULL; diff --git a/src/lib/ipcp.c b/src/lib/ipcp.c index 53d717ba..d61fcb50 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 <ouroboros/config.h> #include <ouroboros/ipcp.h> #include <ouroboros/common.h> #include <ouroboros/logs.h> -#include <ouroboros/config.h> #include <ouroboros/utils.h> #include <ouroboros/sockets.h> @@ -39,47 +35,72 @@ #include <sys/types.h> #include <sys/wait.h> -static int send_ipcp_msg(pid_t pid, - struct ipcp_msg * msg) +static ipcp_msg_t * send_recv_ipcp_msg(pid_t pid, + ipcp_msg_t * msg) { int sockfd = 0; - buffer_t * buf = NULL; - char * sock_path; + buffer_t buf; + 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 = serialize_ipcp_msg(msg); - if (buf == NULL) { + buf.size = ipcp_msg__get_packed_size(msg); + if (buf.size == 0) { + close(sockfd); free(sock_path); + return NULL; + } + + buf.data = malloc(buf.size); + if (buf.data == NULL) { close(sockfd); - return -1; + free(sock_path); + return NULL; } - if (write(sockfd, buf->data, buf->size) == -1) { + 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; + return NULL; } - free(buf->data); - free(buf); + 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(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; @@ -90,6 +111,8 @@ pid_t ipcp_create(rina_name_t name, if (ipcp_type == NULL) return -1; + LOG_DBG("%lu", _POSIX_C_SOURCE); + pid = fork(); if (pid == -1) { LOG_ERR("Failed to fork"); @@ -100,12 +123,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); @@ -122,7 +145,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}; @@ -158,84 +181,362 @@ int ipcp_reg(pid_t pid, char ** difs, size_t difs_size) { - struct ipcp_msg msg; + ipcp_msg_t msg = IPCP_MSG__INIT; + ipcp_msg_t * recv_msg = NULL; + int ret = -1; - 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; + + recv_msg = send_recv_ipcp_msg(pid, &msg); + if (recv_msg == NULL) + return -1; - if (send_ipcp_msg(pid, &msg)) { - LOG_ERR("Failed to send message to daemon"); + 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, char ** difs, size_t difs_size) { - struct ipcp_msg msg; + ipcp_msg_t msg = IPCP_MSG__INIT; + ipcp_msg_t * recv_msg = NULL; + int ret = -1; - 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"); + 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) + struct dif_config * conf) { - struct ipcp_msg msg; + ipcp_msg_t msg = IPCP_MSG__INIT; + ipcp_msg_t * recv_msg = NULL; + int ret = -1; - 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"); + 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) { - struct ipcp_msg msg; + ipcp_msg_t msg = IPCP_MSG__INIT; + ipcp_msg_t * recv_msg = NULL; + int ret = -1; + + if (n_1_dif == NULL || member_name == NULL) + return -EINVAL; - if (n_1_difs == NULL) + 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.dif_name[0] = n_1_dif; + msg.ap_name = member_name; - if (dif_name == NULL) + recv_msg = send_recv_ipcp_msg(pid, &msg); + if (recv_msg == NULL) { + free(msg.dif_name); return -1; + } - msg.code = IPCP_ENROLL; - msg.dif_name = dif_name; - msg.ap_name = member_name; - msg.difs = n_1_difs; - msg.difs_size = n_1_difs_size; + 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 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 (send_ipcp_msg(pid, &msg)) { - LOG_ERR("Failed to send message to daemon"); + 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; } - return 0; + 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 new file mode 100644 index 00000000..bcdd54ae --- /dev/null +++ b/src/lib/ipcpd_messages.proto @@ -0,0 +1,23 @@ +enum ipcp_msg_code { + IPCP_BOOTSTRAP = 1; + 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 { + required ipcp_msg_code code = 1; + optional string ap_name = 2; + // Missing dif_config field here + repeated string dif_name = 4; + 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/irm.c b/src/lib/irm.c index 9fd13d52..7c187be1 100644 --- a/src/lib/irm.c +++ b/src/lib/irm.c @@ -26,141 +26,211 @@ #include <ouroboros/common.h> #include <ouroboros/logs.h> #include <ouroboros/sockets.h> +#include <ouroboros/instance_name.h> + #include <stdlib.h> -int irm_create_ipcp(rina_name_t name, - char * ipcp_type) +int irm_create_ipcp(instance_name_t * api, + char * ipcp_type) { - struct irm_msg msg; - - if (ipcp_type == NULL) - return -1; + irm_msg_t msg = IRM_MSG__INIT; + irm_msg_t * recv_msg = NULL; + int ret = -1; - if (!name_is_ok(&name)) { - LOG_ERR("Bad name"); - return -1; - } + if (api == NULL || ipcp_type == NULL || api->name == NULL) + return -EINVAL; - msg.code = IRM_CREATE_IPCP; - msg.name = &name; + msg.code = IRM_MSG_CODE__IRM_CREATE_IPCP; + msg.ap_name = api->name; + msg.has_api_id = true; + msg.api_id = api->id; msg.ipcp_type = ipcp_type; - if (send_irmd_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(rina_name_t name) +int irm_destroy_ipcp(instance_name_t * api) { - struct irm_msg msg; + irm_msg_t msg = IRM_MSG__INIT; + irm_msg_t * recv_msg = NULL; + int ret = -1; - if (!name_is_ok(&name)) { - LOG_ERR("Bad name"); - return -1; - } + if (api == NULL || api->name == NULL) + return -EINVAL; - msg.code = IRM_DESTROY_IPCP; - msg.name = &name; + msg.code = IRM_MSG_CODE__IRM_DESTROY_IPCP; + msg.ap_name = api->name; + msg.has_api_id = true; + msg.api_id = api->id; + + recv_msg = send_recv_irm_msg(&msg); + if (recv_msg == NULL) + return -1; - if (send_irmd_msg(&msg)) { - LOG_ERR("Failed to send message to daemon"); + 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(rina_name_t name, - struct dif_config conf) +int irm_bootstrap_ipcp(instance_name_t * api, + struct dif_config * conf) { - struct irm_msg msg; + irm_msg_t msg = IRM_MSG__INIT; + irm_msg_t * recv_msg = NULL; + int ret = -1; - if (!name_is_ok(&name)) { - LOG_ERR("Bad name"); - return -1; - } + if (api == NULL || api->name == NULL || conf == NULL) + return -EINVAL; + + msg.code = IRM_MSG_CODE__IRM_BOOTSTRAP_IPCP; + msg.ap_name = api->name; + msg.has_api_id = true; + msg.api_id = api->id; - msg.code = IRM_BOOTSTRAP_IPCP; - msg.name = &name; - msg.conf = &conf; + recv_msg = send_recv_irm_msg(&msg); + if (recv_msg == NULL) + return -1; - if (send_irmd_msg(&msg)) { - LOG_ERR("Failed to send message to daemon"); + 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(rina_name_t name, - char * dif_name) +int irm_enroll_ipcp(instance_name_t * api, + char * dif_name) { - struct irm_msg msg; - - if (!name_is_ok(&name)) { - LOG_ERR("Bad 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; + + msg.code = IRM_MSG_CODE__IRM_ENROLL_IPCP; + msg.ap_name = api->name; + msg.has_api_id = true; + msg.api_id = api->id; + msg.n_dif_name = 1; + msg.dif_name = malloc(sizeof(*(msg.dif_name))); + if (msg.dif_name == NULL) { + LOG_ERR("Failed to malloc"); return -1; } + msg.dif_name[0] = dif_name; - msg.code = IRM_ENROLL_IPCP; - msg.name = &name; - msg.dif_name = dif_name; + recv_msg = send_recv_irm_msg(&msg); + if (recv_msg == NULL) { + free(msg.dif_name); + return -1; + } - if (send_irmd_msg(&msg)) { - LOG_ERR("Failed to send message to daemon"); + 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); + + free(msg.dif_name); + return ret; } -int irm_reg_ipcp(rina_name_t name, - char ** difs, - size_t difs_size) +int irm_reg_ipcp(instance_name_t * api, + char ** difs, + size_t difs_size) { - struct irm_msg msg; - - if (!name_is_ok(&name)) { - LOG_ERR("Bad name"); - return -1; + irm_msg_t msg = IRM_MSG__INIT; + irm_msg_t * recv_msg = NULL; + int ret = -1; + + if (api->name == NULL || + difs == NULL || + difs_size == 0 || + difs[0] == NULL) { + return -EINVAL; } - msg.code = IRM_REG_IPCP; - msg.name = &name; - msg.difs = difs; - msg.difs_size = difs_size; + msg.code = IRM_MSG_CODE__IRM_REG_IPCP; + msg.ap_name = api->name; + msg.has_api_id = true; + msg.api_id = api->id; + msg.dif_name = difs; + msg.n_dif_name = difs_size; - if (send_irmd_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(rina_name_t name, - char ** difs, - size_t difs_size) +int irm_unreg_ipcp(const instance_name_t * api, + char ** difs, + size_t difs_size) { - struct irm_msg msg; - - if (!name_is_ok(&name)) { - LOG_ERR("Bad name"); - return -1; + irm_msg_t msg = IRM_MSG__INIT; + irm_msg_t * recv_msg = NULL; + int ret = -1; + + if (api == NULL || + api->name == NULL || + difs == NULL || + difs_size == 0 || + difs[0] == NULL) { + return -EINVAL; } - msg.code = IRM_UNREG_IPCP; - msg.name = &name; - msg.difs = difs; - msg.difs_size = difs_size; + msg.code = IRM_MSG_CODE__IRM_UNREG_IPCP; + msg.ap_name = api->name; + msg.has_api_id = true; + msg.api_id = api->id; + msg.dif_name = difs; + msg.n_dif_name = difs_size; + + recv_msg = send_recv_irm_msg(&msg); + if (recv_msg == NULL) + return -1; - if (send_irmd_msg(&msg)) { - LOG_ERR("Failed to send message to daemon"); + 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 new file mode 100644 index 00000000..44070755 --- /dev/null +++ b/src/lib/irmd_messages.proto @@ -0,0 +1,39 @@ +enum irm_msg_code { + IRM_CREATE_IPCP = 1; + IRM_DESTROY_IPCP = 2; + IRM_BOOTSTRAP_IPCP = 3; + IRM_ENROLL_IPCP = 4; + IRM_REG_IPCP = 5; + IRM_UNREG_IPCP = 6; + IRM_AP_REG = 7; + 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; + IPCP_FLOW_REQ_ARR = 17; + IPCP_FLOW_ALLOC_REPLY = 18; + IPCP_FLOW_DEALLOC = 19; + IRM_REPLY = 20; +}; + +message irm_msg { + required irm_msg_code code = 1; + optional string ap_name = 2; + optional uint32 api_id = 3; + optional string ae_name = 4; + optional string ipcp_type = 5; + // Missing dif_config field here + repeated string dif_name = 7; + optional int32 fd = 8; + optional int32 result = 9; + // Missing qos_spec here + optional int32 oflags = 10; + optional string dst_ap_name = 11; + optional uint32 port_id = 12; + optional int32 pid = 13; +}; diff --git a/src/lib/sockets.c b/src/lib/sockets.c index b157b628..f9024f70 100644 --- a/src/lib/sockets.c +++ b/src/lib/sockets.c @@ -96,76 +96,58 @@ int server_socket_open(char * file_name) return sockfd; } -int send_irmd_msg(struct irm_msg * msg) -{ - int sockfd; - buffer_t * buf; - - sockfd = client_socket_open(IRM_SOCK_PATH); - if (sockfd < 0) - return -1; - - buf = serialize_irm_msg(msg); - if (buf == NULL) { - close(sockfd); - return -1; - } - - if (write(sockfd, buf->data, buf->size) == -1) { - free(buf->data); - free(buf); - close(sockfd); - return -1; - } - - free(buf->data); - free(buf); - - close(sockfd); - return 0; -} - -struct irm_msg * send_recv_irmd_msg(struct irm_msg * msg) +irm_msg_t * send_recv_irm_msg(irm_msg_t * msg) { int sockfd; - buffer_t * buf; + buffer_t buf; ssize_t count = 0; - struct irm_msg * recv_msg = NULL; + irm_msg_t * recv_msg = NULL; sockfd = client_socket_open(IRM_SOCK_PATH); if (sockfd < 0) return NULL; - buf = serialize_irm_msg(msg); - if (buf == NULL) { + buf.size = irm_msg__get_packed_size(msg); + if (buf.size == 0) { close(sockfd); return NULL; } - if (write(sockfd, buf->data, buf->size) == -1) { - free(buf->data); - free(buf); + LOG_DBG("Size will be %lu", buf.size); + buf.data = malloc(buf.size); + if (buf.data == NULL) { close(sockfd); return NULL; } - count = read(sockfd, buf->data, IRM_MSG_BUF_SIZE); - if (count <= 0) { - free(buf->data); - free(buf); + irm_msg__pack(msg, buf.data); + + if (write(sockfd, buf.data, buf.size) == -1) { + free(buf.data); close(sockfd); return NULL; } - recv_msg = deserialize_irm_msg(buf); + count = read(sockfd, buf.data, IRM_MSG_BUF_SIZE); + if (count <= 0) { + free(buf.data); + close(sockfd); + return NULL; + } - free(buf->data); - free(buf); + recv_msg = irm_msg__unpack(NULL, count, buf.data); + if (recv_msg == NULL) { + free(buf.data); + close(sockfd); + return NULL; + } + free(buf.data); close(sockfd); return recv_msg; } + char * ipcp_sock_path(pid_t pid) { char * full_name = NULL; @@ -196,611 +178,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 ser_copy_name(rina_name_t * name, - uint8_t * data, - int * offset) -{ - ser_copy_value(strlen(name->ap_name) + 1, - data, name->ap_name, offset); - ser_copy_value(sizeof(int), data, - &name->api_id, offset); -} - -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_int(uint8_t * data, - unsigned int * dst, - int * offset) -{ - *dst = 0; - deser_copy_value(sizeof(int), dst, data, offset); -} - -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); -} - -static rina_name_t * deser_copy_name(uint8_t * data, - int * offset) -{ - rina_name_t * name; - - name = name_create(); - if (name == NULL) { - LOG_ERR("Failed to alloc memory"); - return NULL; - } - - if (deser_copy_string(data, &name->ap_name, offset)) { - name_destroy(name); - return NULL; - } - - deser_copy_int(data, &name->api_id, offset); - - return name; -} - - -/* 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_irm_msg(struct irm_msg * msg) -{ - buffer_t * buf; - uint8_t * data; - int offset = 0; - int i; - char ** pos; - - if (msg == NULL) - return NULL; - - buf = buffer_create(); - if (buf == NULL) - return NULL; - - data = buf->data; - - ser_copy_value(sizeof(enum irm_msg_code), data, &msg->code, &offset); - - ser_copy_name(msg->name, data, &offset); - - switch (msg->code) { - case IRM_CREATE_IPCP: - if (msg->ipcp_type == NULL || - !name_is_ok(msg->name)) { - LOG_ERR("Null pointer passed"); - buffer_destroy(buf); - return NULL; - } - ser_copy_name(msg->name, data, &offset); - ser_copy_value(strlen(msg->ipcp_type) + 1, data, - msg->ipcp_type, &offset); - break; - case IRM_DESTROY_IPCP: - if (!name_is_ok(msg->name)) { - LOG_ERR("Null pointer passed"); - buffer_destroy(buf); - return NULL; - } - ser_copy_name(msg->name, data, &offset); - break; - case IRM_BOOTSTRAP_IPCP: - if (!name_is_ok(msg->name)) { - LOG_ERR("Null pointer passed"); - buffer_destroy(buf); - return NULL; - } - ser_copy_name(msg->name, data, &offset); - /* FIXME: Fields missing, need to define dif_conf properly */ - break; - case IRM_ENROLL_IPCP: - if (msg->dif_name == NULL || - !name_is_ok(msg->name)) { - buffer_destroy(buf); - return NULL; - } - - ser_copy_name(msg->name, data, &offset); - ser_copy_value(strlen(msg->dif_name) + 1, data, - msg->dif_name, &offset); - - break; - case IRM_REG_IPCP: - case IRM_UNREG_IPCP: - if (msg->difs == NULL || - msg->difs[0] == NULL || - !name_is_ok(msg->name)) { - buffer_destroy(buf); - return NULL; - } - - ser_copy_name(msg->name, data, &offset); - - 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; - case IRM_AP_REG: - case IRM_AP_UNREG: - if (msg->ap_name == NULL || - msg->difs == NULL || - msg->difs[0] == NULL) { - LOG_ERR("Invalid arguments"); - buffer_destroy(buf); - return NULL; - } - - ser_copy_value(strlen(msg->ap_name) + 1, - data, msg->ap_name, &offset); - - 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; - case IRM_FLOW_ACCEPT: - if (msg->ap_name == NULL || - msg->ae_name == NULL) { - LOG_ERR("Invalid arguments"); - buffer_destroy(buf); - return NULL; - } - - ser_copy_value(sizeof(int), data, &msg->fd, &offset); - - ser_copy_value(strlen(msg->ap_name) + 1, - data, msg->ap_name, &offset); - - ser_copy_value(strlen(msg->ae_name) + 1, - data, msg->ae_name, &offset); - - break; - case IRM_FLOW_ALLOC_RESP: - ser_copy_value(sizeof(int), data, &msg->fd, &offset); - - ser_copy_value(sizeof(int), data, &msg->result, &offset); - - break; - case IRM_FLOW_ALLOC: - if (msg->ap_name == NULL || - msg->ae_name == NULL || - msg->dst_ap_name == NULL || - msg->qos == NULL) { - LOG_ERR("Invalid arguments"); - buffer_destroy(buf); - return NULL; - } - - ser_copy_value(strlen(msg->dst_ap_name) + 1, - data, msg->dst_ap_name, &offset); - - ser_copy_value(strlen(msg->ap_name) + 1, - data, msg->ap_name, &offset); - - ser_copy_value(strlen(msg->ae_name) + 1, - data, msg->ae_name, &offset); - - /* FIXME: Serialize qos spec here */ - - ser_copy_value(sizeof(int), data, &msg->oflags, &offset); - - break; - case IRM_FLOW_ALLOC_RES: - case IRM_FLOW_DEALLOC: - ser_copy_value(sizeof(int), data, &msg->fd, &offset); - break; - case IRM_FLOW_CONTROL: - ser_copy_value(sizeof(int), data, &msg->fd, &offset); - ser_copy_value(sizeof(int), data, &msg->oflags, &offset); - break; - case IRM_FLOW_WRITE: - LOG_MISSING; - break; - case IRM_FLOW_READ: - LOG_MISSING; - break; - case IRM_AP_REG_R: - case IRM_FLOW_ALLOC_R: - ser_copy_value(sizeof(int), data, &msg->fd, &offset); - break; - case IRM_FLOW_ACCEPT_R: - ser_copy_value(sizeof(int), data, &msg->fd, &offset); - - ser_copy_value(strlen(msg->ap_name) + 1, - data, msg->ap_name, &offset); - - ser_copy_value(strlen(msg->ae_name) + 1, - data, msg->ae_name, &offset); - break; - case IRM_FLOW_ALLOC_RES_R: - ser_copy_value(sizeof(int), data, &msg->result, &offset); - break; - default: - LOG_ERR("Don't know that code"); - buffer_destroy(buf); - return NULL; - } - - buf->size = offset; - - return buf; -} - -struct irm_msg * deserialize_irm_msg(buffer_t * data) -{ - struct irm_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 irm_msg_code), - &msg->code, data->data, &offset); - - switch (msg->code) { - case IRM_CREATE_IPCP: - msg->name = deser_copy_name(data->data, &offset); - if (msg->name == NULL) { - LOG_ERR("Failed to reconstruct name"); - free(msg); - return NULL; - } - - if (deser_copy_string(data->data, - &msg->ipcp_type, - &offset)) { - name_destroy(msg->name); - free(msg); - return NULL; - } - - break; - case IRM_DESTROY_IPCP: - msg->name = deser_copy_name(data->data, &offset); - if (msg->name == NULL) { - LOG_ERR("Failed to reconstruct name"); - free(msg); - return NULL; - } - - break; - case IRM_BOOTSTRAP_IPCP: - msg->name = deser_copy_name(data->data, &offset); - if (msg->name == NULL) { - LOG_ERR("Failed to reconstruct name"); - free(msg); - return NULL; - } - - break; - case IRM_ENROLL_IPCP: - msg->name = deser_copy_name(data->data, &offset); - if (msg->name == NULL) { - LOG_ERR("Failed to reconstruct name"); - free(msg); - return NULL; - } - - if (deser_copy_string(data->data, - &msg->dif_name, - &offset)) { - name_destroy(msg->name); - free(msg); - return NULL; - } - - break; - case IRM_REG_IPCP: - case IRM_UNREG_IPCP: - msg->name = deser_copy_name(data->data, &offset); - if (msg->name == NULL) { - LOG_ERR("Failed to reconstruct 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) { - name_destroy(msg->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->difs); - name_destroy(msg->name); - free(msg); - return NULL; - } - } - - break; - case IRM_AP_REG: - case IRM_AP_REG_R: - case IRM_AP_UNREG: - case IRM_FLOW_ACCEPT: - case IRM_FLOW_ACCEPT_R: - case IRM_FLOW_ALLOC_RESP: - case IRM_FLOW_ALLOC: - case IRM_FLOW_ALLOC_R: - case IRM_FLOW_ALLOC_RES: - case IRM_FLOW_ALLOC_RES_R: - case IRM_FLOW_DEALLOC: - case IRM_FLOW_CONTROL: - case IRM_FLOW_WRITE: - case IRM_FLOW_READ: - LOG_MISSING; - break; - default: - LOG_ERR("Don't know that code"); - free(msg); - return NULL; - } - - return msg; -} - -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; -} 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 <ouroboros/config.h> #include <ouroboros/shm_du_map.h> #include <sys/types.h> #include <stdlib.h> |