Committer: Tim Just <tim.just@xxxxxxxxxxxxxx> Date: Thu Feb 25 16:43:37 2010 +0100 Revision: 3576 Revision-id: tim.just@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Branch nick: tiny Log: Removed dependency between hadb and update module. Now each module can register a state item initialization function using hip_register_state_init_function. These functions are called every time a new hadb entry is created. The update module registers an init function and therefore the unintended depedency beteween hadb.c and update.c is gone. Modified: M hipd/hadb.c M lib/modularization/modularization.c M lib/modularization/modularization.h M modules/update/hipd/update.c M modules/update/hipd/update.h === modified file 'hipd/hadb.c' --- hipd/hadb.c 2010-02-23 10:18:38 +0000 +++ hipd/hadb.c 2010-02-25 15:43:37 +0000 @@ -13,9 +13,6 @@ #include "lib/core/list.h" #include "lib/modularization/modularization.h" -/* TODO Remove this include when modularization is finised */ -#include "modules/update/hipd/update.h" - #define HIP_HADB_SIZE 53 #define HIP_MAX_HAS 100 @@ -573,44 +570,32 @@ static int hip_hadb_init_entry(hip_ha_t *entry) { int err = 0; - void *update_state = NULL; HIP_IFEL(!entry, -1, "HA is NULL\n"); -#if 0 - INIT_LIST_HEAD(&entry->next_hit); - INIT_LIST_HEAD(&entry->spis_in_old); - INIT_LIST_HEAD(&entry->spis_out_old); -#endif - - //HIP_LOCK_INIT(entry); - //atomic_set(&entry->refcnt,0); - entry->state = HIP_STATE_UNASSOCIATED; entry->hastate = HIP_HASTATE_INVALID; entry->purge_timeout = HIP_HA_PURGE_TIMEOUT; - //initialize the peer hostname + /* Initialize the peer host name */ memset(entry->peer_hostname, '\0', HIP_HOST_ID_HOSTNAME_LEN_MAX); - /* @todo Need hook for modularization - * FIXME This initialization should be done in the update module! - */ - update_state = hip_update_init_state(); - entry->hip_modular_state = hip_init_state(); - hip_add_state_item(entry->hip_modular_state, update_state, "update"); - entry->peer_addresses_old = hip_linked_list_init(); - // Randomize inbound SPI + /* Randomize inbound SPI */ get_random_bytes(&entry->spi_inbound_current, sizeof(entry->spi_inbound_current)); - HIP_IFE(!(entry->hip_msg_retrans.buf = - malloc(HIP_MAX_NETWORK_PACKET)), -ENOMEM); + HIP_IFE(!(entry->hip_msg_retrans.buf = malloc(HIP_MAX_NETWORK_PACKET)), + -ENOMEM); entry->hip_msg_retrans.count = 0; memset(entry->hip_msg_retrans.buf, 0, HIP_MAX_NETWORK_PACKET); + /* Initialize module states */ + entry->hip_modular_state = hip_init_state(); + hip_init_state_items(entry->hip_modular_state); + HIP_DEBUG("Modular state initialized.\n"); + out_err: return err; } === modified file 'lib/modularization/modularization.c' --- lib/modularization/modularization.c 2010-02-23 11:31:56 +0000 +++ lib/modularization/modularization.c 2010-02-25 15:43:37 +0000 @@ -29,6 +29,11 @@ static hip_ll_t *handle_functions[HIP_MAX_PACKET_TYPE][HIP_MAX_HA_STATE]; /** + * @todo add description + */ +static hip_ll_t *state_init_functions; + +/** * hip_register_handle_function * * Register a function for handling of the specified combination from packet @@ -208,7 +213,7 @@ * * @return Success = Pointer to the new data structure * Error = NULL - **/ + */ struct modular_state *hip_init_state(void) { struct modular_state *state; @@ -231,6 +236,60 @@ } /** + * hip_register_state_init_function + * + * Registers a new state initialization function. These functions are called, + * when a new host association database entry is created. + * + * @param Pointer to the state initialization function. + * + * @return Success = 0 + * Error = -1 + */ +int hip_register_state_init_function(void *func) +{ + int err = 0; + hip_ll_t *new_func_list = NULL; + + HIP_IFEL(!func, -1, "Invalid init function provided"); + + if (!state_init_functions) { + HIP_IFEL(((new_func_list = (hip_ll_t *) malloc(sizeof(hip_ll_t))) == NULL), + -1, + "Error on allocating memory for a linked list.\n"); + hip_ll_init(new_func_list); + state_init_functions = new_func_list; + } + + err = hip_ll_add_last(state_init_functions, func); + +out_err: + return err; +} + +/** + * hip_init_state_items + * + * Initialize all registered state items. This function is called, when a new + * host association database entry is created. + * + * @note Call hip_register_state_init_function to add an initialization + * function. + * + * @param *state Pointer to the modular state data structure. + */ +void hip_init_state_items(struct modular_state *state) +{ + hip_ll_node_t *iter = NULL; + int (*init_function)(struct modular_state *state) = NULL; + + while((iter = hip_ll_iterate(state_init_functions, iter)) != NULL) { + init_function = iter->ptr; + init_function(state); + } +} + +/** * hip_add_state_item * * Registers a new state item to the global state. The state item can be of any @@ -245,8 +304,11 @@ * @return Success = id (unsigned int) for retrieving the state by number * Error = -1 **/ -int hip_add_state_item(struct modular_state *state, void *state_item, const char *item_name) +int hip_add_state_item(struct modular_state *state, + void *state_item, + const char *item_name) { + /* Check if identifier already exists */ if (-1 != hip_get_state_item_id(state, item_name)) { return -1; @@ -293,7 +355,8 @@ * @return Success = Pointer to the requested state item (if exists) * Error = NULL **/ -void *hip_get_state_item_by_id(struct modular_state *state, const unsigned int id) +void *hip_get_state_item_by_id(struct modular_state *state, + const unsigned int id) { return hip_ll_get(state->item_list, id); } === modified file 'lib/modularization/modularization.h' --- lib/modularization/modularization.h 2010-02-23 11:31:56 +0000 +++ lib/modularization/modularization.h 2010-02-25 15:43:37 +0000 @@ -50,6 +50,10 @@ void hip_uninit_handle_functions(void); +int hip_register_state_init_function(void *func); + +void hip_init_state_items(struct modular_state *state); + struct modular_state *hip_init_state(void); int hip_add_state_item(struct modular_state *state, === modified file 'modules/update/hipd/update.c' --- modules/update/hipd/update.c 2010-02-25 11:13:41 +0000 +++ modules/update/hipd/update.c 2010-02-25 15:43:37 +0000 @@ -54,17 +54,39 @@ return err; } +/** + * hip_update_get_out_id + * + * @note RFC 5201 Section 5.2.13: + * Notice that the section says "The Update ID is an unsigned quantity, + * initialized by a host to zero upon moving to ESTABLISHED state" and + * "The Update ID is incremented by one before each new UPDATE that is + * sent by the host; the first UPDATE packet originated by a host has + * an Update ID of 0". Therefore we initialize the Update ID with 0 and + * increment this value before a new UPDATE packet is sent. Because the + * first UPDATE packet should contain 0 as value, we need to decrement + * the packet value by one for each UPDATE packet. + * + * @param *state Pointer to the update state. + * + * @return The next UPDATE out ID if state is set, -1 on error +*/ +static inline uint32_t hip_update_get_out_id(struct update_state *state) +{ + HIP_IFCS(state, return (state->update_id_out - 1)); + return -1; +} + /// @todo : should we implement base draft update with ifindex 0 stuff ?? /// @todo : Divide this function into more pieces, handle_spi, handle_seq, etc /// @todo : Remove the uncommented lines? static int hip_create_update_msg(hip_common_t *received_update_packet, - struct hip_hadb_state *ha, hip_common_t *update_packet_to_send, + struct hip_hadb_state *ha, + hip_common_t *update_packet_to_send, struct hip_locator_info_addr_item *locators, int type) { int err = 0; - - uint32_t update_id_out = 0; uint32_t esp_info_old_spi = 0, esp_info_new_spi = 0; uint16_t mask = 0; struct hip_seq *seq = NULL; @@ -130,13 +152,13 @@ * esp_info_new_spi, 0);*/ localstate = hip_get_state_item(ha->hip_modular_state, "update"); - localstate->update_id_out++; - update_id_out = localstate->update_id_out; - _HIP_DEBUG("outgoing UPDATE ID=%u\n", update_id_out); + HIP_DEBUG("outgoing UPDATE ID=%u\n", hip_update_get_out_id(localstate)); /** @todo Handle this case. */ - HIP_IFEL(hip_build_param_seq(update_packet_to_send, update_id_out), -1, - "Building of SEQ param failed\n"); + HIP_IFEL(hip_build_param_seq(update_packet_to_send, + hip_update_get_out_id(localstate)), + -1, + "Building of SEQ parameter failed\n"); /* remember the update id of this update */ /* hip_update_set_status(ha, esp_info_old_spi, @@ -659,49 +681,6 @@ } } -/** - * hip_update_init_id_out - * - * Initialize the id counter for outgoing UPDATE packets. - * - * @note RFC 5201 Section 5.2.13: - * Notice that the section says "The Update ID is an unsigned quantity, - * initialized by a host to zero upon moving to ESTABLISHED state" and - * "The Update ID is incremented by one before each new UPDATE that is - * sent by the host; the first UPDATE packet originated by a host has - * an Update ID of 0". All of these requirements can not be achieved - * at the same time so we initialize the id to -1. - * - * @todo Initialization of an unsigned integer with -1 seems curious. - * - * @param packet_type Type of the received control packet. - * @param ha_state Host association state when receiving the packet. - * @param *ctx Pointer to the packet context. - * - * @return 0 on success, -1 on error - */ -static int hip_update_init_id_out(const uint32_t packet_type, - const uint32_t ha_state, - struct hip_packet_context *ctx) -{ - int err = 0; - struct update_state *localstate = NULL; - - HIP_IFEL(!ctx->hadb_entry, -1, - "No entry in host association database. Dropping.\n"); - - if (ctx->hadb_entry->state == HIP_STATE_ESTABLISHED) { - localstate = hip_get_state_item(ctx->hadb_entry->hip_modular_state, - "update"); - localstate->update_id_out = -1; - } else { - err = -1; - } - -out_err: - return err; -} - int hip_handle_update(const uint32_t packet_type, const uint32_t ha_state, struct hip_packet_context *ctx) @@ -892,80 +871,16 @@ */ int hip_update_init(void) { - hip_register_handle_function(HIP_UPDATE, - HIP_STATE_ESTABLISHED, - &hip_handle_update, - 0); - hip_register_handle_function(HIP_UPDATE, - HIP_STATE_R2_SENT, - &hip_handle_update, - 0); - - hip_register_handle_function(HIP_I2, - HIP_STATE_UNASSOCIATED, - &hip_update_init_id_out, - 10); - hip_register_handle_function(HIP_I2, - HIP_STATE_I1_SENT, - &hip_update_init_id_out, - 10); - hip_register_handle_function(HIP_I2, - HIP_STATE_I2_SENT, - &hip_update_init_id_out, - 10); - hip_register_handle_function(HIP_I2, - HIP_STATE_R2_SENT, - &hip_update_init_id_out, - 10); - hip_register_handle_function(HIP_I2, - HIP_STATE_ESTABLISHED, - &hip_update_init_id_out, - 10); - hip_register_handle_function(HIP_I2, - HIP_STATE_CLOSING, - &hip_update_init_id_out, - 10); - hip_register_handle_function(HIP_I2, - HIP_STATE_CLOSED, - &hip_update_init_id_out, - 10); - hip_register_handle_function(HIP_I2, - HIP_STATE_NONE, - &hip_update_init_id_out, - 10); - - hip_register_handle_function(HIP_R2, - HIP_STATE_UNASSOCIATED, - &hip_update_init_id_out, - 10); - hip_register_handle_function(HIP_R2, - HIP_STATE_I1_SENT, - &hip_update_init_id_out, - 10); - hip_register_handle_function(HIP_R2, - HIP_STATE_I2_SENT, - &hip_update_init_id_out, - 10); - hip_register_handle_function(HIP_R2, - HIP_STATE_R2_SENT, - &hip_update_init_id_out, - 10); - hip_register_handle_function(HIP_R2, - HIP_STATE_ESTABLISHED, - &hip_update_init_id_out, - 10); - hip_register_handle_function(HIP_R2, - HIP_STATE_CLOSING, - &hip_update_init_id_out, - 10); - hip_register_handle_function(HIP_R2, - HIP_STATE_CLOSED, - &hip_update_init_id_out, - 10); - hip_register_handle_function(HIP_R2, - HIP_STATE_NONE, - &hip_update_init_id_out, - 10); + hip_register_state_init_function(&hip_update_init_state); + + hip_register_handle_function(HIP_UPDATE, + HIP_STATE_ESTABLISHED, + &hip_handle_update, + 0); + hip_register_handle_function(HIP_UPDATE, + HIP_STATE_R2_SENT, + &hip_handle_update, + 0); return 0; } @@ -974,22 +889,27 @@ * * Allocates the required memory and sets the members to the start values. * - * @return Success = Pointer to the new data structure - * Error = NULL + * @return Success = Index of the update state item in the global state. (>0) + * Error = -1 */ -struct update_state *hip_update_init_state(void) +int hip_update_init_state(struct modular_state *state) { - struct update_state *state; - - if ((state = (struct update_state*) malloc(sizeof(struct update_state))) == NULL) { - HIP_ERROR("Error on allocating memory for a update_state instance.\n"); - return NULL; - } - state->update_state = 0; - state->hadb_update_func = NULL; - state->addresses_to_send_echo_request = hip_linked_list_init(); - state->update_id_out = 0; - state->update_id_in = 0; - - return state; + int err = 0; + struct update_state *update_state; + + HIP_IFEL((update_state = (struct update_state*) + malloc(sizeof(struct update_state))) == NULL, + -1, + "Error on allocating memory for a update state instance.\n"); + + update_state->update_state = 0; + update_state->hadb_update_func = NULL; + update_state->addresses_to_send_echo_request = hip_linked_list_init(); + update_state->update_id_out = 0; + update_state->update_id_in = 0; + + err = hip_add_state_item(state, update_state, "update"); + +out_err: + return err; } === modified file 'modules/update/hipd/update.h' --- modules/update/hipd/update.h 2010-02-24 13:06:21 +0000 +++ modules/update/hipd/update.h 2010-02-25 15:43:37 +0000 @@ -82,6 +82,6 @@ int hip_update_init(void); -struct update_state *hip_update_init_state(void); +int hip_update_init_state(struct modular_state *state); #endif /* HIP_HIPD_UPDATE_H */