Is there a way to get interface's MAC address via getifaddrs()
?
I already have this, to get IP addresses, but I have kinda missed MAC
. Ive tried to look for the information in getifaddrs()
, but there is nothing about MAC
addresses
struct ifaddrs *iflist, *iface;
if (getifaddrs(&iflist) < 0)
{
perror("getifaddrs");
}
char addrp[INET6_ADDRSTRLEN];
char macp[INET6_ADDRSTRLEN];
int i=0;
for (iface = iflist; iface; iface = iface->ifa_next)
{
int af = iface->ifa_addr->sa_family;
const void *addr;
const void *mac;
switch (af)
{
case AF_INET:
addr = &((struct sockaddr_in *)iface->ifa_addr)->sin_addr;
break;
//get mac address somehow?
default:
addr = NULL;
}
if (addr)
{
if (inet_ntop(af, addr, addrp, sizeof addrp) == NULL)
{
perror("inet_ntop");
continue;
}
if (inet_ntop(af, mac, macp, sizeof macp) == NULL) // this is already for MAC add
{
perror("inet_ntop");
continue;
}
if (strcmp(addrp, "127.0.0.1") != 0)
{
strcat(tableO[i].IPaddr, addrp);
strcat(tableO[i].MACaddr, macp);
i++;
}
}
Thanks
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…