What's the proper way to traverse a getifaddrs() interface list?

Peter Steele psteele at maxiscale.com
Tue Jan 5 02:05:27 UTC 2010


I have an application where I want to collect information on the network interfaces. I've researched this and the function getifaddrs(struct ifaddrs *ifap) appears to be the way to go, but I'm having some trouble understanding exactly how to process the information returned by this call. It's basically a linked list, but there are two entries for each interface. I initially thought I could just skip one of the entries but that doesn't appear to be the case, not entirely anyway. What I want to do is write a function that returns a list of dynamically allocated structures, one for each interface. My structure looks like this:

typedef struct
{
    string *ifcId;
    string *ipAddr;
    bytearray *hwAddr;
    string *subnetMask;
    string *bcastAddr;
} net_attributes;

and the function I am implementing has the following prototype:

int getAllNetAttributes(net_attributes **result, int **count)

where count is the number of elements in the net_attributes result array. This means I need to first count the number of interfaces and allocate my result array, and then collect the information for each interface. This means two traversals of the linked list, once just to count the entries (the length of the list divided by 2) and then traversing it again to collect the data. I've got this mostly working but I'm having trouble with how to deal with the duplicate entries in the linked list of ifaddrs structures returned by getifaddrs. I've looked at ifconfig.c and I can't tell exactly what the code is doing as far as how it handles these duplicate entries. Can someone explain the proper way to traverse this linked list?



More information about the freebsd-net mailing list