[lib] mac_bytes_to_string(): do not malloc anymore

owl_mac_bytes_to_string() now uses a static buffer, so the caller do not
need to free the returned buffer manually. Because of this static
buffer, it is not thread-safe anymore, therefore we introduce
owl_mac_bytes_to_string_r() that is a thread-safe version.
This commit is contained in:
Matteo Cypriani 2011-03-25 10:16:41 +01:00
parent e8aa0838f4
commit 4720399513
6 changed files with 59 additions and 68 deletions

9
TODO
View File

@ -9,11 +9,10 @@
That is done in the owlps-positioning C++ code, but not constantly
in C modules.
- Allow to use hostnames instead of IP addresses.
* libowlps
- mac_bytes_to_string(): do not malloc anymore.
- Use struct ether_addr to store MAC addresses?
We could use the struct ether_addr to store binary MAC addresses,
and convert them to strings with ether_ntoa() instead of
owl_mac_bytes_to_string(). See ether_aton(3).
* Aggregator

View File

@ -15,18 +15,30 @@ owl_bool run = TRUE ;
/*
* Converts a MAC address from bytes to string.
* /!\ You *must* manually free the returned 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!
*/
char* owl_mac_bytes_to_string(uint8_t *mac_binary)
const char* owl_mac_bytes_to_string(uint8_t *mac_binary)
{
char *ret = malloc(sizeof(char) * 18) ;
static char mac_str[OWL_ETHER_ADDR_STRLEN] ;
owl_mac_bytes_to_string_r(mac_binary, mac_str) ;
return mac_str ;
}
sprintf(ret, "%02x:%02x:%02x:%02x:%02x:%02x",
/*
* 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(uint8_t *mac_binary,
char mac_str[OWL_ETHER_ADDR_STRLEN])
{
sprintf(mac_str, "%02x:%02x:%02x:%02x:%02x:%02x",
mac_binary[0], mac_binary[1], mac_binary[2],
mac_binary[3], mac_binary[4], mac_binary[5]) ;
ret[17] = '\0' ;
return ret ;
}

View File

@ -226,6 +226,11 @@ typedef struct _owl_autocalibration_order
//#define RTAP_EXT 31
/* Misc. */
// Length of a MAC address in string format (including '\0')
#define OWL_ETHER_ADDR_STRLEN 18
/* Global variables */
extern owl_bool run ;
@ -240,7 +245,9 @@ extern owl_bool run ;
/* Function headers */
// Misc
char* owl_mac_bytes_to_string(uint8_t *mac_binary) ;
const char* owl_mac_bytes_to_string(uint8_t *mac_binary) ;
void owl_mac_bytes_to_string_r(uint8_t *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) ;

View File

@ -381,9 +381,7 @@ int read_loop(int sockfd)
struct sockaddr_in client; // UDP client structure
socklen_t client_len = sizeof(client) ; // Size of clients
owl_captured_request request ; // Message read on the socket
char
*ap_mac_str, // Return pointers for owl_mac_bytes_to_string(),
*mobile_mac_str,
char // Return values of
mobile_ip_str[INET_ADDRSTRLEN], // inet_ntop()
// and owl_timestamp_to_string():
request_time_str[OWL_TIMESTAMP_STR_LEN],
@ -410,10 +408,6 @@ int read_loop(int sockfd)
if (cfg_getbool(cfg, "verbose"))
{
ap_mac_str =
owl_mac_bytes_to_string(request.ap_mac_addr_bytes) ;
mobile_mac_str =
owl_mac_bytes_to_string(request.mobile_mac_addr_bytes) ;
inet_ntop(AF_INET, &request.mobile_ip_addr_bytes,
mobile_ip_str, INET_ADDRSTRLEN) ;
owl_timestamp_to_string(request_time_str,
@ -433,8 +427,8 @@ int read_loop(int sockfd)
"\tPosition Z: %f\n"
"\tDirection: %hhd\n"
,
ap_mac_str,
mobile_mac_str,
owl_mac_bytes_to_string(request.ap_mac_addr_bytes),
owl_mac_bytes_to_string(request.mobile_mac_addr_bytes),
mobile_ip_str,
request_time_str,
start_time_str,
@ -444,18 +438,11 @@ int read_loop(int sockfd)
request.z_position,
request.direction
) ;
free(ap_mac_str) ;
free(mobile_mac_str) ;
}
#ifdef DEBUG
else
{
ap_mac_str =
owl_mac_bytes_to_string(request.ap_mac_addr_bytes) ;
fprintf(stderr, "Request received from AP « %s ».\n",
ap_mac_str) ;
free(ap_mac_str) ;
}
fprintf(stderr, "Request received from AP « %s ».\n",
owl_mac_bytes_to_string(request.ap_mac_addr_bytes)) ;
#endif // DEBUG
got_request(request) ;
@ -476,7 +463,7 @@ void* monitor_requests(void *NULL_value)
request_info_list *request_info_ptr ;
owl_timestamp current_time ;
FILE *fd = NULL ;
char *mac_str ;
char mac_str[OWL_ETHER_ADDR_STRLEN] ;
uint_fast32_t sub ; // owl_time_elapsed_ms() result
#ifdef USE_TIMESTAMP
char request_time_str[OWL_TIMESTAMP_STR_LEN] ;
@ -546,11 +533,10 @@ void* monitor_requests(void *NULL_value)
#endif // DEBUG
// Print mobile MAC address to the output file
mac_str =
owl_mac_bytes_to_string(request_ptr
->mobile_mac_addr_bytes) ;
owl_mac_bytes_to_string_r(request_ptr->
mobile_mac_addr_bytes,
mac_str) ;
fprintf(fd, "%s;", mac_str) ;
free(mac_str) ;
#ifdef USE_TIMESTAMP
// Print request mobile timestamp to the output file
@ -601,13 +587,12 @@ void* monitor_requests(void *NULL_value)
0, (struct sockaddr *)&serv, serv_len) ;
// Print AP info to the output file
mac_str =
owl_mac_bytes_to_string(request_info_ptr
->ap_mac_addr_bytes) ;
owl_mac_bytes_to_string_r(request_info_ptr->
ap_mac_addr_bytes,
mac_str) ;
fprintf(fd, ";%s;%d", mac_str,
request_info_ptr->antenna_signal_dbm
- 0x100) ;
free(mac_str) ;
// Delete request
request_info_ptr = request_info_ptr->next ;
@ -916,9 +901,9 @@ ap_list* find_ap(uint8_t mac_addr_bytes[ETHER_ADDR_LEN])
ap_list* add_ap_front(uint8_t mac_addr_bytes[ETHER_ADDR_LEN])
{
#ifdef DEBUG
char *mac_str = owl_mac_bytes_to_string(mac_addr_bytes) ;
char mac_str[OWL_ETHER_ADDR_STRLEN] ;
owl_mac_bytes_to_string_r(mac_addr_bytes, mac_str) ;
fprintf(stderr, "Creating AP with MAC address « %s »...\n", mac_str) ;
free(mac_str) ;
#endif // DEBUG
ap_list *ap = malloc(sizeof(ap_list)) ;
@ -1028,9 +1013,9 @@ void delete_ap(ap_list *ap)
{
#ifdef DEBUG
assert(ap) ;
char *mac_str = owl_mac_bytes_to_string(token_aps->mac_addr_bytes) ;
char mac_str[OWL_ETHER_ADDR_STRLEN] ;
owl_mac_bytes_to_string_r(token_aps->mac_addr_bytes, mac_str) ;
fprintf(stderr, "Deleting AP « %s »...\n", mac_str) ;
free(mac_str) ;
#endif // DEBUG
unlink_ap(ap) ;
@ -1137,7 +1122,7 @@ void print_request_list()
{
request_list *request_ptr = requests ;
request_info_list *info_ptr = NULL ;
char *mobile_mac_str ;
char mobile_mac_str[OWL_ETHER_ADDR_STRLEN] ;
char
request_time_str[OWL_TIMESTAMP_STR_LEN],
start_time_str[OWL_TIMESTAMP_STR_LEN] ;
@ -1152,8 +1137,8 @@ void print_request_list()
{
info_ptr = request_ptr->info ; // Get the sub-list pointer
mobile_mac_str =
owl_mac_bytes_to_string(request_ptr->mobile_mac_addr_bytes) ;
owl_mac_bytes_to_string_r(request_ptr->mobile_mac_addr_bytes,
mobile_mac_str) ;
owl_timestamp_to_string(request_time_str,
request_ptr->request_time) ;
owl_timestamp_to_string(start_time_str,
@ -1167,7 +1152,6 @@ void print_request_list()
request_time_str,
start_time_str
) ;
free(mobile_mac_str) ;
// Parse information relative to the current request
while (info_ptr != NULL)
@ -1190,18 +1174,17 @@ void print_request_list()
*/
void print_request_info(request_info_list *info)
{
char *ap_mac_str ;
char ap_mac_str[OWL_ETHER_ADDR_STRLEN] ;
if (info == NULL)
return ;
ap_mac_str = owl_mac_bytes_to_string(info->ap_mac_addr_bytes) ;
owl_mac_bytes_to_string_r(info->ap_mac_addr_bytes, ap_mac_str) ;
fprintf(stderr,
"\tAP MAC: %s\n"
"\tSignal strength: %d dBm\n",
ap_mac_str,
info->antenna_signal_dbm - 0x100
) ;
free(ap_mac_str) ;
}
#endif // DEBUG

View File

@ -74,7 +74,6 @@ struct {
int main(int argc, char *argv[])
{
struct sigaction action ; // Signal handler structure
char *mac_string ; // AP MAC (string value)
int ret ; // Program return value
#ifdef USE_PTHREAD
pthread_t
@ -96,9 +95,8 @@ int main(int argc, char *argv[])
sigaction(SIGTERM, &action, NULL) ;
get_mac_addr(GET_WIFI_IFACE(), my_mac_bytes) ;
mac_string = owl_mac_bytes_to_string(my_mac_bytes) ;
printf("My MAC address is: %s\n", mac_string) ;
free(mac_string) ;
printf("My MAC address is: %s\n",
owl_mac_bytes_to_string(my_mac_bytes)) ;
get_ip_addr(GET_WIFI_IFACE(), my_ip) ;
printf("My IP address is: %s\n", my_ip) ;
@ -924,10 +922,6 @@ void read_packet(u_char *args, const struct pcap_pkthdr *header,
/* Display the packet details */
if (GET_DISPLAY_CAPTURED())
{
char *ap_mac_str =
owl_mac_bytes_to_string(request.ap_mac_addr_bytes) ;
char *mobile_mac_str =
owl_mac_bytes_to_string(request.mobile_mac_addr_bytes) ;
char
request_time_str[OWL_TIMESTAMP_STR_LEN],
start_time_str[OWL_TIMESTAMP_STR_LEN] ;
@ -946,8 +940,8 @@ void read_packet(u_char *args, const struct pcap_pkthdr *header,
"\tPosition Z: %f\n"
"\tDirection: %hhd\n"
,
ap_mac_str,
mobile_mac_str,
owl_mac_bytes_to_string(request.ap_mac_addr_bytes),
owl_mac_bytes_to_string(request.mobile_mac_addr_bytes),
request_time_str,
start_time_str,
request.antenna_signal_dbm - 0x100,
@ -956,8 +950,6 @@ void read_packet(u_char *args, const struct pcap_pkthdr *header,
request.z_position,
request.direction
) ;
free(ap_mac_str) ;
free(mobile_mac_str) ;
}
/* Send the request to the aggregator */

View File

@ -105,10 +105,9 @@ const Request& InputUDPSocket::get_next_request()
request.nb_info = ntohs(request.nb_info) ;
// Mobile MAC
char *mac_mobile =
const char *const mac_mobile =
owl_mac_bytes_to_string(request.mobile_mac_addr_bytes) ;
const Mobile &mobile = Stock::find_create_mobile(mac_mobile) ;
free(mac_mobile) ;
current_request->set_mobile(&mobile) ;
// Timestamp
@ -117,7 +116,6 @@ const Request& InputUDPSocket::get_next_request()
// Read {MAC_AP;SS} mcouples (request_info)
unordered_map<string, Measurement> measurements ;
owl_request_info request_info ;
char *mac_ap ;
for (int i = 0 ; i < request.nb_info ; ++i)
{
nread = recvfrom(sockfd, &request_info, sizeof(request_info), 0,
@ -128,12 +126,12 @@ const Request& InputUDPSocket::get_next_request()
current_request->clear() ;
return *current_request ;
}
mac_ap = owl_mac_bytes_to_string(request_info.ap_mac_addr_bytes) ;
const char *const mac_ap =
owl_mac_bytes_to_string(request_info.ap_mac_addr_bytes) ;
const AccessPoint &ap = Stock::find_create_ap(mac_ap) ;
measurements[mac_ap].set_ap(&ap) ;
measurements[mac_ap].add_ss(static_cast<int_fast8_t>
(request_info.antenna_signal_dbm)) ;
free(mac_ap) ;
}
current_request->set_measurements(measurements) ;