From nobody Sun Dec 22 06:22:07 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1726778373912571.0459176265327; Thu, 19 Sep 2024 13:39:33 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 94F9B159A; Thu, 19 Sep 2024 16:39:30 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id AC676149D; Thu, 19 Sep 2024 16:38:58 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 117091470; Thu, 19 Sep 2024 16:38:55 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id B9566145D for ; Thu, 19 Sep 2024 16:38:53 -0400 (EDT) Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-21-QNDlczNlPruqubXqKkEHOg-1; Thu, 19 Sep 2024 16:38:51 -0400 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (unknown [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E74871977037 for ; Thu, 19 Sep 2024 20:38:50 +0000 (UTC) Received: from vhost3.router.laine.org (unknown [10.22.32.101]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 7266C1956088 for ; Thu, 19 Sep 2024 20:38:50 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.5 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1726778333; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=uocOR6Wt6BmfHvhLlCMVMvC7VbjE+7IRwk9ct6F+GnM=; b=PKATOibs6zYJZSdLZiBK+h3ptKe7E++lFnlg+GUFFR3ieLO/IAOv4V1kn/zWFXxHlbqY73 mzCDnlx/N4HGu2uBHXaiycTt5pYIbNWxb82XgYXcq44O4ijxngFiqiM3gb6EN6ihvINaUe pmChjuaGchiZJNBNYIgjv3eD9OVX9uI= X-MC-Unique: QNDlczNlPruqubXqKkEHOg-1 From: Laine Stump To: devel@lists.libvirt.org Subject: [PATCH 1/4] network: fix argument order/log level in message about firewall_backend Date: Thu, 19 Sep 2024 16:38:45 -0400 Message-ID: <20240919203848.44545-2-laine@redhat.com> In-Reply-To: <20240919203848.44545-1-laine@redhat.com> References: <20240919203848.44545-1-laine@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: WGTKJ2SGIH5CMVHK2SGJO376UMGDR633 X-Message-ID-Hash: WGTKJ2SGIH5CMVHK2SGJO376UMGDR633 X-MailFrom: laine@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1726778375440116600 Content-Type: text/plain; charset="utf-8"; x-default="true" Oops. Fixes: 64b966558cc6002fe150a0292a24eb2802a792c5 Signed-off-by: Laine Stump Reviewed-by: J=C3=A1n Tomko --- src/network/bridge_driver_conf.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/network/bridge_driver_conf.c b/src/network/bridge_driver_c= onf.c index 9da5e790b7..4e40286ee3 100644 --- a/src/network/bridge_driver_conf.c +++ b/src/network/bridge_driver_conf.c @@ -101,8 +101,8 @@ virNetworkLoadDriverConfig(virNetworkDriverConfig *cfg = G_GNUC_UNUSED, fwBackendStr, filename); return -1; } - VIR_INFO("firewall_backend setting requested from config file = %s: '%s'", - virFirewallBackendTypeToString(fwBackends[0]), filena= me); + VIR_DEBUG("firewall_backend setting requested from config file= %s: '%s'", + filename, virFirewallBackendTypeToString(fwBackends[= 0])); } } =20 --=20 2.46.0 From nobody Sun Dec 22 06:22:07 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1726778392139427.9971194755449; Thu, 19 Sep 2024 13:39:52 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id C4D1F1634; Thu, 19 Sep 2024 16:39:50 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id DE9AC15A8; Thu, 19 Sep 2024 16:39:01 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 146B21460; Thu, 19 Sep 2024 16:38:55 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 4543B13BC for ; Thu, 19 Sep 2024 16:38:54 -0400 (EDT) Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-623-Wj70HXetOyiZ95NLiaJrZw-1; Thu, 19 Sep 2024 16:38:52 -0400 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (unknown [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 9B2F1193EF42 for ; Thu, 19 Sep 2024 20:38:51 +0000 (UTC) Received: from vhost3.router.laine.org (unknown [10.22.32.101]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 31FAD195608A for ; Thu, 19 Sep 2024 20:38:51 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.5 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1726778334; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wT9AyuoONCUTR75KAGti2DZzMKaykrfedZf6xDNjrB8=; b=bVCqwkwdafgggNW2ntDIJssW85TGM2JFLu6No1y8JrPhey2S3mmihSFeAcO/ECp/ObIaSl Gr1PyDwI81/M5OBG3NRx1/us+BbqsSF+8h8i9vK9H/tmwRGgAdDEZsm4S+oiH6MMDyIPUI 6Y4BwmXCP3GUZkb2jMrcuY2QrcCazKE= X-MC-Unique: Wj70HXetOyiZ95NLiaJrZw-1 From: Laine Stump To: devel@lists.libvirt.org Subject: [PATCH 2/4] util: fix virSocketAddrMask() when source and result are the same object Date: Thu, 19 Sep 2024 16:38:46 -0400 Message-ID: <20240919203848.44545-3-laine@redhat.com> In-Reply-To: <20240919203848.44545-1-laine@redhat.com> References: <20240919203848.44545-1-laine@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: JFLF2AQEWHKMZ6QA33I464ZBK4GHIYY6 X-Message-ID-Hash: JFLF2AQEWHKMZ6QA33I464ZBK4GHIYY6 X-MailFrom: laine@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1726778393606116600 Content-Type: text/plain; charset="utf-8"; x-default="true" Many years ago (2011), virSocketAddrMask() had caused a bug by failing to initialize an IPv6-specific field in the result virSocketAddr. This was fixed by memset(0)ing the entire result (*network) at the beginning of the function (thus making sure anything and everything was initialized). The problem is that virSocketAddrMask() has a comment above it that says that the source (addr) and destination (network) arguments can point to the same virSocketAddr. But in that case, the memset(*network, 0) at the top of the function is actually doing a memset(*addr, 0), and so there is nothing left for all the assignments to copy except a giant field of 0's. Fortunately in the 13 years since the memset was added, nobody has ever called virSocketAddrMask() with addr and network being the same. This patch makes the code agree with the comment by copying/masking into a local virSocketAddr (which is initialized to all 0!) and then copying that to *network after it's finished assigning things from addr. Fixes: ba08c5932e556aa4f5101357127a6224c40e5ebe Signed-off-by: Laine Stump Reviewed-by: J=C3=A1n Tomko --- src/util/virsocketaddr.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/util/virsocketaddr.c b/src/util/virsocketaddr.c index 0a1ae1ac5f..60d8071da7 100644 --- a/src/util/virsocketaddr.c +++ b/src/util/virsocketaddr.c @@ -685,31 +685,34 @@ virSocketAddrMask(const virSocketAddr *addr, const virSocketAddr *netmask, virSocketAddr *network) { - memset(network, 0, sizeof(*network)); + virSocketAddr tmp =3D { 0 }; + if (addr->data.stor.ss_family !=3D netmask->data.stor.ss_family) { network->data.stor.ss_family =3D AF_UNSPEC; return -1; } =20 if (addr->data.stor.ss_family =3D=3D AF_INET) { - network->data.inet4.sin_addr.s_addr + tmp.data.inet4.sin_addr.s_addr =3D (addr->data.inet4.sin_addr.s_addr & netmask->data.inet4.sin_addr.s_addr); - network->data.inet4.sin_port =3D 0; - network->data.stor.ss_family =3D AF_INET; - network->len =3D addr->len; + tmp.data.inet4.sin_port =3D 0; + tmp.data.stor.ss_family =3D AF_INET; + tmp.len =3D addr->len; + *network =3D tmp; return 0; } if (addr->data.stor.ss_family =3D=3D AF_INET6) { size_t i; for (i =3D 0; i < 16; i++) { - network->data.inet6.sin6_addr.s6_addr[i] + tmp.data.inet6.sin6_addr.s6_addr[i] =3D (addr->data.inet6.sin6_addr.s6_addr[i] & netmask->data.inet6.sin6_addr.s6_addr[i]); } - network->data.inet6.sin6_port =3D 0; - network->data.stor.ss_family =3D AF_INET6; - network->len =3D addr->len; + tmp.data.inet6.sin6_port =3D 0; + tmp.data.stor.ss_family =3D AF_INET6; + tmp.len =3D addr->len; + *network =3D tmp; return 0; } network->data.stor.ss_family =3D AF_UNSPEC; --=20 2.46.0 From nobody Sun Dec 22 06:22:07 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1726778419048488.4897062031796; Thu, 19 Sep 2024 13:40:19 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id C7DC1169F; Thu, 19 Sep 2024 16:40:17 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 37C1516D5; Thu, 19 Sep 2024 16:39:03 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id AF90A1460; Thu, 19 Sep 2024 16:38:55 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id ADDB1146C for ; Thu, 19 Sep 2024 16:38:54 -0400 (EDT) Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-115-dWDTelv4NwiOFpSwkRNwKQ-1; Thu, 19 Sep 2024 16:38:53 -0400 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (unknown [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 5211A19776A7 for ; Thu, 19 Sep 2024 20:38:52 +0000 (UTC) Received: from vhost3.router.laine.org (unknown [10.22.32.101]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id DAC001956086 for ; Thu, 19 Sep 2024 20:38:51 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.5 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1726778334; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wx9szTgmEEmslGM1mhEGLaeUHJErI6kQnNo7Dn4LkM0=; b=H336GXD3r3s6OYYaHAiBBjJL1RixV5+kC7skwnqGnTWMQVQzzp0uENNY3SPS0lFJJqvBvw UIFVXCGRpawJ6OKY+FSuRAuyjro9d5EzVlUyAiZEL/I1wVQEfJgzkGtFSSqUvJoBitO5px e+0fnk4O+H3/nLm55UhuxGd0HG/glLw= X-MC-Unique: dWDTelv4NwiOFpSwkRNwKQ-1 From: Laine Stump To: devel@lists.libvirt.org Subject: [PATCH 3/4] util: make virSocketAddrIPv4 a union Date: Thu, 19 Sep 2024 16:38:47 -0400 Message-ID: <20240919203848.44545-4-laine@redhat.com> In-Reply-To: <20240919203848.44545-1-laine@redhat.com> References: <20240919203848.44545-1-laine@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: GXQOEI7ONCTHIHV4SFB2QHYC2F3TIWXU X-Message-ID-Hash: GXQOEI7ONCTHIHV4SFB2QHYC2F3TIWXU X-MailFrom: laine@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1726778421720116600 Content-Type: text/plain; charset="utf-8"; x-default="true" virSocketAddrIPv4 is a type used only internally by virsocketaddr.c. It is defined to be a character array, which leads to multiple occurences of extra bit fiddling and byte swapping for no good reason (except to confuse). An IPv4 address is really just a uint32_t with the bytes in network order, which is exactly the type of the s_addr member of the sockaddr_in that is a part of the publicly consumed struct virSocketAddr, and that we are copying in and out of a virSocketAddrIPv4. Sometimes it's simpler to just treat it as a network-order uint32_t, so let's make our virSocketAddrIPv4 a union that has both an unsigned char bytes[4] (for the times when we need to look one byte at a time) and a uint32_t val (for the times when it's simpler to treat it as a single value). For now we just change all the uses from, e.g. x[i] to x.bytes[y]; an upcoming patch will simplify some of the code to remove loops by using x.val instead of x.bytes when appropriate. Signed-off-by: Laine Stump Reviewed-by: J=C3=A1n Tomko --- src/util/virsocketaddr.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/util/virsocketaddr.c b/src/util/virsocketaddr.c index 60d8071da7..4180fa1282 100644 --- a/src/util/virsocketaddr.c +++ b/src/util/virsocketaddr.c @@ -28,7 +28,11 @@ * Helpers to extract the IP arrays from the virSocketAddr * * That part is the less portable of the module */ -typedef unsigned char virSocketAddrIPv4[4]; +typedef union { + uint32_t val; + unsigned char bytes[4]; +} virSocketAddrIPv4; + typedef unsigned short virSocketAddrIPv6[8]; typedef unsigned char virSocketAddrIPv6Bytes[16]; typedef unsigned char virSocketAddrIPv6Nibbles[32]; @@ -46,7 +50,7 @@ virSocketAddrGetIPv4Addr(const virSocketAddr *addr, val =3D ntohl(addr->data.inet4.sin_addr.s_addr); =20 for (i =3D 0; i < 4; i++) { - (*tab)[3 - i] =3D val & 0xFF; + tab->bytes[3 - i] =3D val & 0xFF; val >>=3D 8; } =20 @@ -838,7 +842,7 @@ int virSocketAddrCheckNetmask(virSocketAddr *addr1, vir= SocketAddr *addr2, return -1; =20 for (i =3D 0; i < 4; i++) { - if ((t1[i] & tm[i]) !=3D (t2[i] & tm[i])) + if ((t1.bytes[i] & tm.bytes[i]) !=3D (t2.bytes[i] & tm.bytes[i= ])) return 0; } =20 @@ -986,14 +990,14 @@ virSocketAddrGetRange(virSocketAddr *start, virSocket= Addr *end, * are the same */ for (i =3D 0; i < 2; i++) { - if (t1[i] !=3D t2[i]) { + if (t1.bytes[i] !=3D t2.bytes[i]) { virReportError(VIR_ERR_INTERNAL_ERROR, _("range %1$s - %2$s is too large (> 65535)= "), startStr, endStr); return -1; } } - ret =3D (t2[2] - t1[2]) * 256 + (t2[3] - t1[3]); + ret =3D (t2.bytes[2] - t1.bytes[2]) * 256 + (t2.bytes[3] - t1.byte= s[3]); if (ret < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, _("range %1$s - %2$s is reversed "), @@ -1064,7 +1068,7 @@ int virSocketAddrGetNumNetmaskBits(const virSocketAdd= r *netmask) return -1; =20 for (i =3D 0; i < 4; i++) - if (tm[i] =3D=3D 0xff) + if (tm.bytes[i] =3D=3D 0xff) c +=3D 8; else break; @@ -1075,7 +1079,7 @@ int virSocketAddrGetNumNetmaskBits(const virSocketAdd= r *netmask) j =3D i << 3; while (j < (8 * 4)) { bit =3D 1 << (7 - (j & 7)); - if ((tm[j >> 3] & bit)) + if ((tm.bytes[j >> 3] & bit)) c++; else break; @@ -1084,7 +1088,7 @@ int virSocketAddrGetNumNetmaskBits(const virSocketAdd= r *netmask) =20 while (j < (8 * 4)) { bit =3D 1 << (7 - (j & 7)); - if ((tm[j >> 3] & bit)) + if ((tm.bytes[j >> 3] & bit)) return -1; j++; } @@ -1326,7 +1330,7 @@ virSocketAddrPTRDomain(const virSocketAddr *addr, return -1; =20 for (i =3D prefix / 8; i > 0; i--) - virBufferAsprintf(&buf, "%u.", ip[i - 1]); + virBufferAsprintf(&buf, "%u.", ip.bytes[i - 1]); =20 virBufferAddLit(&buf, VIR_SOCKET_ADDR_IPV4_ARPA); } else if (VIR_SOCKET_ADDR_IS_FAMILY(addr, AF_INET6)) { @@ -1389,7 +1393,7 @@ virSocketAddrBytes(const virSocketAddr *addr, return 0; =20 virSocketAddrGetIPv4Addr(addr, &ip); - memcpy(bytes, ip, sizeof(ip)); + memcpy(bytes, &ip, sizeof(ip)); return sizeof(ip); } =20 --=20 2.46.0 From nobody Sun Dec 22 06:22:07 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 172677844019357.491675759936015; Thu, 19 Sep 2024 13:40:40 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id DFAE8146E; Thu, 19 Sep 2024 16:40:38 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 1D45315FB; Thu, 19 Sep 2024 16:39:05 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 0933113BC; Thu, 19 Sep 2024 16:38:56 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 7BA1013BC for ; Thu, 19 Sep 2024 16:38:55 -0400 (EDT) Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-605-o1Zg-M6EMKC3LKXhK5km1g-1; Thu, 19 Sep 2024 16:38:53 -0400 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (unknown [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 0BC441944B37 for ; Thu, 19 Sep 2024 20:38:53 +0000 (UTC) Received: from vhost3.router.laine.org (unknown [10.22.32.101]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 920DB1956086 for ; Thu, 19 Sep 2024 20:38:52 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.5 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1726778335; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Rm6idb0HN+7mXktctwSF+7hA/oNkA6KaJz5TrbdA3PE=; b=VxUUc2uhzo/7RS2PklIr7RDap8QBVlIrOx1GvAEZ06JTDxXw1u4dANLpz1f7UV7cms1Ypo m4EHX0eKdkSigyuqvEDqfeGyR1j2HNOSWOzWvvlbeFl211WnxD+h9D2wB/UmKqrt2Xev3J l4xyXB8cURSuriqG7KkdB48/ITHRGl0= X-MC-Unique: o1Zg-M6EMKC3LKXhK5km1g-1 From: Laine Stump To: devel@lists.libvirt.org Subject: [PATCH 4/4] util: use uint32 instead of char[4] for several virSocketAddrIPv4 operations Date: Thu, 19 Sep 2024 16:38:48 -0400 Message-ID: <20240919203848.44545-5-laine@redhat.com> In-Reply-To: <20240919203848.44545-1-laine@redhat.com> References: <20240919203848.44545-1-laine@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: JUO4JGOSFYXAVEKB5UY47IUEJQNEO2NB X-Message-ID-Hash: JUO4JGOSFYXAVEKB5UY47IUEJQNEO2NB X-MailFrom: laine@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1726778441644116600 Content-Type: text/plain; charset="utf-8"; x-default="true" These 3 functions are easier to understand, and more efficient, when the IPv4 address is viewed as a uint32 rather than an array of bytes. virsocketAddrGetIPv4Addr() has bothered me for a long time - it was doing ntohl of the address into a temporary uint32, and then a loop one-by-one swapping the order of all the bytes back to network order. Of course this only works as described on little-endian architectures - on big-endian architectures the first assignment won't swap the bytes' ordering, but the loop assumes the bytes are now in little-endian order and "swaps them back", so the result will be incorrect. (Do we not support any big-endian targets that would have exposed this bug long before now??) virSocketAddrCheckNetmask() was checking each byte of the two addresses individually, when it could instead just do the operation once on the full 32 bit values. virSocketGetRange() was checking for "range > 65535" by seeing if the first 2 bytes of the start and end were different, and then doing arithmetic combining the lower two bytes (along with necessary bit shifting to account for network byte order) to determine the exact size of the range. Instead we can just get the ntohl of start & end, and do the math directly. Signed-off-by: Laine Stump Reviewed-by: J=C3=A1n Tomko --- src/util/virsocketaddr.c | 47 +++++++++++++++------------------------- 1 file changed, 18 insertions(+), 29 deletions(-) diff --git a/src/util/virsocketaddr.c b/src/util/virsocketaddr.c index 4180fa1282..61fe820d73 100644 --- a/src/util/virsocketaddr.c +++ b/src/util/virsocketaddr.c @@ -41,18 +41,10 @@ static int virSocketAddrGetIPv4Addr(const virSocketAddr *addr, virSocketAddrIPv4 *tab) { - unsigned long val; - size_t i; - if (!addr || !tab || addr->data.stor.ss_family !=3D AF_INET) return -1; =20 - val =3D ntohl(addr->data.inet4.sin_addr.s_addr); - - for (i =3D 0; i < 4; i++) { - tab->bytes[3 - i] =3D val & 0xFF; - val >>=3D 8; - } + tab->val =3D addr->data.inet4.sin_addr.s_addr; =20 return 0; } @@ -841,10 +833,8 @@ int virSocketAddrCheckNetmask(virSocketAddr *addr1, vi= rSocketAddr *addr2, (virSocketAddrGetIPv4Addr(netmask, &tm) < 0)) return -1; =20 - for (i =3D 0; i < 4; i++) { - if ((t1.bytes[i] & tm.bytes[i]) !=3D (t2.bytes[i] & tm.bytes[i= ])) - return 0; - } + if ((t1.val & tm.val) !=3D (t2.val & tm.val)) + return 0; =20 } else if (addr1->data.stor.ss_family =3D=3D AF_INET6) { virSocketAddrIPv6 t1, t2, tm; @@ -976,35 +966,34 @@ virSocketAddrGetRange(virSocketAddr *start, virSocket= Addr *end, } =20 if (VIR_SOCKET_ADDR_IS_FAMILY(start, AF_INET)) { - virSocketAddrIPv4 t1, t2; + virSocketAddrIPv4 startv4, endv4; + uint32_t startHost, endHost; =20 - if (virSocketAddrGetIPv4Addr(start, &t1) < 0 || - virSocketAddrGetIPv4Addr(end, &t2) < 0) { + if (virSocketAddrGetIPv4Addr(start, &startv4) < 0 || + virSocketAddrGetIPv4Addr(end, &endv4) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, _("failed to get IPv4 address for start or end = of range %1$s - %2$s"), startStr, endStr); return -1; } =20 - /* legacy check that everything except the last two bytes - * are the same - */ - for (i =3D 0; i < 2; i++) { - if (t1.bytes[i] !=3D t2.bytes[i]) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("range %1$s - %2$s is too large (> 65535)= "), - startStr, endStr); - return -1; - } + startHost =3D ntohl(startv4.val); + endHost =3D ntohl(endv4.val); + + if (endHost - startHost > 65535) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("range %1$s - %2$s is too large (> 65535)"), + startStr, endStr); + return -1; } - ret =3D (t2.bytes[2] - t1.bytes[2]) * 256 + (t2.bytes[3] - t1.byte= s[3]); - if (ret < 0) { + + if (endHost < startHost) { virReportError(VIR_ERR_INTERNAL_ERROR, _("range %1$s - %2$s is reversed "), startStr, endStr); return -1; } - ret++; + ret =3D endHost - startHost + 1; } else if (VIR_SOCKET_ADDR_IS_FAMILY(start, AF_INET6)) { virSocketAddrIPv6 t1, t2; =20 --=20 2.46.0