[ntp:questions] Pool returns IPv6 address to IPv4 query

Rob nomail at example.com
Wed Nov 20 15:39:47 UTC 2013

Brian Utterback <brian.utterback at oracle.com> wrote:
>> However, what I don't understand is why an IPv6 address does not fit
>> into a struct sockaddr, and why this fact is so badly documented.
>> It took me a lot of time to find why my queried IPv6 addresses were
>> truncated.
> It is a little tricky to be sure. Part of it was backwards compatibility 
> and history, part of it was providing the proper abstractions. The root 
> cause of course is that IPv6 addresses are longer than IPv4 addresses. 

But a struct sockaddr has always been padded to 16 bytes, with a little
care an IPv6 address and the associated info would have fitted in that.

> If you use them properly, you should never have a problem with 
> truncation though. You would have to be trying to put a IPv6 address 
> into a IPv4 sockaddr for that to happen.

I used a "struct sockaddr" as the parameter to those getinfo calls,
and it turned out to be too small to return a sockaddr_in6.
(while it is large enough for a sockaddr_in)

However, all the examples I found always used that struct sockaddr,
and it is also the type of the parameter in the declaration.
And in fact it works fine with IPv4!

Only after long hairpulling I found you need to custom-write a union
of struct sockaddr and struct sockaddr_storage, and pass that to the
functions.  (or declare variables of type struct sockaddr_storage and
cast the types all the time)

More information about the questions mailing list