Added command line parsing, reformatted.

FossilOrigin-Name: 2b4ff900c9ee7fd36afc54d46d3dc64a98dd7a09a3836d7e19763bba9918bb8c
This commit is contained in:
7u83@mail.ru 2015-10-18 08:14:33 +00:00
parent 5c36230620
commit ea179aaf38
1 changed files with 131 additions and 138 deletions

View File

@ -16,22 +16,20 @@
*/ */
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h>
#include <errno.h> #include <errno.h>
#include <stdint.h> #include <stdint.h>
#include <netinet/in.h> //#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netdb.h>
#include "actube.h" #include "actube.h"
#include "wtplist.h" #include "wtplist.h"
#include "capwap/dtls.h" #include "capwap/dtls.h"
#include "capwap/log.h" #include "capwap/log.h"
#include "capwap/dbg.h" #include "capwap/dbg.h"
#include "conf.h"
#include "capwap/sock.h" #include "capwap/sock.h"
#include "conf.h"
#include "socklist.h" #include "socklist.h"
@ -50,72 +48,75 @@
int ac_run(); int ac_run();
static void * alive_thread(void *data) static void *alive_thread(void *data)
{ {
/* Ping the database every 5 seconds */ /* Ping the database every 5 seconds */
while(1){ while (1) {
sleep(5); sleep(5);
db_ping(); db_ping();
} }
} }
int main (int argc, const char * argv[]) #include <getopt.h>
static int parse_args(int argc, char *argv[])
{ {
int rc =0; int getopt_ret, option_index;
/* static struct option long_options[] = {
int n; {"version", optional_argument, 0, 'v'},
cw_itemdefheap_t h=cw_itemdefheap_create(); {0, 0, 0, 0}
n=cw_itemdefheap_register(h,capwap_itemdefs); };
printf("Registered: %d\n",n); int o;
while ((o = getopt_long(argc, argv, "v", long_options, &option_index)) != -1) {
const cw_itemdef_t * id = cw_itemdef_get(h,"wtp_name",NULL); switch (o) {
case 0:
if (id) { break;
printf("Found %s is of type %s\n",id->id,id->type->name); case 'v':
} printf("AC-Tube 0.01, %s\n", SYS_ARCH);
else{ exit(0);
printf("Not found, why?\n"); }
}
return 0;
} }
mavl_destroy(h); int main(int argc, char *argv[])
{
int rc = 0;
/* parse arguments */
parse_args(argc, argv);
exit(0); cw_log_name = "AC-Tube";
*/
cw_log_name="AC-Tube";
read_config("ac.conf"); read_config("ac.conf");
/* Show debug options if there are any set */ /* Show debug options if there are any set */
if(cw_dbg_opt_level) if (cw_dbg_opt_level)
cw_log(LOG_INFO,"Debug Options: %08X",cw_dbg_opt_level); cw_log(LOG_INFO, "Debug Options: %08X", cw_dbg_opt_level);
/* XXX Hard coded debug settigns, set it by config in the future */ /* XXX Hard coded debug settigns, set it by config in the future */
cw_dbg_opt_display=DBG_DISP_ASC_DMP | DBG_DISP_COLORS; cw_dbg_opt_display = DBG_DISP_ASC_DMP | DBG_DISP_COLORS;
/* Warn, if the "secret" debugging feature for /* Warn, if the "secret" debugging feature for
developers is turned on ;)*/ developers is turned on ;) */
DBGX("Attention! %s","DBGX is ON!"); DBGX("Attention! %s", "DBGX is ON!");
/* Initialize the database */ /* Initialize the database */
if (!db_init()) if (!db_init())
goto errX; goto errX;
/* Start the database */
if (!db_start()) if (!db_start())
goto errX; goto errX;
db_ping(); db_ping();
/* Start a database "pinger thread", which inserts /* Start a database "pinger thread", which inserts
every xx seconds a timestamp into the DB */ every xx seconds a timestamp into the DB */
pthread_t alth; pthread_t alth;
pthread_create (&alth, NULL, alive_thread, NULL); pthread_create(&alth, NULL, alive_thread, NULL);
/* Init DTLS library */ /* Init DTLS library */
dtls_init(); dtls_init();
@ -123,20 +124,19 @@ exit(0);
int regn; int regn;
/* Load CAPWAP base protocol */ /* Load CAPWAP base protocol */
if (conf_capwap_mode==CW_MODE_CIPWAP){ if (conf_capwap_mode == CW_MODE_CIPWAP) {
cw_dbg(DBG_INFO,"Loading CIPWAP Actions ..."); cw_dbg(DBG_INFO, "Loading CIPWAP Actions ...");
regn = cw_register_actions_cipwap_ac(&capwap_actions); regn = cw_register_actions_cipwap_ac(&capwap_actions);
} } else {
else { cw_dbg(DBG_INFO, "Loading standard CAPWAP Actions ...");
cw_dbg(DBG_INFO,"Loading standard CAPWAP Actions ...");
regn = cw_register_actions_capwap_ac(&capwap_actions); regn = cw_register_actions_capwap_ac(&capwap_actions);
} }
/* Load bindings */ /* Load bindings */
cw_dbg(DBG_INFO,"Loading 802.11 Bindings ..."); cw_dbg(DBG_INFO, "Loading 802.11 Bindings ...");
// regn += cw_register_actions_capwap_80211_ac(&capwap_actions); // regn += cw_register_actions_capwap_80211_ac(&capwap_actions);
cw_dbg(DBG_INFO,"Registered %d protocol actions and strings.",regn); cw_dbg(DBG_INFO, "Registered %d protocol actions and strings.", regn);
@ -149,10 +149,10 @@ exit(0);
cw_log(LOG_INFO,"Starting AC-Tube, Name=%s, ID=%s",conf_acname,conf_acid); cw_log(LOG_INFO, "Starting AC-Tube, Name=%s, ID=%s", conf_acname, conf_acid);
rc = ac_run(); rc = ac_run();
errX: errX:
/* XXX There is more cleanup to do */ /* XXX There is more cleanup to do */
wtplist_destroy(); wtplist_destroy();
socklist_destroy(); socklist_destroy();
@ -165,7 +165,7 @@ errX:
void process_ctrl_packet(int index, struct sockaddr * addr, uint8_t * buffer, int len); void process_ctrl_packet(int index, struct sockaddr *addr, uint8_t * buffer, int len);
#define AC_PROTO_CAPWAP 0 #define AC_PROTO_CAPWAP 0
#define AC_PROTO_LWAPP 1 #define AC_PROTO_LWAPP 1
@ -175,8 +175,8 @@ void process_ctrl_packet(int index, struct sockaddr * addr, uint8_t * buffer, i
int ac_run() int ac_run()
{ {
if (!conf_listen_addrs_len){ if (!conf_listen_addrs_len) {
cw_log(LOG_ERR,"Fatal error: No listen addresses found."); cw_log(LOG_ERR, "Fatal error: No listen addresses found.");
return 1; return 1;
} }
@ -188,38 +188,44 @@ int ac_run()
* good unicast reply socket */ * good unicast reply socket */
int i; int i;
for(i=0; i<conf_listen_addrs_len; i++){ for (i = 0; i < conf_listen_addrs_len; i++) {
socklist_add_unicast(conf_listen_addrs[i],conf_control_port,AC_PROTO_CAPWAP); socklist_add_unicast(conf_listen_addrs[i], conf_control_port,
AC_PROTO_CAPWAP);
#ifdef WITH_LWAPP #ifdef WITH_LWAPP
if (conf_lwapp) if (conf_lwapp)
socklist_add_unicast(conf_listen_addrs[i],conf_lw_control_port,AC_PROTO_LWAPP); socklist_add_unicast(conf_listen_addrs[i], conf_lw_control_port,
#endif AC_PROTO_LWAPP);
#endif
} }
if (socklist_len==0){ if (socklist_len == 0) {
cw_log(LOG_ERR,"Fatal error: Could not setup any listen socket"); cw_log(LOG_ERR, "Fatal error: Could not setup any listen socket");
return 1; return 1;
} }
/* create multicast sockets */ /* create multicast sockets */
for (i=0; i<conf_mcast_groups_len;i++){ for (i = 0; i < conf_mcast_groups_len; i++) {
socklist_add_multicast(conf_mcast_groups[i],conf_control_port,AC_PROTO_CAPWAP); socklist_add_multicast(conf_mcast_groups[i], conf_control_port,
AC_PROTO_CAPWAP);
#ifdef WITH_LWAPP #ifdef WITH_LWAPP
if (conf_lwapp) if (conf_lwapp)
socklist_add_multicast(conf_mcast_groups[i],conf_lw_control_port,AC_PROTO_LWAPP); socklist_add_multicast(conf_mcast_groups[i], conf_lw_control_port,
#endif AC_PROTO_LWAPP);
#endif
} }
/* broadcast sockety ipv4 only */ /* broadcast sockety ipv4 only */
for (i=0; i<conf_bcast_addrs_len;i++){ for (i = 0; i < conf_bcast_addrs_len; i++) {
socklist_add_broadcast(conf_bcast_addrs[i],conf_control_port,AC_PROTO_CAPWAP); socklist_add_broadcast(conf_bcast_addrs[i], conf_control_port,
AC_PROTO_CAPWAP);
#ifdef WITH_LWAPP #ifdef WITH_LWAPP
// printf("Adding %d\n",socklist_len); // printf("Adding %d\n",socklist_len);
if (conf_lwapp) if (conf_lwapp)
socklist_add_broadcast(conf_bcast_addrs[i],conf_lw_control_port,AC_PROTO_LWAPP); socklist_add_broadcast(conf_bcast_addrs[i], conf_lw_control_port,
// printf ("SI %d, PROTO: %d\n",socklist_len-1,socklist[socklist_len-1].ac_proto); AC_PROTO_LWAPP);
#endif // printf ("SI %d, PROTO: %d\n",socklist_len-1,socklist[socklist_len-1].ac_proto);
#endif
} }
@ -227,145 +233,148 @@ int ac_run()
//get_acinfo(); //get_acinfo();
while(1){ while (1) {
/* prepare fdset */ /* prepare fdset */
fd_set fset; fd_set fset;
int max = 0; int max = 0;
FD_ZERO(&fset); FD_ZERO(&fset);
for (i=0; i<socklist_len; i++){ for (i = 0; i < socklist_len; i++) {
FD_SET(socklist[i].sockfd,&fset); FD_SET(socklist[i].sockfd, &fset);
if (socklist[i].sockfd>max) if (socklist[i].sockfd > max)
max=socklist[i].sockfd; max = socklist[i].sockfd;
} }
/* wait for an event */ /* wait for an event */
int n; int n;
while((n=select(max+1, &fset, NULL, NULL, NULL)) < 0) { while ((n = select(max + 1, &fset, NULL, NULL, NULL)) < 0) {
if (errno != EINTR) if (errno != EINTR)
return n; return n;
} }
/* process the received packet */ /* process the received packet */
for( i=0; i<socklist_len; i++){ for (i = 0; i < socklist_len; i++) {
if (!FD_ISSET(socklist[i].sockfd,&fset)) if (!FD_ISSET(socklist[i].sockfd, &fset))
continue; continue;
struct sockaddr_storage srcaddr; struct sockaddr_storage srcaddr;
socklen_t sockaddrlen; socklen_t sockaddrlen;
memset(&srcaddr,0,sizeof(struct sockaddr_storage)); memset(&srcaddr, 0, sizeof(struct sockaddr_storage));
sockaddrlen = sizeof(struct sockaddr_storage); sockaddrlen = sizeof(struct sockaddr_storage);
uint8_t buffer[4096]; uint8_t buffer[4096];
int len = sock_receive(socklist[i].sockfd, int len = sock_receive(socklist[i].sockfd,
buffer, sizeof(buffer), buffer, sizeof(buffer),
0, 0,
(struct sockaddr*)&srcaddr, &sockaddrlen); (struct sockaddr *) &srcaddr,
&sockaddrlen);
process_ctrl_packet(i, (struct sockaddr*)&srcaddr,buffer,len); process_ctrl_packet(i, (struct sockaddr *) &srcaddr, buffer, len);
} }
} }
/* close and free all sockts */ /* close and free all sockts */
for(i=0; i<socklist_len; i++){ for (i = 0; i < socklist_len; i++) {
// close(socklist[i]); // close(socklist[i]);
} }
free(socklist); free(socklist);
return 0; return 0;
} }
void process_cw_ctrl_packet(int index,struct sockaddr * addr, uint8_t * buffer, int len) void process_cw_ctrl_packet(int index, struct sockaddr *addr, uint8_t * buffer, int len)
{ {
/* first of all check preamble */ /* first of all check preamble */
int preamble = cw_get_hdr_preamble(buffer); int preamble = cw_get_hdr_preamble(buffer);
if (preamble != CAPWAP_PACKET_PREAMBLE && preamble != CAPWAP_DTLS_PACKET_PREAMBLE){ if (preamble != CAPWAP_PACKET_PREAMBLE && preamble != CAPWAP_DTLS_PACKET_PREAMBLE) {
cw_dbg(DBG_PKT_ERR,"Discarding packet from %s, wrong preamble, preamble = 0x%01X",sock_addr2str(addr),preamble); cw_dbg(DBG_PKT_ERR,
"Discarding packet from %s, wrong preamble, preamble = 0x%01X",
sock_addr2str(addr), preamble);
return; return;
} }
wtplist_lock(); wtplist_lock();
struct wtpman * wtpman = wtplist_get(addr); struct wtpman *wtpman = wtplist_get(addr);
if (!wtpman){ if (!wtpman) {
wtpman = wtpman_create(index,addr); wtpman = wtpman_create(index, addr);
if (!wtpman ){ if (!wtpman) {
cw_log(LOG_ERR,"Error creating wtpman: %s",strerror(errno)); cw_log(LOG_ERR, "Error creating wtpman: %s", strerror(errno));
wtplist_unlock(); wtplist_unlock();
return; return;
} }
if (!wtplist_add(wtpman)){
cw_log(LOG_ERR,"Error adding wtpman: Too many wtp connections"); if (!wtplist_add(wtpman)) {
cw_log(LOG_ERR, "Error adding wtpman: Too many wtp connections");
wtpman_destroy(wtpman); wtpman_destroy(wtpman);
wtplist_unlock(); wtplist_unlock();
return; return;
}; };
wtpman_start(wtpman,preamble & 0xf); wtpman_start(wtpman, preamble & 0xf);
} }
wtpman_addpacket(wtpman,buffer,len); wtpman_addpacket(wtpman, buffer, len);
wtplist_unlock(); wtplist_unlock();
} }
void process_lw_ctrl_packet(int index,struct sockaddr * addr, uint8_t * buffer, int len) void process_lw_ctrl_packet(int index, struct sockaddr *addr, uint8_t * buffer, int len)
{ {
//int sock = socklist[index].reply_sockfd; //int sock = socklist[index].reply_sockfd;
uint8_t * m = buffer+6; uint8_t *m = buffer + 6;
uint32_t val = ntohl(*((uint32_t*)(m))); uint32_t val = ntohl(*((uint32_t *) (m)));
printf ("VAL: %08X\n",val); printf("VAL: %08X\n", val);
/* first of all check preamble */ /* first of all check preamble */
int version = LWTH_GET_VERSION(m); int version = LWTH_GET_VERSION(m);
if (version != LW_VERSION){ if (version != LW_VERSION) {
// cw_log_debug1("Discarding LWAPP packet, wrong verson"); // cw_log_debug1("Discarding LWAPP packet, wrong verson");
return; return;
} }
int l = LWTH_GET_LENGTH(m); int l = LWTH_GET_LENGTH(m);
printf ("LEN = %d\n",l); printf("LEN = %d\n", l);
if (l+12 != len){ if (l + 12 != len) {
// cw_log_debug1("Discarding LWAPP packet, wrong length"); // cw_log_debug1("Discarding LWAPP packet, wrong length");
return; return;
} }
wtplist_lock(); wtplist_lock();
struct wtpman * wtpman = wtplist_get(addr); struct wtpman *wtpman = wtplist_get(addr);
if (!wtpman){ if (!wtpman) {
wtpman = wtpman_create(index,addr); wtpman = wtpman_create(index, addr);
if (!wtpman ){ if (!wtpman) {
cw_log(LOG_ERR,"Error creating wtpman: %s",strerror(errno)); cw_log(LOG_ERR, "Error creating wtpman: %s", strerror(errno));
wtplist_unlock(); wtplist_unlock();
return; return;
} }
if (!wtplist_add(wtpman)){
cw_log(LOG_ERR,"Error adding wtpman: Too many wtp connections"); if (!wtplist_add(wtpman)) {
cw_log(LOG_ERR, "Error adding wtpman: Too many wtp connections");
wtpman_destroy(wtpman); wtpman_destroy(wtpman);
wtplist_unlock(); wtplist_unlock();
return; return;
@ -373,7 +382,6 @@ void process_lw_ctrl_packet(int index,struct sockaddr * addr, uint8_t * buffer,
//wtpman_lw_start(wtpman); //wtpman_lw_start(wtpman);
} }
//wtpman_lw_addpacket(wtpman,buffer,len); //wtpman_lw_addpacket(wtpman,buffer,len);
wtplist_unlock(); wtplist_unlock();
} }
@ -382,29 +390,14 @@ void process_lw_ctrl_packet(int index,struct sockaddr * addr, uint8_t * buffer,
void process_ctrl_packet(int index, struct sockaddr *addr, uint8_t * buffer, int len)
void process_ctrl_packet(int index,struct sockaddr * addr, uint8_t * buffer, int len)
{ {
switch (socklist[index].ac_proto){ switch (socklist[index].ac_proto) {
case AC_PROTO_CAPWAP: case AC_PROTO_CAPWAP:
process_cw_ctrl_packet(index,addr,buffer,len); process_cw_ctrl_packet(index, addr, buffer, len);
return; return;
case AC_PROTO_LWAPP: case AC_PROTO_LWAPP:
process_lw_ctrl_packet(index,addr,buffer,len); process_lw_ctrl_packet(index, addr, buffer, len);
return; return;
} }
} }