diff options
Diffstat (limited to 'src/irmd/main.c')
-rw-r--r-- | src/irmd/main.c | 526 |
1 files changed, 287 insertions, 239 deletions
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); } |