2014-07-11 22:12:11 +02:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
#include "wtp_interface.h"
|
|
|
|
#include "wtp_conf.h"
|
|
|
|
|
2016-03-03 19:51:42 +01:00
|
|
|
#include "cw/dtls.h"
|
|
|
|
#include "cw/log.h"
|
|
|
|
#include "cw/dbg.h"
|
|
|
|
#include "cw/sock.h"
|
|
|
|
#include "cw/dtls.h"
|
|
|
|
#include "cw/aciplist.h"
|
|
|
|
#include "cw/capwap_items.h"
|
|
|
|
#include "cw/mbag.h"
|
2014-07-11 22:12:11 +02:00
|
|
|
|
2015-04-07 07:42:36 +02:00
|
|
|
/*
|
2014-07-11 22:12:11 +02:00
|
|
|
#define acinfo_log acinfo_log_
|
|
|
|
|
|
|
|
void acinfo_log_(int level,const struct ac_info *acinfo,const char * xstr)
|
|
|
|
{
|
|
|
|
char str[8192];
|
|
|
|
acinfo_print(str,acinfo);
|
2015-03-14 21:41:50 +01:00
|
|
|
cw_dbg(DBG_CW_INFO,"%s",str);
|
2015-02-08 21:07:55 +01:00
|
|
|
// cw_log_debug(level,"%s\n%s",xstr,str);
|
2014-07-11 22:12:11 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int join_state(struct conn * conn)
|
|
|
|
{
|
|
|
|
struct wtpinfo * wtpinfo = get_wtpinfo();
|
|
|
|
|
|
|
|
struct radioinfo ri;
|
|
|
|
memset(&ri,0,sizeof(ri));
|
|
|
|
int rc;
|
|
|
|
|
2015-03-14 21:41:50 +01:00
|
|
|
|
|
|
|
struct radioinfo *rip = &(wtpinfo->radioinfo[0]);
|
|
|
|
|
2015-03-23 22:26:05 +01:00
|
|
|
//#ifdef WITH_CW_LOG_DEBUG
|
|
|
|
// char str[64];
|
|
|
|
// sock_addrtostr(&conn->addr,str,64);
|
2015-02-08 21:07:55 +01:00
|
|
|
// cw_log_debug0("Sending join request to %s",str);
|
2015-03-23 22:26:05 +01:00
|
|
|
//#endif
|
2014-07-11 22:12:11 +02:00
|
|
|
printf("Seqnum before = %i\n",conn->seqnum);
|
2015-03-14 21:41:50 +01:00
|
|
|
rc = cwsend_join_request(conn,rip,wtpinfo);
|
2014-07-11 22:12:11 +02:00
|
|
|
printf("Seqnum after = %i\n",conn->seqnum);
|
|
|
|
|
2015-03-14 10:15:12 +01:00
|
|
|
|
|
|
|
struct cwrmsg * cwrmsg;
|
|
|
|
// do {
|
2015-03-14 21:41:50 +01:00
|
|
|
int i;
|
|
|
|
for(int i=0; i<10; i++){
|
|
|
|
|
2015-03-14 10:15:12 +01:00
|
|
|
cwrmsg = conn_get_message(conn);
|
2015-03-14 21:41:50 +01:00
|
|
|
if ( cwrmsg)
|
|
|
|
break;
|
|
|
|
}
|
2015-03-12 23:21:57 +01:00
|
|
|
printf("Received %08p\n",cwrmsg);
|
2015-03-14 10:15:12 +01:00
|
|
|
// }while(cwrmsg==0);
|
|
|
|
|
|
|
|
|
2015-03-23 07:48:27 +01:00
|
|
|
if(!cwrmsg) {
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("SEQS %d %d\n",cwrmsg->type,conn->seqnum);
|
2015-03-12 23:21:57 +01:00
|
|
|
|
2015-02-08 21:07:55 +01:00
|
|
|
// cw_log_debug0("Received message %i",cwrmsg->seqnum);
|
2014-07-11 22:12:11 +02:00
|
|
|
|
|
|
|
if (cwrmsg->type != CWMSG_JOIN_RESPONSE || cwrmsg->seqnum != conn->seqnum){
|
2015-03-23 07:48:27 +01:00
|
|
|
printf("Wrong message %d %d\n",cwrmsg->type,conn->seqnum);
|
2015-03-14 21:41:50 +01:00
|
|
|
|
2014-07-11 22:12:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ac_info acinfo;
|
|
|
|
memset(&acinfo,0,sizeof(acinfo));
|
|
|
|
|
2015-03-23 07:48:27 +01:00
|
|
|
acinfo.result_code=99;
|
2015-03-14 21:41:50 +01:00
|
|
|
|
2014-07-11 22:12:11 +02:00
|
|
|
cwread_join_response(&acinfo,cwrmsg->msgelems,cwrmsg->msgelems_len);
|
2015-03-14 21:41:50 +01:00
|
|
|
|
2014-07-11 22:12:11 +02:00
|
|
|
acinfo_log(0,&acinfo,"Connectet to the following AC");
|
2015-03-14 21:41:50 +01:00
|
|
|
|
|
|
|
printf("Result_code: %d\n",acinfo.result_code);
|
|
|
|
|
2014-07-11 22:12:11 +02:00
|
|
|
}
|
|
|
|
|
2015-04-10 17:14:55 +02:00
|
|
|
*/
|
2014-07-11 22:12:11 +02:00
|
|
|
|
2015-04-10 17:14:55 +02:00
|
|
|
int run_join_d(struct sockaddr *sa)
|
|
|
|
{
|
|
|
|
struct conn *conn = get_conn();
|
2016-03-05 08:51:52 +01:00
|
|
|
conn->capwap_state = CW_STATE_JOIN;
|
2015-04-10 17:14:55 +02:00
|
|
|
|
|
|
|
int sockfd;
|
|
|
|
int rc;
|
|
|
|
|
2016-03-06 11:23:50 +01:00
|
|
|
sockfd = socket(sa->sa_family, SOCK_DGRAM, 0);
|
2016-03-05 08:51:52 +01:00
|
|
|
if (sockfd == -1) {
|
|
|
|
cw_log(LOG_ERR, "Can't create socket: %s\n", strerror(errno));
|
2015-04-10 17:14:55 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2016-03-05 08:51:52 +01:00
|
|
|
sock_set_recvtimeout(sockfd, 1);
|
2016-03-06 11:23:50 +01:00
|
|
|
conn->sock = sockfd;
|
|
|
|
sock_copyaddr(&conn->addr, sa);
|
2015-04-10 17:14:55 +02:00
|
|
|
|
2016-03-06 16:48:49 +01:00
|
|
|
|
|
|
|
/* we call connect to bind this socket to a local IP address,
|
|
|
|
* which we can later obtain by getsockname */
|
|
|
|
rc = connect(sockfd, (struct sockaddr *) sa,
|
2016-03-05 08:51:52 +01:00
|
|
|
sock_addrlen((struct sockaddr *) sa));
|
2015-04-10 17:14:55 +02:00
|
|
|
|
2016-03-05 08:51:52 +01:00
|
|
|
if (rc < 0) {
|
|
|
|
cw_log(LOG_ERR, "Can't connect to %s: %s\n", sock_addr2str(sa),
|
|
|
|
strerror(errno));
|
2015-04-10 17:14:55 +02:00
|
|
|
close(sockfd);
|
|
|
|
return -1;
|
|
|
|
}
|
2016-03-06 16:48:49 +01:00
|
|
|
|
2015-04-10 17:14:55 +02:00
|
|
|
|
2016-03-05 08:51:52 +01:00
|
|
|
cw_dbg(DBG_DTLS, "Establishing DTLS session with %s", sock_addr2str(sa));
|
2015-04-10 17:14:55 +02:00
|
|
|
|
2016-03-05 08:51:52 +01:00
|
|
|
int dtls_conf_ok=0;
|
|
|
|
|
|
|
|
if (conf_dtls_psk) {
|
|
|
|
conn->dtls_psk = conf_dtls_psk;
|
|
|
|
conn->dtls_psk_len = strlen(conn->dtls_psk);
|
|
|
|
conn->dtls_cipher = conf_dtls_cipher;
|
|
|
|
dtls_conf_ok=1;
|
2015-04-10 17:14:55 +02:00
|
|
|
}
|
|
|
|
|
2016-03-05 08:51:52 +01:00
|
|
|
if (conf_sslkeyfilename && conf_sslcertfilename) {
|
2015-04-10 17:14:55 +02:00
|
|
|
|
|
|
|
conn->dtls_key_file = conf_sslkeyfilename;
|
|
|
|
conn->dtls_cert_file = conf_sslcertfilename;
|
|
|
|
conn->dtls_key_pass = conf_sslkeypass;
|
2016-03-05 08:51:52 +01:00
|
|
|
conn->dtls_cipher = conf_dtls_cipher;
|
|
|
|
dtls_conf_ok=1;
|
2015-04-10 17:14:55 +02:00
|
|
|
}
|
|
|
|
|
2016-03-05 08:51:52 +01:00
|
|
|
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));
|
|
|
|
close(sockfd);
|
|
|
|
return 0;
|
|
|
|
}
|
2015-04-10 17:14:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
rc = dtls_connect(conn);
|
2016-03-05 08:51:52 +01:00
|
|
|
if (rc != 1) {
|
2015-04-10 17:14:55 +02:00
|
|
|
dtls_shutdown(conn);
|
2016-03-06 11:23:50 +01:00
|
|
|
cw_log(LOG_ERR, "Can't establish DTLS connection with %s",
|
2016-03-05 08:51:52 +01:00
|
|
|
sock_addr2str(sa));
|
2015-04-10 17:14:55 +02:00
|
|
|
close(sockfd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-05 08:51:52 +01:00
|
|
|
cw_dbg(DBG_DTLS, "DTLS Connection successful established with %s",
|
|
|
|
sock_addr2str(sa));
|
2014-07-11 22:12:11 +02:00
|
|
|
|
2015-04-10 17:14:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-05 08:51:52 +01:00
|
|
|
int run_join(struct conn *conn)
|
2015-04-10 17:14:55 +02:00
|
|
|
{
|
|
|
|
|
2016-03-05 08:51:52 +01:00
|
|
|
// cw_init_request(conn, CW_MSG_JOIN_REQUEST);
|
|
|
|
// if ( cw_put_msg(conn, conn->req_buffer) == -1 )
|
|
|
|
// return 0;
|
2015-04-10 17:14:55 +02:00
|
|
|
//
|
2016-03-05 08:51:52 +01:00
|
|
|
// conn_send_msg(conn, conn->req_buffer);
|
2015-04-10 17:14:55 +02:00
|
|
|
|
|
|
|
|
2015-04-26 15:48:40 +02:00
|
|
|
|
2016-03-05 08:51:52 +01:00
|
|
|
int rc = cw_send_request(conn, CW_MSG_JOIN_REQUEST);
|
|
|
|
|
|
|
|
if (!cw_rcok(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));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
cw_log(LOG_ERR, "Can't Join AC at %s: %d - %s.",
|
|
|
|
sock_addr2str(&conn->addr), errno, cw_strerror(rc));
|
2015-04-26 15:48:40 +02:00
|
|
|
}
|
2015-04-10 17:14:55 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-05 08:51:52 +01:00
|
|
|
cw_dbg(DBG_ELEM, "Joined AC at %s, Join Result: %d - %s",
|
|
|
|
sock_addr2str(&conn->addr), rc, cw_strresult(rc));
|
2015-04-10 17:14:55 +02:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int join()
|
|
|
|
{
|
2016-03-05 08:51:52 +01:00
|
|
|
struct conn *conn = get_conn();
|
|
|
|
|
|
|
|
printf("Join\n");
|
2015-04-18 11:20:24 +02:00
|
|
|
|
2016-03-05 08:51:52 +01:00
|
|
|
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.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mavl_get_count(iplist)){
|
|
|
|
cw_log(LOG_ERR, "No IPs to join controller. IP list is empty.");
|
2015-04-10 17:14:55 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-05 08:51:52 +01:00
|
|
|
DEFINE_AVLITER(ii, iplist);
|
|
|
|
avliter_foreach(&ii) {
|
2015-04-10 17:14:55 +02:00
|
|
|
|
2016-03-05 08:51:52 +01:00
|
|
|
cw_acip_t *ip = avliter_get(&ii);
|
|
|
|
|
2016-03-06 11:23:50 +01:00
|
|
|
|
2016-03-05 08:51:52 +01:00
|
|
|
cw_dbg(DBG_INFO, "Going to join CAWAP controller on %s",
|
2016-03-06 11:23:50 +01:00
|
|
|
sock_addr2str_p(&ip->ip));
|
2016-03-05 08:51:52 +01:00
|
|
|
|
|
|
|
int rc = run_join_d((struct sockaddr *) &ip->ip);
|
2016-03-06 11:23:50 +01:00
|
|
|
|
|
|
|
if (rc<=0)
|
2015-04-10 17:14:55 +02:00
|
|
|
continue;
|
2016-03-06 11:23:50 +01:00
|
|
|
|
2015-04-10 17:14:55 +02:00
|
|
|
rc = run_join(conn);
|
2016-03-05 08:51:52 +01:00
|
|
|
if (rc) {
|
|
|
|
conn->capwap_state = CW_STATE_CONFIGURE;
|
2015-04-10 17:14:55 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2016-03-05 08:51:52 +01:00
|
|
|
|
|
|
|
|
2015-04-10 17:14:55 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-05 08:51:52 +01:00
|
|
|
|
2015-04-10 17:14:55 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|