owlps/libowlps/libowlps.c

559 lines
12 KiB
C
Raw Normal View History

/*
* This file is part of the rtap localisation project.
*/
#include "owlps.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <inttypes.h>
#include <string.h>
#include <time.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <iwlib.h>
2011-04-07 14:31:31 +02:00
#include <assert.h>
#define DEBUG
2011-03-29 15:44:37 +02:00
owl_bool owl_run = TRUE ;
/* *** Miscellaneous functions *** */
2011-03-09 17:42:58 +01:00
/*
* Converts a MAC address from bytes to string.
* The string is allocated in a static buffer, and will be overwritten
* each time this function is called.
* This function is not thread-safe!
*/
const char* owl_mac_bytes_to_string(const uint8_t *const mac_binary)
{
static char mac_str[OWL_ETHER_ADDR_STRLEN] ;
owl_mac_bytes_to_string_r(mac_binary, mac_str) ;
return mac_str ;
}
/*
* Converts a MAC address from bytes to string.
* 'mac_str' must be allocated by the caller.
* This function is thread-safe.
*/
void owl_mac_bytes_to_string_r(const uint8_t *const mac_binary,
char mac_str[OWL_ETHER_ADDR_STRLEN])
{
sprintf(mac_str, "%02x:%02x:%02x:%02x:%02x:%02x",
2011-03-09 17:42:58 +01:00
mac_binary[0], mac_binary[1], mac_binary[2],
mac_binary[3], mac_binary[4], mac_binary[5]) ;
}
/*
* Compares two MAC addresses.
* Returns TRUE if they are identical, FALSE otherwise.
*/
owl_bool owl_mac_equals(const uint8_t *const mac1,
const uint8_t *const mac2)
{
int i ;
for (i = 0 ; i < ETHER_ADDR_LEN ; ++i)
if(mac1[i] != mac2[i])
return FALSE ;
return TRUE ;
}
2011-03-09 17:42:58 +01:00
/*
* Converts a IEEE 802.11 frequency into a channel number.
* Returns 0 if the frequency does not correspond to an official channel.
*/
uint_fast8_t owl_frequency_to_channel(const uint_fast16_t channel)
{
uint_fast8_t c = 0 ; // Result
switch (channel)
{
case CHANNEL_1 :
c = 1 ;
break ;
case CHANNEL_2 :
c = 2 ;
break ;
case CHANNEL_3 :
c = 3 ;
break ;
case CHANNEL_4 :
c = 4 ;
break ;
case CHANNEL_5 :
c = 5 ;
break ;
case CHANNEL_6 :
c = 6 ;
break ;
case CHANNEL_7 :
c = 7 ;
break ;
case CHANNEL_8 :
c = 8 ;
break ;
case CHANNEL_9 :
c = 9 ;
break ;
case CHANNEL_10 :
c = 10 ;
break ;
case CHANNEL_11 :
c = 11 ;
break ;
case CHANNEL_12 :
c = 12 ;
break ;
case CHANNEL_13 :
c = 13 ;
break ;
case CHANNEL_14 :
c = 14 ;
break ;
}
return c ;
}
/* *** Time *** */
/*
* Sets the owl_timestamp 'now' at the current time.
* Returns 0 in case of success non-zero otherwise.
*/
int owl_timestamp_now(owl_timestamp *const now)
{
int ret ;
struct timespec now_ts ;
if ((ret = clock_gettime(CLOCK_REALTIME, &now_ts)))
{
perror("Cannot get the current time") ;
return ret ;
}
*now = owl_timespec_to_timestamp(now_ts) ;
return 0 ;
}
/*
* Returns a owl_timestamp from a struct timespec.
*/
owl_timestamp owl_timespec_to_timestamp(const struct timespec d)
{
owl_timestamp res ;
res.tv_sec = d.tv_sec ;
res.tv_nsec = d.tv_nsec ;
return res ;
}
/*
* Returns a owl_timestamp from a struct timeval.
*/
owl_timestamp owl_timeval_to_timestamp(const struct timeval d)
{
owl_timestamp res ;
res.tv_sec = d.tv_sec ;
res.tv_nsec = d.tv_usec * 1000u ;
return res ;
}
owl_bool owl_timestamp_equals(const owl_timestamp d1,
const owl_timestamp d2)
{
return d1.tv_sec == d2.tv_sec && d1.tv_nsec == d2.tv_nsec ;
}
owl_bool owl_timestamp_is_null(const owl_timestamp d)
2011-03-24 23:05:56 +01:00
{
return d.tv_sec == 0 && d.tv_nsec == 0 ;
}
/*
* Converts a owl_timestamp date value into milliseconds.
*/
uint64_t owl_timestamp_to_ms(const owl_timestamp d)
{
return (uint64_t) d.tv_sec * 1000u + (uint64_t) d.tv_nsec / 1000000lu ;
}
2011-03-15 13:50:00 +01:00
/*
* Converts a owl_timestamp date value into a printable string.
* 'dst' must be an allocated array of at least owl_timestamp_STR_LEN
2011-03-15 13:50:00 +01:00
* characters.
*/
void owl_timestamp_to_string(char *const dst, const owl_timestamp src)
2011-03-15 13:50:00 +01:00
{
snprintf(dst, OWL_TIMESTAMP_STR_LEN, "%"PRIu32".%"PRIu32,
2011-03-15 13:50:00 +01:00
src.tv_sec, src.tv_nsec) ;
}
2011-03-09 17:42:58 +01:00
/*
* Returns the time (in milliseconds) between two dates.
*/
uint_fast32_t owl_time_elapsed_ms(const owl_timestamp d1,
const owl_timestamp d2)
{
return owl_timestamp_to_ms(owl_time_elapsed(d1, d2)) ;
}
/*
* Returns a owl_timestamp storing the time between two dates.
*/
owl_timestamp owl_time_elapsed(const owl_timestamp d1,
const owl_timestamp d2)
{
owl_timestamp elapsed ;
elapsed.tv_sec = abs(d1.tv_sec - d2.tv_sec) ;
elapsed.tv_nsec = abs(d1.tv_nsec - d2.tv_nsec) ;
#ifdef DEBUG
fprintf(stderr, "time_elapsed(): %"PRIu64"ms\n",
owl_timestamp_to_ms(elapsed)) ;
#endif
return elapsed ;
}
2011-04-07 14:31:31 +02:00
/* *** Endianess *** */
/*
* Converts a owl_timestamp from host endianess to network endianess.
*/
owl_timestamp owl_hton_timestamp(const owl_timestamp d)
{
owl_timestamp date ;
date.tv_sec = htonl(d.tv_sec) ;
date.tv_nsec = htonl(d.tv_nsec) ;
return date ;
}
/*
* Converts a owl_timestamp from network endianess to host endianess.
*/
owl_timestamp owl_ntoh_timestamp(const owl_timestamp d)
{
owl_timestamp date ;
date.tv_sec = ntohl(d.tv_sec) ;
date.tv_nsec = ntohl(d.tv_nsec) ;
return date ;
}
2011-04-07 14:31:31 +02:00
/*
* Swap bytes composing a float.
* You probably want to use the macros owl_htonf() and owl_ntohf()
* instead of this function.
*/
float owl_swap_float(const float f)
{
float ret ;
char
*f_bytes = (char*) &f,
*ret_bytes = (char*) &ret ;
assert(sizeof(float) == 4) ;
ret_bytes[0] = f_bytes[3] ;
ret_bytes[1] = f_bytes[2] ;
ret_bytes[2] = f_bytes[1] ;
ret_bytes[3] = f_bytes[0] ;
return ret ;
}
/* *** Network *** */
2011-03-09 17:42:58 +01:00
/*
* Creates a UDP transmission socket and returns its descriptor.
* Parameters:
* - server_address: the server IP address.
* - server_port: the listening port on the server.
* - server_description (in/out): the structure in which the server
* description will be saved.
* - client_description (in/out): the structure in which the client
* description will be saved.
*/
int owl_create_udp_trx_socket(const char *const server_address,
const uint_fast16_t server_port,
struct sockaddr_in *const server_description,
struct sockaddr_in *const client_description)
{
2011-03-09 17:42:58 +01:00
int sockfd ; // Socket descriptor
/* Create the UDP socket */
sockfd = socket(AF_INET, SOCK_DGRAM, 0) ;
if (sockfd < 0)
{
2011-03-09 17:42:58 +01:00
perror("UDP socket creation failed") ;
return sockfd ;
}
2011-03-09 17:42:58 +01:00
/* Initialise the client structure */
2011-03-10 16:10:58 +01:00
memset(client_description, 0, sizeof(*client_description)) ;
2011-03-09 17:42:58 +01:00
client_description->sin_family = AF_INET ; // INET socket
client_description->sin_addr.s_addr = htonl(INADDR_ANY) ;
/* Initialise the server structure */
2011-03-10 16:10:58 +01:00
memset(server_description, 0, sizeof(*server_description)) ;
2011-03-09 17:42:58 +01:00
server_description->sin_family = AF_INET ; // INET socket
// Server IP address:
server_description->sin_addr.s_addr = inet_addr(server_address) ;
// Listening port on the server:
server_description->sin_port = htons(server_port) ;
return sockfd ;
}
2011-03-09 17:42:58 +01:00
/*
* Creates a UDP reception socket and returns its descriptor.
* Parameters:
* - port: port on which the socket listens.
*/
int owl_create_udp_listening_socket(const uint_fast16_t port)
{
2011-03-09 17:42:58 +01:00
int sockfd ; // Socket descriptor
struct sockaddr_in server_description ; // Server structure
int ret = 0 ; // Return value
2011-03-09 17:42:58 +01:00
/* Create the UDP socket */
sockfd = socket(AF_INET, SOCK_DGRAM, 0) ;
if (sockfd < 0)
{
2011-03-09 17:42:58 +01:00
perror("UDP socket creation failed") ;
return sockfd ;
}
2011-03-09 17:42:58 +01:00
/* Initialise the server structure */
2011-03-10 16:10:58 +01:00
memset(&server_description, 0, sizeof(server_description)) ;
2011-03-09 17:42:58 +01:00
server_description.sin_family = AF_INET ; // INET socket
// All the connections are accepted:
server_description.sin_addr.s_addr = htonl(INADDR_ANY) ;
server_description.sin_port = htons(port) ; // Listening port
2011-03-09 17:42:58 +01:00
/* Port reservation */
ret = bind(sockfd, (struct sockaddr*) &server_description,
sizeof(server_description)) ;
if (ret < 0)
{
2011-03-09 17:42:58 +01:00
perror("Cannot bind the UDP socket") ;
2011-03-24 08:43:43 +01:00
close(sockfd) ;
return ret ;
}
2011-03-09 17:42:58 +01:00
return sockfd ;
}
2011-03-09 17:42:58 +01:00
/*
* Switches the IEEE 802.11 interface 'iface' to Monitor mode.
*/
int owl_iface_mode_monitor(const char *const iface)
{
struct iwreq wrq ;
int sockfd = iw_sockets_open() ;
strncpy((&wrq)->ifr_name, iface, IFNAMSIZ) ;
2010-08-05 17:03:21 +02:00
if (ioctl(sockfd, SIOCGIWMODE, &wrq) == -1) // Get current mode
{
2010-08-05 17:03:21 +02:00
perror("Error reading interface mode") ;
return ERR_READING_MODE ;
}
2010-08-05 17:03:21 +02:00
// If interface is not yet in Monitor mode
if (wrq.u.mode != IW_MODE_MONITOR)
{
wrq.u.mode = IW_MODE_MONITOR ;
2010-08-05 17:03:21 +02:00
if (ioctl(sockfd, SIOCSIWMODE, &wrq) == -1) // Set up Monitor mode
2011-02-24 14:51:55 +01:00
{
perror("Error setting up Monitor mode") ;
return ERR_SETTING_MODE ;
}
}
close(sockfd) ;
return 0 ;
}
2011-03-09 17:42:58 +01:00
/*
* Sets the IEEE 802.11 channel of the interface 'iface'.
* 'channel' must be an integer between 1 and 14.
*/
int owl_iface_set_channel(const char *const iface,
const uint_fast8_t channel)
{
struct iwreq wrq ;
int sockfd = iw_sockets_open() ;
strncpy((&wrq)->ifr_name, iface, IFNAMSIZ) ;
iw_float2freq(channel, &(wrq.u.freq)) ;
wrq.u.freq.flags = IW_FREQ_FIXED ;
if (ioctl(sockfd, SIOCSIWFREQ, &wrq) == -1)
{
2011-03-09 17:42:58 +01:00
perror("Error setting the Wi-Fi channel") ;
return ERR_SETTING_CHANNEL ;
}
close(sockfd) ;
return 0 ;
}
2011-03-09 17:42:58 +01:00
/*
* Switches alternatively the Wi-Fi channel of the IEEE 802.11 interface
* 'iface' to 4 or 11.
*/
int owl_iface_channel_hop(const char *const iface)
{
uint_fast16_t channel ;
struct iwreq wrq ;
int sockfd = iw_sockets_open() ;
strncpy((&wrq)->ifr_name, iface, IFNAMSIZ) ;
if (ioctl(sockfd, SIOCGIWFREQ, &wrq) == -1)
{
perror("Error reading the frequency") ;
return ERR_READING_CHANNEL ;
}
2011-03-09 17:42:58 +01:00
// The following is not very clean: we should use iw_freq2float(),
// iw_freq_to_channel() & friends, cf. /usr/include/{iwlib.h,wireless.h}.
channel = wrq.u.freq.m / 100000 ;
2011-03-09 17:42:58 +01:00
if (channel > 1000) // If the value is in Hz, we convert it to a
2011-03-11 11:19:44 +01:00
channel = owl_frequency_to_channel(channel) ; // channel number
2011-03-09 17:42:58 +01:00
// (with our own function, still not very clean).
close(sockfd) ;
2011-03-09 17:42:58 +01:00
/* Switch the canal */
if (channel == 4) // If channel is 4
2011-03-11 11:19:44 +01:00
return owl_iface_set_channel(iface, 11) ; // switch to 11 ;
else
2011-03-11 11:19:44 +01:00
return owl_iface_set_channel(iface, 4) ; // else, set it to 4.
}
/* *** Signals *** */
2011-03-09 17:42:58 +01:00
/*
* Generic signal handler for SIGINT.
*/
void owl_sigint_handler(const int num)
{
if (num != SIGINT)
{
2011-03-09 17:42:58 +01:00
fprintf(stderr, "Error! The SIGINT handler was called but the"
" signal is not SIGINT.\n") ;
exit(ERR_BAD_SIGNAL) ;
}
2011-03-29 15:44:37 +02:00
owl_run = FALSE ;
#ifdef DEBUG
fprintf(stderr, "\nSignal received: end.\n");
#endif // DEBUG
fflush(NULL) ;
}
/*
* Generic signal handler for SIGTERM.
*/
void owl_sigterm_handler(const int num)
{
if (num != SIGTERM)
{
2011-03-09 17:42:58 +01:00
fprintf(stderr, "Error! The SIGTERM handler was called but the"
" signal is not SIGTERM.\n") ;
exit(ERR_BAD_SIGNAL) ;
}
2011-03-11 11:19:44 +01:00
owl_sigint_handler(SIGINT) ;
}
/* *** Thread-related functions *** */
/*
* Closes the file descriptor 'fd'.
* 'fd' must be passed as an int pointer (int*).
*/
void owl_close_fd(void *const fd)
{
if (fd == NULL)
return ;
const int *const file_desc = fd ;
if (close(*file_desc) != 0)
perror("Error closing file descriptor") ;
#ifdef DEBUG
else
fprintf(stderr, "File descriptor %d closed successfully.\n",
*file_desc) ;
#endif // DEBUG
}
/*
* Closes the stream 'file'.
* 'file' must be passed as a pointer on a pointer of FILE (FILE**).
* If *file either stdout, stderr or stdin, it will not be closed.
*/
void owl_close_file(void *const file)
{
if (file == NULL)
return ;
FILE **stream = file ;
if (*stream == stdout || *stream == stderr || *stream == stdin)
return ;
if (fclose(*stream) != 0)
perror("Error closing stream") ;
#ifdef DEBUG
else
fprintf(stderr, "Stream closed successfully.\n") ;
#endif // DEBUG
}