freewtp/src/wtp/wtp_dfa_idle.c

102 lines
2.6 KiB
C
Raw Normal View History

2013-05-01 14:52:55 +02:00
#include "wtp.h"
#include "capwap_dfa.h"
#include "wtp_dfa.h"
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
while (g_wtp.acpreferedselected < g_wtp.acpreferedarray->count)
{
union sockaddr_capwap localaddr;
struct addr_capwap *peeraddr;
2013-09-16 22:10:37 +02:00
/* Found in configuration file the AC address */
peeraddr = capwap_array_get_item_pointer(g_wtp.acpreferedarray,
g_wtp.acpreferedselected);
/* Next AC */
g_wtp.acpreferedselected++;
2013-11-07 22:06:29 +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
if(!peeraddr->resolved) {
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);
}
peeraddr->resolved = 1;
} else {
capwap_logging_info("%s:%d Could not resolve application.acprefered.host %s", __FILE__, __LINE__, peeraddr->fqdn);
}
}
if (capwap_connect_socket(&g_wtp.net, &peeraddr->sockaddr) < 0) {
capwap_logging_fatal("Cannot bind control address");
capwap_close_sockets(&g_wtp.net);
return -1;
}
2013-05-01 14:52:55 +02: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
/* */
capwap_crypt_setconnection(&g_wtp.dtls, g_wtp.net.socket, &localaddr, &peeraddr->sockaddr);
/* */
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
}
return 0;
2013-05-01 14:52:55 +02: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 */
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 */
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
}