2014-07-11 22:12:11 +02:00
|
|
|
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
2018-03-24 07:56:05 +01:00
|
|
|
|
2016-03-03 19:51:42 +01:00
|
|
|
#include "cw/capwap.h"
|
2018-03-12 18:01:40 +01:00
|
|
|
#include "cw/conn.h"
|
2016-03-03 19:51:42 +01:00
|
|
|
#include "cw/sock.h"
|
2018-03-12 18:01:40 +01:00
|
|
|
#include "cw/ktv.h"
|
|
|
|
#include "cw/log.h"
|
|
|
|
#include "cw/dbg.h"
|
2016-03-03 19:51:42 +01:00
|
|
|
#include "cw/timer.h"
|
2018-03-17 16:21:23 +01:00
|
|
|
#include "cw/cw.h"
|
2018-03-30 19:45:27 +02:00
|
|
|
#include "cw/rand.h"
|
2014-07-11 22:12:11 +02:00
|
|
|
|
|
|
|
#include "wtp.h"
|
2015-04-10 17:14:55 +02:00
|
|
|
|
|
|
|
|
2018-03-30 23:58:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int cw_select_ac(mavl_t local_cfg, struct cw_DiscoveryResult * dis)
|
2018-03-30 19:45:27 +02:00
|
|
|
{
|
|
|
|
mlistelem_t *e;
|
2018-03-30 23:58:21 +02:00
|
|
|
/*mavl_t iplist;*/
|
2018-03-30 11:58:05 +02:00
|
|
|
int en;
|
2018-03-30 19:45:27 +02:00
|
|
|
|
2018-03-30 23:58:21 +02:00
|
|
|
/*iplist = cw_ktv_create();
|
|
|
|
if (iplist == NULL){
|
|
|
|
cw_log_errno("cw_select_ac: Can't allocate memory for aciplist");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
*/
|
2018-03-30 11:12:50 +02:00
|
|
|
|
2018-03-30 19:45:27 +02:00
|
|
|
en = 0;
|
2018-03-30 11:12:50 +02:00
|
|
|
|
|
|
|
/* for each discovery response */
|
2018-03-30 23:58:21 +02:00
|
|
|
mlist_foreach(e, dis->results) {
|
2018-03-30 19:45:27 +02:00
|
|
|
char acname[CAPWAP_MAX_AC_NAME_LEN + 1];
|
2018-03-29 10:53:26 +02:00
|
|
|
char key[CW_KTV_MAX_KEY_LEN];
|
2018-03-30 11:12:50 +02:00
|
|
|
mavl_t remote_cfg;
|
2018-03-30 19:45:27 +02:00
|
|
|
cw_KTV_t *val, *ipval;
|
|
|
|
int prio, i;
|
|
|
|
|
2018-03-30 11:12:50 +02:00
|
|
|
remote_cfg = mlistelem_get_ptr(e);
|
2018-03-30 19:45:27 +02:00
|
|
|
|
2018-03-30 11:12:50 +02:00
|
|
|
/* get ac name */
|
2018-03-30 19:45:27 +02:00
|
|
|
val = cw_ktv_get(remote_cfg, "ac-name", CW_TYPE_BSTR16);
|
|
|
|
if (val == NULL) {
|
2018-03-30 11:58:05 +02:00
|
|
|
/* this should not happen, because AC Name is a
|
|
|
|
* amndatory message element */
|
2018-03-30 19:45:27 +02:00
|
|
|
prio = 255;
|
|
|
|
} else {
|
2018-03-30 11:58:05 +02:00
|
|
|
/* Get priority for AC from
|
|
|
|
* ac-name-with-priority list */
|
2018-03-30 19:45:27 +02:00
|
|
|
val->type->to_str(val, acname, CAPWAP_MAX_AC_NAME_LEN);
|
|
|
|
sprintf(key, "ac-name-with-priority/%s", acname);
|
|
|
|
prio = cw_ktv_get_byte(local_cfg, key, 255);
|
2018-03-30 11:58:05 +02:00
|
|
|
}
|
2018-03-30 19:45:27 +02:00
|
|
|
|
2018-03-30 11:58:05 +02:00
|
|
|
/* for each control ip address the AC has sent */
|
2018-03-30 19:45:27 +02:00
|
|
|
i = 0;
|
2018-03-29 10:53:26 +02:00
|
|
|
do {
|
2018-03-30 19:45:27 +02:00
|
|
|
sprintf(key, "%s.%d", "capwap-control-ip-address/wtps", i);
|
|
|
|
val = cw_ktv_get(remote_cfg, key, CW_TYPE_WORD);
|
2018-03-29 10:53:26 +02:00
|
|
|
if (val == NULL)
|
|
|
|
break;
|
2018-03-30 11:12:50 +02:00
|
|
|
|
2018-03-30 19:45:27 +02:00
|
|
|
sprintf(key, "%s.%d", "capwap-control-ip-address/address", i);
|
|
|
|
ipval = cw_ktv_get(remote_cfg, key, CW_TYPE_IPADDRESS);
|
|
|
|
|
|
|
|
sprintf(key, "%04d%05d%04d", prio, val->val.word, en);
|
|
|
|
|
2018-04-25 10:43:27 +02:00
|
|
|
cw_ktv_add(dis->prio_ip, key, CW_TYPE_SYSPTR, NULL, (uint8_t *) (&ipval),
|
2018-03-30 19:45:27 +02:00
|
|
|
sizeof(ipval));
|
2018-04-25 10:43:27 +02:00
|
|
|
cw_ktv_add(dis->prio_ac, key, CW_TYPE_SYSPTR, NULL, (uint8_t *)(&remote_cfg),
|
2018-03-31 08:37:18 +02:00
|
|
|
sizeof(remote_cfg));
|
2018-03-30 11:58:05 +02:00
|
|
|
i++;
|
|
|
|
en++;
|
2018-03-30 19:45:27 +02:00
|
|
|
} while (1);
|
|
|
|
|
2018-03-29 10:53:26 +02:00
|
|
|
}
|
2018-03-30 19:45:27 +02:00
|
|
|
|
|
|
|
|
2018-03-30 23:58:21 +02:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void cw_discovery_free_results(struct cw_DiscoveryResult * dis)
|
|
|
|
{
|
|
|
|
if (dis->prio_ac != NULL)
|
|
|
|
mavl_destroy(dis->prio_ac);
|
|
|
|
if (dis->prio_ip != NULL)
|
|
|
|
mavl_destroy(dis->prio_ip);
|
|
|
|
if (dis->results != NULL)
|
|
|
|
mlist_destroy(dis->results);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-31 08:37:18 +02:00
|
|
|
static void result_del(void * data)
|
|
|
|
{
|
|
|
|
|
|
|
|
mavl_t todelete = *((void**)data);
|
|
|
|
mavl_destroy(todelete);
|
|
|
|
|
|
|
|
|
|
|
|
/*mavl_t*/
|
|
|
|
}
|
2018-03-30 23:58:21 +02:00
|
|
|
|
|
|
|
int cw_discovery_init_results(struct cw_DiscoveryResult *dis)
|
|
|
|
{
|
2018-03-31 08:37:18 +02:00
|
|
|
|
|
|
|
dis->results = mlist_create(NULL, result_del, sizeof(void *));
|
2018-03-30 23:58:21 +02:00
|
|
|
if (dis->results==NULL)
|
|
|
|
goto errX;
|
|
|
|
dis->prio_ac=cw_ktv_create();
|
|
|
|
if (dis->prio_ac==NULL)
|
|
|
|
goto errX;
|
|
|
|
dis->prio_ip=cw_ktv_create();
|
|
|
|
if (dis->prio_ac==NULL)
|
|
|
|
goto errX;
|
|
|
|
return 1;
|
|
|
|
errX:
|
|
|
|
cw_discovery_free_results(dis);
|
2018-03-29 10:53:26 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-30 23:58:21 +02:00
|
|
|
|
|
|
|
static int run_discovery(struct conn *conn, struct cw_DiscoveryResult * dis)
|
2014-07-11 22:12:11 +02:00
|
|
|
{
|
2018-03-12 18:01:40 +01:00
|
|
|
time_t timer;
|
2018-03-28 09:36:15 +02:00
|
|
|
struct sockaddr_storage from;
|
2018-03-30 19:45:27 +02:00
|
|
|
int delay, min, max;
|
2018-03-30 23:58:21 +02:00
|
|
|
|
2018-03-28 09:36:15 +02:00
|
|
|
|
2018-03-12 18:01:40 +01:00
|
|
|
|
2018-03-30 19:45:27 +02:00
|
|
|
min = cw_ktv_get_byte(conn->local_cfg,"capwap-timers/min-discovery-interval",
|
|
|
|
CAPWAP_MIN_DISCOVERY_INTERVAL);
|
|
|
|
max = cw_ktv_get_byte(conn->local_cfg,"capwap-timers/max-discovery-interval",
|
|
|
|
CAPWAP_MAX_DISCOVERY_INTERVAL);
|
|
|
|
|
|
|
|
delay = cw_randint(min,max);
|
|
|
|
if (delay>0){
|
|
|
|
cw_dbg(DBG_INFO,"Sleeping for %d seconds before sending Discovery Request.", delay);
|
|
|
|
sleep(delay);
|
|
|
|
}
|
2018-03-15 20:07:17 +01:00
|
|
|
|
2018-03-30 19:45:27 +02:00
|
|
|
|
2018-05-07 10:57:12 +02:00
|
|
|
conn->capwap_state = CAPWAP_STATE_DISCOVERY;
|
2018-03-15 20:07:17 +01:00
|
|
|
|
|
|
|
|
2018-03-30 19:45:27 +02:00
|
|
|
/* create and send a discovery request message */
|
2018-02-23 13:59:36 +01:00
|
|
|
cw_init_request(conn, CAPWAP_MSG_DISCOVERY_REQUEST);
|
2015-04-10 17:14:55 +02:00
|
|
|
cw_put_msg(conn, conn->req_buffer);
|
|
|
|
conn_send_msg(conn, conn->req_buffer);
|
2015-03-15 09:18:49 +01:00
|
|
|
|
|
|
|
|
2018-03-30 19:45:27 +02:00
|
|
|
timer = cw_timer_start(cw_ktv_get_byte(conn->local_cfg, "discovery-interval",
|
|
|
|
CAPWAP_DISCOVERY_INTERVAL))-1;
|
2014-07-28 07:08:27 +02:00
|
|
|
|
2018-03-30 23:58:21 +02:00
|
|
|
/*discovery_results = mlist_create(NULL, NULL, sizeof(void *));*/
|
|
|
|
|
|
|
|
|
2015-04-10 17:14:55 +02:00
|
|
|
while (!cw_timer_timeout(timer)
|
2018-05-07 10:57:12 +02:00
|
|
|
&& conn->capwap_state == CAPWAP_STATE_DISCOVERY) {
|
2018-03-12 18:01:40 +01:00
|
|
|
int rc;
|
2018-03-28 09:36:15 +02:00
|
|
|
char addr_str[SOCK_ADDR_BUFSIZE];
|
2018-03-30 19:45:27 +02:00
|
|
|
|
|
|
|
conn->remote_cfg = cw_ktv_create();
|
|
|
|
if (conn->remote_cfg == NULL) {
|
|
|
|
cw_log_errno("Can't allocate memory for remote_cfg");
|
|
|
|
break;
|
|
|
|
}
|
2015-04-10 17:14:55 +02:00
|
|
|
|
2018-03-28 09:36:15 +02:00
|
|
|
rc = cw_read_from(conn, &from);
|
2018-03-30 19:45:27 +02:00
|
|
|
if (rc < 0) {
|
|
|
|
if (errno == EAGAIN)
|
2015-04-10 17:14:55 +02:00
|
|
|
continue;
|
|
|
|
|
2018-03-30 19:45:27 +02:00
|
|
|
cw_log(LOG_ERROR, "Error reading messages: %s", strerror(errno));
|
2015-04-10 17:14:55 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-03-30 19:45:27 +02:00
|
|
|
cw_dbg(DBG_INFO, "Received Discovery Response from %s",
|
|
|
|
sock_addr2str(&from, addr_str));
|
2018-03-30 23:58:21 +02:00
|
|
|
mlist_append_ptr(dis->results, conn->remote_cfg);
|
2015-04-10 17:14:55 +02:00
|
|
|
}
|
2018-03-24 07:56:05 +01:00
|
|
|
|
2018-03-30 23:58:21 +02:00
|
|
|
cw_select_ac(conn->local_cfg, dis);
|
2015-04-10 17:14:55 +02:00
|
|
|
|
2015-04-19 23:27:44 +02:00
|
|
|
|
|
|
|
|
2015-04-26 12:36:53 +02:00
|
|
|
return 1;
|
2014-07-11 22:12:11 +02:00
|
|
|
}
|
|
|
|
|
2018-03-30 23:58:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2015-04-07 07:42:36 +02:00
|
|
|
/**
|
|
|
|
* Run discovery for on address (eg broadcast 255.255.255.255)
|
2015-04-10 17:14:55 +02:00
|
|
|
*/
|
2018-03-30 23:58:21 +02:00
|
|
|
int cw_run_discovery(struct conn *conn, const char *addr, const char *bindaddr,
|
|
|
|
struct cw_DiscoveryResult * dis)
|
2014-07-11 22:12:11 +02:00
|
|
|
{
|
2018-03-12 18:01:40 +01:00
|
|
|
char sock_buf[SOCK_ADDR_BUFSIZE];
|
|
|
|
struct sockaddr_storage dstaddr;
|
|
|
|
char caddr[256], control_port[64];
|
2018-03-30 19:45:27 +02:00
|
|
|
|
2018-03-12 18:01:40 +01:00
|
|
|
int rc;
|
2018-03-30 23:58:21 +02:00
|
|
|
/*struct cw_DiscoveryResult dis;*/
|
|
|
|
|
|
|
|
|
|
|
|
/*dis.results = mlist_create(NULL, NULL, sizeof(void *));*/
|
|
|
|
|
2018-03-30 19:45:27 +02:00
|
|
|
|
2015-04-10 17:14:55 +02:00
|
|
|
/* get addr of destination */
|
2014-07-11 22:12:11 +02:00
|
|
|
struct addrinfo hints;
|
2015-04-10 17:14:55 +02:00
|
|
|
struct addrinfo *res, *res0;
|
2018-03-12 18:01:40 +01:00
|
|
|
|
2018-03-30 19:45:27 +02:00
|
|
|
|
|
|
|
|
2015-04-10 17:14:55 +02:00
|
|
|
memset(&hints, 0, sizeof(hints));
|
2014-07-11 22:12:11 +02:00
|
|
|
|
|
|
|
hints.ai_socktype = SOCK_DGRAM;
|
2018-03-17 11:40:43 +01:00
|
|
|
hints.ai_family = AF_UNSPEC;
|
2018-03-30 19:45:27 +02:00
|
|
|
|
|
|
|
|
2018-03-12 18:01:40 +01:00
|
|
|
strncpy(caddr, addr, sizeof(caddr));
|
2018-03-30 19:45:27 +02:00
|
|
|
|
2018-03-12 18:01:40 +01:00
|
|
|
/* try to convert the given address string to sockaddr */
|
2018-03-30 19:45:27 +02:00
|
|
|
rc = sock_strtoaddr(caddr, (struct sockaddr *) &dstaddr);
|
2018-03-12 18:01:40 +01:00
|
|
|
if (rc) {
|
|
|
|
int iport;
|
2018-03-30 19:45:27 +02:00
|
|
|
/* converting to socaddr was successful */
|
|
|
|
iport = sock_getport((struct sockaddr *) &dstaddr);
|
2018-03-12 18:01:40 +01:00
|
|
|
if (iport != 0)
|
|
|
|
sprintf(control_port, "%d", iport);
|
|
|
|
else
|
2018-03-30 19:45:27 +02:00
|
|
|
sprintf(control_port, "%d", CAPWAP_CONTROL_PORT);
|
|
|
|
} else {
|
|
|
|
char *port;
|
2018-03-12 18:01:40 +01:00
|
|
|
/* converting went wrong, so the address string might
|
|
|
|
* be a DNS entry. Look for a colon to find the port. */
|
2018-03-30 19:45:27 +02:00
|
|
|
port = strchr(addr, ':');
|
|
|
|
if (port) {
|
|
|
|
strncpy(control_port, port + 1, sizeof(control_port));
|
|
|
|
caddr[port - addr] = 0;
|
|
|
|
} else {
|
|
|
|
sprintf(control_port, "%d", CAPWAP_CONTROL_PORT);
|
2018-03-12 18:01:40 +01:00
|
|
|
}
|
|
|
|
}
|
2014-07-11 22:12:11 +02:00
|
|
|
|
2018-03-30 19:45:27 +02:00
|
|
|
|
|
|
|
/*printf("Addr: %s, Port: %s\n",caddr,control_port); */
|
2018-03-12 18:01:40 +01:00
|
|
|
|
|
|
|
rc = getaddrinfo(caddr, control_port, &hints, &res0);
|
2015-04-10 17:14:55 +02:00
|
|
|
if (rc) {
|
2018-03-12 18:01:40 +01:00
|
|
|
cw_log(LOG_ERR, "Can't connect to AC %s: %s", caddr, gai_strerror(rc));
|
2014-07-11 22:12:11 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-10 17:14:55 +02:00
|
|
|
for (res = res0; res; res = res->ai_next) {
|
2014-07-11 22:12:11 +02:00
|
|
|
|
|
|
|
int sockfd;
|
|
|
|
int opt;
|
2015-04-10 17:14:55 +02:00
|
|
|
sockfd = socket(res->ai_family, SOCK_DGRAM, 0);
|
|
|
|
if (sockfd == -1) {
|
2018-03-12 18:01:40 +01:00
|
|
|
cw_log(LOG_ERR, "Can't create socket for %s: %s", caddr,
|
2015-04-10 17:14:55 +02:00
|
|
|
strerror(errno));
|
2014-07-11 22:12:11 +02:00
|
|
|
continue;
|
|
|
|
}
|
2015-04-10 17:14:55 +02:00
|
|
|
|
2014-07-11 22:12:11 +02:00
|
|
|
opt = 1;
|
2015-04-10 17:14:55 +02:00
|
|
|
if (setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &opt, sizeof(opt)) < 0) {
|
|
|
|
cw_log(LOG_ERR, "Can't set broadcast sockopt");
|
2014-07-11 22:12:11 +02:00
|
|
|
}
|
2015-04-10 17:14:55 +02:00
|
|
|
sock_set_recvtimeout(sockfd, 1);
|
|
|
|
sock_set_dontfrag(sockfd, 0);
|
2014-07-11 22:12:11 +02:00
|
|
|
|
2015-04-10 17:14:55 +02:00
|
|
|
sock_copyaddr(&conn->addr, res->ai_addr);
|
2016-03-31 07:49:06 +02:00
|
|
|
|
2018-03-30 19:45:27 +02:00
|
|
|
|
|
|
|
if (bindaddr) {
|
2018-03-12 18:01:40 +01:00
|
|
|
int brc;
|
2018-03-30 19:45:27 +02:00
|
|
|
|
2016-03-31 07:49:06 +02:00
|
|
|
struct sockaddr bind_address;
|
2018-03-30 19:45:27 +02:00
|
|
|
sock_strtoaddr(bindaddr, &bind_address);
|
|
|
|
brc = bind(sockfd, &bind_address, sock_addrlen(&bind_address));
|
|
|
|
if (brc < 0) {
|
|
|
|
cw_log(LOG_ERR, "Can't bind to %s",
|
|
|
|
sock_addr2str(&bind_address, sock_buf));
|
2016-03-31 07:49:06 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2018-03-30 19:45:27 +02:00
|
|
|
|
2016-03-31 07:49:06 +02:00
|
|
|
|
2015-04-10 17:14:55 +02:00
|
|
|
conn->sock = sockfd;
|
2015-04-12 23:28:55 +02:00
|
|
|
conn->readfrom = conn_recvfrom_packet;
|
2014-07-11 22:12:11 +02:00
|
|
|
|
2018-03-30 19:45:27 +02:00
|
|
|
|
|
|
|
cw_dbg(DBG_INFO, "Discovery to %s",
|
|
|
|
sock_addr2str_p(&conn->addr, sock_buf));
|
2018-03-12 18:01:40 +01:00
|
|
|
|
2018-03-30 23:58:21 +02:00
|
|
|
run_discovery(conn, dis);
|
2015-03-23 07:48:27 +01:00
|
|
|
|
2018-03-30 19:45:27 +02:00
|
|
|
conn->readfrom = NULL;
|
2015-03-23 07:48:27 +01:00
|
|
|
close(sockfd);
|
2015-04-07 07:42:36 +02:00
|
|
|
|
2015-04-10 17:14:55 +02:00
|
|
|
|
2014-07-11 22:12:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
freeaddrinfo(res0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|