From 0d789ed8d938cc342c8f2138280795a1d5a61e3d Mon Sep 17 00:00:00 2001 From: Sander Vrijders Date: Tue, 28 Jun 2016 16:11:19 +0200 Subject: lib, irmd, ipcpd: Change of IRM API This changes the IRM API after discussions with Dimitri. The register operation is now split into a bind and register operation. The same for unregister; unbind and unregister. PIDs are now used as the application instance name. A name for a PID is only provided for scriptability in bash. It is therefore also no longer passed down to the IPCP. Every operation on an IPCP through the IRM API has to use the PID. Quering of the PIDs by name is possible. The IRM tool has been updated to use this new API as well. A subcommand 'ipcp' has been added for operations that take effect on IPCPs only. Fixes #12 --- include/ouroboros/CMakeLists.txt | 5 +- include/ouroboros/instance_name.h | 91 --------- include/ouroboros/ipcp.h | 6 +- include/ouroboros/irm.h | 52 +++--- include/ouroboros/irm_config.h | 73 ++++++++ src/ipcpd/ipcp-data.h | 1 - src/ipcpd/ipcp-ops.h | 2 +- src/ipcpd/ipcp.c | 6 +- src/ipcpd/local/main.c | 29 +-- src/ipcpd/shim-eth-llc/main.c | 6 +- src/ipcpd/shim-udp/main.c | 29 +-- src/irmd/main.c | 372 +++++++++++++++++++++---------------- src/lib/CMakeLists.txt | 1 - src/lib/dev.c | 32 +--- src/lib/instance_name.c | 248 ------------------------- src/lib/ipcp.c | 4 +- src/lib/irm.c | 201 ++++++++++++++------ src/lib/irmd_messages.proto | 37 ++-- src/tools/irm/CMakeLists.txt | 11 +- src/tools/irm/irm.c | 16 +- src/tools/irm/irm_bind.c | 81 ++++++++ src/tools/irm/irm_bootstrap_ipcp.c | 192 ------------------- src/tools/irm/irm_create_ipcp.c | 89 --------- src/tools/irm/irm_destroy_ipcp.c | 63 ------- src/tools/irm/irm_enroll_ipcp.c | 67 ------- src/tools/irm/irm_ipcp.c | 77 ++++++++ src/tools/irm/irm_ipcp_bootstrap.c | 200 ++++++++++++++++++++ src/tools/irm/irm_ipcp_create.c | 85 +++++++++ src/tools/irm/irm_ipcp_destroy.c | 70 +++++++ src/tools/irm/irm_ipcp_enroll.c | 74 ++++++++ src/tools/irm/irm_ops.h | 4 + src/tools/irm/irm_register.c | 66 +------ src/tools/irm/irm_unbind.c | 63 +++++++ src/tools/irm/irm_unregister.c | 53 +----- 34 files changed, 1179 insertions(+), 1227 deletions(-) delete mode 100644 include/ouroboros/instance_name.h create mode 100644 include/ouroboros/irm_config.h delete mode 100644 src/lib/instance_name.c create mode 100644 src/tools/irm/irm_bind.c delete mode 100644 src/tools/irm/irm_bootstrap_ipcp.c delete mode 100644 src/tools/irm/irm_create_ipcp.c delete mode 100644 src/tools/irm/irm_destroy_ipcp.c delete mode 100644 src/tools/irm/irm_enroll_ipcp.c create mode 100644 src/tools/irm/irm_ipcp.c create mode 100644 src/tools/irm/irm_ipcp_bootstrap.c create mode 100644 src/tools/irm/irm_ipcp_create.c create mode 100644 src/tools/irm/irm_ipcp_destroy.c create mode 100644 src/tools/irm/irm_ipcp_enroll.c create mode 100644 src/tools/irm/irm_unbind.c diff --git a/include/ouroboros/CMakeLists.txt b/include/ouroboros/CMakeLists.txt index 2ea86883..68c88a18 100644 --- a/include/ouroboros/CMakeLists.txt +++ b/include/ouroboros/CMakeLists.txt @@ -5,13 +5,12 @@ configure_file( set(HEADER_FILES cdap.h dev.h - dif_config.h errno.h flow.h - instance_name.h irm.h + irm_config.h nsm.h qos.h ) -install(FILES ${HEADER_FILES} DESTINATION /usr/include/ouroboros) +install(FILES ${HEADER_FILES} DESTINATION usr/include/ouroboros) diff --git a/include/ouroboros/instance_name.h b/include/ouroboros/instance_name.h deleted file mode 100644 index 92681504..00000000 --- a/include/ouroboros/instance_name.h +++ /dev/null @@ -1,91 +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 INSTANCE_NAME_H -#define INSTANCE_NAME_H - -#include -#include -#include - -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(instance_name_t * dst, - const instance_name_t * src); - -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 0d62bd59..0c0afbe7 100644 --- a/include/ouroboros/ipcp.h +++ b/include/ouroboros/ipcp.h @@ -20,8 +20,7 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -#include -#include +#include #include #include @@ -33,8 +32,7 @@ struct ipcp; /* Returns the process id */ -pid_t ipcp_create(char * ipcp_name, - enum ipcp_type ipcp_type); +pid_t ipcp_create(enum ipcp_type ipcp_type); int ipcp_destroy(pid_t pid); diff --git a/include/ouroboros/irm.h b/include/ouroboros/irm.h index 743ed79c..9f27db05 100644 --- a/include/ouroboros/irm.h +++ b/include/ouroboros/irm.h @@ -1,7 +1,7 @@ /* * Ouroboros - Copyright (C) 2016 * - * The API to instruct the IRM + * The API to instruct the IPC Resource Manager * * Sander Vrijders * @@ -23,34 +23,40 @@ #ifndef OUROBOROS_IRM_H #define OUROBOROS_IRM_H -#include -#include - +#include #include -pid_t irm_create_ipcp(char * ipcp_name, - enum ipcp_type ipcp_type); +pid_t irm_create_ipcp(char * name, + enum ipcp_type ipcp_type); + +int irm_destroy_ipcp(pid_t api); + +/* APIs is an out-parameter */ +ssize_t irm_list_ipcps(char * name, + pid_t ** apis); + +int irm_enroll_ipcp(pid_t api, + char * dif_name); -int irm_destroy_ipcp(instance_name_t * api); +int irm_bootstrap_ipcp(pid_t api, + struct dif_config * conf); -int irm_enroll_ipcp(instance_name_t * api, - char * dif_name); +int irm_bind(char * name, + char * ap_name, + uint16_t opts, + int argc, + char ** argv); -int irm_bootstrap_ipcp(instance_name_t * api, - struct dif_config * conf); +int irm_unbind(char * name, + char * ap_name, + uint16_t opts); -int irm_reg(char * name, - instance_name_t * api, - int argc, - char ** argv, - bool autoexec, - char ** difs, - size_t difs_size); +int irm_reg(char * name, + char ** difs, + size_t difs_size); -int irm_unreg(char * name, - const instance_name_t * api, - char ** difs, - size_t difs_size, - bool hard); +int irm_unreg(char * name, + char ** difs, + size_t difs_size); #endif /* OUROBOROS_IRM_H */ diff --git a/include/ouroboros/irm_config.h b/include/ouroboros/irm_config.h new file mode 100644 index 00000000..d5f2b565 --- /dev/null +++ b/include/ouroboros/irm_config.h @@ -0,0 +1,73 @@ +/* + * Ouroboros - Copyright (C) 2016 + * + * Configuration information for the IPC Resource Manager + * + * Sander Vrijders + * + * 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 + +#ifndef OUROBOROS_IRM_CONFIG_H +#define OUROBOROS_IRM_CONFIG_H + +/* Name binding options */ + +#define BIND_AP_AUTO 0x01 +#define BIND_AP_UNIQUE 0x02 + +enum ipcp_type { + IPCP_NORMAL = 0, + IPCP_LOCAL, + IPCP_SHIM_UDP, + IPCP_SHIM_ETH_LLC +}; + +struct dif_config { + char * dif_name; + enum ipcp_type type; + + union { + /* Normal DIF */ + struct { + uint8_t addr_size; + uint8_t cep_id_size; + uint8_t pdu_length_size; + uint8_t qos_id_size; + uint8_t seqno_size; + + /* DUP constants */ + uint8_t ttl_size; + uint8_t chk_size; + + uint32_t min_pdu_size; + uint32_t max_pdu_size; + }; + /* Shim UDP */ + struct { + uint32_t ip_addr; + uint32_t dns_addr; + }; + /* Shim Ethernet LLC */ + struct { + char * if_name; + }; + }; +}; + +#endif /* OUROBOROS_IRM_CONFIG_H */ diff --git a/src/ipcpd/ipcp-data.h b/src/ipcpd/ipcp-data.h index 2e86ba11..ce20730b 100644 --- a/src/ipcpd/ipcp-data.h +++ b/src/ipcpd/ipcp-data.h @@ -25,7 +25,6 @@ #define IPCPD_IPCP_DATA_H #include -#include #include #include #include diff --git a/src/ipcpd/ipcp-ops.h b/src/ipcpd/ipcp-ops.h index a1d9f23f..879ce8e2 100644 --- a/src/ipcpd/ipcp-ops.h +++ b/src/ipcpd/ipcp-ops.h @@ -24,7 +24,7 @@ #ifndef IPCPD_IPCP_OPS_H #define IPCPD_IPCP_OPS_H -#include +#include #include #include diff --git a/src/ipcpd/ipcp.c b/src/ipcpd/ipcp.c index b5108712..54d1fad3 100644 --- a/src/ipcpd/ipcp.c +++ b/src/ipcpd/ipcp.c @@ -47,17 +47,13 @@ struct ipcp * ipcp_instance_create() int ipcp_arg_check(int argc, char * argv[]) { - if (argc != 3) + if (argc != 2) return -1; /* argument 1: pid of irmd */ if (atoi(argv[1]) == 0) return -1; - /* name conformity responsibility of NMS */ - - /* argument 2: ap name */ - return 0; } diff --git a/src/ipcpd/local/main.c b/src/ipcpd/local/main.c index 06e2b0a4..1f1cf839 100644 --- a/src/ipcpd/local/main.c +++ b/src/ipcpd/local/main.c @@ -28,7 +28,7 @@ #include #include #include -#include +#include #include #include #include @@ -67,7 +67,7 @@ struct ipcp * _ipcp; /* the shim needs access to these internals */ struct shim_ap_data { - instance_name_t * api; + pid_t api; struct shm_du_map * dum; struct bmp * fds; struct shm_ap_rbuff * rb; @@ -82,7 +82,7 @@ struct shim_ap_data { } * _ap_instance; -static int shim_ap_init(char * ap_name) +static int shim_ap_init() { int i; @@ -91,30 +91,16 @@ static int shim_ap_init(char * ap_name) return -1; } - _ap_instance->api = instance_name_create(); - if (_ap_instance->api == NULL) { - free(_ap_instance); - return -1; - } - - if (instance_name_init_from(_ap_instance->api, - ap_name, - getpid()) == NULL) { - instance_name_destroy(_ap_instance->api); - free(_ap_instance); - return -1; - } + _ap_instance->api = getpid(); _ap_instance->fds = bmp_create(AP_MAX_FLOWS, 0); if (_ap_instance->fds == NULL) { - instance_name_destroy(_ap_instance->api); free(_ap_instance); return -1; } _ap_instance->dum = shm_du_map_open(); if (_ap_instance->dum == NULL) { - instance_name_destroy(_ap_instance->api); bmp_destroy(_ap_instance->fds); free(_ap_instance); return -1; @@ -122,7 +108,6 @@ static int shim_ap_init(char * ap_name) _ap_instance->rb = shm_ap_rbuff_create(); if (_ap_instance->rb == NULL) { - instance_name_destroy(_ap_instance->api); shm_du_map_close(_ap_instance->dum); bmp_destroy(_ap_instance->fds); free(_ap_instance); @@ -153,8 +138,6 @@ void shim_ap_fini() if (_ipcp->state != IPCP_SHUTDOWN) LOG_WARN("Cleaning up AP while not in shutdown."); - if (_ap_instance->api != NULL) - instance_name_destroy(_ap_instance->api); if (_ap_instance->fds != NULL) bmp_destroy(_ap_instance->fds); if (_ap_instance->dum != NULL) @@ -592,8 +575,6 @@ static struct ipcp * ipcp_local_create() int main (int argc, char * argv[]) { - /* argument 1: pid of irmd ? */ - /* argument 2: ap name */ struct sigaction sig_act; sigset_t sigset; sigemptyset(&sigset); @@ -607,7 +588,7 @@ int main (int argc, char * argv[]) exit(1); } - if (shim_ap_init(argv[2]) < 0) + if (shim_ap_init() < 0) exit(1); /* store the process id of the irmd */ diff --git a/src/ipcpd/shim-eth-llc/main.c b/src/ipcpd/shim-eth-llc/main.c index 9bd6920b..6cb5c9cc 100644 --- a/src/ipcpd/shim-eth-llc/main.c +++ b/src/ipcpd/shim-eth-llc/main.c @@ -31,7 +31,7 @@ #include #include #include -#include +#include #include #include #include @@ -675,7 +675,6 @@ static void * eth_llc_ipcp_sdu_reader(void * o) dst_mac, MAC_SIZE) && memcmp(br_addr, dst_mac, MAC_SIZE)) { - LOG_DBG("Not a unicast or broadcast frame."); #if defined(PACKET_RX_RING) && defined(PACKET_TX_RING) offset = (offset + 1) & (SHM_BLOCKS_IN_MAP - 1); header->tp_status = TP_STATUS_KERNEL; @@ -715,7 +714,6 @@ static void * eth_llc_ipcp_sdu_reader(void * o) if (j < 0) { pthread_rwlock_unlock(&shim_data(_ipcp)->flows_lock); pthread_rwlock_unlock(&_ipcp->state_lock); - LOG_DBG("Received data for unknown flow."); #if defined(PACKET_RX_RING) && defined(PACKET_TX_RING) offset = (offset + 1) & (SHM_BLOCKS_IN_MAP - 1); @@ -1236,8 +1234,6 @@ static struct ipcp_ops eth_llc_ops = { int main(int argc, char * argv[]) { - /* argument 1: pid of irmd ? */ - /* argument 2: ap name */ struct sigaction sig_act; sigset_t sigset; int i = 0; diff --git a/src/ipcpd/shim-udp/main.c b/src/ipcpd/shim-udp/main.c index 221e60d2..99b8aa7f 100644 --- a/src/ipcpd/shim-udp/main.c +++ b/src/ipcpd/shim-udp/main.c @@ -29,7 +29,7 @@ #include #include #include -#include +#include #include #include #include @@ -86,7 +86,7 @@ struct ipcp * _ipcp; /* the shim needs access to these internals */ struct shim_ap_data { - instance_name_t * api; + pid_t api; struct shm_du_map * dum; struct bmp * fds; struct shm_ap_rbuff * rb; @@ -103,7 +103,7 @@ struct shim_ap_data { pthread_mutex_t fd_set_lock; } * _ap_instance; -static int shim_ap_init(char * ap_name) +static int shim_ap_init() { int i; @@ -112,30 +112,16 @@ static int shim_ap_init(char * ap_name) return -1; } - _ap_instance->api = instance_name_create(); - if (_ap_instance->api == NULL) { - free(_ap_instance); - return -1; - } - - if (instance_name_init_from(_ap_instance->api, - ap_name, - getpid()) == NULL) { - instance_name_destroy(_ap_instance->api); - free(_ap_instance); - return -1; - } + _ap_instance->api = getpid(); _ap_instance->fds = bmp_create(AP_MAX_FLOWS, 0); if (_ap_instance->fds == NULL) { - instance_name_destroy(_ap_instance->api); free(_ap_instance); return -1; } _ap_instance->dum = shm_du_map_open(); if (_ap_instance->dum == NULL) { - instance_name_destroy(_ap_instance->api); bmp_destroy(_ap_instance->fds); free(_ap_instance); return -1; @@ -143,7 +129,6 @@ static int shim_ap_init(char * ap_name) _ap_instance->rb = shm_ap_rbuff_create(); if (_ap_instance->rb == NULL) { - instance_name_destroy(_ap_instance->api); shm_du_map_close(_ap_instance->dum); bmp_destroy(_ap_instance->fds); free(_ap_instance); @@ -174,8 +159,6 @@ void shim_ap_fini() if (_ipcp->state != IPCP_SHUTDOWN) LOG_WARN("Cleaning up AP while not in shutdown."); - if (_ap_instance->api != NULL) - instance_name_destroy(_ap_instance->api); if (_ap_instance->fds != NULL) bmp_destroy(_ap_instance->fds); if (_ap_instance->dum != NULL) @@ -1576,8 +1559,6 @@ static struct ipcp * ipcp_udp_create() int main (int argc, char * argv[]) { - /* argument 1: pid of irmd ? */ - /* argument 2: ap name */ struct sigaction sig_act; sigset_t sigset; sigemptyset(&sigset); @@ -1591,7 +1572,7 @@ int main (int argc, char * argv[]) exit(1); } - if (shim_ap_init(argv[2]) < 0) + if (shim_ap_init() < 0) exit(1); /* store the process id of the irmd */ diff --git a/src/irmd/main.c b/src/irmd/main.c index 12fc4bb1..74105653 100644 --- a/src/irmd/main.c +++ b/src/irmd/main.c @@ -25,13 +25,11 @@ #include #include #include -#include #include #include #include -#include #include -#include +#include #include #include #include @@ -58,17 +56,14 @@ #define IRMD_CLEANUP_TIMER ((IRMD_FLOW_TIMEOUT / 20) * MILLION) /* ns */ -#define REG_AP_AUTO 0x0001 -/* FIXME: add support for unique */ -#define REG_AP_UNIQUE 0x0002 - #define reg_entry_has_api(e, id) (reg_entry_get_reg_instance(e, id) != NULL) #define reg_entry_has_ap_name(e, name) (reg_entry_get_ap_name(e, name) != NULL) #define reg_entry_has_ap_auto(e, name) (reg_entry_get_reg_auto(e, name) != NULL) struct ipcp_entry { struct list_head next; - instance_name_t * api; + char * name; + pid_t api; char * dif_name; }; @@ -342,7 +337,7 @@ static struct ipcp_entry * ipcp_entry_create() if (e == NULL) return NULL; - e->api = NULL; + e->name = NULL; e->dif_name = NULL; INIT_LIST_HEAD(&e->next); @@ -355,8 +350,8 @@ static void ipcp_entry_destroy(struct ipcp_entry * e) if (e == NULL) return; - if (e->api != NULL) - instance_name_destroy(e->api); + if (e->name != NULL) + free(e->name); if (e->dif_name != NULL) free(e->dif_name); @@ -364,7 +359,7 @@ static void ipcp_entry_destroy(struct ipcp_entry * e) free(e); } -static struct ipcp_entry * get_ipcp_entry_by_name(instance_name_t * api) +static struct ipcp_entry * get_ipcp_entry_by_pid(pid_t api) { struct list_head * pos = NULL; @@ -372,34 +367,19 @@ static struct ipcp_entry * get_ipcp_entry_by_name(instance_name_t * api) struct ipcp_entry * tmp = list_entry(pos, struct ipcp_entry, next); - if (instance_name_cmp(api, tmp->api) == 0) + if (api == tmp->api) return tmp; } return NULL; } -static instance_name_t * get_ipcp_by_name(char * ap_name) -{ - struct list_head * pos = NULL; - - list_for_each(pos, &instance->ipcps) { - struct ipcp_entry * e = - list_entry(pos, struct ipcp_entry, next); - - if (strcmp(e->api->name, ap_name) == 0) - return e->api; - } - - return NULL; -} - /* * FIXME: this just returns the first IPCP that * matches the requested DIF name for now */ -static instance_name_t * get_ipcp_by_dst_name(char * dst_name, - char * dif_name) +static pid_t get_ipcp_by_dst_name(char * dst_name, + char * dif_name) { struct list_head * pos = NULL; @@ -419,7 +399,7 @@ static instance_name_t * get_ipcp_by_dst_name(char * dst_name, } } - return NULL; + return 0; } static struct reg_entry * reg_entry_create() @@ -647,7 +627,7 @@ static struct reg_entry * get_reg_entry_by_ap_id(pid_t pid) return NULL; } -static int registry_add_entry(char * name, char * ap_name, uint32_t flags) +static int registry_add_entry(char * name, char * ap_name, uint16_t flags) { struct reg_entry * e = NULL; @@ -694,7 +674,7 @@ static int registry_add_ap_auto(char * name, return -1; } - if (!(e->flags & REG_AP_AUTO)) { + if (!(e->flags & BIND_AP_AUTO)) { LOG_DBG("%s does not allow auto-instantiation.", name); return -1; } @@ -838,7 +818,7 @@ static int registry_remove_ap_instance(char * name, pid_t pid) reg_instance_destroy(i); if (list_empty(&e->ap_instances)) { - if ((e->flags & REG_AP_AUTO) && + if ((e->flags & BIND_AP_AUTO) && !list_empty(&e->auto_ap_info)) e->state = REG_NAME_AUTO_ACCEPT; else @@ -890,7 +870,7 @@ static void registry_del_name(char * name) return; } -static pid_t create_ipcp(char * ap_name, +static pid_t create_ipcp(char * name, enum ipcp_type ipcp_type) { pid_t pid; @@ -903,7 +883,7 @@ static pid_t create_ipcp(char * ap_name, return 0; } - pid = ipcp_create(ap_name, ipcp_type); + pid = ipcp_create(ipcp_type); if (pid == -1) { pthread_rwlock_unlock(&instance->state_lock); LOG_ERR("Failed to create IPCP."); @@ -918,15 +898,9 @@ static pid_t create_ipcp(char * ap_name, INIT_LIST_HEAD(&tmp->next); - tmp->api = instance_name_create(); - if (tmp->api == NULL) { - ipcp_entry_destroy(tmp); - pthread_rwlock_unlock(&instance->state_lock); - return -1; - } - - if(instance_name_init_from(tmp->api, ap_name, pid) == NULL) { - instance_name_destroy(tmp->api); + tmp->api = pid; + tmp->name = strdup(name); + if (tmp->name == NULL) { ipcp_entry_destroy(tmp); pthread_rwlock_unlock(&instance->state_lock); return -1; @@ -941,56 +915,40 @@ static pid_t create_ipcp(char * ap_name, pthread_rwlock_unlock(&instance->reg_lock); pthread_rwlock_unlock(&instance->state_lock); - LOG_INFO("Created IPCP %s-%d.", ap_name, pid); + LOG_INFO("Created IPCP %d.", pid); return pid; } -static int destroy_ipcp(instance_name_t * api) +static int destroy_ipcp(pid_t api) { struct list_head * pos = NULL; struct list_head * n = NULL; - pid_t pid = 0; - - if (api == NULL) - return 0; pthread_rwlock_rdlock(&instance->state_lock); pthread_rwlock_wrlock(&instance->reg_lock); - if (api->id == 0) - api = get_ipcp_by_name(api->name); - - if (api == NULL) { - pthread_rwlock_unlock(&instance->reg_lock); - pthread_rwlock_unlock(&instance->state_lock); - LOG_ERR("No such IPCP in the system."); - return 0; - } - - pid = api->id; - if (ipcp_destroy(api->id)) - LOG_ERR("Could not destroy IPCP."); - 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) { + if (api == tmp->api) { + if (ipcp_destroy(api)) + LOG_ERR("Could not destroy IPCP."); list_del(&tmp->next); ipcp_entry_destroy(tmp); + + LOG_INFO("Destroyed IPCP %d.", api); } } pthread_rwlock_unlock(&instance->reg_lock); pthread_rwlock_unlock(&instance->state_lock); - LOG_INFO("Destroyed IPCP %d.", pid); - return 0; } -static int bootstrap_ipcp(instance_name_t * api, +static int bootstrap_ipcp(pid_t api, dif_config_msg_t * conf) { struct ipcp_entry * entry = NULL; @@ -1004,17 +962,7 @@ static int bootstrap_ipcp(instance_name_t * api, pthread_rwlock_wrlock(&instance->reg_lock); - if (api->id == 0) - api = get_ipcp_by_name(api->name); - - if (api == NULL) { - pthread_rwlock_unlock(&instance->reg_lock); - pthread_rwlock_unlock(&instance->state_lock); - LOG_ERR("No such IPCP in the system."); - return -1; - } - - entry = get_ipcp_entry_by_name(api); + entry = get_ipcp_entry_by_pid(api); if (entry == NULL) { pthread_rwlock_unlock(&instance->reg_lock); pthread_rwlock_unlock(&instance->state_lock); @@ -1030,7 +978,7 @@ static int bootstrap_ipcp(instance_name_t * api, return -1; } - if (ipcp_bootstrap(entry->api->id, conf)) { + if (ipcp_bootstrap(entry->api, conf)) { pthread_rwlock_unlock(&instance->reg_lock); pthread_rwlock_unlock(&instance->state_lock); LOG_ERR("Could not bootstrap IPCP."); @@ -1042,14 +990,14 @@ static int bootstrap_ipcp(instance_name_t * api, pthread_rwlock_unlock(&instance->reg_lock); pthread_rwlock_unlock(&instance->state_lock); - LOG_INFO("Bootstrapped IPCP %s-%d in DIF %s.", - api->name, api->id, conf->dif_name); + LOG_INFO("Bootstrapped IPCP %d in DIF %s.", + entry->api, conf->dif_name); return 0; } -static int enroll_ipcp(instance_name_t * api, - char * dif_name) +static int enroll_ipcp(pid_t api, + char * dif_name) { char ** n_1_difs = NULL; ssize_t n_1_difs_size = 0; @@ -1064,7 +1012,7 @@ static int enroll_ipcp(instance_name_t * api, pthread_rwlock_rdlock(&instance->reg_lock); - entry = get_ipcp_entry_by_name(api); + entry = get_ipcp_entry_by_pid(api); if (entry == NULL) { pthread_rwlock_unlock(&instance->reg_lock); pthread_rwlock_unlock(&instance->state_lock); @@ -1090,7 +1038,7 @@ static int enroll_ipcp(instance_name_t * api, return -1; } - if (ipcp_enroll(api->id, dif_name, n_1_difs[0])) { + if (ipcp_enroll(api, dif_name, n_1_difs[0])) { free(entry->dif_name); entry->dif_name = NULL; pthread_rwlock_unlock(&instance->reg_lock); @@ -1102,30 +1050,138 @@ static int enroll_ipcp(instance_name_t * api, pthread_rwlock_unlock(&instance->reg_lock); pthread_rwlock_unlock(&instance->state_lock); - LOG_INFO("Enrolled IPCP %s-%d in DIF %s.", - api->name, api->id, dif_name); + LOG_INFO("Enrolled IPCP %d in DIF %s.", + entry->api, dif_name); return 0; } -/* FIXME: distinction between registering names and associating instances */ +static int bind_name(char * name, + char * ap_name, + uint16_t opts, + int argc, + char ** argv) +{ + int i; + + char ** argv_dup = NULL; + char * apn = path_strip(ap_name); + + pthread_rwlock_rdlock(&instance->state_lock); + + if (instance->state != IRMD_RUNNING) { + pthread_rwlock_unlock(&instance->state_lock); + return -1; + } + + pthread_rwlock_wrlock(&instance->reg_lock); + + if (registry_add_entry(strdup(name), strdup(apn), opts) < 0) { + pthread_rwlock_unlock(&instance->reg_lock); + pthread_rwlock_unlock(&instance->state_lock); + LOG_ERR("Failed to register %s.", name); + return -1; + } + + if (opts & BIND_AP_AUTO) { + /* we need to duplicate argv */ + if (argc != 0) { + argv_dup = malloc((argc + 2) * sizeof(*argv_dup)); + argv_dup[0] = strdup(ap_name); + for (i = 1; i <= argc; ++i) + argv_dup[i] = strdup(argv[i - 1]); + argv_dup[argc + 1] = NULL; + } + + registry_add_ap_auto(name, strdup(apn), argv_dup); + } + + pthread_rwlock_unlock(&instance->reg_lock); + pthread_rwlock_unlock(&instance->state_lock); + + return 0; +} + +static int unbind_name(char * name, + char * ap_name, + uint16_t opts) + +{ + struct reg_entry * rne = NULL; + + if (name == NULL || ap_name == NULL) + return -1; + + pthread_rwlock_rdlock(&instance->state_lock); + + if (instance->state != IRMD_RUNNING) { + pthread_rwlock_unlock(&instance->state_lock); + return -1; + } + + pthread_rwlock_wrlock(&instance->reg_lock); + + rne = get_reg_entry_by_name(name); + if (rne == NULL) { + pthread_rwlock_unlock(&instance->reg_lock); + pthread_rwlock_unlock(&instance->state_lock); + LOG_ERR("Tried to unbind a name that is not bound."); + return -1; + } + + /* + * FIXME: Remove the mapping of name to ap_name. + * Remove the name only if it was the last mapping. + */ + registry_del_name(rne->name); + + pthread_rwlock_unlock(&instance->reg_lock); + pthread_rwlock_unlock(&instance->state_lock); + + return 0; +} + +static ssize_t list_ipcps(char * name, + pid_t ** pids) +{ + struct list_head * pos = NULL; + ssize_t count = 0; + int i = 0; + + list_for_each(pos, &instance->ipcps) { + struct ipcp_entry * tmp = + list_entry(pos, struct ipcp_entry, next); + + if (wildcard_match(name, tmp->name) == 0) { + count++; + } + } + + *pids = malloc(count * sizeof(pid_t)); + if (*pids == NULL) { + return -1; + } + + list_for_each(pos, &instance->ipcps) { + struct ipcp_entry * tmp = + list_entry(pos, struct ipcp_entry, next); + + if (wildcard_match(name, tmp->name) == 0) { + (*pids)[i++] = tmp->api; + } + } + + return count; +} + static int ap_reg(char * name, - char * ap_name, - pid_t ap_id, - int argc, - char ** argv, - bool autoexec, char ** difs, size_t len) { int i; int ret = 0; - + struct reg_entry * reg = NULL; struct list_head * pos = NULL; - char ** argv_dup = NULL; - char * apn = path_strip(ap_name); - - uint32_t flags = 0; pthread_rwlock_rdlock(&instance->state_lock); @@ -1142,13 +1198,11 @@ static int ap_reg(char * name, return -1; } - if (autoexec) - flags |= REG_AP_AUTO; - - if (registry_add_entry(strdup(name), strdup(apn), flags) < 0) { + reg = get_reg_entry_by_name(name); + if (reg == NULL) { pthread_rwlock_unlock(&instance->reg_lock); pthread_rwlock_unlock(&instance->state_lock); - LOG_ERR("Failed to register %s.", name); + LOG_ERR("Tried to register a name that is not bound."); return -1; } @@ -1161,13 +1215,13 @@ static int ap_reg(char * name, for (i = 0; i < len; ++i) { if (wildcard_match(difs[i], e->dif_name) == 0) { - if (ipcp_name_reg(e->api->id, name)) { + if (ipcp_name_reg(e->api, name)) { LOG_ERR("Could not register " - "%s in DIF %s as %s.", - apn, e->dif_name, name); + "%s in DIF %s.", + name, e->dif_name); } else { - LOG_INFO("Registered %s as %s in %s", - apn, name, e->dif_name); + LOG_INFO("Registered %s in %s", + name, e->dif_name); ++ret; } } @@ -1180,21 +1234,6 @@ static int ap_reg(char * name, return -1; } - if (autoexec) { - /* we need to duplicate argv */ - if (argc != 0) { - argv_dup = malloc((argc + 2) * sizeof(*argv_dup)); - argv_dup[0] = strdup(ap_name); - for (i = 1; i <= argc; ++i) - argv_dup[i] = strdup(argv[i - 1]); - argv_dup[argc + 1] = NULL; - } - - registry_add_ap_auto(name, strdup(apn), argv_dup); - } else { - registry_add_ap_instance(name, ap_id); - } - pthread_rwlock_unlock(&instance->reg_lock); pthread_rwlock_unlock(&instance->state_lock); @@ -1202,11 +1241,8 @@ static int ap_reg(char * name, } static int ap_unreg(char * name, - char * ap_name, - pid_t ap_id, char ** difs, - size_t len, - bool hard) + size_t len) { int i; int ret = 0; @@ -1225,12 +1261,6 @@ static int ap_unreg(char * name, pthread_rwlock_wrlock(&instance->reg_lock); - if (!hard && strcmp(difs[0], "*") != 0) { - LOG_INFO("Unregistration not complete yet."); - LOG_MISSING; - return -1; - } - list_for_each(pos, &instance->ipcps) { struct ipcp_entry * e = list_entry(pos, struct ipcp_entry, next); @@ -1240,7 +1270,7 @@ static int ap_unreg(char * name, for (i = 0; i < len; ++i) { if (wildcard_match(difs[i], e->dif_name) == 0) { - if (ipcp_name_unreg(e->api->id, + if (ipcp_name_unreg(e->api, rne->name)) { LOG_ERR("Could not unregister " "%s in DIF %s.", @@ -1251,8 +1281,6 @@ static int ap_unreg(char * name, } } - registry_del_name(rne->name); - pthread_rwlock_unlock(&instance->reg_lock); pthread_rwlock_unlock(&instance->state_lock); @@ -1405,7 +1433,7 @@ static struct port_map_entry * flow_alloc(pid_t pid, struct qos_spec * qos) { struct port_map_entry * pme; - instance_name_t * ipcp; + pid_t ipcp; char * dif_name = NULL; /* FIXME: Map qos_spec to qos_cube */ @@ -1435,7 +1463,7 @@ static struct port_map_entry * flow_alloc(pid_t pid, dif_name = qos->dif_name; ipcp = get_ipcp_by_dst_name(dst_name, dif_name); - if (ipcp == NULL) { + if (ipcp == 0) { pthread_rwlock_unlock(&instance->reg_lock); pthread_rwlock_unlock(&instance->state_lock); LOG_ERR("Unknown DIF name."); @@ -1446,14 +1474,14 @@ static struct port_map_entry * flow_alloc(pid_t pid, pthread_rwlock_wrlock(&instance->flows_lock); pme->port_id = bmp_allocate(instance->port_ids); - pme->n_1_pid = ipcp->id; + pme->n_1_pid = ipcp; list_add(&pme->next, &instance->port_map); pthread_rwlock_unlock(&instance->flows_lock); pthread_rwlock_unlock(&instance->state_lock); - if (ipcp_flow_alloc(ipcp->id, + if (ipcp_flow_alloc(ipcp, pme->port_id, pme->n_pid, dst_name, @@ -1792,7 +1820,7 @@ static void irm_destroy() list_for_each_safe(h, t, &instance->ipcps) { struct ipcp_entry * e = list_entry(h, struct ipcp_entry, next); list_del(&e->next); - ipcp_destroy(e->api->id); + ipcp_destroy(e->api); ipcp_entry_destroy(e); } @@ -1964,10 +1992,10 @@ void * mainloop() int cli_sockfd; irm_msg_t * msg; ssize_t count; - instance_name_t api; buffer_t buffer; irm_msg_t ret_msg = IRM_MSG__INIT; struct port_map_entry * e = NULL; + pid_t * pids = NULL; ret_msg.code = IRM_MSG_CODE__IRM_REPLY; @@ -1992,48 +2020,57 @@ void * mainloop() pthread_cleanup_push(clean_msg, (void *) msg); - api.name = msg->ap_name; - if (msg->has_api_id == true) - api.id = msg->api_id; - switch (msg->code) { case IRM_MSG_CODE__IRM_CREATE_IPCP: ret_msg.has_result = true; - ret_msg.result = create_ipcp(msg->ap_name, + ret_msg.result = create_ipcp(msg->dst_name, msg->ipcp_type); break; case IRM_MSG_CODE__IRM_DESTROY_IPCP: ret_msg.has_result = true; - ret_msg.result = destroy_ipcp(&api); + ret_msg.result = destroy_ipcp(msg->api); break; case IRM_MSG_CODE__IRM_BOOTSTRAP_IPCP: ret_msg.has_result = true; - ret_msg.result = bootstrap_ipcp(&api, msg->conf); + ret_msg.result = bootstrap_ipcp(msg->api, + msg->conf); break; case IRM_MSG_CODE__IRM_ENROLL_IPCP: ret_msg.has_result = true; - ret_msg.result = enroll_ipcp(&api, + ret_msg.result = enroll_ipcp(msg->api, msg->dif_name[0]); break; - case IRM_MSG_CODE__IRM_AP_REG: + case IRM_MSG_CODE__IRM_BIND: + ret_msg.has_result = true; + ret_msg.result = bind_name(msg->dst_name, + msg->ap_name, + msg->opts, + msg->n_args, + msg->args); + break; + case IRM_MSG_CODE__IRM_UNBIND: + ret_msg.has_result = true; + ret_msg.result = unbind_name(msg->dst_name, + msg->ap_name, + msg->opts); + break; + case IRM_MSG_CODE__IRM_LIST_IPCPS: + ret_msg.n_pids = list_ipcps(msg->dst_name, + &pids); + ret_msg.pids = pids; + ret_msg.has_result = true; + break; + case IRM_MSG_CODE__IRM_REG: ret_msg.has_result = true; ret_msg.result = ap_reg(msg->dst_name, - msg->ap_name, - msg->pid, - msg->n_args, - msg->args, - msg->autoexec, msg->dif_name, msg->n_dif_name); break; - case IRM_MSG_CODE__IRM_AP_UNREG: + case IRM_MSG_CODE__IRM_UNREG: ret_msg.has_result = true; ret_msg.result = ap_unreg(msg->dst_name, - msg->ap_name, - msg->pid, msg->dif_name, - msg->n_dif_name, - msg->hard); + msg->n_dif_name); break; case IRM_MSG_CODE__IRM_FLOW_ACCEPT: e = flow_accept(msg->pid, @@ -2106,12 +2143,16 @@ void * mainloop() buffer.size = irm_msg__get_packed_size(&ret_msg); if (buffer.size == 0) { LOG_ERR("Failed to send reply message."); + if (pids != NULL) + free(pids); close(cli_sockfd); continue; } buffer.data = malloc(buffer.size); if (buffer.data == NULL) { + if (pids != NULL) + free(pids); close(cli_sockfd); continue; } @@ -2120,10 +2161,15 @@ void * mainloop() if (write(cli_sockfd, buffer.data, buffer.size) == -1) { free(buffer.data); + if (pids != NULL) + free(pids); close(cli_sockfd); continue; } + if (pids != NULL) + free(pids); + free(buffer.data); close(cli_sockfd); } diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index 2a75ef17..410be6fe 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -27,7 +27,6 @@ set(SOURCE_FILES cdap.c dev.c du_buff.c - instance_name.c ipcp.c irm.c list.c diff --git a/src/lib/dev.c b/src/lib/dev.c index ad311f7f..c6f25cdf 100644 --- a/src/lib/dev.c +++ b/src/lib/dev.c @@ -28,7 +28,6 @@ #include #include #include -#include #include #include #include @@ -45,7 +44,8 @@ struct flow { }; struct ap_data { - instance_name_t * api; + char * ap_name; + pid_t api; struct shm_du_map * dum; struct bmp * fds; struct shm_ap_rbuff * rb; @@ -66,30 +66,17 @@ int ap_init(char * ap_name) return -ENOMEM; } - _ap_instance->api = instance_name_create(); - if (_ap_instance->api == NULL) { - free(_ap_instance); - return -ENOMEM; - } - - if (instance_name_init_from(_ap_instance->api, - ap_name, - getpid()) == NULL) { - instance_name_destroy(_ap_instance->api); - free(_ap_instance); - return -ENOMEM; - } + _ap_instance->api = getpid(); + _ap_instance->ap_name = ap_name; _ap_instance->fds = bmp_create(AP_MAX_FLOWS, 0); if (_ap_instance->fds == NULL) { - instance_name_destroy(_ap_instance->api); free(_ap_instance); return -ENOMEM; } _ap_instance->dum = shm_du_map_open(); if (_ap_instance->dum == NULL) { - instance_name_destroy(_ap_instance->api); bmp_destroy(_ap_instance->fds); free(_ap_instance); return -1; @@ -97,7 +84,6 @@ int ap_init(char * ap_name) _ap_instance->rb = shm_ap_rbuff_create(); if (_ap_instance->rb == NULL) { - instance_name_destroy(_ap_instance->api); shm_du_map_close(_ap_instance->dum); bmp_destroy(_ap_instance->fds); free(_ap_instance); @@ -124,8 +110,6 @@ void ap_fini(void) pthread_rwlock_wrlock(&_ap_instance->data_lock); - if (_ap_instance->api != NULL) - instance_name_destroy(_ap_instance->api); if (_ap_instance->fds != NULL) bmp_destroy(_ap_instance->fds); if (_ap_instance->dum != NULL) @@ -168,8 +152,8 @@ int flow_accept(char ** ae_name) pthread_rwlock_rdlock(&_ap_instance->data_lock); - msg.ap_name = _ap_instance->api->name; - msg.pid = _ap_instance->api->id; + msg.ap_name = _ap_instance->ap_name; + msg.pid = _ap_instance->api; pthread_rwlock_unlock(&_ap_instance->data_lock); @@ -238,7 +222,7 @@ int flow_alloc_resp(int fd, msg.code = IRM_MSG_CODE__IRM_FLOW_ALLOC_RESP; msg.has_pid = true; - msg.pid = _ap_instance->api->id; + msg.pid = _ap_instance->api; msg.has_port_id = true; pthread_rwlock_rdlock(&_ap_instance->data_lock); @@ -299,7 +283,7 @@ int flow_alloc(char * dst_name, pthread_rwlock_rdlock(&_ap_instance->data_lock); - msg.pid = _ap_instance->api->id; + msg.pid = _ap_instance->api; pthread_rwlock_unlock(&_ap_instance->data_lock); diff --git a/src/lib/instance_name.c b/src/lib/instance_name.c deleted file mode 100644 index 844bb924..00000000 --- a/src/lib/instance_name.c +++ /dev/null @@ -1,248 +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 "instance-name" - -#include -#include -#include -#include - -#include -#include -#include - -instance_name_t * instance_name_create() -{ - instance_name_t * tmp; - - tmp = malloc(sizeof *tmp); - if (tmp == NULL) - return NULL; - - 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 || n->name == NULL) - return; - - 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(instance_name_t * dst, - const instance_name_t * src) -{ - instance_name_t * res; - - if (src == NULL || dst == NULL) - return -EINVAL; - - res = instance_name_init_from(dst, src->name, src->id); - if (res == NULL) - return -ENOMEM; - - 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(tmp, src)) { - 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 77263bcf..70f73e0e 100644 --- a/src/lib/ipcp.c +++ b/src/lib/ipcp.c @@ -99,8 +99,7 @@ static ipcp_msg_t * send_recv_ipcp_msg(pid_t pid, return recv_msg; } -pid_t ipcp_create(char * ipcp_name, - enum ipcp_type ipcp_type) +pid_t ipcp_create(enum ipcp_type ipcp_type) { pid_t pid = 0; char irmd_pid[10]; @@ -150,7 +149,6 @@ pid_t ipcp_create(char * ipcp_name, char * argv[] = {full_name, irmd_pid, - ipcp_name, 0}; char * envp[] = {0}; diff --git a/src/lib/irm.c b/src/lib/irm.c index 8fd0a7e7..e3902469 100644 --- a/src/lib/irm.c +++ b/src/lib/irm.c @@ -27,22 +27,18 @@ #include #include #include -#include #include -pid_t irm_create_ipcp(char * ipcp_name, +pid_t irm_create_ipcp(char * name, enum ipcp_type ipcp_type) { irm_msg_t msg = IRM_MSG__INIT; irm_msg_t * recv_msg = NULL; int ret = -1; - if (ipcp_name == NULL) - return -EINVAL; - msg.code = IRM_MSG_CODE__IRM_CREATE_IPCP; - msg.ap_name = ipcp_name; + msg.dst_name = name; msg.has_ipcp_type = true; msg.ipcp_type = ipcp_type; @@ -61,19 +57,18 @@ pid_t irm_create_ipcp(char * ipcp_name, return ret; } -int irm_destroy_ipcp(instance_name_t * api) +int irm_destroy_ipcp(pid_t api) { irm_msg_t msg = IRM_MSG__INIT; irm_msg_t * recv_msg = NULL; int ret = -1; - if (api == NULL || api->name == NULL) + if (api == 0) return -EINVAL; - msg.code = IRM_MSG_CODE__IRM_DESTROY_IPCP; - msg.ap_name = api->name; - msg.has_api_id = true; - msg.api_id = api->id; + msg.code = IRM_MSG_CODE__IRM_DESTROY_IPCP; + msg.has_api = true; + msg.api = api; recv_msg = send_recv_irm_msg(&msg); if (recv_msg == NULL) @@ -90,7 +85,7 @@ int irm_destroy_ipcp(instance_name_t * api) return ret; } -int irm_bootstrap_ipcp(instance_name_t * api, +int irm_bootstrap_ipcp(pid_t api, struct dif_config * conf) { irm_msg_t msg = IRM_MSG__INIT; @@ -98,13 +93,12 @@ int irm_bootstrap_ipcp(instance_name_t * api, irm_msg_t * recv_msg = NULL; int ret = -1; - if (api == NULL || api->name == NULL || conf == NULL) + if (api == 0 || conf == NULL) return -EINVAL; - msg.code = IRM_MSG_CODE__IRM_BOOTSTRAP_IPCP; - msg.ap_name = api->name; - msg.has_api_id = true; - msg.api_id = api->id; + msg.code = IRM_MSG_CODE__IRM_BOOTSTRAP_IPCP; + msg.has_api = true; + msg.api = api; msg.conf = &config; config.dif_name = conf->dif_name; @@ -163,20 +157,60 @@ int irm_bootstrap_ipcp(instance_name_t * api, return ret; } -int irm_enroll_ipcp(instance_name_t * api, - char * dif_name) +ssize_t irm_list_ipcps(char * name, + pid_t ** apis) +{ + irm_msg_t msg = IRM_MSG__INIT; + irm_msg_t * recv_msg = NULL; + ssize_t nr = -1; + int i; + + if (apis == NULL) + return -EINVAL; + + msg.code = IRM_MSG_CODE__IRM_LIST_IPCPS; + msg.dst_name = name; + + recv_msg = send_recv_irm_msg(&msg); + if (recv_msg == NULL) { + free(msg.dif_name); + return -1; + } + + if (recv_msg->pids == NULL) { + irm_msg__free_unpacked(recv_msg, NULL); + return -1; + } + + nr = recv_msg->n_pids; + *apis = malloc(nr * sizeof(pid_t)); + if (*apis == NULL) { + irm_msg__free_unpacked(recv_msg, NULL); + return -ENOMEM; + } + + for (i = 0; i < nr; i++) { + (*apis)[i] = recv_msg->pids[i]; + } + + irm_msg__free_unpacked(recv_msg, NULL); + + return nr; +} + +int irm_enroll_ipcp(pid_t api, + char * dif_name) { irm_msg_t msg = IRM_MSG__INIT; irm_msg_t * recv_msg = NULL; int ret = -1; - if (api == NULL || api->name == NULL || dif_name == NULL) + if (api == 0 || dif_name == NULL) return -EINVAL; msg.code = IRM_MSG_CODE__IRM_ENROLL_IPCP; - msg.ap_name = api->name; - msg.has_api_id = true; - msg.api_id = api->id; + msg.has_api = true; + msg.api = api; msg.n_dif_name = 1; msg.dif_name = malloc(sizeof(*(msg.dif_name))); if (msg.dif_name == NULL) { @@ -203,39 +237,30 @@ int irm_enroll_ipcp(instance_name_t * api, return ret; } -int irm_reg(char * name, - instance_name_t * api, - int argc, - char ** argv, - bool autoexec, - char ** difs, - size_t difs_len) +int irm_bind(char * name, + char * ap_name, + uint16_t opts, + int argc, + char ** argv) { irm_msg_t msg = IRM_MSG__INIT; irm_msg_t * recv_msg = NULL; int ret = -1; - if (name == NULL || api->name == NULL) + if (name == NULL || ap_name == NULL) return -EINVAL; - msg.code = IRM_MSG_CODE__IRM_AP_REG; + msg.code = IRM_MSG_CODE__IRM_BIND; msg.dst_name = name; - msg.ap_name = api->name; - if (difs != NULL) { - msg.dif_name = difs; - msg.n_dif_name = difs_len; - } + msg.ap_name = ap_name; if (argv != NULL) { msg.n_args = argc; msg.args = argv; - } else { - msg.has_api_id = true; - msg.api_id = api->id; } - msg.has_autoexec = true; - msg.autoexec = autoexec; + msg.has_opts = true; + msg.opts = opts; recv_msg = send_recv_irm_msg(&msg); if (recv_msg == NULL) @@ -252,36 +277,90 @@ int irm_reg(char * name, return ret; } -int irm_unreg(char * name, - const instance_name_t * api, - char ** difs, - size_t difs_len, - bool hard) +int irm_unbind(char * name, + char * ap_name, + uint16_t opts) { irm_msg_t msg = IRM_MSG__INIT; irm_msg_t * recv_msg = NULL; int ret = -1; - if (name == NULL && api == NULL) + if (name == NULL || ap_name == NULL) return -EINVAL; - if (difs == NULL || - difs_len == 0 || - difs[0] == NULL) + msg.code = IRM_MSG_CODE__IRM_UNBIND; + + msg.dst_name = name; + msg.ap_name = ap_name; + msg.has_opts = true; + msg.opts = opts; + + recv_msg = send_recv_irm_msg(&msg); + if (recv_msg == NULL) + return -1; + + if (recv_msg->has_result == false) { + irm_msg__free_unpacked(recv_msg, NULL); + return -1; + } + + ret = recv_msg->result; + irm_msg__free_unpacked(recv_msg, NULL); + + return ret; +} + +int irm_reg(char * name, + char ** difs, + size_t difs_size) +{ + irm_msg_t msg = IRM_MSG__INIT; + irm_msg_t * recv_msg = NULL; + int ret = -1; + + if (name == NULL || difs == NULL || difs_size == 0) return -EINVAL; - msg.code = IRM_MSG_CODE__IRM_AP_UNREG; - if (api != NULL) { - msg.ap_name = api->name; - msg.has_api_id = true; - msg.api_id = api->id; + msg.code = IRM_MSG_CODE__IRM_REG; + + msg.dst_name = name; + + msg.dif_name = difs; + msg.n_dif_name = difs_size; + + recv_msg = send_recv_irm_msg(&msg); + if (recv_msg == NULL) + return -1; + + if (recv_msg->has_result == false) { + irm_msg__free_unpacked(recv_msg, NULL); + return -1; } + ret = recv_msg->result; + irm_msg__free_unpacked(recv_msg, NULL); + + return ret; +} + + +int irm_unreg(char * name, + char ** difs, + size_t difs_size) +{ + irm_msg_t msg = IRM_MSG__INIT; + irm_msg_t * recv_msg = NULL; + int ret = -1; + + if (name == NULL || difs == NULL || difs_size == 0) + return -EINVAL; + + msg.code = IRM_MSG_CODE__IRM_UNREG; + + msg.dst_name = name; + msg.dif_name = difs; - msg.n_dif_name = difs_len; - if (name != NULL) - msg.dst_name = name; - msg.hard = hard; + msg.n_dif_name = difs_size; recv_msg = send_recv_irm_msg(&msg); if (recv_msg == NULL) diff --git a/src/lib/irmd_messages.proto b/src/lib/irmd_messages.proto index 34366975..ab09f0db 100644 --- a/src/lib/irmd_messages.proto +++ b/src/lib/irmd_messages.proto @@ -25,26 +25,29 @@ import "dif_config.proto"; enum irm_msg_code { IRM_CREATE_IPCP = 1; IRM_DESTROY_IPCP = 2; - IRM_BOOTSTRAP_IPCP = 3; - IRM_ENROLL_IPCP = 4; - IRM_AP_REG = 5; - IRM_AP_UNREG = 6; - IRM_FLOW_ACCEPT = 7; - IRM_FLOW_ALLOC_RESP = 8; - IRM_FLOW_ALLOC = 9; - IRM_FLOW_ALLOC_RES = 10; - IRM_FLOW_DEALLOC = 11; - IPCP_FLOW_REQ_ARR = 12; - IPCP_FLOW_ALLOC_REPLY = 13; - IPCP_FLOW_DEALLOC = 14; - IRM_REPLY = 15; + IRM_LIST_IPCPS = 3; + IRM_BOOTSTRAP_IPCP = 4; + IRM_ENROLL_IPCP = 5; + IRM_BIND = 6; + IRM_UNBIND = 7; + IRM_REG = 8; + IRM_UNREG = 9; + IRM_FLOW_ACCEPT = 10; + IRM_FLOW_ALLOC_RESP = 11; + IRM_FLOW_ALLOC = 12; + IRM_FLOW_ALLOC_RES = 13; + IRM_FLOW_DEALLOC = 14; + IPCP_FLOW_REQ_ARR = 15; + IPCP_FLOW_ALLOC_REPLY = 16; + IPCP_FLOW_DEALLOC = 17; + IRM_REPLY = 18; }; message irm_msg { required irm_msg_code code = 1; optional string ap_name = 2; - optional string ae_name = 4; - optional uint32 api_id = 3; + optional string ae_name = 3; + optional uint32 api = 4; optional uint32 ipcp_type = 5; repeated string dif_name = 6; repeated string args = 7; @@ -53,7 +56,7 @@ message irm_msg { optional sint32 port_id = 10; optional int32 pid = 11; optional dif_config_msg conf = 12; - optional bool autoexec = 13; - optional bool hard = 14; + optional uint32 opts = 13; + repeated int32 pids = 14; optional sint32 result = 15; }; diff --git a/src/tools/irm/CMakeLists.txt b/src/tools/irm/CMakeLists.txt index d1f227a8..68297615 100644 --- a/src/tools/irm/CMakeLists.txt +++ b/src/tools/irm/CMakeLists.txt @@ -7,10 +7,13 @@ include_directories(${CMAKE_BINARY_DIR}/include) set(SOURCE_FILES # Add source files here irm.c - irm_create_ipcp.c - irm_destroy_ipcp.c - irm_bootstrap_ipcp.c - irm_enroll_ipcp.c + irm_ipcp_create.c + irm_ipcp_destroy.c + irm_ipcp_bootstrap.c + irm_ipcp_enroll.c + irm_unbind.c + irm_bind.c + irm_ipcp.c irm_register.c irm_unregister.c irm_utils.c diff --git a/src/tools/irm/irm.c b/src/tools/irm/irm.c index a1dc5ade..14420207 100644 --- a/src/tools/irm/irm.c +++ b/src/tools/irm/irm.c @@ -21,7 +21,6 @@ */ #include -#include #include #include #include @@ -32,10 +31,8 @@ static void usage() { printf("Usage: irm [OPERATION]\n\n" - "where OPERATION = {create_ipcp destroy_ipcp \n" - " bootstrap_ipcp enroll_ipcp\n" - " register unregister\n" - " register_ipcp unregister_ipcp\n"); + "where OPERATION = {ipcp bind unbind\n" + " register unregister\n"); } static int do_help(int argc, char **argv) @@ -48,10 +45,9 @@ static const struct cmd { const char * cmd; int (* func)(int argc, char ** argv); } cmds[] = { - { "create_ipcp", do_create_ipcp }, - { "destroy_ipcp", do_destroy_ipcp }, - { "bootstrap_ipcp", do_bootstrap_ipcp }, - { "enroll_ipcp", do_enroll_ipcp }, + { "ipcp", ipcp_cmd }, + { "bind", do_bind }, + { "unbind", do_unbind }, { "register", do_register }, { "unregister", do_unregister }, { "help", do_help }, @@ -78,7 +74,7 @@ int main(int argc, char ** argv) { if (argc < 2) { usage(); - return 0; + return -1; } return do_cmd(argv[1], argc - 1, argv + 1); diff --git a/src/tools/irm/irm_bind.c b/src/tools/irm/irm_bind.c new file mode 100644 index 00000000..85e5bd3d --- /dev/null +++ b/src/tools/irm/irm_bind.c @@ -0,0 +1,81 @@ +/* + * Ouroboros - Copyright (C) 2016 + * + * Bind AP to a name + * + * Sander Vrijders + * + * 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 + +#include + +#include "irm_ops.h" +#include "irm_utils.h" + +static void usage() +{ + printf("Usage: irm bind\n" + " name \n" + " apn \n" + " [auto] (instantiate apn if not running)\n" + " [unique] (there can only be one instantiation)\n" + " [-- ]\n"); +} + + +int do_bind(int argc, char ** argv) +{ + char * name = NULL; + char * ap_name = NULL; + uint16_t flags = 0; + + while (argc > 0) { + if (matches(*argv, "name") == 0) { + name = *(argv + 1); + ++argv; + --argc; + } else if (matches(*argv, "apn") == 0) { + ap_name = *(argv + 1); + ++argv; + --argc; + } else if (strcmp(*argv, "auto") == 0) { + flags |= BIND_AP_AUTO; + } else if (strcmp(*argv, "unique") == 0) { + flags |= BIND_AP_UNIQUE; + } else if (strcmp(*argv, "--") == 0) { + ++argv; + --argc; + break; + } else { + printf("\"%s\" is unknown, try \"irm " + "bind\".\n", *argv); + return -1; + } + + ++argv; + --argc; + } + + if (name == NULL || ap_name == NULL) { + usage(); + return -1; + } + + return irm_bind(name, ap_name, flags, argc, argv); +} diff --git a/src/tools/irm/irm_bootstrap_ipcp.c b/src/tools/irm/irm_bootstrap_ipcp.c deleted file mode 100644 index e11b5f3f..00000000 --- a/src/tools/irm/irm_bootstrap_ipcp.c +++ /dev/null @@ -1,192 +0,0 @@ -/* - * Ouroboros - Copyright (C) 2016 - * - * Bootstrap IPC Processes - * - * Sander Vrijders - * - * 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 -#include -#include - -#include -#include - -#include "irm_ops.h" -#include "irm_utils.h" - -#define NORMAL "normal" -#define SHIM_UDP "shim-udp" -#define SHIM_ETH_LLC "shim-eth-llc" -#define LOCAL "local" - -#define DEFAULT_ADDR_SIZE 4 -#define DEFAULT_CEP_ID_SIZE 2 -#define DEFAULT_PDU_LEN_SIZE 2 -#define DEFAULT_QOS_ID_SIZE 1 -#define DEFAULT_SEQ_NO_SIZE 4 -#define DEFAULT_TTL_SIZE 1 -#define DEFAULT_CHK_SIZE 2 -#define DEFAULT_MIN_PDU_SIZE 0 -#define DEFAULT_MAX_PDU_SIZE 9000 -#define DEFAULT_DDNS 0 - -static void usage() -{ - /* FIXME: Add dif_config stuff */ - printf("Usage: irm bootstrap_ipcp\n" - " ap \n" - " [api ]\n" - " dif \n" - " type [TYPE]\n\n" - "where TYPE = {" NORMAL " " LOCAL " " - SHIM_UDP " " SHIM_ETH_LLC"}\n\n" - "if TYPE == " NORMAL "\n" - " [addr
(default: %d)]\n" - " [cep_id (default: %d)]\n" - " [pdu_len (default: %d)]\n" - " [qos_id (default: %d)]\n" - " [seqno (default: %d)]\n" - " [ttl