From 6b7b76db84d09834cf052e30fe870ec8e7857487 Mon Sep 17 00:00:00 2001
From: "7u83@mail.ru" <7u83@mail.ru@noemail.net>
Date: Fri, 30 Mar 2018 09:12:50 +0000
Subject: [PATCH] cw_select_ac function is done
FossilOrigin-Name: 664aa92562692cc3d73e2540649b3896fac26401247a2a9d0059aa874c2ef914
---
libcw.project | 1 +
src/ac/ac.conf | 2 +-
src/ac/config.ktv | 2 +-
src/cw/Makefile | 1 +
src/cw/cw_ktv_mavlcmp.c | 8 ++-
src/cw/cw_ktv_readline.c | 136 ++++++++++++++++++++++++++++----------
src/cw/cw_ktv_std_types.c | 1 +
src/cw/cw_type_sysptr.c | 75 +++++++++++++++++++++
src/cw/ktv.h | 2 +
src/cw/mavl.h | 2 +-
src/wtp/Makefile | 2 +-
src/wtp/config.ktv | 14 +++-
src/wtp/discovery.c | 66 +++++++++++++-----
src/wtp/join.c | 47 +++++++------
src/wtp/wtp_conf.h | 2 +-
wtp.project | 2 +-
16 files changed, 285 insertions(+), 78 deletions(-)
create mode 100644 src/cw/cw_type_sysptr.c
diff --git a/libcw.project b/libcw.project
index 692ce758..5ed821a5 100644
--- a/libcw.project
+++ b/libcw.project
@@ -268,6 +268,7 @@
+
diff --git a/src/ac/ac.conf b/src/ac/ac.conf
index b0472882..07c4eddd 100644
--- a/src/ac/ac.conf
+++ b/src/ac/ac.conf
@@ -12,7 +12,7 @@
# If ommited ac_name is build from macaddress.
#
#Default: ac_name = Cisco80
-ac_name = "CiscAc73"
+ac_name = "TubesAC"
# ac_id
# A unique ID for this AC
diff --git a/src/ac/config.ktv b/src/ac/config.ktv
index 63952ee8..7d37c7e6 100644
--- a/src/ac/config.ktv
+++ b/src/ac/config.ktv
@@ -11,7 +11,7 @@ ac-descriptor/software/version:Bstr16:.x090103
ac-descriptor/software/vendor:Dword:.1234
ac-descriptor/hardware/vendor:Dword:1234567
ac-descriptor/hardware/version:Bstr16:"1.7.3"
-ac-name:Bstr16:"actubes name"
+ac-name:Bstr16:"TubesAC"
capwap-control-ip-address/address.0:IPAddress:192.168.0.1
capwap-control-ip-address/address.1:IPAddress:192.168.0.7
diff --git a/src/cw/Makefile b/src/cw/Makefile
index 4b8256d9..bc5217b3 100644
--- a/src/cw/Makefile
+++ b/src/cw/Makefile
@@ -105,6 +105,7 @@ CWSRC=\
cw_type_dword.c\
cw_type_ipaddress.c\
cw_type_word.c\
+ cw_type_sysptr.c\
cw_util.c\
cw_write_descriptor_subelem.c\
cw_read_from.c \
diff --git a/src/cw/cw_ktv_mavlcmp.c b/src/cw/cw_ktv_mavlcmp.c
index 460b2b3a..4b249306 100644
--- a/src/cw/cw_ktv_mavlcmp.c
+++ b/src/cw/cw_ktv_mavlcmp.c
@@ -1,5 +1,11 @@
#include "ktv.h"
-
+/**
+ * @brief Default function to compare two values of type #cw_KTV_t.
+ *
+ * @param v1
+ * @param v2
+ * @return
+ */
int cw_ktv_mavlcmp(const void *v1, const void *v2)
{
char *d1,*d2;
diff --git a/src/cw/cw_ktv_readline.c b/src/cw/cw_ktv_readline.c
index 70533159..8d9ad9e6 100644
--- a/src/cw/cw_ktv_readline.c
+++ b/src/cw/cw_ktv_readline.c
@@ -7,12 +7,14 @@ struct parser {
int pos;
int prevpos;
char error[256];
+ int quote;
+ FILE *f;
};
-static int get_char(FILE * f, struct parser *p)
+static int get_char(struct parser *p)
{
int c;
- c = fgetc (f);
+ c = fgetc (p->f);
p->pos++;
if (c=='\n'){
p->prevpos=p->pos;
@@ -22,8 +24,9 @@ static int get_char(FILE * f, struct parser *p)
return c;
}
-static void unget_char(int c, FILE *f, struct parser *p){
- ungetc(c,f);
+
+static void unget_char(struct parser *p,int c){
+ ungetc(c,p->f);
if (c=='\n'){
p->line--;
p->pos=p->prevpos;
@@ -32,11 +35,71 @@ static void unget_char(int c, FILE *f, struct parser *p){
p->pos--;
}
-static int skip_chars (FILE *f, const char * chars, struct parser * p )
+
+
+static int get_char_q(struct parser *p)
+{
+ int c;
+
+ while(1) {
+ c = get_char(p);
+ if (c==EOF || c=='\n')
+ return c;
+
+ if(c=='"' && !p->quote){
+ p->quote=1;
+ continue;
+ }
+ if(c=='"' && p->quote){
+ p->quote=0;
+ continue;
+ }
+ break;
+ }
+
+
+ if (!p->quote)
+ return c;
+
+ if (c!='\\')
+ return c;
+
+ c = get_char(p);
+ switch(c){
+ case EOF:
+ return c;
+ case 'n':
+ return '\n';
+
+ case '\\':
+ return '\\';
+ case '"':
+ return '"';
+ default:
+ unget_char(p,c);
+ return '\\';
+ }
+
+ /* We will never reach here */
+ /* return c;*/
+}
+
+
+
+
+
+
+
+
+
+
+
+
+static int skip_chars (struct parser *p, const char * chars)
{
int c;
- while ( (c = get_char (f, p)) != EOF) {
+ while ( (c = get_char (p)) != EOF) {
if (strchr (chars, c))
continue;
return c;
@@ -44,11 +107,11 @@ static int skip_chars (FILE *f, const char * chars, struct parser * p )
return c;
}
-static int skip_to_chars (FILE *f, const char *chars, struct parser * p)
+static int skip_to_chars (struct parser *p, const char *chars)
{
int c;
- while ( (c = get_char (f, p)) != EOF) {
+ while ( (c = get_char (p)) != EOF) {
if (strchr (chars, c))
return c;
}
@@ -57,30 +120,32 @@ static int skip_to_chars (FILE *f, const char *chars, struct parser * p)
-static int read_key (FILE *f, char *key, int max_len, struct parser * p)
+static int read_key (struct parser *p, char *key, int max_len)
{
int c,n;
do {
- c = skip_chars (f, " \t\n\a\v", p);
+ c = skip_chars (p, " \t\n\a\v");
if (c == '#') {
- c = skip_to_chars (f, "\n\a",p);
+ c = skip_to_chars (p, "\n\a");
} else {
break;
}
} while (c != EOF);
-
+
+ unget_char(p,c);
+ c=get_char_q(p);
n=0;
while(c!=EOF && nquote && !isalnum(c) && !strchr("._/-()@#|{}[]\\",c)/*strchr(": \t\n\a",c)*/){
+ unget_char(p,c);
break;
}
key[n]=c;
- c=get_char(f,p);
+ c=get_char_q(p);
n++;
}
@@ -92,10 +157,10 @@ static int read_key (FILE *f, char *key, int max_len, struct parser * p)
static int skip_to_colon(FILE *f,struct parser * p)
{
int c;
- c = skip_chars (f, " \t", p);
+ c = skip_chars (p, " \t");
if (c!=':'){
if (c=='\n'){
- unget_char(c,f,p);
+ unget_char(p,c);
sprintf(p->error,"Error at line %d, pos %d: Unexpected EOL, collon expected.", p->line, p->pos);
return 0;
}
@@ -106,24 +171,24 @@ static int skip_to_colon(FILE *f,struct parser * p)
}
-static int read_type(FILE *f, char *type, int max_len, struct parser *p)
+static int read_type(struct parser *p, char *type, int max_len)
{
int c,n;
- if (!skip_to_colon(f,p))
+ if (!skip_to_colon(p->f,p))
return -1;
- c = skip_chars (f, " \t", p);
+ c = skip_chars (p, " \t");
if (c==':'){
- unget_char(c,f,p);
+ unget_char(p,c);
sprintf(type,"%s","");
return 0;
}
if (!isalpha(c)){
if (c=='\n'){
- unget_char(c,f,p);
+ unget_char(p,c);
sprintf(p->error,"Error at line %d, pos %d: Unexpected EOL.", p->line, p->pos);
return -1;
}
@@ -135,26 +200,27 @@ static int read_type(FILE *f, char *type, int max_len, struct parser *p)
n=0;
while(c!=EOF && nf,p))
return -1;
- c = skip_chars (f, " \t", p);
+ c = skip_chars (p, " \t");
if (c=='"'){
quote=1;
- c=get_char(f,p);
+ c=get_char(p);
}
else{
quote=0;
@@ -169,7 +235,7 @@ static int read_val(FILE *f, char *val, int max_len, struct parser *p){
}
if (quote){
if (c=='\\'){
- c = get_char(f,p);
+ c = get_char(p);
switch(c){
case 'n':
c='\n';
@@ -179,13 +245,13 @@ static int read_val(FILE *f, char *val, int max_len, struct parser *p){
case '"':
break;
default:
- unget_char(c,f,p);
+ unget_char(p,c);
c='\\';
}
}
}
val[n++]=c;
- c=get_char(f,p);
+ c=get_char(p);
}
@@ -214,14 +280,16 @@ int cw_ktv_read_line (FILE *f, char * key, char * type, char *val)
p.line=1;
p.pos=0;
p.prevpos=0;
+ p.quote=0;
+ p.f=f;
- n = read_key (f,key,CW_KTV_MAX_KEY_LEN,&p);
- n = read_type (f,type,CW_KTV_MAX_KEY_LEN,&p);
+ n = read_key (&p,key,CW_KTV_MAX_KEY_LEN);
+ n = read_type (&p,type,CW_KTV_MAX_KEY_LEN);
if (n==-1){
return -1;
}
- n = read_val (f,val,CW_KTV_MAX_KEY_LEN,&p);
+ n = read_val (&p,val,CW_KTV_MAX_KEY_LEN);
if (n==-1){
return -1;
}
diff --git a/src/cw/cw_ktv_std_types.c b/src/cw/cw_ktv_std_types.c
index c2e75f26..4a2909b5 100644
--- a/src/cw/cw_ktv_std_types.c
+++ b/src/cw/cw_ktv_std_types.c
@@ -6,5 +6,6 @@ const cw_Type_t * cw_ktv_std_types[] = {
CW_TYPE_DWORD,
CW_TYPE_BSTR16,
CW_TYPE_IPADDRESS,
+ CW_TYPE_SYSPTR,
NULL
};
diff --git a/src/cw/cw_type_sysptr.c b/src/cw/cw_type_sysptr.c
new file mode 100644
index 00000000..a47d122c
--- /dev/null
+++ b/src/cw/cw_type_sysptr.c
@@ -0,0 +1,75 @@
+/*
+ This file is part of libcapwap.
+
+ libcapwap is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ libcapwap is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Foobar. If not, see .
+
+*/
+
+#include
+
+#include "format.h"
+#include "cw.h"
+#include "ktv.h"
+
+
+static void del ( struct cw_KTV * data )
+{
+ return;
+}
+
+static struct cw_KTV *get ( struct cw_KTV * data, const uint8_t * src, int len )
+{
+ data->type = &cw_type_sysptr;
+ data->val.ptr = ((void**)(src))[0];
+ return data;
+}
+
+static int put ( const struct cw_KTV *data, uint8_t * dst )
+{
+ ((void**)dst)[0] = data->val.ptr ;
+ return sizeof(void*);
+}
+
+static int to_str ( const struct cw_KTV *data, char *dst, int max_len )
+{
+ return sprintf(dst,"%p",data->val.ptr);
+}
+
+static struct cw_KTV *from_str ( struct cw_KTV * data, const char *src )
+{
+ uint8_t * s;
+ s = bstr16_create_from_str(src);
+
+ if ( !s )
+ return NULL;
+
+ data->type = &cw_type_bstr16;
+ data->val.ptr = s;
+ return data;
+
+}
+
+static int len ( struct cw_KTV * data ){
+ return sizeof(void*);
+}
+
+const struct cw_Type cw_type_sysptr = {
+ "Sysptr", /* name */
+ del, /* del */
+ put, /* put */
+ get, /* get */
+ to_str, /* to_str */
+ from_str, /* from_str */
+ len /* len */
+};
diff --git a/src/cw/ktv.h b/src/cw/ktv.h
index c59a7c4c..2567c2e9 100644
--- a/src/cw/ktv.h
+++ b/src/cw/ktv.h
@@ -96,12 +96,14 @@ extern const struct cw_Type cw_type_word;
extern const struct cw_Type cw_type_dword;
extern const struct cw_Type cw_type_bstr16;
extern const struct cw_Type cw_type_ipaddress;
+extern const struct cw_Type cw_type_sysptr;
#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)
#define CW_TYPE_IPADDRESS (&cw_type_ipaddress)
+#define CW_TYPE_SYSPTR (&cw_type_sysptr)
/*
void cw_kvstore_mavl_delete(const void *data);
diff --git a/src/cw/mavl.h b/src/cw/mavl.h
index 7f9c14bc..67f2d37d 100644
--- a/src/cw/mavl.h
+++ b/src/cw/mavl.h
@@ -16,7 +16,7 @@
*/
/**
- * @file
+ * @file mavl.h
* @brief MAVL, Mini AVL Tree,
* Yet another AVL Tree implementation
*/
diff --git a/src/wtp/Makefile b/src/wtp/Makefile
index 3d4f6a8b..13c87fc7 100644
--- a/src/wtp/Makefile
+++ b/src/wtp/Makefile
@@ -32,7 +32,7 @@ endif
#SRC=$(wildcard *.c)
-SRC=wtp_main.c discovery.c
+SRC=wtp_main.c discovery.c
OBJS=$(patsubst %.c,%.o,$(SRC))
OBJS:=$(patsubst %.o,$(OBJDIR)/%.o,$(OBJS))
diff --git a/src/wtp/config.ktv b/src/wtp/config.ktv
index 972be761..73c0d21b 100644
--- a/src/wtp/config.ktv
+++ b/src/wtp/config.ktv
@@ -1,5 +1,7 @@
+capwap/vars/MaxDiscoveries:Word:10
+capwap/vars/MaxRetransmit:Word:4
+
- # hallo
windows:Word:124
basic/mod:Bstr16:cisco
@@ -24,9 +26,17 @@ wtp-descriptor/software/vendor:Dword:906090
wtp-descriptor/software/version:Bstr16:.x171312
wtp-descriptor/bootloader/vendor:Dword:906090
wtp-descriptor/bootloader/version:Bstr16:.x171312
-wtp-descriptor/max-radios:Byte:2
+wtp-descriptor/max-radios:Byte:3
radio/0/wtp-radio-information:Dword:01
radio/1/wtp-radio-information:Dword:02
wtp-name:Bstr16:WFAT01
+
+
+wtp-fallback:Byte:1
+"ac-name-with-priority/TubesAC":Byte:3
+ac-name-with-priority/"ac2":Byte:01
+
+
+
diff --git a/src/wtp/discovery.c b/src/wtp/discovery.c
index 55d0bba1..1e503829 100644
--- a/src/wtp/discovery.c
+++ b/src/wtp/discovery.c
@@ -16,44 +16,80 @@
#include "wtp.h"
-int cw_select_ac(mlist_t aclist){
+int cw_select_ac(mavl_t local_cfg,mlist_t aclist){
mlistelem_t * e;
- mavl_t best_ac;
-
- best_ac=NULL;
+ int en;
+ mavl_t iplist;
+ iplist=cw_ktv_create();
+ if (iplist == NULL)
+ return 0;
+
+ en=0;
+
+ /* for each discovery response */
mlist_foreach(e,aclist){
char str[1024];
char key[CW_KTV_MAX_KEY_LEN];
- mavl_t cfg;
+ mavl_t remote_cfg;
int i;
-
cw_KTV_t * val;
- cfg = mlistelem_get_ptr(e);
- val = cw_ktv_get(cfg,"ac-name", CW_TYPE_BSTR16);
+ int prio;
+
+ remote_cfg = mlistelem_get_ptr(e);
+
+ /* get ac name */
+ val = cw_ktv_get(remote_cfg,"ac-name", CW_TYPE_BSTR16);
if (val==NULL)
continue;
- if (best_ac==NULL){
- best_ac=cfg;
- }
-
val->type->to_str(val,str,1024);
+ sprintf(key,"ac-name-with-priority/%s",str);
+
+ printf("Get prio: %s\n",key);
+
+ prio = cw_ktv_get_byte(local_cfg,key,255);
i=0;
do {
+ cw_KTV_t * ipval;
sprintf(key,"%s.%d","capwap-control-ip-address/wtps",i);
- val = cw_ktv_get(cfg,key,CW_TYPE_WORD);
+ val = cw_ktv_get(remote_cfg,key,CW_TYPE_WORD);
if (val == NULL)
break;
+ sprintf(key,"%s.%d","capwap-control-ip-address/address",i);
+ printf("ipvalkey: %s\n",key);
+ ipval = cw_ktv_get(remote_cfg,key,CW_TYPE_IPADDRESS);
+
+ sprintf(key,"%04d%05d%04d",prio,val->val.word,en);
+ en++;
+ printf("This is the key: %s\n",key);
+
+ cw_ktv_add(iplist,key,CW_TYPE_SYSPTR,(uint8_t*)(&ipval),sizeof(ipval));
+ i++;
}while(1);
printf("Here we have an AC: %s\n",str);
}
-
+ cw_dbg_ktv_dump(iplist,DBG_INFO,"=== IP list ===", "IP", "=== END IP List ===");
+ {
+ mavliter_t i;
+ mavliter_init(&i,iplist);
+
+ mavliter_foreach(&i){
+ char ipstr[100];
+ char *rk;
+ cw_KTV_t * val;
+ val = mavliter_get(&i);
+ rk = val->key;
+ val = val->val.ptr;
+ val->type->to_str(val,ipstr,100);
+ printf("PTRVAL(%s): %s - %s\n",rk,val->key,ipstr);
+ }
+ }
return 0;
}
@@ -100,7 +136,7 @@ static int run_discovery(struct conn *conn)
conn->remote_cfg=cw_ktv_create();
}
- cw_select_ac(discovery_results);
+ cw_select_ac(conn->local_cfg,discovery_results);
/*
diff --git a/src/wtp/join.c b/src/wtp/join.c
index e131e137..d77c850f 100644
--- a/src/wtp/join.c
+++ b/src/wtp/join.c
@@ -14,8 +14,8 @@
#include "cw/sock.h"
#include "cw/dtls.h"
#include "cw/aciplist.h"
-#include "cw/capwap_items.h"
-#include "cw/mbag.h"
+
+
/*
#define acinfo_log acinfo_log_
@@ -96,6 +96,7 @@ acinfo.result_code=99;
int run_join_d(struct sockaddr *sa)
{
+ char addrstr[SOCK_ADDR_BUFSIZE];
struct conn *conn = get_conn();
conn->capwap_state = CAPWAP_STATE_JOIN;
@@ -125,7 +126,7 @@ int run_join_d(struct sockaddr *sa)
}
*/
- cw_dbg(DBG_DTLS, "Establishing DTLS session with %s", sock_addr2str(sa));
+ cw_dbg(DBG_DTLS, "Establishing DTLS session with %s", sock_addr2str(sa, addrstr));
int dtls_conf_ok=0;
@@ -147,7 +148,7 @@ int run_join_d(struct sockaddr *sa)
if (!dtls_conf_ok){
cw_log(LOG_ERR,"Can't establish DTLS connection with %s, neither psk nor cert set in config",
- sock_addr2str(sa));
+ sock_addr2str(sa,addrstr));
close(sockfd);
return 0;
}
@@ -157,14 +158,14 @@ int run_join_d(struct sockaddr *sa)
if (rc != 1) {
dtls_shutdown(conn);
cw_log(LOG_ERR, "Can't establish DTLS connection with %s",
- sock_addr2str(sa));
+ sock_addr2str(sa,addrstr));
close(sockfd);
return 0;
}
cw_dbg(DBG_DTLS, "DTLS Connection successful established with %s",
- sock_addr2str(sa));
+ sock_addr2str(sa,addrstr));
@@ -172,36 +173,41 @@ int run_join_d(struct sockaddr *sa)
}
+
+
+
+
int run_join(struct conn *conn)
{
-
+ char addrstr[SOCK_ADDR_BUFSIZE];
+/*
// cw_init_request(conn, CW_MSG_JOIN_REQUEST);
// if ( cw_put_msg(conn, conn->req_buffer) == -1 )
// return 0;
//
// conn_send_msg(conn, conn->req_buffer);
+*/
+/* mbag_del_all(conn->incomming);*/
- mbag_del_all(conn->incomming);
-
- //mbag_del (conn->incomming,CW_ITEM_RESULT_CODE);
+/* //mbag_del (conn->incomming,CW_ITEM_RESULT_CODE);*/
int rc = cw_send_request(conn, CAPWAP_MSG_JOIN_REQUEST);
if (!cw_result_is_ok(rc)) {
if (rc > 0) {
cw_log(LOG_ERR, "Can't Join AC at %s, AC said: %d - %s.",
- sock_addr2str(&conn->addr), rc, cw_strerror(rc));
+ sock_addr2str(&conn->addr,addrstr), rc, cw_strerror(rc));
} else {
cw_log(LOG_ERR, "Can't Join AC at %s: %d - %s.",
- sock_addr2str(&conn->addr), errno, cw_strerror(rc));
+ sock_addr2str(&conn->addr,addrstr), errno, cw_strerror(rc));
}
return 0;
}
cw_dbg(DBG_ELEM_IN, "Joined AC at %s, Join Result: %d - %s",
- sock_addr2str(&conn->addr), rc, cw_strresult(rc));
+ sock_addr2str(&conn->addr,addrstr), rc, cw_strresult(rc));
return 1;
}
@@ -211,12 +217,13 @@ int run_join(struct conn *conn)
int join()
{
struct conn *conn = get_conn();
-
+ mavliter_t ii;
+ char addrstr[SOCK_ADDR_BUFSIZE];
printf("Join\n");
- mbag_del_all(conn->incomming);
+ /*mbag_del_all(conn->incomming);*/
- cw_aciplist_t iplist =
+/* cw_aciplist_t iplist =
mbag_get_mavl(conn->local, CW_ITEM_CAPWAP_CONTROL_IP_ADDRESS_LIST);
if (!iplist) {
cw_log(LOG_ERR, "No IPs to join controller.");
@@ -227,16 +234,16 @@ int join()
cw_log(LOG_ERR, "No IPs to join controller. IP list is empty.");
return 0;
}
+*/
-
- DEFINE_AVLITER(ii, iplist);
- avliter_foreach(&ii) {
+ /*DEFINE_AVLITER(ii, iplist);*/
+ mavliter_foreach(&ii) {
cw_acip_t *ip = avliter_get(&ii);
cw_dbg(DBG_INFO, "Going to join CAWAP controller on %s",
- sock_addr2str_p(&ip->ip));
+ sock_addr2str_p(&ip->ip,addrstr));
int rc = run_join_d((struct sockaddr *) &ip->ip);
diff --git a/src/wtp/wtp_conf.h b/src/wtp/wtp_conf.h
index a46d90fb..f6c6f63e 100644
--- a/src/wtp/wtp_conf.h
+++ b/src/wtp/wtp_conf.h
@@ -129,7 +129,7 @@ extern long conf_echo_interval;
extern long conf_max_retransmit;
extern long conf_retransmit_interval;
-//extern long conf_dbg_level;
+
extern int conf_mtu_discovery;
extern int conf_mtu;
diff --git a/wtp.project b/wtp.project
index d8a1f639..c584a37a 100644
--- a/wtp.project
+++ b/wtp.project
@@ -50,7 +50,7 @@
-
+