Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 24 Feb 2004 01:48:57 -0500
From:      Chuck Swiger <cswiger@mac.com>
To:        "Ronald F. Guilmette" <rfg@monkeys.com>
Cc:        freebsd-net@freebsd.org
Subject:   Re: Finding all IPv4 addresses associated with INADDR_ANY (?)
Message-ID:  <403AF3D9.5070506@mac.com>
In-Reply-To: <46800.1077593230@monkeys.com>
References:  <46800.1077593230@monkeys.com>

next in thread | previous in thread | raw e-mail | index | archive | help
Ronald F. Guilmette wrote:
> Given a socket which has been properly created, opened, and then bound
> to some port and the special INADDR_ANY ``wildcard'' address, I need
> to be able to them programatically find all of the IPv4 addresses that
> the socket was just bound to.

Try something like the following:

     struct ifaddrs *if_ptr, *ifap;

     if (getifaddrs(&ifap) == -1) {
         fatal(strerror(errno));
             /*NOTREACHED*/
     }

         /* iterate over the list of interfaces on the machine */
     for (if_ptr = ifap; if_ptr; if_ptr = if_ptr->ifa_next) {
         switch (if_ptr->ifa_addr->sa_family) {
           case AF_INET:
                 /* check that the interface is UP before we try to use it */
             flags = if_ptr->ifa_flags;
             if (!(flags & IFF_UP)) break;
	        /* do something here using if_ptr->ifa_addr */

           case AF_INET6:
                 /* do something else for IPv6... */
         }
     }

...although be sure to call ntohl() on the address to get things in the local 
byte-ordering...

-- 
-Chuck



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