Re: Enumerate interfaces?

Glynn Clements (glynn@sensei.co.uk)
Tue, 8 Sep 1998 11:28:03 +0100 (BST)


--B9cq8v7FPw
Content-Type: text/plain; charset=us-ascii
Content-Description: message body and .signature
Content-Transfer-Encoding: 7bit

Mark H. Wood wrote:

> Would someone point me to documentation on how to walk the list of
> defined network interfaces? Thanks!

The attached example was derived from the appropriate sections of the
source code for `ifconfig'.

-- 
Glynn Clements <glynn@sensei.co.uk>

--B9cq8v7FPw Content-Type: text/plain Content-Description: iflist.c Content-Disposition: inline; filename="iflist.c" Content-Transfer-Encoding: 7bit

#include <sys/socket.h> #include <sys/ioctl.h> #include <net/if.h> #include <stdio.h>

int main(void) { char buff[1024]; struct ifconf ifc; struct ifreq *ifr; int skfd; int i;

skfd = socket(AF_INET, SOCK_DGRAM, 0); if (skfd < 0) { perror("socket"); return 1; }

ifc.ifc_len = sizeof(buff); ifc.ifc_buf = buff; if (ioctl(skfd, SIOCGIFCONF, &ifc) < 0) { perror("ioctl(SIOCGIFCONF)"); return 1; }

ifr = ifc.ifc_req;

for (i = 0; i < ifc.ifc_len / sizeof(struct ifreq); i++) printf("%s\n", ifr[i].ifr_name);

return 0; }

--B9cq8v7FPw-- - To unsubscribe from this list: send the line "unsubscribe linux-net" in the body of a message to majordomo@vger.rutgers.edu