mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-05-05 13:28:02 +02:00
shared,dhcp: add _nm_utils_ip4_get_default_prefix0() helper
This commit is contained in:
parent
94e474fa62
commit
67dd25a396
3 changed files with 25 additions and 54 deletions
|
|
@ -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)
|
||||
{
|
||||
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 */
|
||||
/*****************************************************************************/
|
||||
|
||||
return 24; /* Class C - 255.255.255.0 */
|
||||
guint32
|
||||
_nm_utils_ip4_get_default_prefix0(in_addr_t ip)
|
||||
{
|
||||
/* 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;
|
||||
}
|
||||
|
||||
guint32
|
||||
_nm_utils_ip4_get_default_prefix(in_addr_t ip)
|
||||
{
|
||||
return _nm_utils_ip4_get_default_prefix0(ip) ?: 24;
|
||||
}
|
||||
|
||||
gboolean
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue