From d51f136d703c2a1b90a9627a17515425a6adc499 Mon Sep 17 00:00:00 2001 From: Matteo Cypriani Date: Tue, 12 Oct 2010 10:13:59 +0200 Subject: [PATCH] [Listener] Send & receive autocalibration requests --- .../owlps-listener/owlps-listener.h | 15 +- .../owlps-listener/owlps-listenerd.c | 153 +++++++++++++++++- libowlps/owlps.h | 6 + 3 files changed, 166 insertions(+), 8 deletions(-) diff --git a/infrastructure-centred/owlps-listener/owlps-listener.h b/infrastructure-centred/owlps-listener/owlps-listener.h index c45c13d..cf0adf2 100644 --- a/infrastructure-centred/owlps-listener/owlps-listener.h +++ b/infrastructure-centred/owlps-listener/owlps-listener.h @@ -48,10 +48,12 @@ /* Arguments & program configuration */ -#define OPTIONS "Aa:d:f:hH:kl:m:p:qr:vw:" // getopt string +#define OPTIONS "Aa:d:f:hH:kl:m:n:p:qr:t:vw:" // getopt string #define DEFAULT_CONFIG_FILE "/usr/local/etc/owlps/owlps-listener.conf" enum {MODE_ACTIVE = 'a', MODE_PASSIVE = 'p', MODE_MIXED = 'm'} ; #define AUTOCALIBRATION_DEFAULT_HELLO_DELAY 120 // seconds +#define AUTOCALIBRATION_DEFAULT_DELAY 25000 // ms +#define AUTOCALIBRATION_DEFAULT_NBPKT 20 /* Error codes */ @@ -78,7 +80,10 @@ void read_packet(u_char *args, const struct pcap_pkthdr *header, void get_mac_addr(char *eth, unsigned char mac_bytes[6]) ; #ifdef USE_PTHREAD +void autocalibrate(void) ; void autocalibrate_hello(void) ; +void send_autocalibration_request(void) ; +int make_packet(char **packet) ; #endif // USE_PTHREAD void print_usage(void) ; @@ -114,6 +119,10 @@ void print_usage(void) ; #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")) #endif // USE_PTHREAD #define SET_VERBOSE() (cfg_setbool(cfg, "verbose", cfg_true)) @@ -146,6 +155,10 @@ void print_usage(void) ; #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) #endif // USE_PTHREAD #define SET_VERBOSE() (options.verbose = TRUE) diff --git a/infrastructure-centred/owlps-listener/owlps-listenerd.c b/infrastructure-centred/owlps-listener/owlps-listenerd.c index 829a800..cc13621 100644 --- a/infrastructure-centred/owlps-listener/owlps-listenerd.c +++ b/infrastructure-centred/owlps-listener/owlps-listenerd.c @@ -12,6 +12,8 @@ unsigned char mac[6] ; // AP MAC address int aggregation_sockfd ; struct sockaddr_in aggregation_server ; +int autocalibration_send_sockfd ; +struct sockaddr_in autocalibration_send_server ; #ifdef USE_CONFIG_FILE cfg_t *cfg ; // Configuration structure @@ -34,6 +36,8 @@ struct { BOOL autocalibration ; long autocalibration_port ; long autocalibration_hello_delay ; + long autocalibration_delay ; + int autocalibration_nb_packets ; #endif // USE_PTHREAD BOOL verbose ; } options = { // Initalise default options: @@ -50,6 +54,8 @@ struct { FALSE, AUTOCALIBRATION_DEFAULT_PORT, AUTOCALIBRATION_DEFAULT_HELLO_DELAY, + AUTOCALIBRATION_DEFAULT_DELAY, + AUTOCALIBRATION_DEFAULT_NBPKT, #endif // USE_PTHREAD FALSE } ; @@ -65,6 +71,7 @@ int main(int argc, char *argv[]) #ifdef USE_PTHREAD pthread_t keep_monitor_thread, + autocalibration_thread, autocalibration_hello_thread ; #endif // USE_PTHREAD @@ -87,6 +94,8 @@ int main(int argc, char *argv[]) (void *) &keep_mode_monitor, GET_WIFI_IFACE()) ; if (GET_AUTOCALIBRATION()) { + pthread_create(&autocalibration_thread, NULL, + (void *) &autocalibrate, NULL) ; pthread_create(&autocalibration_hello_thread, NULL, (void *) &autocalibrate_hello, NULL) ; } @@ -157,6 +166,12 @@ void parse_config_file(int argc, char **argv) CFG_INT("autocalibration_hello_delay", AUTOCALIBRATION_DEFAULT_HELLO_DELAY, CFGF_NONE), + // Delay between two calibration packet transmission: + CFG_INT("autocalibration_delay", AUTOCALIBRATION_DEFAULT_DELAY, + CFGF_NONE), + // Number of packets for a calibration request: + CFG_INT("autocalibration_nb_packets", + AUTOCALIBRATION_DEFAULT_NBPKT, CFGF_NONE), #endif // USE_PTHREAD // Display capture packets, or not: CFG_BOOL("verbose", cfg_false, CFGF_NONE), @@ -266,6 +281,11 @@ void parse_command_line(int argc, char **argv) case 'm' : SET_MODE(optarg[0]) ; break ; + case 'n' : +#ifdef USE_PTHREAD + SET_AUTOCALIBRATION_NBPKT(strtol(optarg, NULL, 0)) ; +#endif // USE_PTHREAD + break ; case 'p' : SET_AGGREGATION_PORT(strtol(optarg, NULL, 0)) ; break ; @@ -275,6 +295,11 @@ void parse_command_line(int argc, char **argv) case 'r' : SET_RTAP_IFACE(optarg) ; break ; + case 't' : +#ifdef USE_PTHREAD + SET_AUTOCALIBRATION_DELAY(strtol(optarg, NULL, 0)) ; +#endif // USE_PTHREAD + break ; case 'v' : SET_VERBOSE() ; break ; @@ -513,6 +538,11 @@ void read_packet(u_char *args, const struct pcap_pkthdr *header, // an explicit request): couple.request_time.tv_sec = 0 ; couple.request_time.tv_usec = 0 ; + // Blank position data: + couple.direction = 0 ; + couple.x_position = 0 ; + couple.y_position = 0 ; + couple.z_position = 0 ; /* Active mode */ if (is_explicit_packet @@ -528,15 +558,11 @@ void read_packet(u_char *args, const struct pcap_pkthdr *header, case PACKET_TYPE_NORMAL : if (GET_VERBOSE()) printf("\nExplicit packet received.\n") ; - couple.direction = 0 ; - couple.x_position = 0 ; - couple.y_position = 0 ; - couple.z_position = 0 ; break ; case PACKET_TYPE_CALIBRATION : if (GET_VERBOSE()) - printf("\nExplicite calibration packet received.\n") ; + printf("\nExplicit calibration packet received.\n") ; couple.direction = data[rtap_bytes + IEEE80211_HEADER_SIZE + LLC_HEADER_SIZE + sizeof(struct iphdr) + sizeof(struct udphdr) + 9]; @@ -554,6 +580,11 @@ void read_packet(u_char *args, const struct pcap_pkthdr *header, + sizeof(struct udphdr) + 18], sizeof(float)) ; break ; + case PACKET_TYPE_AUTOCALIBRATION : + if (GET_VERBOSE()) + printf("\nAutocalibration packet received.\n") ; + break ; + default : if (GET_VERBOSE()) printf("\nStrange explicit packet received\n") ; @@ -766,6 +797,10 @@ void autocalibrate_hello() socklen_t serv_len = sizeof(serv); autocalibration_hello message ; +#ifdef DEBUG + fprintf(stderr, "Autocalibration Hello thread launched.\n") ; +#endif // DEBUG + send_sockfd = create_udp_sending_socket(GET_AGGREGATION_IP(), GET_AUTOCALIBRATION_PORT(), &serv, &client) ; @@ -782,6 +817,103 @@ void autocalibrate_hello() (void) close(send_sockfd) ; } + + +void autocalibrate() +{ + int nread ; // recvfrom return value + struct sockaddr_in client; // UDP client structure + socklen_t client_len = sizeof(client) ; // Size of clients + int listen_sockfd ; + autocalibration_order message ; + +#ifdef DEBUG + fprintf(stderr, "Autocalibration thread launched.\n") ; +#endif // DEBUG + + autocalibration_send_sockfd = + owlps_create_socket_to_aggregator(GET_AGGREGATION_IP(), + GET_AGGREGATION_PORT(), + &autocalibration_send_server, + GET_WIFI_IFACE()) ; + + listen_sockfd = + create_udp_listening_socket(GET_AUTOCALIBRATION_PORT()) ; + if (listen_sockfd < 0) + { + perror("Error! Cannot create UDP listening socket from the" + " aggregation server") ; + exit(ERR_CREATING_SOCKET) ; + } + + while (run) + { + nread = recvfrom(listen_sockfd, &message, sizeof(message), 0, + (struct sockaddr *) &client, &client_len) ; + + if (nread <= 0 && run) + { + if (run) + fprintf(stderr, "No message received from aggregator!\n") ; + continue ; + } + + if (message.order == AUTOCALIBRATION_ORDER_SEND) + send_autocalibration_request() ; +#ifdef DEBUG + else + fprintf(stderr, + "Autocalibration order unknown: %d.\n", message.order) ; +#endif // DEBUG + } + + (void) close(listen_sockfd) ; +} + + + +void send_autocalibration_request() +{ + char *packet ; + int packet_size = make_packet(&packet) ; + + owlps_send_request(autocalibration_send_sockfd, + &autocalibration_send_server, + packet, packet_size, + GET_AUTOCALIBRATION_NBPKT(), + GET_AUTOCALIBRATION_DELAY()) ; + + free(packet) ; +} + + + +/* + * Creates the calibration packet to send. + * The packet must be freed by the calling function. + * Returns the size of the packet. + */ +int make_packet(char **packet) +{ + char *pkt ; + int size ; // Packet size + struct timeval request_time ; + + gettimeofday(&request_time, NULL) ; + + if (GET_VERBOSE()) + printf("Autocalibration time: %llu\n", timeval_to_ms(request_time)) ; + + size = sizeof(char) + sizeof(struct timeval) ; + pkt = malloc(size) ; + + pkt[0] = PACKET_TYPE_AUTOCALIBRATION ; // Packet type + memcpy(&pkt[1], &request_time, sizeof(request_time)) ; + + *packet = pkt ; + return size ; +} + #endif // USE_PTHREAD /* *** End of autocalibration functions *** */ @@ -793,7 +925,8 @@ void print_usage() "\t%s [-f config_file] [-m mode] [-d aggregation_ip]" " [-l listening_port] [-p aggregation_port] -r rtap_iface" " [-w wifi_iface] [-k] [-v | -q] [-A] [-a autocalibration_port]" - " [-H autocalibration_hello_delay]\n" + " [-H autocalibration_hello_delay] [-t autocalibration_delay]" + " [-n autocalibration_nb_packets]\n" "Main options:\n" "\t-h\t\tPrint this help.\n" @@ -822,6 +955,10 @@ void print_usage() " are exchanged with the aggregation server (default: %d).\n" "\t-H hello_delay\tTime between each hello" " message sent to the aggregation server (default: %d s).\n" + "\t-t delay\tTime between each autocalibration" + " packet transmission (default: %d µs).\n" + "\t-n nb_packets\tNumber of packet transmitted" + " for one autocalibration request (default: %d).\n" "Other options:\n" "\t-k\tKeep the monitor mode up on wifi_iface. Use it with buggy" @@ -836,6 +973,8 @@ void print_usage() LOC_REQUEST_DEFAULT_PORT, AGGREGATE_DEFAULT_PORT, AUTOCALIBRATION_DEFAULT_PORT, - AUTOCALIBRATION_DEFAULT_HELLO_DELAY + AUTOCALIBRATION_DEFAULT_HELLO_DELAY, + AUTOCALIBRATION_DEFAULT_DELAY, + AUTOCALIBRATION_DEFAULT_NBPKT ) ; } diff --git a/libowlps/owlps.h b/libowlps/owlps.h index f92c3e0..9423f61 100644 --- a/libowlps/owlps.h +++ b/libowlps/owlps.h @@ -91,9 +91,15 @@ typedef struct _autocalibration_hello unsigned char ap_mac_addr_bytes[6]; } autocalibration_hello ; +typedef struct _autocalibration_order +{ + enum {AUTOCALIBRATION_ORDER_SEND = 1} order ; +} autocalibration_order ; + /* Types de demandes de localisation */ #define PACKET_TYPE_NORMAL 0 #define PACKET_TYPE_CALIBRATION 1 +#define PACKET_TYPE_AUTOCALIBRATION 2 /* Fréquences des canaux Wi-Fi en Hz */