VM
FossilOrigin-Name: 4bbb8402b5110003c6d19908b6fa9b4a7a6b5903ba1fa691eb93a49aa0f089f4
This commit is contained in:
@ -25,253 +25,55 @@
|
||||
|
||||
|
||||
#include "capwap/capwap.h"
|
||||
#include "capwap/capwap_items.h"
|
||||
#include "capwap/cw_log.h"
|
||||
#include "capwap/wtpinfo.h"
|
||||
#include "capwap/acinfo.h"
|
||||
#include "capwap/sock.h"
|
||||
#include "capwap/cw_util.h"
|
||||
|
||||
|
||||
#include "wtp.h"
|
||||
#include "wtp_conf.h"
|
||||
#include "wtp_interface.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
struct discovery_info{
|
||||
ACIPLIST * aciplist;
|
||||
struct conn * conn;
|
||||
int response_count;
|
||||
};
|
||||
|
||||
static int acprint(void *p,void*d) //,int ctr)
|
||||
|
||||
static int run_discovery(struct conn *conn)
|
||||
{
|
||||
conn->capwap_state=CW_STATE_DISCOVERY;
|
||||
cw_itemstore_set_byte(conn->local,CW_ITEM_DISCOVERY_TYPE,CW_DISCOVERY_TYPE_UNKNOWN);
|
||||
|
||||
|
||||
ACIP * ip = (ACIP*)d;
|
||||
char str[100];
|
||||
sock_addrtostr((struct sockaddr*)&ip->ip,str,100);
|
||||
printf("ACIP: %s\n",str);
|
||||
printf("CTR: %i\n",ip->wtp_count);
|
||||
return 1;
|
||||
cw_init_request(conn,CW_MSG_DISCOVERY_REQUEST);
|
||||
cw_put_msg(conn,conn->req_buffer);
|
||||
conn_send_msg(conn,conn->req_buffer);
|
||||
|
||||
|
||||
time_t timer = cw_timer_start(10);
|
||||
|
||||
while (!cw_timer_timeout(timer)
|
||||
&& conn->capwap_state == CW_STATE_DISCOVERY) {
|
||||
cw_read_messages(conn);
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
struct ac_info wtp_acinfo;
|
||||
|
||||
|
||||
static int msg_cb(void *priv,struct cwrmsg * cwrmsg)
|
||||
{
|
||||
if (cwrmsg->type != CW_MSG_DISCOVERY_RESPONSE){
|
||||
cw_dbg(DBG_ERR,"Expected discovery response, but received %i",cwrmsg->type);
|
||||
return 1;
|
||||
}
|
||||
|
||||
struct discovery_info * di = (struct discovery_info*)priv;
|
||||
|
||||
/*
|
||||
if (di->conn->seqnum != cwrmsg->seqnum){
|
||||
cw_log_debug0("Ignoring discovery respone, seqnum=%i, expected seqnum %i",cwrmsg->seqnum,di->conn->seqnum);
|
||||
return 1;
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
struct ac_info * acinfo = &wtp_acinfo;
|
||||
memset(acinfo,0,sizeof(struct ac_info));
|
||||
|
||||
acinfo->aciplist = di->aciplist;
|
||||
|
||||
|
||||
|
||||
|
||||
cwread_discovery_response(acinfo,cwrmsg->msgelems,cwrmsg->msgelems_len);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
char ai [4096];
|
||||
acinfo_print(ai,acinfo);
|
||||
printf("AC INFO\n%s",ai);
|
||||
|
||||
|
||||
//printf("CWRHW: %s\n",sock_hwaddr2str(bstr_data(cwrmsg->rmac),bstr_len(cwrmsg->rmac)));
|
||||
///exit(0);
|
||||
|
||||
|
||||
/*
|
||||
printf("AC NAME %s\n",acinfo.ac_name);
|
||||
printf("AC Software version %s\n",acinfo.software_version);
|
||||
printf("AC Hardwareversion %s\n",acinfo.hardware_version);
|
||||
|
||||
printf("Active wtps: %i\n",acinfo.active_wtps);
|
||||
printf("Active max wtps: %i\n",acinfo.max_wtps);
|
||||
|
||||
|
||||
printf("Got Discovery response\n");
|
||||
|
||||
printf("ACL COUNT: %i\n",acinfo.aciplist->count);
|
||||
*/
|
||||
aciplist_foreach(acinfo->aciplist,acprint,0);
|
||||
|
||||
// responses++;
|
||||
//
|
||||
di->response_count++;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void rand_sleep(int seconds)
|
||||
{
|
||||
int usecs = seconds * 1000;
|
||||
uint16_t rnd;
|
||||
cw_rand((uint8_t*)&rnd,sizeof(rnd));
|
||||
uint16_t max = 0-1;
|
||||
int r = (rnd * usecs) / max;
|
||||
cw_dbg(DBG_CW_INFO,"Sleeping for %u milliseconds\n",r);
|
||||
usleep(r*1000);
|
||||
}
|
||||
|
||||
|
||||
static int do_discover_conn(struct conn * conn,struct discovery_info * di)
|
||||
{
|
||||
rand_sleep(conf_max_discovery_interval);
|
||||
|
||||
|
||||
struct wtpinfo * wtpinfo;
|
||||
wtpinfo = get_wtpinfo();
|
||||
// wtpinfo_print(wtpinfo);
|
||||
|
||||
|
||||
// struct timespec tstart,tcur;
|
||||
struct radioinfo ri;
|
||||
memset(&ri,0,sizeof(ri));
|
||||
|
||||
struct radioinfo * rip = &wtpinfo->radioinfo[0];
|
||||
|
||||
/*
|
||||
ri.rmac[0]=6;
|
||||
ri.rmac[2]=14;
|
||||
ri.rmac[3]=14;
|
||||
ri.rmac[4]=14;
|
||||
ri.rmac[5]=14;
|
||||
ri.rmac[6]=14;
|
||||
ri.rmac[7]=14;
|
||||
*/
|
||||
|
||||
#ifdef WITH_CW_LOG_DEBUG
|
||||
char str[100];
|
||||
sock_addrtostr((struct sockaddr*)&conn->addr,str,100);
|
||||
// cw_log_debug0("Sending discovery request to %s",str);
|
||||
#endif
|
||||
|
||||
int rc;
|
||||
|
||||
do {
|
||||
conn->capwap_mode=CWMODE_CISCO;
|
||||
rc = cwsend_discovery_request(conn,rip,wtpinfo);
|
||||
if (rc<0){
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
if (errno == EMSGSIZE){
|
||||
conn->mtu-=4;
|
||||
// cw_log_debug2("Setting mtu to %i",conn->mtu);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
}while (rc<0);
|
||||
|
||||
|
||||
if (rc < 0 )
|
||||
{
|
||||
char str[100];
|
||||
sock_addrtostr((struct sockaddr*)&conn->addr,str,100);
|
||||
cw_log(LOG_ERR,"Error sending discovery request to %s: %s",str,strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct connlist * connlist;
|
||||
connlist = connlist_create(30);
|
||||
|
||||
// clock_gettime(CLOCK_REALTIME,&tstart);
|
||||
//
|
||||
int tstart = time(0);
|
||||
int treset = 0;
|
||||
do {
|
||||
char buf[2048];
|
||||
int buflen=2048;
|
||||
struct sockaddr_storage sa;
|
||||
socklen_t fromlen=sizeof(struct sockaddr_storage);
|
||||
rc = recvfrom(conn->sock,buf,buflen,0,(struct sockaddr*)&sa,&fromlen);
|
||||
if (rc<0){
|
||||
if (errno==EINTR)
|
||||
rc=0;
|
||||
if (errno==EAGAIN)
|
||||
rc=0;
|
||||
if (errno==EWOULDBLOCK)
|
||||
rc=0;
|
||||
}
|
||||
if (rc>0) {
|
||||
#ifdef WITH_CW_LOG_DEBUG
|
||||
char str[100];
|
||||
sock_addrtostr((struct sockaddr*)&sa,str,100);
|
||||
// cw_log_debug0("Received packet from %s",str);
|
||||
#endif
|
||||
|
||||
struct conn * rconn;
|
||||
rconn = connlist_get(connlist,(struct sockaddr*)&sa);
|
||||
|
||||
if (!rconn){
|
||||
rconn = conn_create_noq(conn->sock,(struct sockaddr*)&sa); //msg_cb,NULL,0);
|
||||
// rconn->pmsgarg=conn->pmsgarg;
|
||||
rconn->mtu = conn->mtu;
|
||||
rconn->seqnum=conn->seqnum;
|
||||
connlist_add(connlist,rconn);
|
||||
}
|
||||
|
||||
conn_process_packet(rconn,(uint8_t*)buf,rc,msg_cb,di);
|
||||
}
|
||||
|
||||
/* reset discovery timer after we have received the first response */
|
||||
if ( di->response_count == 1 && !treset ){
|
||||
tstart=time(0);
|
||||
treset=1;
|
||||
}
|
||||
|
||||
//clock_gettime(CLOCK_REALTIME,&tcur);
|
||||
// printf("TTSub: %i %i\n",time(0)-tstart, conf_discovery_interval);
|
||||
}while(time(0)-tstart < conf_discovery_interval && rc>=0 );
|
||||
|
||||
|
||||
if (rc <0){
|
||||
char str[100];
|
||||
sock_addrtostr((struct sockaddr*)&conn->addr,str,100);
|
||||
cw_log(LOG_ERR,"Error sendings discovery request to %s: %s",str,strerror(errno));
|
||||
}
|
||||
|
||||
connlist_destroy(connlist);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int discovery_count;
|
||||
|
||||
ACIPLIST * do_discovery(const char *acaddr)
|
||||
/**
|
||||
* Run discovery for on address (eg broadcast 255.255.255.255)
|
||||
*/
|
||||
static int cw_run_discovery(struct conn *conn, const char *acaddr)
|
||||
{
|
||||
|
||||
/* get an partially intialized connection object
|
||||
* (seqnum should be set)
|
||||
* */
|
||||
struct conn * conn = get_conn();
|
||||
if (!conn){
|
||||
cw_log(LOG_ERR,"Can't create conn for %s: %s",acaddr,strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* get addr of destination */
|
||||
/* get addr of destination */
|
||||
struct addrinfo hints;
|
||||
struct addrinfo * res,*res0;
|
||||
memset(&hints,0,sizeof(hints));
|
||||
@ -285,21 +87,8 @@ ACIPLIST * do_discovery(const char *acaddr)
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct discovery_info di;
|
||||
memset (&di,0,sizeof(struct discovery_info));
|
||||
di.aciplist = aciplist_create();
|
||||
di.response_count=0;
|
||||
|
||||
|
||||
for(res=res0; res; res=res->ai_next)
|
||||
{
|
||||
if ( discovery_count >= conf_max_discoveries){
|
||||
sulking_state();
|
||||
discovery_count=0;
|
||||
// cw_log_debug0("Entering discovery state");
|
||||
}
|
||||
discovery_count++;
|
||||
|
||||
|
||||
int sockfd;
|
||||
int opt;
|
||||
@ -311,39 +100,30 @@ ACIPLIST * do_discovery(const char *acaddr)
|
||||
|
||||
opt = 1;
|
||||
if (setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &opt, sizeof(opt))<0){
|
||||
cw_log(LOG_ERR,"Cant set broadcast sockopt");
|
||||
cw_log(LOG_ERR,"Can't set broadcast sockopt");
|
||||
}
|
||||
sock_set_recvtimeout(sockfd,1);
|
||||
|
||||
sock_set_dontfrag(sockfd,0);
|
||||
|
||||
di.conn=conn;
|
||||
|
||||
sock_copyaddr(&conn->addr,res->ai_addr);
|
||||
conn->sock=sockfd;
|
||||
|
||||
do_discover_conn(conn,&di);
|
||||
|
||||
|
||||
if ( di.aciplist->count != 0){
|
||||
set_sock(sockfd);
|
||||
break;
|
||||
}
|
||||
run_discovery(conn);
|
||||
|
||||
close(sockfd);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
freeaddrinfo(res0);
|
||||
|
||||
if (di.aciplist->count){
|
||||
cw_dbg(DBG_CW_INFO,"Discovery responses received: %i\n",di.response_count);
|
||||
return di.aciplist;
|
||||
|
||||
}
|
||||
|
||||
aciplist_destroy(di.aciplist);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int discovery()
|
||||
{
|
||||
struct conn * conn = get_conn();
|
||||
cw_run_discovery(conn,"255.255.255.255");
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user