From nobody Sat Feb 7 09:47:45 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1562854074; cv=none; d=zoho.com; s=zohoarc; b=kK5XJDUnRlmonYh5WtefkJDau5SR2RMTMZuezgtgPWev+FMmXjHpeTw9Y51bPPnFG+QIcCKBYPbMQ6APCymYyDYiTD3ccuggerzY/oIkVC4irlF/waf85xH6AY/GwsolvBtYnOJdKBQFkDu2uTc3CK7SUsJ6UOhBvEkb2U+rzaI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1562854074; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=GeCatd2MAcquqvQOrNezcPPfpAXXt+OmMBLKUFrA6xI=; b=EePafFPr+kOzgMYER3TIsXBzlwbrMX/Hfhce7P1k3kUGA5V4ogE634uVf4BM9knYl4RNGiq1DYAmCBzkURSZ55Tcd2Jx2RcfOfx+pI2/njlHCAdB+0Pfl5s5021Rk6mb/eTzNR3MD1ycY/aCLG+acI9raVtIKtEDLYfYBrMk3UA= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1562854074583830.6246449400708; Thu, 11 Jul 2019 07:07:54 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id F3A665D619; Thu, 11 Jul 2019 14:07:52 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id BB61419729; Thu, 11 Jul 2019 14:07:52 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 78C4441F53; Thu, 11 Jul 2019 14:07:52 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x6BE7lkA028787 for ; Thu, 11 Jul 2019 10:07:47 -0400 Received: by smtp.corp.redhat.com (Postfix) id 2B62560148; Thu, 11 Jul 2019 14:07:47 +0000 (UTC) Received: from dhcp-17-95.lcy.redhat.com (unknown [10.42.17.95]) by smtp.corp.redhat.com (Postfix) with ESMTP id 989C4600CD; Thu, 11 Jul 2019 14:07:46 +0000 (UTC) From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Date: Thu, 11 Jul 2019 15:07:24 +0100 Message-Id: <20190711140742.31029-2-berrange@redhat.com> In-Reply-To: <20190711140742.31029-1-berrange@redhat.com> References: <20190711140742.31029-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v2 01/19] rpc: ensure all sockets bind to same port when service is NULL X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Thu, 11 Jul 2019 14:07:53 +0000 (UTC) When the service passed to getaddrinfo is NULL the kernel will choose a free port to bind to. In a dual stack though we will get separate sockets for IPv4 and IPv6 and we need them to bind to the same port number. Thus once the kerel has auto-selected a port for the first socket, we must disable auto-select for subsequent IP sockets and force reuse of the first port. Signed-off-by: Daniel P. Berrang=C3=A9 --- src/rpc/virnetsocket.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/rpc/virnetsocket.c b/src/rpc/virnetsocket.c index 254f39c4ec..fc13b1654a 100644 --- a/src/rpc/virnetsocket.c +++ b/src/rpc/virnetsocket.c @@ -311,6 +311,7 @@ int virNetSocketNewListenTCP(const char *nodename, int socketErrno =3D 0; int bindErrno =3D 0; virSocketAddr tmp_addr; + int port =3D 0; =20 *retsocks =3D NULL; *nretsocks =3D 0; @@ -379,7 +380,24 @@ int virNetSocketNewListenTCP(const char *nodename, } #endif =20 - if (bind(fd, runp->ai_addr, runp->ai_addrlen) < 0) { + addr.len =3D runp->ai_addrlen; + memcpy(&addr.data.sa, runp->ai_addr, runp->ai_addrlen); + + /* When service is NULL, we let the kernel auto-select the + * port. Once we've selected a port for one IP protocol + * though, we want to ensure we pick the same port for the + * other IP protocol + */ + if (port !=3D 0 && service =3D=3D NULL) { + if (runp->ai_addr->sa_family =3D=3D AF_INET) { + addr.data.inet4.sin_port =3D port; + } else if (addr.data.sa.sa_family =3D=3D AF_INET6) { + addr.data.inet6.sin6_port =3D port; + } + VIR_DEBUG("Used saved port %d", port); + } + + if (bind(fd, &addr.data.sa, addr.len) < 0) { if (errno !=3D EADDRINUSE && errno !=3D EADDRNOTAVAIL) { virReportSystemError(errno, "%s", _("Unable to bind to por= t")); goto error; @@ -396,6 +414,14 @@ int virNetSocketNewListenTCP(const char *nodename, goto error; } =20 + if (port =3D=3D 0 && service =3D=3D NULL) { + if (addr.data.sa.sa_family =3D=3D AF_INET) + port =3D addr.data.inet4.sin_port; + else if (addr.data.sa.sa_family =3D=3D AF_INET6) + port =3D addr.data.inet6.sin6_port; + VIR_DEBUG("Saved port %d", port); + } + VIR_DEBUG("%p f=3D%d f=3D%d", &addr, runp->ai_family, addr.data.sa= .sa_family); =20 if (VIR_EXPAND_N(socks, nsocks, 1) < 0) --=20 2.21.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list