new mavl is working now
FossilOrigin-Name: 65a4eefffea57509c0d7fd573e53b15bf4a730ce297b7db182aaac796ea92448
This commit is contained in:
parent
e4d65ef470
commit
4171d208d0
@ -25,6 +25,8 @@
|
||||
</VirtualDirectory>
|
||||
<Description/>
|
||||
<Dependencies/>
|
||||
<Dependencies Name="Debug"/>
|
||||
<Dependencies Name="Release"/>
|
||||
<Settings Type="Executable">
|
||||
<GlobalSettings>
|
||||
<Compiler Options="" C_Options="" Assembler="">
|
||||
@ -43,7 +45,7 @@
|
||||
<ResourceCompiler Options="" Required="no"/>
|
||||
<General OutputFile="" IntermediateDirectory="./" Command="$(WorkspacePath)/src/ac/actube" CommandArguments="" UseSeparateDebugArgs="no" DebugArguments="" WorkingDirectory="$(WorkspacePath)/src/ac/" PauseExecWhenProcTerminates="yes" IsGUIProgram="no" IsEnabled="yes"/>
|
||||
<Environment EnvVarSetName="<Use Defaults>" DbgSetName="<Use Defaults>">
|
||||
<![CDATA[]]>
|
||||
<![CDATA[LD_LIBRARY_PATH=../../lib]]>
|
||||
</Environment>
|
||||
<Debugger IsRemote="no" RemoteHostName="" RemoteHostPort="" DebuggerPath="" IsExtended="no">
|
||||
<DebuggerSearchPaths/>
|
||||
@ -112,6 +114,4 @@
|
||||
</Completion>
|
||||
</Configuration>
|
||||
</Settings>
|
||||
<Dependencies Name="Debug"/>
|
||||
<Dependencies Name="Release"/>
|
||||
</CodeLite_Project>
|
||||
|
@ -1,11 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<CodeLite_Workspace Name="actube" Database="">
|
||||
<Project Name="ac" Path="ac.project" Active="No"/>
|
||||
<Project Name="ac" Path="ac.project" Active="Yes"/>
|
||||
<Project Name="wtp" Path="wtp.project" Active="No"/>
|
||||
<Project Name="mod_cipwap" Path="mod_cipwap.project" Active="No"/>
|
||||
<Project Name="mod_capwap" Path="mod_capwap.project" Active="No"/>
|
||||
<Project Name="mod_cisco" Path="mod_cisco.project" Active="No"/>
|
||||
<Project Name="libcw" Path="libcw.project" Active="Yes"/>
|
||||
<Project Name="libcw" Path="libcw.project" Active="No"/>
|
||||
<Project Name="mod_capwap80211" Path="mod_capwap80211.project" Active="No"/>
|
||||
<Project Name="mod_fortinet" Path="mod_fortinet.project" Active="No"/>
|
||||
<BuildMatrix>
|
||||
|
@ -224,7 +224,6 @@
|
||||
<File Name="src/cw/fragman.c"/>
|
||||
<File Name="src/cw/lwmsg.h"/>
|
||||
<File Name="src/cw/cw_check_missing_mand.c"/>
|
||||
<File Name="src/cw/cw_strnelem.c"/>
|
||||
<File Name="src/cw/format_dot11_fc.c"/>
|
||||
<File Name="src/cw/wtpinfo_lwreadelem_wtp_descriptor.c"/>
|
||||
<File Name="src/cw/dbg_strings.c"/>
|
||||
@ -289,8 +288,6 @@
|
||||
<File Name="src/cw/capwap_strings_state.c"/>
|
||||
<File Name="src/cw/cw_out_radio_info.c"/>
|
||||
<File Name="src/cw/capwap_items.c"/>
|
||||
<File Name="src/cw/message_set.c"/>
|
||||
<File Name="src/cw/message_set.h"/>
|
||||
<File Name="src/mod/cisco/mod_cisco.h"/>
|
||||
<File Name="src/cw/cw_filename.c"/>
|
||||
<File Name="src/cw/mdata.h"/>
|
||||
@ -298,33 +295,33 @@
|
||||
<File Name="src/cw/mdata_elem_new.c"/>
|
||||
<File Name="src/cw/cw_type_byte.c"/>
|
||||
<File Name="src/cw/cw_type_word.c"/>
|
||||
<File Name="src/cw/cw_types.h"/>
|
||||
<File Name="src/cw/cw_type_dword.c"/>
|
||||
<File Name="src/cw/mavl_cmp_dword.c"/>
|
||||
<File Name="src/cw/mavl_free_bin.c"/>
|
||||
<File Name="src/cw/debug.h"/>
|
||||
<File Name="src/cw/debug.c"/>
|
||||
<File Name="src/cw/mavl_get_ptr.c"/>
|
||||
<File Name="src/cw/mavl_add_strdup.c"/>
|
||||
<File Name="src/cw/mavl_cmp_str.c"/>
|
||||
<File Name="src/cw/mavliter_init.c"/>
|
||||
<File Name="src/cw/mavl_add_ptr.c"/>
|
||||
<File Name="src/cw/cw_format_dump.c"/>
|
||||
<File Name="src/cw/cw_format_pkt_hdr.c"/>
|
||||
<File Name="src/cw/mavl_cmp_kv.c"/>
|
||||
<File Name="src/cw/mavl_add_kv_byte.c"/>
|
||||
<File Name="src/cw/cw_types_mavl_delete.c"/>
|
||||
<File Name="src/cw/cw_format_scan_hex_bytes.c"/>
|
||||
<File Name="src/cw/cw_dbg_elem.c"/>
|
||||
<File Name="src/cw/cw_kvstore_mavl_delete.c"/>
|
||||
<File Name="src/cw/cw_format_version.c"/>
|
||||
<File Name="src/cw/cw_strdup.c"/>
|
||||
<File Name="src/cw/kvstore.h"/>
|
||||
<File Name="src/cw/cw_kvstore_add.c"/>
|
||||
<File Name="src/cw/cw_types_del_null.c"/>
|
||||
<File Name="src/cw/cw_type_bstr16.c"/>
|
||||
<File Name="src/cw/cw_process_element.c"/>
|
||||
<File Name="src/cw/cw_result_is_ok.c"/>
|
||||
<File Name="src/cw/mavliter_get.c"/>
|
||||
<File Name="src/cw/kvt.h"/>
|
||||
<File Name="src/cw/cw_kvt_mavlcmp.c"/>
|
||||
<File Name="src/cw/cw_kvt_mavldel.c"/>
|
||||
<File Name="src/cw/mavliter_get_ptr.c"/>
|
||||
<File Name="src/cw/mavl_cmpstr.c"/>
|
||||
<File Name="src/cw/mavl_freeptr.c"/>
|
||||
<File Name="src/cw/msgset.c"/>
|
||||
<File Name="src/cw/msget.h"/>
|
||||
<File Name="src/cw/mavl_replace.c"/>
|
||||
<File Name="src/cw/cw_kvt_add.c"/>
|
||||
</VirtualDirectory>
|
||||
</VirtualDirectory>
|
||||
<Description/>
|
||||
|
742
src/ac/ac_main.c
742
src/ac/ac_main.c
@ -52,208 +52,367 @@
|
||||
int ac_run();
|
||||
|
||||
|
||||
static void *alive_thread(void *data)
|
||||
static void *alive_thread (void *data)
|
||||
{
|
||||
/* Ping the database every 5 seconds */
|
||||
while (1) {
|
||||
sleep(5);
|
||||
sleep (5);
|
||||
db_ping();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#include <getopt.h>
|
||||
static int parse_args(int argc, char *argv[])
|
||||
static int parse_args (int argc, char *argv[])
|
||||
{
|
||||
// int getopt_ret;
|
||||
int option_index;
|
||||
|
||||
|
||||
static struct option long_options[] = {
|
||||
{"version", optional_argument, NULL, 'v'},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
int o;
|
||||
while ((o = getopt_long(argc, argv, "v", long_options, &option_index)) != -1) {
|
||||
|
||||
while ( (o = getopt_long (argc, argv, "v", long_options, &option_index)) != -1) {
|
||||
switch (o) {
|
||||
case 0:
|
||||
break;
|
||||
|
||||
case 'v':
|
||||
printf("AC-Tube 0.01, %s\n", SYS_ARCH);
|
||||
exit(0);
|
||||
printf ("AC-Tube 0.01, %s\n", SYS_ARCH);
|
||||
exit (0);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
#include "cw/mod.h"
|
||||
extern struct cw_Mod * cw_get_mod_ac(const char *name);
|
||||
extern struct cw_Mod * cw_get_mod_ac (const char *name);
|
||||
|
||||
#include "cw/mlist.h"
|
||||
|
||||
|
||||
extern void test_sets();
|
||||
#include "cw/file.h"
|
||||
#include "cw/cw_types.h"
|
||||
#include "cw/kvt.h"
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
struct mykv {
|
||||
int key;
|
||||
int value;
|
||||
};
|
||||
|
||||
int mycmp (void *e1, void *e2)
|
||||
{
|
||||
/* mavl_t kv;
|
||||
mavliter_t kviter;
|
||||
uint8_t bytes[] = { 99,4,5,7 };
|
||||
struct mykv * k1 = e1;
|
||||
struct mykv * k2 = e2;
|
||||
|
||||
//printf("My CMP: %d and %d\n", k1->key, k2->key);
|
||||
|
||||
kv = cw_types_mavl_create();
|
||||
//mavl_cmp_kv,cw_type_delete);
|
||||
|
||||
mavldata_t data, *result;
|
||||
|
||||
data.kv.key="wtp_board_data";
|
||||
result = cw_type_byte.get(&data,bytes,1);
|
||||
mavl_add(kv,result);
|
||||
|
||||
data.kv.key="wtp_next_data";
|
||||
result = cw_type_byte.get(&data,bytes+1,1);
|
||||
mavl_add(kv,result);
|
||||
|
||||
|
||||
mavliter_init(&kviter,kv);
|
||||
mavliter_foreach(&kviter){
|
||||
char value[500];
|
||||
mavldata_t * data;
|
||||
data = mavliter_get(&kviter);
|
||||
struct cw_Type * type = data->kv.priv;
|
||||
type->to_str(data,value,0);
|
||||
if (k1->key > k2->key)
|
||||
return 1;
|
||||
|
||||
printf("Got %s (%s): %s\n",data->kv.key,type->name, value);
|
||||
}
|
||||
|
||||
mavl_destroy(kv);
|
||||
exit(0);
|
||||
*/
|
||||
|
||||
|
||||
/* cw_log_init();
|
||||
cw_log(LOG_ERROR,"Hello Debug World222");
|
||||
exit(0);
|
||||
*/
|
||||
/*
|
||||
char * dump_data = "Eine kleine Mickey Maus zog sich mal die Hosen\
|
||||
aus, zog sie wieder an, und du bist dran. Dran bist du noch lange nicht\
|
||||
musst erst sagen, wie alt du bist.";
|
||||
|
||||
char * result = cw_format_dump(dump_data,strlen(dump_data),NULL);
|
||||
|
||||
printf("Dump Result:\n%s\n", result);
|
||||
exit(0);
|
||||
*/
|
||||
|
||||
/* char data[100];
|
||||
mavl_t im;
|
||||
mavldata_t * val, * val2,itt, itt2, * result, search ;
|
||||
mavliter_t myit;
|
||||
|
||||
im = mavl_create(mavl_cmp_dword,NULL);
|
||||
itt.dword = 7;
|
||||
itt2.dword = 7;
|
||||
|
||||
|
||||
val = mavl_add(im,&itt);
|
||||
val2 = mavl_add(im,&itt2);
|
||||
|
||||
search.dword = 7;
|
||||
result = mavl_get(im,&search);
|
||||
|
||||
printf("Result: (%p, %p) (%p, %p) (%p, %p)\n",val, &itt, val2, &itt2, result, &search);
|
||||
|
||||
exit(0);
|
||||
|
||||
|
||||
for (itt.dword=100; itt.dword>0; itt.dword--){
|
||||
printf("Copunting: %d\n",itt.dword);
|
||||
mavl_add(im,&itt);
|
||||
}
|
||||
|
||||
mavliter_init(&myit,im);
|
||||
mavliter_foreach(&myit){
|
||||
union mavldata * result;
|
||||
result = mavliter_get(&myit);
|
||||
printf("Got Value %d\n",result->dword);
|
||||
}
|
||||
if (k1->key < k2->key)
|
||||
return -1;
|
||||
|
||||
mavl_destroy(im);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void mavl_walk (struct mavlnode * node)
|
||||
{
|
||||
struct mykv * kv;
|
||||
|
||||
if (!node) {
|
||||
printf ("\n");
|
||||
return;
|
||||
}
|
||||
|
||||
kv = mavlnode_dataptr (node);
|
||||
printf ("KV %d\n", kv->key);
|
||||
|
||||
printf ("go left:");
|
||||
mavl_walk (node->left);
|
||||
printf ("go right:");
|
||||
mavl_walk (node->right);
|
||||
|
||||
}
|
||||
|
||||
int stcmp (const void * sa1, const void *sa2)
|
||||
{
|
||||
const char **s1 = sa1;
|
||||
const char **s2 = sa2;
|
||||
int rc;
|
||||
rc = strcmp (*s1, *s2);
|
||||
|
||||
printf ("CMP %s, %s = %d\n", *s1, *s2, rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
int main (int argc, char *argv[])
|
||||
{
|
||||
mavl_t tree;
|
||||
int i;
|
||||
mavliter_t it;
|
||||
|
||||
|
||||
/*tree = mavl_create_ptr( (int(*)(const void*,const void*))strcmp,free);
|
||||
*/
|
||||
|
||||
/*
|
||||
tree = mavl_create_ptr(mycmp,NULL);
|
||||
|
||||
struct mykv * mdata;
|
||||
|
||||
|
||||
mdata = malloc(sizeof(struct mykv));
|
||||
mdata->key=14;
|
||||
mdata->value=15;
|
||||
mavl_add_ptr(tree, mdata);
|
||||
|
||||
mdata = malloc(sizeof(struct mykv));
|
||||
mdata->key=16;
|
||||
mdata->value=17;
|
||||
mavl_add_ptr(tree, mdata);
|
||||
|
||||
mdata = malloc(sizeof(struct mykv));
|
||||
mdata->key=12;
|
||||
mdata->value=13;
|
||||
mavl_add_ptr(tree, mdata);
|
||||
|
||||
mavliter_init(&it,tree);
|
||||
mavliter_foreach(&it){
|
||||
mdata = mavliter_get_ptr(&it);
|
||||
printf("MAVLIT: %d %d\n",mdata->key,mdata->value);
|
||||
|
||||
struct mykv search;
|
||||
search.key=mdata->key;
|
||||
mdata = mavl_get_ptr(tree,&search);
|
||||
printf("Search T: %d %d\n",mdata->key,mdata->value);
|
||||
}
|
||||
|
||||
exit(0);
|
||||
*/
|
||||
|
||||
/*
|
||||
if (tree==NULL)
|
||||
return 0;
|
||||
|
||||
tree = mavl_create_str();
|
||||
|
||||
mavl_add_str(tree,cw_strdup("7u83"));
|
||||
mavl_add_str(tree,cw_strdup("Herre"));
|
||||
mavl_add_str(tree,cw_strdup("Tube"));
|
||||
mavl_add_str(tree,cw_strdup("Tobias"));
|
||||
printf("Count: %d\n", mavl_get_count(tree));
|
||||
mavliter_init(&it,tree);
|
||||
|
||||
mavliter_foreach(&it){
|
||||
|
||||
printf("iterWert %s\n",mavliter_get_str(&it));
|
||||
}
|
||||
*/
|
||||
/*
|
||||
tree = mavl_create (mycmp, NULL, sizeof (struct mykv));
|
||||
|
||||
struct mykv nwert;
|
||||
int result;
|
||||
struct mykv * mr;
|
||||
|
||||
nwert.key = 7;
|
||||
nwert.value = 777;
|
||||
mr = mavl_add_exists (tree, &nwert, &result);
|
||||
printf ("7Result %d: %d %d\n", result, mr->key, mr->value);
|
||||
|
||||
for (i = 0; i < 10; i++) {
|
||||
//cw_rand(&nwert.key,sizeof(nwert.key));
|
||||
// printf("Addwert: %u\n",nwert.key);
|
||||
// printf("add %u\n", i);
|
||||
nwert.key = 9 - i;
|
||||
nwert.value = 100 + i;
|
||||
result=0;
|
||||
mr = mavl_add_exists (tree, &nwert, &result);
|
||||
printf ("Result %d: %d %d\n", result, mr->key, mr->value);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
mavliter_init (&it, tree);
|
||||
|
||||
mavliter_foreach (&it) {
|
||||
struct mykv *kv;
|
||||
kv = mavliter_get (&it);
|
||||
printf ("iterWert %u\n", kv->key);
|
||||
}
|
||||
|
||||
exit (0);
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* mavl_t kv;
|
||||
mavliter_t kviter;
|
||||
uint8_t bytes[] = { 99,4,5,7 };
|
||||
|
||||
|
||||
kv = cw_types_mavl_create();
|
||||
//mavl_cmp_kv,cw_type_delete);
|
||||
|
||||
mavldata_t data, *result;
|
||||
|
||||
data.kv.key="wtp_board_data";
|
||||
result = cw_type_byte.get(&data,bytes,1);
|
||||
mavl_add(kv,result);
|
||||
|
||||
data.kv.key="wtp_next_data";
|
||||
result = cw_type_byte.get(&data,bytes+1,1);
|
||||
mavl_add(kv,result);
|
||||
|
||||
|
||||
mavliter_init(&kviter,kv);
|
||||
mavliter_foreach(&kviter){
|
||||
char value[500];
|
||||
mavldata_t * data;
|
||||
data = mavliter_get(&kviter);
|
||||
struct cw_Type * type = data->kv.priv;
|
||||
type->to_str(data,value,0);
|
||||
|
||||
printf("Got %s (%s): %s\n",data->kv.key,type->name, value);
|
||||
}
|
||||
|
||||
mavl_destroy(kv);
|
||||
exit(0);
|
||||
*/
|
||||
|
||||
|
||||
/* cw_log_init();
|
||||
cw_log(LOG_ERROR,"Hello Debug World222");
|
||||
exit(0);
|
||||
*/
|
||||
/*
|
||||
char * dump_data = "Eine kleine Mickey Maus zog sich mal die Hosen\
|
||||
aus, zog sie wieder an, und du bist dran. Dran bist du noch lange nicht\
|
||||
musst erst sagen, wie alt du bist.";
|
||||
|
||||
char * result = cw_format_dump(dump_data,strlen(dump_data),NULL);
|
||||
|
||||
printf("Dump Result:\n%s\n", result);
|
||||
exit(0);
|
||||
*/
|
||||
|
||||
/* char data[100];
|
||||
mavl_t im;
|
||||
mavldata_t * val, * val2,itt, itt2, * result, search ;
|
||||
mavliter_t myit;
|
||||
|
||||
im = mavl_create(mavl_cmp_dword,NULL);
|
||||
itt.dword = 7;
|
||||
itt2.dword = 7;
|
||||
|
||||
|
||||
val = mavl_add(im,&itt);
|
||||
val2 = mavl_add(im,&itt2);
|
||||
|
||||
search.dword = 7;
|
||||
result = mavl_get(im,&search);
|
||||
|
||||
printf("Result: (%p, %p) (%p, %p) (%p, %p)\n",val, &itt, val2, &itt2, result, &search);
|
||||
|
||||
exit(0);
|
||||
|
||||
|
||||
for (itt.dword=100; itt.dword>0; itt.dword--){
|
||||
printf("Copunting: %d\n",itt.dword);
|
||||
mavl_add(im,&itt);
|
||||
}
|
||||
|
||||
mavliter_init(&myit,im);
|
||||
mavliter_foreach(&myit){
|
||||
union mavldata * result;
|
||||
result = mavliter_get(&myit);
|
||||
printf("Got Value %d\n",result->dword);
|
||||
}
|
||||
|
||||
mavl_destroy(im);
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
int rc = 0;
|
||||
|
||||
|
||||
/* parse arguments */
|
||||
parse_args(argc, argv);
|
||||
|
||||
parse_args (argc, argv);
|
||||
|
||||
cw_log_name = "AC-Tube";
|
||||
|
||||
if (!read_config("ac.conf"))
|
||||
|
||||
if (!read_config ("ac.conf"))
|
||||
return 1;
|
||||
|
||||
|
||||
|
||||
|
||||
/* Show debug options if there are any set */
|
||||
if (cw_dbg_opt_level)
|
||||
cw_log(LOG_INFO, "Debug Options: %08X", cw_dbg_opt_level);
|
||||
|
||||
cw_log (LOG_INFO, "Debug Options: %08X", cw_dbg_opt_level);
|
||||
|
||||
/* XXX Hard coded debug settigns, set it by config in the future */
|
||||
cw_dbg_opt_display = DBG_DISP_ASC_DMP | DBG_DISP_COLORS;
|
||||
|
||||
/* Warn, if the "secret" debugging feature for
|
||||
|
||||
/* Warn, if the "secret" debugging feature for
|
||||
developers is turned on ;) */
|
||||
// DBGX("Attention! %s", "DBG X is ON!");
|
||||
|
||||
// cw_mod_set_mod_path("../../lib/actube");
|
||||
//cw_mod_load("capwap");
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* Initialize the database */
|
||||
if (!db_init())
|
||||
goto errX;
|
||||
|
||||
|
||||
/* Start the database */
|
||||
if (!db_start())
|
||||
goto errX;
|
||||
|
||||
|
||||
db_ping();
|
||||
|
||||
|
||||
|
||||
|
||||
/* Start a database "pinger thread", which inserts
|
||||
every xx seconds a timestamp into the DB */
|
||||
pthread_t alth;
|
||||
pthread_create(&alth, NULL, alive_thread, NULL);
|
||||
|
||||
pthread_create (&alth, NULL, alive_thread, NULL);
|
||||
|
||||
/* Init DTLS library */
|
||||
dtls_init();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
ac_global_init();
|
||||
|
||||
if (!socklist_init())
|
||||
goto errX;
|
||||
|
||||
|
||||
if (!wtplist_init())
|
||||
goto errX;
|
||||
|
||||
|
||||
if (!dataman_list_init())
|
||||
goto errX;
|
||||
|
||||
|
||||
cw_log(LOG_INFO, "Starting AC-Tube, Name=%s, ID=%s", conf_acname, conf_acid);
|
||||
|
||||
|
||||
cw_log (LOG_INFO, "Starting AC-Tube, Name=%s, ID=%s", conf_acname, conf_acid);
|
||||
rc = ac_run();
|
||||
errX:
|
||||
errX:
|
||||
/* XXX There is more cleanup to do */
|
||||
wtplist_destroy();
|
||||
socklist_destroy();
|
||||
@ -266,297 +425,315 @@ int main(int argc, char *argv[])
|
||||
|
||||
|
||||
|
||||
void process_ctrl_packet(int index, struct sockaddr *addr, uint8_t * buffer, int len);
|
||||
void process_ctrl_packet (int index, struct sockaddr *addr, uint8_t * buffer, int len);
|
||||
|
||||
void process_cw_data_packet(int index, struct sockaddr *addr, uint8_t * buffer, int len);
|
||||
void process_cw_data_packet (int index, struct sockaddr *addr, uint8_t * buffer, int len);
|
||||
|
||||
|
||||
int ac_run()
|
||||
{
|
||||
|
||||
if (!conf_listen_addrs_len) {
|
||||
cw_log(LOG_ERR, "Fatal error: No listen addresses found.");
|
||||
cw_log (LOG_ERR, "Fatal error: No listen addresses found.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* it is important to create the unicast sockets first,
|
||||
* because when we create the mcast an bcast sockets next
|
||||
|
||||
|
||||
|
||||
/* it is important to create the unicast sockets first,
|
||||
* because when we create the mcast an bcast sockets next
|
||||
* we will look for already created sockets to find a
|
||||
* good unicast reply socket */
|
||||
|
||||
|
||||
int i;
|
||||
|
||||
for (i = 0; i < conf_listen_addrs_len; i++) {
|
||||
char addr[100];
|
||||
char port[50];
|
||||
int proto;
|
||||
|
||||
conf_parse_listen_addr(conf_listen_addrs[i], addr, port, &proto);
|
||||
|
||||
|
||||
|
||||
conf_parse_listen_addr (conf_listen_addrs[i], addr, port, &proto);
|
||||
|
||||
|
||||
//struct sockaddr sa;
|
||||
|
||||
|
||||
socklist_add_unicast(addr, port,proto);
|
||||
|
||||
|
||||
|
||||
socklist_add_unicast (addr, port, proto);
|
||||
|
||||
//#ifdef WITH_LWAPP
|
||||
// if (conf_lwapp)
|
||||
// socklist_add_unicast(conf_listen_addrs[i], conf_lw_control_port,
|
||||
// AC_PROTO_LWAPP);
|
||||
//#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (socklist_len == 0) {
|
||||
cw_log(LOG_ERR, "Fatal error: Could not setup any listen socket");
|
||||
cw_log (LOG_ERR, "Fatal error: Could not setup any listen socket");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/* create multicast sockets */
|
||||
for (i = 0; i < conf_mcast_groups_len; i++) {
|
||||
|
||||
socklist_add_multicast(conf_mcast_groups[i], conf_control_port,
|
||||
AC_PROTO_CAPWAP);
|
||||
|
||||
socklist_add_multicast (conf_mcast_groups[i], conf_control_port,
|
||||
AC_PROTO_CAPWAP);
|
||||
#ifdef WITH_LWAPP
|
||||
|
||||
if (conf_lwapp)
|
||||
socklist_add_multicast(conf_mcast_groups[i], conf_lw_control_port,
|
||||
AC_PROTO_LWAPP);
|
||||
socklist_add_multicast (conf_mcast_groups[i], conf_lw_control_port,
|
||||
AC_PROTO_LWAPP);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/* broadcast socket ipv4 only */
|
||||
for (i = 0; i < conf_bcast_addrs_len; i++) {
|
||||
|
||||
char addr[50],port[50];
|
||||
|
||||
char addr[50], port[50];
|
||||
int proto;
|
||||
conf_parse_listen_addr(conf_bcast_addrs[i], addr, port, &proto);
|
||||
|
||||
socklist_add_broadcast(addr,port,proto);
|
||||
conf_parse_listen_addr (conf_bcast_addrs[i], addr, port, &proto);
|
||||
|
||||
socklist_add_broadcast (addr, port, proto);
|
||||
#ifdef WITH_LWAPP
|
||||
|
||||
// printf("Adding %d\n",socklist_len);
|
||||
if (conf_lwapp)
|
||||
socklist_add_broadcast(conf_bcast_addrs[i], conf_lw_control_port,
|
||||
AC_PROTO_LWAPP);
|
||||
socklist_add_broadcast (conf_bcast_addrs[i], conf_lw_control_port,
|
||||
AC_PROTO_LWAPP);
|
||||
|
||||
// printf ("SI %d, PROTO: %d\n",socklist_len-1,socklist[socklist_len-1].ac_proto);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//get_acinfo();
|
||||
|
||||
|
||||
|
||||
|
||||
while (1) {
|
||||
|
||||
|
||||
/* prepare fdset */
|
||||
fd_set fset;
|
||||
int max = 0;
|
||||
FD_ZERO(&fset);
|
||||
FD_ZERO (&fset);
|
||||
|
||||
for (i = 0; i < socklist_len; i++) {
|
||||
FD_SET(socklist[i].sockfd, &fset);
|
||||
FD_SET (socklist[i].sockfd, &fset);
|
||||
|
||||
if (socklist[i].sockfd > max)
|
||||
max = socklist[i].sockfd;
|
||||
|
||||
if (socklist[i].data_sockfd) {
|
||||
FD_SET(socklist[i].data_sockfd,&fset);
|
||||
FD_SET (socklist[i].data_sockfd, &fset);
|
||||
|
||||
if (socklist[i].sockfd > max)
|
||||
max = socklist[i].sockfd;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* wait for an event */
|
||||
int n;
|
||||
while ((n = select(max + 1, &fset, NULL, NULL, NULL)) < 0) {
|
||||
|
||||
while ( (n = select (max + 1, &fset, NULL, NULL, NULL)) < 0) {
|
||||
if (errno != EINTR)
|
||||
return n;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/* process the received packet */
|
||||
for (i = 0; i < socklist_len; i++) {
|
||||
uint8_t buffer[4096];
|
||||
struct sockaddr_storage srcaddr;
|
||||
socklen_t srcaddrlen;
|
||||
|
||||
|
||||
if (FD_ISSET(socklist[i].data_sockfd, &fset)){
|
||||
|
||||
int len = sock_receive(socklist[i].data_sockfd,
|
||||
buffer, sizeof(buffer),
|
||||
0,
|
||||
(struct sockaddr *) &srcaddr,
|
||||
&srcaddrlen);
|
||||
|
||||
process_cw_data_packet(i, (struct sockaddr *) &srcaddr, buffer, len);
|
||||
|
||||
if (FD_ISSET (socklist[i].data_sockfd, &fset)) {
|
||||
|
||||
int len = sock_receive (socklist[i].data_sockfd,
|
||||
buffer, sizeof (buffer),
|
||||
0,
|
||||
(struct sockaddr *) &srcaddr,
|
||||
&srcaddrlen);
|
||||
|
||||
process_cw_data_packet (i, (struct sockaddr *) &srcaddr, buffer, len);
|
||||
|
||||
}
|
||||
|
||||
|
||||
if (FD_ISSET(socklist[i].sockfd, &fset)){
|
||||
|
||||
int len = sock_receive(socklist[i].sockfd,
|
||||
buffer, sizeof(buffer),
|
||||
0,
|
||||
(struct sockaddr *) &srcaddr,
|
||||
&srcaddrlen);
|
||||
|
||||
process_ctrl_packet(i, (struct sockaddr *) &srcaddr, buffer, len);
|
||||
|
||||
if (FD_ISSET (socklist[i].sockfd, &fset)) {
|
||||
|
||||
int len = sock_receive (socklist[i].sockfd,
|
||||
buffer, sizeof (buffer),
|
||||
0,
|
||||
(struct sockaddr *) &srcaddr,
|
||||
&srcaddrlen);
|
||||
|
||||
process_ctrl_packet (i, (struct sockaddr *) &srcaddr, buffer, len);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void process_cw_data_packet(int index, struct sockaddr *addr, uint8_t * buffer, int len)
|
||||
void process_cw_data_packet (int index, struct sockaddr *addr, uint8_t * buffer, int len)
|
||||
{
|
||||
char sock_buf[SOCK_ADDR_BUFSIZE];
|
||||
cw_dbg(DBG_X, "There is a data packet now");
|
||||
|
||||
cw_dbg (DBG_X, "There is a data packet now");
|
||||
|
||||
dataman_list_lock();
|
||||
cw_dbg(DBG_X, "Dataman list locked, now getting");
|
||||
struct dataman * dm = dataman_list_get(socklist[index].data_sockfd,addr);
|
||||
cw_dbg(DBG_X, "Dataman list locked, now gotted");
|
||||
|
||||
cw_dbg(DBG_INFO,"Packet for dataman %s,%d",sock_addr2str_p(addr,sock_buf),socklist[index].data_sockfd);
|
||||
cw_dbg (DBG_X, "Dataman list locked, now getting");
|
||||
struct dataman * dm = dataman_list_get (socklist[index].data_sockfd, addr);
|
||||
cw_dbg (DBG_X, "Dataman list locked, now gotted");
|
||||
|
||||
cw_dbg (DBG_INFO, "Packet for dataman %s,%d", sock_addr2str_p (addr, sock_buf), socklist[index].data_sockfd);
|
||||
|
||||
if (!dm) {
|
||||
cw_dbg(DBG_INFO,"No dataman %s,%d",sock_addr2str_p(addr,sock_buf),socklist[index].data_sockfd);
|
||||
dm = dataman_create(socklist[index].data_sockfd,addr);
|
||||
if (!dm){
|
||||
cw_log(LOG_ERR,"Can't create dataman for packet from %s",sock_addr2str_p(addr,sock_buf));
|
||||
cw_dbg (DBG_INFO, "No dataman %s,%d", sock_addr2str_p (addr, sock_buf), socklist[index].data_sockfd);
|
||||
dm = dataman_create (socklist[index].data_sockfd, addr);
|
||||
|
||||
if (!dm) {
|
||||
cw_log (LOG_ERR, "Can't create dataman for packet from %s", sock_addr2str_p (addr, sock_buf));
|
||||
return;
|
||||
}
|
||||
dataman_list_add(dm);
|
||||
|
||||
dataman_start(dm);
|
||||
|
||||
|
||||
|
||||
dataman_list_add (dm);
|
||||
|
||||
dataman_start (dm);
|
||||
|
||||
|
||||
}
|
||||
|
||||
dataman_list_unlock();
|
||||
|
||||
dataman_add_packet(dm,buffer,len);
|
||||
|
||||
|
||||
dataman_add_packet (dm, buffer, len);
|
||||
|
||||
return;
|
||||
|
||||
exit(0);
|
||||
|
||||
|
||||
|
||||
|
||||
printf("Data packet received len = %d\n",len);
|
||||
exit(0);
|
||||
struct wtpman *wtpman = wtplist_get(addr);
|
||||
if (!wtpman){
|
||||
cw_dbg(DBG_PKT_ERR,"Discarding packet on data channel from %s - No wtpman found.",sock_addr2str(addr,sock_buf));
|
||||
|
||||
exit (0);
|
||||
|
||||
|
||||
|
||||
|
||||
printf ("Data packet received len = %d\n", len);
|
||||
exit (0);
|
||||
struct wtpman *wtpman = wtplist_get (addr);
|
||||
|
||||
if (!wtpman) {
|
||||
cw_dbg (DBG_PKT_ERR, "Discarding packet on data channel from %s - No wtpman found.", sock_addr2str (addr, sock_buf));
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
wtpman_addpacket(wtpman, buffer, len);
|
||||
|
||||
|
||||
wtpman_addpacket (wtpman, buffer, len);
|
||||
}
|
||||
|
||||
|
||||
void process_cw_ctrl_packet(int index, struct sockaddr *addr, uint8_t * buffer, int len)
|
||||
void process_cw_ctrl_packet (int index, struct sockaddr *addr, uint8_t * buffer, int len)
|
||||
{
|
||||
char sock_buf[SOCK_ADDR_BUFSIZE];
|
||||
|
||||
|
||||
/* first of all check preamble */
|
||||
int preamble = cw_get_hdr_preamble(buffer);
|
||||
|
||||
int preamble = cw_get_hdr_preamble (buffer);
|
||||
|
||||
if (preamble != CAPWAP_PACKET_PREAMBLE && preamble != CAPWAP_DTLS_PACKET_PREAMBLE) {
|
||||
cw_dbg(DBG_PKT_ERR,
|
||||
"Discarding packet from %s, wrong preamble, preamble = 0x%01X",
|
||||
sock_addr2str(addr,sock_buf), preamble);
|
||||
cw_dbg (DBG_PKT_ERR,
|
||||
"Discarding packet from %s, wrong preamble, preamble = 0x%01X",
|
||||
sock_addr2str (addr, sock_buf), preamble);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
wtplist_lock();
|
||||
struct wtpman *wtpman = wtplist_get(addr);
|
||||
struct wtpman *wtpman = wtplist_get (addr);
|
||||
|
||||
if (!wtpman) {
|
||||
|
||||
wtpman = wtpman_create(index, addr);
|
||||
|
||||
|
||||
|
||||
wtpman = wtpman_create (index, addr);
|
||||
|
||||
|
||||
if (!wtpman) {
|
||||
cw_log(LOG_ERR, "Error creating wtpman: %s", strerror(errno));
|
||||
cw_log (LOG_ERR, "Error creating wtpman: %s", strerror (errno));
|
||||
wtplist_unlock();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (!wtplist_add(wtpman)) {
|
||||
cw_log(LOG_ERR, "Error adding wtpman: Too many wtp connections");
|
||||
wtpman_destroy(wtpman);
|
||||
|
||||
|
||||
if (!wtplist_add (wtpman)) {
|
||||
cw_log (LOG_ERR, "Error adding wtpman: Too many wtp connections");
|
||||
wtpman_destroy (wtpman);
|
||||
wtplist_unlock();
|
||||
return;
|
||||
};
|
||||
|
||||
wtpman_start(wtpman, preamble & 0xf);
|
||||
|
||||
wtpman_start (wtpman, preamble & 0xf);
|
||||
}
|
||||
|
||||
|
||||
wtpman_addpacket(wtpman, buffer, len);
|
||||
|
||||
|
||||
wtpman_addpacket (wtpman, buffer, len);
|
||||
wtplist_unlock();
|
||||
}
|
||||
|
||||
|
||||
|
||||
void process_lw_ctrl_packet(int index, struct sockaddr *addr, uint8_t * buffer, int len)
|
||||
void process_lw_ctrl_packet (int index, struct sockaddr *addr, uint8_t * buffer, int len)
|
||||
{
|
||||
//int sock = socklist[index].reply_sockfd;
|
||||
|
||||
|
||||
uint8_t *m = buffer + 6;
|
||||
uint32_t val = ntohl(*((uint32_t *) (m)));
|
||||
|
||||
|
||||
printf("VAL: %08X\n", val);
|
||||
|
||||
|
||||
|
||||
uint32_t val = ntohl (* ( (uint32_t *) (m)));
|
||||
|
||||
|
||||
printf ("VAL: %08X\n", val);
|
||||
|
||||
|
||||
|
||||
/* first of all check preamble */
|
||||
int version = LWTH_GET_VERSION(m);
|
||||
|
||||
int version = LWTH_GET_VERSION (m);
|
||||
|
||||
if (version != LW_VERSION) {
|
||||
// cw_log_debug1("Discarding LWAPP packet, wrong verson");
|
||||
return;
|
||||
}
|
||||
|
||||
int l = LWTH_GET_LENGTH(m);
|
||||
printf("LEN = %d\n", l);
|
||||
|
||||
|
||||
int l = LWTH_GET_LENGTH (m);
|
||||
printf ("LEN = %d\n", l);
|
||||
|
||||
if (l + 12 != len) {
|
||||
// cw_log_debug1("Discarding LWAPP packet, wrong length");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
wtplist_lock();
|
||||
struct wtpman *wtpman = wtplist_get(addr);
|
||||
struct wtpman *wtpman = wtplist_get (addr);
|
||||
|
||||
if (!wtpman) {
|
||||
|
||||
wtpman = wtpman_create(index, addr);
|
||||
|
||||
|
||||
wtpman = wtpman_create (index, addr);
|
||||
|
||||
if (!wtpman) {
|
||||
cw_log(LOG_ERR, "Error creating wtpman: %s", strerror(errno));
|
||||
cw_log (LOG_ERR, "Error creating wtpman: %s", strerror (errno));
|
||||
wtplist_unlock();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (!wtplist_add(wtpman)) {
|
||||
cw_log(LOG_ERR, "Error adding wtpman: Too many wtp connections");
|
||||
wtpman_destroy(wtpman);
|
||||
|
||||
|
||||
if (!wtplist_add (wtpman)) {
|
||||
cw_log (LOG_ERR, "Error adding wtpman: Too many wtp connections");
|
||||
wtpman_destroy (wtpman);
|
||||
wtplist_unlock();
|
||||
return;
|
||||
};
|
||||
|
||||
|
||||
//wtpman_lw_start(wtpman);
|
||||
}
|
||||
|
||||
//wtpman_lw_addpacket(wtpman,buffer,len);
|
||||
wtplist_unlock();
|
||||
}
|
||||
@ -565,14 +742,15 @@ void process_lw_ctrl_packet(int index, struct sockaddr *addr, uint8_t * buffer,
|
||||
|
||||
|
||||
|
||||
void process_ctrl_packet(int index, struct sockaddr *addr, uint8_t * buffer, int len)
|
||||
void process_ctrl_packet (int index, struct sockaddr *addr, uint8_t * buffer, int len)
|
||||
{
|
||||
switch (socklist[index].ac_proto) {
|
||||
case AC_PROTO_CAPWAP:
|
||||
process_cw_ctrl_packet(index, addr, buffer, len);
|
||||
process_cw_ctrl_packet (index, addr, buffer, len);
|
||||
return;
|
||||
|
||||
case AC_PROTO_LWAPP:
|
||||
process_lw_ctrl_packet(index, addr, buffer, len);
|
||||
process_lw_ctrl_packet (index, addr, buffer, len);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
147
src/ac/conf.c
147
src/ac/conf.c
@ -38,7 +38,6 @@ uint8_t conf_macaddress_len = 0;
|
||||
|
||||
long conf_strict_capwap = 1;
|
||||
long conf_strict_headers = 0;
|
||||
//char *conf_capwap_mode_str = NULL;
|
||||
int conf_capwap_mode = CW_MODE_CAPWAP;
|
||||
|
||||
|
||||
@ -79,8 +78,6 @@ char *conf_sslkeypass = NULL;
|
||||
char *conf_sslcipher = NULL;
|
||||
char *conf_dtls_psk = NULL;
|
||||
|
||||
//char * conf_ac_hardware_version=NULL;
|
||||
//char * conf_ac_software_version=NULL;
|
||||
int conf_security = 0;
|
||||
long conf_vendor_id = CONF_DEFAULT_VENDOR_ID;
|
||||
|
||||
@ -92,11 +89,6 @@ bstr_t conf_cisco_hardware_version = NULL;
|
||||
bstr_t conf_cisco_software_version = NULL;
|
||||
|
||||
|
||||
//int conf_hardware_version_len=0;
|
||||
|
||||
//int conf_software_version_len=0;
|
||||
|
||||
|
||||
int conf_use_loopback = 0;
|
||||
|
||||
long conf_debug_level = -1;
|
||||
@ -140,7 +132,9 @@ static int init_acname()
|
||||
|
||||
static int init_acid()
|
||||
{
|
||||
|
||||
int i;
|
||||
char *s;
|
||||
|
||||
if (conf_acid != NULL)
|
||||
return 1;
|
||||
|
||||
@ -165,11 +159,11 @@ static int init_acid()
|
||||
return 0;
|
||||
};
|
||||
|
||||
int i;
|
||||
|
||||
|
||||
|
||||
conf_acid = malloc(2 * conf_macaddress_len + 1);
|
||||
char *s = conf_acid;
|
||||
s = conf_acid;
|
||||
|
||||
for (i = 0; i < conf_macaddress_len; i++) {
|
||||
s += sprintf(s, "%02X", conf_macaddress[i]);
|
||||
@ -178,10 +172,13 @@ static int init_acid()
|
||||
|
||||
}
|
||||
|
||||
|
||||
static int init_dtls()
|
||||
{
|
||||
if (conf_dtls_psk != NULL) {
|
||||
// conf_security=CW_SECURITY_FLAGS_S;
|
||||
/*
|
||||
// conf_security=CW_SECURITY_FLAGS_S;
|
||||
*/
|
||||
}
|
||||
|
||||
return 1;
|
||||
@ -260,17 +257,21 @@ static int init_control_port()
|
||||
|
||||
static int init_listen_addrs()
|
||||
{
|
||||
struct ifaddrs *ifap, *ifa;
|
||||
int rc;
|
||||
int ctr;
|
||||
|
||||
if (conf_listen_addrs != 0)
|
||||
return 1;
|
||||
|
||||
struct ifaddrs *ifap, *ifa;
|
||||
|
||||
int rc = getifaddrs(&ifap);
|
||||
|
||||
rc = getifaddrs(&ifap);
|
||||
if (rc == -1)
|
||||
return 0;
|
||||
|
||||
/* count the addresses */
|
||||
int ctr = 0;
|
||||
ctr = 0;
|
||||
for (ifa = ifap; ifa != 0; ifa = ifa->ifa_next) {
|
||||
if (!ifa->ifa_addr)
|
||||
continue;
|
||||
@ -307,8 +308,7 @@ static int init_listen_addrs()
|
||||
|
||||
if (ifa->ifa_addr->sa_family == AF_INET && conf_ipv4) {
|
||||
sock_addrtostr(ifa->ifa_addr, str, 100,0);
|
||||
// printf("The converter has %s\n",str);
|
||||
// *strchr(str, ':') = 0;
|
||||
|
||||
conf_listen_addrs[ctr] =
|
||||
(char *) cw_setstr((uint8_t **) & conf_listen_addrs[ctr],
|
||||
(uint8_t *) str, strlen(str));
|
||||
@ -346,6 +346,7 @@ static char *conf_default_mcast_groups_ipv4[] = {
|
||||
|
||||
#ifdef WITH_IPV6
|
||||
static char *conf_default_mcast_groups_ipv6[] = {
|
||||
"ff01:0:0:0:0:0:0:18c",
|
||||
/* "ff01:0:0:0:0:0:0:18c",
|
||||
"ff02:0:0:0:0:0:0:18c%em0",
|
||||
"ff03:0:0:0:0:0:0:18c",
|
||||
@ -356,11 +357,8 @@ static char *conf_default_mcast_groups_ipv6[] = {
|
||||
};
|
||||
#endif
|
||||
|
||||
//#include "avltree"
|
||||
//#include "cw/stravltree.h"
|
||||
|
||||
|
||||
|
||||
/*
|
||||
static int add_bcast_addr(void *priv, void *addr)
|
||||
{
|
||||
printf("Callback Called\n");
|
||||
@ -374,12 +372,17 @@ static int add_bcast_addr(void *priv, void *addr)
|
||||
conf_bcast_addrs_len++;
|
||||
return 1;
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
* Initialize broadcast addresses (ipv4 only)
|
||||
*/
|
||||
int init_bcast_addrs()
|
||||
{
|
||||
int rc;
|
||||
char str[100];
|
||||
struct ifaddrs *ifa0, *ifa;
|
||||
mavl_t t;
|
||||
mavliter_t it;
|
||||
|
||||
@ -389,17 +392,17 @@ int init_bcast_addrs()
|
||||
if (!conf_ipv4)
|
||||
return 1;
|
||||
|
||||
t = mavl_create(mavl_cmp_str,mavl_free_str);
|
||||
if (!t)
|
||||
t = mavl_create_str();
|
||||
if (t==NULL)
|
||||
return 0;
|
||||
|
||||
/* add the default broadast address */
|
||||
mavl_add_strdup(t, "255.255.255.255");
|
||||
mavl_add_str(t, cw_strdup("255.255.255.255"));
|
||||
|
||||
|
||||
/* add all other local broadcast addresses */
|
||||
struct ifaddrs *ifa0, *ifa;
|
||||
int rc = getifaddrs(&ifa0);
|
||||
|
||||
rc = getifaddrs(&ifa0);
|
||||
if (rc == -1)
|
||||
return 0;
|
||||
|
||||
@ -421,24 +424,22 @@ int init_bcast_addrs()
|
||||
if (sa->sa_family != AF_INET)
|
||||
continue;
|
||||
|
||||
char str[100];
|
||||
|
||||
if (ifa->ifa_broadaddr) {
|
||||
sock_addrtostr(ifa->ifa_broadaddr, str, 100,1);
|
||||
*strchr(str, ':') = 0;
|
||||
mavl_add_strdup(t, str);
|
||||
mavl_add_str(t, cw_strdup(str));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
conf_bcast_addrs = malloc(t->count * sizeof(char *));
|
||||
|
||||
//mavl_foreach_asc(t, add_bcast_addr, 0);
|
||||
|
||||
mavliter_init(&it,t);
|
||||
mavliter_foreach(&it){
|
||||
union mavldata * d;
|
||||
d = mavliter_get(&it);
|
||||
conf_bcast_addrs[conf_bcast_addrs_len] = strdup(d->str);
|
||||
char * d;
|
||||
d = mavliter_get_str(&it);
|
||||
conf_bcast_addrs[conf_bcast_addrs_len] = cw_strdup(d);
|
||||
if (conf_bcast_addrs[conf_bcast_addrs_len] != 0)
|
||||
conf_bcast_addrs_len++;
|
||||
|
||||
@ -454,19 +455,23 @@ int init_bcast_addrs()
|
||||
|
||||
int init_mcast_groups()
|
||||
{
|
||||
if (conf_mcast_groups)
|
||||
return 1;
|
||||
|
||||
int ctr;
|
||||
int i;
|
||||
|
||||
int n = 0;
|
||||
int n4 = 0, n6 = 0;
|
||||
|
||||
if (conf_mcast_groups)
|
||||
return 1;
|
||||
|
||||
if (conf_ipv4) {
|
||||
n4 = sizeof(conf_default_mcast_groups_ipv4) / sizeof(char *);
|
||||
}
|
||||
#ifdef WITH_IPV6
|
||||
|
||||
if (conf_ipv6) {
|
||||
n6 = sizeof(conf_default_mcast_groups_ipv6) / sizeof(char *);
|
||||
}
|
||||
#endif
|
||||
|
||||
n = n4 + n6;
|
||||
if (n == 0)
|
||||
return 1;
|
||||
@ -476,8 +481,8 @@ int init_mcast_groups()
|
||||
return 0;
|
||||
memset(conf_mcast_groups, 0, n * sizeof(char *));
|
||||
|
||||
int ctr = 0;
|
||||
int i;
|
||||
ctr = 0;
|
||||
|
||||
for (i = 0; i < n4; i++) {
|
||||
uint8_t *g = (uint8_t *) conf_default_mcast_groups_ipv4[i];
|
||||
conf_mcast_groups[ctr] =
|
||||
@ -546,8 +551,8 @@ static int conf_read_dbg_level(cfg_t * cfg)
|
||||
|
||||
for (i = 0; i < n; i++) {
|
||||
char *str = cfg_getnstr(cfg, name, i);
|
||||
//int u = cw_log_str2dbglevel(str);
|
||||
int b = cw_strlist_get_id(cw_dbg_strings, str); //cw_log_str2dbglevel(str);
|
||||
int b = cw_strlist_get_id(cw_dbg_strings, str);
|
||||
|
||||
|
||||
cw_dbg_set_level(b, 1);
|
||||
|
||||
@ -555,34 +560,9 @@ static int conf_read_dbg_level(cfg_t * cfg)
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
struct cw_Mod ** conf_mods; //[10];
|
||||
struct cw_Mod ** conf_mods;
|
||||
char *conf_mods_dir = NULL;
|
||||
|
||||
|
||||
static int init_mods()
|
||||
{
|
||||
|
||||
/* conf_mods[0]=modload_ac("cisco");
|
||||
conf_mods[1]=modload_ac("fortinet");
|
||||
// conf_mods[2]=modload_ac("zyxel");
|
||||
conf_mods[2]=modload_ac("cipwap");
|
||||
conf_mods[3]=modload_ac("capwap");
|
||||
conf_mods[4]=modload_ac("capwap80211");
|
||||
conf_mods[5]=NULL;
|
||||
*/
|
||||
int i;
|
||||
for(i=0; conf_mods[i]; i++){
|
||||
if (conf_mods[i]->init){
|
||||
conf_mods[i]->init();
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Read the module names from config file
|
||||
*/
|
||||
@ -626,6 +606,10 @@ void conf_init_capwap_mode()
|
||||
|
||||
int conf_parse_listen_addr(const char *addrstr, char *saddr, char *port, int *proto)
|
||||
{
|
||||
char *b;
|
||||
int c;
|
||||
int i, l;
|
||||
char *ctrlport;
|
||||
char *p = strchr(addrstr, '/');
|
||||
if (!p)
|
||||
*proto = AC_PROTO_CAPWAP;
|
||||
@ -638,7 +622,7 @@ int conf_parse_listen_addr(const char *addrstr, char *saddr, char *port, int *pr
|
||||
*proto = AC_PROTO_UNKNOWN;
|
||||
}
|
||||
|
||||
char *ctrlport;
|
||||
|
||||
switch (*proto) {
|
||||
case AC_PROTO_CAPWAP:
|
||||
ctrlport = CAPWAP_CONTROL_PORT_STR;
|
||||
@ -651,8 +635,8 @@ int conf_parse_listen_addr(const char *addrstr, char *saddr, char *port, int *pr
|
||||
}
|
||||
|
||||
|
||||
int i, l;
|
||||
int c = 0;
|
||||
|
||||
c = 0;
|
||||
|
||||
if (p)
|
||||
l = p - addrstr;
|
||||
@ -684,7 +668,7 @@ int conf_parse_listen_addr(const char *addrstr, char *saddr, char *port, int *pr
|
||||
}
|
||||
|
||||
/* we assume now, its ipv6 */
|
||||
char *b = strstr(addrstr, "]:");
|
||||
b = strstr(addrstr, "]:");
|
||||
if (!b) {
|
||||
/* ippv6 w/o port */
|
||||
strncpy(saddr, addrstr, l);
|
||||
@ -716,9 +700,6 @@ int read_config(const char *filename)
|
||||
{
|
||||
int i, n;
|
||||
|
||||
if (!init_control_port())
|
||||
return 0;
|
||||
|
||||
cfg_opt_t opts[] = {
|
||||
CFG_STR_LIST("mods", "{}", CFGF_NONE),
|
||||
CFG_SIMPLE_STR("mods_dir", &conf_mods_dir),
|
||||
@ -733,7 +714,7 @@ int read_config(const char *filename)
|
||||
CFG_SIMPLE_BOOL("strict_capwap", &conf_strict_capwap),
|
||||
CFG_SIMPLE_BOOL("strict_headers", &conf_strict_headers),
|
||||
CFG_SIMPLE_BOOL("use_loopback", &conf_use_loopback),
|
||||
// CFG_SIMPLE_STR("capwap_mode", &conf_capwap_mode_str),
|
||||
/*// CFG_SIMPLE_STR("capwap_mode", &conf_capwap_mode_str),*/
|
||||
|
||||
|
||||
#ifdef WITH_LWAPP
|
||||
@ -765,12 +746,17 @@ int read_config(const char *filename)
|
||||
CFG_SIMPLE_BOOL("dtls_verify_peer", &conf_dtls_verify_peer),
|
||||
CFG_SIMPLE_BOOL("ipv4", &conf_ipv4),
|
||||
CFG_SIMPLE_BOOL("ipv6", &conf_ipv6),
|
||||
CFG_SIMPLE_STR("db_file", conf_db_file),
|
||||
CFG_SIMPLE_STR("db_file", &conf_db_file),
|
||||
CFG_SIMPLE_STR("image_dir", &conf_image_dir),
|
||||
|
||||
CFG_END()
|
||||
};
|
||||
cfg_t *cfg;
|
||||
|
||||
if (!init_control_port())
|
||||
return 0;
|
||||
|
||||
|
||||
cfg = cfg_init(opts, CFGF_NONE);
|
||||
cfg_set_error_function(cfg, errfunc);
|
||||
|
||||
@ -838,18 +824,13 @@ int read_config(const char *filename)
|
||||
if (!conf_image_dir)
|
||||
conf_image_dir = CONF_DEFAULT_IMAGE_DIR;
|
||||
|
||||
//printf("INIT MODS\n");
|
||||
// init_mods();
|
||||
//printf("done init mods");
|
||||
|
||||
// conf_init_capwap_mode();
|
||||
|
||||
|
||||
init_listen_addrs();
|
||||
init_mcast_groups();
|
||||
init_bcast_addrs();
|
||||
|
||||
//printf("Yea all mods inited\n");
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -33,7 +33,7 @@ static int cmp(const void *d1, const void *d2)
|
||||
|
||||
int dataman_list_init()
|
||||
{
|
||||
dataman_list = mavl_create(cmp, NULL);
|
||||
dataman_list = mavl_create_ptr(cmp, NULL);
|
||||
if (!dataman_list)
|
||||
return 0;
|
||||
|
||||
@ -85,7 +85,7 @@ struct dataman *dataman_list_add(struct dataman *dm)
|
||||
{
|
||||
|
||||
cw_dbg(DBG_X,"Adding dataman %p\n",dm);
|
||||
return mavl_add(dataman_list, dm);
|
||||
return mavl_add(dataman_list, dm,NULL);
|
||||
}
|
||||
|
||||
|
||||
|
@ -24,7 +24,6 @@
|
||||
#include <sys/types.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
|
||||
#define MAX_WTPS 200
|
||||
|
||||
#include <stdio.h>
|
||||
|
@ -41,7 +41,7 @@
|
||||
#include "socklist.h"
|
||||
#include "wtpman.h"
|
||||
#include "wtplist.h"
|
||||
|
||||
#include "cw/kvt.h"
|
||||
|
||||
static void reset_echointerval_timer(struct wtpman *wtpman)
|
||||
{
|
||||
@ -94,10 +94,10 @@ static void wtpman_run_discovery(void *arg)
|
||||
wtpman->conn->capwap_state = CAPWAP_STATE_DISCOVERY;
|
||||
// wtpman->conn->actions = &capwap_actions;
|
||||
|
||||
wtpman->conn->outgoing = mbag_create();
|
||||
/* wtpman->conn->outgoing = mbag_create();
|
||||
wtpman->conn->incomming = mbag_create();
|
||||
|
||||
wtpman->conn->remote_cfg = mavl_create(mavl_cmp_kv,NULL);
|
||||
*/
|
||||
wtpman->conn->remote_cfg = cw_kvt_create(); //mavl_cmp_kv,NULL,1312);
|
||||
|
||||
while (!cw_timer_timeout(timer)
|
||||
&& wtpman->conn->capwap_state == CAPWAP_STATE_DISCOVERY) {
|
||||
|
@ -32,10 +32,10 @@
|
||||
#include "sock.h"
|
||||
|
||||
|
||||
static int acip_cmp(const mavldata_t *x1, const mavldata_t *x2)
|
||||
static int acip_cmp(const void *x1, const void *x2)
|
||||
{
|
||||
struct cw_acip *ip1 = (struct cw_acip *) x1->ptr;
|
||||
struct cw_acip *ip2 = (struct cw_acip *) x2->ptr;
|
||||
struct cw_acip *ip1 = *((struct cw_acip **) x1);
|
||||
struct cw_acip *ip2 = *((struct cw_acip **) x2);
|
||||
|
||||
int r = ip1->index - ip2->index;
|
||||
if (r!=0)
|
||||
@ -68,9 +68,9 @@ static int acip_cmp(const mavldata_t *x1, const mavldata_t *x2)
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void acip_del(mavldata_t *d)
|
||||
static void acip_del(void *d)
|
||||
{
|
||||
free(d->ptr);
|
||||
free(*(void**)d);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -79,6 +79,6 @@ static void acip_del(mavldata_t *d)
|
||||
*/
|
||||
cw_aciplist_t cw_aciplist_create()
|
||||
{
|
||||
return mavl_create(acip_cmp, acip_del);
|
||||
return mavl_create(acip_cmp, acip_del,1312);
|
||||
}
|
||||
|
||||
|
@ -15,10 +15,10 @@ typedef struct mavl * cw_aciplist_t;
|
||||
extern cw_aciplist_t cw_aciplist_create();
|
||||
|
||||
#define cw_aciplist_destroy(l) mavl_destroy(l)
|
||||
#define cw_aciplist_add(l,elem) mavl_add(l,elem)
|
||||
#define cw_aciplist_add(l,elem) mavl_add(l,elem,NULL)
|
||||
#define cw_aciplist_del(l,elem) mavl_del(l,elem)
|
||||
#define cw_aciplist_foreach(l,callback,cbpriv) mavl_foreach_asc(l,callback,cbpriv)
|
||||
#define cw_aciplist_replace(l,r) mavl_replace(l,r)
|
||||
#define cw_aciplist_replace(l,r) mavl_replace(l,r,NULL)
|
||||
|
||||
#endif
|
||||
|
||||
|
@ -53,7 +53,7 @@ const struct mbag_typedef mbag_type_acprio = {
|
||||
|
||||
cw_acpriolist_t cw_acpriolist_create()
|
||||
{
|
||||
return mavl_create(acprio_cmp, acprio_del);
|
||||
return mavl_create(acprio_cmp, acprio_del,4);
|
||||
}
|
||||
|
||||
cw_acprio_t * cw_acpriolist_add(cw_acpriolist_t l, const char *name,int name_len, uint8_t prio)
|
||||
@ -66,7 +66,7 @@ cw_acprio_t * cw_acpriolist_add(cw_acpriolist_t l, const char *name,int name_len
|
||||
s->prio=prio;
|
||||
|
||||
cw_acpriolist_del(l,s);
|
||||
return mavl_add(l,s);
|
||||
return mavl_add(l,s,NULL);
|
||||
|
||||
}
|
||||
|
||||
|
@ -67,7 +67,7 @@ static int cw_action_in_cmp(const void *elem1, const void *elem2)
|
||||
*/
|
||||
cw_actionlist_in_t cw_actionlist_in_create()
|
||||
{
|
||||
return mavl_create(cw_action_in_cmp, free);
|
||||
return mavl_create(cw_action_in_cmp, free,1312);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -89,7 +89,7 @@ cw_action_in_t *cw_actionlist_in_add(cw_actionlist_in_t t, struct cw_action_in *
|
||||
return NULL;
|
||||
|
||||
memcpy(an, a, s);
|
||||
return mavl_add(t, an);
|
||||
return mavl_add(t, an,NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -217,7 +217,7 @@ cw_action_out_t *cw_actionlist_out_add(cw_actionlist_out_t t, struct cw_action_o
|
||||
if (!o) {
|
||||
return NULL;
|
||||
}
|
||||
mavl_add(t,o);
|
||||
mavl_add(t,o,NULL);
|
||||
}
|
||||
|
||||
struct mlist_elem * e = mlist_replace(o->mlist,NULL,a);
|
||||
@ -273,7 +273,7 @@ void *cw_actionlist_add(struct mavl *t, void *a, size_t s)
|
||||
return NULL;
|
||||
|
||||
memcpy(an, a, s);
|
||||
return mavl_add(t, an);
|
||||
return mavl_add(t, an,NULL);
|
||||
|
||||
}
|
||||
|
||||
@ -284,7 +284,7 @@ void *cw_actionlist_add(struct mavl *t, void *a, size_t s)
|
||||
*/
|
||||
cw_actionlist_out_t cw_actionlist_out_create()
|
||||
{
|
||||
return mavl_create(cw_action_out_cmp, free);
|
||||
return mavl_create(cw_action_out_cmp, free,1312);
|
||||
}
|
||||
|
||||
|
||||
|
@ -30,10 +30,9 @@
|
||||
|
||||
#include "stravltree.h"
|
||||
#include "mod.h"
|
||||
#include "message_set.h"
|
||||
|
||||
#include "cw_types.h"
|
||||
#include "msget.h"
|
||||
|
||||
#include "kvt.h"
|
||||
|
||||
int conn_send_msg(struct conn *conn, uint8_t * rawmsg);
|
||||
|
||||
@ -301,7 +300,7 @@ static int process_elements(struct conn *conn, uint8_t * rawmsg, int len,
|
||||
|
||||
/* Search message */
|
||||
struct cw_MsgData * message;
|
||||
message = mavl_find_ptr(conn->msgset->messages,&search);
|
||||
message = mavl_find(conn->msgset->msgdata,&search);
|
||||
|
||||
int result_code = 0;
|
||||
|
||||
@ -509,12 +508,12 @@ exit(0);
|
||||
|
||||
mavliter_foreach(&it){
|
||||
char value[500];
|
||||
mavldata_t * data;
|
||||
struct cw_KVT * data;
|
||||
data = mavliter_get(&it);
|
||||
type = data->kv.priv;
|
||||
type = data->type;
|
||||
type->to_str(data,value,0);
|
||||
|
||||
printf("Got %s (%s): %s\n",data->kv.key,type->name, value);
|
||||
printf("Got %s (%s): %s\n",data->key,type->name, value);
|
||||
}
|
||||
|
||||
|
||||
|
@ -37,110 +37,109 @@
|
||||
|
||||
|
||||
|
||||
static int cmp_by_addr(const mavldata_t * d1,const mavldata_t *d2)
|
||||
static int cmp_by_addr ( const void * d1, const void *d2 )
|
||||
{
|
||||
struct conn * c1=(struct conn *) d1->ptr;
|
||||
struct conn * c2=(struct conn *) d2->ptr;
|
||||
|
||||
return sock_cmpaddr((struct sockaddr*)&c1->addr,(struct sockaddr*)&c2->addr,1);
|
||||
struct conn * c1 = * ( void ** ) d1 ;
|
||||
struct conn * c2 = * ( void ** ) d2 ;
|
||||
|
||||
return sock_cmpaddr ( ( struct sockaddr* ) &c1->addr, ( struct sockaddr* ) &c2->addr, 1 );
|
||||
}
|
||||
|
||||
static int cmp_by_session_id(const mavldata_t *d1, const mavldata_t *d2)
|
||||
static int cmp_by_session_id ( const void *d1, const void *d2 )
|
||||
{
|
||||
struct conn * c1=(struct conn *) d1->ptr;
|
||||
struct conn * c2=(struct conn *) d2->ptr;
|
||||
return memcmp(c1->session_id,c2->session_id,16);
|
||||
struct conn * c1 = *( void ** ) d1;
|
||||
struct conn * c2 = *( void ** ) d2;
|
||||
return memcmp ( c1->session_id, c2->session_id, 16 );
|
||||
}
|
||||
|
||||
|
||||
struct connlist * connlist_create(int len)
|
||||
struct connlist * connlist_create ( int len )
|
||||
{
|
||||
|
||||
struct connlist * cl = malloc(sizeof(struct connlist));
|
||||
if (!cl)
|
||||
struct connlist * cl = malloc ( sizeof ( struct connlist ) );
|
||||
|
||||
if ( !cl )
|
||||
return 0;
|
||||
|
||||
|
||||
cl->by_addr = mavl_create(cmp_by_addr,0);
|
||||
|
||||
if (!cl->by_addr){
|
||||
free(cl);
|
||||
|
||||
|
||||
cl->by_addr = mavl_create_ptr ( cmp_by_addr, NULL );
|
||||
|
||||
if ( !cl->by_addr ) {
|
||||
free ( cl );
|
||||
return 0;
|
||||
}
|
||||
|
||||
cl->by_session_id = mavl_create(cmp_by_session_id,0);
|
||||
|
||||
|
||||
if (pthread_mutex_init(&cl->connlist_mutex,NULL)){
|
||||
mavl_destroy(cl->by_addr);
|
||||
free(cl);
|
||||
|
||||
cl->by_session_id = mavl_create_ptr ( cmp_by_session_id, NULL );
|
||||
|
||||
|
||||
if ( pthread_mutex_init ( &cl->connlist_mutex, NULL ) ) {
|
||||
mavl_destroy ( cl->by_addr );
|
||||
free ( cl );
|
||||
return 0;
|
||||
};
|
||||
|
||||
cl->len=len;
|
||||
|
||||
|
||||
cl->len = len;
|
||||
|
||||
return cl;
|
||||
}
|
||||
|
||||
void connlist_lock(struct connlist * cl)
|
||||
void connlist_lock ( struct connlist * cl )
|
||||
{
|
||||
pthread_mutex_lock(&cl->connlist_mutex);
|
||||
pthread_mutex_lock ( &cl->connlist_mutex );
|
||||
}
|
||||
|
||||
void connlist_unlock(struct connlist * cl)
|
||||
void connlist_unlock ( struct connlist * cl )
|
||||
{
|
||||
pthread_mutex_unlock(&cl->connlist_mutex);
|
||||
pthread_mutex_unlock ( &cl->connlist_mutex );
|
||||
}
|
||||
|
||||
|
||||
void connlist_destroy(struct connlist * cl)
|
||||
void connlist_destroy ( struct connlist * cl )
|
||||
{
|
||||
if (!cl)
|
||||
if ( !cl )
|
||||
return;
|
||||
|
||||
if (cl->by_addr)
|
||||
mavl_destroy(cl->by_addr);
|
||||
pthread_mutex_destroy(&cl->connlist_mutex);
|
||||
free(cl);
|
||||
|
||||
if ( cl->by_addr )
|
||||
mavl_destroy ( cl->by_addr );
|
||||
|
||||
pthread_mutex_destroy ( &cl->connlist_mutex );
|
||||
free ( cl );
|
||||
|
||||
}
|
||||
|
||||
|
||||
struct conn * connlist_get(struct connlist * cl, const struct sockaddr * addr)
|
||||
struct conn * connlist_get ( struct connlist * cl, const struct sockaddr * addr )
|
||||
{
|
||||
struct conn search;
|
||||
sock_copyaddr(&search.addr,addr);
|
||||
return mavl_get_ptr(cl->by_addr,&search);
|
||||
sock_copyaddr ( &search.addr, addr );
|
||||
return mavl_get_ptr ( cl->by_addr, &search );
|
||||
}
|
||||
|
||||
|
||||
struct conn * connlist_add(struct connlist * cl, struct conn * conn)
|
||||
struct conn * connlist_add ( struct connlist * cl, struct conn * conn )
|
||||
{
|
||||
if ( cl->len!=0)
|
||||
if (cl->by_addr->count>=cl->len)
|
||||
if ( cl->len != 0 )
|
||||
if ( cl->by_addr->count >= cl->len )
|
||||
return NULL;
|
||||
conn->connlist=cl;
|
||||
return mavl_add_ptr(cl->by_addr,conn);
|
||||
|
||||
conn->connlist = cl;
|
||||
return mavl_add_ptr ( cl->by_addr, conn );
|
||||
}
|
||||
|
||||
struct conn * connlist_get_by_session_id(struct connlist *cl, struct conn * conn)
|
||||
struct conn * connlist_get_by_session_id ( struct connlist *cl, struct conn * conn )
|
||||
{
|
||||
return mavl_get_ptr(cl->by_session_id,conn);
|
||||
return mavl_get_ptr ( cl->by_session_id, conn );
|
||||
}
|
||||
|
||||
struct conn * connlist_add_by_session_id(struct connlist * cl, struct conn * conn)
|
||||
struct conn * connlist_add_by_session_id ( struct connlist * cl, struct conn * conn )
|
||||
{
|
||||
return mavl_add_ptr(cl->by_session_id,conn);
|
||||
return mavl_add_ptr ( cl->by_session_id, conn );
|
||||
}
|
||||
|
||||
void connlist_remove(struct connlist *cl,struct conn * conn)
|
||||
void connlist_remove ( struct connlist *cl, struct conn * conn )
|
||||
{
|
||||
mavldata_t md;
|
||||
md.ptr=conn;
|
||||
mavl_del(cl->by_session_id,&md);
|
||||
md.ptr=conn;
|
||||
mavl_del(cl->by_addr,&md);
|
||||
void * md;
|
||||
md = conn;
|
||||
mavl_del ( cl->by_session_id, &md );
|
||||
md = conn;
|
||||
mavl_del ( cl->by_addr, &md );
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -10,7 +10,7 @@
|
||||
|
||||
#include "lw.h"
|
||||
#include "capwap.h"
|
||||
#include "message_set.h"
|
||||
#include "msget.h"
|
||||
|
||||
/**
|
||||
* @defgroup CW CW
|
||||
|
@ -24,7 +24,7 @@ int cw_in_ac_name_with_priority(struct conn *conn, struct cw_action_in *a, uint8
|
||||
|
||||
prio->prio=cw_get_byte(data);
|
||||
prio->name=strndup((char*)data+1,len-1);
|
||||
mavl_replace(prios,prio);
|
||||
mavl_replace(prios,prio,NULL);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
#include "message_set.h"
|
||||
#include "cw_types.h"
|
||||
#include "kvstore.h"
|
||||
#include "msget.h"
|
||||
#include "kvt.h"
|
||||
#include "log.h"
|
||||
|
||||
int cw_in_generic(struct cw_ElemHandler * handler, struct cw_ElemHandlerParams * params,
|
||||
@ -13,7 +12,7 @@ int cw_in_generic(struct cw_ElemHandler * handler, struct cw_ElemHandlerParams *
|
||||
cw_log(LOG_ERR,"Can't handle element: %s, no type defined",handler->name);
|
||||
return 0;
|
||||
}
|
||||
cw_kvstore_add(params->conn->remote_cfg, handler->key, handler->type, elem_data,elem_len);
|
||||
cw_kvt_add(params->conn->remote_cfg, handler->key, handler->type, elem_data,elem_len);
|
||||
|
||||
|
||||
/*
|
||||
|
@ -1,13 +0,0 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "cw_types.h"
|
||||
#include "kvstore.h"
|
||||
|
||||
void cw_types_kvstore_mavl_delete(mavldata_t *data){
|
||||
const struct cw_Type * type = data->kv.priv;
|
||||
if (type->del)
|
||||
type->del(data);
|
||||
free(data->kv.key);
|
||||
}
|
||||
|
@ -1,42 +1,42 @@
|
||||
#include "kvstore.h"
|
||||
#include "cw_types.h"
|
||||
#include "kvt.h"
|
||||
#include "cw.h"
|
||||
|
||||
#include "log.h"
|
||||
#include "dbg.h"
|
||||
|
||||
const char * cw_kvstore_add(mavl_t kvstore, const char *key, const struct cw_Type *type,
|
||||
const char * cw_kvt_add(mavl_t kvtstore, const char *key, const struct cw_Type *type,
|
||||
const uint8_t * data, int len)
|
||||
{
|
||||
mavldata_t mdata, *mresult;
|
||||
cw_KVT_t mdata, *mresult;
|
||||
int exists;
|
||||
|
||||
/* cw_dbg(DBG_ELEM,"KVStore (%p,%d) add elem (%s): %s", kvstore, kvstore->count,
|
||||
type->name, key );
|
||||
*/
|
||||
mdata.kv.key=cw_strdup(key);
|
||||
if (!mdata.kv.key){
|
||||
mdata.key=cw_strdup(key);
|
||||
if (!mdata.key){
|
||||
cw_log(LOG_ERR, "Can't allocate memory for key %s: %s",
|
||||
key,strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
mresult = type->get(&mdata,data,len);
|
||||
if (!mresult){
|
||||
cw_log(LOG_ERR, "Can't create kvstore element for key %s of type %s: %s",
|
||||
key,type->name, strerror(errno));
|
||||
free(mdata.kv.key);
|
||||
free(mdata.key);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mresult = mavl_add(kvstore, &mdata);
|
||||
if (mresult != &mdata){
|
||||
mresult = mavl_add(kvtstore, &mdata, &exists);
|
||||
if (exists){
|
||||
cw_log(LOG_ERR, "Element already exists %s", key);
|
||||
/* element already exists */
|
||||
free(mdata.kv.key);
|
||||
type->del(&mdata);
|
||||
free(mdata.key);
|
||||
if (type->del)
|
||||
type->del(&mdata);
|
||||
return key;
|
||||
}
|
||||
|
||||
return mdata.kv.key;
|
||||
return mdata.key;
|
||||
}
|
6
src/cw/cw_kvt_mavlcmp.c
Normal file
6
src/cw/cw_kvt_mavlcmp.c
Normal file
@ -0,0 +1,6 @@
|
||||
#include "kvt.h"
|
||||
|
||||
int cw_kvt_mavlcmp(const void *v1, const void *v2)
|
||||
{
|
||||
return strcmp(((cw_KVT_t *) v1)->key, ((cw_KVT_t *) v2)->key);
|
||||
}
|
15
src/cw/cw_kvt_mavldel.c
Normal file
15
src/cw/cw_kvt_mavldel.c
Normal file
@ -0,0 +1,15 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
#include "kvt.h"
|
||||
|
||||
void cw_kvt_mavldel(void *data)
|
||||
{
|
||||
struct cw_KVT *kvt = data;
|
||||
|
||||
if (kvt->type->del)
|
||||
kvt->type->del(data);
|
||||
|
||||
free(kvt->key);
|
||||
}
|
@ -30,7 +30,7 @@ int cw_process_element(struct cw_ElemHandlerParams *params, int proto, int vendo
|
||||
elem_data_search.id=elem_id;
|
||||
elem_data_search.proto=0;
|
||||
elem_data_search.vendor=0;
|
||||
elem_data = mavl_find_ptr(params->msgdata->elements_tree,&elem_data_search);
|
||||
elem_data = mavl_find(params->msgdata->elements_tree,&elem_data_search);
|
||||
if (!elem_data){
|
||||
cw_dbg(DBG_ELEM_ERR, "Element %d - %s, not allowed here",
|
||||
elem_id, handler->name);
|
||||
|
@ -17,10 +17,10 @@
|
||||
*/
|
||||
|
||||
#include "cw.h"
|
||||
#include "cw_types.h"
|
||||
#include "kvt.h"
|
||||
#include "dbg.h"
|
||||
#include "keys.h"
|
||||
#include "kvstore.h"
|
||||
|
||||
|
||||
int cw_read_descriptor_subelems(mavl_t cfg, const char * parent_key,
|
||||
uint8_t * data, int len,
|
||||
@ -69,11 +69,11 @@ int cw_read_descriptor_subelems(mavl_t cfg, const char * parent_key,
|
||||
|
||||
/* vendor */
|
||||
sprintf(key,"%s/%s/%s",parent_key,elems[i].key,CW_KEY_VENDOR);
|
||||
cw_kvstore_add(cfg,key,CW_TYPE_DWORD,data + sub,4);
|
||||
cw_kvt_add(cfg,key,CW_TYPE_DWORD,data + sub,4);
|
||||
|
||||
/* version */
|
||||
sprintf(key,"%s/%s/%s",parent_key,elems[i].key,CW_KEY_VERSION);
|
||||
cw_kvstore_add(cfg,key,CW_TYPE_BSTR16,data+sub+8,l);
|
||||
cw_kvt_add(cfg,key,CW_TYPE_BSTR16,data+sub+8,l);
|
||||
|
||||
sprintf(dbgstr, "%s", key);
|
||||
cw_dbg_version_subelem(DBG_SUBELEM, dbgstr, subtype, vendor_id, data+sub+8,l);
|
||||
|
@ -3,7 +3,7 @@
|
||||
#include "capwap.h"
|
||||
#include "capwap_items.h"
|
||||
#include "dbg.h"
|
||||
#include "cw_types.h"
|
||||
#include "kvt.h"
|
||||
|
||||
|
||||
static struct cw_DescriptorSubelemDef allowed_default[] = {
|
||||
@ -19,6 +19,10 @@ int cw_read_wtp_descriptor(mavl_t cfg, struct conn *conn,
|
||||
struct cw_ElemHandler *eh, uint8_t * data, int len,
|
||||
struct cw_DescriptorSubelemDef *allowed)
|
||||
{
|
||||
printf( "toto here cw_read_wtp_descriptor\n");
|
||||
exit(0);
|
||||
|
||||
/*
|
||||
int ncrypt, pos,i;
|
||||
mavldata_t md;
|
||||
char key[64];
|
||||
@ -30,13 +34,12 @@ int cw_read_wtp_descriptor(mavl_t cfg, struct conn *conn,
|
||||
sprintf(key,"%s/%s",eh->key, "radios_in_use");
|
||||
md.kv.key=strdup(key);
|
||||
mavl_replace (cfg, cw_type_byte.get(&md,data+1,1));
|
||||
|
||||
|
||||
/* mbag_set_byte(mbag, CW_ITEM_WTP_MAX_RADIOS, cw_get_byte(data));
|
||||
mbag_set_byte(mbag, CW_ITEM_WTP_RADIOS_IN_USE, cw_get_byte(data + 1));
|
||||
*/
|
||||
|
||||
|
||||
/* Get number of encryption elements */
|
||||
|
||||
/*
|
||||
ncrypt = cw_get_byte(data + 2);
|
||||
if (ncrypt == 0) {
|
||||
if (conn->strict_capwap) {
|
||||
@ -49,15 +52,18 @@ int cw_read_wtp_descriptor(mavl_t cfg, struct conn *conn,
|
||||
}
|
||||
|
||||
pos = 3;
|
||||
for (i = 0; i < ncrypt; i++) {
|
||||
/* // It's a dummy for now */
|
||||
pos += 3;
|
||||
}
|
||||
|
||||
*/
|
||||
//
|
||||
// for (i = 0; i < ncrypt; i++) {
|
||||
///* // It's a dummy for now *
|
||||
// pos += 3;
|
||||
// }
|
||||
|
||||
/*
|
||||
if (!allowed) {
|
||||
allowed=allowed_default;
|
||||
}
|
||||
printf("call read subelems\n");
|
||||
return cw_read_descriptor_subelems(cfg,eh->key , data + pos, len - pos, allowed);
|
||||
*/
|
||||
}
|
||||
|
@ -2,9 +2,9 @@
|
||||
|
||||
#include "capwap.h"
|
||||
#include "capwap_items.h"
|
||||
#include "cw_types.h"
|
||||
#include "kvt.h"
|
||||
#include "keys.h"
|
||||
#include "kvstore.h"
|
||||
|
||||
|
||||
|
||||
static struct cw_DescriptorSubelemDef allowed_default[] = {
|
||||
@ -24,18 +24,18 @@ int cw_read_wtp_descriptor_7(mavl_t cfg, struct conn *conn,
|
||||
struct cw_ElemHandler *eh, uint8_t * data, int len,
|
||||
struct cw_DescriptorSubelemDef *allowed)
|
||||
{
|
||||
int ncrypt, pos,i;
|
||||
mavldata_t md;
|
||||
|
||||
int ncrypt, pos;
|
||||
char key[64];
|
||||
|
||||
sprintf(key,"%s/%s",eh->key, "max_radios");
|
||||
/* md.kv.key=strdup(key);
|
||||
mavl_replace (cfg, cw_type_byte.get(&md,data,1));
|
||||
*/
|
||||
cw_kvstore_add(cfg,key,CW_TYPE_BYTE,data,1);
|
||||
cw_kvt_add(cfg,key,CW_TYPE_BYTE,data,1);
|
||||
|
||||
sprintf(key,"%s/%s",eh->key, "radios_in_use");
|
||||
cw_kvstore_add(cfg,key,CW_TYPE_BYTE,data+1,1);
|
||||
cw_kvt_add(cfg,key,CW_TYPE_BYTE,data+1,1);
|
||||
|
||||
/* md.kv.key=strdup(key);
|
||||
mavl_replace (cfg, cw_type_byte.get(&md,data+1,1));
|
||||
|
@ -1,6 +1,11 @@
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/**
|
||||
* @brief Duplicate a string
|
||||
* @param s string to duplicate
|
||||
* @return duplicated string, the memory acllocated has to be freed by #free.
|
||||
*/
|
||||
char *cw_strdup(const char *s) {
|
||||
size_t size = strlen(s) + 1;
|
||||
char *p = malloc(size);
|
||||
|
@ -20,47 +20,50 @@
|
||||
|
||||
#include "format.h"
|
||||
#include "cw.h"
|
||||
#include "cw_types.h"
|
||||
#include "kvt.h"
|
||||
|
||||
|
||||
static void del(mavldata_t * data){
|
||||
free (data->kv.val.ptr);
|
||||
static void del ( struct cw_KVT * data )
|
||||
{
|
||||
free ( data->val.ptr );
|
||||
}
|
||||
|
||||
static mavldata_t *get(mavldata_t * data, const uint8_t * src, int len)
|
||||
static struct cw_KVT *get ( struct cw_KVT * data, const uint8_t * src, int len )
|
||||
{
|
||||
|
||||
uint8_t * s;
|
||||
s = bstr16_create(src,len);
|
||||
if (!s)
|
||||
s = bstr16_create ( src, len );
|
||||
|
||||
if ( !s )
|
||||
return NULL;
|
||||
|
||||
data->kv.priv = &cw_type_bstr16;
|
||||
data->kv.val.ptr=s;
|
||||
|
||||
data->type = &cw_type_bstr16;
|
||||
data->val.ptr = s;
|
||||
return data;
|
||||
}
|
||||
|
||||
static int put(mavldata_t *data, uint8_t * dst)
|
||||
static int put ( const struct cw_KVT *data, uint8_t * dst )
|
||||
{
|
||||
return cw_put_bstr16(dst, data->kv.val.ptr);
|
||||
return cw_put_bstr16 ( dst, data->val.ptr );
|
||||
}
|
||||
|
||||
static int to_str(const mavldata_t *data, char *dst, int max_len)
|
||||
static int to_str ( const struct cw_KVT *data, char *dst, int max_len )
|
||||
{
|
||||
char *d;
|
||||
d=dst;
|
||||
if (format_is_utf8(bstr16_data(data->kv.val.ptr), bstr16_len(data->kv.val.ptr))) {
|
||||
d += sprintf(d, "%.*s", bstr16_len(data->kv.val.ptr),
|
||||
bstr16_data(data->kv.val.ptr));
|
||||
d = dst;
|
||||
|
||||
if ( format_is_utf8 ( bstr16_data ( data->val.ptr ), bstr16_len ( data->val.ptr ) ) ) {
|
||||
d += sprintf ( d, "%.*s", bstr16_len ( data->val.ptr ),
|
||||
bstr16_data ( data->val.ptr ) );
|
||||
|
||||
} else {
|
||||
d += sprintf(d, ".x");
|
||||
d += format_hex(d, bstr16_data(data->kv.val.ptr), bstr16_len(data->kv.val.ptr));
|
||||
d += sprintf ( d, ".x" );
|
||||
d += format_hex ( d, bstr16_data ( data->val.ptr ), bstr16_len ( data->val.ptr ) );
|
||||
}
|
||||
|
||||
return d-dst;
|
||||
|
||||
return d - dst;
|
||||
}
|
||||
|
||||
static mavldata_t *from_str(mavldata_t * data, const char *src)
|
||||
static struct cw_KVT *from_str ( struct cw_KVT * data, const char *src )
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
@ -72,6 +75,5 @@ const struct cw_Type cw_type_bstr16 = {
|
||||
put, /* put */
|
||||
get, /* get */
|
||||
to_str, /* to_str */
|
||||
from_str /* from_str */
|
||||
from_str /* from_str */
|
||||
};
|
||||
|
||||
|
@ -19,35 +19,35 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#include "cw.h"
|
||||
#include "cw_types.h"
|
||||
#include "kvt.h"
|
||||
|
||||
static mavldata_t *get(mavldata_t * data, const uint8_t * src, int len)
|
||||
static cw_KVT_t *get(cw_KVT_t * data, const uint8_t * src, int len)
|
||||
{
|
||||
data->kv.priv = &cw_type_byte;
|
||||
data->kv.val.byte = cw_get_byte(src);
|
||||
data->type = &cw_type_byte;
|
||||
data->val.byte = cw_get_byte(src);
|
||||
return data;
|
||||
}
|
||||
|
||||
static int put(mavldata_t *data, uint8_t * dst)
|
||||
static int put(cw_KVT_t *data, uint8_t * dst)
|
||||
{
|
||||
return cw_put_byte(dst, data->kv.val.byte);
|
||||
return cw_put_byte(dst, data->val.byte);
|
||||
}
|
||||
|
||||
static int to_str(const mavldata_t *data, char *dst, int max_len)
|
||||
static int to_str(const cw_KVT_t *data, char *dst, int max_len)
|
||||
{
|
||||
return sprintf(dst, "%d", data->kv.val.byte);
|
||||
return sprintf(dst, "%d", data->val.byte);
|
||||
}
|
||||
|
||||
static mavldata_t *from_str(mavldata_t * data, const char *src)
|
||||
static cw_KVT_t *from_str(cw_KVT_t * data, const char *src)
|
||||
{
|
||||
data->kv.val.byte = atoi(src);
|
||||
data->val.byte = atoi(src);
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
const struct cw_Type cw_type_byte = {
|
||||
"Byte", /* name */
|
||||
cw_types_del_null, /* del */
|
||||
NULL, /* del */
|
||||
put, /* put */
|
||||
get, /* get */
|
||||
to_str, /* to_str */
|
||||
|
@ -19,34 +19,34 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#include "cw.h"
|
||||
#include "cw_types.h"
|
||||
#include "kvt.h"
|
||||
|
||||
static mavldata_t *get(mavldata_t * data, const uint8_t * src, int len)
|
||||
static struct cw_KVT *get(struct cw_KVT * data, const uint8_t * src, int len)
|
||||
{
|
||||
data->kv.priv = &cw_type_dword;
|
||||
data->kv.val.dword = cw_get_dword(src);
|
||||
data->type = &cw_type_dword;
|
||||
data->val.dword = cw_get_dword(src);
|
||||
return data;
|
||||
}
|
||||
|
||||
static int put(mavldata_t *data, uint8_t * dst)
|
||||
static int put(struct cw_KVT *data, uint8_t * dst)
|
||||
{
|
||||
return cw_put_dword(dst, data->kv.val.dword);
|
||||
return cw_put_dword(dst, data->val.dword);
|
||||
}
|
||||
|
||||
static int to_str(const mavldata_t *data, char *dst, int max_len)
|
||||
static int to_str(const struct cw_KVT *data, char *dst, int max_len)
|
||||
{
|
||||
return sprintf(dst, "%d", data->kv.val.dword);
|
||||
return sprintf(dst, "%d", data->val.dword);
|
||||
}
|
||||
|
||||
static mavldata_t *from_str(mavldata_t * data, const char *src)
|
||||
static struct cw_KVT *from_str(struct cw_KVT * data, const char *src)
|
||||
{
|
||||
data->kv.val.dword = atoi(src);
|
||||
data->val.dword = atoi(src);
|
||||
return data;
|
||||
}
|
||||
|
||||
const struct cw_Type cw_type_dword = {
|
||||
"Dword", /* name */
|
||||
cw_types_del_null, /* del */
|
||||
NULL, /* del */
|
||||
put, /* put */
|
||||
get, /* get */
|
||||
to_str, /* to_str */
|
||||
|
@ -18,36 +18,36 @@
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "cw_types.h"
|
||||
#include "kvt.h"
|
||||
#include "cw.h"
|
||||
|
||||
|
||||
static mavldata_t *get(mavldata_t * data, const uint8_t * src, int len)
|
||||
static struct cw_KVT *get(struct cw_KVT * data, const uint8_t * src, int len)
|
||||
{
|
||||
data->kv.priv = &cw_type_word;
|
||||
data->kv.val.word = cw_get_word(src);
|
||||
data->type = &cw_type_word;
|
||||
data->val.word = cw_get_word(src);
|
||||
return data;
|
||||
}
|
||||
|
||||
static int put(mavldata_t *data, uint8_t * dst)
|
||||
static int put(cw_KVT_t *data, uint8_t * dst)
|
||||
{
|
||||
return cw_put_word(dst, data->kv.val.word);
|
||||
return cw_put_word(dst, data->val.word);
|
||||
}
|
||||
|
||||
static int to_str(const mavldata_t *data, char *dst, int max_len)
|
||||
static int to_str(const cw_KVT_t *data, char *dst, int max_len)
|
||||
{
|
||||
return sprintf(dst, "%d", data->kv.val.word);
|
||||
return sprintf(dst, "%d", data->val.word);
|
||||
}
|
||||
|
||||
static mavldata_t *from_str(mavldata_t * data, const char *src)
|
||||
static cw_KVT_t *from_str(cw_KVT_t * data, const char *src)
|
||||
{
|
||||
data->kv.val.word = atoi(src);
|
||||
data->val.word = atoi(src);
|
||||
return data;
|
||||
}
|
||||
|
||||
const struct cw_Type cw_type_word = {
|
||||
"Word", /* name */
|
||||
cw_types_del_null, /* del */
|
||||
NULL, /* del */
|
||||
put, /* put */
|
||||
get, /* get */
|
||||
to_str, /* to_str */
|
||||
|
@ -1,48 +0,0 @@
|
||||
#ifndef __CW_TYPES_H
|
||||
#define __CW_TYPES_H
|
||||
|
||||
#include "mavl.h"
|
||||
|
||||
struct cw_Type{
|
||||
/** A human readable name for this type */
|
||||
const char *name;
|
||||
|
||||
/** A pointer to a function to delete elements of this type */
|
||||
void (*del)(mavldata_t *data);
|
||||
|
||||
/** A method to put this object to a buffer */
|
||||
int (*put)(mavldata_t * data, uint8_t*dst);
|
||||
|
||||
/** The get emthod */
|
||||
mavldata_t * (*get)(mavldata_t *data, const uint8_t*src, int len);
|
||||
|
||||
/** A pointer to a function to convert elements of this type to a string.
|
||||
This function is mainly used to store elements to an SQL database
|
||||
or to json strings */
|
||||
int (*to_str)(const mavldata_t * data, char *dst, int max_len);
|
||||
|
||||
/** Cereate an item of this type from a string, which was previously
|
||||
created by the #del function. */
|
||||
mavldata_t * (*from_str)(mavldata_t * data, const char *src);
|
||||
|
||||
/*
|
||||
int (*def)(void *, void *);
|
||||
*/
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
extern const struct cw_Type cw_type_byte;
|
||||
extern const struct cw_Type cw_type_word;
|
||||
extern const struct cw_Type cw_type_dword;
|
||||
extern const struct cw_Type cw_type_bstr16;
|
||||
|
||||
#define CW_TYPE_BYTE (&cw_type_byte)
|
||||
#define CW_TYPE_WORD (&cw_type_word)
|
||||
#define CW_TYPE_DWORD (&cw_type_dword)
|
||||
#define CW_TYPE_BSTR16 (&cw_type_bstr16)
|
||||
|
||||
void cw_types_del_null(mavldata_t *data);
|
||||
|
||||
#endif /* __CW_TYPES_H */
|
@ -1,6 +0,0 @@
|
||||
#include "mavl.h"
|
||||
#include "cw_types.h"
|
||||
|
||||
void cw_types_del_null(mavldata_t *data){
|
||||
return;
|
||||
}
|
@ -36,7 +36,7 @@
|
||||
#include "capwap_cisco.h"
|
||||
#include "lwapp_cisco.h"
|
||||
#include "cw.h"
|
||||
#include "message_set.h"
|
||||
#include "msget.h"
|
||||
|
||||
|
||||
|
||||
@ -314,7 +314,7 @@ void cw_dbg_msg(int level, struct conn *conn, uint8_t * packet, int len,
|
||||
|
||||
search.type = msg_id;
|
||||
|
||||
message = mavl_get_ptr(conn->msgset->messages,&search);
|
||||
message = mavl_get(conn->msgset->msgdata,&search);
|
||||
|
||||
|
||||
if (!message)
|
||||
|
@ -18,7 +18,7 @@ static void del(void* d)
|
||||
|
||||
struct mavl * intavltree_create()
|
||||
{
|
||||
return mavl_create(cmp,del);
|
||||
return mavl_create(cmp,del,100);
|
||||
}
|
||||
|
||||
int * intavltree_add(struct mavl * t, int val)
|
||||
@ -31,5 +31,5 @@ int * intavltree_add(struct mavl * t, int val)
|
||||
if (!v)
|
||||
return NULL;
|
||||
*v=val;
|
||||
return mavl_add(t,v);
|
||||
return mavl_add(t,v,NULL);
|
||||
}
|
||||
|
@ -61,14 +61,14 @@ const cw_itemdef_t * cw_itemdef_get(cw_itemdefheap_t t, const char *id, const ch
|
||||
|
||||
cw_itemdefheap_t cw_itemdefheap_create()
|
||||
{
|
||||
return mavl_create(cmp, NULL);
|
||||
return mavl_create(cmp, NULL,1312);
|
||||
}
|
||||
|
||||
int cw_itemdefheap_register(cw_itemdefheap_t t, cw_itemdef_t * defs)
|
||||
{
|
||||
int n = 0;
|
||||
while (defs->id != CW_ITEM_NONE) {
|
||||
cw_itemdef_t *a = mavl_add(t, defs);
|
||||
cw_itemdef_t *a = mavl_add(t, defs,NULL);
|
||||
if (a != defs) {
|
||||
cw_log(LOG_ERR, "Duplicated item: %s", defs->id);
|
||||
} else
|
||||
|
@ -1,15 +0,0 @@
|
||||
#ifndef __CW_KVSTORE_H
|
||||
#define __CW_KVSTORE_H
|
||||
|
||||
#include "mavl.h"
|
||||
#include "cw_types.h"
|
||||
|
||||
void cw_kvstore_mavl_delete(mavldata_t *data);
|
||||
const char * cw_kvstore_add(mavl_t kvstore, const char *key, const struct cw_Type *type,
|
||||
const uint8_t * data, int len);
|
||||
|
||||
#define cw_kvstore_create()\
|
||||
mavl_create(mavl_cmp_kv, cw_kvstore_mavl_delete)
|
||||
|
||||
|
||||
#endif /* __CW_KVSTORE_H */
|
70
src/cw/kvt.h
Normal file
70
src/cw/kvt.h
Normal file
@ -0,0 +1,70 @@
|
||||
#ifndef __KVT_H
|
||||
#define __KVT_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "mavl.h"
|
||||
|
||||
struct cw_KVT {
|
||||
char *key;
|
||||
union {
|
||||
uint32_t dword;
|
||||
uint16_t word;
|
||||
uint8_t byte;
|
||||
void *ptr;
|
||||
} val;
|
||||
const struct cw_Type *type;
|
||||
};
|
||||
typedef struct cw_KVT cw_KVT_t;
|
||||
|
||||
|
||||
struct cw_Type {
|
||||
/** A human readable name for this type */
|
||||
const char *name;
|
||||
|
||||
/** A pointer to a function to delete elements of this type */
|
||||
void (*del) (struct cw_KVT * data);
|
||||
|
||||
/** A method to put this object to a buffer */
|
||||
int (*put) (struct cw_KVT * data, uint8_t * dst);
|
||||
|
||||
/** The get method */
|
||||
struct cw_KVT *(*get) (struct cw_KVT * data, const uint8_t * src, int len);
|
||||
|
||||
/** A pointer to a function to convert elements of this type to a string.
|
||||
This function is mainly used to store elements to an SQL database
|
||||
or to json strings */
|
||||
int (*to_str) (const struct cw_KVT * data, char *dst, int max_len);
|
||||
|
||||
/** Cereate an item of this type from a string, which was previously
|
||||
created by the #del function. */
|
||||
struct cw_KVT *(*from_str) (struct cw_KVT * data, const char *src);
|
||||
|
||||
/*
|
||||
int (*def)(void *, void *);
|
||||
*/
|
||||
};
|
||||
|
||||
extern const struct cw_Type cw_type_byte;
|
||||
extern const struct cw_Type cw_type_word;
|
||||
extern const struct cw_Type cw_type_dword;
|
||||
extern const struct cw_Type cw_type_bstr16;
|
||||
|
||||
#define CW_TYPE_BYTE (&cw_type_byte)
|
||||
#define CW_TYPE_WORD (&cw_type_word)
|
||||
#define CW_TYPE_DWORD (&cw_type_dword)
|
||||
#define CW_TYPE_BSTR16 (&cw_type_bstr16)
|
||||
|
||||
/*
|
||||
void cw_kvstore_mavl_delete(const void *data);
|
||||
*/
|
||||
const char *cw_kvt_add(mavl_t kvstore, const char *key, const struct cw_Type *type,
|
||||
const uint8_t * data, int len);
|
||||
|
||||
int cw_kvt_mavlcmp(const void *v1, const void *v2);
|
||||
void cw_kvt_mavldel(void *data);
|
||||
|
||||
#define cw_kvt_create()\
|
||||
mavl_create(cw_kvt_mavlcmp, cw_kvt_mavldel, sizeof(cw_KVT_t))
|
||||
|
||||
#endif /* __KVT_H */
|
206
src/cw/mavl.h
206
src/cw/mavl.h
@ -17,17 +17,18 @@
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief MAVL, Mini AVL Tree,
|
||||
* Yet another AVL Tree implementation!
|
||||
* @brief MAVL, Mini AVL Tree,
|
||||
* Yet another AVL Tree implementation
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @defgroup MAVL MAVL
|
||||
/**
|
||||
* @addtogroup DATAMGMT
|
||||
* @{
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#ifndef __MAVL_H
|
||||
#define __MAVL_H
|
||||
|
||||
@ -38,43 +39,27 @@
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @defgroup MAV_CONSTANTS Constants
|
||||
* @defgroup MAVL_CONSTANTS Constants
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** Maximum AVL Tree depth.
|
||||
/** Maximum AVL Tree depth.
|
||||
The number of nodes is calculated by 2^depth.
|
||||
So a value of 32 should be enough for around 4
|
||||
So a value of 32 should be enough for around 4
|
||||
billion nodes. */
|
||||
#define MAVL_MAX_DEPTH 32
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
struct mavl_KeyVal{
|
||||
char *key;
|
||||
const void *priv;
|
||||
union{
|
||||
void * ptr;
|
||||
uint32_t dword;
|
||||
uint16_t word;
|
||||
uint8_t byte;
|
||||
char * str;
|
||||
}val;
|
||||
};
|
||||
|
||||
union mavldata {
|
||||
void *ptr;
|
||||
uint32_t dword;
|
||||
uint16_t word;
|
||||
uint8_t byte;
|
||||
char *str;
|
||||
struct mavl_KeyVal kv;
|
||||
};
|
||||
typedef union mavldata mavldata_t;
|
||||
|
||||
/**
|
||||
* @addtogroup MAVLStructures Structures
|
||||
* @{
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
@ -82,8 +67,8 @@ typedef union mavldata mavldata_t;
|
||||
*/
|
||||
struct mavlnode {
|
||||
/** Pointer to data, that belongs to the node */
|
||||
|
||||
/* union mavldata data; */
|
||||
|
||||
/* union mavldata data; */
|
||||
|
||||
/** Pointer to left son*/
|
||||
struct mavlnode *left;
|
||||
@ -93,30 +78,32 @@ struct mavlnode {
|
||||
int bal;
|
||||
};
|
||||
|
||||
#define mavlnode_data(node) ((node)+sizeof(struct mavlnode))
|
||||
|
||||
#define mavlnode_copy_data(tree,dst,src)\
|
||||
memcpy(mavlnode_data(dst),mavlnode_data(src),\
|
||||
(tree)->data_size)
|
||||
|
||||
/**
|
||||
* AVL Tree
|
||||
*/
|
||||
*/
|
||||
struct mavl {
|
||||
/** Pointer to root node */
|
||||
struct mavlnode *root;
|
||||
/** Compare function */
|
||||
int (*cmp) (const union mavldata *, const union mavldata *);
|
||||
int ( *cmp ) ( const void *, const void * );
|
||||
/** Delete element function */
|
||||
void (*del) (union mavldata *);
|
||||
void ( *del ) ( void * );
|
||||
/** Number of elements currently stored in the tree */
|
||||
int count;
|
||||
|
||||
/** size of data appended to each mavlnode element.
|
||||
* used to allocate space in #mavl_add. */
|
||||
size_t data_size;
|
||||
};
|
||||
|
||||
/**
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* MAVL AVL Tree type
|
||||
*/
|
||||
*/
|
||||
typedef struct mavl * mavl_t;
|
||||
|
||||
|
||||
@ -125,33 +112,40 @@ typedef struct mavl * mavl_t;
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param node node
|
||||
*/
|
||||
#define mavlnode_dataptr(node) (((uint8_t*)(node))+sizeof(struct mavlnode))
|
||||
|
||||
|
||||
struct mavl *mavl_create(int (*cmp) (const void *, const void *),
|
||||
void (*del) (void *), size_t data_size);
|
||||
struct mavl *mavl_create ( int ( *cmp ) ( const void *, const void * ),
|
||||
void ( *del ) ( void * ), size_t data_size );
|
||||
|
||||
void mavlnode_destroy(struct mavl *t, struct mavlnode *n);
|
||||
|
||||
void mavl_del_all(struct mavl *t);
|
||||
union mavldata *mavl_del(struct mavl *t, union mavldata *data);
|
||||
union mavldata *mavl_add(struct mavl *t, union mavldata *data);
|
||||
union mavldata * mavl_get(struct mavl *t ,union mavldata *data);
|
||||
struct mavlnode *mavl_get_node(struct mavl *t, void *data);
|
||||
void * mavl_get_ptr(mavl_t tree, void * search);
|
||||
void *mavl_add_ptr(mavl_t tree, void *ptr);
|
||||
|
||||
int mavl_cmp_kv(const union mavldata *e1, const union mavldata *e2);
|
||||
union mavldata *mavl_add_kv_byte(mavl_t tree, const char *key, uint8_t val);
|
||||
|
||||
void mavl_merge(mavl_t m, mavl_t t);
|
||||
void *mavl_add ( struct mavl *t, const void *data, int *exists );
|
||||
/*void *mavl_add ( struct mavl *t, const void *data );*/
|
||||
void * mavl_get ( struct mavl *t , void *data );
|
||||
void *mavl_del ( struct mavl *t, const void *data );
|
||||
void *mavl_replace ( struct mavl *t, const void *data, int * result );
|
||||
void mavl_destroy ( struct mavl *t );
|
||||
void mavl_del_all ( struct mavl *t );
|
||||
void mavl_merge ( mavl_t m, mavl_t t );
|
||||
void mavlnode_destroy ( struct mavl *t, struct mavlnode *n );
|
||||
struct mavlnode *mavl_get_node ( struct mavl *t, void *data );
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
extern int mavl_foreach_lr ( struct mavlnode *n, int ( *callback ) ( void *, void * ),
|
||||
void *cbpriv );
|
||||
extern int mavl_foreach_rl ( struct mavlnode *n, int ( *callback ) ( void *, void * ),
|
||||
void *cbpriv );
|
||||
int mavl_foreach_from_lr ( struct mavl *t, struct mavlnode *n, void *data,
|
||||
int ( *callback ) ( void *, void * ), void *cbpriv );
|
||||
|
||||
|
||||
extern int mavl_foreach_lr(struct mavlnode *n, int (*callback) (void *, void *),
|
||||
void *cbpriv);
|
||||
extern int mavl_foreach_rl(struct mavlnode *n, int (*callback) (void *, void *),
|
||||
void *cbpriv);
|
||||
int mavl_foreach_from_lr(struct mavl *t, struct mavlnode *n, void *data,
|
||||
int (*callback) (void *, void *), void *cbpriv);
|
||||
|
||||
/**
|
||||
* @}
|
||||
@ -159,12 +153,8 @@ int mavl_foreach_from_lr(struct mavl *t, struct mavlnode *n, void *data,
|
||||
|
||||
|
||||
|
||||
void *mavl_replace_data(struct mavl *t, void *data, int len);
|
||||
void *mavl_replace_data ( struct mavl *t, void *data, int len );
|
||||
|
||||
union mavldata *mavl_replace(struct mavl *t,union mavldata *data);
|
||||
|
||||
|
||||
void mavl_destroy(struct mavl *t);
|
||||
|
||||
|
||||
|
||||
@ -179,50 +169,52 @@ void mavl_destroy(struct mavl *t);
|
||||
#define mavl_foreach_from_asc(t,d,cb,priv) mavl_foreach_from_lr(t,(t)->root,d,cb,priv);
|
||||
|
||||
|
||||
struct mavliter{
|
||||
struct mavlnode *stack[MAVL_MAX_DEPTH*2];
|
||||
|
||||
struct mavliter {
|
||||
struct mavlnode *stack[MAVL_MAX_DEPTH * 2];
|
||||
|
||||
struct mavlnode *cur;
|
||||
int stack_ptr;
|
||||
struct mavlnode * root;
|
||||
int (*cmp) (const union mavldata *, const union mavldata *);
|
||||
|
||||
int ( *cmp ) ( const void *, const void * );
|
||||
|
||||
};
|
||||
typedef struct mavliter mavliter_t;
|
||||
|
||||
|
||||
union mavldata * mavliter_next(mavliter_t *i);
|
||||
union mavldata * mavliter_next ( mavliter_t *i );
|
||||
|
||||
union mavldata * mavliter_seek_set(struct mavliter *i);
|
||||
union mavldata * mavliter_seek_set ( struct mavliter *i );
|
||||
|
||||
void mavliter_init(mavliter_t *i, mavl_t t);
|
||||
void mavliter_init ( mavliter_t *i, mavl_t t );
|
||||
|
||||
|
||||
void * mavliter_get(mavliter_t *i);
|
||||
|
||||
extern union mavldata * mavliter_seek(mavliter_t *i,void *d);
|
||||
void * mavliter_get ( mavliter_t *i );
|
||||
|
||||
/**
|
||||
* Define a AVL Iterator varialble and accociate it with
|
||||
void * mavliter_get_ptr(mavliter_t *i);
|
||||
|
||||
extern union mavldata * mavliter_seek ( mavliter_t *i, void *d );
|
||||
|
||||
/**
|
||||
* Define a AVL Iterator varialble and accociate it with
|
||||
* an AVL Tree.
|
||||
* @param i Name of the variable to define
|
||||
* @param t #mavl_t Tree to associate
|
||||
* @see #mavliter_get #mavliter_foreach
|
||||
*
|
||||
* Example:
|
||||
* \code
|
||||
* \code
|
||||
mavl_t datatree = mavl_create();
|
||||
|
||||
// fill up datatree with some values
|
||||
|
||||
|
||||
// fill up datatree with some values
|
||||
|
||||
MAVLITER_DEFINE (i,datatree);
|
||||
mavliter_foreach(&i) {
|
||||
// Get the current value
|
||||
void * value = mavliter_get(&i);
|
||||
|
||||
|
||||
}
|
||||
\endcode
|
||||
*/
|
||||
*/
|
||||
#define MAVLITER_DEFINE(iterator,mavl_obj)\
|
||||
mavliter_t iterator; mavliter_init(&iterator,mavl_obj)
|
||||
|
||||
@ -230,7 +222,7 @@ extern union mavldata * mavliter_seek(mavliter_t *i,void *d);
|
||||
/**
|
||||
* Iterate through all elements of an MAVL Object using a MAVL Iterator.
|
||||
* @param i pointer to MAVL Interator object
|
||||
*/
|
||||
*/
|
||||
#define mavliter_foreach(iterator)\
|
||||
for (mavliter_seek_set(iterator); NULL != mavliter_get(iterator); mavliter_next(iterator))
|
||||
|
||||
@ -252,11 +244,9 @@ extern union mavldata * mavliter_seek(mavliter_t *i,void *d);
|
||||
typedef mavl_t mavl_conststr_t;
|
||||
extern mavl_conststr_t mavl_create_conststr();
|
||||
|
||||
int mavl_cmp_dword(const union mavldata *e1, const union mavldata *e2);
|
||||
int mavl_cmp_str(const union mavldata *e1, const union mavldata *e2);
|
||||
|
||||
void mavl_free_bin(union mavldata *data);
|
||||
void mavl_free_str(union mavldata *data);
|
||||
/*
|
||||
//int mavl_cmp_str ( const union mavldata *e1, const union mavldata *e2 );
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
@ -273,16 +263,34 @@ static inline void *mavl_replace(struct mavl *t,void *data){
|
||||
struct mavlnode * node = mavl_get_node(t,data);
|
||||
if (node){
|
||||
t->del(node->data);
|
||||
return node->data=data;
|
||||
return node->data=data;
|
||||
}
|
||||
return mavl_add(t,data);
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
|
||||
/** @} */
|
||||
|
||||
void mavl_freeptr(void *ptr);
|
||||
#define mavl_create_ptr(cmp,del)\
|
||||
mavl_create(cmp,del,sizeof(void*))
|
||||
int mavl_cmpstr(const void *p1, const void *p2);
|
||||
#define mavl_create_str() mavl_create_ptr(mavl_cmpstr,mavl_freeptr)
|
||||
|
||||
void * mavl_get_ptr ( mavl_t tree, void * search );
|
||||
void * mavl_add_ptr ( mavl_t tree, void *ptr );
|
||||
|
||||
#define mavl_add_str(tree,str) mavl_add_ptr(tree,str)
|
||||
#define mavl_get_str(tree,search) ((char *)(mavl_get_ptr(tree,search)))
|
||||
#define mavliter_get_str(iter) ((char*)(mavliter_get_ptr(iter)))
|
||||
|
||||
/**
|
||||
*
|
||||
* @}
|
||||
* datamanag
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
@ -1,45 +1,52 @@
|
||||
#include "mavl.h"
|
||||
|
||||
static struct mavlnode *mavlnode_create(const union mavldata *data)
|
||||
static struct mavlnode *mavlnode_create ( struct mavl *t, const void *data )
|
||||
{
|
||||
struct mavlnode *n = malloc(sizeof(struct mavlnode));
|
||||
if (!n)
|
||||
struct mavlnode *n = malloc ( sizeof ( struct mavlnode ) + t->data_size );
|
||||
|
||||
if ( !n )
|
||||
return NULL;
|
||||
|
||||
n->left = n->right = 0;
|
||||
|
||||
n->left = n->right = NULL;
|
||||
n->bal = 0;
|
||||
n->data = *data;
|
||||
|
||||
/*n->data = *data;*/
|
||||
memcpy ( mavlnode_dataptr ( n ), data, t->data_size );
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
static int mavl_add0(struct mavl *t, struct mavlnode **parent, union mavldata ** data)
|
||||
static int mavl_add0 ( struct mavl *t, struct mavlnode **parent, const void ** data )
|
||||
{
|
||||
struct mavlnode *tmp;
|
||||
|
||||
struct mavlnode *n = *parent;
|
||||
int rc = t->cmp(*data, &n->data);
|
||||
|
||||
int rc = t->cmp ( *data, mavlnode_dataptr ( n ) );
|
||||
|
||||
int bal;
|
||||
if (rc == 0) {
|
||||
*data = &n->data;
|
||||
|
||||
if ( rc == 0 ) {
|
||||
/* *data = &n->data;*/
|
||||
|
||||
/* memcpy(data,mavlnode_dataptr(n),t->data_size);*/
|
||||
*data = mavlnode_dataptr ( n );
|
||||
return 2;
|
||||
}
|
||||
|
||||
if (rc < 0) {
|
||||
if (n->left) {
|
||||
bal = mavl_add0(t, &n->left, data);
|
||||
if (bal > 1)
|
||||
|
||||
if ( rc < 0 ) {
|
||||
if ( n->left ) {
|
||||
bal = mavl_add0 ( t, &n->left, data );
|
||||
|
||||
if ( bal > 1 )
|
||||
return bal;
|
||||
|
||||
|
||||
n->bal -= bal;
|
||||
if (n->bal == 0)
|
||||
|
||||
if ( n->bal == 0 )
|
||||
return 0;
|
||||
|
||||
if (n->bal == -2) {
|
||||
if (n->left->bal == -1) {
|
||||
|
||||
if ( n->bal == -2 ) {
|
||||
if ( n->left->bal == -1 ) {
|
||||
n->bal = 0;
|
||||
n->left->bal = 0;
|
||||
*parent = n->left;
|
||||
@ -48,61 +55,70 @@ static int mavl_add0(struct mavl *t, struct mavlnode **parent, union mavldata **
|
||||
n->left = tmp;
|
||||
return 0;
|
||||
}
|
||||
if (n->left->bal == 1) {
|
||||
|
||||
if ( n->left->bal == 1 ) {
|
||||
*parent = n->left->right;
|
||||
if ((*parent)->bal == 1) {
|
||||
|
||||
if ( ( *parent )->bal == 1 ) {
|
||||
n->bal = 0;
|
||||
n->left->bal = -1;
|
||||
} else if ((*parent)->bal == -1) {
|
||||
|
||||
} else if ( ( *parent )->bal == -1 ) {
|
||||
n->bal = 1;
|
||||
n->left->bal = 0;
|
||||
|
||||
} else {
|
||||
n->bal = 0;
|
||||
n->left->bal = 0;
|
||||
}
|
||||
|
||||
(*parent)->bal = 0;
|
||||
n->left->right = (*parent)->left;
|
||||
(*parent)->left = n->left;
|
||||
tmp = (*parent)->right;
|
||||
(*parent)->right = n;
|
||||
|
||||
( *parent )->bal = 0;
|
||||
n->left->right = ( *parent )->left;
|
||||
( *parent )->left = n->left;
|
||||
tmp = ( *parent )->right;
|
||||
( *parent )->right = n;
|
||||
n->left = tmp;
|
||||
return 0;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
return bal;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/* n->left is 0 */
|
||||
n->left = mavlnode_create(*data);
|
||||
if (!n->left)
|
||||
n->left = mavlnode_create ( t, *data );
|
||||
*data = mavlnode_dataptr(n->left);
|
||||
|
||||
if ( !n->left )
|
||||
return 3;
|
||||
|
||||
|
||||
t->count++;
|
||||
|
||||
if (n->right == 0) {
|
||||
|
||||
if ( n->right == 0 ) {
|
||||
n->bal = -1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
n->bal = 0;
|
||||
return 0;
|
||||
|
||||
|
||||
} else {
|
||||
if (n->right) {
|
||||
bal = mavl_add0(t, &n->right, data);
|
||||
if (bal > 1)
|
||||
if ( n->right ) {
|
||||
bal = mavl_add0 ( t, &n->right, data );
|
||||
|
||||
if ( bal > 1 )
|
||||
return bal;
|
||||
|
||||
|
||||
n->bal += bal;
|
||||
if (n->bal == 0)
|
||||
|
||||
if ( n->bal == 0 )
|
||||
return 0;
|
||||
|
||||
if (n->bal == 2) {
|
||||
if (n->right->bal == 1) {
|
||||
|
||||
if ( n->bal == 2 ) {
|
||||
if ( n->right->bal == 1 ) {
|
||||
n->bal = 0;
|
||||
n->right->bal = 0;
|
||||
*parent = n->right;
|
||||
@ -110,45 +126,52 @@ static int mavl_add0(struct mavl *t, struct mavlnode **parent, union mavldata **
|
||||
n->right->left = n;
|
||||
n->right = tmp;
|
||||
return 0;
|
||||
} else if (n->right->bal == -1) {
|
||||
|
||||
} else if ( n->right->bal == -1 ) {
|
||||
*parent = n->right->left;
|
||||
|
||||
if ((*parent)->bal == -1) {
|
||||
|
||||
if ( ( *parent )->bal == -1 ) {
|
||||
n->bal = 0;
|
||||
n->right->bal = 1;
|
||||
} else if ((*parent)->bal == 1) {
|
||||
|
||||
} else if ( ( *parent )->bal == 1 ) {
|
||||
n->bal = -1;
|
||||
n->right->bal = 0;
|
||||
|
||||
} else {
|
||||
n->bal = 0;
|
||||
n->right->bal = 0;
|
||||
}
|
||||
|
||||
(*parent)->bal = 0;
|
||||
n->right->left = (*parent)->right;
|
||||
(*parent)->right = n->right;
|
||||
tmp = (*parent)->left;
|
||||
(*parent)->left = n;
|
||||
|
||||
( *parent )->bal = 0;
|
||||
n->right->left = ( *parent )->right;
|
||||
( *parent )->right = n->right;
|
||||
tmp = ( *parent )->left;
|
||||
( *parent )->left = n;
|
||||
n->right = tmp;
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return bal;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/* n->right is 0 */
|
||||
|
||||
n->right = mavlnode_create ( t, *data );
|
||||
*data = mavlnode_dataptr(n->right);
|
||||
|
||||
n->right = mavlnode_create(*data);
|
||||
if (!n->right)
|
||||
|
||||
if ( !n->right )
|
||||
return 3;
|
||||
|
||||
|
||||
t->count++;
|
||||
if (n->left == 0) {
|
||||
|
||||
if ( n->left == 0 ) {
|
||||
n->bal = 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
n->bal = 0;
|
||||
return 0;
|
||||
}
|
||||
@ -157,41 +180,54 @@ static int mavl_add0(struct mavl *t, struct mavlnode **parent, union mavldata **
|
||||
|
||||
/**
|
||||
* Add an element to an AVL tree
|
||||
*
|
||||
*
|
||||
* @param t mavl
|
||||
* @param data pointer to element
|
||||
*
|
||||
* @return A pointer to the element in the tree. If teh returned pointer
|
||||
*
|
||||
* @return A pointer to the element in the tree. If teh returned pointer
|
||||
* equals to the pointer given in the data argument, the element has already
|
||||
* been in the tree. If the function returns NULL, there was en error, e.g.
|
||||
* no memory left. Consult therefore errno.
|
||||
* no memory left. Consult therefore errno.
|
||||
*
|
||||
* @example mavl_add_example.c
|
||||
*/
|
||||
|
||||
union mavldata *mavl_add(struct mavl *t, union mavldata *data)
|
||||
|
||||
void *mavl_add ( struct mavl *t, const void *data, int *exists )
|
||||
{
|
||||
union mavldata * d;
|
||||
const void * d;
|
||||
int rc;
|
||||
|
||||
|
||||
if (t->root == NULL) {
|
||||
t->root = mavlnode_create(data);
|
||||
if (t->root)
|
||||
t->count++;
|
||||
if ( t->root == NULL ) {
|
||||
t->root = mavlnode_create ( t, data );
|
||||
|
||||
if ( t->root )
|
||||
t->count++;
|
||||
|
||||
/* return &t->root->data; */
|
||||
return data;
|
||||
if (exists != NULL)
|
||||
*exists=0;
|
||||
return mavlnode_dataptr(t->root);
|
||||
}
|
||||
|
||||
|
||||
d = data;
|
||||
|
||||
rc = mavl_add0(t, &t->root, &d);
|
||||
|
||||
if (rc > 3)
|
||||
|
||||
rc = mavl_add0 ( t, &t->root, &d );
|
||||
|
||||
if ( rc >= 3 )
|
||||
return NULL;
|
||||
|
||||
|
||||
if ( exists != NULL){
|
||||
if (rc==2)
|
||||
*exists=1;
|
||||
else
|
||||
*exists=0;
|
||||
}
|
||||
return d;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
void *mavl_add ( struct mavl *t, const void *data)
|
||||
{
|
||||
return mavl_add_exists(t,data,NULL);
|
||||
}
|
||||
*/
|
@ -1,9 +0,0 @@
|
||||
#include "mavl.h"
|
||||
|
||||
union mavldata *mavl_add_kv_byte(mavl_t tree, const char *key, uint8_t val)
|
||||
{
|
||||
mavldata_t data;
|
||||
data.kv.key = key;
|
||||
data.kv.val.byte = val;
|
||||
return mavl_add(tree, &data);
|
||||
}
|
@ -1,10 +1,9 @@
|
||||
#include "mavl.h"
|
||||
|
||||
void *mavl_add_ptr(mavl_t tree, void *ptr){
|
||||
mavldata_t data, *result;
|
||||
data.ptr = ptr;
|
||||
result = mavl_add(tree,&data);
|
||||
void * result;
|
||||
result = mavl_add(tree,&ptr,NULL);
|
||||
if (result)
|
||||
return result->ptr;
|
||||
return *(void**)result;
|
||||
return NULL;
|
||||
}
|
||||
|
@ -1,17 +0,0 @@
|
||||
#include "mavl.h"
|
||||
|
||||
const char * mavl_add_strdup(mavl_t t, const char * str)
|
||||
{
|
||||
mavldata_t s, *result;
|
||||
s.str = strdup(str);
|
||||
if (!s.str)
|
||||
return NULL;
|
||||
|
||||
result = mavl_add(t,&s);
|
||||
if (!result){
|
||||
free(s.str);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return result->str;
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
#include "mavl.h"
|
||||
|
||||
int mavl_cmp_dword(const union mavldata *e1, const union mavldata *e2){
|
||||
return e1->dword - e2->dword;
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
#include "mavl.h"
|
||||
|
||||
int mavl_cmp_kv(const union mavldata *e1, const union mavldata *e2){
|
||||
return strcmp(e1->kv.key, e2->kv.key);
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
|
||||
#include "mavl.h"
|
||||
|
||||
int mavl_cmp_str(const union mavldata *e1, const union mavldata *e2){
|
||||
return strcmp (e1->str,e2->str);
|
||||
}
|
6
src/cw/mavl_cmpstr.c
Normal file
6
src/cw/mavl_cmpstr.c
Normal file
@ -0,0 +1,6 @@
|
||||
#include "mavl.h"
|
||||
|
||||
int mavl_cmpstr (const void *s1, const void *s2)
|
||||
{
|
||||
return strcmp( *((const char**)s1), *((const char**)s2));
|
||||
}
|
@ -18,19 +18,21 @@
|
||||
|
||||
/**
|
||||
*@file
|
||||
*@Implementation of mavl_create
|
||||
*@brief Implementation of mavl_create
|
||||
*@addtogroup MavlFunctions
|
||||
*@{
|
||||
*/
|
||||
*/
|
||||
|
||||
#include "mavl.h"
|
||||
|
||||
/**
|
||||
* Create an AVL tree
|
||||
* @param cmp pointer compare function
|
||||
* @param del pointer to delete function which is called when an element will be deletet
|
||||
* @return pointer to an #mavl struct
|
||||
* @param cmp pointer to compare function
|
||||
* @param del pointer to delete function which is called when an element
|
||||
* will be deletet
|
||||
* @return pointer to a #mavl struct
|
||||
*/
|
||||
struct mavl *mavl_create(int (*cmp) (const void *, const void *),
|
||||
struct mavl *mavl_create(int (*cmp) (const void *, const void *),
|
||||
void (*del) (void *), size_t data_size)
|
||||
{
|
||||
struct mavl *t = malloc(sizeof(struct mavl));
|
||||
@ -40,6 +42,7 @@
|
||||
t->count = 0;
|
||||
t->cmp = cmp;
|
||||
t->del = del;
|
||||
t->data_size=data_size;
|
||||
return t;
|
||||
}
|
||||
|
||||
|
@ -39,5 +39,5 @@ static int cmp(const void *v1,const void*v2)
|
||||
|
||||
mavl_conststr_t mavl_create_conststr()
|
||||
{
|
||||
return mavl_create(cmp,NULL);
|
||||
return mavl_create(cmp,NULL,1312);
|
||||
}
|
||||
|
@ -3,7 +3,7 @@
|
||||
void mavlnode_destroy(struct mavl *t, struct mavlnode *n)
|
||||
{
|
||||
if (t->del) {
|
||||
t->del(&n->data);
|
||||
t->del(mavlnode_dataptr(n));
|
||||
}
|
||||
free(n);
|
||||
t->count--;
|
||||
@ -107,12 +107,12 @@ static int adj_bal_r(struct mavlnode *n, struct mavlnode **parent)
|
||||
|
||||
|
||||
|
||||
static int mavl_del_lo(struct mavlnode **parent, union mavldata *data)
|
||||
static int mavl_del_lo(mavl_t t, struct mavlnode **parent, void *data)
|
||||
{
|
||||
struct mavlnode *n = *parent;
|
||||
|
||||
if (n->left != 0) {
|
||||
int bal = mavl_del_lo(&n->left, data);
|
||||
int bal = mavl_del_lo(t, &n->left, data);
|
||||
n->bal += bal;
|
||||
if (n->bal == 1) {
|
||||
return 0;
|
||||
@ -126,7 +126,9 @@ static int mavl_del_lo(struct mavlnode **parent, union mavldata *data)
|
||||
/* found the lowest element */
|
||||
|
||||
*parent = n->right;
|
||||
*data = n->data;
|
||||
/* *data = n->data;*/
|
||||
memcpy(data,mavlnode_dataptr(n),t->data_size);
|
||||
|
||||
free(n);
|
||||
return 1;
|
||||
|
||||
@ -143,12 +145,12 @@ static int mavl_del_lo(struct mavlnode **parent, union mavldata *data)
|
||||
|
||||
|
||||
|
||||
int mavl_del0(struct mavl *t, struct mavlnode **parent, union mavldata *data)
|
||||
int mavl_del0(struct mavl *t, struct mavlnode **parent, void *data)
|
||||
{
|
||||
struct mavlnode *n = *parent;
|
||||
int rc;
|
||||
int bal;
|
||||
rc = t->cmp(data, &n->data);
|
||||
rc = t->cmp(data, mavlnode_dataptr(n));
|
||||
|
||||
if (rc == 0) {
|
||||
if (n->right == 0 && n->left == 0) {
|
||||
@ -174,10 +176,10 @@ int mavl_del0(struct mavl *t, struct mavlnode **parent, union mavldata *data)
|
||||
/* node has two childs */
|
||||
|
||||
if (t->del) {
|
||||
t->del(&n->data);
|
||||
t->del(mavlnode_dataptr(n));
|
||||
}
|
||||
t->count--;
|
||||
bal = mavl_del_lo(&n->right, &n->data);
|
||||
bal = mavl_del_lo(t,&n->right, mavlnode_dataptr(n));
|
||||
n->bal -= bal;
|
||||
if (n->bal == -1)
|
||||
return 0;
|
||||
@ -228,9 +230,9 @@ int mavl_del0(struct mavl *t, struct mavlnode **parent, union mavldata *data)
|
||||
|
||||
}
|
||||
|
||||
union mavldata *mavl_del(struct mavl *t, union mavldata *data)
|
||||
void *mavl_del(struct mavl *t, const void *data)
|
||||
{
|
||||
union mavldata *d;
|
||||
void *d;
|
||||
int rc;
|
||||
|
||||
if (!t->root)
|
||||
|
@ -24,7 +24,7 @@ int mavl_foreach_rl(struct mavlnode *n, int (*callback)(void *,void *),void *cbp
|
||||
return 1;
|
||||
if (!mavl_foreach_rl(n->right,callback,cbpriv))
|
||||
return 0;
|
||||
if (!callback(cbpriv,&n->data))
|
||||
if (!callback(cbpriv,mavlnode_dataptr(n)))
|
||||
return 0;
|
||||
return mavl_foreach_rl(n->left,callback,cbpriv);
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ int mavl_foreach_lr(struct mavlnode *n, int (*callback)(void *,void *),void *cbp
|
||||
return 1;
|
||||
if (!mavl_foreach_lr(n->left,callback,cbpriv))
|
||||
return 0;
|
||||
if (!callback(cbpriv,&n->data))
|
||||
if (!callback(cbpriv,mavlnode_dataptr(n)))
|
||||
return 0;
|
||||
return mavl_foreach_lr(n->right,callback,cbpriv);
|
||||
|
||||
|
@ -1,7 +0,0 @@
|
||||
|
||||
#include "mavl.h"
|
||||
|
||||
void mavl_free_bin(union mavldata *data){
|
||||
free(data->ptr);
|
||||
}
|
||||
|
@ -1,7 +0,0 @@
|
||||
|
||||
#include "mavl.h"
|
||||
|
||||
void mavl_free_str(union mavldata *data){
|
||||
free(data->str);
|
||||
}
|
||||
|
5
src/cw/mavl_freeptr.c
Normal file
5
src/cw/mavl_freeptr.c
Normal file
@ -0,0 +1,5 @@
|
||||
#include "mavl.h"
|
||||
|
||||
void mavl_freeptr(void *ptr){
|
||||
free( *((void**)ptr) );
|
||||
}
|
@ -29,13 +29,13 @@
|
||||
* @param data Element to get
|
||||
* @return pointer to element or NULL if not found.
|
||||
*/
|
||||
union mavldata * mavl_get(struct mavl *t ,union mavldata *data)
|
||||
void * mavl_get(struct mavl *t ,void *data)
|
||||
{
|
||||
struct mavlnode *n = t->root;
|
||||
while(n){
|
||||
int rc=t->cmp(data,&n->data);
|
||||
int rc=t->cmp(data,mavlnode_dataptr(n));
|
||||
if (rc==0)
|
||||
return &n->data;
|
||||
return mavlnode_dataptr(n);
|
||||
if (rc<0)
|
||||
n=n->left;
|
||||
else
|
||||
|
@ -26,7 +26,7 @@ struct mavlnode * mavl_get_node(struct mavl *t ,void *data)
|
||||
{
|
||||
struct mavlnode *n = t->root;
|
||||
while(n){
|
||||
int rc=t->cmp(data,&n->data);
|
||||
int rc=t->cmp(data,mavlnode_dataptr(n));
|
||||
if (rc==0)
|
||||
return n;
|
||||
if (rc<0)
|
||||
|
@ -2,10 +2,10 @@
|
||||
|
||||
void * mavl_get_ptr(mavl_t tree, void * search)
|
||||
{
|
||||
mavldata_t search_data, *result;
|
||||
search_data.ptr=search;
|
||||
result = mavl_find(tree,&search_data);
|
||||
void * result;
|
||||
|
||||
result = mavl_find(tree,&search);
|
||||
if (result)
|
||||
return result->ptr;
|
||||
return *(void**)result;
|
||||
return NULL;
|
||||
}
|
||||
|
@ -27,17 +27,17 @@
|
||||
|
||||
static void mavlnode_move(mavl_t m,mavl_t t, struct mavlnode *n)
|
||||
{
|
||||
struct mavlnode * mn = mavl_get_node(m,mavlnode_data(n));
|
||||
struct mavlnode * mn = mavl_get_node(m,mavlnode_dataptr(n));
|
||||
if (mn) {
|
||||
if (m->del) {
|
||||
m->del(mavlnode_data(mn));
|
||||
m->del(mavlnode_dataptr(mn));
|
||||
}
|
||||
/*mn->data=n->data;*/
|
||||
mavlnode_copy_data(m,mn,n);
|
||||
memcpy(mavlnode_dataptr(mn),mavlnode_dataptr(n),t->data_size);
|
||||
}
|
||||
else{
|
||||
/*mavl_add(m,&n->data);*/
|
||||
mavl_add(m,mavlnode_data(n));
|
||||
mavl_add(m,mavlnode_dataptr(n),NULL);
|
||||
|
||||
}
|
||||
free(n);
|
||||
|
@ -1,15 +1,38 @@
|
||||
#include "mavl.h"
|
||||
|
||||
union mavldata *mavl_replace(struct mavl *t,void *data){
|
||||
void *mavl_replace(struct mavl *t,const void *data, int * replaced){
|
||||
|
||||
int exists;
|
||||
void * add = mavl_add(t,data,&exists);
|
||||
|
||||
if (replaced != NULL){
|
||||
*replaced = exists;
|
||||
}
|
||||
|
||||
if (!exists){
|
||||
return add;
|
||||
}
|
||||
|
||||
if(t->del){
|
||||
t->del(add);
|
||||
}
|
||||
|
||||
memcpy(add,data,t->data_size);
|
||||
return add;
|
||||
|
||||
/*
|
||||
struct mavlnode * node = mavl_get_node(t,data);
|
||||
if (node){
|
||||
if (t->del)
|
||||
t->del(node+sizeof(struct mavlnode));
|
||||
/* node->data=*data;
|
||||
*/ mavlnode_copy_data(node+sizeof(struct mavlnode), data);
|
||||
return &node->data;
|
||||
// node->data=*data;
|
||||
memcpy(mavlnode_dataptr(node), data, t->data_size);
|
||||
|
||||
// return &node->data;
|
||||
return mavlnode_dataptr(node);
|
||||
}
|
||||
return mavl_add(t,data);
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
|
@ -5,10 +5,10 @@
|
||||
* @param i AVL Iterator
|
||||
* @return element or NULL if not found.
|
||||
*/
|
||||
void * mavliter_get(mavliter_t *i){
|
||||
if(!i->cur)
|
||||
void * mavliter_get(mavliter_t *i)
|
||||
{
|
||||
if (!i->cur)
|
||||
return NULL;
|
||||
/*return &i->cur->data; */
|
||||
|
||||
return mavlnode_dataptr(i->cur);
|
||||
}
|
||||
|
||||
|
||||
|
15
src/cw/mavliter_get_ptr.c
Normal file
15
src/cw/mavliter_get_ptr.c
Normal file
@ -0,0 +1,15 @@
|
||||
#include "mavl.h"
|
||||
|
||||
/**
|
||||
* Get the element, where AVL Iterator currently is positioned.
|
||||
* @param i AVL Iterator
|
||||
* @return element or NULL if not found.
|
||||
*/
|
||||
void * mavliter_get_ptr(mavliter_t *i)
|
||||
{
|
||||
/*void * data;*/
|
||||
if (!i->cur)
|
||||
return NULL;
|
||||
|
||||
return *(void**)(mavlnode_dataptr(i->cur));
|
||||
}
|
@ -8,12 +8,11 @@
|
||||
* @param i AVL Iterator to initialize
|
||||
* @param t correspondending AVL Tree
|
||||
*
|
||||
* @See mavliter_t,
|
||||
* @See mavliter_t,
|
||||
*/
|
||||
void mavliter_init(mavliter_t *i, mavl_t t){
|
||||
void mavliter_init ( mavliter_t *i, mavl_t t )
|
||||
{
|
||||
i->root = t->root;
|
||||
i->stack_ptr=0;
|
||||
i->cmp=t->cmp;
|
||||
i->stack_ptr = 0;
|
||||
i->cmp = t->cmp;
|
||||
}
|
||||
|
||||
|
||||
|
@ -17,7 +17,7 @@
|
||||
*/
|
||||
|
||||
/**
|
||||
*@file
|
||||
*@file
|
||||
*@brief Implementation of mavliter_next
|
||||
*/
|
||||
|
||||
@ -27,44 +27,42 @@
|
||||
* Get the next element within an AVL Tree.
|
||||
* @param i pointer to AVL Iterator
|
||||
* @return the element or NULL if there is no next elemeent.
|
||||
*/
|
||||
*/
|
||||
|
||||
union mavldata * mavliter_next(mavliter_t *i)
|
||||
union mavldata * mavliter_next ( mavliter_t *i )
|
||||
{
|
||||
|
||||
while ( i->stack_ptr) {
|
||||
while ( i->stack_ptr ) {
|
||||
i->stack_ptr--;
|
||||
i->cur=i->stack[i->stack_ptr];
|
||||
|
||||
if (!i->cur)
|
||||
i->cur = i->stack[i->stack_ptr];
|
||||
|
||||
if ( !i->cur )
|
||||
continue;
|
||||
|
||||
if ((i->stack_ptr)&1) {
|
||||
return &i->cur->data;
|
||||
|
||||
if ( ( i->stack_ptr ) & 1 ) {
|
||||
/*return &i->cur->data;*/
|
||||
return mavlnode_dataptr ( i->cur );
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
if (!i->cur){
|
||||
|
||||
if ( !i->cur ) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
while(i->cur->left) {
|
||||
|
||||
while ( i->cur->left ) {
|
||||
/* push right branch */
|
||||
i->stack[i->stack_ptr++]=i->cur->right;
|
||||
i->stack[i->stack_ptr++] = i->cur->right;
|
||||
|
||||
/* push node */
|
||||
i->stack[i->stack_ptr++]=i->cur;
|
||||
|
||||
i->cur=i->cur->left;
|
||||
|
||||
i->stack[i->stack_ptr++] = i->cur;
|
||||
|
||||
i->cur = i->cur->left;
|
||||
}
|
||||
|
||||
i->stack[i->stack_ptr++]=i->cur->right;
|
||||
|
||||
return &i->cur->data;
|
||||
|
||||
i->stack[i->stack_ptr++] = i->cur->right;
|
||||
|
||||
/* return &i->cur->data;*/
|
||||
return mavlnode_dataptr ( i->cur );
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -43,7 +43,8 @@ union mavldata * mavliter_seek(mavliter_t *i,void *d)
|
||||
|
||||
while(i->cur) {
|
||||
|
||||
rc = i->cmp(d,&i->cur->data);
|
||||
/* rc = i->cmp(d,&i->cur->data); */
|
||||
rc = i->cmp(d,mavlnode_dataptr(i->cur));
|
||||
if (rc==0){
|
||||
i->stack[i->stack_ptr++]=i->cur->right;
|
||||
i->stack[i->stack_ptr++]=i->cur;
|
||||
|
@ -1,10 +1,8 @@
|
||||
#include "mavl.h"
|
||||
|
||||
union mavldata * mavliter_seek_set(struct mavliter *i)
|
||||
union mavldata * mavliter_seek_set ( struct mavliter *i )
|
||||
{
|
||||
i->stack_ptr=0;
|
||||
i->cur=i->root;
|
||||
return mavliter_next(i);
|
||||
i->stack_ptr = 0;
|
||||
i->cur = i->root;
|
||||
return mavliter_next ( i );
|
||||
}
|
||||
|
||||
|
||||
|
@ -146,13 +146,13 @@ static int mbag_i_cmp_fun(const void *x1, const void *x2)
|
||||
|
||||
mbag_t mbag_i_create()
|
||||
{
|
||||
return mavl_create(mbag_i_cmp_fun, mbag_i_del_fun);
|
||||
return mavl_create(mbag_i_cmp_fun, mbag_i_del_fun,1312);
|
||||
}
|
||||
|
||||
|
||||
mbag_t mbag_create()
|
||||
{
|
||||
return mavl_create(mbag_cmp_fun, mbag_del_fun);
|
||||
return mavl_create(mbag_cmp_fun, mbag_del_fun,1312);
|
||||
|
||||
}
|
||||
|
||||
@ -186,7 +186,7 @@ struct mbag_item *mbag_item_create(mbag_t s, const char *id)
|
||||
return NULL;
|
||||
i->u1.id = id;
|
||||
i->dynid=0;
|
||||
return mavl_add(s, i);
|
||||
return mavl_add(s, i,NULL);
|
||||
}
|
||||
|
||||
|
||||
@ -207,7 +207,7 @@ struct mbag_item *mbag_i_item_create(mbag_t s, uint32_t id)
|
||||
return NULL;
|
||||
i->u1.iid = id;
|
||||
i->dynid=0;
|
||||
return mavl_add(s, i);
|
||||
return mavl_add(s, i,NULL);
|
||||
}
|
||||
|
||||
|
||||
@ -253,7 +253,7 @@ struct mbag_item *strmbag_item_create(mbag_t s, char *name)
|
||||
int mbag_set(mbag_t t,mbag_item_t *is)
|
||||
{
|
||||
mavl_del(t,is);
|
||||
mavl_add(t,is);
|
||||
mavl_add(t,is,NULL);
|
||||
return 1;
|
||||
|
||||
/*
|
||||
|
@ -18,7 +18,7 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief
|
||||
*/
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
@ -30,29 +30,28 @@
|
||||
#define BLOCK_SIZE 4096
|
||||
|
||||
/**
|
||||
* Calculate MD5 checksum for an opened file.
|
||||
* @param digest destination buffer for calculated
|
||||
* Calculate MD5 checksum for an opened file.
|
||||
* @param digest destination buffer for calculated
|
||||
* checksum (16 bytes, for a predifned constant use #CW_MD5_DIGEST_SIZE)
|
||||
* @param infile file hanle
|
||||
*
|
||||
* @param infile file hanle
|
||||
*
|
||||
* Remember to set the file pointer to the beginning of the file, before
|
||||
* calling this function. Therefore use fseek(file,0,SEEK_SET).
|
||||
*/
|
||||
int cw_fgetmd5sum(uint8_t *digest, FILE *infile)
|
||||
int cw_fgetmd5sum ( uint8_t *digest, FILE *infile )
|
||||
{
|
||||
struct md5_ctx ctx;
|
||||
uint8_t buffer[BLOCK_SIZE];
|
||||
md5_init(&ctx);
|
||||
|
||||
while (!feof(infile)){
|
||||
int bytes = fread(buffer,1,sizeof(buffer),infile);
|
||||
md5_update(&ctx,bytes,buffer);
|
||||
}
|
||||
|
||||
if (ferror(infile))
|
||||
return errno;
|
||||
md5_init ( &ctx );
|
||||
|
||||
md5_digest(&ctx, MD5_DIGEST_SIZE, digest);
|
||||
while ( !feof ( infile ) ) {
|
||||
int bytes = fread ( buffer, 1, sizeof ( buffer ), infile );
|
||||
md5_update ( &ctx, bytes, buffer );
|
||||
}
|
||||
|
||||
if ( ferror ( infile ) )
|
||||
return errno;
|
||||
|
||||
md5_digest ( &ctx, MD5_DIGEST_SIZE, digest );
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -51,7 +51,7 @@ struct mlist {
|
||||
struct mlist_elem *first;
|
||||
struct mlist_elem *last;
|
||||
int count;
|
||||
//size_t elem_size;
|
||||
/* //size_t elem_size;*/
|
||||
};
|
||||
|
||||
typedef struct mlist * mlist_t;
|
||||
|
18
src/cw/mod.c
18
src/cw/mod.c
@ -31,7 +31,7 @@
|
||||
#include "log.h"
|
||||
#include "file.h"
|
||||
#include "cw.h"
|
||||
#include "cw/message_set.h"
|
||||
#include "cw/msget.h"
|
||||
|
||||
static void (*actions_registered_cb) (struct cw_Mod * capwap, struct cw_Mod * bindings,
|
||||
struct cw_actiondef * actions) = NULL;
|
||||
@ -77,10 +77,10 @@ struct cw_Mod mod_null = {
|
||||
|
||||
|
||||
|
||||
static int cmp(const mavldata_t *p1, const mavldata_t *p2)
|
||||
static int cmp(const void *p1, const void *p2)
|
||||
{
|
||||
struct cache_item *c1 = (struct cache_item *) p1->ptr;
|
||||
struct cache_item *c2 = (struct cache_item *) p2->ptr;
|
||||
struct cache_item *c1 = ((struct cache_item **) p1)[0];
|
||||
struct cache_item *c2 = ((struct cache_item **) p2)[0];
|
||||
|
||||
int r;
|
||||
r = strcmp(c1->capwap, c2->capwap);
|
||||
@ -104,7 +104,7 @@ struct cw_MsgSet *cw_mod_get_msg_set(struct conn *conn,
|
||||
struct cw_MsgSet * set;
|
||||
|
||||
if (!msgset_cache) {
|
||||
msgset_cache = mavl_create(cmp, NULL);
|
||||
msgset_cache = mavl_create(cmp, NULL,1312);
|
||||
if (!msgset_cache) {
|
||||
cw_log(LOG_ERR, "Can't initialize msgset cache: %s",
|
||||
strerror(errno));
|
||||
@ -164,9 +164,9 @@ struct cw_MsgSet *cw_mod_get_msg_set(struct conn *conn,
|
||||
|
||||
/* static mavl to store modules */
|
||||
static struct mavl * mods_loaded = NULL;
|
||||
static int mod_cmp_mavl(const mavldata_t *e1, const mavldata_t *e2){
|
||||
const struct cw_Mod * m1 = e1->ptr;
|
||||
const struct cw_Mod * m2 = e2->ptr;
|
||||
static int mod_cmp_mavl(const void *e1, const void *e2){
|
||||
const struct cw_Mod * m1 = *((const struct cw_Mod**)e1);
|
||||
const struct cw_Mod * m2 = *((const struct cw_Mod**)e2);
|
||||
return strcmp(m1->name,m2->name);
|
||||
}
|
||||
|
||||
@ -199,7 +199,7 @@ struct cw_Mod * cw_mod_load(const char * mod_name){
|
||||
|
||||
/* if modlist is not initialized, initialize ... */
|
||||
if (mods_loaded==NULL){
|
||||
mods_loaded=mavl_create(mod_cmp_mavl,NULL);
|
||||
mods_loaded=mavl_create(mod_cmp_mavl,NULL,1312);
|
||||
if (mods_loaded==NULL){
|
||||
cw_log(LOG_ERROR, "Can't init modlist, no memory");
|
||||
return NULL;
|
||||
|
@ -102,6 +102,8 @@ struct cw_Mod * cw_mod_detect(struct conn *conn,
|
||||
struct cw_MsgSet *cw_mod_get_msg_set(struct conn *conn,
|
||||
struct cw_Mod * capwap_mod, struct cw_Mod *bindings_mod);
|
||||
|
||||
void cw_mod_set_mod_path(const char * path);
|
||||
|
||||
#define CW_MOD_MAX_MOD_NAME_LEN 128
|
||||
#define CW_MOD_INTERFACE_FUNCTION_NAME_SUFFIX "_get_interface"
|
||||
|
||||
|
@ -7,7 +7,7 @@
|
||||
#include "conn.h"
|
||||
|
||||
struct cw_MsgSet {
|
||||
mavl_t messages;
|
||||
mavl_t msgdata;
|
||||
mavl_t handlers_by_id;
|
||||
mavl_t handlers_by_key;
|
||||
|
@ -6,11 +6,11 @@
|
||||
#include "dbg.h"
|
||||
#include "log.h"
|
||||
|
||||
#include "message_set.h"
|
||||
#include "msget.h"
|
||||
|
||||
static int cmp_cw_elemhandler_by_id(const mavldata_t * elem1, const mavldata_t *elem2){
|
||||
struct cw_ElemHandler * e1 = elem1->ptr;
|
||||
struct cw_ElemHandler * e2 = elem2->ptr;
|
||||
static int cmp_cw_elemhandler_by_id(const void * elem1, const void *elem2){
|
||||
const struct cw_ElemHandler * e1 = elem1;
|
||||
const struct cw_ElemHandler * e2 = elem2;
|
||||
int r;
|
||||
r = e1->id - e2->id;
|
||||
if (r!=0)
|
||||
@ -24,23 +24,23 @@ static int cmp_cw_elemhandler_by_id(const mavldata_t * elem1, const mavldata_t *
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int cmp_cw_elemhandler_by_key(const mavldata_t *elem1, const mavldata_t *elem2){
|
||||
struct cw_ElemHandler * e1 = elem1->ptr;
|
||||
struct cw_ElemHandler * e2 = elem2->ptr;
|
||||
static int cmp_cw_elemhandler_by_key(const void *elem1, const void *elem2){
|
||||
const struct cw_ElemHandler * e1 = elem1;
|
||||
const struct cw_ElemHandler * e2 = elem2;
|
||||
return strcmp(e1->key,e2->key);
|
||||
}
|
||||
|
||||
static int cmp_msgdata(const mavldata_t * elem1, const mavldata_t *elem2)
|
||||
static int cmp_msgdata(const void * elem1, const void *elem2)
|
||||
{
|
||||
struct cw_MsgData * e1 = elem1->ptr;
|
||||
struct cw_MsgData * e2 = elem2->ptr;
|
||||
const struct cw_MsgData * e1 = elem1;
|
||||
const struct cw_MsgData * e2 = elem2;
|
||||
return e1->type - e2->type;
|
||||
}
|
||||
|
||||
static int cmp_elemdata(const mavldata_t * elem1, const mavldata_t *elem2)
|
||||
static int cmp_elemdata(const void * elem1, const void *elem2)
|
||||
{
|
||||
struct cw_ElemData * e1 = elem1->ptr;
|
||||
struct cw_ElemData * e2 = elem2->ptr;
|
||||
const struct cw_ElemData * e1 = elem1;
|
||||
const struct cw_ElemData * e2 = elem2;
|
||||
int r;
|
||||
r = e1->id - e2->id;
|
||||
if (r!=0)
|
||||
@ -66,26 +66,9 @@ static void msgdata_destroy(struct cw_MsgData *data){
|
||||
free(data);
|
||||
}
|
||||
|
||||
static struct cw_MsgData * msgdata_create(int type){
|
||||
struct cw_MsgData * msg;
|
||||
|
||||
msg = malloc( sizeof(struct cw_MsgData));
|
||||
if (!msg)
|
||||
return NULL;
|
||||
|
||||
msg->elements_tree = mavl_create(cmp_elemdata,mavl_free_bin);
|
||||
if (!msg->elements_tree){
|
||||
msgdata_destroy(msg);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
msg->type=type;
|
||||
return msg;
|
||||
}
|
||||
|
||||
|
||||
static void del_mavl_msdgdata( mavldata_t * d ){
|
||||
msgdata_destroy(d->ptr);
|
||||
static void del_mavl_msdgdata( void * d ){
|
||||
msgdata_destroy(d);
|
||||
}
|
||||
|
||||
|
||||
@ -94,8 +77,8 @@ static void del_mavl_msdgdata( mavldata_t * d ){
|
||||
* @param set Message set to destroy
|
||||
*/
|
||||
void cw_msgset_destroy(struct cw_MsgSet * set){
|
||||
if (set->messages)
|
||||
mavl_destroy(set->messages);
|
||||
if (set->msgdata)
|
||||
mavl_destroy(set->msgdata);
|
||||
if (set->handlers_by_id)
|
||||
mavl_destroy(set->handlers_by_id);
|
||||
if (set->handlers_by_key)
|
||||
@ -118,22 +101,25 @@ struct cw_MsgSet * cw_msgset_create(){
|
||||
memset(set,0,sizeof(struct cw_MsgSet));
|
||||
|
||||
/* create mavl for all_elems by id */
|
||||
set->handlers_by_id = mavl_create(cmp_cw_elemhandler_by_id,mavl_free_bin);
|
||||
set->handlers_by_id = mavl_create(cmp_cw_elemhandler_by_id,NULL,
|
||||
sizeof(struct cw_ElemHandler));
|
||||
if (set->handlers_by_id==NULL){
|
||||
cw_msgset_destroy(set);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* create mavl for all_elems by id */
|
||||
set->handlers_by_key = mavl_create(cmp_cw_elemhandler_by_key,NULL);
|
||||
/* create mavl for all_elems by key */
|
||||
set->handlers_by_key = mavl_create(cmp_cw_elemhandler_by_key,NULL,
|
||||
sizeof(struct cw_ElemHandler));
|
||||
if (set->handlers_by_key==NULL){
|
||||
cw_msgset_destroy(set);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* create mavl for messages */
|
||||
set->messages = mavl_create(cmp_msgdata,del_mavl_msdgdata);
|
||||
if (set->messages==NULL){
|
||||
set->msgdata = mavl_create(cmp_msgdata,del_mavl_msdgdata,
|
||||
sizeof(struct cw_MsgData));
|
||||
if (set->msgdata==NULL){
|
||||
cw_msgset_destroy(set);
|
||||
return NULL;
|
||||
}
|
||||
@ -148,7 +134,7 @@ struct cw_ElemHandler * cw_msgset_get_elemhandler(struct cw_MsgSet * set,
|
||||
search.proto=proto;
|
||||
search.vendor=vendor;
|
||||
search.id=id;
|
||||
return mavl_find_ptr(set->handlers_by_id,&search);
|
||||
return mavl_find(set->handlers_by_id,&search);
|
||||
}
|
||||
|
||||
|
||||
@ -156,11 +142,12 @@ static int update_msgdata(struct cw_MsgSet * set, struct cw_MsgData * msgdata,
|
||||
struct cw_MsgDef * msgdef)
|
||||
{
|
||||
struct cw_ElemDef * elemdef;
|
||||
mavldata_t mavldata, *result;
|
||||
struct cw_ElemData ed, *result;
|
||||
|
||||
/* iterate through all defined elements */
|
||||
for(elemdef = msgdef->elements; elemdef->id; elemdef++){
|
||||
struct cw_ElemHandler * handler;
|
||||
int replaced;
|
||||
|
||||
handler = cw_msgset_get_elemhandler(set,
|
||||
elemdef->proto,
|
||||
@ -175,18 +162,15 @@ static int update_msgdata(struct cw_MsgSet * set, struct cw_MsgData * msgdata,
|
||||
continue;
|
||||
}
|
||||
|
||||
mavldata.ptr = malloc(sizeof(struct cw_ElemData));
|
||||
if (!mavldata.ptr){
|
||||
cw_log(LOG_ERR,"Can't create ElemData element: %s",strerror(errno));
|
||||
}
|
||||
((struct cw_ElemData *)(mavldata.ptr))->id=elemdef->id;
|
||||
((struct cw_ElemData *)(mavldata.ptr))->proto=elemdef->proto;
|
||||
((struct cw_ElemData *)(mavldata.ptr))->vendor=elemdef->vendor;
|
||||
((struct cw_ElemData *)(mavldata.ptr))->mand=elemdef->mand;
|
||||
ed.id=elemdef->id;
|
||||
ed.proto=elemdef->proto;
|
||||
ed.vendor=elemdef->vendor;
|
||||
ed.mand=elemdef->mand;
|
||||
|
||||
result = mavl_replace(msgdata->elements_tree, &mavldata);
|
||||
|
||||
if (result != &mavldata){
|
||||
result = mavl_replace(msgdata->elements_tree, &ed, &replaced);
|
||||
|
||||
if (!replaced){
|
||||
cw_dbg(DBG_MOD, " adding message element %d %d %d - %s",
|
||||
elemdef->proto,
|
||||
elemdef->vendor,
|
||||
@ -194,7 +178,7 @@ static int update_msgdata(struct cw_MsgSet * set, struct cw_MsgData * msgdata,
|
||||
handler->name);
|
||||
}
|
||||
else{
|
||||
cw_dbg(DBG_MOD, " replaceing message element %d %d %d - %s",
|
||||
cw_dbg(DBG_MOD, " replacing message element %d %d %d - %s",
|
||||
elemdef->proto,
|
||||
elemdef->vendor,
|
||||
elemdef->id,
|
||||
@ -217,46 +201,34 @@ int cw_msgset_add(struct cw_MsgSet * set,
|
||||
|
||||
/* Create mavl for all handlers */
|
||||
for(handler = handlers; handler->id; handler++){
|
||||
mavldata_t copy;
|
||||
copy.ptr = malloc(sizeof(struct cw_ElemHandler));
|
||||
if (!copy.ptr){
|
||||
cw_log(LOG_ERR,"Can't alloc mem:", strerror(errno));
|
||||
continue;
|
||||
}
|
||||
|
||||
cw_dbg(DBG_MOD,"Adding handler for element %d - %s - with key: %s",
|
||||
handler->id,
|
||||
handler->name,
|
||||
handler->key);
|
||||
|
||||
|
||||
memcpy(copy.ptr,handler,sizeof(struct cw_ElemHandler));
|
||||
|
||||
mavl_replace(set->handlers_by_id,©);
|
||||
mavl_replace(set->handlers_by_key,©);
|
||||
mavl_replace(set->handlers_by_id,handler,NULL);
|
||||
mavl_replace(set->handlers_by_key,handler,NULL);
|
||||
}
|
||||
|
||||
|
||||
for (msgdef=messages; msgdef->type !=0; msgdef++){
|
||||
struct cw_MsgData search, *msg;
|
||||
struct cw_MsgData search;
|
||||
struct cw_MsgData *msg;
|
||||
int exists;
|
||||
|
||||
/* Look if message already exists */
|
||||
search.type=msgdef->type;
|
||||
msg = mavl_find_ptr(set->messages,&search);
|
||||
msg = mavl_add(set->msgdata,&search,&exists);
|
||||
|
||||
if (!msg){
|
||||
msg = msgdata_create(msgdef->type);
|
||||
|
||||
if (!msg){
|
||||
if (msg == NULL){
|
||||
cw_log(LOG_ERR,"Can't create messae");
|
||||
return 0;
|
||||
}
|
||||
|
||||
mavl_add_ptr(set->messages,msg);
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (!exists){
|
||||
msg->elements_tree = mavl_create(cmp_elemdata,NULL,
|
||||
sizeof(struct cw_ElemData));
|
||||
}
|
||||
|
||||
/* Overwrite the found message */
|
||||
if (msgdef->name)
|
||||
msg->name=msgdef->name;
|
||||
@ -283,5 +255,5 @@ int cw_msgset_add(struct cw_MsgSet * set,
|
||||
struct cw_MsgData * cw_msgset_get_msgdata(struct cw_MsgSet *set,int type){
|
||||
struct cw_MsgData search;
|
||||
search.type=type;
|
||||
return mavl_find_ptr(set->messages,&search);
|
||||
return mavl_find_ptr(set->msgdata,&search);
|
||||
}
|
@ -18,7 +18,7 @@ static void del(void* d)
|
||||
|
||||
struct avltree * stravltree_create()
|
||||
{
|
||||
return mavl_create(cmp,del);
|
||||
return mavl_create(cmp,del,100);
|
||||
}
|
||||
|
||||
const char * stravltree_add(struct avltree * t, const char * str)
|
||||
@ -26,5 +26,5 @@ const char * stravltree_add(struct avltree * t, const char * str)
|
||||
char * s = strdup(str);
|
||||
if (!s)
|
||||
return 0;
|
||||
return mavl_add(t,s);
|
||||
return mavl_add(t,s,NULL);
|
||||
}
|
||||
|
@ -14,13 +14,13 @@ static void del(void* d)
|
||||
|
||||
cw_strheap_t cw_strheap_create()
|
||||
{
|
||||
return mavl_create(cmp,del);
|
||||
return mavl_create(cmp,del,1312);
|
||||
}
|
||||
|
||||
const char * cw_strheap_add(cw_strheap_t t, struct cw_strlist_elem *s)
|
||||
{
|
||||
mavl_del(t,s);
|
||||
return mavl_add(t,s);
|
||||
return mavl_add(t,s,NULL);
|
||||
}
|
||||
|
||||
int cw_strheap_register_strings(cw_strheap_t h, struct cw_strlist_elem *s)
|
||||
|
@ -18,10 +18,10 @@
|
||||
|
||||
|
||||
#include "cw/cw.h"
|
||||
#include "cw/cw_types.h"
|
||||
#include "cw/kvt.h"
|
||||
#include "cw/log.h"
|
||||
|
||||
#include "cw/message_set.h"
|
||||
#include "cw/msget.h"
|
||||
|
||||
#include "mod_capwap.h"
|
||||
|
||||
|
@ -24,8 +24,8 @@
|
||||
#include "cw/dbg.h"
|
||||
#include "cw/cw.h"
|
||||
|
||||
#include "cw/kvstore.h"
|
||||
#include "cw/cw_types.h"
|
||||
#include "cw/kvt.h"
|
||||
|
||||
#include "cw/keys.h"
|
||||
|
||||
|
||||
@ -99,7 +99,7 @@ static void readsubelems_wtp_board_data(mavl_t cfg, uint8_t * msgelem,
|
||||
if (key){
|
||||
char add_key[256];
|
||||
sprintf(add_key,"wtp_board_data/%s",key);
|
||||
cw_kvstore_add(cfg,add_key,CW_TYPE_BSTR16,msgelem+i,sublen);
|
||||
cw_kvt_add(cfg,add_key,CW_TYPE_BSTR16,msgelem+i,sublen);
|
||||
|
||||
}
|
||||
|
||||
@ -134,7 +134,7 @@ int capwap_in_wtp_board_data(struct cw_ElemHandler *eh, struct cw_ElemHandlerPar
|
||||
|
||||
sprintf(vendor_key,"%s/%s",eh->key,CW_KEY_VENDOR);
|
||||
|
||||
cw_kvstore_add(cfg,vendor_key,CW_TYPE_DWORD,data,len);
|
||||
cw_kvt_add(cfg,vendor_key,CW_TYPE_DWORD,data,len);
|
||||
|
||||
/*
|
||||
mbag_t itemstore = conn->incomming;
|
||||
|
@ -4,7 +4,7 @@
|
||||
struct cw_Mod *mod_capwap_ac();
|
||||
struct cw_Mod *mod_capwap_wtp();
|
||||
|
||||
#include "cw/message_set.h"
|
||||
#include "cw/msget.h"
|
||||
#include "cw/conn.h"
|
||||
|
||||
|
||||
|
@ -41,8 +41,8 @@
|
||||
|
||||
#include "include/cipwap_items.h"
|
||||
|
||||
#include "cw/message_set.h"
|
||||
#include "cw/cw_types.h"
|
||||
#include "cw/msget.h"
|
||||
#include "cw/kvt.h"
|
||||
|
||||
|
||||
static struct cw_ElemHandler handlers[] = {
|
||||
|
Loading…
Reference in New Issue
Block a user