diff --git a/libowlps/libowlps.c b/libowlps/libowlps.c index df88676..3cc946c 100644 --- a/libowlps/libowlps.c +++ b/libowlps/libowlps.c @@ -30,7 +30,7 @@ owl_bool owl_run = TRUE ; * each time this function is called. * This function is not thread-safe! */ -const char* owl_mac_bytes_to_string(uint8_t *mac_binary) +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) ; @@ -44,7 +44,7 @@ const char* owl_mac_bytes_to_string(uint8_t *mac_binary) * 'mac_str' must be allocated by the caller. * This function is thread-safe. */ -void owl_mac_bytes_to_string_r(uint8_t *mac_binary, +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", @@ -54,11 +54,27 @@ void owl_mac_bytes_to_string_r(uint8_t *mac_binary, +/* + * 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 ; +} + + + /* * 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(uint_fast16_t channel) +uint_fast8_t owl_frequency_to_channel(const uint_fast16_t channel) { uint_fast8_t c = 0 ; // Result @@ -117,7 +133,7 @@ uint_fast8_t owl_frequency_to_channel(uint_fast16_t channel) * Sets the owl_timestamp 'now' at the current time. * Returns 0 in case of success non-zero otherwise. */ -int owl_timestamp_now(owl_timestamp *now) +int owl_timestamp_now(owl_timestamp *const now) { int ret ; struct timespec now_ts ; @@ -160,14 +176,15 @@ owl_timestamp owl_timeval_to_timestamp(const struct timeval d) -owl_bool owl_timestamp_equals(owl_timestamp d1, owl_timestamp d2) +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(owl_timestamp d) +owl_bool owl_timestamp_is_null(const owl_timestamp d) { return d.tv_sec == 0 && d.tv_nsec == 0 ; } @@ -177,7 +194,7 @@ owl_bool owl_timestamp_is_null(owl_timestamp d) /* * Converts a owl_timestamp date value into milliseconds. */ -uint64_t owl_timestamp_to_ms(owl_timestamp d) +uint64_t owl_timestamp_to_ms(const owl_timestamp d) { return (uint64_t) d.tv_sec * 1000u + (uint64_t) d.tv_nsec / 1000000lu ; } @@ -189,7 +206,7 @@ uint64_t owl_timestamp_to_ms(owl_timestamp d) * 'dst' must be an allocated array of at least owl_timestamp_STR_LEN * characters. */ -void owl_timestamp_to_string(char *dst, owl_timestamp src) +void owl_timestamp_to_string(char *const dst, const owl_timestamp src) { snprintf(dst, OWL_TIMESTAMP_STR_LEN, "%"PRIu32".%"PRIu32, src.tv_sec, src.tv_nsec) ; @@ -229,12 +246,12 @@ owl_timestamp owl_time_elapsed(const owl_timestamp d1, /* * Converts a owl_timestamp from host endianess to network endianess. */ -owl_timestamp owl_hton_timestamp(owl_timestamp date) +owl_timestamp owl_hton_timestamp(const owl_timestamp d) { - owl_timestamp d ; - d.tv_sec = htonl(date.tv_sec) ; - d.tv_nsec = htonl(date.tv_nsec) ; - return d ; + owl_timestamp date ; + date.tv_sec = htonl(d.tv_sec) ; + date.tv_nsec = htonl(d.tv_nsec) ; + return date ; } @@ -242,27 +259,12 @@ owl_timestamp owl_hton_timestamp(owl_timestamp date) /* * Converts a owl_timestamp from network endianess to host endianess. */ -owl_timestamp owl_ntoh_timestamp(owl_timestamp date) +owl_timestamp owl_ntoh_timestamp(const owl_timestamp d) { - owl_timestamp d ; - d.tv_sec = ntohl(date.tv_sec) ; - d.tv_nsec = ntohl(date.tv_nsec) ; - return d ; -} - - - -/* - * Compares two MAC addresses. - * Returns TRUE if they are identical, FALSE otherwise. - */ -owl_bool owl_mac_equals(uint8_t *mac1, uint8_t *mac2) -{ - int i ; - for (i = 0 ; i < ETHER_ADDR_LEN ; ++i) - if(mac1[i] != mac2[i]) - return FALSE ; - return TRUE ; + owl_timestamp date ; + date.tv_sec = ntohl(d.tv_sec) ; + date.tv_nsec = ntohl(d.tv_nsec) ; + return date ; } @@ -277,10 +279,10 @@ owl_bool owl_mac_equals(uint8_t *mac1, uint8_t *mac2) * - client_description (in/out): the structure in which the client * description will be saved. */ -int owl_create_udp_trx_socket(char *server_address, - uint_fast16_t server_port, - struct sockaddr_in *server_description, - struct sockaddr_in *client_description) +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) { int sockfd ; // Socket descriptor @@ -315,7 +317,7 @@ int owl_create_udp_trx_socket(char *server_address, * Parameters: * - port: port on which the socket listens. */ -int owl_create_udp_listening_socket(uint_fast16_t port) +int owl_create_udp_listening_socket(const uint_fast16_t port) { int sockfd ; // Socket descriptor struct sockaddr_in server_description ; // Server structure @@ -354,7 +356,7 @@ int owl_create_udp_listening_socket(uint_fast16_t port) /* * Switches the IEEE 802.11 interface 'iface' to Monitor mode. */ -int owl_iface_mode_monitor(char *iface) +int owl_iface_mode_monitor(const char *const iface) { struct iwreq wrq ; int sockfd = iw_sockets_open() ; @@ -389,7 +391,8 @@ int owl_iface_mode_monitor(char *iface) * Sets the IEEE 802.11 channel of the interface 'iface'. * 'channel' must be an integer between 1 and 14. */ -int owl_iface_set_channel(char *iface, uint_fast8_t channel) +int owl_iface_set_channel(const char *const iface, + const uint_fast8_t channel) { struct iwreq wrq ; int sockfd = iw_sockets_open() ; @@ -415,7 +418,7 @@ int owl_iface_set_channel(char *iface, uint_fast8_t channel) * Switches alternatively the Wi-Fi channel of the IEEE 802.11 interface * 'iface' to 4 or 11. */ -int owl_iface_channel_hop(char *iface) +int owl_iface_channel_hop(const char *const iface) { uint_fast16_t channel ; struct iwreq wrq ; @@ -450,7 +453,7 @@ int owl_iface_channel_hop(char *iface) /* * Generic signal handler for SIGINT. */ -void owl_sigint_handler(int num) +void owl_sigint_handler(const int num) { if (num != SIGINT) { @@ -470,7 +473,7 @@ void owl_sigint_handler(int num) /* Gestionnaire de signal générique pour SIGTERM */ -void owl_sigterm_handler(int num) +void owl_sigterm_handler(const int num) { if (num != SIGTERM) { @@ -488,12 +491,12 @@ void owl_sigterm_handler(int num) * Closes the file descriptor 'fd'. * 'fd' must be passed as an int pointer (int*). */ -void owl_close_fd(void *fd) +void owl_close_fd(void *const fd) { if (fd == NULL) return ; - int *file_desc = fd ; + const int *const file_desc = fd ; if (close(*file_desc) != 0) perror("Error closing file descriptor") ; #ifdef DEBUG @@ -510,7 +513,7 @@ void owl_close_fd(void *fd) * '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 *file) +void owl_close_file(void *const file) { if (file == NULL) return ; diff --git a/libowlps/owlps.h b/libowlps/owlps.h index b28e324..a8dda9b 100644 --- a/libowlps/owlps.h +++ b/libowlps/owlps.h @@ -232,44 +232,47 @@ extern owl_bool owl_run ; /* Function headers */ // Misc -const char* owl_mac_bytes_to_string(uint8_t *mac_binary) ; -void owl_mac_bytes_to_string_r(uint8_t *mac_binary, +const char* owl_mac_bytes_to_string(const uint8_t *const mac_binary) ; +void owl_mac_bytes_to_string_r(const uint8_t *const mac_binary, char mac_str[OWL_ETHER_ADDR_STRLEN]) ; -owl_bool owl_mac_equals(uint8_t *mac1, uint8_t *mac2) ; -uint_fast8_t owl_frequency_to_channel(uint_fast16_t channel) ; +owl_bool owl_mac_equals(const uint8_t *const mac1, + const uint8_t *const mac2) ; +uint_fast8_t owl_frequency_to_channel(const uint_fast16_t channel) ; // Time -int owl_timestamp_now(owl_timestamp *now) ; +int owl_timestamp_now(owl_timestamp *const now) ; owl_timestamp owl_timespec_to_timestamp(const struct timespec d) ; owl_timestamp owl_timeval_to_timestamp(const struct timeval d) ; -void owl_timestamp_to_string(char *dst, owl_timestamp src) ; -owl_bool owl_timestamp_equals(owl_timestamp d1, owl_timestamp d2) ; -owl_bool owl_timestamp_is_null(owl_timestamp d) ; -uint64_t owl_timestamp_to_ms(owl_timestamp date) ; +owl_bool owl_timestamp_equals(const owl_timestamp d1, + const owl_timestamp d2) ; +owl_bool owl_timestamp_is_null(const owl_timestamp d) ; +uint64_t owl_timestamp_to_ms(const owl_timestamp d) ; +void owl_timestamp_to_string(char *const dst, const owl_timestamp src) ; uint_fast32_t owl_time_elapsed_ms(const owl_timestamp d1, const owl_timestamp d2) ; owl_timestamp owl_time_elapsed(const owl_timestamp d1, const owl_timestamp d2) ; -owl_timestamp owl_hton_timestamp(owl_timestamp date) ; -owl_timestamp owl_ntoh_timestamp(owl_timestamp date) ; +owl_timestamp owl_hton_timestamp(const owl_timestamp d) ; +owl_timestamp owl_ntoh_timestamp(const owl_timestamp d) ; // Network -int owl_create_udp_trx_socket(char *server_address, - uint_fast16_t server_port, - struct sockaddr_in *server_description, - struct sockaddr_in *client_description) ; -int owl_create_udp_listening_socket(uint_fast16_t port) ; -int owl_iface_mode_monitor(char *iface) ; -int owl_iface_set_channel(char *iface, uint_fast8_t channel) ; -int owl_iface_channel_hop(char *iface) ; +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) ; +int owl_create_udp_listening_socket(const uint_fast16_t port) ; +int owl_iface_mode_monitor(const char *const iface) ; +int owl_iface_set_channel(const char *const iface, + const uint_fast8_t channel) ; +int owl_iface_channel_hop(const char *const iface) ; // Signals -void owl_sigint_handler(int num) ; -void owl_sigterm_handler(int num) ; +void owl_sigint_handler(const int num) ; +void owl_sigterm_handler(const int num) ; // Threads -void owl_close_fd(void *fd) ; -void owl_close_file(void *file) ; +void owl_close_fd(void *const fd) ; +void owl_close_file(void *const file) ; /* Macros */