diff options
author | dimitri staessens <[email protected]> | 2016-06-28 17:02:42 +0200 |
---|---|---|
committer | dimitri staessens <[email protected]> | 2016-06-28 17:02:42 +0200 |
commit | a19521aa74edfa4a2eb62466b04c9264c3b6576c (patch) | |
tree | 36b94730d8690c2d131f39b3cc3ff715674f9fcc /src | |
parent | acd29da104d0d8ddace2b2693314542bb5a56fcc (diff) | |
parent | 99d19307fae8f1370f52a62aee88fded624ad464 (diff) | |
download | ouroboros-a19521aa74edfa4a2eb62466b04c9264c3b6576c.tar.gz ouroboros-a19521aa74edfa4a2eb62466b04c9264c3b6576c.zip |
Merged in sandervrijders/ouroboros/be (pull request #144)
lib, irmd, ipcpd: Change of IRM API
Diffstat (limited to 'src')
30 files changed, 916 insertions, 951 deletions
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 <ouroboros/common.h> -#include <ouroboros/instance_name.h> #include <ouroboros/list.h> #include <sys/types.h> #include <pthread.h> diff --git a/src/ipcpd/ipcp-ops.h b/src/ipcpd/ipcp-ops.h index a1d9f23f..40c331ec 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 <ouroboros/dif_config.h> +#include <ouroboros/irm_config.h> #include <ouroboros/common.h> #include <sys/types.h> @@ -38,12 +38,12 @@ struct ipcp_ops { size_t len); int (* ipcp_name_reg)(char * name); int (* ipcp_name_unreg)(char * name); - int (* ipcp_flow_alloc)(pid_t n_pid, + int (* ipcp_flow_alloc)(pid_t n_api, int port_id, char * dst_ap_name, char * src_ae_name, enum qos_cube qos); - int (* ipcp_flow_alloc_resp)(pid_t n_pid, + int (* ipcp_flow_alloc_resp)(pid_t n_api, int port_id, int response); int (* ipcp_flow_dealloc)(int port_id); diff --git a/src/ipcpd/ipcp.c b/src/ipcpd/ipcp.c index b5108712..4acbffa2 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 */ + /* argument 1: api of irmd */ if (atoi(argv[1]) == 0) return -1; - /* name conformity responsibility of NMS */ - - /* argument 2: ap name */ - return 0; } @@ -207,7 +203,7 @@ void * ipcp_main_loop(void * o) } ret_msg.has_result = true; ret_msg.result = - _ipcp->ops->ipcp_flow_alloc(msg->pid, + _ipcp->ops->ipcp_flow_alloc(msg->api, msg->port_id, msg->dst_name, msg->src_ae_name, @@ -220,7 +216,7 @@ void * ipcp_main_loop(void * o) } ret_msg.has_result = true; ret_msg.result = - _ipcp->ops->ipcp_flow_alloc_resp(msg->pid, + _ipcp->ops->ipcp_flow_alloc_resp(msg->api, msg->port_id, msg->result); break; diff --git a/src/ipcpd/local/main.c b/src/ipcpd/local/main.c index 06e2b0a4..2120e4e8 100644 --- a/src/ipcpd/local/main.c +++ b/src/ipcpd/local/main.c @@ -28,7 +28,7 @@ #include <ouroboros/list.h> #include <ouroboros/utils.h> #include <ouroboros/ipcp.h> -#include <ouroboros/dif_config.h> +#include <ouroboros/irm_config.h> #include <ouroboros/sockets.h> #include <ouroboros/bitmap.h> #include <ouroboros/common.h> @@ -50,7 +50,7 @@ #define shim_data(type) ((struct ipcp_local_data *) type->data) /* global for trapping signal */ -int irmd_pid; +int irmd_api; /* this IPCP's data */ #ifdef MAKE_CHECK @@ -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) @@ -243,7 +226,7 @@ void ipcp_sig_handler(int sig, siginfo_t * info, void * c) case SIGTERM: case SIGHUP: case SIGQUIT: - if (info->si_pid == irmd_pid) { + if (info->si_pid == irmd_api) { bool clean_threads = false; LOG_DBG("Terminating by order of %d. Bye.", info->si_pid); @@ -293,7 +276,7 @@ static int ipcp_local_bootstrap(struct dif_config * conf) pthread_rwlock_unlock(&_ipcp->state_lock); - LOG_DBG("Bootstrapped local IPCP with pid %d.", + LOG_DBG("Bootstrapped local IPCP with api %d.", getpid()); return 0; @@ -337,7 +320,7 @@ static int ipcp_local_name_unreg(char * name) return 0; } -static int ipcp_local_flow_alloc(pid_t n_pid, +static int ipcp_local_flow_alloc(pid_t n_api, int port_id, char * dst_name, char * src_ae_name, @@ -363,7 +346,7 @@ static int ipcp_local_flow_alloc(pid_t n_pid, return -1; /* -ENOTENROLLED */ } - rb = shm_ap_rbuff_open(n_pid); + rb = shm_ap_rbuff_open(n_api); if (rb == NULL) { pthread_rwlock_unlock(&_ipcp->state_lock); return -1; /* -ENORBUFF */ @@ -393,13 +376,13 @@ static int ipcp_local_flow_alloc(pid_t n_pid, pthread_rwlock_unlock(&_ap_instance->flows_lock); pthread_rwlock_unlock(&_ipcp->state_lock); LOG_ERR("Could not get port id from IRMd"); - /* shm_ap_rbuff_close(n_pid); */ + /* shm_ap_rbuff_close(n_api); */ return -1; } out_fd = bmp_allocate(_ap_instance->fds); if (!bmp_is_id_valid(_ap_instance->fds, out_fd)) { - /* shm_ap_rbuff_close(n_pid); */ + /* shm_ap_rbuff_close(n_api); */ pthread_rwlock_unlock(&_ap_instance->flows_lock); pthread_rwlock_unlock(&_ipcp->state_lock); return -1; /* -ENOMOREFDS */ @@ -420,7 +403,7 @@ static int ipcp_local_flow_alloc(pid_t n_pid, return 0; } -static int ipcp_local_flow_alloc_resp(pid_t n_pid, +static int ipcp_local_flow_alloc_resp(pid_t n_api, int port_id, int response) { @@ -453,7 +436,7 @@ static int ipcp_local_flow_alloc_resp(pid_t n_pid, return -1; } - rb = shm_ap_rbuff_open(n_pid); + rb = shm_ap_rbuff_open(n_api); if (rb == NULL) { LOG_ERR("Could not open N + 1 ringbuffer."); _ap_instance->flows[in_fd].state = FLOW_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,11 +588,11 @@ 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 */ - irmd_pid = atoi(argv[1]); + irmd_api = atoi(argv[1]); /* init sig_act */ memset(&sig_act, 0, sizeof(sig_act)); diff --git a/src/ipcpd/shim-eth-llc/main.c b/src/ipcpd/shim-eth-llc/main.c index 9bd6920b..48b6391f 100644 --- a/src/ipcpd/shim-eth-llc/main.c +++ b/src/ipcpd/shim-eth-llc/main.c @@ -31,7 +31,7 @@ #include <ouroboros/list.h> #include <ouroboros/utils.h> #include <ouroboros/ipcp.h> -#include <ouroboros/dif_config.h> +#include <ouroboros/irm_config.h> #include <ouroboros/sockets.h> #include <ouroboros/bitmap.h> #include <ouroboros/flow.h> @@ -70,7 +70,7 @@ typedef ShimEthLlcMsg shim_eth_llc_msg_t; #define MAX_SAPS 64 /* global for trapping signal */ -int irmd_pid; +int irmd_api; struct ipcp * _ipcp; @@ -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); @@ -818,7 +816,7 @@ void ipcp_sig_handler(int sig, siginfo_t * info, void * c) case SIGINT: case SIGTERM: case SIGHUP: - if (info->si_pid == irmd_pid) { + if (info->si_pid == irmd_api) { bool clean_threads = false; LOG_DBG("Terminating by order of %d. Bye.", info->si_pid); @@ -990,7 +988,7 @@ static int eth_llc_ipcp_bootstrap(struct dif_config * conf) pthread_rwlock_unlock(&_ipcp->state_lock); - LOG_DBG("Bootstrapped shim IPCP over Ethernet with LLC with pid %d.", + LOG_DBG("Bootstrapped shim IPCP over Ethernet with LLC with api %d.", getpid()); return 0; @@ -1030,7 +1028,7 @@ static int eth_llc_ipcp_name_unreg(char * name) return 0; } -static int eth_llc_ipcp_flow_alloc(pid_t n_pid, +static int eth_llc_ipcp_flow_alloc(pid_t n_api, int port_id, char * dst_name, char * src_ae_name, @@ -1049,7 +1047,7 @@ static int eth_llc_ipcp_flow_alloc(pid_t n_pid, if (qos != QOS_CUBE_BE) LOG_DBGF("QoS requested. Ethernet LLC can't do that. For now."); - rb = shm_ap_rbuff_open(n_pid); + rb = shm_ap_rbuff_open(n_api); if (rb == NULL) return -1; /* -ENORBUFF */ @@ -1108,7 +1106,7 @@ static int eth_llc_ipcp_flow_alloc(pid_t n_pid, return index; } -static int eth_llc_ipcp_flow_alloc_resp(pid_t n_pid, +static int eth_llc_ipcp_flow_alloc_resp(pid_t n_api, int port_id, int response) { @@ -1134,7 +1132,7 @@ static int eth_llc_ipcp_flow_alloc_resp(pid_t n_pid, return -1; } - rb = shm_ap_rbuff_open(n_pid); + rb = shm_ap_rbuff_open(n_api); if (rb == NULL) { LOG_ERR("Could not open N + 1 ringbuffer."); ipcp_flow(index)->state = FLOW_NULL; @@ -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; @@ -1254,7 +1250,7 @@ int main(int argc, char * argv[]) } /* store the process id of the irmd */ - irmd_pid = atoi(argv[1]); + irmd_api = atoi(argv[1]); /* init sig_act */ memset(&sig_act, 0, sizeof(sig_act)); diff --git a/src/ipcpd/shim-udp/main.c b/src/ipcpd/shim-udp/main.c index 221e60d2..a28c262f 100644 --- a/src/ipcpd/shim-udp/main.c +++ b/src/ipcpd/shim-udp/main.c @@ -29,7 +29,7 @@ #include <ouroboros/list.h> #include <ouroboros/utils.h> #include <ouroboros/ipcp.h> -#include <ouroboros/dif_config.h> +#include <ouroboros/irm_config.h> #include <ouroboros/sockets.h> #include <ouroboros/bitmap.h> #include <ouroboros/flow.h> @@ -69,7 +69,7 @@ typedef ShimUdpMsg shim_udp_msg_t; _ipcp->data)->s_saddr.sin_addr.s_addr) /* global for trapping signal */ -int irmd_pid; +int irmd_api; /* this IPCP's data */ #ifdef MAKE_CHECK @@ -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) @@ -834,7 +817,7 @@ void ipcp_sig_handler(int sig, siginfo_t * info, void * c) case SIGINT: case SIGTERM: case SIGHUP: - if (info->si_pid == irmd_pid) { + if (info->si_pid == irmd_api) { bool clean_threads = false; LOG_DBG("Terminating by order of %d. Bye.", info->si_pid); @@ -964,7 +947,7 @@ static int ipcp_udp_bootstrap(struct dif_config * conf) pthread_rwlock_unlock(&_ipcp->state_lock); - LOG_DBG("Bootstrapped shim IPCP over UDP with pid %d.", + LOG_DBG("Bootstrapped shim IPCP over UDP with api %d.", getpid()); LOG_DBG("Bound to IP address %s.", ipstr); @@ -978,7 +961,7 @@ static int ipcp_udp_bootstrap(struct dif_config * conf) /* NOTE: Disgusted with this crap */ static int ddns_send(char * cmd) { - pid_t pid = 0; + pid_t api = 0; int wstatus; int pipe_fd[2]; char * argv[] = {NSUPDATE_EXEC, 0}; @@ -989,13 +972,13 @@ static int ddns_send(char * cmd) return -1; } - pid = fork(); - if (pid == -1) { + api = fork(); + if (api == -1) { LOG_ERR("Failed to fork."); return -1; } - if (pid == 0) { + if (api == 0) { close(pipe_fd[1]); dup2(pipe_fd[0], 0); execve(argv[0], &argv[0], envp); @@ -1009,7 +992,7 @@ static int ddns_send(char * cmd) return -1; } - waitpid(pid, &wstatus, 0); + waitpid(api, &wstatus, 0); if (WIFEXITED(wstatus) == true && WEXITSTATUS(wstatus) == 0) LOG_DBG("Succesfully communicated with DNS server."); @@ -1022,7 +1005,7 @@ static int ddns_send(char * cmd) static uint32_t ddns_resolve(char * name, uint32_t dns_addr) { - pid_t pid = 0; + pid_t api = 0; int wstatus; int pipe_fd[2]; char dnsstr[INET_ADDRSTRLEN]; @@ -1043,13 +1026,13 @@ static uint32_t ddns_resolve(char * name, uint32_t dns_addr) return 0; } - pid = fork(); - if (pid == -1) { + api = fork(); + if (api == -1) { LOG_ERR("Failed to fork."); return 0; } - if (pid == 0) { + if (api == 0) { char * argv[] = {NSLOOKUP_EXEC, name, dnsstr, 0}; char * envp[] = {0}; @@ -1069,7 +1052,7 @@ static uint32_t ddns_resolve(char * name, uint32_t dns_addr) close(pipe_fd[0]); - waitpid(pid, &wstatus, 0); + waitpid(api, &wstatus, 0); if (WIFEXITED(wstatus) == true && WEXITSTATUS(wstatus) == 0) LOG_DBG("Succesfully communicated with nslookup."); @@ -1215,7 +1198,7 @@ static int ipcp_udp_name_unreg(char * name) return 0; } -static int ipcp_udp_flow_alloc(pid_t n_pid, +static int ipcp_udp_flow_alloc(pid_t n_api, int port_id, char * dst_name, char * src_ae_name, @@ -1245,7 +1228,7 @@ static int ipcp_udp_flow_alloc(pid_t n_pid, if (qos != QOS_CUBE_BE) LOG_DBG("QoS requested. UDP/IP can't do that."); - rb = shm_ap_rbuff_open(n_pid); + rb = shm_ap_rbuff_open(n_api); if (rb == NULL) return -1; /* -ENORBUFF */ @@ -1365,7 +1348,7 @@ static int ipcp_udp_flow_alloc(pid_t n_pid, return fd; } -static int ipcp_udp_flow_alloc_resp(pid_t n_pid, +static int ipcp_udp_flow_alloc_resp(pid_t n_api, int port_id, int response) { @@ -1406,7 +1389,7 @@ static int ipcp_udp_flow_alloc_resp(pid_t n_pid, return -1; } - rb = shm_ap_rbuff_open(n_pid); + rb = shm_ap_rbuff_open(n_api); if (rb == NULL) { LOG_ERR("Could not open N + 1 ringbuffer."); _ap_instance->flows[fd].state = FLOW_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,11 +1572,11 @@ 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 */ - irmd_pid = atoi(argv[1]); + irmd_api = atoi(argv[1]); /* init sig_act */ memset(&sig_act, 0, sizeof(sig_act)); diff --git a/src/ipcpd/shim-udp/tests/shim_udp_test.c b/src/ipcpd/shim-udp/tests/shim_udp_test.c index e5e8b32d..b222ac42 100644 --- a/src/ipcpd/shim-udp/tests/shim_udp_test.c +++ b/src/ipcpd/shim-udp/tests/shim_udp_test.c @@ -34,9 +34,6 @@ struct ipcp * _ipcp; int shim_udp_test(int argc, char ** argv) { - /* argument 1: pid of irmd ? */ - /* argument 2: ap name */ - /* argument 3: instance id */ struct shm_du_map * dum; char * ipcp_name = "test-shim-ipcp"; int i = 0; diff --git a/src/irmd/main.c b/src/irmd/main.c index 12fc4bb1..f6c27377 100644 --- a/src/irmd/main.c +++ b/src/irmd/main.c @@ -25,13 +25,11 @@ #include <ouroboros/config.h> #include <ouroboros/logs.h> #include <ouroboros/sockets.h> -#include <ouroboros/irm.h> #include <ouroboros/ipcp.h> #include <ouroboros/nsm.h> #include <ouroboros/list.h> -#include <ouroboros/instance_name.h> #include <ouroboros/utils.h> -#include <ouroboros/dif_config.h> +#include <ouroboros/irm_config.h> #include <ouroboros/shm_du_map.h> #include <ouroboros/bitmap.h> #include <ouroboros/flow.h> @@ -50,6 +48,8 @@ #include <pthread.h> #include <sys/stat.h> +#define API_INVALID 0 + #define IRMD_MAX_FLOWS 4096 #define IRMD_THREADPOOL_SIZE 3 @@ -58,17 +58,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; }; @@ -95,22 +92,22 @@ enum reg_i_state { struct reg_instance { struct list_head next; - pid_t pid; + pid_t api; - /* the pid will block on this */ + /* the api will block on this */ enum reg_i_state state; pthread_cond_t wakeup; pthread_mutex_t mutex; }; -static struct reg_instance * reg_instance_create(pid_t pid) +static struct reg_instance * reg_instance_create(pid_t api) { struct reg_instance * i; i = malloc(sizeof(*i)); if (i == NULL) return NULL; - i->pid = pid; + i->api = api; i->state = REG_I_WAKE; pthread_mutex_init(&i->mutex, NULL); @@ -222,8 +219,8 @@ struct port_map_entry { int port_id; - pid_t n_pid; - pid_t n_1_pid; + pid_t n_api; + pid_t n_1_api; pthread_cond_t res_signal; pthread_mutex_t res_lock; @@ -242,7 +239,7 @@ struct irm { /* keep track of all flows in this processing system */ struct bmp * port_ids; - /* maps port_ids to pid pair */ + /* maps port_ids to api pair */ struct list_head port_map; pthread_rwlock_t flows_lock; @@ -262,8 +259,8 @@ static struct port_map_entry * port_map_entry_create() if (e == NULL) return NULL; - e->n_pid = 0; - e->n_1_pid = 0; + e->n_api = 0; + e->n_1_api = 0; e->port_id = 0; e->state = FLOW_NULL; @@ -321,7 +318,7 @@ static struct port_map_entry * get_port_map_entry(int port_id) return NULL; } -static struct port_map_entry * get_port_map_entry_n(pid_t n_pid) +static struct port_map_entry * get_port_map_entry_n(pid_t n_api) { struct list_head * pos = NULL; @@ -329,7 +326,7 @@ static struct port_map_entry * get_port_map_entry_n(pid_t n_pid) struct port_map_entry * e = list_entry(pos, struct port_map_entry, next); - if (e->n_pid == n_pid) + if (e->n_api == n_api) return e; } @@ -342,7 +339,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 +352,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 +361,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_api(pid_t api) { struct list_head * pos = NULL; @@ -372,34 +369,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 +401,7 @@ static instance_name_t * get_ipcp_by_dst_name(char * dst_name, } } - return NULL; + return 0; } static struct reg_entry * reg_entry_create() @@ -559,7 +541,7 @@ static struct reg_ap_name * reg_entry_get_ap_name(struct reg_entry * e, } static struct reg_instance * reg_entry_get_reg_instance(struct reg_entry * e, - pid_t pid) + pid_t api) { struct list_head * pos = NULL; @@ -567,7 +549,7 @@ static struct reg_instance * reg_entry_get_reg_instance(struct reg_entry * e, struct reg_instance * r = list_entry(pos, struct reg_instance, next); - if (r->pid == pid) + if (r->api == api) return r; } @@ -626,7 +608,7 @@ static struct reg_entry * get_reg_entry_by_ap_name(char * ap_name) return NULL; } -static struct reg_entry * get_reg_entry_by_ap_id(pid_t pid) +static struct reg_entry * get_reg_entry_by_ap_id(pid_t api) { struct list_head * pos = NULL; @@ -639,7 +621,7 @@ static struct reg_entry * get_reg_entry_by_ap_id(pid_t pid) struct reg_instance * r = list_entry(p, struct reg_instance, next); - if (r->pid == pid) + if (r->api == api) return e; } } @@ -647,7 +629,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 +676,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; } @@ -767,12 +749,12 @@ static int registry_remove_ap_auto(char * name, #endif static struct reg_instance * registry_add_ap_instance(char * name, - pid_t pid) + pid_t api) { struct reg_entry * e = NULL; struct reg_instance * i = NULL; - if (name == NULL || pid == 0) + if (name == NULL || api == 0) return NULL; e = get_reg_entry_by_name(name); @@ -781,12 +763,12 @@ static struct reg_instance * registry_add_ap_instance(char * name, return NULL; } - if (pid == 0) { - LOG_DBG("Invalid pid."); + if (api == API_INVALID) { + LOG_DBG("Invalid api."); return NULL; } - if (reg_entry_has_api(e, pid)) { + if (reg_entry_has_api(e, api)) { LOG_DBG("Instance already registered with this name."); return NULL; } @@ -796,7 +778,7 @@ static struct reg_instance * registry_add_ap_instance(char * name, return NULL; } - i = reg_instance_create(pid); + i = reg_instance_create(api); if (i == NULL) { LOG_DBG("Failed to create reg_instance"); return NULL; @@ -812,12 +794,12 @@ static struct reg_instance * registry_add_ap_instance(char * name, return i; } -static int registry_remove_ap_instance(char * name, pid_t pid) +static int registry_remove_ap_instance(char * name, pid_t api) { struct reg_entry * e = NULL; struct reg_instance * i = NULL; - if (name == NULL || pid == 0) + if (name == NULL || api == 0) return -1; e = get_reg_entry_by_name(name); @@ -826,10 +808,10 @@ static int registry_remove_ap_instance(char * name, pid_t pid) return -1; } - i = reg_entry_get_reg_instance(e, pid); + i = reg_entry_get_reg_instance(e, api); if (i == NULL) { LOG_DBG("Instance %d is not accepting flows for %s.", - pid, name); + api, name); return -1; } @@ -838,7 +820,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 @@ -858,7 +840,7 @@ static pid_t registry_resolve_api(struct reg_entry * e) list_for_each(pos, &e->ap_instances) { struct reg_instance * r = list_entry(pos, struct reg_instance, next); - return r->pid; + return r->api; } return 0; @@ -890,10 +872,10 @@ 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; + pid_t api; struct ipcp_entry * tmp = NULL; pthread_rwlock_rdlock(&instance->state_lock); @@ -903,8 +885,8 @@ static pid_t create_ipcp(char * ap_name, return 0; } - pid = ipcp_create(ap_name, ipcp_type); - if (pid == -1) { + api = ipcp_create(ipcp_type); + if (api == -1) { pthread_rwlock_unlock(&instance->state_lock); LOG_ERR("Failed to create IPCP."); return -1; @@ -918,15 +900,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 = api; + tmp->name = strdup(name); + if (tmp->name == NULL) { ipcp_entry_destroy(tmp); pthread_rwlock_unlock(&instance->state_lock); return -1; @@ -941,56 +917,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.", api); - return pid; + return api; } -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 +964,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_api(api); if (entry == NULL) { pthread_rwlock_unlock(&instance->reg_lock); pthread_rwlock_unlock(&instance->state_lock); @@ -1030,7 +980,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 +992,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 +1014,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_api(api); if (entry == NULL) { pthread_rwlock_unlock(&instance->reg_lock); pthread_rwlock_unlock(&instance->state_lock); @@ -1090,7 +1040,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 +1052,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; +} + +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; } -/* FIXME: distinction between registering names and associating instances */ +static ssize_t list_ipcps(char * name, + pid_t ** apis) +{ + 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++; + } + } + + *apis = malloc(count * sizeof(pid_t)); + if (*apis == 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) { + (*apis)[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 +1200,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 +1217,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 +1236,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 +1243,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 +1263,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 +1272,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,15 +1283,13 @@ static int ap_unreg(char * name, } } - registry_del_name(rne->name); - pthread_rwlock_unlock(&instance->reg_lock); pthread_rwlock_unlock(&instance->state_lock); return ret; } -static struct port_map_entry * flow_accept(pid_t pid, +static struct port_map_entry * flow_accept(pid_t api, char * srv_ap_name, char ** dst_ae_name) { @@ -1284,16 +1314,16 @@ static struct port_map_entry * flow_accept(pid_t pid, return NULL; } - if (!reg_entry_has_api(rne, pid)) { - rgi = registry_add_ap_instance(rne->name, pid); + if (!reg_entry_has_api(rne, api)) { + rgi = registry_add_ap_instance(rne->name, api); if (rgi == NULL) { pthread_rwlock_unlock(&instance->reg_lock); pthread_rwlock_unlock(&instance->state_lock); LOG_ERR("Failed to register instance %d with %s.", - pid,srv_ap_name); + api,srv_ap_name); return NULL; } - LOG_INFO("New instance (%d) of %s added.", pid, srv_ap_name); + LOG_INFO("New instance (%d) of %s added.", api, srv_ap_name); } pthread_rwlock_unlock(&instance->reg_lock); @@ -1317,7 +1347,7 @@ static struct port_map_entry * flow_accept(pid_t pid, pthread_rwlock_rdlock(&instance->flows_lock); - pme = get_port_map_entry_n(pid); + pme = get_port_map_entry_n(api); if (pme == NULL) { pthread_rwlock_unlock(&instance->flows_lock); pthread_rwlock_unlock(&instance->state_lock); @@ -1336,7 +1366,7 @@ static struct port_map_entry * flow_accept(pid_t pid, return pme; } -static int flow_alloc_resp(pid_t n_pid, +static int flow_alloc_resp(pid_t n_api, int port_id, int response) { @@ -1353,7 +1383,7 @@ static int flow_alloc_resp(pid_t n_pid, pthread_rwlock_wrlock(&instance->reg_lock); - rne = get_reg_entry_by_ap_id(n_pid); + rne = get_reg_entry_by_ap_id(n_api); if (rne == NULL) { pthread_rwlock_unlock(&instance->reg_lock); pthread_rwlock_unlock(&instance->state_lock); @@ -1369,7 +1399,7 @@ static int flow_alloc_resp(pid_t n_pid, pthread_mutex_lock(&rne->state_lock); - registry_remove_ap_instance(rne->name, n_pid); + registry_remove_ap_instance(rne->name, n_api); pthread_mutex_unlock(&rne->state_lock); @@ -1388,9 +1418,9 @@ static int flow_alloc_resp(pid_t n_pid, pme->state = FLOW_ALLOCATED; pthread_rwlock_unlock(&instance->flows_lock); - ret = ipcp_flow_alloc_resp(pme->n_1_pid, + ret = ipcp_flow_alloc_resp(pme->n_1_api, port_id, - pme->n_pid, + pme->n_api, response); } @@ -1399,13 +1429,13 @@ static int flow_alloc_resp(pid_t n_pid, return ret; } -static struct port_map_entry * flow_alloc(pid_t pid, +static struct port_map_entry * flow_alloc(pid_t api, char * dst_name, char * src_ae_name, 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 */ @@ -1424,7 +1454,7 @@ static struct port_map_entry * flow_alloc(pid_t pid, return NULL; } - pme->n_pid = pid; + pme->n_api = api; pme->state = FLOW_PENDING; if (clock_gettime(CLOCK_MONOTONIC, &pme->t0) < 0) LOG_WARN("Failed to set timestamp."); @@ -1435,7 +1465,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,16 +1476,16 @@ 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_api = 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, + pme->n_api, dst_name, src_ae_name, QOS_CUBE_BE) < 0) { @@ -1550,7 +1580,7 @@ static int flow_alloc_res(int port_id) static int flow_dealloc(int port_id) { - pid_t n_1_pid; + pid_t n_1_api; int ret = 0; struct port_map_entry * e = NULL; @@ -1566,13 +1596,13 @@ static int flow_dealloc(int port_id) return 0; } - n_1_pid = e->n_1_pid; + n_1_api = e->n_1_api; list_del(&e->next); pthread_rwlock_unlock(&instance->flows_lock); - ret = ipcp_flow_dealloc(n_1_pid, port_id); + ret = ipcp_flow_dealloc(n_1_api, port_id); pthread_rwlock_unlock(&instance->state_lock); @@ -1583,16 +1613,16 @@ static int flow_dealloc(int port_id) static int auto_execute(char ** argv) { - pid_t pid; + pid_t api; LOG_INFO("Executing %s.", argv[0]); - pid = fork(); - if (pid == -1) { + api = fork(); + if (api == -1) { LOG_ERR("Failed to fork"); - return pid; + return api; } - if (pid != 0) { - return pid; + if (api != 0) { + return api; } execv(argv[0], argv); @@ -1602,7 +1632,7 @@ static int auto_execute(char ** argv) exit(EXIT_FAILURE); } -static struct port_map_entry * flow_req_arr(pid_t pid, +static struct port_map_entry * flow_req_arr(pid_t api, char * dst_name, char * ae_name) { @@ -1618,7 +1648,7 @@ static struct port_map_entry * flow_req_arr(pid_t pid, } pme->state = FLOW_PENDING; - pme->n_1_pid = pid; + pme->n_1_api = api; if (clock_gettime(CLOCK_MONOTONIC, &pme->t0) < 0) LOG_WARN("Failed to set timestamp."); @@ -1668,11 +1698,11 @@ static struct port_map_entry * flow_req_arr(pid_t pid, case REG_NAME_FLOW_ACCEPT: pthread_mutex_unlock(&rne->state_lock); - pme->n_pid = registry_resolve_api(rne); - if(pme->n_pid == 0) { + pme->n_api = registry_resolve_api(rne); + if(pme->n_api == 0) { pthread_rwlock_unlock(&instance->reg_lock); pthread_rwlock_unlock(&instance->state_lock); - LOG_ERR("Invalid pid returned."); + LOG_ERR("Invalid api returned."); return NULL; } @@ -1700,7 +1730,7 @@ static struct port_map_entry * flow_req_arr(pid_t pid, rne->state = REG_NAME_FLOW_ARRIVED; - reg_instance_wake(reg_entry_get_reg_instance(rne, pme->n_pid)); + reg_instance_wake(reg_entry_get_reg_instance(rne, pme->n_api)); pthread_mutex_unlock(&rne->state_lock); @@ -1792,7 +1822,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); } @@ -1905,19 +1935,19 @@ void * irm_flow_cleaner() pthread_mutex_unlock(&e->res_lock); - if (kill(e->n_pid, 0) < 0) { + if (kill(e->n_api, 0) < 0) { bmp_release(instance->port_ids, e->port_id); list_del(&e->next); LOG_INFO("Process %d gone, %d deallocated.", - e->n_pid, e->port_id); - ipcp_flow_dealloc(e->n_1_pid, e->port_id); + e->n_api, e->port_id); + ipcp_flow_dealloc(e->n_1_api, e->port_id); port_map_entry_destroy(e); } - if (kill(e->n_1_pid, 0) < 0) { + if (kill(e->n_1_api, 0) < 0) { list_del(&e->next); LOG_ERR("IPCP %d gone, flow %d removed.", - e->n_1_pid, e->port_id); + e->n_1_api, e->port_id); port_map_entry_destroy(e); } } @@ -1934,12 +1964,12 @@ void * irm_flow_cleaner() list_entry(pos2, struct reg_instance, next); - if (kill(r->pid, 0) < 0) { + if (kill(r->api, 0) < 0) { LOG_INFO("Process %d gone, " "instance deleted.", - r->pid); + r->api); registry_remove_ap_instance(e->name, - r->pid); + r->api); } } } @@ -1964,10 +1994,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 * apis = NULL; ret_msg.code = IRM_MSG_CODE__IRM_REPLY; @@ -1992,51 +2022,60 @@ 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_apis = list_ipcps(msg->dst_name, + &apis); + ret_msg.apis = apis; + 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, + e = flow_accept(msg->api, msg->ap_name, &ret_msg.ae_name); @@ -2045,17 +2084,17 @@ void * mainloop() ret_msg.has_port_id = true; ret_msg.port_id = e->port_id; - ret_msg.has_pid = true; - ret_msg.pid = e->n_1_pid; + ret_msg.has_api = true; + ret_msg.api = e->n_1_api; break; case IRM_MSG_CODE__IRM_FLOW_ALLOC_RESP: ret_msg.has_result = true; - ret_msg.result = flow_alloc_resp(msg->pid, + ret_msg.result = flow_alloc_resp(msg->api, msg->port_id, msg->response); break; case IRM_MSG_CODE__IRM_FLOW_ALLOC: - e = flow_alloc(msg->pid, + e = flow_alloc(msg->api, msg->dst_name, msg->ae_name, NULL); @@ -2064,8 +2103,8 @@ void * mainloop() ret_msg.has_port_id = true; ret_msg.port_id = e->port_id; - ret_msg.has_pid = true; - ret_msg.pid = e->n_1_pid; + ret_msg.has_api = true; + ret_msg.api = e->n_1_api; break; case IRM_MSG_CODE__IRM_FLOW_ALLOC_RES: ret_msg.has_result = true; @@ -2076,7 +2115,7 @@ void * mainloop() ret_msg.result = flow_dealloc(msg->port_id); break; case IRM_MSG_CODE__IPCP_FLOW_REQ_ARR: - e = flow_req_arr(msg->pid, + e = flow_req_arr(msg->api, msg->dst_name, msg->ae_name); if (e == NULL) @@ -2084,8 +2123,8 @@ void * mainloop() ret_msg.has_port_id = true; ret_msg.port_id = e->port_id; - ret_msg.has_pid = true; - ret_msg.pid = e->n_pid; + ret_msg.has_api = true; + ret_msg.api = e->n_api; break; case IRM_MSG_CODE__IPCP_FLOW_ALLOC_REPLY: ret_msg.has_result = true; @@ -2106,12 +2145,16 @@ void * mainloop() buffer.size = irm_msg__get_packed_size(&ret_msg); if (buffer.size == 0) { LOG_ERR("Failed to send reply message."); + if (apis != NULL) + free(apis); close(cli_sockfd); continue; } buffer.data = malloc(buffer.size); if (buffer.data == NULL) { + if (apis != NULL) + free(apis); close(cli_sockfd); continue; } @@ -2120,10 +2163,15 @@ void * mainloop() if (write(cli_sockfd, buffer.data, buffer.size) == -1) { free(buffer.data); + if (apis != NULL) + free(apis); close(cli_sockfd); continue; } + if (apis != NULL) + free(apis); + 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..ac995b2d 100644 --- a/src/lib/dev.c +++ b/src/lib/dev.c @@ -28,7 +28,6 @@ #include <ouroboros/dev.h> #include <ouroboros/sockets.h> #include <ouroboros/bitmap.h> -#include <ouroboros/instance_name.h> #include <ouroboros/shm_du_map.h> #include <ouroboros/shm_ap_rbuff.h> #include <ouroboros/utils.h> @@ -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) @@ -164,12 +148,12 @@ int flow_accept(char ** ae_name) int cfd = -1; msg.code = IRM_MSG_CODE__IRM_FLOW_ACCEPT; - msg.has_pid = true; + msg.has_api = true; 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.api = _ap_instance->api; pthread_rwlock_unlock(&_ap_instance->data_lock); @@ -178,7 +162,7 @@ int flow_accept(char ** ae_name) return -1; } - if (!recv_msg->has_pid || !recv_msg->has_port_id) { + if (!recv_msg->has_api || !recv_msg->has_port_id) { irm_msg__free_unpacked(recv_msg, NULL); return -1; } @@ -194,7 +178,7 @@ int flow_accept(char ** ae_name) return -1; } - _ap_instance->flows[cfd].rb = shm_ap_rbuff_open(recv_msg->pid); + _ap_instance->flows[cfd].rb = shm_ap_rbuff_open(recv_msg->api); if (_ap_instance->flows[cfd].rb == NULL) { bmp_release(_ap_instance->fds, cfd); pthread_rwlock_unlock(&_ap_instance->flows_lock); @@ -237,8 +221,8 @@ int flow_alloc_resp(int fd, return -EBADF; msg.code = IRM_MSG_CODE__IRM_FLOW_ALLOC_RESP; - msg.has_pid = true; - msg.pid = _ap_instance->api->id; + msg.has_api = true; + msg.api = _ap_instance->api; msg.has_port_id = true; pthread_rwlock_rdlock(&_ap_instance->data_lock); @@ -295,11 +279,11 @@ int flow_alloc(char * dst_name, msg.code = IRM_MSG_CODE__IRM_FLOW_ALLOC; msg.dst_name = dst_name; msg.ae_name = src_ae_name; - msg.has_pid = true; + msg.has_api = true; pthread_rwlock_rdlock(&_ap_instance->data_lock); - msg.pid = _ap_instance->api->id; + msg.api = _ap_instance->api; pthread_rwlock_unlock(&_ap_instance->data_lock); @@ -308,7 +292,7 @@ int flow_alloc(char * dst_name, return -1; } - if (!recv_msg->has_pid || !recv_msg->has_port_id) { + if (!recv_msg->has_api || !recv_msg->has_port_id) { irm_msg__free_unpacked(recv_msg, NULL); return -1; } @@ -324,7 +308,7 @@ int flow_alloc(char * dst_name, return -1; } - _ap_instance->flows[fd].rb = shm_ap_rbuff_open(recv_msg->pid); + _ap_instance->flows[fd].rb = shm_ap_rbuff_open(recv_msg->api); if (_ap_instance->flows[fd].rb == NULL) { bmp_release(_ap_instance->fds, fd); pthread_rwlock_unlock(&_ap_instance->flows_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 <[email protected]> - * Francesco Salvestrini <[email protected]> - * - * 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 <ouroboros/logs.h> -#include <ouroboros/common.h> -#include <ouroboros/instance_name.h> -#include <ouroboros/utils.h> - -#include <string.h> -#include <math.h> -#include <stdlib.h> - -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..2b6b6825 100644 --- a/src/lib/ipcp.c +++ b/src/lib/ipcp.c @@ -35,7 +35,7 @@ #include <sys/types.h> #include <sys/wait.h> -static ipcp_msg_t * send_recv_ipcp_msg(pid_t pid, +static ipcp_msg_t * send_recv_ipcp_msg(pid_t api, ipcp_msg_t * msg) { int sockfd = 0; @@ -44,7 +44,7 @@ static ipcp_msg_t * send_recv_ipcp_msg(pid_t pid, ssize_t count = 0; ipcp_msg_t * recv_msg = NULL; - sock_path = ipcp_sock_path(pid); + sock_path = ipcp_sock_path(api); if (sock_path == NULL) return NULL; @@ -99,26 +99,25 @@ 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]; + pid_t api = 0; + char irmd_api[10]; size_t len = 0; char * ipcp_dir = "/sbin/"; char * full_name = NULL; char * exec_name = NULL; - sprintf(irmd_pid, "%u", getpid()); + sprintf(irmd_api, "%u", getpid()); - pid = fork(); - if (pid == -1) { + api = fork(); + if (api == -1) { LOG_ERR("Failed to fork"); - return pid; + return api; } - if (pid != 0) { - return pid; + if (api != 0) { + return api; } if (ipcp_type == IPCP_NORMAL) @@ -149,8 +148,7 @@ pid_t ipcp_create(char * ipcp_name, full_name[len] = '\0'; char * argv[] = {full_name, - irmd_pid, - ipcp_name, + irmd_api, 0}; char * envp[] = {0}; @@ -164,16 +162,16 @@ pid_t ipcp_create(char * ipcp_name, exit(EXIT_FAILURE); } -int ipcp_destroy(pid_t pid) +int ipcp_destroy(pid_t api) { int status; - if (kill(pid, SIGTERM)) { + if (kill(api, SIGTERM)) { LOG_ERR("Failed to destroy IPCP"); return -1; } - if (waitpid(pid, &status, 0) < 0) { + if (waitpid(api, &status, 0) < 0) { LOG_ERR("Failed to destroy IPCP"); return -1; } @@ -181,7 +179,7 @@ int ipcp_destroy(pid_t pid) return 0; } -int ipcp_reg(pid_t pid, +int ipcp_reg(pid_t api, char ** dif_names, size_t len) { @@ -198,7 +196,7 @@ int ipcp_reg(pid_t pid, msg.dif_names = dif_names; msg.len = len; - recv_msg = send_recv_ipcp_msg(pid, &msg); + recv_msg = send_recv_ipcp_msg(api, &msg); if (recv_msg == NULL) return -1; @@ -213,7 +211,7 @@ int ipcp_reg(pid_t pid, return ret; } -int ipcp_unreg(pid_t pid, +int ipcp_unreg(pid_t api, char ** dif_names, size_t len) { @@ -230,7 +228,7 @@ int ipcp_unreg(pid_t pid, msg.dif_names = dif_names; msg.len = len; - recv_msg = send_recv_ipcp_msg(pid, &msg); + recv_msg = send_recv_ipcp_msg(api, &msg); if (recv_msg == NULL) return -1; @@ -246,7 +244,7 @@ int ipcp_unreg(pid_t pid, } -int ipcp_bootstrap(pid_t pid, +int ipcp_bootstrap(pid_t api, dif_config_msg_t * conf) { ipcp_msg_t msg = IPCP_MSG__INIT; @@ -259,7 +257,7 @@ int ipcp_bootstrap(pid_t pid, msg.code = IPCP_MSG_CODE__IPCP_BOOTSTRAP; msg.conf = conf; - recv_msg = send_recv_ipcp_msg(pid, &msg); + recv_msg = send_recv_ipcp_msg(api, &msg); if (recv_msg == NULL) return -1; @@ -274,7 +272,7 @@ int ipcp_bootstrap(pid_t pid, return ret; } -int ipcp_enroll(pid_t pid, +int ipcp_enroll(pid_t api, char * dif_name, char * n_1_dif) { @@ -289,7 +287,7 @@ int ipcp_enroll(pid_t pid, msg.dif_name = dif_name; msg.n_1_dif = n_1_dif; - recv_msg = send_recv_ipcp_msg(pid, &msg); + recv_msg = send_recv_ipcp_msg(api, &msg); if (recv_msg == NULL) { return -1; } @@ -305,7 +303,7 @@ int ipcp_enroll(pid_t pid, return ret; } -int ipcp_name_reg(pid_t pid, +int ipcp_name_reg(pid_t api, char * name) { ipcp_msg_t msg = IPCP_MSG__INIT; @@ -318,7 +316,7 @@ int ipcp_name_reg(pid_t pid, msg.code = IPCP_MSG_CODE__IPCP_NAME_REG; msg.name = name; - recv_msg = send_recv_ipcp_msg(pid, &msg); + recv_msg = send_recv_ipcp_msg(api, &msg); if (recv_msg == NULL) return -1; @@ -333,7 +331,7 @@ int ipcp_name_reg(pid_t pid, return ret; } -int ipcp_name_unreg(pid_t pid, +int ipcp_name_unreg(pid_t api, char * name) { ipcp_msg_t msg = IPCP_MSG__INIT; @@ -343,7 +341,7 @@ int ipcp_name_unreg(pid_t pid, msg.code = IPCP_MSG_CODE__IPCP_NAME_UNREG; msg.name = name; - recv_msg = send_recv_ipcp_msg(pid, &msg); + recv_msg = send_recv_ipcp_msg(api, &msg); if (recv_msg == NULL) return -1; @@ -358,9 +356,9 @@ int ipcp_name_unreg(pid_t pid, return ret; } -int ipcp_flow_alloc(pid_t pid, +int ipcp_flow_alloc(pid_t api, int port_id, - pid_t n_pid, + pid_t n_api, char * dst_name, char * src_ae_name, enum qos_cube qos) @@ -375,14 +373,14 @@ int ipcp_flow_alloc(pid_t pid, msg.code = IPCP_MSG_CODE__IPCP_FLOW_ALLOC; msg.has_port_id = true; msg.port_id = port_id; - msg.has_pid = true; - msg.pid = n_pid; + msg.has_api = true; + msg.api = n_api; msg.src_ae_name = src_ae_name; msg.dst_name = dst_name; msg.has_qos_cube = true; msg.qos_cube = qos; - recv_msg = send_recv_ipcp_msg(pid, &msg); + recv_msg = send_recv_ipcp_msg(api, &msg); if (recv_msg == NULL) return -1; @@ -397,9 +395,9 @@ int ipcp_flow_alloc(pid_t pid, return ret; } -int ipcp_flow_alloc_resp(pid_t pid, +int ipcp_flow_alloc_resp(pid_t api, int port_id, - pid_t n_pid, + pid_t n_api, int response) { ipcp_msg_t msg = IPCP_MSG__INIT; @@ -409,12 +407,12 @@ int ipcp_flow_alloc_resp(pid_t pid, msg.code = IPCP_MSG_CODE__IPCP_FLOW_ALLOC_RESP; msg.has_port_id = true; msg.port_id = port_id; - msg.has_pid = true; - msg.pid = n_pid; + msg.has_api = true; + msg.api = n_api; msg.has_response = true; msg.response = response; - recv_msg = send_recv_ipcp_msg(pid, &msg); + recv_msg = send_recv_ipcp_msg(api, &msg); if (recv_msg == NULL) return -1; @@ -429,7 +427,7 @@ int ipcp_flow_alloc_resp(pid_t pid, return ret; } -int ipcp_flow_req_arr(pid_t pid, +int ipcp_flow_req_arr(pid_t api, char * dst_name, char * src_ae_name) { @@ -441,8 +439,8 @@ int ipcp_flow_req_arr(pid_t pid, return -EINVAL; msg.code = IRM_MSG_CODE__IPCP_FLOW_REQ_ARR; - msg.has_pid = true; - msg.pid = pid; + msg.has_api = true; + msg.api = api; msg.dst_name = dst_name; msg.ae_name = src_ae_name; @@ -461,7 +459,7 @@ int ipcp_flow_req_arr(pid_t pid, return port_id; } -int ipcp_flow_alloc_reply(pid_t pid, +int ipcp_flow_alloc_reply(pid_t api, int port_id, int response) { @@ -491,10 +489,10 @@ int ipcp_flow_alloc_reply(pid_t pid, } -int ipcp_flow_dealloc(pid_t pid, +int ipcp_flow_dealloc(pid_t api, int port_id) { - if (pid != 0) { + if (api != 0) { ipcp_msg_t msg = IPCP_MSG__INIT; ipcp_msg_t * recv_msg = NULL; int ret = -1; @@ -503,7 +501,7 @@ int ipcp_flow_dealloc(pid_t pid, msg.has_port_id = true; msg.port_id = port_id; - recv_msg = send_recv_ipcp_msg(pid, &msg); + recv_msg = send_recv_ipcp_msg(api, &msg); if (recv_msg == NULL) return 0; diff --git a/src/lib/ipcpd_messages.proto b/src/lib/ipcpd_messages.proto index 9eb5707c..f9e0f972 100644 --- a/src/lib/ipcpd_messages.proto +++ b/src/lib/ipcpd_messages.proto @@ -27,7 +27,7 @@ message ipcp_msg { optional sint32 qos_cube = 11; optional dif_config_msg conf = 12; optional sint32 fd = 13; - optional sint32 pid = 14; + optional sint32 api = 14; optional sint32 response = 15; optional sint32 result = 16; }; diff --git a/src/lib/irm.c b/src/lib/irm.c index 8fd0a7e7..6b296258 100644 --- a/src/lib/irm.c +++ b/src/lib/irm.c @@ -27,22 +27,18 @@ #include <ouroboros/common.h> #include <ouroboros/logs.h> #include <ouroboros/sockets.h> -#include <ouroboros/instance_name.h> #include <stdlib.h> -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->apis == NULL) { + irm_msg__free_unpacked(recv_msg, NULL); + return -1; + } + + nr = recv_msg->n_apis; + *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->apis[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..fa2ca258 100644 --- a/src/lib/irmd_messages.proto +++ b/src/lib/irmd_messages.proto @@ -25,35 +25,37 @@ 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; optional sint32 response = 8; optional string dst_name = 9; optional sint32 port_id = 10; - optional int32 pid = 11; - optional dif_config_msg conf = 12; - optional bool autoexec = 13; - optional bool hard = 14; - optional sint32 result = 15; + optional dif_config_msg conf = 11; + optional uint32 opts = 12; + repeated int32 apis = 13; + optional sint32 result = 14; }; diff --git a/src/lib/shm_ap_rbuff.c b/src/lib/shm_ap_rbuff.c index 6ee2936c..69e96c40 100644 --- a/src/lib/shm_ap_rbuff.c +++ b/src/lib/shm_ap_rbuff.c @@ -55,7 +55,7 @@ struct shm_ap_rbuff { size_t * ptr_tail; /* start of ringbuffer tail */ pthread_mutex_t * shm_mutex; /* lock all free space in shm */ pthread_cond_t * work; /* threads will wait for a signal */ - pid_t pid; /* pid to which this rb belongs */ + pid_t api; /* api to which this rb belongs */ int fd; }; @@ -138,19 +138,19 @@ struct shm_ap_rbuff * shm_ap_rbuff_create() *rb->ptr_tail = 0; rb->fd = shm_fd; - rb->pid = getpid(); + rb->api = getpid(); return rb; } -struct shm_ap_rbuff * shm_ap_rbuff_open(pid_t pid) +struct shm_ap_rbuff * shm_ap_rbuff_open(pid_t api) { struct shm_ap_rbuff * rb; int shm_fd; struct rb_entry * shm_base; char fn[25]; - sprintf(fn, SHM_AP_RBUFF_PREFIX "%d", pid); + sprintf(fn, SHM_AP_RBUFF_PREFIX "%d", api); rb = malloc(sizeof(*rb)); if (rb == NULL) { @@ -190,7 +190,7 @@ struct shm_ap_rbuff * shm_ap_rbuff_open(pid_t pid) rb->work = (pthread_cond_t *) (rb->shm_mutex + 1); rb->fd = shm_fd; - rb->pid = pid; + rb->api = api; return rb; } @@ -219,7 +219,7 @@ void shm_ap_rbuff_destroy(struct shm_ap_rbuff * rb) return; } - if (rb->pid != getpid()) { + if (rb->api != getpid()) { LOG_ERR("Tried to destroy other AP's rbuff."); return; } @@ -227,7 +227,7 @@ void shm_ap_rbuff_destroy(struct shm_ap_rbuff * rb) if (close(rb->fd) < 0) LOG_DBGF("Couldn't close shared memory."); - sprintf(fn, SHM_AP_RBUFF_PREFIX "%d", rb->pid); + sprintf(fn, SHM_AP_RBUFF_PREFIX "%d", rb->api); if (munmap(rb->shm_base, SHM_RBUFF_FILE_SIZE) == -1) LOG_DBGF("Couldn't unmap shared memory."); diff --git a/src/lib/shm_du_map.c b/src/lib/shm_du_map.c index f5909e11..2a316265 100644 --- a/src/lib/shm_du_map.c +++ b/src/lib/shm_du_map.c @@ -75,7 +75,7 @@ struct shm_du_map { size_t * ptr_tail; /* start of ringbuffer tail */ pthread_mutex_t * shm_mutex; /* lock all free space in shm */ pthread_cond_t * sanitize; /* run sanitizer when buffer full */ - pid_t * pid; /* pid of the irmd owner */ + pid_t * api; /* api of the irmd owner */ int fd; }; @@ -141,7 +141,7 @@ struct shm_du_map * shm_du_map_create() dum->ptr_tail = dum->ptr_head + 1; dum->shm_mutex = (pthread_mutex_t *) (dum->ptr_tail + 1); dum->sanitize = (pthread_cond_t *) (dum->shm_mutex + 1); - dum->pid = (pid_t *) (dum->sanitize + 1); + dum->api = (pid_t *) (dum->sanitize + 1); pthread_mutexattr_init(&mattr); pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED); @@ -155,7 +155,7 @@ struct shm_du_map * shm_du_map_create() *dum->ptr_head = 0; *dum->ptr_tail = 0; - *dum->pid = getpid(); + *dum->api = getpid(); dum->fd = shm_fd; @@ -203,7 +203,7 @@ struct shm_du_map * shm_du_map_open() dum->ptr_tail = dum->ptr_head + 1; dum->shm_mutex = (pthread_mutex_t *) (dum->ptr_tail + 1); dum->sanitize = (pthread_cond_t *) (dum->shm_mutex + 1); - dum->pid = (pid_t *) (dum->sanitize + 1); + dum->api = (pid_t *) (dum->sanitize + 1); dum->fd = shm_fd; @@ -212,7 +212,7 @@ struct shm_du_map * shm_du_map_open() pid_t shm_du_map_owner(struct shm_du_map * dum) { - return *dum->pid; + return *dum->api; } void * shm_du_map_sanitize(void * o) diff --git a/src/lib/sockets.c b/src/lib/sockets.c index 403d2833..9bfbad5e 100644 --- a/src/lib/sockets.c +++ b/src/lib/sockets.c @@ -149,19 +149,19 @@ irm_msg_t * send_recv_irm_msg(irm_msg_t * msg) } -char * ipcp_sock_path(pid_t pid) +char * ipcp_sock_path(pid_t api) { char * full_name = NULL; - char * pid_string = NULL; + char * api_string = NULL; size_t len = 0; char * delim = "_"; - len = n_digits(pid); - pid_string = malloc(len + 1); - if (pid_string == NULL) + len = n_digits(api); + api_string = malloc(len + 1); + if (api_string == NULL) return NULL; - sprintf(pid_string, "%d", pid); + sprintf(api_string, "%d", api); len += strlen(IPCP_SOCK_PATH_PREFIX); len += strlen(delim); @@ -169,16 +169,16 @@ char * ipcp_sock_path(pid_t pid) full_name = malloc(len + 1); if (full_name == NULL) { - free(pid_string); + free(api_string); return NULL; } strcpy(full_name, IPCP_SOCK_PATH_PREFIX); strcat(full_name, delim); - strcat(full_name, pid_string); + strcat(full_name, api_string); strcat(full_name, SOCK_PATH_SUFFIX); - free(pid_string); + free(api_string); return full_name; } 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 <ouroboros/common.h> -#include <ouroboros/instance_name.h> #include <ouroboros/irm.h> #include <stdio.h> #include <string.h> @@ -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 <[email protected]> + * + * 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 <stdio.h> +#include <string.h> + +#include <ouroboros/irm.h> + +#include "irm_ops.h" +#include "irm_utils.h" + +static void usage() +{ + printf("Usage: irm bind\n" + " name <name>\n" + " apn <application process name>\n" + " [auto] (instantiate apn if not running)\n" + " [unique] (there can only be one instantiation)\n" + " [-- <application arguments>]\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_ipcp.c b/src/tools/irm/irm_ipcp.c new file mode 100644 index 00000000..f658ead5 --- /dev/null +++ b/src/tools/irm/irm_ipcp.c @@ -0,0 +1,77 @@ +/* + * Ouroboros - Copyright (C) 2016 + * + * A tool to instruct the IRM daemon + * + * Sander Vrijders <[email protected]> + * + * 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 <stdio.h> + +#include "irm_ops.h" +#include "irm_utils.h" + +static void usage() +{ + printf("Usage: irm ipcp [OPERATION]\n\n" + "where OPERATION = {create destroy\n" + " bootstrap enroll help\n"); +} + +static int do_help(int argc, char **argv) +{ + usage(); + return 0; +} + +static const struct cmd { + const char * cmd; + int (* func)(int argc, char ** argv); +} cmds[] = { + { "create", do_create_ipcp }, + { "destroy", do_destroy_ipcp }, + { "bootstrap", do_bootstrap_ipcp }, + { "enroll", do_enroll_ipcp }, + { "help", do_help }, + { 0 } +}; + +static int do_cmd(const char * argv0, + int argc, + char ** argv) +{ + const struct cmd * c; + + for (c = cmds; c->cmd; ++c) { + if (matches(argv0, c->cmd) == 0) + return c->func(argc - 1, argv + 1); + } + + fprintf(stderr, "\"%s\" is unknown, try \"irm ipcp help\".\n", argv0); + + return -1; +} + +int ipcp_cmd(int argc, char ** argv) +{ + if (argc < 1) { + usage(); + return -1; + } + + return do_cmd(argv[0], argc, argv); +} diff --git a/src/tools/irm/irm_bootstrap_ipcp.c b/src/tools/irm/irm_ipcp_bootstrap.c index e11b5f3f..c2b696e4 100644 --- a/src/tools/irm/irm_bootstrap_ipcp.c +++ b/src/tools/irm/irm_ipcp_bootstrap.c @@ -26,7 +26,7 @@ #include <arpa/inet.h> #include <ouroboros/irm.h> -#include <ouroboros/dif_config.h> +#include <ouroboros/irm_config.h> #include "irm_ops.h" #include "irm_utils.h" @@ -50,29 +50,28 @@ static void usage() { /* FIXME: Add dif_config stuff */ - printf("Usage: irm bootstrap_ipcp\n" - " ap <application process name>\n" - " [api <application process instance>]\n" - " dif <DIF name>\n" - " type [TYPE]\n\n" + printf("Usage: irm ipcp bootstrap\n" + " name <ipcp name>\n" + " dif <DIF name>\n" + " type [TYPE]\n\n" "where TYPE = {" NORMAL " " LOCAL " " SHIM_UDP " " SHIM_ETH_LLC"}\n\n" "if TYPE == " NORMAL "\n" - " [addr <address size> (default: %d)]\n" - " [cep_id <CEP-id size> (default: %d)]\n" - " [pdu_len <PDU length size> (default: %d)]\n" - " [qos_id <QoS-id size> (default: %d)]\n" - " [seqno <sequence number size> (default: %d)]\n" - " [ttl <time to live size> (default: %d)]\n" - " [chk <checksum size> (default: %d)]\n" - " [min_pdu <minimum PDU size> (default: %d)]\n" - " [max_pdu <maximum PDU size> (default: %d)]\n" + " [addr <address size> (default: %d)]\n" + " [cep_id <CEP-id size> (default: %d)]\n" + " [pdu_len <PDU length size> (default: %d)]\n" + " [qos_id <QoS-id size> (default: %d)]\n" + " [seqno <sequence number size> (default: %d)]\n" + " [ttl <time to live size> (default: %d)]\n" + " [chk <checksum size> (default: %d)]\n" + " [min_pdu <minimum PDU size> (default: %d)]\n" + " [max_pdu <maximum PDU size> (default: %d)]\n" "if TYPE == " SHIM_UDP "\n" - " ip <IP address in dotted notation>\n" - " [dns <DDNS IP address in dotted notation>" + " ip <IP address in dotted notation>\n" + " [dns <DDNS IP address in dotted notation>" " (default = none: %d)]\n" "if TYPE == " SHIM_ETH_LLC "\n" - " if_name <interface name>\n", + " if_name <interface name>\n", DEFAULT_ADDR_SIZE, DEFAULT_CEP_ID_SIZE, DEFAULT_PDU_LEN_SIZE, DEFAULT_QOS_ID_SIZE, DEFAULT_SEQ_NO_SIZE, DEFAULT_TTL_SIZE, @@ -82,7 +81,7 @@ static void usage() int do_bootstrap_ipcp(int argc, char ** argv) { - instance_name_t api = {NULL, 0}; + char * name = NULL; struct dif_config conf; uint8_t addr_size = DEFAULT_ADDR_SIZE; uint8_t cep_id_size = DEFAULT_CEP_ID_SIZE; @@ -98,16 +97,17 @@ int do_bootstrap_ipcp(int argc, char ** argv) char * ipcp_type = NULL; char * dif_name = NULL; char * if_name = NULL; + pid_t * apis; + ssize_t len = 0; + int i = 0; while (argc > 0) { if (matches(*argv, "type") == 0) { ipcp_type = *(argv + 1); } else if (matches(*argv, "dif") == 0) { dif_name = *(argv + 1); - } else if (matches(*argv, "ap") == 0) { - api.name = *(argv + 1); - } else if (matches(*argv, "api") == 0) { - api.id = atoi(*(argv + 1)); + } else if (matches(*argv, "name") == 0) { + name = *(argv + 1); } else if (matches(*argv, "ip") == 0) { if (inet_pton (AF_INET, *(argv + 1), &ip_addr) != 1) { usage(); @@ -140,7 +140,7 @@ int do_bootstrap_ipcp(int argc, char ** argv) max_pdu_size = atoi(*(argv + 1)); } else { printf("\"%s\" is unknown, try \"irm " - "bootstrap_ipcp\".\n", *argv); + "ipcp bootstrap\".\n", *argv); return -1; } @@ -148,7 +148,7 @@ int do_bootstrap_ipcp(int argc, char ** argv) argv += 2; } - if (api.name == NULL || dif_name == NULL || ipcp_type == NULL) { + if (name == NULL || dif_name == NULL || ipcp_type == NULL) { usage(); return -1; } @@ -188,5 +188,13 @@ int do_bootstrap_ipcp(int argc, char ** argv) return -1; } - return irm_bootstrap_ipcp(&api, &conf); + len = irm_list_ipcps(name, &apis); + if (len <= 0) + return -1; + + for (i = 0; i < len; i++) + if (irm_bootstrap_ipcp(apis[i], &conf)) + return -1; + + return 0; } diff --git a/src/tools/irm/irm_create_ipcp.c b/src/tools/irm/irm_ipcp_create.c index cb957d94..b43a544e 100644 --- a/src/tools/irm/irm_create_ipcp.c +++ b/src/tools/irm/irm_ipcp_create.c @@ -21,13 +21,9 @@ */ #include <ouroboros/irm.h> -#include <ouroboros/common.h> -#include <ouroboros/instance_name.h> #include <stdio.h> -#include <stdlib.h> #include <string.h> -#include <errno.h> #include "irm_ops.h" #include "irm_utils.h" @@ -39,9 +35,9 @@ static void usage() { - printf("Usage: irm create_ipcp\n" - " ap <application process name>\n" - " type [TYPE]\n\n" + printf("Usage: irm ipcp create\n" + " name <ipcp name>\n" + " type [TYPE]\n\n" "where TYPE = {" NORMAL " " LOCAL " " SHIM_UDP " " SHIM_ETH_LLC "}\n"); } @@ -55,11 +51,11 @@ int do_create_ipcp(int argc, char ** argv) while (argc > 0) { if (matches(*argv, "type") == 0) { ipcp_type = *(argv + 1); - } else if (matches(*argv, "ap") == 0) { + } else if (matches(*argv, "name") == 0) { ipcp_name = *(argv + 1); } else { printf("\"%s\" is unknown, try \"irm " - "create_ipcp\".\n", *argv); + "ipcp create\".\n", *argv); return -1; } diff --git a/src/tools/irm/irm_destroy_ipcp.c b/src/tools/irm/irm_ipcp_destroy.c index fe6ef57e..ebd4283d 100644 --- a/src/tools/irm/irm_destroy_ipcp.c +++ b/src/tools/irm/irm_ipcp_destroy.c @@ -21,32 +21,31 @@ */ #include <stdio.h> -#include <stdlib.h> + #include <ouroboros/irm.h> -#include <ouroboros/common.h> #include "irm_ops.h" #include "irm_utils.h" static void usage() { - printf("Usage: irm destroy_ipcp\n" - " ap <application process name>\n" - " [api <application process instance>]\n"); + printf("Usage: irm ipcp destroy\n" + " name <ipcp name>\n"); } int do_destroy_ipcp(int argc, char ** argv) { - instance_name_t api = {NULL, 0}; + char * name = NULL; + pid_t * apis; + ssize_t len = 0; + int i = 0; while (argc > 0) { - if (matches(*argv, "ap") == 0) { - api.name = *(argv + 1); - } else if (matches(*argv, "api") == 0) { - api.id = atoi(*(argv + 1)); + if (matches(*argv, "name") == 0) { + name = *(argv + 1); } else { printf("\"%s\" is unknown, try \"irm " - "destroy_ipcp\".\n", *argv); + "ipcp destroy\".\n", *argv); return -1; } @@ -54,10 +53,18 @@ int do_destroy_ipcp(int argc, char ** argv) argv += 2; } - if (api.name == NULL) { + if (name == NULL) { usage(); return -1; } - return irm_destroy_ipcp(&api); + len = irm_list_ipcps(name, &apis); + if (len <= 0) + return -1; + + for (i = 0; i < len; i++) + if (irm_destroy_ipcp(apis[i])) + return -1; + + return 0; } diff --git a/src/tools/irm/irm_enroll_ipcp.c b/src/tools/irm/irm_ipcp_enroll.c index 5c9572bf..d6b1b27e 100644 --- a/src/tools/irm/irm_enroll_ipcp.c +++ b/src/tools/irm/irm_ipcp_enroll.c @@ -21,31 +21,30 @@ */ #include <stdio.h> -#include <stdlib.h> + #include <ouroboros/irm.h> -#include <ouroboros/common.h> #include "irm_ops.h" #include "irm_utils.h" static void usage() { - printf("Usage: irm enroll_ipcp\n" - " ap <application process name>\n" - " [api <application process instance>]\n" - " dif <dif to enroll in>\n"); + printf("Usage: irm ipcp enroll\n" + " name <ipcp name>\n" + " dif <dif to enroll in>\n"); } int do_enroll_ipcp(int argc, char ** argv) { - instance_name_t api = {NULL, 0}; + char * name = NULL; char * dif_name = NULL; + pid_t * apis; + ssize_t len = 0; + int i = 0; while (argc > 0) { - if (matches(*argv, "ap") == 0) { - api.name = *(argv + 1); - } else if (matches(*argv, "api") == 0) { - api.id = atoi(*(argv + 1)); + if (matches(*argv, "name") == 0) { + name = *(argv + 1); } else if (matches(*argv, "dif") == 0) { dif_name = *(argv + 1); } else { @@ -58,10 +57,18 @@ int do_enroll_ipcp(int argc, char ** argv) argv += 2; } - if (dif_name == NULL || api.name == NULL) { + if (dif_name == NULL || name == NULL) { usage(); return -1; } - return irm_enroll_ipcp(&api, dif_name); + len = irm_list_ipcps(name, &apis); + if (len <= 0) + return -1; + + for (i = 0; i < len; i++) + if (irm_enroll_ipcp(apis[i], dif_name)) + return -1; + + return 0; } diff --git a/src/tools/irm/irm_ops.h b/src/tools/irm/irm_ops.h index ea51cbeb..24eee0df 100644 --- a/src/tools/irm/irm_ops.h +++ b/src/tools/irm/irm_ops.h @@ -20,9 +20,13 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ +int ipcp_cmd(int argc, char ** argv); int do_create_ipcp(int argc, char ** argv); int do_destroy_ipcp(int argc, char ** argv); int do_bootstrap_ipcp(int argc, char ** argv); int do_enroll_ipcp(int argc, char ** argv); + +int do_bind(int argc, char ** argv); +int do_unbind(int argc, char ** argv); int do_register(int argc, char ** argv); int do_unregister(int argc, char ** argv); diff --git a/src/tools/irm/irm_register.c b/src/tools/irm/irm_register.c index 67c81025..62470d1d 100644 --- a/src/tools/irm/irm_register.c +++ b/src/tools/irm/irm_register.c @@ -1,7 +1,7 @@ /* * Ouroboros - Copyright (C) 2016 * - * Register AP's in DIFs + * Register names in IPCPs * * Dimitri Staessens <[email protected]> * Sander Vrijders <[email protected]> @@ -21,15 +21,9 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -#include <ouroboros/config.h> -#include <stdio.h> #include <ouroboros/irm.h> -#include <ouroboros/common.h> -#include <stdlib.h> -#include <string.h> -#include <errno.h> -#include <sys/types.h> -#include <signal.h> + +#include <stdio.h> #include "irm_ops.h" #include "irm_utils.h" @@ -39,13 +33,10 @@ static void usage() { printf("Usage: irm register\n" - " n <name>\n" - " apn <application process name>\n" - " [api <application instance id>]\n" - " [auto] (instantiate apn if not running)\n" + " name <name>\n" + " dif <dif name to register with>\n" " [dif <dif name to register with>]\n" " [... (maximum %d difs)]\n" - " [-- <application arguments>]\n" , MAX_DIFS); } @@ -53,33 +44,12 @@ static void usage() int do_register(int argc, char ** argv) { char * name = NULL; - char ** args = NULL; char * difs[MAX_DIFS]; size_t difs_len = 0; - bool api_opt = false; - bool args_opt = false; - bool autoexec = false; - int i = argc; - instance_name_t api = {NULL, 0}; - - while (i > 0) { + while (argc > 0) { if (matches(*argv, "name") == 0) { name = *(argv + 1); - } else if (matches(*argv, "apn") == 0) { - api.name = *(argv + 1); - } else if (matches(*argv, "api") == 0) { - api.id = atoi(*(argv + 1)); - api_opt = true; - } else if (strcmp(*argv, "auto") == 0) { - autoexec = true; - ++i; - --argv; - } else if (strcmp(*argv, "--") == 0) { - ++argv; - --i; - args_opt = true; - break; } else if (matches(*argv, "dif") == 0) { difs[difs_len++] = *(argv + 1); if (difs_len > MAX_DIFS) { @@ -92,32 +62,14 @@ int do_register(int argc, char ** argv) return -1; } - i -= 2; + argc -= 2; argv += 2; } - if (name == NULL || api.name == NULL) { + if (difs_len < 1 || name == NULL) { usage(); return -1; } - if (api_opt && kill(api.id, 0) < 0) { - printf("No application running with that pid."); - return -1; - } - - if (api_opt && autoexec) { - printf("Instance is given, auto disabled.\n"); - autoexec = false; - } - - args = argv; - - if (args_opt && api_opt) { - printf("Instance is given, args ignored.\n"); - args = NULL; - i = 0; - } - - return irm_reg(name, &api, i, args, autoexec, difs, difs_len); + return irm_reg(name, difs, difs_len); } diff --git a/src/tools/irm/irm_unbind.c b/src/tools/irm/irm_unbind.c new file mode 100644 index 00000000..9e8f3c9c --- /dev/null +++ b/src/tools/irm/irm_unbind.c @@ -0,0 +1,63 @@ +/* + * Ouroboros - Copyright (C) 2016 + * + * Unbind names in the processing system + * + * Sander Vrijders <[email protected]> + * + * 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 <stdio.h> + +#include <ouroboros/irm.h> + +#include "irm_ops.h" +#include "irm_utils.h" + +static void usage() +{ + printf("Usage: irm unbind\n" + " name <name>\n" + " ap <application process name>\n"); +} + +int do_unbind(int argc, char ** argv) +{ + char * name = NULL; + char * ap_name = NULL; + + while (argc > 0) { + if (matches(*argv, "name") == 0) { + name = *(argv + 1); + } else if (matches(*argv, "ap") == 0) { + ap_name = *(argv + 1); + } else { + printf("\"%s\" is unknown, try \"irm " + "unbind\".\n", *argv); + return -1; + } + + argc -= 2; + argv += 2; + } + + if (name == NULL && ap_name == NULL) { + usage(); + return -1; + } + + return irm_unbind(name, ap_name, 0); +} diff --git a/src/tools/irm/irm_unregister.c b/src/tools/irm/irm_unregister.c index d778e285..edcd42bb 100644 --- a/src/tools/irm/irm_unregister.c +++ b/src/tools/irm/irm_unregister.c @@ -1,7 +1,7 @@ /* * Ouroboros - Copyright (C) 2016 * - * Unregister IPC Processes in an N-1 DIF + * Unregister names from IPCPs * * Dimitri Staessens <[email protected]> * Sander Vrijders <[email protected]> @@ -21,15 +21,10 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -#include <ouroboros/config.h> #include <stdio.h> -#include <ouroboros/irm.h> -#include <ouroboros/common.h> -#include <stdlib.h> #include <string.h> -#include <errno.h> -#include <sys/types.h> -#include <signal.h> + +#include <ouroboros/irm.h> #include "irm_ops.h" #include "irm_utils.h" @@ -39,40 +34,22 @@ static void usage() { printf("Usage: irm unregister\n" - " [name <name>]\n" - " [apn <application process name>]\n" - " [api <application process instance>]\n" + " name <name>\n" " dif <dif name to unregister from>\n" " [dif <dif name to unregister from>]\n" " [... (maximum %d difs)]\n" - " [hard] (unregisters everything using that name)\n" , MAX_DIFS); } int do_unregister(int argc, char ** argv) { - instance_name_t api = {NULL, 0}; char * difs[MAX_DIFS]; size_t difs_len = 0; char * name = NULL; - bool hard_opt = false; - bool ap_id = false; - instance_name_t * ptr_api = NULL; while (argc > 0) { if (matches(*argv, "name") == 0) { name = *(argv + 1); - } else if (matches(*argv, "ap") == 0) { - api.name = *(argv + 1); - ptr_api = &api; - } else if (matches(*argv, "api") == 0) { - api.id = atoi(*(argv + 1)); - ap_id = true; - } else if (strcmp(*argv, "hard") == 0) { - hard_opt = true; - /* this has no value */ - ++argc; - --argv; } else if (matches(*argv, "dif") == 0) { difs[difs_len++] = *(argv + 1); if (difs_len > MAX_DIFS) { @@ -89,28 +66,10 @@ int do_unregister(int argc, char ** argv) argv += 2; } - if (difs_len == 0) { - usage(); - return -1; - } - - if (name == NULL && api.name == NULL) { - printf("apn or name must be set.\n"); - usage(); - return -1; - } - - if (ap_id && api.name == NULL) { - printf("api requires apn.\n"); - usage(); - return -1; - } - - if (hard_opt && api.name != NULL) { - printf("apn and/or api must not be set when using hard.\n"); + if (difs_len == 0 || name == NULL) { usage(); return -1; } - return irm_unreg(name, ptr_api, difs, difs_len, hard_opt); + return irm_unreg(name, difs, difs_len); } |