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 * --- 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 - 16 files changed, 406 insertions(+), 410 deletions(-) create mode 100644 src/lib/instance_name.c delete mode 100644 src/lib/rina_name.c (limited to 'src') 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 9aa7cd1d8d137bdb11f963af3e29ba4f421ab6b3 Mon Sep 17 00:00:00 2001 From: dimitri staessens Date: Wed, 30 Mar 2016 13:17:34 +0200 Subject: lib: fixes for instance_name fixes wrong check, checks now use lazy evaluation changed the order of instance_name_cpy to (dst, src) --- include/ouroboros/instance_name.h | 4 ++-- src/lib/instance_name.c | 26 ++++++++++---------------- src/lib/irm.c | 27 ++++++--------------------- 3 files changed, 18 insertions(+), 39 deletions(-) (limited to 'src') diff --git a/include/ouroboros/instance_name.h b/include/ouroboros/instance_name.h index b3e528c0..351b222f 100644 --- a/include/ouroboros/instance_name.h +++ b/include/ouroboros/instance_name.h @@ -72,8 +72,8 @@ 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_cpy(instance_name_t * dst, + const instance_name_t * src); int instance_name_cmp(const instance_name_t * a, const instance_name_t * b); diff --git a/src/lib/instance_name.c b/src/lib/instance_name.c index 1e61f790..0f666211 100644 --- a/src/lib/instance_name.c +++ b/src/lib/instance_name.c @@ -86,7 +86,7 @@ instance_name_t * instance_name_init_from(instance_name_t * dst, } instance_name_t * instance_name_init_with(instance_name_t * dst, - char * name, + char * name, uint16_t id) { if (dst == NULL) @@ -103,13 +103,11 @@ instance_name_t * instance_name_init_with(instance_name_t * dst, void instance_name_fini(instance_name_t * n) { - if (n == NULL) + if (n == NULL || n->name == NULL) return; - if (n->name != NULL) { - free(n->name); - n->name = NULL; - } + free(n->name); + n->name = NULL; } void instance_name_destroy(instance_name_t * ptr) @@ -122,17 +120,15 @@ void instance_name_destroy(instance_name_t * ptr) free(ptr); } -int instance_name_cpy(const instance_name_t * src, - instance_name_t * dst) +int instance_name_cpy(instance_name_t * dst, + const instance_name_t * src) { instance_name_t * res; if (src == NULL || dst == NULL) return -1; - res = instance_name_init_from(dst, - src->name, - src->id); + res = instance_name_init_from(dst, src->name, src->id); if (res == NULL) return -1; @@ -150,7 +146,7 @@ instance_name_t * instance_name_dup(const instance_name_t * src) if (tmp == NULL) return NULL; - if (instance_name_cpy(src, tmp)) { + if (instance_name_cpy(tmp, src)) { instance_name_destroy(tmp); return NULL; } @@ -160,9 +156,7 @@ instance_name_t * instance_name_dup(const instance_name_t * src) bool instance_name_is_valid(const instance_name_t * n) { - return (n != NULL && - n->name != NULL && - strlen(n->name)); + return (n != NULL && n->name != NULL && strlen(n->name)); } int instance_name_cmp(const instance_name_t * a, @@ -205,7 +199,7 @@ char * instance_name_to_string(const instance_name_t * n) if (n == NULL) return NULL; - size = 0; + size = 0; size += (n->name != NULL ? strlen(n->name) : none_len); diff --git a/src/lib/irm.c b/src/lib/irm.c index af899d0a..644e1113 100644 --- a/src/lib/irm.c +++ b/src/lib/irm.c @@ -35,10 +35,7 @@ int irm_create_ipcp(instance_name_t * api, { irm_msg_t msg = IRM_MSG__INIT; - if (api == NULL) - return -EINVAL; - - if (ipcp_type == NULL || api == NULL) + if (api == NULL || ipcp_type == NULL || api->name == NULL) return -EINVAL; msg.code = IRM_MSG_CODE__IRM_CREATE_IPCP; @@ -59,12 +56,8 @@ int irm_destroy_ipcp(instance_name_t * api) { irm_msg_t msg = IRM_MSG__INIT; - if (api == NULL) - return -EINVAL; - - if (api->name == NULL) { + if (api == NULL || api->name == NULL) return -EINVAL; - } msg.code = IRM_MSG_CODE__IRM_DESTROY_IPCP; msg.ap_name = api->name; @@ -84,10 +77,7 @@ int irm_bootstrap_ipcp(instance_name_t * api, { irm_msg_t msg = IRM_MSG__INIT; - if (api == NULL) - return -EINVAL; - - if (api->name == NULL || conf == NULL) + if (api == NULL || api->name == NULL || conf == NULL) return -EINVAL; msg.code = IRM_MSG_CODE__IRM_BOOTSTRAP_IPCP; @@ -108,10 +98,7 @@ int irm_enroll_ipcp(instance_name_t * api, { irm_msg_t msg = IRM_MSG__INIT; - if (api == NULL) - return -EINVAL; - - if (api->name == NULL || dif_name == NULL) + if (api == NULL || api->name == NULL || dif_name == NULL) return -EINVAL; msg.code = IRM_MSG_CODE__IRM_ENROLL_IPCP; @@ -171,10 +158,8 @@ int irm_unreg_ipcp(const instance_name_t * api, { irm_msg_t msg = IRM_MSG__INIT; - if (api == NULL) - return -EINVAL; - - if (api->name == NULL || + if (api == NULL || + api->name == NULL || difs == NULL || difs_size == 0 || difs[0] == NULL) { -- cgit v1.2.3