diff options
author | Sander Vrijders <sander.vrijders@intec.ugent.be> | 2016-03-11 16:52:28 +0100 |
---|---|---|
committer | Sander Vrijders <sander.vrijders@intec.ugent.be> | 2016-03-11 16:52:28 +0100 |
commit | f5cf4e478bf7dec70dc22d80de706f82ef4b38f1 (patch) | |
tree | 60160c5cac28eb8cfcd376428429e03cf2e7e203 /src/irmd/main.c | |
parent | d68e4e5e540720d9b02e2062e3982f1c438eb1e0 (diff) | |
download | ouroboros-f5cf4e478bf7dec70dc22d80de706f82ef4b38f1.tar.gz ouroboros-f5cf4e478bf7dec70dc22d80de706f82ef4b38f1.zip |
lib: Add helpers for RINA names
This adds helper functions for RINA names, to aid with handling them.
Diffstat (limited to 'src/irmd/main.c')
-rw-r--r-- | src/irmd/main.c | 164 |
1 files changed, 137 insertions, 27 deletions
diff --git a/src/irmd/main.c b/src/irmd/main.c index f744ee7f..e28e6a28 100644 --- a/src/irmd/main.c +++ b/src/irmd/main.c @@ -28,48 +28,126 @@ #include <ouroboros/irm.h> #include <ouroboros/ipcp.h> #include <ouroboros/da.h> +#include <ouroboros/list.h> +#include <ouroboros/rina_name.h> #include <sys/socket.h> #include <sys/un.h> #include <stdlib.h> #include <errno.h> -struct irm { +struct name_to_pid_entry { + struct list_head next; + int pid; + rina_name_t * name; +}; +struct irm { + struct list_head name_to_pid; }; -static void create_ipcp(rina_name_t name, +static int find_pid_by_name(struct irm * instance, + rina_name_t * name) +{ + struct list_head * pos; + + list_for_each(pos, &instance->name_to_pid) { + struct name_to_pid_entry * tmp = + list_entry(pos, struct name_to_pid_entry, next); + + LOG_DBG("name is %s", name->ap_name); + + if (name_is_equal(name, tmp->name)) + return tmp->pid; + } + + return 0; +} + +static void create_ipcp(struct irm * instance, + rina_name_t name, char * ipcp_type) { - struct ipcp * instance = NULL; + int pid; + struct name_to_pid_entry * tmp; + rina_name_t * ipcp_name = NULL; - instance = ipcp_create(name, ipcp_type); - if (instance == NULL) + pid = ipcp_create(name, ipcp_type); + if (pid == 0) { LOG_ERR("Failed to create IPCP"); + return; + } + + tmp = malloc(sizeof(*tmp)); + if (tmp == NULL) + return; + + INIT_LIST_HEAD(&tmp->next); + + tmp->pid = pid; + tmp->name = name_dup(ipcp_name); + if (tmp->name == NULL) { + free(tmp); + return; + } + + list_add(&tmp->next, &instance->name_to_pid); } -static void destroy_ipcp(rina_name_t name) +static void destroy_ipcp(struct irm * instance, + rina_name_t name) { - struct ipcp * instance = NULL; + int pid = 0; + struct list_head * pos; - if (ipcp_destroy(instance)) + pid = find_pid_by_name(instance, &name); + if (pid == 0) { + LOG_ERR("No such IPCP"); + return; + } + + if (ipcp_destroy(pid)) LOG_ERR("Could not destroy IPCP"); + + list_for_each(pos, &instance->name_to_pid) { + struct name_to_pid_entry * tmp = + list_entry(pos, struct name_to_pid_entry, next); + + if (name_is_equal(&name, tmp->name)) + list_del(&tmp->next); + } } -static void bootstrap_ipcp(rina_name_t name, +static void bootstrap_ipcp(struct irm * instance, + rina_name_t name, struct dif_config conf) { - struct ipcp * instance = NULL; + int pid = 0; - if (ipcp_bootstrap(instance, conf)) + pid = find_pid_by_name(instance, &name); + if (pid == 0) { + LOG_ERR("No such IPCP"); + return; + } + + if (ipcp_bootstrap(pid, conf)) LOG_ERR("Could not bootstrap IPCP"); } -static void enroll_ipcp(rina_name_t name, +static void enroll_ipcp(struct irm * instance, + rina_name_t name, char * dif_name) { - struct ipcp * instance = NULL; + int pid = 0; rina_name_t * member; + char ** n_1_difs = NULL; + ssize_t n_1_difs_size = 0; + + pid = find_pid_by_name(instance, &name); + if (pid == 0) { + LOG_ERR("No such IPCP"); + return; + } member = da_resolve_daf(dif_name); if (member == NULL) { @@ -77,35 +155,60 @@ static void enroll_ipcp(rina_name_t name, return; } - if (ipcp_enroll(instance, dif_name, *member)) - LOG_ERR("Could not enroll IPCP"); + n_1_difs_size = da_resolve_dap(member, n_1_difs); + if (n_1_difs_size != 0) + if (ipcp_enroll(pid, dif_name, *member, + n_1_difs, n_1_difs_size)) + LOG_ERR("Could not enroll IPCP"); } -static void reg_ipcp(rina_name_t name, +static void reg_ipcp(struct irm * instance, + rina_name_t name, char ** difs, size_t difs_size) { - struct ipcp * instance = NULL; + int pid = 0; + + pid = find_pid_by_name(instance, &name); + if (pid == 0) { + LOG_ERR("No such IPCP"); + return; + } - if (ipcp_reg(instance, difs, difs_size)) + if (ipcp_reg(pid, difs, difs_size)) LOG_ERR("Could not register IPCP to N-1 DIF(s)"); } -static void unreg_ipcp(rina_name_t name, +static void unreg_ipcp(struct irm * instance, + rina_name_t name, char ** difs, size_t difs_size) { - struct ipcp * instance = NULL; + int pid = 0; - if (ipcp_unreg(instance, difs, difs_size)) + pid = find_pid_by_name(instance, &name); + if (pid == 0) { + LOG_ERR("No such IPCP"); + return; + } + + if (ipcp_unreg(pid, difs, difs_size)) LOG_ERR("Could not unregister IPCP from N-1 DIF(s)"); } +/* FIXME: Close sockfd on closing and release irm */ int main() { + struct irm * instance; int sockfd; uint8_t buf[IRM_MSG_BUF_SIZE]; + instance = malloc(sizeof(*instance)); + if (instance == NULL) + return -1; + + INIT_LIST_HEAD(&instance->name_to_pid); + sockfd = server_socket_open(IRM_SOCK_PATH); if (sockfd < 0) return -1; @@ -132,26 +235,33 @@ int main() switch (msg->code) { case IRM_CREATE_IPCP: - create_ipcp(*(msg->name), msg->ipcp_type); + create_ipcp(instance, + *(msg->name), + msg->ipcp_type); break; case IRM_DESTROY_IPCP: - destroy_ipcp(*(msg->name)); + destroy_ipcp(instance, + *(msg->name)); break; case IRM_BOOTSTRAP_IPCP: - bootstrap_ipcp(*(msg->name), + bootstrap_ipcp(instance, + *(msg->name), *(msg->conf)); break; case IRM_ENROLL_IPCP: - enroll_ipcp(*(msg->name), + enroll_ipcp(instance, + *(msg->name), msg->dif_name); break; case IRM_REG_IPCP: - reg_ipcp(*(msg->name), + reg_ipcp(instance, + *(msg->name), msg->difs, msg->difs_size); break; case IRM_UNREG_IPCP: - unreg_ipcp(*(msg->name), + unreg_ipcp(instance, + *(msg->name), msg->difs, msg->difs_size); break; |