From nobody Sat Feb 7 02:36:55 2026 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