From d37add0f20c93432c0b4c12866810c124a7a18ec Mon Sep 17 00:00:00 2001 From: Sander Vrijders Date: Fri, 25 Mar 2016 19:13:32 +0100 Subject: build: Add protobuf-c commands for cmake This adds a cmake file so that the build can ask to generate protobuf-c files from .proto files. The messages between the IRM and the library are compiled into the library. --- src/lib/CMakeLists.txt | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'src/lib/CMakeLists.txt') diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index 7ce98bf2..e05dce8b 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -3,6 +3,12 @@ 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}) +include_directories(${CMAKE_CURRENT_BINARY_DIR}) + +protobuf_generate_c(PROTO_SRCS PROTO_HDRS irmd_messages.proto) find_library(LIBRT_LIBRARIES rt) if(NOT LIBRT_LIBRARIES) @@ -30,8 +36,8 @@ set(SOURCE_FILES utils.c ) -add_library(ouroboros SHARED ${SOURCE_FILES}) -target_link_libraries(ouroboros rt pthread) +add_library(ouroboros SHARED ${SOURCE_FILES} ${PROTO_SRCS} ${PROTO_HDRS}) +target_link_libraries(ouroboros rt pthread ${PROTOBUF_LIBRARIES}) include(MacroAddCompileFlags) if (CMAKE_BUILD_TYPE MATCHES Debug) -- cgit v1.2.3 From a14d696bdbc72754e8019fa9579d5a338cc85a05 Mon Sep 17 00:00:00 2001 From: Sander Vrijders Date: Mon, 28 Mar 2016 14:43:16 +0200 Subject: lib: Update irm.h API Removes rina_name_t from that API. Passing ap_name and api_id as params instead. The IRM tool has been updated accordingly. Some errors in the build related to protobuf-c have also been resolved. --- cmake/FindProtobufC.cmake | 4 +- include/ouroboros/irm.h | 32 ++++---- include/ouroboros/sockets.h | 5 ++ src/lib/.gitignore | 1 + src/lib/CMakeLists.txt | 11 ++- src/lib/irm.c | 141 +++++++++++++++++++++--------------- src/lib/sockets.c | 7 ++ src/tools/irm/irm_bootstrap_ipcp.c | 20 +++-- src/tools/irm/irm_create_ipcp.c | 28 +++---- src/tools/irm/irm_destroy_ipcp.c | 17 +++-- src/tools/irm/irm_enroll_ipcp.c | 29 ++++---- src/tools/irm/irm_register_ipcp.c | 32 ++++---- src/tools/irm/irm_unregister_ipcp.c | 31 ++++---- src/tools/irm/irm_utils.c | 16 ---- src/tools/irm/irm_utils.h | 2 - 15 files changed, 207 insertions(+), 169 deletions(-) create mode 100644 src/lib/.gitignore (limited to 'src/lib/CMakeLists.txt') diff --git a/cmake/FindProtobufC.cmake b/cmake/FindProtobufC.cmake index cae9f1c3..bfa50110 100644 --- a/cmake/FindProtobufC.cmake +++ b/cmake/FindProtobufC.cmake @@ -24,11 +24,11 @@ function(PROTOBUF_GENERATE_C SRCS HDRS) get_filename_component(ABS_FIL ${FIL} ABSOLUTE) get_filename_component(FIL_WE ${FIL} NAME_WE) - list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb-c.cc") + list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb-c.c") list(APPEND ${HDRS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb-c.h") add_custom_command( - OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb-c.cc" + OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb-c.c" "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb-c.h" COMMAND ${PROTOBUF_PROTOC_C_EXECUTABLE} ARGS --c_out=${CMAKE_CURRENT_BINARY_DIR} ${_protobuf_include_path} ${ABS_FIL} diff --git a/include/ouroboros/irm.h b/include/ouroboros/irm.h index a6f0d9f3..fe72aefe 100644 --- a/include/ouroboros/irm.h +++ b/include/ouroboros/irm.h @@ -26,20 +26,26 @@ #include "common.h" #include "rina_name.h" -int irm_create_ipcp(rina_name_t name, - char * ipcp_type); -int irm_destroy_ipcp(rina_name_t name); +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_bootstrap_ipcp(rina_name_t name, - struct dif_config conf); -int irm_enroll_ipcp(rina_name_t name, - char * dif_name); +int irm_bootstrap_ipcp(char * ap_name, + int api_id, + struct dif_config * conf); +int irm_enroll_ipcp(char * ap_name, + int api_id, + char * dif_name); -int irm_reg_ipcp(rina_name_t name, - char ** difs, - size_t difs_size); -int irm_unreg_ipcp(rina_name_t name, - char ** difs, - size_t difs_size); +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 diff --git a/include/ouroboros/sockets.h b/include/ouroboros/sockets.h index 45d7a27d..69d86cd0 100644 --- a/include/ouroboros/sockets.h +++ b/include/ouroboros/sockets.h @@ -28,6 +28,9 @@ #include +#include "irmd_messages.pb-c.h" +typedef IrmMsg irm_msg_t; + #define IRM_SOCK_PATH "/tmp/irm_sock" #define IRM_MSG_BUF_SIZE 256 @@ -100,6 +103,8 @@ int client_socket_open(char * file_name); int send_irmd_msg(struct irm_msg * msg); struct irm_msg * send_recv_irmd_msg(struct irm_msg * msg); +int send_irm_msg(irm_msg_t * msg); + /* Caller has to free the buffer */ buffer_t * serialize_irm_msg(struct irm_msg * msg); buffer_t * serialize_ipcp_msg(struct ipcp_msg * msg); 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 e05dce8b..0427e236 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -34,10 +34,13 @@ set(SOURCE_FILES shm_du_map.c sockets.c utils.c -) + ) -add_library(ouroboros SHARED ${SOURCE_FILES} ${PROTO_SRCS} ${PROTO_HDRS}) -target_link_libraries(ouroboros rt pthread ${PROTOBUF_LIBRARIES}) +install(FILES ${PROTO_HDRS} + DESTINATION include/ouroboros) + +add_library(ouroboros SHARED ${SOURCE_FILES} ${PROTO_SRCS}) +target_link_libraries(ouroboros rt pthread ${PROTOBUF_C_LIBRARY}) include(MacroAddCompileFlags) if (CMAKE_BUILD_TYPE MATCHES Debug) @@ -46,4 +49,6 @@ 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/irm.c b/src/lib/irm.c index 9fd13d52..92d8b3a5 100644 --- a/src/lib/irm.c +++ b/src/lib/irm.c @@ -26,26 +26,25 @@ #include #include #include + #include -int irm_create_ipcp(rina_name_t name, +int irm_create_ipcp(char * ap_name, + int api_id, char * ipcp_type) { - struct irm_msg msg; + irm_msg_t msg = IRM_MSG__INIT; - if (ipcp_type == NULL) - return -1; + if (ipcp_type == NULL || ap_name == NULL) + return -EINVAL; - if (!name_is_ok(&name)) { - LOG_ERR("Bad name"); - return -1; - } - - msg.code = IRM_CREATE_IPCP; - msg.name = &name; + msg.code = IRM_MSG_CODE__IRM_CREATE_IPCP; + msg.ap_name = ap_name; + msg.has_api_id = true; + msg.api_id = api_id; msg.ipcp_type = ipcp_type; - if (send_irmd_msg(&msg)) { + if (send_irm_msg(&msg)) { LOG_ERR("Failed to send message to daemon"); return -1; } @@ -53,19 +52,21 @@ int irm_create_ipcp(rina_name_t name, return 0; } -int irm_destroy_ipcp(rina_name_t name) +int irm_destroy_ipcp(char * ap_name, + int api_id) { - struct irm_msg msg; + irm_msg_t msg = IRM_MSG__INIT; - if (!name_is_ok(&name)) { - LOG_ERR("Bad name"); - return -1; + if (ap_name == NULL) { + return -EINVAL; } - msg.code = IRM_DESTROY_IPCP; - msg.name = &name; + msg.code = IRM_MSG_CODE__IRM_DESTROY_IPCP; + msg.ap_name = ap_name; + msg.has_api_id = true; + msg.api_id = api_id; - if (send_irmd_msg(&msg)) { + if (send_irm_msg(&msg)) { LOG_ERR("Failed to send message to daemon"); return -1; } @@ -73,21 +74,22 @@ int irm_destroy_ipcp(rina_name_t name) return 0; } -int irm_bootstrap_ipcp(rina_name_t name, - struct dif_config conf) +int irm_bootstrap_ipcp(char * ap_name, + int api_id, + struct dif_config * conf) { - struct irm_msg msg; + irm_msg_t msg = IRM_MSG__INIT; - if (!name_is_ok(&name)) { - LOG_ERR("Bad name"); - return -1; + if (ap_name == NULL || conf == NULL) { + return -EINVAL; } - msg.code = IRM_BOOTSTRAP_IPCP; - msg.name = &name; - msg.conf = &conf; + msg.code = IRM_MSG_CODE__IRM_BOOTSTRAP_IPCP; + msg.ap_name = ap_name; + msg.has_api_id = true; + msg.api_id = api_id; - if (send_irmd_msg(&msg)) { + if (send_irm_msg(&msg)) { LOG_ERR("Failed to send message to daemon"); return -1; } @@ -95,45 +97,61 @@ int irm_bootstrap_ipcp(rina_name_t name, return 0; } -int irm_enroll_ipcp(rina_name_t name, +int irm_enroll_ipcp(char * ap_name, + int api_id, char * dif_name) { - struct irm_msg msg; + irm_msg_t msg = IRM_MSG__INIT; - if (!name_is_ok(&name)) { - LOG_ERR("Bad name"); - return -1; + if (ap_name == NULL || dif_name == NULL) { + return -EINVAL; } - msg.code = IRM_ENROLL_IPCP; - msg.name = &name; - msg.dif_name = dif_name; + msg.code = IRM_MSG_CODE__IRM_ENROLL_IPCP; + msg.ap_name = ap_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; - if (send_irmd_msg(&msg)) { + if (send_irm_msg(&msg)) { LOG_ERR("Failed to send message to daemon"); + free(msg.dif_name); return -1; } + free(msg.dif_name); + return 0; } -int irm_reg_ipcp(rina_name_t name, +int irm_reg_ipcp(char * ap_name, + int api_id, char ** difs, size_t difs_size) { - struct irm_msg msg; + irm_msg_t msg = IRM_MSG__INIT; - if (!name_is_ok(&name)) { - LOG_ERR("Bad name"); - return -1; + if (ap_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 = ap_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)) { + if (send_irm_msg(&msg)) { LOG_ERR("Failed to send message to daemon"); return -1; } @@ -141,23 +159,28 @@ int irm_reg_ipcp(rina_name_t name, return 0; } -int irm_unreg_ipcp(rina_name_t name, +int irm_unreg_ipcp(char * ap_name, + int api_id, char ** difs, size_t difs_size) { - struct irm_msg msg; + irm_msg_t msg = IRM_MSG__INIT; - if (!name_is_ok(&name)) { - LOG_ERR("Bad name"); - return -1; + if (ap_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 = ap_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)) { + if (send_irm_msg(&msg)) { LOG_ERR("Failed to send message to daemon"); return -1; } diff --git a/src/lib/sockets.c b/src/lib/sockets.c index b157b628..b98f6bbc 100644 --- a/src/lib/sockets.c +++ b/src/lib/sockets.c @@ -166,6 +166,13 @@ struct irm_msg * send_recv_irmd_msg(struct irm_msg * msg) return recv_msg; } + +int send_irm_msg(irm_msg_t * msg) +{ + + return -1; +} + char * ipcp_sock_path(pid_t pid) { char * full_name = NULL; diff --git a/src/tools/irm/irm_bootstrap_ipcp.c b/src/tools/irm/irm_bootstrap_ipcp.c index 89950069..0843083d 100644 --- a/src/tools/irm/irm_bootstrap_ipcp.c +++ b/src/tools/irm/irm_bootstrap_ipcp.c @@ -21,6 +21,7 @@ */ #include +#include #include #include @@ -38,29 +39,32 @@ static void usage() int do_bootstrap_ipcp(int argc, char ** argv) { - rina_name_t name; + char * ap_name = NULL; + int api_id = 0; struct dif_config conf; conf.qosspecs = NULL; - name.ap_name = NULL; - name.api_id = 0; - while (argc > 0) { - if (!parse_name(argv, &name)) { + if (matches(*argv, "ap") == 0) { + ap_name = *(argv + 1); + } else if (matches(*argv, "api") == 0) { + api_id = atoi(*(argv + 1)); + } else { printf("\"%s\" is unknown, try \"irm " - "enroll_ipcp\".\n", *argv); + "destroy_ipcp\".\n", *argv); return -1; } + argc -= 2; argv += 2; } - if (name.ap_name == NULL) { + if (ap_name == NULL) { usage(); return -1; } - return irm_bootstrap_ipcp(name, conf); + return irm_bootstrap_ipcp(ap_name, api_id, &conf); } diff --git a/src/tools/irm/irm_create_ipcp.c b/src/tools/irm/irm_create_ipcp.c index 854a15f9..3262bd5c 100644 --- a/src/tools/irm/irm_create_ipcp.c +++ b/src/tools/irm/irm_create_ipcp.c @@ -40,31 +40,31 @@ static void usage() int do_create_ipcp(int argc, char ** argv) { - rina_name_t name; char * ipcp_type = NULL; - - name.ap_name = NULL; - name.api_id = 0; + char * ap_name = NULL; + int api_id = 0; while (argc > 0) { - if (!parse_name(argv, &name)) { - if (matches(*argv, "type") == 0) { - ipcp_type = *(argv + 1); - } else { - printf("\"%s\" is unknown, try \"irm " - "create_ipcp\".\n", *argv); - return -1; - } + if (matches(*argv, "type") == 0) { + ipcp_type = *(argv + 1); + } else if (matches(*argv, "ap") == 0) { + ap_name = *(argv + 1); + } else if (matches(*argv, "api") == 0) { + api_id = atoi(*(argv + 1)); + } else { + printf("\"%s\" is unknown, try \"irm " + "create_ipcp\".\n", *argv); + return -1; } argc -= 2; argv += 2; } - if (ipcp_type == NULL || name.ap_name == NULL) { + if (ipcp_type == NULL || ap_name == NULL) { usage(); return -1; } - return irm_create_ipcp(name, ipcp_type); + return irm_create_ipcp(ap_name, api_id, ipcp_type); } diff --git a/src/tools/irm/irm_destroy_ipcp.c b/src/tools/irm/irm_destroy_ipcp.c index 4f02f9cb..fbbeb5bd 100644 --- a/src/tools/irm/irm_destroy_ipcp.c +++ b/src/tools/irm/irm_destroy_ipcp.c @@ -21,6 +21,7 @@ */ #include +#include #include #include @@ -36,13 +37,15 @@ static void usage() int do_destroy_ipcp(int argc, char ** argv) { - rina_name_t name; - - name.ap_name = NULL; - name.api_id = 0; + char * ap_name = NULL; + int api_id = 0; while (argc > 0) { - if (!parse_name(argv, &name)) { + if (matches(*argv, "ap") == 0) { + ap_name = *(argv + 1); + } else if (matches(*argv, "api") == 0) { + api_id = atoi(*(argv + 1)); + } else { printf("\"%s\" is unknown, try \"irm " "destroy_ipcp\".\n", *argv); return -1; @@ -52,10 +55,10 @@ int do_destroy_ipcp(int argc, char ** argv) argv += 2; } - if (name.ap_name == NULL) { + if (ap_name == NULL) { usage(); return -1; } - return irm_destroy_ipcp(name); + return irm_destroy_ipcp(ap_name, api_id); } diff --git a/src/tools/irm/irm_enroll_ipcp.c b/src/tools/irm/irm_enroll_ipcp.c index 1dcdc919..70798821 100644 --- a/src/tools/irm/irm_enroll_ipcp.c +++ b/src/tools/irm/irm_enroll_ipcp.c @@ -21,6 +21,7 @@ */ #include +#include #include #include @@ -37,31 +38,31 @@ static void usage() int do_enroll_ipcp(int argc, char ** argv) { - rina_name_t name; + char * ap_name = NULL; + int api_id = 0; char * dif_name = NULL; - name.ap_name = NULL; - name.api_id = 0; - while (argc > 0) { - if (!parse_name(argv, &name)) { - if (matches(*argv, "dif") == 0) { - dif_name = *(argv + 1); - } else { - printf("\"%s\" is unknown, try \"irm " - "enroll_ipcp\".\n", *argv); - return -1; - } + if (matches(*argv, "ap") == 0) { + ap_name = *(argv + 1); + } else if (matches(*argv, "api") == 0) { + api_id = atoi(*(argv + 1)); + } else if (matches(*argv, "dif") == 0) { + dif_name = *(argv + 1); + } else { + printf("\"%s\" is unknown, try \"irm " + "enroll_ipcp\".\n", *argv); + return -1; } argc -= 2; argv += 2; } - if (dif_name == NULL || name.ap_name == NULL) { + if (dif_name == NULL || ap_name == NULL) { usage(); return -1; } - return irm_enroll_ipcp(name, dif_name); + return irm_enroll_ipcp(ap_name, api_id, dif_name); } diff --git a/src/tools/irm/irm_register_ipcp.c b/src/tools/irm/irm_register_ipcp.c index 468ef28f..b8808ecd 100644 --- a/src/tools/irm/irm_register_ipcp.c +++ b/src/tools/irm/irm_register_ipcp.c @@ -45,36 +45,36 @@ static void usage() int do_register_ipcp(int argc, char ** argv) { - rina_name_t name; char * difs[MAX_DIFS]; size_t difs_size = 0; - - name.ap_name = NULL; - name.api_id = 0; + char * ap_name = NULL; + int api_id = 0; while (argc > 0) { - if (!parse_name(argv, &name)) { - if (matches(*argv, "dif") == 0) { - difs[difs_size++] = *(argv + 1); - if (difs_size > MAX_DIFS) { - printf("Too many difs specified\n"); - return -1; - } - } else { - printf("\"%s\" is unknown, try \"irm " - "register_ipcp\".\n", *argv); + if (matches(*argv, "ap") == 0) { + ap_name = *(argv + 1); + } else if (matches(*argv, "api") == 0) { + api_id = atoi(*(argv + 1)); + } else if (matches(*argv, "dif") == 0) { + difs[difs_size++] = *(argv + 1); + if (difs_size > MAX_DIFS) { + printf("Too many difs specified\n"); return -1; } + } else { + printf("\"%s\" is unknown, try \"irm " + "register_ipcp\".\n", *argv); + return -1; } argc -= 2; argv += 2; } - if (difs_size == 0 || name.ap_name == NULL) { + if (difs_size == 0 || ap_name == NULL) { usage(); return -1; } - return irm_reg_ipcp(name, difs, difs_size); + return irm_reg_ipcp(ap_name, api_id, difs, difs_size); } diff --git a/src/tools/irm/irm_unregister_ipcp.c b/src/tools/irm/irm_unregister_ipcp.c index 0b669503..1321c263 100644 --- a/src/tools/irm/irm_unregister_ipcp.c +++ b/src/tools/irm/irm_unregister_ipcp.c @@ -45,36 +45,37 @@ static void usage() int do_unregister_ipcp(int argc, char ** argv) { - rina_name_t name; + char * ap_name = NULL; + int api_id = 0; char * difs[MAX_DIFS]; size_t difs_size = 0; - name.ap_name = NULL; - name.api_id = 0; while (argc > 0) { - if (!parse_name(argv, &name)) { - if (matches(*argv, "dif") == 0) { - difs[difs_size++] = *(argv + 1); - if (difs_size > MAX_DIFS) { - printf("Too many difs specified\n"); - return -1; - } - } else { - printf("\"%s\" is unknown, try \"irm " - "unregister_ipcp\".\n", *argv); + if (matches(*argv, "ap") == 0) { + ap_name = *(argv + 1); + } else if (matches(*argv, "api") == 0) { + api_id = atoi(*(argv + 1)); + } else if (matches(*argv, "dif") == 0) { + difs[difs_size++] = *(argv + 1); + if (difs_size > MAX_DIFS) { + printf("Too many difs specified\n"); return -1; } + } else { + printf("\"%s\" is unknown, try \"irm " + "unregister_ipcp\".\n", *argv); + return -1; } argc -= 2; argv += 2; } - if (difs_size == 0 || name.ap_name == NULL) { + if (difs_size == 0 || ap_name == NULL) { usage(); return -1; } - return irm_unreg_ipcp(name, difs, difs_size); + return irm_unreg_ipcp(ap_name, api_id, difs, difs_size); } diff --git a/src/tools/irm/irm_utils.c b/src/tools/irm/irm_utils.c index 04cb7242..feb8ac98 100644 --- a/src/tools/irm/irm_utils.c +++ b/src/tools/irm/irm_utils.c @@ -36,19 +36,3 @@ int matches(const char * cmd, const char * pattern) return memcmp(pattern, cmd, len); } - - -bool parse_name(char ** argv, - rina_name_t * name) -{ - bool found = true; - - if (matches(*argv, "ap") == 0) - name->ap_name = *(argv + 1); - else if (matches(*argv, "api") == 0) - name->api_id = atoi(*(argv + 1)); - else - found = false; - - return found; -} diff --git a/src/tools/irm/irm_utils.h b/src/tools/irm/irm_utils.h index 2a478d09..3d328d95 100644 --- a/src/tools/irm/irm_utils.h +++ b/src/tools/irm/irm_utils.h @@ -25,5 +25,3 @@ #include int matches(const char * cmd, const char * pattern); - -bool parse_name(char ** argv, rina_name_t * name); -- 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/lib/CMakeLists.txt') 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/lib/CMakeLists.txt') 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 8cef483119da184d631634bc98b5236ac54c30ae Mon Sep 17 00:00:00 2001 From: dimitri staessens Date: Wed, 30 Mar 2016 00:29:38 +0200 Subject: lib: api for handling flows moved the flow definition to the library and made it public. thread-safety implemented without compiler checks. --- include/ouroboros/CMakeLists.txt | 1 + include/ouroboros/flow.h | 61 +++++++++++++++++++++++++++++ src/ipcpd/CMakeLists.txt | 1 - src/ipcpd/flow.c | 83 ---------------------------------------- src/ipcpd/flow.h | 66 -------------------------------- src/lib/CMakeLists.txt | 1 + src/lib/flow.c | 77 +++++++++++++++++++++++++++++++++++++ 7 files changed, 140 insertions(+), 150 deletions(-) create mode 100644 include/ouroboros/flow.h delete mode 100644 src/ipcpd/flow.c delete mode 100644 src/ipcpd/flow.h create mode 100644 src/lib/flow.c (limited to 'src/lib/CMakeLists.txt') diff --git a/include/ouroboros/CMakeLists.txt b/include/ouroboros/CMakeLists.txt index a196140b..9d1a623d 100644 --- a/include/ouroboros/CMakeLists.txt +++ b/include/ouroboros/CMakeLists.txt @@ -9,6 +9,7 @@ set(HEADER_FILES da.h dev.h du_buff.h + flow.h ipcp.h irm.h list.h diff --git a/include/ouroboros/flow.h b/include/ouroboros/flow.h new file mode 100644 index 00000000..456a85c7 --- /dev/null +++ b/include/ouroboros/flow.h @@ -0,0 +1,61 @@ +/* + * Ouroboros - Copyright (C) 2016 + * + * Flows + * + * Dimitri Staessens + * + * 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 OUROBOROS_FLOW_H +#define OUROBOROS_FLOW_H + +#include + +#include + +/* same values as fcntl.h */ +#define FLOW_O_RDONLY 00000000 +#define FLOW_O_WRONLY 00000001 +#define FLOW_O_RDWR 00000002 +#define FLOW_O_ACCMODE 00000003 + +#define FLOW_O_NONBLOCK 00004000 +#define FLOW_O_DEFAULT 00000002 + +#define FLOW_O_INVALID 00037777 + +enum flow_state { + FLOW_INIT = 0, + FLOW_ALLOCATED, + FLOW_PENDING +}; + +typedef struct flow { + int32_t port_id; + uint16_t oflags; + enum flow_state state; + + pthread_mutex_t lock; +} flow_t; + +flow_t * flow_create(int32_t port_id); +void flow_destroy(flow_t * flow); + +int flow_set_opts(flow_t * flow, uint16_t opts); +uint16_t flow_get_opts(const flow_t * flow); + +#endif /* OUROBOROS_FLOW_H */ diff --git a/src/ipcpd/CMakeLists.txt b/src/ipcpd/CMakeLists.txt index 27d41801..bcb5b986 100644 --- a/src/ipcpd/CMakeLists.txt +++ b/src/ipcpd/CMakeLists.txt @@ -9,7 +9,6 @@ set(SOURCE_FILES main.c pci.c shm_pci.c - flow.c ) add_executable (ipcpd ${SOURCE_FILES}) diff --git a/src/ipcpd/flow.c b/src/ipcpd/flow.c deleted file mode 100644 index 395a0a0d..00000000 --- a/src/ipcpd/flow.c +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Ouroboros - Copyright (C) 2016 - * - * Flows - * - * Dimitri Staessens - * - * 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 "flow.h" -#include - -#define OUROBOROS_PREFIX "ipcpd/flow" - -#include - -flow_t * flow_create(port_id_t port_id) -{ - flow_t * flow = malloc(sizeof *flow); - flow->port_id = port_id; - flow->flags = FLOW_O_DEFAULT; - flow->state = FLOW_INIT; - -#ifdef FLOW_MT_SAFE - pthread_mutex_init(&flow->lock, NULL); -#endif - return flow; -} - -void flow_destroy(flow_t * flow) -{ - free(flow); -} - -int flow_set_opts(flow_t * flow, uint16_t opts) -{ - if (flow == NULL) { - LOG_ERR("Non-existing flow."); - return -1; - } - -#ifdef FLOW_MT_SAFE - pthread_mutex_lock(&flow->lock); -#endif - - if ((opts & FLOW_O_ACCMODE) == FLOW_O_ACCMODE) { -#ifdef FLOW_MT_SAFE - pthread_mutex_unlock(&flow->lock); -#endif - LOG_WARN("Invalid flow options. Setting default."); - opts = FLOW_O_DEFAULT; - } - - flow->flags = opts; - -#ifdef FLOW_MT_SAFE - pthread_mutex_unlock(&flow->lock); -#endif - return 0; -} - -uint16_t flow_get_opts(const flow_t * flow) -{ - if (flow == NULL) { - LOG_ERR("Non-existing flow."); - return FLOW_O_INVALID; - } - - return flow->flags; -} diff --git a/src/ipcpd/flow.h b/src/ipcpd/flow.h deleted file mode 100644 index 83f4076d..00000000 --- a/src/ipcpd/flow.h +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Ouroboros - Copyright (C) 2016 - * - * Flows - * - * Dimitri Staessens - * - * 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 IPCPD_FLOW_H -#define IPCPD_FLOW_H - -#include - -#ifdef FLOW_MT_SAFE -#include -#endif - -/* same values as fcntl.h */ -#define FLOW_O_RDONLY 00000000 -#define FLOW_O_WRONLY 00000001 -#define FLOW_O_RDWR 00000002 -#define FLOW_O_ACCMODE 00000003 - -#define FLOW_O_NONBLOCK 00004000 -#define FLOW_O_DEFAULT 00000002 - -#define FLOW_O_INVALID 00037777 - -typedef long port_id_t; - -enum flow_state { - FLOW_INIT = 0, - FLOW_ALLOCATED, - FLOW_PENDING -}; - -typedef struct flow { - port_id_t port_id; - uint16_t flags; - uint8_t state; -#ifdef FLOW_MT_SAFE - pthread_mutex_t lock; -#endif -} flow_t; - -flow_t * flow_create(port_id_t port_id); -void flow_destroy(flow_t * flow); - -int flow_set_opts(flow_t * flow, uint16_t opts); -uint16_t flow_get_opts(const flow_t * flow); - -#endif /* IPCPD_FLOW_H */ diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index 42a4d5c0..7a78bf3e 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -21,6 +21,7 @@ set(SOURCE_FILES da.c dev.c du_buff.c + flow.c ipcp.c irm.c list.c diff --git a/src/lib/flow.c b/src/lib/flow.c new file mode 100644 index 00000000..67b8e71b --- /dev/null +++ b/src/lib/flow.c @@ -0,0 +1,77 @@ +/* + * Ouroboros - Copyright (C) 2016 + * + * Flows + * + * Dimitri Staessens + * + * 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 +#include + +#define OUROBOROS_PREFIX "ipcpd/flow" + +#include + +flow_t * flow_create(int32_t port_id) +{ + flow_t * flow = malloc(sizeof *flow); + 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_ERR("Non-existing flow."); + return -1; + } + + pthread_mutex_lock(&flow->lock); + + if ((opts & FLOW_O_ACCMODE) == FLOW_O_ACCMODE) { + pthread_mutex_unlock(&flow->lock); + LOG_WARN("Invalid flow options. Setting default."); + opts = FLOW_O_DEFAULT; + } + + flow->oflags = opts; + + pthread_mutex_unlock(&flow->lock); + + return 0; +} + +uint16_t flow_get_opts(const flow_t * flow) +{ + if (flow == NULL) { + LOG_ERR("Non-existing flow."); + return FLOW_O_INVALID; + } + + return flow->oflags; +} -- cgit v1.2.3