summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2021-02-10 16:38:21 +0100
committerThomas Haller <thaller@redhat.com>2021-02-11 09:23:15 +0100
commit67dd25a396588070d12d0ab1e975679833f49842 (patch)
tree1aef2bf94e11017023ab86599d40f8978185504f
parent94e474fa624542bc5eb11a894d265b4e15c2d132 (diff)
downloadNetworkManager-67dd25a396588070d12d0ab1e975679833f49842.tar.gz
shared,dhcp: add _nm_utils_ip4_get_default_prefix0() helper
-rw-r--r--shared/nm-glib-aux/nm-shared-utils.c36
-rw-r--r--shared/nm-glib-aux/nm-shared-utils.h3
-rw-r--r--src/core/dhcp/nm-dhcp-nettools.c36
3 files changed, 23 insertions, 52 deletions
diff --git a/shared/nm-glib-aux/nm-shared-utils.c b/shared/nm-glib-aux/nm-shared-utils.c
index 492a3d5098..3215a33b5b 100644
--- a/shared/nm-glib-aux/nm-shared-utils.c
+++ b/shared/nm-glib-aux/nm-shared-utils.c
@@ -786,27 +786,27 @@ nm_utils_ip6_address_same_prefix_cmp(const struct in6_addr *addr_a,
return 0;
}
-/**
- * _nm_utils_ip4_get_default_prefix:
- * @ip: an IPv4 address (in network byte order)
- *
- * When the Internet was originally set up, various ranges of IP addresses were
- * segmented into three network classes: A, B, and C. This function will return
- * a prefix that is associated with the IP address specified defining where it
- * falls in the predefined classes.
- *
- * Returns: the default class prefix for the given IP
- **/
-/* The function is originally from ipcalc.c of Red Hat's initscripts. */
+/*****************************************************************************/
+
guint32
-_nm_utils_ip4_get_default_prefix(guint32 ip)
+_nm_utils_ip4_get_default_prefix0(in_addr_t ip)
{
- if (((ntohl(ip) & 0xFF000000) >> 24) <= 127)
- return 8; /* Class A - 255.0.0.0 */
- else if (((ntohl(ip) & 0xFF000000) >> 24) <= 191)
- return 16; /* Class B - 255.255.0.0 */
+ /* The function is originally from ipcalc.c of Red Hat's initscripts. */
+ switch (ntohl(ip) >> 24) {
+ case 0 ... 127:
+ return 8; /* Class A */
+ case 128 ... 191:
+ return 16; /* Class B */
+ case 192 ... 223:
+ return 24; /* Class C */
+ }
+ return 0;
+}
- return 24; /* Class C - 255.255.255.0 */
+guint32
+_nm_utils_ip4_get_default_prefix(in_addr_t ip)
+{
+ return _nm_utils_ip4_get_default_prefix0(ip) ?: 24;
}
gboolean
diff --git a/shared/nm-glib-aux/nm-shared-utils.h b/shared/nm-glib-aux/nm-shared-utils.h
index c432b125b6..7d330458c1 100644
--- a/shared/nm-glib-aux/nm-shared-utils.h
+++ b/shared/nm-glib-aux/nm-shared-utils.h
@@ -699,7 +699,8 @@ nm_utils_escaped_tokens_options_escape_val(const char *val, char **out_to_free)
/*****************************************************************************/
guint32 _nm_utils_ip4_prefix_to_netmask(guint32 prefix);
-guint32 _nm_utils_ip4_get_default_prefix(guint32 ip);
+guint32 _nm_utils_ip4_get_default_prefix0(in_addr_t ip);
+guint32 _nm_utils_ip4_get_default_prefix(in_addr_t ip);
gconstpointer
nm_utils_ipx_address_clear_host_address(int family, gpointer dst, gconstpointer src, guint8 plen);
diff --git a/src/core/dhcp/nm-dhcp-nettools.c b/src/core/dhcp/nm-dhcp-nettools.c
index 37f812c9fb..1381de47ae 100644
--- a/src/core/dhcp/nm-dhcp-nettools.c
+++ b/src/core/dhcp/nm-dhcp-nettools.c
@@ -86,27 +86,7 @@ set_error_nettools(GError **error, int r, const char *message)
#define DHCP_MAX_FQDN_LENGTH 255
-enum {
- NM_IN_ADDR_CLASS_A,
- NM_IN_ADDR_CLASS_B,
- NM_IN_ADDR_CLASS_C,
- NM_IN_ADDR_CLASS_INVALID,
-};
-
-static int
-in_addr_class(in_addr_t addr)
-{
- switch (ntohl(addr) >> 24) {
- case 0 ... 127:
- return NM_IN_ADDR_CLASS_A;
- case 128 ... 191:
- return NM_IN_ADDR_CLASS_B;
- case 192 ... 223:
- return NM_IN_ADDR_CLASS_C;
- default:
- return NM_IN_ADDR_CLASS_INVALID;
- }
-}
+/*****************************************************************************/
static gboolean
lease_option_consume(uint8_t **datap, size_t *n_datap, void *out, size_t n_out)
@@ -158,19 +138,9 @@ lease_option_consume_route(uint8_t ** datap,
if (!lease_option_consume_in_addr(&data, &n_data, &dest))
return FALSE;
- switch (in_addr_class(dest)) {
- case NM_IN_ADDR_CLASS_A:
- plen = 8;
- break;
- case NM_IN_ADDR_CLASS_B:
- plen = 16;
- break;
- case NM_IN_ADDR_CLASS_C:
- plen = 24;
- break;
- case NM_IN_ADDR_CLASS_INVALID:
+ plen = _nm_utils_ip4_get_default_prefix0(dest);
+ if (plen == 0)
return FALSE;
- }
}
dest = nm_utils_ip4_address_clear_host_address(dest, plen);