From ea179aaf38048c0994def7c3de171bb8395cd447 Mon Sep 17 00:00:00 2001 From: "7u83@mail.ru" <7u83@mail.ru@noemail.net> Date: Sun, 18 Oct 2015 08:14:33 +0000 Subject: [PATCH] Added command line parsing, reformatted. FossilOrigin-Name: 2b4ff900c9ee7fd36afc54d46d3dc64a98dd7a09a3836d7e19763bba9918bb8c --- src/ac/ac_main.c | 269 +++++++++++++++++++++++------------------------ 1 file changed, 131 insertions(+), 138 deletions(-) diff --git a/src/ac/ac_main.c b/src/ac/ac_main.c index 942d6c8a..3fc43ae6 100644 --- a/src/ac/ac_main.c +++ b/src/ac/ac_main.c @@ -16,22 +16,20 @@ */ +#include #include +#include #include #include -#include -#include -#include -#include -#include +//#include #include "actube.h" #include "wtplist.h" #include "capwap/dtls.h" #include "capwap/log.h" #include "capwap/dbg.h" -#include "conf.h" #include "capwap/sock.h" +#include "conf.h" #include "socklist.h" @@ -50,72 +48,75 @@ int ac_run(); -static void * alive_thread(void *data) +static void *alive_thread(void *data) { /* Ping the database every 5 seconds */ - while(1){ + while (1) { sleep(5); db_ping(); } } -int main (int argc, const char * argv[]) +#include +static int parse_args(int argc, char *argv[]) { - int rc =0; + int getopt_ret, option_index; -/* -int n; -cw_itemdefheap_t h=cw_itemdefheap_create(); -n=cw_itemdefheap_register(h,capwap_itemdefs); -printf("Registered: %d\n",n); - -const cw_itemdef_t * id = cw_itemdef_get(h,"wtp_name",NULL); - -if (id) { - printf("Found %s is of type %s\n",id->id,id->type->name); -} -else{ - printf("Not found, why?\n"); + static struct option long_options[] = { + {"version", optional_argument, 0, 'v'}, + {0, 0, 0, 0} + }; + int o; + while ((o = getopt_long(argc, argv, "v", long_options, &option_index)) != -1) { + switch (o) { + case 0: + break; + case 'v': + printf("AC-Tube 0.01, %s\n", SYS_ARCH); + exit(0); + } + } + 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"); /* Show debug options if there are any set */ - if(cw_dbg_opt_level) - cw_log(LOG_INFO,"Debug Options: %08X",cw_dbg_opt_level); + if (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 */ - 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 - developers is turned on ;)*/ - DBGX("Attention! %s","DBGX is ON!"); + developers is turned on ;) */ + DBGX("Attention! %s", "DBGX is ON!"); /* Initialize the database */ - if (!db_init()) + if (!db_init()) goto errX; + /* Start the database */ if (!db_start()) goto errX; db_ping(); - - /* Start a database "pinger thread", which inserts every xx seconds a timestamp into the DB */ pthread_t alth; - pthread_create (&alth, NULL, alive_thread, NULL); + pthread_create(&alth, NULL, alive_thread, NULL); /* Init DTLS library */ dtls_init(); @@ -123,20 +124,19 @@ exit(0); int regn; /* Load CAPWAP base protocol */ - if (conf_capwap_mode==CW_MODE_CIPWAP){ - cw_dbg(DBG_INFO,"Loading CIPWAP Actions ..."); + if (conf_capwap_mode == CW_MODE_CIPWAP) { + cw_dbg(DBG_INFO, "Loading CIPWAP Actions ..."); regn = cw_register_actions_cipwap_ac(&capwap_actions); - } - else { - cw_dbg(DBG_INFO,"Loading standard CAPWAP Actions ..."); + } else { + cw_dbg(DBG_INFO, "Loading standard CAPWAP Actions ..."); regn = cw_register_actions_capwap_ac(&capwap_actions); } /* Load bindings */ - cw_dbg(DBG_INFO,"Loading 802.11 Bindings ..."); -// regn += cw_register_actions_capwap_80211_ac(&capwap_actions); + cw_dbg(DBG_INFO, "Loading 802.11 Bindings ..."); +// 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(); -errX: + errX: /* XXX There is more cleanup to do */ wtplist_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_LWAPP 1 @@ -175,8 +175,8 @@ void process_ctrl_packet(int index, struct sockaddr * addr, uint8_t * buffer, i int ac_run() { - if (!conf_listen_addrs_len){ - cw_log(LOG_ERR,"Fatal error: No listen addresses found."); + if (!conf_listen_addrs_len) { + cw_log(LOG_ERR, "Fatal error: No listen addresses found."); return 1; } @@ -188,38 +188,44 @@ int ac_run() * good unicast reply socket */ int i; - for(i=0; imax) - max=socklist[i].sockfd; + for (i = 0; i < socklist_len; i++) { + FD_SET(socklist[i].sockfd, &fset); + if (socklist[i].sockfd > max) + max = socklist[i].sockfd; } /* wait for an event */ int n; - while((n=select(max+1, &fset, NULL, NULL, NULL)) < 0) { - if (errno != EINTR) + while ((n = select(max + 1, &fset, NULL, NULL, NULL)) < 0) { + if (errno != EINTR) return n; - } + } /* process the received packet */ - for( i=0; i