This is the mail archive of the
mailing list for the glibc project.
Re: Using getaddrinfo() to retrieve local IP addresses
- From: Yann Droneaud <ydroneaud at opteya dot com>
- To: Brendan Shanks <bshanks at codeweavers dot com>, libc-help at sourceware dot org
- Date: Mon, 07 Oct 2019 10:48:20 +0200
- Subject: Re: Using getaddrinfo() to retrieve local IP addresses
- References: <313C8A17-CD96-4639-B106-83702271E7DD@codeweavers.com>
Le vendredi 04 octobre 2019 à 18:36 -0700, Brendan Shanks a écrit :
> Traditionally, passing the local hostname (i.e. returned from
> gethostname()) to getaddrinfo() returns the host’s IP addresses,
> suitable for binding to.
This is assuming NSS database (likely /etc/hosts) to be filled with
> However, some Linux distributions (Debian, Ubuntu, Arch, likely
> others) define the local hostname to ‘127.0.1.1’ in /etc/hosts. (
> In this case, getaddrinfo() only returns ‘127.0.1.1’.
That's probably a workaround for some broken configuration when machine
hostname is set to some name not present in NSS database, and not
resolvable through DNS: resolving the name will take some amount of
time until it timeout.
Having a default entry that resolve to loopback will cut the resolution
> On these systems, is there a flag or an alternate ’node’ name that
> can be passed to getaddrinfo() that will result in all the host’s IP
> addresses rather than just the useless ‘127.0.1.1’?
I'm not aware of such feature.
> For background, I’m working on fixing a Wine bug (
> https://bugs.winehq.org/show_bug.cgi?id=46940) where a Windows game
> uses this method to get the local network IP address. Wine’s
> getaddrinfo() implementation largely passes through to the host
> getaddrinfo(), and the game tries to use ‘127.0.1.1’ for local
> network sockets. I’ll have to add a special case to Wine’s
> getaddrinfo() for the local hostname, but it would be much simpler to
> use the host getaddrinfo()’s list of IP addresses rather than having
> to use netlink. getifaddrs() is close to being correct, but it
> includes loopback addresses, deprecated IPv6 temporary addresses,
It's quite difficult to identify the "local network IP address" from
- a device can have multiple network interface,
- each interface can have multiple addresses, in different family.
- interface goes up, down
- addresses are added, removed
- a device can only have a single hostname
- this hostname can be a valid, resolvable DNS address, mDNS address,
/etc/hosts entry, or not.
You probably have to look for freedesktop API, NetworkManager, or
systemd, for some hints.