diff options
author | Thomas Haller <thaller@redhat.com> | 2014-01-09 15:51:23 +0100 |
---|---|---|
committer | Dan Williams <dcbw@redhat.com> | 2014-01-10 13:34:57 -0600 |
commit | 4dd6ab8f4b47e906be84442be8d68e82e41a1a24 (patch) | |
tree | 168c276486cc3b0c3b30558cf10ff30842c84af3 /libnm-util/nm-utils.c | |
parent | a6432ef4d11e5396db44c85e6ed195c3e8e36ed1 (diff) | |
download | NetworkManager-4dd6ab8f4b47e906be84442be8d68e82e41a1a24.tar.gz |
libnl-util: refactor nm_utils_ip4_prefix_to_netmask/netmask_to_prefix
- use a more efficient implementation for prefix_to_netmask
- fix netmask_to_prefix to behave consistently in case of
invalid netmask
- remove unused duplicated functions from NetworkManagerUtils.c
- add test functions
Based-on-patch-by: Pavel Šimerda <psimerda@redhat.com>
Signed-off-by: Thomas Haller <thaller@redhat.com>
Related: https://bugzilla.gnome.org/show_bug.cgi?id=721771
Diffstat (limited to 'libnm-util/nm-utils.c')
-rw-r--r-- | libnm-util/nm-utils.c | 46 |
1 files changed, 20 insertions, 26 deletions
diff --git a/libnm-util/nm-utils.c b/libnm-util/nm-utils.c index 0e3d99b946..4272ddb752 100644 --- a/libnm-util/nm-utils.c +++ b/libnm-util/nm-utils.c @@ -1060,24 +1060,27 @@ nm_utils_ip4_routes_to_gvalue (GSList *list, GValue *value) guint32 nm_utils_ip4_netmask_to_prefix (guint32 netmask) { - guchar *p, *end; - guint32 prefix = 0; - - p = (guchar *) &netmask; - end = p + sizeof (guint32); - - while ((*p == 0xFF) && p < end) { - prefix += 8; - p++; + guint32 prefix; + guint8 v; + const guint8 *p = (guint8 *) &netmask; + + if (p[3]) { + prefix = 24; + v = p[3]; + } else if (p[2]) { + prefix = 16; + v = p[2]; + } else if (p[1]) { + prefix = 8; + v = p[1]; + } else { + prefix = 0; + v = p[0]; } - if (p < end) { - guchar v = *p; - - while (v) { - prefix++; - v <<= 1; - } + while (v) { + prefix++; + v <<= 1; } return prefix; @@ -1092,16 +1095,7 @@ nm_utils_ip4_netmask_to_prefix (guint32 netmask) guint32 nm_utils_ip4_prefix_to_netmask (guint32 prefix) { - guint32 msk = 0x80000000; - guint32 netmask = 0; - - while (prefix > 0) { - netmask |= msk; - msk >>= 1; - prefix--; - } - - return (guint32) htonl (netmask); + return prefix < 32 ? ~htonl(0xFFFFFFFF >> prefix) : 0xFFFFFFFF; } |