diff options
Diffstat (limited to 'src/ipcpd/unicast/main.c')
-rw-r--r-- | src/ipcpd/unicast/main.c | 96 |
1 files changed, 41 insertions, 55 deletions
diff --git a/src/ipcpd/unicast/main.c b/src/ipcpd/unicast/main.c index e6cb2994..7989d3e1 100644 --- a/src/ipcpd/unicast/main.c +++ b/src/ipcpd/unicast/main.c @@ -55,13 +55,8 @@ #include <assert.h> #include <inttypes.h> -struct ipcp ipcpi; - -static int initialize_components(const struct ipcp_config * conf) +static int initialize_components(struct ipcp_config * conf) { - strcpy(ipcpi.layer_name, conf->layer_info.name); - ipcpi.dir_hash_algo = (enum hash_algo) conf->layer_info.dir_hash_algo; - assert(ipcp_dir_hash_len() != 0); if (addr_auth_init(conf->unicast.addr_auth_type, @@ -70,13 +65,7 @@ static int initialize_components(const struct ipcp_config * conf) goto fail_addr_auth; } - ipcpi.dt_addr = addr_auth_address(); - if (ipcpi.dt_addr == 0) { - log_err("Failed to get a valid address."); - goto fail_addr_auth; - } - - log_info("IPCP got address %" PRIu64 ".", ipcpi.dt_addr); + log_info("IPCP got address %" PRIu64 ".", addr_auth_address()); if (ca_init(conf->unicast.cong_avoid)) { log_err("Failed to initialize congestion avoidance."); @@ -88,23 +77,25 @@ static int initialize_components(const struct ipcp_config * conf) goto fail_dt; } - if (fa_init()) { - log_err("Failed to initialize flow allocator component."); - goto fail_fa; - } + ipcp_set_dir_hash_algo((enum hash_algo) conf->layer_info.dir_hash_algo); - if (dir_init()) { + if (dir_init(&conf->unicast.dir)) { log_err("Failed to initialize directory."); goto fail_dir; } + if (fa_init()) { + log_err("Failed to initialize flow allocator component."); + goto fail_fa; + } + ipcp_set_state(IPCP_INIT); return 0; - fail_dir: - fa_fini(); fail_fa: + dir_fini(); + fail_dir: dt_fini(); fail_dt: ca_fini(); @@ -116,10 +107,10 @@ static int initialize_components(const struct ipcp_config * conf) static void finalize_components(void) { - dir_fini(); - fa_fini(); + dir_fini(); + dt_fini(); ca_fini(); @@ -129,6 +120,11 @@ static void finalize_components(void) static int start_components(void) { + if (connmgr_start() < 0) { + log_err("Failed to start AP connection manager."); + goto fail_connmgr_start; + } + if (dt_start() < 0) { log_err("Failed to start data transfer."); goto fail_dt_start; @@ -144,27 +140,29 @@ static int start_components(void) goto fail_enroll_start; } - if (connmgr_start() < 0) { - log_err("Failed to start AP connection manager."); - goto fail_connmgr_start; + if (dir_start() < 0) { + log_err("Failed to start directory."); + goto fail_dir_start; } return 0; - fail_connmgr_start: + fail_dir_start: enroll_stop(); fail_enroll_start: fa_stop(); fail_fa_start: dt_stop(); fail_dt_start: + connmgr_stop(); + fail_connmgr_start: ipcp_set_state(IPCP_INIT); return -1; } static void stop_components(void) { - connmgr_stop(); + dir_stop(); enroll_stop(); @@ -172,24 +170,17 @@ static void stop_components(void) dt_stop(); - ipcp_set_state(IPCP_INIT); -} - -static int bootstrap_components(void) -{ - if (dir_bootstrap()) { - log_err("Failed to bootstrap directory."); - return -1; - } + connmgr_stop(); - return 0; + ipcp_set_state(IPCP_BOOT); } static int unicast_ipcp_enroll(const char * dst, struct layer_info * info) { - struct conn conn; - uint8_t id[ENROLL_ID_LEN]; + struct ipcp_config * conf; + struct conn conn; + uint8_t id[ENROLL_ID_LEN]; if (random_buffer(id, ENROLL_ID_LEN) < 0) { log_err("Failed to generate enrollment ID."); @@ -209,7 +200,11 @@ static int unicast_ipcp_enroll(const char * dst, goto fail_enroll_boot; } - if (initialize_components(enroll_get_conf()) < 0) { + conf = enroll_get_conf(); + + *info = conf->layer_info; + + if (initialize_components(conf) < 0) { log_err_id(id, "Failed to initialize components."); goto fail_enroll_boot; } @@ -227,9 +222,6 @@ static int unicast_ipcp_enroll(const char * dst, log_info_id(id, "Enrolled with %s.", dst); - info->dir_hash_algo = (enum pol_dir_hash) ipcpi.dir_hash_algo; - strcpy(info->name, ipcpi.layer_name); - return 0; fail_start_comp: @@ -240,32 +232,25 @@ static int unicast_ipcp_enroll(const char * dst, return -1; } -static int unicast_ipcp_bootstrap(const struct ipcp_config * conf) +static int unicast_ipcp_bootstrap(struct ipcp_config * conf) { assert(conf); assert(conf->type == THIS_TYPE); - enroll_bootstrap(conf); - if (initialize_components(conf) < 0) { log_err("Failed to init IPCP components."); goto fail_init; } + enroll_bootstrap(conf); + if (start_components() < 0) { log_err("Failed to init IPCP components."); goto fail_start; } - if (bootstrap_components() < 0) { - log_err("Failed to bootstrap IPCP components."); - goto fail_bootstrap; - } - return 0; - fail_bootstrap: - stop_components(); fail_start: finalize_components(); fail_init: @@ -323,11 +308,12 @@ int main(int argc, if (ipcp_get_state() == IPCP_SHUTDOWN) { stop_components(); + ipcp_stop(); finalize_components(); + } else { + ipcp_stop(); } - ipcp_stop(); - enroll_fini(); connmgr_fini(); |