From nobody Sun Jun 12 16:40:11 2022 X-Original-To: freebsd-net@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id DFA05857237; Sun, 12 Jun 2022 16:40:14 +0000 (UTC) (envelope-from cross+freebsd@distal.com) Received: from relay.wiredblade.com (relay.wiredblade.com [168.235.105.136]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 4LLgRK6lTgz4bqb; Sun, 12 Jun 2022 16:40:13 +0000 (UTC) (envelope-from cross+freebsd@distal.com) Received: from mail.distal.com (pool-108-45-159-88.washdc.fios.verizon.net [108.45.159.88]) by relay.wiredblade.com with ESMTPSA (version=TLSv1.2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256) ; Sun, 12 Jun 2022 16:40:12 +0000 Received: from smtpclient.apple ( [2001:470:e24c:200:8852:3b29:50e0:32ca]) by tristain.distal.com (OpenSMTPD) with ESMTPSA id ec370aad (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256:NO); Sun, 12 Jun 2022 12:40:11 -0400 (EDT) From: Chris Ross Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable List-Id: Networking and TCP/IP with FreeBSD List-Archive: https://lists.freebsd.org/archives/freebsd-net List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-net@freebsd.org Mime-Version: 1.0 (Mac OS X Mail 15.0 \(3693.60.0.1.1\)) Subject: Troubles with adding IPv6 support to a program Message-Id: Date: Sun, 12 Jun 2022 12:40:11 -0400 To: freebsd-net@freebsd.org, freebsd-transport@freebsd.org X-Mailer: Apple Mail (2.3693.60.0.1.1) X-Rspamd-Queue-Id: 4LLgRK6lTgz4bqb X-Spamd-Bar: - Authentication-Results: mx1.freebsd.org; dkim=none; dmarc=none; spf=pass (mx1.freebsd.org: domain of cross@distal.com designates 168.235.105.136 as permitted sender) smtp.mailfrom=cross@distal.com X-Spamd-Result: default: False [-1.65 / 15.00]; RCVD_VIA_SMTP_AUTH(0.00)[]; ARC_NA(0.00)[]; MID_RHS_MATCH_FROM(0.00)[]; FROM_HAS_DN(0.00)[]; MV_CASE(0.50)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_GOOD(-0.10)[text/plain]; TO_DN_NONE(0.00)[]; DMARC_NA(0.00)[distal.com]; R_SPF_ALLOW(-0.20)[+a:relay.dynu.com]; NEURAL_HAM_LONG(-1.00)[-0.998]; RCVD_COUNT_THREE(0.00)[3]; NEURAL_HAM_MEDIUM(-1.00)[-1.000]; NEURAL_SPAM_SHORT(0.15)[0.150]; RCPT_COUNT_TWO(0.00)[2]; MLMMJ_DEST(0.00)[freebsd-transport,freebsd-net]; FROM_EQ_ENVFROM(0.00)[]; R_DKIM_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; ASN(0.00)[asn:3842, ipnet:168.235.104.0/22, country:US]; TAGGED_FROM(0.00)[freebsd]; RCVD_TLS_ALL(0.00)[]; RECEIVED_SPAMHAUS_PBL(0.00)[108.45.159.88:received] X-ThisMailContainsUnwantedMimeParts: N Tl;dr; I don=E2=80=99t know why I=E2=80=99m getting an EINVAL from a call to = bind for a second socket 20 years ago, I spent a lot of time adding IPv6 support to IPv4-only = programs. So I thought this (adding IPv6 support to simpleproxy[1]) would be an = easy project to pick up. I=E2=80=99ve gotten most of the framework in, = delayed only slightly when I learned that FreeBSD is =E2=80=9Cdifferent=E2=80=9D in = not routing IPv4 traffic to AF_INET6 sockets [2]. However, I=E2=80=99m getting an issue from one of my bind(2) calls that = I=E2=80=99m not able to figure out. I was hoping someone else had a few minutes to take a = quick look and help me find out what I=E2=80=99m doing wrong. A ~70 line relevant section of the source is at = https://justpaste.it/6u3jd The tl;dr; of this is that I: * getaddrinfo(NULL, portIwant) * for each address returned: * create a socket * setsockopt(SO_REUSEADDR) * bind * listen The first address I get is IPv6 localhost, and that binds and listens, = then continues to the next address. A new socket is created, setsockopt=E2=80=99= d, but bind for the second address (IPv4 localhost) fails with EINVAL. As you can see in the code shared, I have lots of debugging logs in the = code, and it looks like everything is as it should be. I=E2=80=99m probably = just missing Something simple, and am looking for another set of eyes. Thanks all. Contact me off list if you like, or on-list if it=E2=80=99s = obvious what I=E2=80=99ve done and it will help others. - Chris [1] https://github.com/vzaliva/simpleproxy [2] inet6(4), "Interaction between IPv4/v6 sockets"=