[Listener] Refactor configuration code
This commit is contained in:
parent
7aa0b65f72
commit
a0be0f5bef
|
@ -59,13 +59,21 @@ enum {MODE_ACTIVE = 'a', MODE_PASSIVE = 'p', MODE_MIXED = 'm'} ;
|
|||
|
||||
|
||||
/* Function headers */
|
||||
void initialise_configuration(int argc, char **argv) ;
|
||||
void parse_config_file(int argc, char **argv) ;
|
||||
void parse_command_line(int argc, char **argv) ;
|
||||
void check_configuration(void) ;
|
||||
#ifdef DEBUG
|
||||
void print_configuration(void) ;
|
||||
#endif // DEBUG
|
||||
|
||||
#ifdef USE_PTHREAD
|
||||
void* keep_mode_monitor(char *iface) ;
|
||||
#endif // USE_PTHREAD
|
||||
int capture(void) ;
|
||||
void read_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet, int sockfd, struct sockaddr_in *server) ;
|
||||
void get_mac_addr(char *eth, unsigned char mac_bytes[6]) ;
|
||||
void print_usage(char *prog) ;
|
||||
void print_usage(void) ;
|
||||
|
||||
|
||||
/* Macros to allow switching option handling with the libconfuse
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
|
||||
|
||||
|
||||
char *program_name = NULL ;
|
||||
unsigned char mac[6] ; // AP MAC address
|
||||
|
||||
#ifdef USE_CONFIG_FILE
|
||||
|
@ -52,6 +53,57 @@ int main(int argc, char *argv[])
|
|||
pthread_t thread ; // Thread pour le repassage en mode monitor
|
||||
#endif // USE_PTHREAD
|
||||
|
||||
program_name = argv[0] ;
|
||||
initialise_configuration(argc, argv) ;
|
||||
|
||||
run = TRUE ;
|
||||
|
||||
/* Set up signal handlers */
|
||||
sigemptyset(&action.sa_mask) ;
|
||||
action.sa_handler = sigint_handler ;
|
||||
sigaction(SIGINT, &action, NULL) ;
|
||||
action.sa_handler = sigterm_handler ;
|
||||
sigaction(SIGTERM, &action, NULL) ;
|
||||
|
||||
#ifdef USE_PTHREAD
|
||||
/* Set up thread */
|
||||
if (GET_KEEP_MONITOR())
|
||||
pthread_create(&thread, NULL,
|
||||
(void *) &keep_mode_monitor, GET_WIFI_IFACE()) ;
|
||||
#endif // USE_PTHREAD
|
||||
|
||||
get_mac_addr(GET_WIFI_IFACE(), mac) ;
|
||||
mac_string = mac_bytes_to_string(mac) ;
|
||||
printf("My MAC address is: %s\n", mac_string) ;
|
||||
free(mac_string) ;
|
||||
|
||||
ret = capture() ; // Capture loop
|
||||
|
||||
#ifdef USE_CONFIG_FILE
|
||||
cfg_free(cfg) ; // Clean configuration
|
||||
#endif // USE_CONFIG_FILE
|
||||
|
||||
printf("%s: end.\n", program_name) ;
|
||||
return ret ;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void initialise_configuration(int argc, char **argv)
|
||||
{
|
||||
parse_config_file(argc, argv) ;
|
||||
parse_command_line(argc, argv) ;
|
||||
check_configuration() ;
|
||||
|
||||
#ifdef DEBUG
|
||||
print_configuration() ;
|
||||
#endif // DEBUG
|
||||
}
|
||||
|
||||
|
||||
|
||||
void parse_config_file(int argc, char **argv)
|
||||
{
|
||||
#ifdef USE_CONFIG_FILE
|
||||
// If we use libconfuse, we declare options:
|
||||
cfg_opt_t opts[] =
|
||||
|
@ -83,9 +135,8 @@ int main(int argc, char *argv[])
|
|||
char *config_file ; // Configuration file name
|
||||
#endif // USE_CONFIG_FILE
|
||||
|
||||
int opt ; // getopt return value
|
||||
|
||||
// Option -f specifies a config file, so we search for it first
|
||||
int opt ;
|
||||
do
|
||||
opt = getopt(argc, argv, OPTIONS) ;
|
||||
while (opt != 'f' && opt != -1) ;
|
||||
|
@ -127,10 +178,16 @@ int main(int argc, char *argv[])
|
|||
}
|
||||
free(config_file) ;
|
||||
#endif // USE_CONFIG_FILE
|
||||
}
|
||||
|
||||
|
||||
/* Parse command line */
|
||||
|
||||
void parse_command_line(int argc, char **argv)
|
||||
{
|
||||
int opt ;
|
||||
|
||||
optind = 1 ; // Rewind argument parsing
|
||||
|
||||
while ((opt = getopt(argc, argv, OPTIONS)) != -1)
|
||||
{
|
||||
switch (opt)
|
||||
|
@ -141,7 +198,7 @@ int main(int argc, char *argv[])
|
|||
case 'f' : // Config file
|
||||
break ; // (already parsed)
|
||||
case 'h' :
|
||||
print_usage(argv[0]) ;
|
||||
print_usage() ;
|
||||
exit(0) ;
|
||||
case 'k' :
|
||||
#ifdef USE_PTHREAD
|
||||
|
@ -175,12 +232,16 @@ int main(int argc, char *argv[])
|
|||
SET_WIFI_IFACE(optarg) ;
|
||||
break ;
|
||||
default :
|
||||
print_usage(argv[0]) ;
|
||||
return ERR_BAD_USAGE ;
|
||||
print_usage() ;
|
||||
exit(ERR_BAD_USAGE) ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Check arguments */
|
||||
|
||||
|
||||
void check_configuration()
|
||||
{
|
||||
switch (GET_MODE())
|
||||
{
|
||||
case MODE_ACTIVE :
|
||||
|
@ -189,15 +250,15 @@ int main(int argc, char *argv[])
|
|||
break ;
|
||||
default :
|
||||
fprintf(stderr, "Error! Unknown mode « %c ».\n", (char) GET_MODE()) ;
|
||||
print_usage(argv[0]) ;
|
||||
return ERR_BAD_USAGE ;
|
||||
print_usage() ;
|
||||
exit(ERR_BAD_USAGE) ;
|
||||
}
|
||||
if (GET_RTAP_IFACE()[0] == '\0')
|
||||
{
|
||||
fprintf(stderr, "Error! You must specify a radiotap interface"
|
||||
" for the capture.\n") ;
|
||||
print_usage(argv[0]) ;
|
||||
return ERR_BAD_USAGE ;
|
||||
print_usage() ;
|
||||
exit(ERR_BAD_USAGE) ;
|
||||
}
|
||||
if (GET_WIFI_IFACE()[0] == '\0')
|
||||
{
|
||||
|
@ -208,9 +269,13 @@ int main(int argc, char *argv[])
|
|||
#endif // DEBUG
|
||||
SET_WIFI_IFACE(GET_RTAP_IFACE()) ;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#ifdef DEBUG
|
||||
/* Print configuration */
|
||||
void print_configuration()
|
||||
{
|
||||
fprintf(stderr, "Configuration:\n") ;
|
||||
#ifdef USE_CONFIG_FILE
|
||||
cfg_print(cfg, stderr) ;
|
||||
|
@ -234,38 +299,8 @@ int main(int argc, char *argv[])
|
|||
) ;
|
||||
#endif // USE_PTHREAD
|
||||
#endif // USE_CONFIG_FILE
|
||||
#endif // DEBUG
|
||||
|
||||
run = TRUE ;
|
||||
|
||||
/* Set up signal handlers */
|
||||
sigemptyset(&action.sa_mask) ;
|
||||
action.sa_handler = sigint_handler ;
|
||||
sigaction(SIGINT, &action, NULL) ;
|
||||
action.sa_handler = sigterm_handler ;
|
||||
sigaction(SIGTERM, &action, NULL) ;
|
||||
|
||||
#ifdef USE_PTHREAD
|
||||
/* Set up thread */
|
||||
if (GET_KEEP_MONITOR())
|
||||
pthread_create(&thread, NULL,
|
||||
(void *) &keep_mode_monitor, GET_WIFI_IFACE()) ;
|
||||
#endif // USE_PTHREAD
|
||||
|
||||
get_mac_addr(GET_WIFI_IFACE(), mac) ;
|
||||
mac_string = mac_bytes_to_string(mac) ;
|
||||
printf("My MAC address is: %s\n", mac_string) ;
|
||||
free(mac_string) ;
|
||||
|
||||
ret = capture() ;
|
||||
|
||||
#ifdef USE_CONFIG_FILE
|
||||
cfg_free(cfg) ; // Clean configuration
|
||||
#endif // USE_CONFIG_FILE
|
||||
|
||||
printf("%s: end.\n", argv[0]) ;
|
||||
return ret ;
|
||||
}
|
||||
#endif // DEBUG
|
||||
|
||||
|
||||
|
||||
|
@ -687,7 +722,7 @@ void get_mac_addr(char *eth, unsigned char mac_bytes[6])
|
|||
|
||||
|
||||
|
||||
void print_usage(char *prog)
|
||||
void print_usage()
|
||||
{
|
||||
printf("Usage :\n"
|
||||
"\t%s [-f config_file] [-m mode] [-d aggregation_ip]"
|
||||
|
@ -719,7 +754,7 @@ void print_usage(char *prog)
|
|||
"\t-v\tVerbose mode (display captured packets).\n"
|
||||
"\t-q\tQuiet mode (default).\n"
|
||||
,
|
||||
prog,
|
||||
program_name,
|
||||
DEFAULT_CONFIG_FILE,
|
||||
AGGREGATE_DEFAULT_PORT
|
||||
) ;
|
||||
|
|
Loading…
Reference in New Issue