From 8e1e865a25ec4e88f64b149764c239e286e42c49 Mon Sep 17 00:00:00 2001 From: Sander Vrijders Date: Sat, 2 Apr 2016 14:06:30 +0200 Subject: lib: Move strdup to utils This moves strdup to utils to make it globally available. --- include/ouroboros/common.h | 2 +- include/ouroboros/utils.h | 3 +++ src/lib/instance_name.c | 19 ------------------- src/lib/utils.c | 22 ++++++++++++++++++++++ 4 files changed, 26 insertions(+), 20 deletions(-) diff --git a/include/ouroboros/common.h b/include/ouroboros/common.h index 7114ed73..658c3618 100644 --- a/include/ouroboros/common.h +++ b/include/ouroboros/common.h @@ -45,7 +45,7 @@ struct qos_spec { /* FIXME: What should be configurable in the DIF? */ struct dif_config { /* general data */ - struct qos_spec * qosspecs; + char * dif_name; /* TODO: efficient policies */ diff --git a/include/ouroboros/utils.h b/include/ouroboros/utils.h index 0e50c039..2eaccae9 100644 --- a/include/ouroboros/utils.h +++ b/include/ouroboros/utils.h @@ -25,3 +25,6 @@ * need when represented as a string */ int n_digits(unsigned i); + +/* Returns a copy of the source string */ +char * strdup(const char * src); diff --git a/src/lib/instance_name.c b/src/lib/instance_name.c index 0f666211..2c70b419 100644 --- a/src/lib/instance_name.c +++ b/src/lib/instance_name.c @@ -33,25 +33,6 @@ #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; diff --git a/src/lib/utils.c b/src/lib/utils.c index ca082642..77a2d44c 100644 --- a/src/lib/utils.c +++ b/src/lib/utils.c @@ -20,6 +20,9 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ +#include +#include + int n_digits(unsigned i) { int n = 1; @@ -31,3 +34,22 @@ int n_digits(unsigned i) return n; } + +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; +} -- cgit v1.2.3 From 4459045d1beb1478943d96c314e72c5ede18154a Mon Sep 17 00:00:00 2001 From: Sander Vrijders Date: Sat, 2 Apr 2016 14:06:40 +0200 Subject: irmd: Rename name_to_pid to ipcp_data Did a rename of the struct so that it can hold all data related to an IPCP in the system. The DIF name an IPCP belongs to is set on bootstrap and enrolment. --- src/irmd/main.c | 106 +++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 75 insertions(+), 31 deletions(-) diff --git a/src/irmd/main.c b/src/irmd/main.c index 0256248b..a900b0ed 100644 --- a/src/irmd/main.c +++ b/src/irmd/main.c @@ -30,31 +30,33 @@ #include #include #include +#include #include #include #include #include -struct name_to_pid_entry { +struct ipcp_entry { struct list_head next; pid_t pid; instance_name_t * api; + char * dif_name; }; struct irm { - struct list_head name_to_pid; + struct list_head ipcps; }; struct irm * instance = NULL; -static pid_t find_pid_by_name(instance_name_t * api) +static pid_t find_pid_by_ipcp_name(instance_name_t * api) { - struct list_head * pos; + struct list_head * pos = NULL; - list_for_each(pos, &instance->name_to_pid) { - struct name_to_pid_entry * tmp = - list_entry(pos, struct name_to_pid_entry, next); + list_for_each(pos, &instance->ipcps) { + struct ipcp_entry * tmp = + list_entry(pos, struct ipcp_entry, next); LOG_DBG("name is %s", api->name); @@ -65,11 +67,27 @@ static pid_t find_pid_by_name(instance_name_t * api) return 0; } +static struct ipcp_entry * find_ipcp_by_name(instance_name_t * api) +{ + struct ipcp_entry * tmp = NULL; + struct list_head * pos = NULL; + + list_for_each(pos, &instance->ipcps) { + struct ipcp_entry * tmp = + list_entry(pos, struct ipcp_entry, next); + + if (instance_name_cmp(api, tmp->api) == 0) + return tmp; + } + + return tmp; +} + static int create_ipcp(instance_name_t * api, char * ipcp_type) { pid_t pid; - struct name_to_pid_entry * tmp; + struct ipcp_entry * tmp = NULL; pid = ipcp_create(api, ipcp_type); if (pid == -1) { @@ -90,19 +108,21 @@ static int create_ipcp(instance_name_t * api, return -1; } + tmp->dif_name = NULL; + LOG_DBG("Created IPC process with pid %d", pid); - list_add(&tmp->next, &instance->name_to_pid); + list_add(&tmp->next, &instance->ipcps); return 0; } static int destroy_ipcp(instance_name_t * api) { pid_t pid = 0; - struct list_head * pos; - struct list_head * n; + struct list_head * pos = NULL; + struct list_head * n = NULL; - pid = find_pid_by_name(api); + pid = find_pid_by_ipcp_name(api); if (pid == 0) { LOG_ERR("No such IPCP"); return -1; @@ -113,9 +133,9 @@ static int destroy_ipcp(instance_name_t * api) if (ipcp_destroy(pid)) LOG_ERR("Could not destroy IPCP"); - list_for_each_safe(pos, n, &(instance->name_to_pid)) { - struct name_to_pid_entry * tmp = - list_entry(pos, struct name_to_pid_entry, next); + list_for_each_safe(pos, n, &(instance->ipcps)) { + struct ipcp_entry * tmp = + list_entry(pos, struct ipcp_entry, next); if (instance_name_cmp(api, tmp->api) == 0) list_del(&tmp->next); @@ -127,16 +147,24 @@ static int destroy_ipcp(instance_name_t * api) static int bootstrap_ipcp(instance_name_t * api, struct dif_config * conf) { - pid_t pid = 0; + struct ipcp_entry * entry = NULL; - pid = find_pid_by_name(api); - if (pid == 0) { + entry = find_ipcp_by_name(api); + if (entry == NULL) { LOG_ERR("No such IPCP"); return -1; } - if (ipcp_bootstrap(pid, conf)) { + entry->dif_name = strdup( conf->dif_name); + if (entry->dif_name == NULL) { + LOG_ERR("Failed to strdup"); + return -1; + } + + if (ipcp_bootstrap(entry->pid, conf)) { LOG_ERR("Could not bootstrap IPCP"); + free(entry->dif_name); + entry->dif_name = NULL; return -1; } @@ -146,29 +174,45 @@ static int bootstrap_ipcp(instance_name_t * api, static int enroll_ipcp(instance_name_t * api, char * dif_name) { - pid_t pid = 0; - char * member; + char * member = NULL; char ** n_1_difs = NULL; ssize_t n_1_difs_size = 0; + struct ipcp_entry * entry = NULL; - pid = find_pid_by_name(api); - if (pid == 0) { + entry = find_ipcp_by_name(api); + if (entry == NULL) { LOG_ERR("No such IPCP"); return -1; } + entry->dif_name = strdup(dif_name); + if (entry->dif_name == NULL) { + LOG_ERR("Failed to strdup"); + return -1; + } + member = da_resolve_daf(dif_name); if (member == NULL) { LOG_ERR("Could not find a member of that DIF"); + free(entry->dif_name); + entry->dif_name = NULL; return -1; } n_1_difs_size = da_resolve_dap(member, n_1_difs); - if (n_1_difs_size != 0) - if (ipcp_enroll(pid, member, n_1_difs[0])) { - LOG_ERR("Could not enroll IPCP"); - return -1; - } + if (n_1_difs_size < 1) { + LOG_ERR("Could not find N-1 DIFs"); + free(entry->dif_name); + entry->dif_name = NULL; + return -1; + } + + if (ipcp_enroll(entry->pid, member, n_1_difs[0])) { + LOG_ERR("Could not enroll IPCP"); + free(entry->dif_name); + entry->dif_name = NULL; + return -1; + } return 0; } @@ -179,7 +223,7 @@ static int reg_ipcp(instance_name_t * api, { pid_t pid = 0; - pid = find_pid_by_name(api); + pid = find_pid_by_ipcp_name(api); if (pid == 0) { LOG_ERR("No such IPCP"); return -1; @@ -199,7 +243,7 @@ static int unreg_ipcp(instance_name_t * api, { pid_t pid = 0; - pid = find_pid_by_name(api); + pid = find_pid_by_ipcp_name(api); if (pid == 0) { LOG_ERR("No such IPCP"); return -1; @@ -295,7 +339,7 @@ int main() if (instance == NULL) return -1; - INIT_LIST_HEAD(&instance->name_to_pid); + INIT_LIST_HEAD(&instance->ipcps); sockfd = server_socket_open(IRM_SOCK_PATH); if (sockfd < 0) -- cgit v1.2.3 From 9fed4c5bdb4928c7f5c73e85888de54672cd4c56 Mon Sep 17 00:00:00 2001 From: Sander Vrijders Date: Sat, 2 Apr 2016 17:17:39 +0200 Subject: lib: Remove check for equal names Removes name_is_equal. Just using instance_name_cmp is encouraged. It also removes a wrong initialization in the irm tool. --- src/lib/instance_name.c | 2 -- src/tools/irm/irm_bootstrap_ipcp.c | 2 -- 2 files changed, 4 deletions(-) diff --git a/src/lib/instance_name.c b/src/lib/instance_name.c index 2c70b419..e560c0b9 100644 --- a/src/lib/instance_name.c +++ b/src/lib/instance_name.c @@ -31,8 +31,6 @@ #include #include -#define instance_name_is_equal(a, b) (instance_name_cmp(a, b) == 0) - instance_name_t * instance_name_create() { instance_name_t * tmp; diff --git a/src/tools/irm/irm_bootstrap_ipcp.c b/src/tools/irm/irm_bootstrap_ipcp.c index c7b82c4a..03a913fb 100644 --- a/src/tools/irm/irm_bootstrap_ipcp.c +++ b/src/tools/irm/irm_bootstrap_ipcp.c @@ -42,8 +42,6 @@ int do_bootstrap_ipcp(int argc, char ** argv) instance_name_t api = {NULL, 0}; struct dif_config conf; - conf.qosspecs = NULL; - while (argc > 0) { if (matches(*argv, "ap") == 0) { api.name = *(argv + 1); -- cgit v1.2.3