From 5c42cdb287c3aa3f48b7ddeefb578a4557c1252b Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 5 Sep 2017 15:25:34 +0200 Subject: [PATCH] all: use _nm_utils_ip4_*() utils functions --- libnm-core/nm-utils.c | 10 ++-------- libnm-core/tests/test-general.c | 6 +++--- libnm-util/nm-utils.c | 9 ++------- src/devices/nm-device.c | 2 +- src/dhcp/nm-dhcp-dhclient-utils.c | 2 +- src/dhcp/nm-dhcp-utils.c | 6 +++--- src/dnsmasq/nm-dnsmasq-utils.c | 2 +- src/nm-core-utils.c | 2 +- src/nm-ip4-config.c | 8 ++++---- src/platform/nm-fake-platform.c | 2 +- src/platform/nm-linux-platform.c | 2 +- src/platform/nm-platform.c | 4 ++-- src/platform/nmp-object.c | 4 ++-- src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c | 4 ++-- src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c | 4 ++-- 15 files changed, 28 insertions(+), 39 deletions(-) diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c index 9966b7c68f..36169e2b70 100644 --- a/libnm-core/nm-utils.c +++ b/libnm-core/nm-utils.c @@ -1490,10 +1490,9 @@ nm_utils_ip4_netmask_to_prefix (guint32 netmask) guint32 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: * @ip: an IPv4 address (in network byte order) @@ -1509,12 +1508,7 @@ nm_utils_ip4_prefix_to_netmask (guint32 prefix) 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 */ + return _nm_utils_ip4_get_default_prefix (ip); } /** diff --git a/libnm-core/tests/test-general.c b/libnm-core/tests/test-general.c index 00ac2cebe5..d33a6e87d2 100644 --- a/libnm-core/tests/test-general.c +++ b/libnm-core/tests/test-general.c @@ -3249,7 +3249,7 @@ test_ip4_prefix_to_netmask (void) int 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); g_assert_cmpint (i, ==, plen); @@ -3277,8 +3277,8 @@ test_ip4_netmask_to_prefix (void) g_rand_set_seed (rand, 1); for (i = 2; i<=32; 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 = _nm_utils_ip4_prefix_to_netmask (i); + guint32 netmask_lowest_bit = netmask & ~_nm_utils_ip4_prefix_to_netmask (i-1); g_assert_cmpint (i, ==, nm_utils_ip4_netmask_to_prefix (netmask)); diff --git a/libnm-util/nm-utils.c b/libnm-util/nm-utils.c index 04deecf986..fba87c5a79 100644 --- a/libnm-util/nm-utils.c +++ b/libnm-util/nm-utils.c @@ -1102,7 +1102,7 @@ nm_utils_ip4_netmask_to_prefix (guint32 netmask) guint32 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) 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 */ + return _nm_utils_ip4_get_default_prefix (ip); } /** diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c index 95c95479d8..8001e74b08 100644 --- a/src/devices/nm-device.c +++ b/src/devices/nm-device.c @@ -8385,7 +8385,7 @@ start_sharing (NMDevice *self, NMIP4Config *config) if (!ip4_addr || !ip4_addr->address) 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))) return FALSE; diff --git a/src/dhcp/nm-dhcp-dhclient-utils.c b/src/dhcp/nm-dhcp-dhclient-utils.c index 168c13192e..f66c5880e4 100644 --- a/src/dhcp/nm-dhcp-dhclient-utils.c +++ b/src/dhcp/nm-dhcp-dhclient-utils.c @@ -782,7 +782,7 @@ nm_dhcp_dhclient_read_lease_ip_configs (NMDedupMultiIndex *multi_idx, /* Get default netmask for the IP according to appropriate class. */ 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.lifetime = address.preferred = expiry; diff --git a/src/dhcp/nm-dhcp-utils.c b/src/dhcp/nm-dhcp-utils.c index 520046395a..f544a644bc 100644 --- a/src/dhcp/nm-dhcp-utils.c +++ b/src/dhcp/nm-dhcp-utils.c @@ -317,8 +317,8 @@ process_classful_routes (const char *iface, 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 whatever network number is specified in each route entry */ - route.plen = nm_utils_ip4_get_default_prefix (rt_addr); - if (rt_addr & ~nm_utils_ip4_prefix_to_netmask (route.plen)) { + route.plen = _nm_utils_ip4_get_default_prefix (rt_addr); + if (rt_addr & ~_nm_utils_ip4_prefix_to_netmask (route.plen)) { /* RFC 943: target not "this network"; using host routing */ route.plen = 32; } @@ -418,7 +418,7 @@ nm_dhcp_utils_ip4_config_from_options (NMDedupMultiIndex *multi_idx, _LOG2I (LOGD_DHCP4, iface, " plen %d (%s)", plen, str); } else { /* 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); } nm_platform_ip4_address_set_addr (&address, addr, plen); diff --git a/src/dnsmasq/nm-dnsmasq-utils.c b/src/dnsmasq/nm-dnsmasq-utils.c index cee52c3def..382b3aeb49 100644 --- a/src/dnsmasq/nm-dnsmasq-utils.c +++ b/src/dnsmasq/nm-dnsmasq-utils.c @@ -65,7 +65,7 @@ nm_dnsmasq_utils_get_range (const NMPlatformIP4Address *addr, 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. */ netmask = ntohl (netmask); diff --git a/src/nm-core-utils.c b/src/nm-core-utils.c index 9a3efd5dce..5cb53982e0 100644 --- a/src/nm-core-utils.c +++ b/src/nm-core-utils.c @@ -278,7 +278,7 @@ nm_utils_ipx_address_clear_host_address (int family, gpointer dst, gconstpointer in_addr_t 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: diff --git a/src/nm-ip4-config.c b/src/nm-ip4-config.c index 8740261f3a..ad939d0008 100644 --- a/src/nm-ip4-config.c +++ b/src/nm-ip4-config.c @@ -61,7 +61,7 @@ nm_ip_config_obj_id_equal_ip4_address (const NMPlatformIP4Address *a, { return a->address == b->address && 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 @@ -589,8 +589,8 @@ _addresses_sort_cmp (gconstpointer a, gconstpointer b, gpointer user_data) * subnet (and thus also the primary/secondary role) is * preserved. */ - n1 = a1->address & nm_utils_ip4_prefix_to_netmask (a1->plen); - n2 = a2->address & nm_utils_ip4_prefix_to_netmask (a2->plen); + n1 = a1->address & _nm_utils_ip4_prefix_to_netmask (a1->plen); + n2 = a2->address & _nm_utils_ip4_prefix_to_netmask (a2->plen); return memcmp (&n1, &n2, sizeof (guint32)); } @@ -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) { hash_u32 (sum, address->address); 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) { diff --git a/src/platform/nm-fake-platform.c b/src/platform/nm-fake-platform.c index 1512b4f090..9ecfaaa704 100644 --- a/src/platform/nm-fake-platform.c +++ b/src/platform/nm-fake-platform.c @@ -1071,7 +1071,7 @@ ipx_address_delete (NMPlatform *platform, || (addr && address->address != *((guint32 *) addr)) || (plen && address->plen != *plen) || ( 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; } else { const NMPlatformIP6Address *address = NMP_OBJECT_CAST_IP6_ADDRESS (o); diff --git a/src/platform/nm-linux-platform.c b/src/platform/nm-linux-platform.c index ff7fc71b6e..96cd99d658 100644 --- a/src/platform/nm-linux-platform.c +++ b/src/platform/nm-linux-platform.c @@ -2543,7 +2543,7 @@ _nl_msg_new_address (int nlmsg_type, && *((in_addr_t *) address) != 0) { 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); } diff --git a/src/platform/nm-platform.c b/src/platform/nm-platform.c index 9a350861a6..8542c945bd 100644 --- a/src/platform/nm-platform.c +++ b/src/platform/nm-platform.c @@ -3190,7 +3190,7 @@ ip4_addr_subnets_build_index (const GPtrArray *addresses, p_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)) { g_hash_table_insert (subnets, GUINT_TO_POINTER (net), p_address); continue; @@ -3251,7 +3251,7 @@ ip4_addr_subnets_is_secondary (const NMPObject *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)); nm_assert (p); if (!ip4_addr_subnets_is_plain_address (addresses, p)) { diff --git a/src/platform/nmp-object.c b/src/platform/nmp-object.c index 9d2a2bad5b..9c8845a4d5 100644 --- a/src/platform/nmp-object.c +++ b/src/platform/nmp-object.c @@ -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 (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 ? 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)); guint @@ -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->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, { hash = (guint) 2907861637u; diff --git a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c index d605acf81e..ae720b9803 100644 --- a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c +++ b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c @@ -399,7 +399,7 @@ read_full_ip4_address (shvarFile *ifcfg, prefix = nm_ip_address_get_prefix (base_addr); else { /* 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); } } @@ -588,7 +588,7 @@ read_one_ip4_route (shvarFile *ifcfg, return FALSE; if (has_key) { 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, "Invalid IP4 netmask '%s' \"%s\"", netmask_tag, nm_utils_inet4_ntop (netmask, inet_buf)); return FALSE; diff --git a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c index c2c3786330..c1b927aadf 100644 --- a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c +++ b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c @@ -2151,7 +2151,7 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) char buf[INET_ADDRSTRLEN]; 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 svUnsetValue (ifcfg, tag); @@ -2293,7 +2293,7 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) svSetValueStr (routefile, addr_key, nm_ip_route_get_dest (route)); 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)); svSetValueStr (routefile, netmask_key, &buf[0]);