2013-05-01 14:52:55 +02:00
|
|
|
#include "wtp.h"
|
|
|
|
#include "capwap_dfa.h"
|
|
|
|
#include "wtp_dfa.h"
|
|
|
|
|
2016-03-02 15:13:56 +01:00
|
|
|
static int wtp_join_prefered_ac()
|
|
|
|
{
|
|
|
|
if (g_wtp.acdiscoveryrequest ||
|
|
|
|
g_wtp.acpreferedarray->count == 0)
|
|
|
|
/* goto discovery */
|
|
|
|
return -1;
|
2013-11-07 22:06:29 +01:00
|
|
|
|
2016-03-02 15:13:56 +01:00
|
|
|
while (g_wtp.acpreferedselected < g_wtp.acpreferedarray->count)
|
|
|
|
{
|
|
|
|
union sockaddr_capwap localaddr;
|
2016-03-24 07:09:29 +01:00
|
|
|
struct addr_capwap *peeraddr;
|
2013-09-16 22:10:37 +02:00
|
|
|
|
2016-03-02 15:13:56 +01:00
|
|
|
/* Found in configuration file the AC address */
|
|
|
|
peeraddr = capwap_array_get_item_pointer(g_wtp.acpreferedarray,
|
2016-03-24 07:09:29 +01:00
|
|
|
g_wtp.acpreferedselected);
|
|
|
|
|
2014-09-10 21:58:23 +02:00
|
|
|
|
2016-03-02 15:13:56 +01:00
|
|
|
/* Next AC */
|
|
|
|
g_wtp.acpreferedselected++;
|
2013-11-07 22:06:29 +01:00
|
|
|
|
2016-03-02 15:13:56 +01:00
|
|
|
/* restart and connect the control Socket */
|
|
|
|
capwap_close_sockets(&g_wtp.net);
|
|
|
|
if (capwap_bind_sockets(&g_wtp.net) < 0) {
|
|
|
|
capwap_logging_fatal("Cannot bind control address");
|
|
|
|
return -1;
|
|
|
|
}
|
2013-11-07 22:06:29 +01:00
|
|
|
|
2016-03-24 07:09:29 +01:00
|
|
|
if(!peeraddr->resolved) {
|
2016-03-25 12:41:43 +01:00
|
|
|
if (capwap_address_from_string(peeraddr->fqdn, &peeraddr->sockaddr)) {
|
|
|
|
if (!CAPWAP_GET_NETWORK_PORT(&peeraddr->sockaddr)) {
|
|
|
|
CAPWAP_SET_NETWORK_PORT(&peeraddr->sockaddr, CAPWAP_CONTROL_PORT);
|
2016-03-24 07:09:29 +01:00
|
|
|
}
|
|
|
|
peeraddr->resolved = 1;
|
|
|
|
} else {
|
|
|
|
capwap_logging_info("%s:%d Could not resolve application.acprefered.host %s", __FILE__, __LINE__, peeraddr->fqdn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-25 12:41:43 +01:00
|
|
|
if (capwap_connect_socket(&g_wtp.net, &peeraddr->sockaddr) < 0) {
|
2016-03-02 15:13:56 +01:00
|
|
|
capwap_logging_fatal("Cannot bind control address");
|
|
|
|
capwap_close_sockets(&g_wtp.net);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-05-01 14:52:55 +02:00
|
|
|
|
2016-03-02 15:13:56 +01:00
|
|
|
/* Retrieve local address */
|
|
|
|
if (capwap_getsockname(&g_wtp.net, &localaddr) < 0) {
|
|
|
|
capwap_logging_fatal("Cannot get local endpoint address");
|
|
|
|
capwap_close_sockets(&g_wtp.net);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-11-07 22:06:29 +01:00
|
|
|
|
2016-03-02 15:13:56 +01:00
|
|
|
/* */
|
2016-03-25 12:41:43 +01:00
|
|
|
capwap_crypt_setconnection(&g_wtp.dtls, g_wtp.net.socket, &localaddr, &peeraddr->sockaddr);
|
2014-09-10 21:58:23 +02:00
|
|
|
|
2016-03-02 15:13:56 +01:00
|
|
|
/* */
|
|
|
|
if (!g_wtp.enabledtls) {
|
|
|
|
wtp_send_join(); /* Bypass DTLS connection */
|
|
|
|
} else {
|
|
|
|
wtp_start_dtlssetup(); /* Create DTLS connection */
|
2013-11-07 22:06:29 +01:00
|
|
|
}
|
2016-03-02 15:13:56 +01:00
|
|
|
|
|
|
|
return 0;
|
2013-05-01 14:52:55 +02:00
|
|
|
}
|
|
|
|
|
2016-03-02 15:13:56 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* */
|
|
|
|
void wtp_dfa_state_idle(void) {
|
|
|
|
long discoveryinterval;
|
|
|
|
|
|
|
|
/* Remove teardown */
|
|
|
|
g_wtp.teardown = 0;
|
|
|
|
capwap_timeout_unsetall(g_wtp.timeout);
|
|
|
|
|
|
|
|
if (wtp_join_prefered_ac() == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (g_wtp.net.socket < 0)
|
|
|
|
if (capwap_bind_sockets(&g_wtp.net) < 0) {
|
|
|
|
capwap_logging_fatal("Cannot bind control address");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
2013-11-07 22:06:29 +01:00
|
|
|
/* Discovery AC */
|
|
|
|
g_wtp.acpreferedselected = 0;
|
2013-05-01 14:52:55 +02:00
|
|
|
|
|
|
|
/* Set discovery interval */
|
2014-03-02 19:31:27 +01:00
|
|
|
g_wtp.discoverycount = 0;
|
|
|
|
discoveryinterval = capwap_get_rand(g_wtp.discoveryinterval - WTP_MIN_DISCOVERY_INTERVAL) + WTP_MIN_DISCOVERY_INTERVAL;
|
2013-11-07 22:06:29 +01:00
|
|
|
|
2013-05-01 14:52:55 +02:00
|
|
|
/* Change state */
|
|
|
|
wtp_dfa_change_state(CAPWAP_DISCOVERY_STATE);
|
2013-11-07 22:06:29 +01:00
|
|
|
|
2013-05-01 14:52:55 +02:00
|
|
|
/* Wait before send Discovery Request */
|
2014-03-02 19:31:27 +01:00
|
|
|
capwap_timeout_set(g_wtp.timeout, g_wtp.idtimercontrol, discoveryinterval, wtp_dfa_state_discovery_timeout, NULL, NULL);
|
2013-05-01 14:52:55 +02:00
|
|
|
}
|