From owner-freebsd-net@FreeBSD.ORG Tue Jan 5 02:05:26 2010 Return-Path: Delivered-To: freebsd-net@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id DF693106566B for ; Tue, 5 Jan 2010 02:05:26 +0000 (UTC) (envelope-from psteele@maxiscale.com) Received: from server505.appriver.com (server505c.appriver.com [98.129.35.7]) by mx1.freebsd.org (Postfix) with ESMTP id A59F88FC1B for ; Tue, 5 Jan 2010 02:05:26 +0000 (UTC) X-Policy: GLOBAL - maxiscale.com X-Primary: psteele@maxiscale.com X-Note: This Email was scanned by AppRiver SecureTide X-ALLOW: psteele@maxiscale.com ALLOWED X-Virus-Scan: V- X-Note: Spam Tests Failed: X-Country-Path: UNITED STATES->UNITED STATES->UNITED STATES X-Note-Sending-IP: 98.129.23.14 X-Note-Reverse-DNS: ht01.exg5.exghost.com X-Note-WHTLIST: psteele@maxiscale.com X-Note: User Rule Hits: X-Note: Global Rule Hits: G113 G114 G115 G116 G120 G121 G132 G219 X-Note: Encrypt Rule Hits: X-Note: Mail Class: ALLOWEDSENDER X-Note: Headers Injected Received: from [98.129.23.14] (HELO ht01.exg5.exghost.com) by server505.appriver.com (CommuniGate Pro SMTP 5.2.14) with ESMTPS id 21900234 for freebsd-net@freebsd.org; Mon, 04 Jan 2010 20:05:24 -0600 Received: from mbx03.exg5.exghost.com ([169.254.1.249]) by ht01.exg5.exghost.com ([98.129.23.14]) with mapi; Mon, 4 Jan 2010 20:05:25 -0600 From: Peter Steele To: "freebsd-net@freebsd.org" Date: Mon, 4 Jan 2010 20:05:23 -0600 Thread-Topic: What's the proper way to traverse a getifaddrs() interface list? Thread-Index: AcqNq4prss8w1vwXSmaDSc9k2ECSig== Message-ID: <7B9397B189EB6E46A5EE7B4C8A4BB7CB36CA401D@MBX03.exg5.exghost.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: acceptlanguage: en-US MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable X-Content-Filtered-By: Mailman/MimeDel 2.1.5 Subject: What's the proper way to traverse a getifaddrs() interface list? X-BeenThere: freebsd-net@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Networking and TCP/IP with FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 05 Jan 2010 02:05:27 -0000 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?