From owner-freebsd-net@FreeBSD.ORG Mon Feb 23 22:49:06 2004 Return-Path: Delivered-To: freebsd-net@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id B030816A4CE for ; Mon, 23 Feb 2004 22:49:06 -0800 (PST) Received: from out002.verizon.net (out002pub.verizon.net [206.46.170.141]) by mx1.FreeBSD.org (Postfix) with ESMTP id 5D23543D2F for ; Mon, 23 Feb 2004 22:49:06 -0800 (PST) (envelope-from cswiger@mac.com) Received: from mac.com ([68.160.202.196]) by out002.verizon.net (InterMail vM.5.01.06.06 201-253-122-130-106-20030910) with ESMTP id <20040224064905.LRVE23576.out002.verizon.net@mac.com>; Tue, 24 Feb 2004 00:49:05 -0600 Message-ID: <403AF3D9.5070506@mac.com> Date: Tue, 24 Feb 2004 01:48:57 -0500 From: Chuck Swiger Organization: The Courts of Chaos User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.6) Gecko/20040113 X-Accept-Language: en-us, en MIME-Version: 1.0 To: "Ronald F. Guilmette" References: <46800.1077593230@monkeys.com> In-Reply-To: <46800.1077593230@monkeys.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Authentication-Info: Submitted using SMTP AUTH at out002.verizon.net from [68.160.202.196] at Tue, 24 Feb 2004 00:49:05 -0600 cc: freebsd-net@freebsd.org Subject: Re: Finding all IPv4 addresses associated with INADDR_ANY (?) X-BeenThere: freebsd-net@freebsd.org X-Mailman-Version: 2.1.1 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, 24 Feb 2004 06:49:06 -0000 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