Commit 5c42cdb2 authored by Thomas Haller's avatar Thomas Haller

all: use _nm_utils_ip4_*() utils functions

parent 7319fdd7
...@@ -1490,10 +1490,9 @@ nm_utils_ip4_netmask_to_prefix (guint32 netmask) ...@@ -1490,10 +1490,9 @@ nm_utils_ip4_netmask_to_prefix (guint32 netmask)
guint32 guint32
nm_utils_ip4_prefix_to_netmask (guint32 prefix) nm_utils_ip4_prefix_to_netmask (guint32 prefix)
{ {
return prefix < 32 ? ~htonl(0xFFFFFFFF >> prefix) : 0xFFFFFFFF; return _nm_utils_ip4_prefix_to_netmask (prefix);
} }
/** /**
* nm_utils_ip4_get_default_prefix: * nm_utils_ip4_get_default_prefix:
* @ip: an IPv4 address (in network byte order) * @ip: an IPv4 address (in network byte order)
...@@ -1509,12 +1508,7 @@ nm_utils_ip4_prefix_to_netmask (guint32 prefix) ...@@ -1509,12 +1508,7 @@ nm_utils_ip4_prefix_to_netmask (guint32 prefix)
guint32 guint32
nm_utils_ip4_get_default_prefix (guint32 ip) nm_utils_ip4_get_default_prefix (guint32 ip)
{ {
if (((ntohl (ip) & 0xFF000000) >> 24) <= 127) return _nm_utils_ip4_get_default_prefix (ip);
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 */
} }
/** /**
......
...@@ -3249,7 +3249,7 @@ test_ip4_prefix_to_netmask (void) ...@@ -3249,7 +3249,7 @@ test_ip4_prefix_to_netmask (void)
int i; int i;
for (i = 0; i<=32; i++) { for (i = 0; i<=32; i++) {
guint32 netmask = nm_utils_ip4_prefix_to_netmask (i); guint32 netmask = _nm_utils_ip4_prefix_to_netmask (i);
int plen = nm_utils_ip4_netmask_to_prefix (netmask); int plen = nm_utils_ip4_netmask_to_prefix (netmask);
g_assert_cmpint (i, ==, plen); g_assert_cmpint (i, ==, plen);
...@@ -3277,8 +3277,8 @@ test_ip4_netmask_to_prefix (void) ...@@ -3277,8 +3277,8 @@ test_ip4_netmask_to_prefix (void)
g_rand_set_seed (rand, 1); g_rand_set_seed (rand, 1);
for (i = 2; i<=32; i++) { for (i = 2; i<=32; i++) {
guint32 netmask = nm_utils_ip4_prefix_to_netmask (i); guint32 netmask = _nm_utils_ip4_prefix_to_netmask (i);
guint32 netmask_lowest_bit = netmask & ~nm_utils_ip4_prefix_to_netmask (i-1); guint32 netmask_lowest_bit = netmask & ~_nm_utils_ip4_prefix_to_netmask (i-1);
g_assert_cmpint (i, ==, nm_utils_ip4_netmask_to_prefix (netmask)); g_assert_cmpint (i, ==, nm_utils_ip4_netmask_to_prefix (netmask));
......
...@@ -1102,7 +1102,7 @@ nm_utils_ip4_netmask_to_prefix (guint32 netmask) ...@@ -1102,7 +1102,7 @@ nm_utils_ip4_netmask_to_prefix (guint32 netmask)
guint32 guint32
nm_utils_ip4_prefix_to_netmask (guint32 prefix) nm_utils_ip4_prefix_to_netmask (guint32 prefix)
{ {
return prefix < 32 ? ~htonl(0xFFFFFFFF >> prefix) : 0xFFFFFFFF; return _nm_utils_ip4_prefix_to_netmask (prefix);
} }
...@@ -1121,12 +1121,7 @@ nm_utils_ip4_prefix_to_netmask (guint32 prefix) ...@@ -1121,12 +1121,7 @@ nm_utils_ip4_prefix_to_netmask (guint32 prefix)
guint32 guint32
nm_utils_ip4_get_default_prefix (guint32 ip) nm_utils_ip4_get_default_prefix (guint32 ip)
{ {
if (((ntohl (ip) & 0xFF000000) >> 24) <= 127) return _nm_utils_ip4_get_default_prefix (ip);
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 */
} }
/** /**
......
...@@ -8385,7 +8385,7 @@ start_sharing (NMDevice *self, NMIP4Config *config) ...@@ -8385,7 +8385,7 @@ start_sharing (NMDevice *self, NMIP4Config *config)
if (!ip4_addr || !ip4_addr->address) if (!ip4_addr || !ip4_addr->address)
return FALSE; return FALSE;
netmask = nm_utils_ip4_prefix_to_netmask (ip4_addr->plen); netmask = _nm_utils_ip4_prefix_to_netmask (ip4_addr->plen);
if (!inet_ntop (AF_INET, &netmask, str_mask, sizeof (str_mask))) if (!inet_ntop (AF_INET, &netmask, str_mask, sizeof (str_mask)))
return FALSE; return FALSE;
......
...@@ -782,7 +782,7 @@ nm_dhcp_dhclient_read_lease_ip_configs (NMDedupMultiIndex *multi_idx, ...@@ -782,7 +782,7 @@ nm_dhcp_dhclient_read_lease_ip_configs (NMDedupMultiIndex *multi_idx,
/* Get default netmask for the IP according to appropriate class. */ /* Get default netmask for the IP according to appropriate class. */
if (!address.plen) if (!address.plen)
address.plen = nm_utils_ip4_get_default_prefix (address.address); address.plen = _nm_utils_ip4_get_default_prefix (address.address);
address.timestamp = now_monotonic_ts; address.timestamp = now_monotonic_ts;
address.lifetime = address.preferred = expiry; address.lifetime = address.preferred = expiry;
......
...@@ -317,8 +317,8 @@ process_classful_routes (const char *iface, ...@@ -317,8 +317,8 @@ process_classful_routes (const char *iface,
The Static Routes option (option 33) does not provide a subnet mask The Static Routes option (option 33) does not provide a subnet mask
for each route - it is assumed that the subnet mask is implicit in for each route - it is assumed that the subnet mask is implicit in
whatever network number is specified in each route entry */ whatever network number is specified in each route entry */
route.plen = nm_utils_ip4_get_default_prefix (rt_addr); route.plen = _nm_utils_ip4_get_default_prefix (rt_addr);
if (rt_addr & ~nm_utils_ip4_prefix_to_netmask (route.plen)) { if (rt_addr & ~_nm_utils_ip4_prefix_to_netmask (route.plen)) {
/* RFC 943: target not "this network"; using host routing */ /* RFC 943: target not "this network"; using host routing */
route.plen = 32; route.plen = 32;
} }
...@@ -418,7 +418,7 @@ nm_dhcp_utils_ip4_config_from_options (NMDedupMultiIndex *multi_idx, ...@@ -418,7 +418,7 @@ nm_dhcp_utils_ip4_config_from_options (NMDedupMultiIndex *multi_idx,
_LOG2I (LOGD_DHCP4, iface, " plen %d (%s)", plen, str); _LOG2I (LOGD_DHCP4, iface, " plen %d (%s)", plen, str);
} else { } else {
/* Get default netmask for the IP according to appropriate class. */ /* Get default netmask for the IP according to appropriate class. */
plen = nm_utils_ip4_get_default_prefix (addr); plen = _nm_utils_ip4_get_default_prefix (addr);
_LOG2I (LOGD_DHCP4, iface, " plen %d (default)", plen); _LOG2I (LOGD_DHCP4, iface, " plen %d (default)", plen);
} }
nm_platform_ip4_address_set_addr (&address, addr, plen); nm_platform_ip4_address_set_addr (&address, addr, plen);
......
...@@ -65,7 +65,7 @@ nm_dnsmasq_utils_get_range (const NMPlatformIP4Address *addr, ...@@ -65,7 +65,7 @@ nm_dnsmasq_utils_get_range (const NMPlatformIP4Address *addr,
prefix = 24; prefix = 24;
} }
netmask = nm_utils_ip4_prefix_to_netmask (prefix); netmask = _nm_utils_ip4_prefix_to_netmask (prefix);
/* treat addresses in host-order from here on. */ /* treat addresses in host-order from here on. */
netmask = ntohl (netmask); netmask = ntohl (netmask);
......
...@@ -278,7 +278,7 @@ nm_utils_ipx_address_clear_host_address (int family, gpointer dst, gconstpointer ...@@ -278,7 +278,7 @@ nm_utils_ipx_address_clear_host_address (int family, gpointer dst, gconstpointer
in_addr_t in_addr_t
nm_utils_ip4_address_clear_host_address (in_addr_t addr, guint8 plen) nm_utils_ip4_address_clear_host_address (in_addr_t addr, guint8 plen)
{ {
return addr & nm_utils_ip4_prefix_to_netmask (plen); return addr & _nm_utils_ip4_prefix_to_netmask (plen);
} }
/* nm_utils_ip6_address_clear_host_address: /* nm_utils_ip6_address_clear_host_address:
......
...@@ -61,7 +61,7 @@ nm_ip_config_obj_id_equal_ip4_address (const NMPlatformIP4Address *a, ...@@ -61,7 +61,7 @@ nm_ip_config_obj_id_equal_ip4_address (const NMPlatformIP4Address *a,
{ {
return a->address == b->address return a->address == b->address
&& a->plen == b->plen && a->plen == b->plen
&& ((a->peer_address ^ b->peer_address) & nm_utils_ip4_prefix_to_netmask (a->plen)) == 0; && ((a->peer_address ^ b->peer_address) & _nm_utils_ip4_prefix_to_netmask (a->plen)) == 0;
} }
gboolean gboolean
...@@ -589,8 +589,8 @@ _addresses_sort_cmp (gconstpointer a, gconstpointer b, gpointer user_data) ...@@ -589,8 +589,8 @@ _addresses_sort_cmp (gconstpointer a, gconstpointer b, gpointer user_data)
* subnet (and thus also the primary/secondary role) is * subnet (and thus also the primary/secondary role) is
* preserved. * preserved.
*/ */
n1 = a1->address & nm_utils_ip4_prefix_to_netmask (a1->plen); n1 = a1->address & _nm_utils_ip4_prefix_to_netmask (a1->plen);
n2 = a2->address & nm_utils_ip4_prefix_to_netmask (a2->plen); n2 = a2->address & _nm_utils_ip4_prefix_to_netmask (a2->plen);
return memcmp (&n1, &n2, sizeof (guint32)); return memcmp (&n1, &n2, sizeof (guint32));
} }
...@@ -2698,7 +2698,7 @@ nm_ip4_config_hash (const NMIP4Config *self, GChecksum *sum, gboolean dns_only) ...@@ -2698,7 +2698,7 @@ nm_ip4_config_hash (const NMIP4Config *self, GChecksum *sum, gboolean dns_only)
nm_ip_config_iter_ip4_address_for_each (&ipconf_iter, self, &address) { nm_ip_config_iter_ip4_address_for_each (&ipconf_iter, self, &address) {
hash_u32 (sum, address->address); hash_u32 (sum, address->address);
hash_u32 (sum, address->plen); hash_u32 (sum, address->plen);
hash_u32 (sum, address->peer_address & nm_utils_ip4_prefix_to_netmask (address->plen)); hash_u32 (sum, address->peer_address & _nm_utils_ip4_prefix_to_netmask (address->plen));
} }
nm_ip_config_iter_ip4_route_for_each (&ipconf_iter, self, &route) { nm_ip_config_iter_ip4_route_for_each (&ipconf_iter, self, &route) {
......
...@@ -1071,7 +1071,7 @@ ipx_address_delete (NMPlatform *platform, ...@@ -1071,7 +1071,7 @@ ipx_address_delete (NMPlatform *platform,
|| (addr && address->address != *((guint32 *) addr)) || (addr && address->address != *((guint32 *) addr))
|| (plen && address->plen != *plen) || (plen && address->plen != *plen)
|| ( peer_addr || ( peer_addr
&& (((peer_addr_i ^ address->peer_address) & nm_utils_ip4_prefix_to_netmask (address->plen)) != 0))) && (((peer_addr_i ^ address->peer_address) & _nm_utils_ip4_prefix_to_netmask (address->plen)) != 0)))
continue; continue;
} else { } else {
const NMPlatformIP6Address *address = NMP_OBJECT_CAST_IP6_ADDRESS (o); const NMPlatformIP6Address *address = NMP_OBJECT_CAST_IP6_ADDRESS (o);
......
...@@ -2543,7 +2543,7 @@ _nl_msg_new_address (int nlmsg_type, ...@@ -2543,7 +2543,7 @@ _nl_msg_new_address (int nlmsg_type,
&& *((in_addr_t *) address) != 0) { && *((in_addr_t *) address) != 0) {
in_addr_t broadcast; in_addr_t broadcast;
broadcast = *((in_addr_t *) address) | ~nm_utils_ip4_prefix_to_netmask (plen); broadcast = *((in_addr_t *) address) | ~_nm_utils_ip4_prefix_to_netmask (plen);
NLA_PUT (msg, IFA_BROADCAST, addr_len, &broadcast); NLA_PUT (msg, IFA_BROADCAST, addr_len, &broadcast);
} }
......
...@@ -3190,7 +3190,7 @@ ip4_addr_subnets_build_index (const GPtrArray *addresses, ...@@ -3190,7 +3190,7 @@ ip4_addr_subnets_build_index (const GPtrArray *addresses,
p_address = &addresses->pdata[i]; p_address = &addresses->pdata[i];
address = NMP_OBJECT_CAST_IP4_ADDRESS (addresses->pdata[i]); address = NMP_OBJECT_CAST_IP4_ADDRESS (addresses->pdata[i]);
net = address->address & nm_utils_ip4_prefix_to_netmask (address->plen); net = address->address & _nm_utils_ip4_prefix_to_netmask (address->plen);
if (!g_hash_table_lookup_extended (subnets, GUINT_TO_POINTER (net), NULL, &p)) { if (!g_hash_table_lookup_extended (subnets, GUINT_TO_POINTER (net), NULL, &p)) {
g_hash_table_insert (subnets, GUINT_TO_POINTER (net), p_address); g_hash_table_insert (subnets, GUINT_TO_POINTER (net), p_address);
continue; continue;
...@@ -3251,7 +3251,7 @@ ip4_addr_subnets_is_secondary (const NMPObject *address, ...@@ -3251,7 +3251,7 @@ ip4_addr_subnets_is_secondary (const NMPObject *address,
a = NMP_OBJECT_CAST_IP4_ADDRESS (address); a = NMP_OBJECT_CAST_IP4_ADDRESS (address);
net = a->address & nm_utils_ip4_prefix_to_netmask (a->plen); net = a->address & _nm_utils_ip4_prefix_to_netmask (a->plen);
p = g_hash_table_lookup (subnets, GUINT_TO_POINTER (net)); p = g_hash_table_lookup (subnets, GUINT_TO_POINTER (net));
nm_assert (p); nm_assert (p);
if (!ip4_addr_subnets_is_plain_address (addresses, p)) { if (!ip4_addr_subnets_is_plain_address (addresses, p)) {
......
...@@ -740,7 +740,7 @@ _vt_cmd_plobj_to_string_id_##type (const NMPlatformObject *_obj, char *buf, gsiz ...@@ -740,7 +740,7 @@ _vt_cmd_plobj_to_string_id_##type (const NMPlatformObject *_obj, char *buf, gsiz
_vt_cmd_plobj_to_string_id (link, NMPlatformLink, "%d", obj->ifindex); _vt_cmd_plobj_to_string_id (link, NMPlatformLink, "%d", obj->ifindex);
_vt_cmd_plobj_to_string_id (ip4_address, NMPlatformIP4Address, "%d: %s/%d%s%s", obj->ifindex, nm_utils_inet4_ntop ( obj->address, buf1), obj->plen, _vt_cmd_plobj_to_string_id (ip4_address, NMPlatformIP4Address, "%d: %s/%d%s%s", obj->ifindex, nm_utils_inet4_ntop ( obj->address, buf1), obj->plen,
obj->peer_address != obj->address ? "," : "", obj->peer_address != obj->address ? "," : "",
obj->peer_address != obj->address ? nm_utils_inet4_ntop (obj->peer_address & nm_utils_ip4_prefix_to_netmask (obj->plen), buf2) : ""); obj->peer_address != obj->address ? nm_utils_inet4_ntop (obj->peer_address & _nm_utils_ip4_prefix_to_netmask (obj->plen), buf2) : "");
_vt_cmd_plobj_to_string_id (ip6_address, NMPlatformIP6Address, "%d: %s", obj->ifindex, nm_utils_inet6_ntop (&obj->address, buf1)); _vt_cmd_plobj_to_string_id (ip6_address, NMPlatformIP6Address, "%d: %s", obj->ifindex, nm_utils_inet6_ntop (&obj->address, buf1));
guint guint
...@@ -1102,7 +1102,7 @@ _vt_cmd_plobj_id_hash (ip4_address, NMPlatformIP4Address, { ...@@ -1102,7 +1102,7 @@ _vt_cmd_plobj_id_hash (ip4_address, NMPlatformIP4Address, {
hash = NM_HASH_COMBINE (hash, obj->plen); hash = NM_HASH_COMBINE (hash, obj->plen);
hash = NM_HASH_COMBINE (hash, obj->address); hash = NM_HASH_COMBINE (hash, obj->address);
/* for IPv4 we must also consider the net-part of the peer-address (IFA_ADDRESS) */ /* for IPv4 we must also consider the net-part of the peer-address (IFA_ADDRESS) */
hash = NM_HASH_COMBINE (hash, (obj->peer_address & nm_utils_ip4_prefix_to_netmask (obj->plen))); hash = NM_HASH_COMBINE (hash, (obj->peer_address & _nm_utils_ip4_prefix_to_netmask (obj->plen)));
}) })
_vt_cmd_plobj_id_hash (ip6_address, NMPlatformIP6Address, { _vt_cmd_plobj_id_hash (ip6_address, NMPlatformIP6Address, {
hash = (guint) 2907861637u; hash = (guint) 2907861637u;
......
...@@ -399,7 +399,7 @@ read_full_ip4_address (shvarFile *ifcfg, ...@@ -399,7 +399,7 @@ read_full_ip4_address (shvarFile *ifcfg,
prefix = nm_ip_address_get_prefix (base_addr); prefix = nm_ip_address_get_prefix (base_addr);
else { else {
/* Try to autodetermine the prefix for the address' class */ /* Try to autodetermine the prefix for the address' class */
prefix = nm_utils_ip4_get_default_prefix (ipaddr); prefix = _nm_utils_ip4_get_default_prefix (ipaddr);
PARSE_WARNING ("missing %s, assuming %s/%d", prefix_tag, nm_utils_inet4_ntop (ipaddr, inet_buf), prefix); PARSE_WARNING ("missing %s, assuming %s/%d", prefix_tag, nm_utils_inet4_ntop (ipaddr, inet_buf), prefix);
} }
} }
...@@ -588,7 +588,7 @@ read_one_ip4_route (shvarFile *ifcfg, ...@@ -588,7 +588,7 @@ read_one_ip4_route (shvarFile *ifcfg,
return FALSE; return FALSE;
if (has_key) { if (has_key) {
prefix = nm_utils_ip4_netmask_to_prefix (netmask); prefix = nm_utils_ip4_netmask_to_prefix (netmask);
if (prefix == 0 || netmask != nm_utils_ip4_prefix_to_netmask (prefix)) { if (prefix == 0 || netmask != _nm_utils_ip4_prefix_to_netmask (prefix)) {
g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION,
"Invalid IP4 netmask '%s' \"%s\"", netmask_tag, nm_utils_inet4_ntop (netmask, inet_buf)); "Invalid IP4 netmask '%s' \"%s\"", netmask_tag, nm_utils_inet4_ntop (netmask, inet_buf));
return FALSE; return FALSE;
......
...@@ -2151,7 +2151,7 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) ...@@ -2151,7 +2151,7 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error)
char buf[INET_ADDRSTRLEN]; char buf[INET_ADDRSTRLEN];
svSetValueStr (ifcfg, tag, svSetValueStr (ifcfg, tag,
nm_utils_inet4_ntop (nm_utils_ip4_prefix_to_netmask (prefix), buf)); nm_utils_inet4_ntop (_nm_utils_ip4_prefix_to_netmask (prefix), buf));
} else } else
svUnsetValue (ifcfg, tag); svUnsetValue (ifcfg, tag);
...@@ -2293,7 +2293,7 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) ...@@ -2293,7 +2293,7 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error)
svSetValueStr (routefile, addr_key, nm_ip_route_get_dest (route)); svSetValueStr (routefile, addr_key, nm_ip_route_get_dest (route));
memset (buf, 0, sizeof (buf)); memset (buf, 0, sizeof (buf));
netmask = nm_utils_ip4_prefix_to_netmask (nm_ip_route_get_prefix (route)); netmask = _nm_utils_ip4_prefix_to_netmask (nm_ip_route_get_prefix (route));
inet_ntop (AF_INET, (const void *) &netmask, &buf[0], sizeof (buf)); inet_ntop (AF_INET, (const void *) &netmask, &buf[0], sizeof (buf));
svSetValueStr (routefile, netmask_key, &buf[0]); svSetValueStr (routefile, netmask_key, &buf[0]);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment