Re: local host IP address on X
Re: local host IP address on X
- Subject: Re: local host IP address on X
- From: Allan Nathanson <email@hidden>
- Date: Tue, 16 Jul 2002 10:38:05 -0400
On Tuesday, July 16, 2002, at 05:32 AM, Quinn wrote:
At 20:02 -0700 15/7/02, Kevin Parichan wrote:
I'm porting some Windows WinSock code to the Mac. The 8/9 version
works
fine via a 3rd party sockets library, but the X version is having
problems. I'm trying to determine the local host IP address. The
original
code is using gethostname, gethostbyname, then inet_ntoa. What's
coming
back is a list of one item with the address 127.0.0.1. This obviously
doesn't match what is in the Internet Connect window and not what I
expected. If I use OTInetGetInterfaceInfo it returns the same thing at
the Internet Connect window. I looked at the Stevens book, and other
than
using uname instead of gethostname, is basically doing the same thing.
I'm new to the sockets stuff, so what is it that I'm not
understanding or
doing wrong?
The problem here is that these standard sockets calls aren't really
well suited to a dynamic TCP/IP environment. Because Mac OS X has
lots of code that assumes that these calls do something meaningful,
they've been hardwired to always return the loopback address. That
way UNIX programs that assume a single fixed IP address continue to
limp along.
Actually, the name returned by gethostname(3) is just that, a "name".
The returned "name" may have no relation to a DNS name associated with
one of the systems IP address. The actual "name" that gets set is
based on a couple of factors which end up looking like:
1. The value of the HOSTNAME environment variable set in
/etc/hostconfig (assuming that it is not set to -AUTOMATIC-)
2. If your system gets its network configuration via DHCP (or BOOTP)
and the server provides a "name" then ...
3. We then attempt to do a reverse DNS query on the systems primary IP
address. If the DNS server returns a name then ...
4. Lastly, we will use the name "localhost"
For 10.2 (Jaguar) we have inserted a step 3.5 to use the Rendezvous
name if it is available.
...and if your change your network configuration the name will also
change.
As to getting "the" local host IP address. As Quinn noted, making an
assumption that your system has a single address is flawed. Welcome to
the world of multiple network interfaces (LAN, AirPort, PPP, ...),
multiple IP addresses per interface, IPv4 & IPv6, etc. Which IP
address did you want?
The best fix is to use the System Configuration framework APIs to
build an IP address list, and also to be notified when that list
changes. Alternatively, you can use the get_ifi_info routine (on page
434 of Stevens) to get the full list of IP addresses and choose
amongst them.
In 10.2 (Jaguar) you can also use the getifaddrs(3) function to the a
list of addresses. The advantage of using the SC APIs is that you can
register for notifications when the list of addresses changes. You can
also determine which of the addresses (services) has been deemed
"primary".
- Allan
_______________________________________________
macnetworkprog mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/macnetworkprog
Do not post admin requests to the list. They will be ignored.