getifaddrs return same adapter multiple times

This is a discussion on getifaddrs return same adapter multiple times within the C Programming forums, part of the General Programming Boards category; Hi, I have written a c program to retrieve all the network interfaces. Code: getifaddrs(&ifaddr) But when I iterate through ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    22

    getifaddrs return same adapter multiple times

    Hi,
    I have written a c program to retrieve all the network interfaces.
    Code:
    getifaddrs(&ifaddr)
    But when I iterate through the ifaddr elements of the linklist it returns same interface many times. When I prints the ifa_name s it prints like as follow.

    lo
    eth0 *
    irda0
    wmaster0
    wlan0
    pan0
    lo
    eth0 *
    lo
    eth0 *

    * - repeats

    I want to eliminate this repeating and need to retrieve the address that return with ifconfig -a command.
    How can I overcome this ?

    when I command $ifconfig -a its list as follows.

    eth0 Link encap:Ethernet

    irda0 Link encap:IrLAP

    lo Link encap:Local

    pan0 Link encap:Ethernet

    wlan0 Link encap:Ethernet

    wmaster0 Link encap:UNSPEC


    Thank you very much.

    Reg,
    Nuwan Gunarathne

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    977
    If this is on Linux, look at the NOTES section of the getifaddrs() man page:
    The addresses returned on Linux will usually be the IPv4 and IPv6 addresses assigned to the interface, but also one AF_PACKET address per interface containing lower-level details about the interface and its physical layer. In this case, the ifa_data field may contain a pointer to a struct net_device_stats, defined in <linux/netdevice.h>, which contains various interface attributes and statistics.
    That's probably where the multiple interfaces are coming from. You should be able to filter these by looking at ifa_addr->sa_family.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  2. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  3. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 06:57 AM
  4. sort linked list using BST
    By Micko in forum C Programming
    Replies: 8
    Last Post: 10-04-2004, 02:04 PM
  5. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21