Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 4 Jan 2010 20:05:23 -0600
From:      Peter Steele <psteele@maxiscale.com>
To:        "freebsd-net@freebsd.org" <freebsd-net@freebsd.org>
Subject:   What's the proper way to traverse a getifaddrs() interface list?
Message-ID:  <7B9397B189EB6E46A5EE7B4C8A4BB7CB36CA401D@MBX03.exg5.exghost.com>

next in thread | raw e-mail | index | archive | help
I have an application where I want to collect information on the network in=
terfaces. I've researched this and the function getifaddrs(struct ifaddrs *=
ifap) appears to be the way to go, but I'm having some trouble understandin=
g exactly how to process the information returned by this call. It's basica=
lly a linked list, but there are two entries for each interface. I initiall=
y thought I could just skip one of the entries but that doesn't appear to b=
e 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. T=
his means I need to first count the number of interfaces and allocate my re=
sult array, and then collect the information for each interface. This means=
 two traversals of the linked list, once just to count the entries (the len=
gth of the list divided by 2) and then traversing it again to collect the d=
ata. I've got this mostly working but I'm having trouble with how to deal w=
ith 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 someon=
e explain the proper way to traverse this linked list?




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?7B9397B189EB6E46A5EE7B4C8A4BB7CB36CA401D>