diff --git a/TODO b/TODO index 2465a16..dc31ebc 100644 --- a/TODO +++ b/TODO @@ -39,9 +39,6 @@ * Listener -- Send coordinates in autocalibration requests - The listener should be able to know its own position and transmit - it in autocalibration requests. - read_packet(): use ieee80211_header_size for all implicit packets Currently the size is corrected only for data packets. - Fix segfault when rtap_iface is not in monitor mode (?) on Foneras. diff --git a/owlps-listener/owlps-listener.h b/owlps-listener/owlps-listener.h index 47af346..3f7ece2 100644 --- a/owlps-listener/owlps-listener.h +++ b/owlps-listener/owlps-listener.h @@ -63,6 +63,8 @@ enum {MODE_ACTIVE = 'a', MODE_PASSIVE = 'p', MODE_MIXED = 'm'} ; 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) ; +void parse_calibration_data(int argc, char **argv) ; void check_configuration(void) ; #ifdef DEBUG void print_configuration(void) ; @@ -157,6 +159,22 @@ void print_version(void) ; (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 SET_VERBOSE() \ @@ -234,6 +252,22 @@ void print_version(void) ; (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 SET_VERBOSE() \ diff --git a/owlps-listener/owlps-listenerd.c b/owlps-listener/owlps-listenerd.c index 813e8ed..81e999a 100644 --- a/owlps-listener/owlps-listenerd.c +++ b/owlps-listener/owlps-listenerd.c @@ -28,6 +28,8 @@ #include #include +#include + char *program_name = NULL ; uint8_t my_mac_bytes[ETHER_ADDR_LEN] ; // AP MAC address @@ -36,8 +38,13 @@ char my_ip[INET_ADDRSTRLEN] ; // AP IP address pcap_t *capture_handler = NULL ; // Packet capture descriptor int aggregation_sockfd ; struct sockaddr_in aggregation_server ; + +#ifdef USE_PTHREAD int autocalibration_send_sockfd ; struct sockaddr_in autocalibration_send_server ; +// TRUE if the coordinates of the listener were provided by the user: +owl_bool coordinates_provided = FALSE ; +#endif // USE_PTHREAD #ifdef USE_CONFIG_FILE cfg_t *cfg ; // Configuration structure @@ -64,6 +71,10 @@ struct { uint_fast32_t autocalibration_hello_delay ; uint_fast32_t autocalibration_delay ; uint_fast16_t autocalibration_nb_packets ; + owl_direction my_direction ; + float my_position_x ; + float my_position_y ; + float my_position_z ; #endif // USE_PTHREAD owl_bool verbose ; owl_bool display_captured ; @@ -85,6 +96,7 @@ struct { DEFAULT_AUTOCALIBRATION_HELLO_DELAY, DEFAULT_AUTOCALIBRATION_DELAY, DEFAULT_AUTOCALIBRATION_NBPKT, + 0, 0, 0, 0, #endif // USE_PTHREAD FALSE, FALSE @@ -278,6 +290,14 @@ void parse_config_file(int argc, char **argv) // Number of packets for a calibration request: CFG_INT("autocalibration_nb_packets", DEFAULT_AUTOCALIBRATION_NBPKT, CFGF_NONE), + // Direction + CFG_INT("my_direction", 0, CFGF_NONE), + // Position X + CFG_FLOAT("my_position_x", 0, CFGF_NONE), + // Position Y + CFG_FLOAT("my_position_y", 0, CFGF_NONE), + // Position Z + CFG_FLOAT("my_position_z", 0, CFGF_NONE), #endif // USE_PTHREAD // Be verbose, or not: CFG_BOOL("verbose", cfg_false, CFGF_NONE), @@ -347,6 +367,14 @@ void parse_config_file(int argc, char **argv) void parse_command_line(int argc, char **argv) +{ + parse_main_options(argc, argv) ; + parse_calibration_data(argc, argv) ; +} + + + +void parse_main_options(int argc, char **argv) { int opt ; @@ -450,6 +478,29 @@ void parse_command_line(int argc, char **argv) +void parse_calibration_data(int argc, char **argv) +{ + /* Parse remaining arguments (possible calibration data) */ + if (argc - optind != 0) + { + if (argc - optind == 4) + { + coordinates_provided = TRUE ; + SET_MY_DIRECTION(strtoul(argv[optind++], NULL, 0)) ; + SET_MY_POSITION_X(strtod(argv[optind++], NULL)) ; + SET_MY_POSITION_Y(strtod(argv[optind++], NULL)) ; + SET_MY_POSITION_Z(strtod(argv[optind], NULL)) ; + } + else // Bad number of arguments + { + print_usage() ; + exit(ERR_BAD_USAGE) ; + } + } +} + + + void check_configuration() { switch (GET_MODE()) @@ -481,24 +532,6 @@ void check_configuration() SET_WIFI_IFACE(GET_RTAP_IFACE()) ; } -#ifdef USE_PTHREAD - if (GET_AUTOCALIBRATION()) - { - if (GET_AUTOCALIBRATION_IP()[0] == '\0') - { - if (GET_VERBOSE()) - fprintf(stderr, "No autocalibration IP specified, we will" - " use the aggregation IP as the destination of" - " autocalibration requests.\n") ; - SET_AUTOCALIBRATION_IP(GET_AGGREGATION_IP()) ; - } - if (GET_AUTOCALIBRATION_NBPKT() < 1) - fprintf(stderr, "Warning! autocalibration_nb_packets is null," - " no autocalibration request will be sent!\n") ; - } - -#endif // USE_PTHREAD - // Port numbers // if (GET_AGGREGATION_PORT() < 1 || GET_AGGREGATION_PORT() > 65535) { @@ -514,22 +547,47 @@ void check_configuration() " failing back to the default value.\n") ; SET_LISTENING_PORT(LOC_REQUEST_DEFAULT_PORT) ; } + + // Autocalibration stuff // #ifdef USE_PTHREAD - if (GET_AUTOCALIBRATION_REQUEST_PORT() < 1 || - GET_AUTOCALIBRATION_REQUEST_PORT() > 65535) + if (GET_AUTOCALIBRATION()) { - if (GET_VERBOSE()) - fprintf(stderr, "Warning! Bad autocalibration_request_port:" - " failing back to the default value.\n") ; - SET_AUTOCALIBRATION_REQUEST_PORT(DEFAULT_AUTOCALIBRATION_REQUEST_PORT) ; - } - if (GET_AUTOCALIBRATION_PORT() < 1 || - GET_AUTOCALIBRATION_PORT() > 65535) - { - if (GET_VERBOSE()) - fprintf(stderr, "Warning! Bad autocalibration_port:" - " failing back to the default value.\n") ; - SET_AUTOCALIBRATION_PORT(DEFAULT_AUTOCALIBRATION_PORT) ; + if (GET_AUTOCALIBRATION_IP()[0] == '\0') + { + if (GET_VERBOSE()) + fprintf(stderr, "No autocalibration IP specified, we will" + " use the aggregation IP as the destination of" + " autocalibration requests.\n") ; + SET_AUTOCALIBRATION_IP(GET_AGGREGATION_IP()) ; + } + + if (GET_AUTOCALIBRATION_NBPKT() < 1) + fprintf(stderr, "Warning! autocalibration_nb_packets is null," + " no autocalibration request will be sent!\n") ; + + if (coordinates_provided) + if (GET_MY_DIRECTION() < OWL_DIRECTION_MIN || + GET_MY_DIRECTION() > OWL_DIRECTION_MAX) + fprintf(stderr, "Warning! « %d » is not a valid" + " direction.\n", (int) GET_MY_DIRECTION()) ; + + // Autocalibration port numbers + if (GET_AUTOCALIBRATION_REQUEST_PORT() < 1 || + GET_AUTOCALIBRATION_REQUEST_PORT() > 65535) + { + if (GET_VERBOSE()) + fprintf(stderr, "Warning! Bad autocalibration_request_port:" + " failing back to the default value.\n") ; + SET_AUTOCALIBRATION_REQUEST_PORT(DEFAULT_AUTOCALIBRATION_REQUEST_PORT) ; + } + if (GET_AUTOCALIBRATION_PORT() < 1 || + GET_AUTOCALIBRATION_PORT() > 65535) + { + if (GET_VERBOSE()) + fprintf(stderr, "Warning! Bad autocalibration_port:" + " failing back to the default value.\n") ; + SET_AUTOCALIBRATION_PORT(DEFAULT_AUTOCALIBRATION_PORT) ; + } } #endif // USE_PTHREAD } @@ -559,6 +617,10 @@ void print_configuration() "autocalibration_hello_delay = %"PRIuFAST32"\n" "autocalibration_delay = %"PRIuFAST32"\n" "autocalibration_nb_packets = %"PRIuFAST16"\n" + "my_direction = %"PRIu8"\n" + "my_position_x = %f\n" + "my_position_y = %f\n" + "my_position_z = %f\n" #endif // USE_PTHREAD "verbose = %s\n" "display_captured = %s\n" @@ -578,6 +640,10 @@ void print_configuration() GET_AUTOCALIBRATION_HELLO_DELAY(), GET_AUTOCALIBRATION_DELAY(), GET_AUTOCALIBRATION_NBPKT(), + GET_MY_DIRECTION(), + GET_MY_POSITION_X(), + GET_MY_POSITION_Y(), + GET_MY_POSITION_Z(), #endif // USE_PTHREAD OWL_BOOL_TO_STRING(GET_VERBOSE()), OWL_BOOL_TO_STRING(GET_DISPLAY_CAPTURED()) @@ -823,6 +889,21 @@ void read_packet(u_char *args, const struct pcap_pkthdr *header, printf(".. on the wrong port!") ; putchar('\n') ; } + request.direction = + packet[rtap_bytes + ieee80211_header_size + LLC_HEADER_SIZE + + sizeof(struct iphdr) + sizeof(struct udphdr) + 9]; + memcpy(&request.x_position, + &packet[rtap_bytes + ieee80211_header_size + + LLC_HEADER_SIZE + sizeof(struct iphdr) + + sizeof(struct udphdr) + 10], sizeof(float)) ; + memcpy(&request.y_position, + &packet[rtap_bytes + ieee80211_header_size + + LLC_HEADER_SIZE + sizeof(struct iphdr) + + sizeof(struct udphdr) + 14], sizeof(float)) ; + memcpy(&request.z_position, + &packet[rtap_bytes + ieee80211_header_size + + LLC_HEADER_SIZE + sizeof(struct iphdr) + + sizeof(struct udphdr) + 18], sizeof(float)) ; break ; default : @@ -1183,7 +1264,12 @@ uint_fast16_t make_packet(uint8_t **packet) { uint8_t *pkt ; uint_fast16_t size ; // Packet size + uint_fast16_t offset ; // Index used to create the packet owl_timestamp request_time ; + float + my_position_x = GET_MY_POSITION_X(), + my_position_y = GET_MY_POSITION_Y(), + my_position_z = GET_MY_POSITION_Z() ; owl_timestamp_now(&request_time) ; @@ -1196,11 +1282,29 @@ uint_fast16_t make_packet(uint8_t **packet) request_time = owl_hton_timestamp(request_time) ; - size = sizeof(char) + sizeof(owl_timestamp) ; + offset = 0 ; + size = + sizeof(uint8_t) * 2 + sizeof(owl_timestamp) + sizeof(float) * 3 ; pkt = malloc(size) ; - memset(&pkt[0], OWL_REQUEST_AUTOCALIBRATION, 1) ; // Packet type - memcpy(&pkt[1], &request_time, sizeof(request_time)) ; + // Request type: + memset(&pkt[offset], OWL_REQUEST_AUTOCALIBRATION, 1) ; + ++offset ; + // Timestamp: + memcpy(&pkt[offset], &request_time, sizeof(request_time)) ; + offset += sizeof(request_time) ; + // Coordinates: + pkt[offset] = GET_MY_DIRECTION() ; + ++offset ; + memcpy(&pkt[offset], &my_position_x, sizeof(float)) ; + offset += sizeof(float) ; + memcpy(&pkt[offset], &my_position_y, sizeof(float)) ; + offset += sizeof(float) ; + memcpy(&pkt[offset], &my_position_z, sizeof(float)) ; +#ifdef DEBUG + offset += sizeof(float) ; + assert(offset == size) ; +#endif // DEBUG *packet = pkt ; return size ; @@ -1236,7 +1340,7 @@ void print_usage() " [-D autocalibration_ip]" " [-P autocalibration_request_port ] [-a autocalibration_port]" " [-H autocalibration_hello_delay] [-t autocalibration_delay]" - " [-n autocalibration_nb_packets]\n" + " [-n autocalibration_nb_packets] [direction x y z]\n" "\t%s -h\n" "\t%s -V\n" @@ -1266,16 +1370,19 @@ void print_usage() "\t-A\t\t\tEnable autocalibration (default: disabled).\n" "\t-D autocalib_ip\t\tDestination IP of the autocalibration" "requests (default: aggregation_ip).\n" - "\t-P autocalib_req_port\tPort on which autocalibration positioning" - " requests are sent (default: %d).\n" - "\t-a autocalib_port\tPort on which autocalibration data (hello & orders)" - " are exchanged with the aggregation server (default: %d).\n" + "\t-P autocalib_req_port\tPort on which autocalibration" + " positioning requests are sent (default: %d).\n" + "\t-a autocalib_port\tPort on which autocalibration data (hello" + " & orders) are exchanged with the aggregation server" + " (default: %d).\n" "\t-H hello_delay\t\tTime between each hello" " message sent to the aggregation server (default: %d s).\n" "\t-t delay\t\tTime between each autocalibration" " packet transmission (default: %d µs).\n" "\t-n nb_packets\t\tNumber of packet transmitted" " for one autocalibration request (default: %d).\n" + "\tdirection x y z\t\tThe coordinates of the listener" + " (direction is an integer; x, y, z are floats).\n" "Other options:\n" "\t-k\tKeep the monitor mode up on wifi_iface. Use it with buggy"