Committer: Tim Just <tim.just@xxxxxxxxxxxxxx> Date: Sat Feb 06 16:10:02 2010 +0100 Revision: 3519 Revision-id: tim.just@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Branch nick: tiny Log: Added source files for libmodularization and small test program. Modified Makefile.am and .bzrignore accordingly. The test routine is disabled, because there appear some strange linker errors otherwise. Need to fix that... Modified: A lib/modularization/ A lib/modularization/modularization.c A lib/modularization/modularization.h A test/modularization.c M .bzrignore M Makefile.am === modified file '.bzrignore' --- .bzrignore 2010-02-05 10:00:48 +0000 +++ .bzrignore 2010-02-06 15:10:02 +0000 @@ -71,3 +71,4 @@ results modules/hipd_modules.h modules/hipfw_modules.h +test/modularization === modified file 'Makefile.am' --- Makefile.am 2010-02-05 07:13:04 +0000 +++ Makefile.am 2010-02-06 15:10:02 +0000 @@ -26,6 +26,7 @@ EXTRA_DIST += $(wildcard lib/conf/*.h) EXTRA_DIST += $(wildcard lib/core/*.h) EXTRA_DIST += $(wildcard lib/ipsec/*.h) +EXTRA_DIST += $(wildcard lib/modularization/*.h) EXTRA_DIST += $(wildcard lib/performance/*.h) EXTRA_DIST += $(wildcard lib/tool/*.h) EXTRA_DIST += $(wildcard hipd/*.h) @@ -74,7 +75,8 @@ ### bin_PROGRAMS ### bin_PROGRAMS = test/certteststub \ test/hc_performance \ - test/auth_performance + test/auth_performance \ + test/modularization if HIP_PERFORMANCE bin_PROGRAMS += test/dh_performance @@ -92,7 +94,8 @@ ### lib_LTLIBRARIES ### lib_LTLIBRARIES = lib/conf/libhipconf.la \ lib/core/libhipcore.la \ - lib/tool/libhiptool.la + lib/tool/libhiptool.la \ + lib/modularization/libmodularization.la if HIP_PFKEY lib_LTLIBRARIES += lib/ipsec/libhipsec.la endif @@ -108,6 +111,8 @@ test_auth_performance_SOURCES = test/auth_performance.c +test_modularization_SOURCES = test/modularization.c + if HIP_PERFORMANCE test_dh_performance_SOURCES = test/dh_performance.c endif @@ -212,6 +217,8 @@ lib/tool/pk.c \ lib/tool/nlink.c \ lib/tool/lutil.c + +lib_modularization_libmodularization_la_SOURCES = lib/modularization/modularization.c if HIP_PFKEY ############################ NOTE ################################## @@ -282,6 +289,9 @@ lib/performance/libperformance.la endif +test_modularization_LDADD = lib/core/libhipcore.la \ + lib/modularization/libmodularization.la + tools_hipconf_LDADD = lib/core/libhipcore.la \ lib/tool/libhiptool.la \ lib/conf/libhipconf.la === added directory 'lib/modularization' === added file 'lib/modularization/modularization.c' --- lib/modularization/modularization.c 1970-01-01 00:00:00 +0000 +++ lib/modularization/modularization.c 2010-02-06 15:10:02 +0000 @@ -0,0 +1,174 @@ +/** @file + * The main source file for libmodularization + * + * @author Tim Just + * @version 0.1 + * @date 04.02.2010 + * @note Distributed under <a href="http://www.gnu.org/licenses/gpl2.txt";>GNU/GPL</a>. + */ +#ifdef HAVE_CONFIG_H + #include <config.h> +#endif /* HAVE_CONFIG_H */ + +#include <stdlib.h> +#include <string.h> + +#include "modularization.h" +#include "lib/core/debug.h" +#include "lib/core/linkedlist.h" + +struct modular_state { + hip_ll_t *item_list; + char **item_names; + unsigned int num_items; +}; + +/** + * hip_init_state + * + * Initializes a new data structure for storage of references to state items. + * This data structure consists of a pointer set and can be mentioned as global + * state. + * + * @return Success = Pointer to the new data structure + * Error = NULL + **/ +void *hip_init_state(void) +{ + struct modular_state *state; + + if ((state = (struct modular_state*) malloc(sizeof(struct modular_state))) == NULL) { + HIP_ERROR("Error on allocating memory for a modular_state instance.\n"); + return NULL; + } + + if ((state->item_list = (hip_ll_t*) malloc(sizeof(hip_ll_t))) == NULL) { + HIP_ERROR("Error on allocating memory for a linked list.\n"); + return NULL; + } + + hip_ll_init(state->item_list); + state->item_names = NULL; + state->num_items = 0; + + return state; +} + +/** + * hip_add_state_item + * + * Registers a new state item to the global state. The state item can be of any + * type. This function stores a reference to the new state item. + * + * Afterwards the state item is retrievable by the provided @c item_name or the + * returned id (unsigned int). + * + * @param state Pointer to the global state. + * @param state_item Pointer to the new state information. + * @param item_name String for retrieving the state item by name. + * @return Success = id (unsigned int) for retrieving the state by number + * Error = -1 + **/ +int hip_add_state_item(void *state, void *state_item, const char *item_name) +{ + struct modular_state *localstate = state; + + /* Check if identifier already exists */ + if (-1 != hip_get_state_item_id(state, item_name)) { + return -1; + } + + hip_ll_add_last(localstate->item_list, state_item); + + localstate->item_names = (char **)realloc(localstate->item_names, + (localstate->num_items + 1) * sizeof(char *)); + + localstate->item_names[localstate->num_items++] = strdup(item_name); + + return localstate->num_items-1; +} + +/** + * hip_get_state_item + * + * Returns a void pointer to a state item from the global state set using + * the string identifier. + * + * @param state Pointer to the global state. + * @param item_name String identifying the state. + * @return Success = Pointer to the requested state item (if exists) + * Error = NULL + **/ +void *hip_get_state_item(void *state, const char *item_name) +{ + unsigned int state_id; + + state_id = hip_get_state_item_id(state, item_name); + + return hip_get_state_item_by_id(state, state_id); +} + +/** + * hip_get_state_item_by_id + * + * Returns a void pointer to a state item from the global state set using + * the id (index number). + * + * @param state Pointer to the global state. + * @param id Index number of the requested state. + * @return Success = Pointer to the requested state item (if exists) + * Error = NULL + **/ +void *hip_get_state_item_by_id(void *state, const unsigned int id) +{ + struct modular_state *localstate = state; + return hip_ll_get(localstate->item_list, id); +} + +/** + * hip_get_state_item_id + * + * Retrieve a void pointer to a state variable from the global state set using + * the state item name. + * + * @param state Pointer to the global state. + * @param item_name String identifying a state. + * @return Success = id (index number) of the state item as unsigned int + * Error = -1 + **/ +int hip_get_state_item_id(void *state, const char *item_name) +{ + unsigned int i; + struct modular_state *localstate = state; + + for (i = 0; i < localstate->num_items; i++) { + if (0 == strcmp(item_name, localstate->item_names[i])) { + return i; + } + } + + return -1; +} + +/** + * hip_free_state + * + * Free all allocated memory for storage of the global state set. + * + * @param state Pointer to the global state. + **/ +void hip_free_state(void *state) +{ + unsigned int i; + struct modular_state *localstate = state; + + hip_ll_uninit(localstate->item_list, NULL); + free(localstate->item_list); + + for (i = 0; i < localstate->num_items; i++) { + free(localstate->item_names[i]); + } + + free(localstate->item_names); + free(localstate); +} === added file 'lib/modularization/modularization.h' --- lib/modularization/modularization.h 1970-01-01 00:00:00 +0000 +++ lib/modularization/modularization.h 2010-02-06 15:10:02 +0000 @@ -0,0 +1,23 @@ +/** @file + * The header file for libmodularization + * + * @author Tim Just + * @version 0.1 + * @date 04.02.2010 + * @note Distributed under <a href="http://www.gnu.org/licenses/gpl2.txt";>GNU/GPL</a>. + */ +#ifndef HIP_MODULARIZATION_H +#define HIP_MODULARIZATION_H + +#ifdef HAVE_CONFIG_H + #include "config.h" +#endif /* HAVE_CONFIG_H */ + +void *hip_init_state(void); +int hip_add_state_item(void *state, void *state_item, const char *item_name); +void *hip_get_state_item(void *state, const char *item_name); +void *hip_get_state_item_by_id(void *state, const unsigned int index); +int hip_get_state_item_id(void *state, const char *item_name); +void hip_free_state(void *state); + +#endif /* HIP_MODULARIZATION_H */ === added file 'test/modularization.c' --- test/modularization.c 1970-01-01 00:00:00 +0000 +++ test/modularization.c 2010-02-06 15:10:02 +0000 @@ -0,0 +1,87 @@ +/** @file + * A small test program for the modularization lib. + * + * @brief Runs various tests in the data structures and functions provided by + * libmodularization. + * + * @author Tim Just + * @version 0.1 + * @date 04.02.2010 + * @note Distributed under <a href="http://www.gnu.org/licenses/gpl2.txt";>GNU/GPL</a>. + */ +#include <stdio.h> + +#ifdef HAVE_CONFIG_H + #include "config.h" +#endif /* HAVE_CONFIG_H */ + +#include "lib/modularization/modularization.h" + +struct ha_state { + int id; + char name[10]; +}; + +struct update_state { + int id; + char name[10]; +}; + +int main(void) +{ +#if 0 + void *tmp; + tmp = hip_init_state(); + struct ha_state ha_state_item1; + struct update_state update_state_item1; + struct ha_state ha_state_item2; + struct update_state update_state_item2; + void *entry1; + void *entry2; + void *tmp; + struct update_state *tmp2; + + 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(); + + hip_add_state_item(entry1, &ha_state_item1, "ha"); + hip_add_state_item(entry1, &update_state_item1, "update"); + + hip_add_state_item(entry2, &ha_state_item2, "ha"); + hip_add_state_item(entry2, &update_state_item2, "update"); + + 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); +#endif + return 0; +}