Unchecked ioctl Return Value
This commit is contained in:
@ -16,6 +16,10 @@
|
|||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
#include <ifaddrs.h>
|
||||||
|
#include <netpacket/packet.h>
|
||||||
|
#include <cstring>
|
||||||
|
#include <string>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
|
|||||||
@ -143,24 +143,44 @@ string Logger::getTimestamp()
|
|||||||
* \param void
|
* \param void
|
||||||
* \return string MAC address (formatted)
|
* \return string MAC address (formatted)
|
||||||
*/
|
*/
|
||||||
string Logger::getMacAddress()
|
std::string Logger::getMacAddress()
|
||||||
{
|
{
|
||||||
struct ifreq ifr;
|
struct ifaddrs *ifaddr, *ifa;
|
||||||
int s = socket(AF_INET, SOCK_STREAM, 0);
|
|
||||||
|
|
||||||
strcpy(ifr.ifr_name, "eth0");
|
// default MAC if none found
|
||||||
if (ioctl(s, SIOCGIFHWADDR, &ifr) < 0)
|
std::string result = "00:00:00:00:00:00";
|
||||||
|
|
||||||
|
if (getifaddrs(&ifaddr) == -1)
|
||||||
|
return result;
|
||||||
|
|
||||||
|
for (ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next)
|
||||||
{
|
{
|
||||||
strcpy(ifr.ifr_name, "eno1");
|
if (!ifa->ifa_addr)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// We want AF_PACKET interfaces (Ethernet)
|
||||||
|
if (ifa->ifa_addr->sa_family == AF_PACKET &&
|
||||||
|
!(ifa->ifa_flags & IFF_LOOPBACK) && // skip loopback interface
|
||||||
|
(ifa->ifa_flags & IFF_UP)) // must be up
|
||||||
|
{
|
||||||
|
struct sockaddr_ll *s = (struct sockaddr_ll *)ifa->ifa_addr;
|
||||||
|
|
||||||
|
if (s->sll_halen == 6)
|
||||||
|
{
|
||||||
|
char buf[32];
|
||||||
|
snprintf(buf, sizeof(buf),
|
||||||
|
"%02X:%02X:%02X:%02X:%02X:%02X",
|
||||||
|
s->sll_addr[0], s->sll_addr[1], s->sll_addr[2],
|
||||||
|
s->sll_addr[3], s->sll_addr[4], s->sll_addr[5]);
|
||||||
|
|
||||||
|
freeifaddrs(ifaddr);
|
||||||
|
return std::string(buf);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char *hwaddr = (unsigned char *)ifr.ifr_hwaddr.sa_data;
|
freeifaddrs(ifaddr);
|
||||||
char buffer[80];
|
return result;
|
||||||
snprintf(buffer, sizeof(buffer), "%02X:%02X:%02X:%02X:%02X:%02X", hwaddr[0], hwaddr[1], hwaddr[2],
|
|
||||||
hwaddr[3], hwaddr[4], hwaddr[5]);
|
|
||||||
close(s);
|
|
||||||
string tmp = buffer;
|
|
||||||
return tmp;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user