From owner-freebsd-net@freebsd.org Wed Sep 30 13:40:40 2015 Return-Path: Delivered-To: freebsd-net@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 0094EA0C0C6 for ; Wed, 30 Sep 2015 13:40:40 +0000 (UTC) (envelope-from vangyzen@FreeBSD.org) Received: from smtp.vangyzen.net (hotblack.vangyzen.net [IPv6:2607:fc50:1000:7400:216:3eff:fe72:314f]) by mx1.freebsd.org (Postfix) with ESMTP id DCDA91021 for ; Wed, 30 Sep 2015 13:40:39 +0000 (UTC) (envelope-from vangyzen@FreeBSD.org) Received: from marvin.beer.town (unknown [76.164.8.130]) by smtp.vangyzen.net (Postfix) with ESMTPSA id 110CB56483; Wed, 30 Sep 2015 08:40:33 -0500 (CDT) Subject: Re: getaddrinfo() question To: Dmitry Sivachenko , FreeBSD Net References: From: Eric van Gyzen X-Enigmail-Draft-Status: N1110 Message-ID: <560BE64C.7090800@FreeBSD.org> Date: Wed, 30 Sep 2015 08:40:28 -0500 User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit X-BeenThere: freebsd-net@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: Networking and TCP/IP with FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 30 Sep 2015 13:40:40 -0000 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