Committer: Tim Just <tim.just@xxxxxxxxxxxxxx> Date: Fri Feb 19 19:05:57 2010 +0100 Revision: 3539 Revision-id: tim.just@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Branch nick: tiny Log: Added basic functionality for dynamic registration of handle functions. Modified: M lib/core/protodefs.h M lib/core/state.h M lib/modularization/modularization.c M lib/modularization/modularization.h M test/modularization.c === modified file 'lib/core/protodefs.h' --- lib/core/protodefs.h 2010-02-17 16:39:54 +0000 +++ lib/core/protodefs.h 2010-02-19 18:05:57 +0000 @@ -47,6 +47,11 @@ /* only hip network message types here */ /* @} */ +/** + * @todo add description + */ +#define HIP_MAX_PACKET_TYPE 64 + #define HIP_HIT_TYPE_HASH100 1 #define HIP_HIT_TYPE_MASK_100 0x20010010 #define HIP_TEREDO_TYPE_MASK_100 0x20010000 === modified file 'lib/core/state.h' --- lib/core/state.h 2010-02-18 14:18:07 +0000 +++ lib/core/state.h 2010-02-19 18:05:57 +0000 @@ -44,6 +44,11 @@ #define HIP_STATE_CLOSED 9 /* @} */ +/** + * @todo add description + */ +#define HIP_MAX_HA_STATE 16 + #define HIP_UPDATE_STATE_REKEYING 1 /**< @todo REMOVE */ #define HIP_UPDATE_STATE_DEPRECATING 2 === modified file 'lib/modularization/modularization.c' --- lib/modularization/modularization.c 2010-02-18 08:46:16 +0000 +++ lib/modularization/modularization.c 2010-02-19 18:05:57 +0000 @@ -13,6 +13,97 @@ #include "modularization.h" #include "lib/core/debug.h" +#include "lib/core/protodefs.h" +#include "lib/core/state.h" + +/** + * @todo add description + */ +static hip_ll_t *handle_functions[HIP_MAX_PACKET_TYPE][HIP_MAX_HA_STATE]; + +/** + * hip_register_handle_function + * + * Register a function for handling of the specified combination from packet + * type and host association state. + * + * @param packet_type + * @param ha_state + * @param *handle_function + * + * @return Success = 0 + * Error = -1 + **/ +int hip_register_handle_function(uint32_t packet_type, + uint32_t ha_state, + void *handle_function) +{ + int err = 0; + hip_ll_t *newlist = NULL; + + HIP_IFE(packet_type > HIP_MAX_PACKET_TYPE, -1); + HIP_IFE(ha_state > HIP_MAX_HA_STATE, -1); + + if (!handle_functions[packet_type][ha_state]) { + if ((newlist = (hip_ll_t *) malloc(sizeof(hip_ll_t))) == NULL) { + HIP_ERROR("Error on allocating memory for a linked list.\n"); + return -1; + } + hip_ll_init(newlist); + handle_functions[packet_type][ha_state] = newlist; + } + + err = hip_ll_add_last(handle_functions[packet_type][ha_state], + handle_function); +out_err: + return err; +} + +/** + * @todo add description + */ +int hip_run_handle_functions(uint32_t packet_type, + uint32_t ha_state, + struct hip_packet_context *ctx) +{ + int err = 0; + hip_ll_t *list = NULL; + hip_ll_node_t *iter = NULL; + int (*handle_func)(uint32_t packet_type, + uint32_t ha_state, + struct hip_packet_context *ctx) = NULL; + + HIP_IFE(packet_type > HIP_MAX_PACKET_TYPE, -1); + HIP_IFE(ha_state > HIP_MAX_HA_STATE, -1); + + list = handle_functions[packet_type][ha_state]; + + while((iter = hip_ll_iterate(list, iter)) != NULL) { + handle_func = iter->ptr; + + handle_func(packet_type, ha_state, ctx); + } + +out_err: + return err; +} + +/** + * @todo add description + */ +void hip_uninit_handle_functions(void) +{ + int i,j; + + for (i = 0; i < HIP_MAX_PACKET_TYPE; i++) { + for (j = 0; j < HIP_MAX_HA_STATE; j++) { + if (handle_functions[i][j]) { + hip_ll_uninit(handle_functions[i][j], NULL); + free(handle_functions[i][j]); + } + } + } +} /** * hip_init_state === modified file 'lib/modularization/modularization.h' --- lib/modularization/modularization.h 2010-02-19 17:03:23 +0000 +++ lib/modularization/modularization.h 2010-02-19 18:05:57 +0000 @@ -11,14 +11,37 @@ #ifndef HIP_LIB_MODULARIZATION_MODULARIZATION_H #define HIP_LIB_MODULARIZATION_MODULARIZATION_H +#include <stdint.h> + #include "lib/core/linkedlist.h" +/** + * @todo add description + */ +struct hip_packet_context { + + +}; + +/** + * @todo add description + */ struct modular_state { hip_ll_t *item_list; char **item_names; unsigned int num_items; }; +int hip_register_handle_function(uint32_t packet_type, + uint32_t ha_state, + void *handle_function); + +int hip_run_handle_functions(uint32_t packet_type, + uint32_t ha_state, + struct hip_packet_context *ctx); + +void hip_uninit_handle_functions(void); + struct modular_state *hip_init_state(void); int hip_add_state_item(struct modular_state *state, === modified file 'test/modularization.c' --- test/modularization.c 2010-02-18 08:46:16 +0000 +++ test/modularization.c 2010-02-19 18:05:57 +0000 @@ -10,6 +10,8 @@ * */ #include <stdio.h> +#include <malloc.h> +#include <sys/types.h> #include "lib/modularization/modularization.h" @@ -23,27 +25,34 @@ char name[10]; }; -int main(void) + +static int test_function(void) +{ + printf("test_function\n"); + return 0; +} + +static int test_function2(void) +{ + printf("test_function2\n"); + return 0; +} + +/* +int test_modular_state(void) { struct ha_state ha_state_item1; struct update_state update_state_item1; struct ha_state ha_state_item2; struct update_state update_state_item2; struct modular_state *entry1, *entry2; - void *tmp; - struct update_state *tmp2; + void *tmp = NULL; ha_state_item1.id = 11; -// ha_state_item1.name = {"h", "a", "1"}; - update_state_item1.id = 12; -// update_state_item1.name = {"u", ,"p", "1"}; ha_state_item2.id = 21; -// ha_state_item2.name = {"h", "a", "2"}; - update_state_item2.id = 22; -// update_state_item2.name = {"u", "p","2"}; entry1 = hip_init_state(); entry2 = hip_init_state(); @@ -56,24 +65,34 @@ printf("update_state1: %p\n", hip_get_state_item(entry1, "update")); tmp = hip_get_state_item(entry1, "update"); - tmp2 = tmp; - printf("id update 1: %d\n", tmp2->id); - //printf("update_state1: %p\n", &update_state_item); printf("ha_state2: %p\n", hip_get_state_item(entry1, "ha")); - //printf("ha_state2: %p\n", &ha_state_item); - printf("\n"); - printf("update_state2: %p\n", hip_get_state_item(entry2, "update")); tmp = hip_get_state_item(entry2, "update"); - tmp2 = tmp; - printf("id update 2: %d\n", tmp2->id); -// printf("update state: %p\n", &ha_state_item); printf("ha_state2: %p\n", hip_get_state_item(entry2, "ha")); - //printf("ha state: %p\n", &update_state_item); hip_free_state(entry1); hip_free_state(entry2); return 0; } +*/ + +int test_handle_functions(void) +{ + hip_register_handle_function(1, 1, &test_function); + hip_register_handle_function(1, 1, &test_function2); + + hip_run_handle_functions(1, 1, NULL); + + hip_uninit_handle_functions(); + + return 0; +} + +int main(void) +{ + test_handle_functions(); + + return 0; +}