Log in

No account? Create an account
getaddrinfo is not just for IPv6 - Ulrich Drepper — LiveJournal [entries|archive|friends|userinfo]
Ulrich Drepper

[ website | My Website ]
[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

getaddrinfo is not just for IPv6 [Mar. 7th, 2007|01:11 am]
Ulrich Drepper

I've heard far too often that getaddrinfo is only interesting for IPv6 and therefore can be ignored since one does not have IPv6.

Aside from the fact that all programs should be protocol independent this statement is bogus. gethostbyname etc do not perform correctly in some situations where only ever IPv4 is involved.

Assume you have an internal IPv4 network with, say, 192.168.x.y addresses. In addition you have a server (web server, for instance) which is also visible on the Internet. This server has two addresses: one 192.168.x.y address and one global address. The client is a NATed machine on the intranet.

Now what happens if the nameserver returns both addresses to a query for the addresses of said server? With gethostbyname the addresses are returned to the caller in the order they are received from the DNS server. Maybe some randomization is applied. In short, it is possible that the internal machine gets sees the public IPv4 address and then connects to it. This is not only wasteful (the request has to be routed through a switch), it might even be dangerous (the traffic might actually have to go through the Internet).

With getaddrinfo this is not the case. The sorting according to RFC 3484 makes sure that the internal address of the server is returned first. The sorting function will notice that the source address used on the client is also an internal address and therefore the internal address of the server is a better match than the global address.

In summary, gethostbyaddr is not only about IPv6. The old interfaces were simply completely inadequate and should never be used. If you still haven't converted your programs to use getaddrinfo instead of gethostbyname and gethostbyname2 do it now. I have written some time ago a brief intro.


From: (Anonymous)
2007-03-07 03:26 pm (UTC)


I agree in principle about getaddrinfo() but I've got a few questions...

1. How portable is this API? I see from the manual page that it conforms to POSIX.1-2001 but I'm not sure who that excludes and who excludes it.

2. There's no way to multiplex a bunch of these, or run them non-blocking, sans giving each a thread, is there?
(Reply) (Thread)
From: udrepper
2007-03-07 06:39 pm (UTC)

Re: Curious

First, the function is everywhere. I think even the Evil from the North-West has it.

Second, of course there are asynchronous versions. I developed it a long time ago. It's not as widely available (although Linux has it for many years now) but neither are asynchronous versions for the old functions.

(Reply) (Parent) (Thread)