glib-aux: add "NM_IPV4LO_NETWORK" defines and similar

Co-authored-by: Thomas Haller <thaller@redhat.com>
This commit is contained in:
Wen Liang 2022-06-12 19:50:09 -04:00 committed by Thomas Haller
parent c413d7c657
commit 75349dc566
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
2 changed files with 11 additions and 3 deletions

View file

@ -254,15 +254,19 @@ gboolean nm_ip6_addr_is_ula(const struct in6_addr *address);
/*****************************************************************************/
#define NM_IPV4LL_NETWORK ((in_addr_t) htonl(0xA9FE0000lu))
#define NM_IPV4LL_NETMASK ((in_addr_t) htonl(0xFFFF0000lu))
#define NM_IPV4LL_NETWORK ((in_addr_t) htonl(0xA9FE0000lu)) /* 169.254.0.0 */
#define NM_IPV4LL_NETMASK ((in_addr_t) htonl(0xFFFF0000lu)) /* 255.255.0.0 */
#define NM_IPV4LO_NETWORK ((in_addr_t) htonl(0x7F000000lu)) /* 127.0.0.0 */
#define NM_IPV4LO_NETMASK ((in_addr_t) htonl(0xFF000000lu)) /* 255.0.0.0 */
#define NM_IPV4LO_PREFIXLEN 8
#define NM_IPV4LO_ADDR1 ((in_addr_t) htonl(0x7F000001lu)) /* 127.0.0.1 */
static inline gboolean
nm_ip4_addr_is_loopback(in_addr_t addr)
{
/* There is also IN_LOOPBACK() in <linux/in.h>, but there the
* argument is in host order not `in_addr_t`. */
return (addr & htonl(0xFF000000u)) == htonl(0x7F000000u);
return (addr & NM_IPV4LO_NETMASK) == NM_IPV4LO_NETWORK;
}
static inline gboolean

View file

@ -259,6 +259,10 @@ test_nm_ip4_addr_is_loopback(void)
g_assert(!nm_ip4_addr_is_loopback(nmtst_inet4_from_string("126.5.0.1")));
g_assert(!nm_ip4_addr_is_loopback(nmtst_inet4_from_string("128.5.0.1")));
g_assert(!nm_ip4_addr_is_loopback(nmtst_inet4_from_string("129.5.0.1")));
g_assert_cmpint(nmtst_inet4_from_string("127.0.0.0"), ==, NM_IPV4LO_NETWORK);
g_assert_cmpint(nmtst_inet4_from_string("127.0.0.1"), ==, NM_IPV4LO_ADDR1);
g_assert_cmpint(nmtst_inet4_from_string("255.0.0.0"), ==, NM_IPV4LO_NETMASK);
g_assert_cmpint(nm_ip4_addr_netmask_to_prefix(NM_IPV4LO_NETMASK), ==, NM_IPV4LO_PREFIXLEN);
}
/*****************************************************************************/