From nobody Sun Feb 8 04:04:00 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 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