From owner-freebsd-net@freebsd.org Thu Jan 4 16:48:31 2018 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 1D2D9EA4183 for ; Thu, 4 Jan 2018 16:48:31 +0000 (UTC) (envelope-from lew@perftech.com) Received: from smtp-gw.pt.net (smtp-gw.pt.net [206.210.194.15]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "smtp-gw.pt.net", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id E205F73086 for ; Thu, 4 Jan 2018 16:48:30 +0000 (UTC) (envelope-from lew@perftech.com) X-ASG-Debug-ID: 1515083529-09411a0f9912d2c00001-QdxwpM Received: from mail.pt.net (mail.pt.net [206.210.194.11]) by smtp-gw.pt.net with ESMTP id VCsomdT4O9CEiTsV (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Thu, 04 Jan 2018 10:32:09 -0600 (CST) X-Barracuda-Envelope-From: lew@perftech.com X-Barracuda-Effective-Source-IP: mail.pt.net[206.210.194.11] X-Barracuda-Apparent-Source-IP: 206.210.194.11 Received: from localhost (localhost [IPv6:::1]) by mail.pt.net (Postfix) with ESMTP id 2B6828428F0; Thu, 4 Jan 2018 10:32:09 -0600 (CST) Received: from mail.pt.net ([IPv6:::1]) by localhost (mail.pt.net [IPv6:::1]) (amavisd-new, port 10032) with ESMTP id LM1uC34GxSMC; Thu, 4 Jan 2018 10:32:08 -0600 (CST) Received: from localhost (localhost [IPv6:::1]) by mail.pt.net (Postfix) with ESMTP id 3F9C28428F3; Thu, 4 Jan 2018 10:32:08 -0600 (CST) X-Virus-Scanned: amavisd-new at pt.net Received: from mail.pt.net ([IPv6:::1]) by localhost (mail.pt.net [IPv6:::1]) (amavisd-new, port 10026) with ESMTP id AagaoLVyC4di; Thu, 4 Jan 2018 10:32:08 -0600 (CST) Received: from dhcp-221-110.perftech.com (dhcp-221-110.perftech.com [206.210.221.110]) (Authenticated sender: lew@pt.net) by mail.pt.net (Postfix) with ESMTPSA id 29E658428F0; Thu, 4 Jan 2018 10:32:08 -0600 (CST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 11.2 \(3445.5.20\)) Subject: Re: IP networking single socket, both IPv4 and V6? From: Lewis Donzis X-ASG-Orig-Subj: Re: IP networking single socket, both IPv4 and V6? In-Reply-To: <2b3944fc-df1a-9998-876e-ad74f8cc073d@denninger.net> Date: Thu, 4 Jan 2018 10:32:07 -0600 Cc: freebsd-net@freebsd.org Content-Transfer-Encoding: quoted-printable Message-Id: References: <2b3944fc-df1a-9998-876e-ad74f8cc073d@denninger.net> To: Karl Denninger X-Mailer: Apple Mail (2.3445.5.20) X-Barracuda-Connect: mail.pt.net[206.210.194.11] X-Barracuda-Start-Time: 1515083529 X-Barracuda-Encrypted: ECDHE-RSA-AES256-GCM-SHA384 X-Barracuda-URL: https://smtp-gw.pt.net:443/cgi-mod/mark.cgi X-Virus-Scanned: by bsmtpd at pt.net X-Barracuda-Scan-Msg-Size: 1583 X-Barracuda-BRTS-Status: 1 X-Barracuda-Spam-Score: 0.00 X-Barracuda-Spam-Status: No, SCORE=0.00 using global scores of TAG_LEVEL=1000.0 QUARANTINE_LEVEL=1000.0 KILL_LEVEL=9.0 tests= X-Barracuda-Spam-Report: Code version 3.2, rules version 3.2.3.46545 Rule breakdown below pts rule name description ---- ---------------------- -------------------------------------------------- X-BeenThere: freebsd-net@freebsd.org X-Mailman-Version: 2.1.25 Precedence: list List-Id: Networking and TCP/IP with FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 04 Jan 2018 16:48:31 -0000 On Jan 4, 2018, at 10:17 AM, Karl Denninger wrote: >=20 > I've written a fair bit of code that binds to both Ipv4 and v6 for > incoming connections, using two sockets (one for each.) >=20 > Perusing around the 'net I see an implementation note written by IBM > that implies that on their Unix implementation you can set up an INET6 > listener and it will open listeners on *both* IPv4 and v6; you code it > as an Ipv6 socket/bind/listen/accept, and if an Ipv4 connection comes = in > you get a prefix'd IPv4 address back when you call getpeername(). >=20 > This would obviously shorten up code and remove the need to open the > second listener socket, but playing with this on FreeBSD it doesn't > appear to work -- I only get the IPv6 listener in "netstat -a -n" > showing up and as expected a connection to a v4 address on that port > fails (refused, since there's no listener.) >=20 > Is this something that *should* work on FreeBSD? It works. We do it all the time. You either have to set the sysctl: net.inet6.ip6.v6only=3D0 which you can do in /etc/sysctl.conf or with the sysctl utility, or, in = your program, use setsockopt to turn off the V6ONLY option, e.g.: setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &(int){0}, sizeof (int)); // = Turn off v6-only We use the first method, which is broken in FreeBSD 11.1 prior to patch = level 5 or 6, I can=E2=80=99t remember which, but works in all others. = The second method is considered to be more portable. FWIW, Linux, by default, sets v6only off, so it doesn't require anything = special.