Problem getting NIC info for WiFi with getifaddrs()

Hi.

I'm using the getifaddrs() to get the IP and MAC address of all the NICs (if available).
While this works fine for the en0 (cable) NIC, it does not work for the en1 (wireless) NIC.

If I loop through the linked list from getifaddrs() with some debug output, I can see that the three instances of en0 is of the the following sa_family:
AF_LINK
AF_INET6
AF_INET

While the en1 is only of these:
AF_LINK
AF_INET6

So no "AF_INET"

"ifconfig en1" or "ipconfig getifaddrs en1" from the console, shows that the interface is up and it's current DHCP IP.

I don't have the code at hand to post it, but can do so if needed. But generally the code just loops through the linked list and matches if the ifaddrs struct's: ifa addr->safamily is of AF_INET or of AF_LINK.

Tried searching, but can't seem to find anyone with a similar problem / solution.
Again, any hints would be greatly appreciated.

Best regards,
Chris

Using Xcode 3.1.2

MacBook Pro, Mac OS X (10.5.7)

Posted on Aug 6, 2009 7:05 AM

Reply
2 replies

Aug 6, 2009 8:21 AM in response to chrcphdkny

The c++ code:


struct ifaddrs *netaddr;
struct ifaddrs *netaddrhead;
if(getifaddrs(&netaddrhead) != 0)
log.fail("getifaddrs failed");
else
{
netaddr = netaddrhead;
do
{
if(netaddr->ifaaddr->safamily == AF_LINK)
{
struct sockaddr_dl* sockInfo = (struct sockaddrdl*)netaddr->ifaaddr;
if(sockInfo != NULL)
{
std::cout << "NIC: " << netaddr->ifa_name << "MAC: " << link_ntoa(sockInfo) << std::endl;
}
}
else if(netaddr->ifaaddr->safamily == AF_INET)
{
struct sockaddr_in* sockInfoIP = (struct sockaddrin*)netaddr->ifaaddr;
if(sockInfoIP != NULL)
{
std::cout << "NIC: " << netaddr->ifa_name << "IP: " << inet_ntoa(sockInfoIP) << std::endl;
}
}
}
while(netaddr->ifa_next != NULL);
}
freeifaddrs(netaddrhead);

Sep 29, 2009 9:09 AM in response to chrcphdkny

This is a helpful snippet for IPv4. But I've made a few changes -- probably what you might have originally intended to demo.


struct ifaddrs *netaddr;
struct ifaddrs *netaddrhead;

if(getifaddrs(&netaddrhead) != 0)
std::cout << ("getifaddrs failed");
else
{
netaddr = netaddrhead;
do
{
if(netaddr->ifaaddr->safamily == AF_LINK)
{
struct sockaddr_dl* sockInfo = (struct sockaddrdl*)netaddr->ifaaddr;
if(sockInfo != NULL)
{
std::cout << "NIC: " << netaddr->ifa_name << " MAC: " << link_ntoa(sockInfo) << std::endl;
}
}
else if(netaddr->ifaaddr->safamily == AF_INET)
{

struct sockaddr_in* sockInfoIP = (struct sockaddrin*)netaddr->ifaaddr;

if(sockInfoIP != NULL)
{
std::cout << "NIC: " << netaddr->ifa_name << " IP: " << inetntoa(sockInfoIP->sinaddr) << std::endl;
}
}
}
while((netaddr = netaddr->ifa_next) != NULL);
}
freeifaddrs(netaddrhead);

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Problem getting NIC info for WiFi with getifaddrs()

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.