Initial commit
FossilOrigin-Name: c53d95729c009f8f80a7d63847cef7668ff73f8af0523ab65f7734696f85399c
This commit is contained in:
493
src/ac/conf.c
Normal file
493
src/ac/conf.c
Normal file
@ -0,0 +1,493 @@
|
||||
/*
|
||||
This file is part of actube.
|
||||
|
||||
libcapwap is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
libcapwap is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/utsname.h>
|
||||
|
||||
#include <confuse.h>
|
||||
|
||||
#include "capwap.h"
|
||||
#include "sock.h"
|
||||
|
||||
#include "conf.h"
|
||||
|
||||
#include "cw_log.h"
|
||||
#include "cw_util.h"
|
||||
|
||||
char * conf_acname = NULL;
|
||||
int conf_acname_len = 0;
|
||||
|
||||
long conf_max_wtps = CONF_DEFAULT_MAXWTPS;
|
||||
char * conf_logfilename=CONF_DEFAULT_LOGFILENAME;
|
||||
struct sockaddr_storage * conf_salist=NULL;
|
||||
|
||||
char ** conf_listen_addrs;
|
||||
int conf_listen_addrs_len=0;
|
||||
|
||||
|
||||
char ** conf_mcast_groups=0;
|
||||
int conf_mcast_groups_len=0;
|
||||
|
||||
char ** conf_bcast_addrs=0;
|
||||
int conf_bcast_addrs_len;
|
||||
|
||||
|
||||
struct sockaddr_storage * conf_bsalist=NULL;
|
||||
|
||||
int conf_salist_len=0;
|
||||
int conf_bsalist_len=0;
|
||||
|
||||
struct sockaddr * conf_ac_ips;
|
||||
int conf_ac_ips_len;
|
||||
|
||||
char * conf_sslcertfilename=NULL;
|
||||
char * conf_sslkeyfilename=NULL;
|
||||
char * conf_dtls_psk=NULL;
|
||||
|
||||
char * conf_ac_hardware_version=NULL;
|
||||
char * conf_ac_software_version=NULL;
|
||||
int conf_security=0;
|
||||
long conf_vendor_id=CONF_DEFAULT_VENDOR_ID;
|
||||
|
||||
char * conf_hardware_version;
|
||||
char * conf_software_version;
|
||||
|
||||
int conf_use_loopback = 0;
|
||||
|
||||
long conf_debug_level=-1;
|
||||
|
||||
char * conf_db_file =0;
|
||||
|
||||
int conf_ipv4=1;
|
||||
#ifdef WITH_IPV6
|
||||
int conf_ipv6=1;
|
||||
#endif
|
||||
|
||||
|
||||
char * conf_control_port=0;
|
||||
cfg_bool_t conf_ignore_wtp_source_port = cfg_false;
|
||||
|
||||
static int init_acname()
|
||||
{
|
||||
if (conf_acname == NULL){
|
||||
conf_acname=CONF_DEFAULT_ACNAME;
|
||||
}
|
||||
conf_acname_len=strlen(conf_acname);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int init_dtls()
|
||||
{
|
||||
if (conf_dtls_psk!=NULL){
|
||||
conf_security=CWACSECURITY_FLAGS_S;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int init_vendor_id()
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int init_version()
|
||||
{
|
||||
if (!conf_hardware_version)
|
||||
{
|
||||
struct utsname u;
|
||||
int rc = uname(&u);
|
||||
if (rc<0)
|
||||
conf_hardware_version=CONF_DEFAULT_HARDWARE_VERSION;
|
||||
else{
|
||||
char str[265];
|
||||
sprintf(str,"%s / %s %s",u.machine,u.sysname,u.release);
|
||||
conf_hardware_version=strdup(str);
|
||||
}
|
||||
}
|
||||
if (!conf_software_version)
|
||||
conf_software_version=CONF_DEFAULT_SOFTWARE_VERSION;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int init_control_port()
|
||||
{
|
||||
if (conf_control_port != 0)
|
||||
return 1;
|
||||
|
||||
char str[30];
|
||||
sprintf(str,"%d",CONF_DEFAULT_CONTROL_PORT);
|
||||
conf_control_port=(char*)cw_setstr((uint8_t**)&conf_control_port,(uint8_t*)str,strlen(str));
|
||||
return 1;
|
||||
}
|
||||
|
||||
#include <ifaddrs.h>
|
||||
|
||||
static int init_listen_addrs()
|
||||
{
|
||||
if (conf_listen_addrs!=0)
|
||||
return 1;
|
||||
|
||||
struct ifaddrs * ifap,*ifa;
|
||||
|
||||
int rc = getifaddrs(&ifap);
|
||||
if (rc==-1)
|
||||
return 0;
|
||||
|
||||
/* count the addresses */
|
||||
int ctr=0;
|
||||
for (ifa=ifap; ifa!=0; ifa=ifa->ifa_next){
|
||||
if (ifa->ifa_addr->sa_family==AF_INET && conf_ipv4)
|
||||
ctr++;
|
||||
#ifdef WITH_IPV6
|
||||
if (ifa->ifa_addr->sa_family==AF_INET6 && conf_ipv6)
|
||||
ctr++;
|
||||
#endif
|
||||
}
|
||||
|
||||
conf_listen_addrs = malloc(sizeof(char*)*ctr);
|
||||
if (!conf_listen_addrs){
|
||||
rc=0;
|
||||
goto errX;
|
||||
}
|
||||
memset(conf_listen_addrs,0,sizeof(char*)*ctr);
|
||||
|
||||
|
||||
ctr=0;
|
||||
/* get the addresses */
|
||||
for (ifa=ifap; ifa!=0; ifa=ifa->ifa_next){
|
||||
char str[100];
|
||||
|
||||
if(!conf_use_loopback){
|
||||
if ((ifa->ifa_flags & IFF_LOOPBACK)){
|
||||
continue;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (ifa->ifa_addr->sa_family==AF_INET && conf_ipv4){
|
||||
sock_addrtostr(ifa->ifa_addr,str,100);
|
||||
*strchr(str,':')=0;
|
||||
conf_listen_addrs[ctr]=(char*)cw_setstr((uint8_t**)&conf_listen_addrs[ctr],(uint8_t*)str,strlen(str));
|
||||
if (conf_listen_addrs[ctr])
|
||||
ctr++;
|
||||
|
||||
}
|
||||
#ifdef WITH_IPV6
|
||||
if (ifa->ifa_addr->sa_family==AF_INET6 && conf_ipv6){
|
||||
sock_addrtostr(ifa->ifa_addr,str,100);
|
||||
if (strncmp(str,"fe80:",5)==0){
|
||||
strcat(str,"%");
|
||||
strcat(str,ifa->ifa_name);
|
||||
}
|
||||
conf_listen_addrs[ctr]=(char*)cw_setstr((uint8_t**)&conf_listen_addrs[ctr],(uint8_t*)str,strlen(str));
|
||||
if (conf_listen_addrs[ctr])
|
||||
ctr++;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
conf_listen_addrs_len=ctr;
|
||||
rc=1;
|
||||
errX:
|
||||
freeifaddrs(ifap);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
static char * conf_default_mcast_groups_ipv4[] = {
|
||||
"224.0.1.140",
|
||||
};
|
||||
|
||||
|
||||
|
||||
#ifdef WITH_IPV6
|
||||
static char * conf_default_mcast_groups_ipv6[] = {
|
||||
/* "ff01:0:0:0:0:0:0:18c",
|
||||
"ff02:0:0:0:0:0:0:18c%em0",
|
||||
"ff03:0:0:0:0:0:0:18c",
|
||||
"ff04:0:0:0:0:0:0:18c",
|
||||
"ff05:0:0:0:0:0:0:18c",
|
||||
"ff06:0:0:0:0:0:0:18c"
|
||||
*/
|
||||
};
|
||||
#endif
|
||||
|
||||
//#include "avltree"
|
||||
#include "stravltree.h"
|
||||
|
||||
|
||||
|
||||
static int add_bcast_addr(void *priv, void * addr)
|
||||
{
|
||||
char *s = (char*)addr;
|
||||
conf_bcast_addrs[conf_bcast_addrs_len]=strdup(s);
|
||||
if (conf_bcast_addrs[conf_bcast_addrs_len]!=0)
|
||||
conf_bcast_addrs_len++;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialize broadcast addresses (ipv4 only)
|
||||
*/
|
||||
int init_bcast_addrs()
|
||||
{
|
||||
if (conf_bcast_addrs)
|
||||
return 1;
|
||||
|
||||
if (!conf_ipv4)
|
||||
return 1;
|
||||
|
||||
struct avltree *t = stravltree_create();
|
||||
if (!t)
|
||||
return 0;
|
||||
|
||||
/* add the default broadast address */
|
||||
stravltree_add(t,"255.255.255.255");
|
||||
|
||||
|
||||
/* add all other local broadcast addresses */
|
||||
struct ifaddrs * ifa0,*ifa;
|
||||
int rc = getifaddrs(&ifa0);
|
||||
if (rc==-1)
|
||||
return 0;
|
||||
|
||||
for (ifa=ifa0; ifa!=0; ifa=ifa->ifa_next){
|
||||
struct sockaddr * sa;
|
||||
if (!(ifa->ifa_flags & IFF_BROADCAST))
|
||||
continue;
|
||||
|
||||
|
||||
if(!conf_use_loopback){
|
||||
if ((ifa->ifa_flags & IFF_LOOPBACK))
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
sa = ifa->ifa_addr;
|
||||
if(sa->sa_family != AF_INET)
|
||||
continue;
|
||||
|
||||
char str[100];
|
||||
if (ifa->ifa_broadaddr){
|
||||
sock_addrtostr(ifa->ifa_broadaddr,str,100);
|
||||
*strchr(str,':')=0;
|
||||
stravltree_add(t,str);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
conf_bcast_addrs=malloc(t->count*sizeof(char*));
|
||||
|
||||
stravltree_foreach(t,add_bcast_addr,0,1);
|
||||
stravltree_destroy(t);
|
||||
|
||||
freeifaddrs(ifa0);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int init_mcast_groups()
|
||||
{
|
||||
if (conf_mcast_groups)
|
||||
return 1;
|
||||
|
||||
int n = 0;
|
||||
int n4=0,n6=0;
|
||||
if (conf_ipv4){
|
||||
n4=sizeof(conf_default_mcast_groups_ipv4)/sizeof(char*);
|
||||
}
|
||||
|
||||
#ifdef WITH_IPV6
|
||||
if (conf_ipv6){
|
||||
n6=sizeof(conf_default_mcast_groups_ipv6)/sizeof(char*);
|
||||
}
|
||||
#endif
|
||||
n=n4+n6;
|
||||
if (n==0)
|
||||
return 1;
|
||||
|
||||
conf_mcast_groups=malloc(sizeof(char*)*n);
|
||||
if (!conf_mcast_groups)
|
||||
return 0;
|
||||
memset(conf_mcast_groups,0,n*sizeof(char*));
|
||||
|
||||
int ctr=0;
|
||||
int i;
|
||||
for(i=0; i<n4; i++){
|
||||
uint8_t *g = (uint8_t*)conf_default_mcast_groups_ipv4[i];
|
||||
conf_mcast_groups[ctr]=(char*)cw_setstr((uint8_t**)&conf_mcast_groups[ctr],g,strlen((char*)g));
|
||||
if (conf_mcast_groups[ctr])
|
||||
ctr++;
|
||||
}
|
||||
|
||||
for(i=0; i<n6; i++){
|
||||
uint8_t *g = (uint8_t*)conf_default_mcast_groups_ipv6[i];
|
||||
conf_mcast_groups[ctr]=(char*)cw_setstr((uint8_t**)&conf_mcast_groups[ctr],g,strlen((char*)g));
|
||||
if (conf_mcast_groups[ctr])
|
||||
ctr++;
|
||||
}
|
||||
|
||||
|
||||
conf_mcast_groups_len=n;
|
||||
return 1;
|
||||
|
||||
}
|
||||
|
||||
static int conf_read_strings( cfg_t * cfg, char * name, char ***dst,int *len)
|
||||
{
|
||||
|
||||
int n,i;
|
||||
n = cfg_size(cfg, name);
|
||||
*len=n;
|
||||
if (n==0)
|
||||
return 1;
|
||||
|
||||
*dst = malloc(sizeof(char*)*n);
|
||||
if (!*dst)
|
||||
return 0;
|
||||
|
||||
for (i=0; i<n; i++) {
|
||||
char * str = cfg_getnstr(cfg,name,i);
|
||||
if (!((*dst)[i]=malloc(strlen(str)+1))) {
|
||||
*len=0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
strcpy((*dst)[i],str);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int read_config(const char * filename){
|
||||
int i,n;
|
||||
|
||||
cfg_opt_t opts[] = {
|
||||
CFG_STR_LIST("listen", "{}", CFGF_NONE),
|
||||
CFG_STR_LIST("mcast_groups", "{}", CFGF_NONE),
|
||||
CFG_STR_LIST("bcast_addrs", "{}", CFGF_NONE),
|
||||
CFG_STR_LIST("ac_ips","{}",CFGF_NONE),
|
||||
CFG_SIMPLE_STR("control_port",&conf_control_port),
|
||||
CFG_SIMPLE_INT("max_wtps",&conf_max_wtps),
|
||||
CFG_SIMPLE_INT("debug_level",&conf_debug_level),
|
||||
CFG_SIMPLE_INT("vendor_id",&conf_vendor_id),
|
||||
CFG_SIMPLE_STR("ac_name",&conf_acname),
|
||||
CFG_SIMPLE_STR("ssl_cert",&conf_sslcertfilename),
|
||||
CFG_SIMPLE_STR("ssl_key",&conf_sslkeyfilename),
|
||||
CFG_SIMPLE_STR("dtls_psk",&conf_dtls_psk),
|
||||
// CFG_SIMPLE_BOOL("ignore_wtp_source_port",&conf_ignore_wtp_source_port),
|
||||
CFG_SIMPLE_BOOL("ipv4",&conf_ipv4),
|
||||
CFG_SIMPLE_BOOL("ipv6",&conf_ipv6),
|
||||
|
||||
|
||||
CFG_SIMPLE_STR("db_file",conf_db_file),
|
||||
|
||||
CFG_END()
|
||||
};
|
||||
cfg_t *cfg;
|
||||
cfg = cfg_init(opts, 0);
|
||||
|
||||
cfg_parse(cfg, filename);
|
||||
|
||||
/* read the listen addresses */
|
||||
conf_read_strings(cfg,"listen",&conf_listen_addrs,&conf_listen_addrs_len);
|
||||
|
||||
/* read multi cast groups */
|
||||
conf_read_strings(cfg,"mcast_groups",&conf_mcast_groups,&conf_mcast_groups_len);
|
||||
|
||||
/* read ipv4 broadcast addresses */
|
||||
conf_read_strings(cfg,"bcast_addrs",&conf_bcast_addrs,&conf_bcast_addrs_len);
|
||||
|
||||
|
||||
/*
|
||||
n = cfg_size(cfg, "listen");
|
||||
if (! (conf_salist = malloc(sizeof (struct sockaddr_storage)*n)) )
|
||||
return 0;
|
||||
|
||||
if (! (conf_listen_addrs=malloc(sizeof(char *)*n)))
|
||||
return 0;
|
||||
|
||||
memset(conf_listen_addrs,0,sizeof(char*)*n);
|
||||
|
||||
conf_salist_len=n;
|
||||
for (i=0; i<n; i++) {
|
||||
char * str = cfg_getnstr(cfg,"listen",i);
|
||||
if (!(conf_listen_addrs[i]=malloc(strlen(str)+1)))
|
||||
return 0;
|
||||
|
||||
strcpy(conf_listen_addrs[i],str);
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* read ac_ips */
|
||||
n = cfg_size(cfg, "ac_ips");
|
||||
if (! (conf_ac_ips = malloc(sizeof (struct sockaddr)*n)) )
|
||||
return 0;
|
||||
|
||||
conf_ac_ips_len=n;
|
||||
for (i=0; i<n; i++) {
|
||||
struct sockaddr sa;
|
||||
char * str = cfg_getnstr(cfg,"ac_ips",i);
|
||||
if (sock_strtoaddr ( cfg_getnstr(cfg, "ac_ips", i),&sa))
|
||||
conf_ac_ips[i]=sa;
|
||||
else{
|
||||
perror(str);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
cfg_free(cfg);
|
||||
|
||||
if (!init_acname() )
|
||||
return 0;
|
||||
|
||||
if (!init_version())
|
||||
return 0;
|
||||
|
||||
if (!init_vendor_id())
|
||||
return 0;
|
||||
|
||||
if (!init_dtls())
|
||||
return 0;
|
||||
|
||||
|
||||
if (!init_control_port())
|
||||
return 0;
|
||||
|
||||
init_listen_addrs();
|
||||
init_mcast_groups();
|
||||
init_bcast_addrs();
|
||||
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void free_config()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user