2008-02-14 08:54:51 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the rtap localisation project.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2008-10-06 17:27:33 +02:00
|
|
|
#include "owlps.h"
|
2008-02-14 08:54:51 +01:00
|
|
|
|
|
|
|
|
2008-03-14 11:31:04 +01:00
|
|
|
#define DEBUG
|
|
|
|
|
|
|
|
|
2011-03-18 15:15:39 +01:00
|
|
|
owl_bool run = TRUE ;
|
2008-02-14 08:54:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2011-03-09 17:42:58 +01:00
|
|
|
/*
|
|
|
|
* Converts a MAC address from bytes to string.
|
2011-03-15 13:50:00 +01:00
|
|
|
* /!\ You *must* manually free the returned string. /!\
|
2008-02-14 08:54:51 +01:00
|
|
|
*/
|
2011-03-11 11:19:44 +01:00
|
|
|
char* owl_mac_bytes_to_string(uint8_t *mac_binary)
|
2008-02-14 08:54:51 +01:00
|
|
|
{
|
|
|
|
char *ret = malloc(sizeof(char) * 18) ;
|
|
|
|
|
2011-03-09 17:42:58 +01:00
|
|
|
sprintf(ret, "%02x:%02x:%02x:%02x:%02x:%02x",
|
|
|
|
mac_binary[0], mac_binary[1], mac_binary[2],
|
|
|
|
mac_binary[3], mac_binary[4], mac_binary[5]) ;
|
2008-02-14 08:54:51 +01:00
|
|
|
ret[17] = '\0' ;
|
|
|
|
|
|
|
|
return ret ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2011-03-11 11:19:44 +01:00
|
|
|
uint_fast8_t owl_frequency_to_channel(uint_fast16_t channel)
|
2008-02-14 08:54:51 +01:00
|
|
|
{
|
2011-03-10 19:18:43 +01:00
|
|
|
uint_fast8_t c = 0 ; // Result
|
2008-02-14 08:54:51 +01:00
|
|
|
|
|
|
|
switch (channel)
|
|
|
|
{
|
2008-02-26 16:51:49 +01:00
|
|
|
case CHANNEL_1 :
|
2008-02-14 08:54:51 +01:00
|
|
|
c = 1 ;
|
|
|
|
break ;
|
2008-02-26 16:51:49 +01:00
|
|
|
case CHANNEL_2 :
|
2008-02-14 08:54:51 +01:00
|
|
|
c = 2 ;
|
|
|
|
break ;
|
2008-02-26 16:51:49 +01:00
|
|
|
case CHANNEL_3 :
|
2008-02-14 08:54:51 +01:00
|
|
|
c = 3 ;
|
|
|
|
break ;
|
2008-02-26 16:51:49 +01:00
|
|
|
case CHANNEL_4 :
|
2008-02-14 08:54:51 +01:00
|
|
|
c = 4 ;
|
|
|
|
break ;
|
2008-02-26 16:51:49 +01:00
|
|
|
case CHANNEL_5 :
|
2008-02-14 08:54:51 +01:00
|
|
|
c = 5 ;
|
|
|
|
break ;
|
2008-02-26 16:51:49 +01:00
|
|
|
case CHANNEL_6 :
|
2008-02-14 08:54:51 +01:00
|
|
|
c = 6 ;
|
|
|
|
break ;
|
2008-02-26 16:51:49 +01:00
|
|
|
case CHANNEL_7 :
|
2008-02-14 08:54:51 +01:00
|
|
|
c = 7 ;
|
|
|
|
break ;
|
2008-02-26 16:51:49 +01:00
|
|
|
case CHANNEL_8 :
|
2008-02-14 08:54:51 +01:00
|
|
|
c = 8 ;
|
|
|
|
break ;
|
2008-02-26 16:51:49 +01:00
|
|
|
case CHANNEL_9 :
|
2008-02-14 08:54:51 +01:00
|
|
|
c = 9 ;
|
|
|
|
break ;
|
2008-02-26 16:51:49 +01:00
|
|
|
case CHANNEL_10 :
|
2008-02-14 08:54:51 +01:00
|
|
|
c = 10 ;
|
|
|
|
break ;
|
2008-02-26 16:51:49 +01:00
|
|
|
case CHANNEL_11 :
|
2008-02-14 08:54:51 +01:00
|
|
|
c = 11 ;
|
|
|
|
break ;
|
2008-02-26 16:51:49 +01:00
|
|
|
case CHANNEL_12 :
|
2008-02-14 08:54:51 +01:00
|
|
|
c = 12 ;
|
|
|
|
break ;
|
2008-02-26 16:51:49 +01:00
|
|
|
case CHANNEL_13 :
|
2008-02-14 08:54:51 +01:00
|
|
|
c = 13 ;
|
|
|
|
break ;
|
2008-02-26 16:51:49 +01:00
|
|
|
case CHANNEL_14 :
|
2008-02-14 08:54:51 +01:00
|
|
|
c = 14 ;
|
|
|
|
break ;
|
|
|
|
}
|
|
|
|
|
|
|
|
return c ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-03-10 15:04:12 +01:00
|
|
|
/*
|
2011-03-18 15:15:39 +01:00
|
|
|
* Sets the owl_timestamp 'now' at the current time.
|
2011-03-15 13:48:16 +01:00
|
|
|
* Returns 0 in case of success non-zero otherwise.
|
2011-03-10 15:04:12 +01:00
|
|
|
*/
|
2011-03-18 15:15:39 +01:00
|
|
|
int owl_timestamp_now(owl_timestamp *now)
|
2008-02-14 08:54:51 +01:00
|
|
|
{
|
2011-03-10 19:38:27 +01:00
|
|
|
int ret ;
|
2011-03-14 11:31:38 +01:00
|
|
|
struct timespec now_ts ;
|
|
|
|
if ((ret = clock_gettime(CLOCK_REALTIME, &now_ts)))
|
2011-03-10 15:51:42 +01:00
|
|
|
{
|
|
|
|
perror("Cannot get the current time") ;
|
2011-03-10 19:38:27 +01:00
|
|
|
return ret ;
|
2011-03-10 15:51:42 +01:00
|
|
|
}
|
2011-03-14 11:31:38 +01:00
|
|
|
|
|
|
|
*now = owl_timespec_to_timestamp(now_ts) ;
|
|
|
|
|
2011-03-10 19:38:27 +01:00
|
|
|
return 0 ;
|
2008-02-14 08:54:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-03-14 11:31:38 +01:00
|
|
|
/*
|
2011-03-18 15:15:39 +01:00
|
|
|
* Returns a owl_timestamp from a struct timespec.
|
2011-03-14 11:31:38 +01:00
|
|
|
*/
|
2011-03-18 15:15:39 +01:00
|
|
|
owl_timestamp owl_timespec_to_timestamp(const struct timespec d)
|
2011-03-14 11:31:38 +01:00
|
|
|
{
|
2011-03-18 15:15:39 +01:00
|
|
|
owl_timestamp res ;
|
2011-03-14 11:31:38 +01:00
|
|
|
res.tv_sec = d.tv_sec ;
|
|
|
|
res.tv_nsec = d.tv_nsec ;
|
|
|
|
return res ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-03-10 15:51:42 +01:00
|
|
|
/*
|
2011-03-18 15:15:39 +01:00
|
|
|
* Returns a owl_timestamp from a struct timeval.
|
2011-03-10 15:51:42 +01:00
|
|
|
*/
|
2011-03-18 15:15:39 +01:00
|
|
|
owl_timestamp owl_timeval_to_timestamp(const struct timeval d)
|
2011-03-10 15:51:42 +01:00
|
|
|
{
|
2011-03-18 15:15:39 +01:00
|
|
|
owl_timestamp res ;
|
2011-03-10 15:51:42 +01:00
|
|
|
res.tv_sec = d.tv_sec ;
|
2011-03-14 14:30:44 +01:00
|
|
|
res.tv_nsec = d.tv_usec * 1000u ;
|
2011-03-10 15:51:42 +01:00
|
|
|
return res ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2011-03-18 15:15:39 +01:00
|
|
|
* Converts a owl_timestamp date value into milliseconds.
|
2011-03-10 15:51:42 +01:00
|
|
|
*/
|
2011-03-18 15:15:39 +01:00
|
|
|
uint64_t owl_timestamp_to_ms(owl_timestamp d)
|
2010-01-11 19:40:45 +01:00
|
|
|
{
|
2011-03-18 15:15:39 +01:00
|
|
|
return (uint64_t) d.tv_sec * 1000u + (uint64_t) d.tv_nsec / 1000000lu ;
|
2010-01-11 19:40:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-03-15 13:50:00 +01:00
|
|
|
/*
|
2011-03-18 15:15:39 +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.
|
|
|
|
*/
|
2011-03-18 15:15:39 +01:00
|
|
|
void owl_timestamp_to_string(char *dst, owl_timestamp src)
|
2011-03-15 13:50:00 +01:00
|
|
|
{
|
2011-03-18 15:15:39 +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.
|
|
|
|
*/
|
2011-03-18 15:15:39 +01:00
|
|
|
uint_fast32_t owl_time_elapsed_ms(const owl_timestamp d1,
|
|
|
|
const owl_timestamp d2)
|
2008-02-14 08:54:51 +01:00
|
|
|
{
|
2011-03-15 13:58:39 +01:00
|
|
|
return owl_timestamp_to_ms(owl_time_elapsed(d1, d2)) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2011-03-18 15:15:39 +01:00
|
|
|
* Returns a owl_timestamp storing the time between two dates.
|
2011-03-15 13:58:39 +01:00
|
|
|
*/
|
2011-03-18 15:15:39 +01:00
|
|
|
owl_timestamp owl_time_elapsed(const owl_timestamp d1,
|
|
|
|
const owl_timestamp d2)
|
2011-03-15 13:58:39 +01:00
|
|
|
{
|
2011-03-18 15:15:39 +01:00
|
|
|
owl_timestamp elapsed ;
|
2011-03-15 13:58:39 +01:00
|
|
|
elapsed.tv_sec = abs(d1.tv_sec - d2.tv_sec) ;
|
|
|
|
elapsed.tv_nsec = abs(d1.tv_nsec - d2.tv_nsec) ;
|
2008-03-14 11:31:04 +01:00
|
|
|
#ifdef DEBUG
|
2011-03-24 09:00:57 +01:00
|
|
|
fprintf(stderr, "time_elapsed(): %"PRIu64"ms\n",
|
|
|
|
owl_timestamp_to_ms(elapsed)) ;
|
2008-03-14 11:31:04 +01:00
|
|
|
#endif
|
2011-03-15 13:58:39 +01:00
|
|
|
return elapsed ;
|
2008-02-14 08:54:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-03-14 11:20:57 +01:00
|
|
|
/*
|
2011-03-18 15:15:39 +01:00
|
|
|
* Converts a owl_timestamp from host endianess to network endianess.
|
2011-03-14 11:20:57 +01:00
|
|
|
*/
|
2011-03-18 15:15:39 +01:00
|
|
|
owl_timestamp owl_hton_timestamp(owl_timestamp date)
|
2011-03-14 11:20:57 +01:00
|
|
|
{
|
2011-03-18 15:15:39 +01:00
|
|
|
owl_timestamp d ;
|
2011-03-14 11:20:57 +01:00
|
|
|
d.tv_sec = htonl(date.tv_sec) ;
|
|
|
|
d.tv_nsec = htonl(date.tv_nsec) ;
|
|
|
|
return d ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2011-03-18 15:15:39 +01:00
|
|
|
* Converts a owl_timestamp from network endianess to host endianess.
|
2011-03-14 11:20:57 +01:00
|
|
|
*/
|
2011-03-18 15:15:39 +01:00
|
|
|
owl_timestamp owl_ntoh_timestamp(owl_timestamp date)
|
2011-03-14 11:20:57 +01:00
|
|
|
{
|
2011-03-18 15:15:39 +01:00
|
|
|
owl_timestamp d ;
|
2011-03-14 11:20:57 +01:00
|
|
|
d.tv_sec = ntohl(date.tv_sec) ;
|
|
|
|
d.tv_nsec = ntohl(date.tv_nsec) ;
|
|
|
|
return d ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-03-09 17:42:58 +01:00
|
|
|
/*
|
|
|
|
* Compares two MAC addresses.
|
|
|
|
* Returns TRUE if they are identical, FALSE otherwise.
|
|
|
|
*/
|
2011-03-18 15:15:39 +01:00
|
|
|
owl_bool owl_mac_equals(uint8_t *mac1, uint8_t *mac2)
|
2008-02-14 08:54:51 +01:00
|
|
|
{
|
|
|
|
int i ;
|
|
|
|
for(i=0 ; i < 6 ; i++)
|
|
|
|
if(mac1[i] != mac2[i])
|
|
|
|
return FALSE ;
|
|
|
|
return TRUE ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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.
|
2008-02-14 08:54:51 +01:00
|
|
|
*/
|
2011-03-11 11:32:17 +01:00
|
|
|
int owl_create_udp_trx_socket(char *server_address,
|
|
|
|
uint_fast16_t server_port,
|
|
|
|
struct sockaddr_in *server_description,
|
|
|
|
struct sockaddr_in *client_description)
|
2008-02-14 08:54:51 +01:00
|
|
|
{
|
2011-03-09 17:42:58 +01:00
|
|
|
int sockfd ; // Socket descriptor
|
2008-02-14 08:54:51 +01:00
|
|
|
|
2011-03-09 17:42:58 +01:00
|
|
|
/* Ceate the UDP socket */
|
2008-02-14 08:54:51 +01:00
|
|
|
sockfd = socket(AF_INET, SOCK_DGRAM, 0) ;
|
|
|
|
if (sockfd < 0)
|
|
|
|
{
|
2011-03-09 17:42:58 +01:00
|
|
|
perror("UDP socket creation failed") ;
|
2008-02-26 16:51:49 +01:00
|
|
|
return sockfd ;
|
2008-02-14 08:54:51 +01:00
|
|
|
}
|
|
|
|
|
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 ;
|
2008-02-14 08:54:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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.
|
2008-02-14 08:54:51 +01:00
|
|
|
*/
|
2011-03-11 11:19:44 +01:00
|
|
|
int owl_create_udp_listening_socket(uint_fast16_t port)
|
2008-02-14 08:54:51 +01:00
|
|
|
{
|
2011-03-09 17:42:58 +01:00
|
|
|
int sockfd ; // Socket descriptor
|
|
|
|
struct sockaddr_in server_description ; // Server structure
|
|
|
|
int ret = 0 ; // Return value
|
2008-02-14 08:54:51 +01:00
|
|
|
|
2011-03-09 17:42:58 +01:00
|
|
|
/* Create the UDP socket */
|
2008-02-14 08:54:51 +01:00
|
|
|
sockfd = socket(AF_INET, SOCK_DGRAM, 0) ;
|
|
|
|
if (sockfd < 0)
|
|
|
|
{
|
2011-03-09 17:42:58 +01:00
|
|
|
perror("UDP socket creation failed") ;
|
2008-02-26 16:51:49 +01:00
|
|
|
return sockfd ;
|
2008-02-14 08:54:51 +01:00
|
|
|
}
|
|
|
|
|
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
|
2008-02-14 08:54:51 +01:00
|
|
|
|
2011-03-09 17:42:58 +01:00
|
|
|
/* Port reservation */
|
|
|
|
ret = bind(sockfd, (struct sockaddr*) &server_description,
|
|
|
|
sizeof(server_description)) ;
|
2008-02-14 08:54:51 +01:00
|
|
|
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) ;
|
2008-02-14 08:54:51 +01:00
|
|
|
return ret ;
|
|
|
|
}
|
|
|
|
|
2011-03-09 17:42:58 +01:00
|
|
|
return sockfd ;
|
2008-02-14 08:54:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-03-09 17:42:58 +01:00
|
|
|
/*
|
|
|
|
* Switches the IEEE 802.11 interface 'iface' to Monitor mode.
|
|
|
|
*/
|
2011-03-11 11:19:44 +01:00
|
|
|
int owl_iface_mode_monitor(char *iface)
|
2008-02-26 16:51:49 +01:00
|
|
|
{
|
|
|
|
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
|
2008-02-27 17:37:08 +01:00
|
|
|
{
|
2010-08-05 17:03:21 +02:00
|
|
|
perror("Error reading interface mode") ;
|
2008-02-27 17:37:08 +01:00
|
|
|
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)
|
2008-02-26 16:51:49 +01:00
|
|
|
{
|
2008-02-27 17:37:08 +01:00
|
|
|
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 ;
|
|
|
|
}
|
2008-02-26 16:51:49 +01:00
|
|
|
}
|
|
|
|
|
2008-03-14 11:31:04 +01:00
|
|
|
close(sockfd) ;
|
|
|
|
|
2008-02-26 16:51:49 +01:00
|
|
|
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.
|
|
|
|
*/
|
2011-03-11 11:19:44 +01:00
|
|
|
int owl_iface_set_channel(char *iface, uint_fast8_t channel)
|
2008-02-26 16:51:49 +01:00
|
|
|
{
|
|
|
|
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") ;
|
2008-02-26 16:51:49 +01:00
|
|
|
return ERR_SETTING_CHANNEL ;
|
|
|
|
}
|
|
|
|
|
2008-03-14 11:31:04 +01:00
|
|
|
close(sockfd) ;
|
|
|
|
|
2008-02-26 16:51:49 +01:00
|
|
|
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.
|
|
|
|
*/
|
2011-03-11 11:19:44 +01:00
|
|
|
int owl_iface_channel_hop(char *iface)
|
2008-02-26 16:51:49 +01:00
|
|
|
{
|
2011-03-10 19:18:43 +01:00
|
|
|
uint_fast16_t channel ;
|
2008-02-26 16:51:49 +01:00
|
|
|
struct iwreq wrq ;
|
|
|
|
int sockfd = iw_sockets_open() ;
|
|
|
|
|
|
|
|
strncpy((&wrq)->ifr_name, iface, IFNAMSIZ) ;
|
|
|
|
|
|
|
|
if (ioctl(sockfd, SIOCGIWFREQ, &wrq) == -1)
|
|
|
|
{
|
|
|
|
perror("Erreur lors de la lecture de la fréquence ") ;
|
|
|
|
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 ;
|
2008-02-26 16:51:49 +01:00
|
|
|
|
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).
|
2008-02-26 16:51:49 +01:00
|
|
|
|
2008-03-14 11:31:04 +01:00
|
|
|
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 ;
|
2008-02-26 16:51:49 +01:00
|
|
|
else
|
2011-03-11 11:19:44 +01:00
|
|
|
return owl_iface_set_channel(iface, 4) ; // else, set it to 4.
|
2008-02-26 16:51:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-03-09 17:42:58 +01:00
|
|
|
/*
|
|
|
|
* Generic signal handler for SIGINT.
|
|
|
|
*/
|
2011-03-11 11:19:44 +01:00
|
|
|
void owl_sigint_handler(int num)
|
2008-02-14 08:54:51 +01:00
|
|
|
{
|
|
|
|
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") ;
|
2008-02-26 16:51:49 +01:00
|
|
|
exit(ERR_BAD_SIGNAL) ;
|
2008-02-14 08:54:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
run = FALSE ;
|
|
|
|
|
2011-03-10 11:11:55 +01:00
|
|
|
#ifdef DEBUG
|
2011-03-24 09:00:57 +01:00
|
|
|
fprintf(stderr, "\nSignal received: end.\n");
|
2011-03-10 11:11:55 +01:00
|
|
|
#endif // DEBUG
|
2008-02-14 08:54:51 +01:00
|
|
|
fflush(NULL) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-11-06 08:41:32 +01:00
|
|
|
/* Gestionnaire de signal générique pour SIGTERM */
|
2011-03-11 11:19:44 +01:00
|
|
|
void owl_sigterm_handler(int num)
|
2008-02-14 08:54:51 +01:00
|
|
|
{
|
|
|
|
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") ;
|
2008-02-26 16:51:49 +01:00
|
|
|
exit(ERR_BAD_SIGNAL) ;
|
2008-02-14 08:54:51 +01:00
|
|
|
}
|
|
|
|
|
2011-03-11 11:19:44 +01:00
|
|
|
owl_sigint_handler(SIGINT) ;
|
2008-02-14 08:54:51 +01:00
|
|
|
}
|