311 lines
9.8 KiB
C
311 lines
9.8 KiB
C
/*
|
|
* This file is part of the rtap localisation project.
|
|
*/
|
|
|
|
#ifndef _OWLPS_LISTENER_H_
|
|
#define _OWLPS_LISTENER_H_
|
|
|
|
/* Compilation-time options: to activate, uncomment or compile with -D
|
|
* option.
|
|
*/
|
|
//#define USE_CONFIG_FILE // Use libconfuse to read a config file
|
|
//#define USE_PTHREAD // POSIX threads available?
|
|
|
|
#define DEBUG
|
|
|
|
|
|
#include <owlps.h>
|
|
#include <owlps-client.h>
|
|
|
|
#include <pcap.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#ifdef __GLIBC__
|
|
# include <endian.h>
|
|
/* <endian.h> defines le*toh only in glibc >= 2.9. If we use an older
|
|
* version of glibc, or another glibc-friendly libc (e.g. uClibc), we
|
|
* must define them manually.
|
|
*/
|
|
# if __BYTE_ORDER == __LITTLE_ENDIAN
|
|
# ifndef le32toh
|
|
# define le32toh(x) (x)
|
|
# endif // le32toh
|
|
# ifndef le16toh
|
|
# define le16toh(x) (x)
|
|
# endif // le16toh
|
|
# elif __BYTE_ORDER == __BIG_ENDIAN
|
|
# ifndef le32toh
|
|
# include <byteswap.h>
|
|
# define le32toh(x) bswap_32(x)
|
|
# endif // le32toh
|
|
# ifndef le16toh
|
|
# include <byteswap.h>
|
|
# define le16toh(x) bswap_16(x)
|
|
# endif // le16toh
|
|
# else // __BYTE_ORDER
|
|
# error "This program does not handle strange architectures."
|
|
# endif // __BYTE_ORDER
|
|
#else // __GLIBC__
|
|
# include <sys/endian.h>
|
|
# ifdef OpenBSD
|
|
# define le32toh letoh32
|
|
# define le16toh letoh16
|
|
# endif // OpenBSD
|
|
#endif // __GLIBC__
|
|
|
|
|
|
/* Arguments & program configuration */
|
|
#define OPTIONS "Aa:cd:D:f:hH:kl:m:n:p:P:qQr:t:vVw:" // getopt string
|
|
#define DEFAULT_CONFIG_FILE "/usr/local/etc/owlps/owlps-listener.conf"
|
|
enum {MODE_ACTIVE = 'a', MODE_PASSIVE = 'p', MODE_MIXED = 'm'} ;
|
|
#define DEFAULT_AUTOCALIBRATION_HELLO_DELAY 120 // seconds
|
|
#define DEFAULT_AUTOCALIBRATION_DELAY 25 // milliseconds
|
|
#define DEFAULT_AUTOCALIBRATION_NBPKT 20
|
|
|
|
|
|
/* Verbosity levels */
|
|
#define VERBOSE_QUIET GET_VERBOSE() == 0
|
|
#define VERBOSE_WARNING GET_VERBOSE() >= 1
|
|
#define VERBOSE_INFO GET_VERBOSE() >= 2
|
|
#define VERBOSE_CHATTERBOX GET_VERBOSE() >= 3
|
|
|
|
|
|
/* Error codes */
|
|
#define ERR_OPENING_IFACE 2 // Error when opening capture interface
|
|
#define ERR_BAD_USAGE 3 // Bad program call
|
|
#define ERR_PARSING_CONFIG_FILE 4 // Error reading the configuration file
|
|
#define ERR_CREATING_THREAD 6 // Error creating a thread
|
|
|
|
|
|
/* 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 parse_main_options(int argc, char **argv) ;
|
|
#ifdef USE_PTHREAD
|
|
void parse_calibration_data(int argc, char **argv) ;
|
|
#endif // USE_PTHREAD
|
|
void check_configuration(void) ;
|
|
#ifdef DEBUG
|
|
void print_configuration(void) ;
|
|
#endif // DEBUG
|
|
|
|
#ifdef USE_PTHREAD
|
|
void* keep_mode_monitor(void *iface) ;
|
|
int iface_mode_monitor(const char *const iface) ;
|
|
#endif // USE_PTHREAD
|
|
int capture(void) ;
|
|
void read_packet(u_char *args, const struct pcap_pkthdr *header,
|
|
const u_char *packet) ;
|
|
void get_mac_addr(char *eth, uint8_t mac_bytes[ETHER_ADDR_LEN]) ;
|
|
void get_ip_addr(char *eth, char *ip_bytes) ;
|
|
|
|
#ifdef USE_PTHREAD
|
|
void* autocalibrate(void *NULL_value) ;
|
|
void* autocalibrate_hello(void *NULL_value) ;
|
|
void send_autocalibration_request(void) ;
|
|
uint_fast16_t make_packet(uint8_t **packet) ;
|
|
#endif // USE_PTHREAD
|
|
|
|
void sigint_handler(int num) ;
|
|
void sigterm_handler(int num) ;
|
|
|
|
void print_usage(void) ;
|
|
void print_version(void) ;
|
|
|
|
|
|
/* Macros to allow switching option handling with the libconfuse
|
|
* structure and a home-made structure (if program is not compiled
|
|
* with libconfuse support).
|
|
*/
|
|
/* libconfuse macros */
|
|
#ifdef USE_CONFIG_FILE
|
|
#define SET_MODE(MODE) \
|
|
(cfg_setint(cfg, "mode", (MODE)))
|
|
#define GET_MODE() \
|
|
(cfg_getint(cfg, "mode"))
|
|
#define SET_AGGREGATION_IP(IP) \
|
|
(cfg_setstr(cfg, "aggregation_ip", (IP)))
|
|
#define GET_AGGREGATION_IP() \
|
|
(cfg_getstr(cfg, "aggregation_ip"))
|
|
#ifdef USE_PTHREAD
|
|
#define SET_KEEP_MONITOR() \
|
|
(cfg_setbool(cfg, "keep_monitor", cfg_true))
|
|
#define GET_KEEP_MONITOR() \
|
|
(cfg_getbool(cfg, "keep_monitor"))
|
|
#endif // USE_PTHREAD
|
|
#define SET_AGGREGATION_PORT(PORT) \
|
|
(cfg_setint(cfg, "aggregation_port", (PORT)))
|
|
#define GET_AGGREGATION_PORT() \
|
|
(cfg_getint(cfg, "aggregation_port"))
|
|
#define SET_LISTENING_PORT(PORT) \
|
|
(cfg_setint(cfg, "listening_port", (PORT)))
|
|
#define GET_LISTENING_PORT() \
|
|
(cfg_getint(cfg, "listening_port"))
|
|
#define SET_RTAP_IFACE(IFACE) \
|
|
(cfg_setstr(cfg, "rtap_iface", (IFACE)))
|
|
#define GET_RTAP_IFACE() \
|
|
(cfg_getstr(cfg, "rtap_iface"))
|
|
#define SET_WIFI_IFACE(IFACE) \
|
|
(cfg_setstr(cfg, "wifi_iface", (IFACE)))
|
|
#define GET_WIFI_IFACE() \
|
|
(cfg_getstr(cfg, "wifi_iface"))
|
|
|
|
#ifdef USE_PTHREAD
|
|
#define SET_AUTOCALIBRATION() \
|
|
(cfg_setbool(cfg, "autocalibration", cfg_true))
|
|
#define GET_AUTOCALIBRATION() \
|
|
(cfg_getbool(cfg, "autocalibration"))
|
|
#define SET_AUTOCALIBRATION_IP(IP) \
|
|
(cfg_setstr(cfg, "autocalibration_ip", (IP)))
|
|
#define GET_AUTOCALIBRATION_IP() \
|
|
(cfg_getstr(cfg, "autocalibration_ip"))
|
|
#define SET_AUTOCALIBRATION_REQUEST_PORT(PORT) \
|
|
(cfg_setint(cfg, "autocalibration_request_port", (PORT)))
|
|
#define GET_AUTOCALIBRATION_REQUEST_PORT() \
|
|
(cfg_getint(cfg, "autocalibration_request_port"))
|
|
#define SET_AUTOCALIBRATION_PORT(PORT) \
|
|
(cfg_setint(cfg, "autocalibration_port", (PORT)))
|
|
#define GET_AUTOCALIBRATION_PORT() \
|
|
(cfg_getint(cfg, "autocalibration_port"))
|
|
#define SET_AUTOCALIBRATION_HELLO_DELAY(DELAY) \
|
|
(cfg_setint(cfg, "autocalibration_hello_delay", (DELAY)))
|
|
#define GET_AUTOCALIBRATION_HELLO_DELAY() \
|
|
(cfg_getint(cfg, "autocalibration_hello_delay"))
|
|
#define SET_AUTOCALIBRATION_DELAY(DELAY) \
|
|
(cfg_setint(cfg, "autocalibration_delay", (DELAY)))
|
|
#define GET_AUTOCALIBRATION_DELAY() \
|
|
(cfg_getint(cfg, "autocalibration_delay"))
|
|
#define SET_AUTOCALIBRATION_NBPKT(NBPKT) \
|
|
(cfg_setint(cfg, "autocalibration_nb_packets", (NBPKT)))
|
|
#define GET_AUTOCALIBRATION_NBPKT() \
|
|
(cfg_getint(cfg, "autocalibration_nb_packets"))
|
|
#define SET_MY_DIRECTION(DIRECTION) \
|
|
(cfg_setint(cfg, "my_direction", (DIRECTION)))
|
|
#define GET_MY_DIRECTION() \
|
|
(cfg_getint(cfg, "my_direction"))
|
|
#define SET_MY_POSITION_X(POSITION) \
|
|
(cfg_setfloat(cfg, "my_position_x", (POSITION)))
|
|
#define GET_MY_POSITION_X() \
|
|
(cfg_getfloat(cfg, "my_position_x"))
|
|
#define SET_MY_POSITION_Y(POSITION) \
|
|
(cfg_setfloat(cfg, "my_position_y", (POSITION)))
|
|
#define GET_MY_POSITION_Y() \
|
|
(cfg_getfloat(cfg, "my_position_y"))
|
|
#define SET_MY_POSITION_Z(POSITION) \
|
|
(cfg_setfloat(cfg, "my_position_z", (POSITION)))
|
|
#define GET_MY_POSITION_Z() \
|
|
(cfg_getfloat(cfg, "my_position_z"))
|
|
#endif // USE_PTHREAD
|
|
|
|
#define INCREMENT_VERBOSE() \
|
|
(cfg_setint(cfg, "verbose", cfg_getint(cfg, "verbose") + 1))
|
|
#define RESET_VERBOSE() \
|
|
(cfg_setint(cfg, "verbose", 0))
|
|
#define GET_VERBOSE() \
|
|
(cfg_getint(cfg, "verbose"))
|
|
#define SET_DISPLAY_CAPTURED() \
|
|
(cfg_setbool(cfg, "display_captured", cfg_true))
|
|
#define UNSET_DISPLAY_CAPTURED() \
|
|
(cfg_setbool(cfg, "display_captured", cfg_false))
|
|
#define GET_DISPLAY_CAPTURED() \
|
|
(cfg_getbool(cfg, "display_captured"))
|
|
|
|
/* Home-made structure macros */
|
|
#else // USE_CONFIG_FILE
|
|
#define SET_MODE(MODE) \
|
|
(options.mode = (MODE))
|
|
#define GET_MODE() \
|
|
(options.mode)
|
|
#define SET_AGGREGATION_IP(IP) \
|
|
(strncpy(options.aggregation_ip, (IP), INET_ADDRSTRLEN))
|
|
#define GET_AGGREGATION_IP() \
|
|
(options.aggregation_ip)
|
|
#ifdef USE_PTHREAD
|
|
#define SET_KEEP_MONITOR() \
|
|
(options.keep_monitor = TRUE)
|
|
#define GET_KEEP_MONITOR() \
|
|
(options.keep_monitor)
|
|
#endif // USE_PTHREAD
|
|
#define SET_AGGREGATION_PORT(PORT) \
|
|
(options.aggregation_port = (PORT))
|
|
#define GET_AGGREGATION_PORT() \
|
|
(options.aggregation_port)
|
|
#define SET_LISTENING_PORT(PORT) \
|
|
(options.listening_port = (PORT))
|
|
#define GET_LISTENING_PORT() \
|
|
(options.listening_port)
|
|
#define SET_RTAP_IFACE(IFACE) \
|
|
(strncpy(options.rtap_iface, (IFACE), IFNAMSIZ+1))
|
|
#define GET_RTAP_IFACE() \
|
|
(options.rtap_iface)
|
|
#define SET_WIFI_IFACE(IFACE) \
|
|
(strncpy(options.wifi_iface, (IFACE), IFNAMSIZ+1))
|
|
#define GET_WIFI_IFACE() \
|
|
(options.wifi_iface)
|
|
|
|
#ifdef USE_PTHREAD
|
|
#define SET_AUTOCALIBRATION() \
|
|
(options.autocalibration = TRUE)
|
|
#define GET_AUTOCALIBRATION() \
|
|
(options.autocalibration)
|
|
#define SET_AUTOCALIBRATION_IP(IP) \
|
|
(strncpy(options.autocalibration_ip, (IP), INET_ADDRSTRLEN))
|
|
#define GET_AUTOCALIBRATION_IP() \
|
|
(options.autocalibration_ip)
|
|
#define SET_AUTOCALIBRATION_REQUEST_PORT(PORT) \
|
|
(options.autocalibration_request_port = (PORT))
|
|
#define GET_AUTOCALIBRATION_REQUEST_PORT() \
|
|
(options.autocalibration_request_port)
|
|
#define SET_AUTOCALIBRATION_PORT(PORT) \
|
|
(options.autocalibration_port = (PORT))
|
|
#define GET_AUTOCALIBRATION_PORT() \
|
|
(options.autocalibration_port)
|
|
#define SET_AUTOCALIBRATION_HELLO_DELAY(DELAY) \
|
|
(options.autocalibration_hello_delay = (DELAY))
|
|
#define GET_AUTOCALIBRATION_HELLO_DELAY() \
|
|
(options.autocalibration_hello_delay)
|
|
#define SET_AUTOCALIBRATION_DELAY(DELAY) \
|
|
(options.autocalibration_delay = (DELAY))
|
|
#define GET_AUTOCALIBRATION_DELAY() \
|
|
(options.autocalibration_delay)
|
|
#define SET_AUTOCALIBRATION_NBPKT(NBPKT) \
|
|
(options.autocalibration_nb_packets = (NBPKT))
|
|
#define GET_AUTOCALIBRATION_NBPKT() \
|
|
(options.autocalibration_nb_packets)
|
|
#define SET_MY_DIRECTION(DIRECTION) \
|
|
(options.my_direction = (DIRECTION))
|
|
#define GET_MY_DIRECTION() \
|
|
(options.my_direction)
|
|
#define SET_MY_POSITION_X(POSITION) \
|
|
(options.my_position_x = (POSITION))
|
|
#define GET_MY_POSITION_X() \
|
|
(options.my_position_x)
|
|
#define SET_MY_POSITION_Y(POSITION) \
|
|
(options.my_position_y = (POSITION))
|
|
#define GET_MY_POSITION_Y() \
|
|
(options.my_position_y)
|
|
#define SET_MY_POSITION_Z(POSITION) \
|
|
(options.my_position_z = (POSITION))
|
|
#define GET_MY_POSITION_Z() \
|
|
(options.my_position_z)
|
|
#endif // USE_PTHREAD
|
|
|
|
#define INCREMENT_VERBOSE() \
|
|
(++options.verbose)
|
|
#define RESET_VERBOSE() \
|
|
(options.verbose = 0)
|
|
#define GET_VERBOSE() \
|
|
(options.verbose)
|
|
#define SET_DISPLAY_CAPTURED() \
|
|
(options.display_captured = TRUE)
|
|
#define UNSET_DISPLAY_CAPTURED() \
|
|
(options.display_captured = FALSE)
|
|
#define GET_DISPLAY_CAPTURED() \
|
|
(options.display_captured)
|
|
#endif // USE_CONFIG_FILE
|
|
|
|
|
|
#endif // _OWLPS_LISTENER_H_
|