Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 18 Jul 2002 16:51:29 +0200
From:      Erik Trulsson <ertr1013@student.uu.se>
To:        "Chuck T." <freebsdfan@hotmail.com>
Cc:        net@FreeBSD.ORG
Subject:   Re: programatically list all local IP addresses ?
Message-ID:  <20020718145129.GA39189@falcon.midgard.homeip.net>
In-Reply-To: <F44lyg35sQtgTIFprhM00019196@hotmail.com>
References:  <F44lyg35sQtgTIFprhM00019196@hotmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
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?



-- 
<Insert your favourite quote here.>
Erik Trulsson
ertr1013@student.uu.se

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-net" in the body of the message




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