]> git.ipfire.org Git - thirdparty/libvirt.git/commit
util: make virSocketAddrIPv4 a union
authorLaine Stump <laine@redhat.com>
Fri, 9 Aug 2024 01:09:43 +0000 (21:09 -0400)
committerLaine Stump <laine@redhat.com>
Sat, 21 Sep 2024 18:39:05 +0000 (14:39 -0400)
commit009464902a4f8bc2d50c9e899e0eff0379202f88
tree519e52687342aeafb9e39ca5ab9bb3eb7d9c998c
parent14623a34247d56581fe9c8a86fd1d1299ed07f3c
util: make virSocketAddrIPv4 a union

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 <laine@redhat.com>
src/util/virsocketaddr.c