getaddrinfo() question

Eric van Gyzen vangyzen at FreeBSD.org
Wed Sep 30 13:40:40 UTC 2015


On 09/29/2015 17:51, Dmitry Sivachenko wrote:
> Hello!
>
> I have a machine (FreeBSD-10/stable) with both ipv4 and ipv6 addresses configured.
> Also I have ip6addrctl_policy="ipv4_prefer" in rc.conf.
>
> I am trying to resolve a hostname which has both A and AAAA records and I expect getaddrinfo() to return A record because of ipv4_prefer.
>
> I am running a sample program attached.
>
> When I use hints.ai_flags = AI_PASSIVE flag, the program prints "AF_INET6" (why?), if I change that line to hints.ai_flags = 0; it printf "AF_INET" (as expected).
>
> Can you please explain why AI_PASSIVE makes a difference?
>
> On FreeBSD, the manual page is unclear about the behavior when hostname is not NULL and AI_PASSIVE is used, but on Linux it explicitly states that
> "If node is not NULL, then the AI_PASSIVE flag is ignored."

I haven't looked at the code or the RFCs recently, so my reply shouldn't be considered even remotely authoritative, but this behavior makes sense to me.  I believe ip6addrctl only applies to active/outgoing connections, since client programs usually don't provide a convenient way to choose the address family.  Passive/listening programs are different by nature:  they usually provide a way to configure the desired address families, and they usually want to listen on all families by default.  By using PF_UNSPEC with AI_PASSIVE, you're saying you want to listen on the given host's addresses, regardless of protocol family/version.  If you follow the ai_next pointers, you'll see that you're getting two addrinfo results:  AF_INET6 and AF_INET.  Since you're going to open passive/listening sockets, the order is irrelevant.

Like I said, though, this is just my interpretation.

Eric


More information about the freebsd-net mailing list