[Listener] Improve *_addr()

Improve the code of the get_mac_addr() and get_ip_addr() functions,
their signature and documentation.
This commit is contained in:
Matteo Cypriani 2013-06-14 16:21:46 -04:00
parent 4ae0874e1f
commit 14e9a4122a
2 changed files with 27 additions and 19 deletions

View File

@ -193,8 +193,9 @@ bool extract_radiotap_ss(const u_char *pkt_data,
uint_fast16_t nat_align(uint_fast16_t offset, uint_fast8_t field_len) ;
void display_captured_request(owl_captured_request *request,
const struct pcap_pkthdr *pkt_header) ;
void get_mac_addr(char *eth, uint8_t mac_bytes[ETHER_ADDR_LEN]) ;
void get_ip_addr(char *eth, char *ip_bytes) ;
void get_mac_addr(const char *const iface,
uint8_t mac_bytes[ETHER_ADDR_LEN]) ;
void get_ip_addr(const char *const iface, char *ip_bytes) ;
#ifdef OWLPS_LISTENER_USES_PTHREAD
void* autocalibrate(void *NULL_value) ;

View File

@ -858,7 +858,7 @@ void print_configuration(FILE *stream)
#ifdef OWLPS_LISTENER_KEEPS_MONITOR
/*
* Thread function. Switches interface 'iface' to monitor mode every
* second.
* second. `iface` must be passed as a const char *const.
*/
void* keep_mode_monitor(void *iface)
{
@ -1376,9 +1376,11 @@ void display_captured_request(owl_captured_request *request,
/*
* Get our own MAC address and copy it to 'mac_bytes'.
* Gets the MAC address of the interface `iface` and copies it to
* `mac_bytes`.
*/
void get_mac_addr(char *eth, uint8_t mac_bytes[ETHER_ADDR_LEN])
void get_mac_addr(const char *const iface,
uint8_t mac_bytes[ETHER_ADDR_LEN])
{
struct ifreq ifr;
int sockfd ;
@ -1388,15 +1390,18 @@ void get_mac_addr(char *eth, uint8_t mac_bytes[ETHER_ADDR_LEN])
sockfd = socket(AF_INET, SOCK_DGRAM, 0) ;
if(sockfd < 0)
perror("Cannot open socket to read MAC address") ;
{
perror("Cannot open socket to read MAC address") ;
return ;
}
strncpy(ifr.ifr_name, eth, IFNAMSIZ) ;
if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0)
return ;
strncpy(ifr.ifr_name, iface, IFNAMSIZ) ;
if (ioctl(sockfd, SIOCGIFHWADDR, &ifr) < 0)
return ;
{
perror("ioctl(SIOCGIFHWADDR) error getting MAC address") ;
return ;
}
memcpy(mac_bytes, ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN) ;
}
@ -1404,9 +1409,9 @@ void get_mac_addr(char *eth, uint8_t mac_bytes[ETHER_ADDR_LEN])
/*
* Get our own IP address and copy it to 'ip'.
* Gets the IP address of the interface `iface` and copies it to `ip`.
*/
void get_ip_addr(char *eth, char ip[INET_ADDRSTRLEN])
void get_ip_addr(const char *const iface, char ip[INET_ADDRSTRLEN])
{
struct ifreq ifr;
int sockfd ;
@ -1417,17 +1422,19 @@ void get_ip_addr(char *eth, char ip[INET_ADDRSTRLEN])
if(sockfd < 0)
perror("Cannot open socket to read IP address") ;
strncpy(ifr.ifr_name, eth, IFNAMSIZ) ;
if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0)
return ;
strncpy(ifr.ifr_name, iface, IFNAMSIZ) ;
if (ioctl(sockfd, SIOCGIFADDR, &ifr) < 0)
return ;
{
perror("ioctl(SIOCGIFADDR) error getting IP address") ;
return ;
}
memcpy(&ip_addr, &ifr.ifr_addr.sa_data[sizeof(sa.sin_port)],
sizeof(ip_addr)) ;
inet_ntop(AF_INET, &ip_addr, ip, INET_ADDRSTRLEN) ;
if (inet_ntop(AF_INET, &ip_addr, ip, INET_ADDRSTRLEN) == NULL)
perror("inet_ntop() error getting IP address") ;
}