glib-aux: optimize nm_ip_addr_is_null()

The nm_ip_addr_*() APIs are supposed to work with unaligned input,
so we could use them while parsing binary data (where the field
may not be properly aligned). For that reason, it used to first
copy the argument to a local (properly aligned) variable.

Rework that a bit, and use unaligned_read_ne32() for IPv4.
This commit is contained in:
Thomas Haller 2023-03-09 11:07:05 +01:00
parent 9530553311
commit 9b48c7f373
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728

View file

@ -3,6 +3,8 @@
#ifndef __NM_INET_UTILS_H__
#define __NM_INET_UTILS_H__
#include "libnm-std-aux/unaligned-fundamental.h"
typedef union _NMIPAddr {
guint8 addr_ptr[sizeof(struct in6_addr)];
in_addr_t addr4;
@ -90,14 +92,15 @@ nm_ip_addr_set(int addr_family, gpointer dst, gconstpointer src)
static inline gboolean
nm_ip_addr_is_null(int addr_family, gconstpointer addr)
{
NMIPAddr a;
struct in6_addr a6;
nm_ip_addr_set(addr_family, &a, addr);
nm_assert(addr);
if (NM_IS_IPv4(addr_family))
return a.addr4 == 0;
return unaligned_read_ne32(addr) == 0;
return IN6_IS_ADDR_UNSPECIFIED(&a.addr6);
memcpy(&a6, addr, sizeof(struct in6_addr));
return IN6_IS_ADDR_UNSPECIFIED(&a6);
}
static inline NMIPAddr