From nobody Wed Nov 5 13:36:54 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1498214010105587.0226828028225; Fri, 23 Jun 2017 03:33:30 -0700 (PDT) Received: from localhost ([::1]:34702 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dOLu4-0007RF-L5 for importer@patchew.org; Fri, 23 Jun 2017 06:33:28 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60481) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dOLsM-0006Ip-6v for qemu-devel@nongnu.org; Fri, 23 Jun 2017 06:31:43 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dOLsI-0001S5-4q for qemu-devel@nongnu.org; Fri, 23 Jun 2017 06:31:42 -0400 Received: from aserp1040.oracle.com ([141.146.126.69]:49479) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dOLsH-0001Rd-Oc for qemu-devel@nongnu.org; Fri, 23 Jun 2017 06:31:38 -0400 Received: from userv0021.oracle.com (userv0021.oracle.com [156.151.31.71]) by aserp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id v5NAVZPT026971 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 23 Jun 2017 10:31:36 GMT Received: from aserv0122.oracle.com (aserv0122.oracle.com [141.146.126.236]) by userv0021.oracle.com (8.14.4/8.14.4) with ESMTP id v5NAVZ7h025140 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 23 Jun 2017 10:31:35 GMT Received: from abhmp0004.oracle.com (abhmp0004.oracle.com [141.146.116.10]) by aserv0122.oracle.com (8.14.4/8.14.4) with ESMTP id v5NAVYHY020005; Fri, 23 Jun 2017 10:31:34 GMT Received: from abi.no.oracle.com (/10.172.144.123) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Fri, 23 Jun 2017 03:31:34 -0700 From: Knut Omang To: "Daniel P . Berrange" , Gerd Hoffmann , Paolo Bonzini Date: Fri, 23 Jun 2017 12:31:07 +0200 Message-Id: <7a233015767d404da2c3d6cd5aac9f9151aed878.1498213152.git-series.knut.omang@oracle.com> X-Mailer: git-send-email 2.9.4 In-Reply-To: References: X-Source-IP: userv0021.oracle.com [156.151.31.71] X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 141.146.126.69 Subject: [Qemu-devel] [PATCH v4 3/4] sockets: factor out a new try_bind() function X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Knut Omang , qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Another refactoring step to prepare for the problem exposed by the test-listen test. This time simplify and reorganize the IPv6 specific extra measures and move it out of the for loop to increase code readability. No semantic changes. Signed-off-by: Knut Omang --- util/qemu-sockets.c | 69 ++++++++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 30 deletions(-) diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c index 699e36c..48b9319 100644 --- a/util/qemu-sockets.c +++ b/util/qemu-sockets.c @@ -163,6 +163,44 @@ static int create_fast_reuse_socket(struct addrinfo *e= , Error **errp) return slisten; } =20 +static int try_bind(int socket, InetSocketAddress *saddr, struct addrinfo = *e) +{ +#ifndef IPV6_V6ONLY + return bind(socket, e->ai_addr, e->ai_addrlen); +#else + /* + * Deals with first & last cases in matrix in comment + * for inet_ai_family_from_address(). + */ + int v6only =3D + ((!saddr->has_ipv4 && !saddr->has_ipv6) || + (saddr->has_ipv4 && saddr->ipv4 && + saddr->has_ipv6 && saddr->ipv6)) ? 0 : 1; + int stat; + + rebind: + if (e->ai_family =3D=3D PF_INET6) { + qemu_setsockopt(socket, IPPROTO_IPV6, IPV6_V6ONLY, &v6only, + sizeof(v6only)); + } + + stat =3D bind(socket, e->ai_addr, e->ai_addrlen); + if (!stat) { + return 0; + } + + /* If we got EADDRINUSE from an IPv6 bind & v6only is unset, + * it could be that the IPv4 port is already claimed, so retry + * with v6only set + */ + if (e->ai_family =3D=3D PF_INET6 && errno =3D=3D EADDRINUSE && !v6only= ) { + v6only =3D 1; + goto rebind; + } + return stat; +#endif +} + static int inet_listen_saddr(InetSocketAddress *saddr, int port_offset, bool update_addr, @@ -238,39 +276,10 @@ static int inet_listen_saddr(InetSocketAddress *saddr, port_min =3D inet_getport(e); port_max =3D saddr->has_to ? saddr->to + port_offset : port_min; for (p =3D port_min; p <=3D port_max; p++) { -#ifdef IPV6_V6ONLY - /* - * Deals with first & last cases in matrix in comment - * for inet_ai_family_from_address(). - */ - int v6only =3D - ((!saddr->has_ipv4 && !saddr->has_ipv6) || - (saddr->has_ipv4 && saddr->ipv4 && - saddr->has_ipv6 && saddr->ipv6)) ? 0 : 1; -#endif inet_setport(e, p); -#ifdef IPV6_V6ONLY - rebind: - if (e->ai_family =3D=3D PF_INET6) { - qemu_setsockopt(slisten, IPPROTO_IPV6, IPV6_V6ONLY, &v6onl= y, - sizeof(v6only)); - } -#endif - if (bind(slisten, e->ai_addr, e->ai_addrlen) =3D=3D 0) { + if (try_bind(slisten, saddr, e) >=3D 0) { goto listen; } - -#ifdef IPV6_V6ONLY - /* If we got EADDRINUSE from an IPv6 bind & V6ONLY is unset, - * it could be that the IPv4 port is already claimed, so retry - * with V6ONLY set - */ - if (e->ai_family =3D=3D PF_INET6 && errno =3D=3D EADDRINUSE &&= !v6only) { - v6only =3D 1; - goto rebind; - } -#endif - if (p =3D=3D port_max) { if (!e->ai_next) { error_setg_errno(errp, errno, "Failed to bind socket"); --=20 git-series 0.9.1