From owner-freebsd-net Thu Jul 18 7:51:41 2002 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 5A3C337B401 for ; Thu, 18 Jul 2002 07:51:36 -0700 (PDT) Received: from maild.telia.com (maild.telia.com [194.22.190.101]) by mx1.FreeBSD.org (Postfix) with ESMTP id 2191343E58 for ; Thu, 18 Jul 2002 07:51:35 -0700 (PDT) (envelope-from erikt@midgard.homeip.net) Received: from d1o913.telia.com (d1o913.telia.com [195.252.44.241]) by maild.telia.com (8.12.5/8.12.5) with ESMTP id g6IEpX2a001888 for ; Thu, 18 Jul 2002 16:51:33 +0200 (CEST) X-Original-Recipient: Received: from falcon.midgard.homeip.net (h187n3fls20o913.telia.com [213.67.148.187]) by d1o913.telia.com (8.8.8/8.8.8) with SMTP id QAA17060 for ; Thu, 18 Jul 2002 16:51:31 +0200 (CEST) Received: (qmail 39317 invoked by uid 1001); 18 Jul 2002 14:51:30 -0000 Date: Thu, 18 Jul 2002 16:51:29 +0200 From: Erik Trulsson To: "Chuck T." Cc: net@FreeBSD.ORG Subject: Re: programatically list all local IP addresses ? Message-ID: <20020718145129.GA39189@falcon.midgard.homeip.net> Mail-Followup-To: "Chuck T." , net@FreeBSD.ORG References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.1i Sender: owner-freebsd-net@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.org On Thu, Jul 18, 2002 at 07:27:08AM -0700, Chuck T. wrote: > I'm sure this is a FAQ, but I sure can't find the answer. I've tried the > usually suggested gethostname()/gethostbyname() approach, but that only > returns the *first* match in /etc/hosts. In one case that was 127.0.0.1. > Clearly this was a case where /etc/host.conf gave priority to the host file. Use gethostname()/gethostbyname() (or gethostbyaddr()) and then look through the 'h_addr_list' array in the 'struct hostent' returned by gethostbyname(). That should contain all the network addresses that were found for the given host. Another possibility is of course to run 'ifconfig -a' and then parse the output to find out what IP-addresses are configured on the machine. > > I don't want to call gethostent() because (if I understand the man page > correctly) that would limit me entries /etc/hosts ignoring DNS. > > I guess I could do a gethostname()/gethostbyname() and then if I fail to > find > a usable (non-rfc1918) address gethosten(). Is there a better way? > > Also is listing 127.0.0.1 as the actual host name in addition to "localhost" > in /etc/hosts considered to be proper ? That might be the only IP-address the machine has, so why not? -- Erik Trulsson ertr1013@student.uu.se To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-net" in the body of the message