summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWen Liang <liangwen12year@gmail.com>2022-06-12 19:50:09 -0400
committerThomas Haller <thaller@redhat.com>2022-09-08 19:42:01 +0200
commit75349dc56669b06009d3d1cd13e6f21fd248a5b8 (patch)
treee425e89e8790964e4d163986fe50e33788daa3a6
parentc413d7c6574dc58c0bb39bc5d7b08d0d5961cff7 (diff)
downloadNetworkManager-75349dc56669b06009d3d1cd13e6f21fd248a5b8.tar.gz
glib-aux: add "NM_IPV4LO_NETWORK" defines and similar
Co-authored-by: Thomas Haller <thaller@redhat.com>
-rw-r--r--src/libnm-glib-aux/nm-inet-utils.h10
-rw-r--r--src/libnm-glib-aux/tests/test-shared-general.c4
2 files changed, 11 insertions, 3 deletions
diff --git a/src/libnm-glib-aux/nm-inet-utils.h b/src/libnm-glib-aux/nm-inet-utils.h
index 043ad79930..b730bda06d 100644
--- a/src/libnm-glib-aux/nm-inet-utils.h
+++ b/src/libnm-glib-aux/nm-inet-utils.h
@@ -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
diff --git a/src/libnm-glib-aux/tests/test-shared-general.c b/src/libnm-glib-aux/tests/test-shared-general.c
index 5d9072e177..a49bbe0a20 100644
--- a/src/libnm-glib-aux/tests/test-shared-general.c
+++ b/src/libnm-glib-aux/tests/test-shared-general.c
@@ -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);
}
/*****************************************************************************/