diff options
Diffstat (limited to 'libnm-core')
-rw-r--r-- | libnm-core/nm-utils.c | 33 | ||||
-rw-r--r-- | libnm-core/nm-utils.h | 23 | ||||
-rw-r--r-- | libnm-core/tests/test-general.c | 30 |
3 files changed, 75 insertions, 11 deletions
diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c index d3c3bef1dc..bafc326c7b 100644 --- a/libnm-core/nm-utils.c +++ b/libnm-core/nm-utils.c @@ -2353,6 +2353,39 @@ nm_utils_hwaddr_valid (const char *asc, gssize length) } /** + * nm_utils_hwaddr_canonical: + * @asc: the ASCII representation of a hardware address + * @length: the length of address that @asc is expected to convert to + * (or -1 to accept any length up to %NM_UTILS_HWADDR_LEN_MAX) + * + * Parses @asc to see if it is a valid hardware address of the given + * length, and if so, returns it in canonical form (uppercase, with + * leading 0s as needed, and with colons rather than hyphens). + * + * Return value: (transfer full): the canonicalized address if @asc appears to + * be a valid hardware address of the indicated length, %NULL if not. + */ +char * +nm_utils_hwaddr_canonical (const char *asc, gssize length) +{ + guint8 buf[NM_UTILS_HWADDR_LEN_MAX]; + + g_return_val_if_fail (asc != NULL, NULL); + g_return_val_if_fail (length == -1 || (length > 0 && length <= NM_UTILS_HWADDR_LEN_MAX), NULL); + + if (length == -1) { + length = hwaddr_binary_len (asc); + if (length == 0 || length > NM_UTILS_HWADDR_LEN_MAX) + return NULL; + } + + if (nm_utils_hwaddr_aton (asc, buf, length) == NULL) + return NULL; + + return g_strdup (nm_utils_hwaddr_ntoa (buf, length)); +} + +/** * nm_utils_hwaddr_matches: * @hwaddr1: pointer to a binary or ASCII hardware address, or %NULL * @hwaddr1_len: size of @hwaddr1, or -1 if @hwaddr1 is ASCII diff --git a/libnm-core/nm-utils.h b/libnm-core/nm-utils.h index 7402cd921d..a94730793e 100644 --- a/libnm-core/nm-utils.h +++ b/libnm-core/nm-utils.h @@ -141,17 +141,18 @@ const char *nm_utils_wifi_strength_bars (guint8 strength); */ #define NM_UTILS_HWADDR_LEN_MAX 20 /* INFINIBAND_ALEN */ -gsize nm_utils_hwaddr_len (int type) G_GNUC_PURE; - -char *nm_utils_hwaddr_ntoa (gconstpointer addr, gsize length); -GByteArray *nm_utils_hwaddr_atoba (const char *asc, gsize length); -guint8 *nm_utils_hwaddr_aton (const char *asc, gpointer buffer, gsize length); - -gboolean nm_utils_hwaddr_valid (const char *asc, gssize length); -gboolean nm_utils_hwaddr_matches (gconstpointer hwaddr1, - gssize hwaddr1_len, - gconstpointer hwaddr2, - gssize hwaddr2_len); +gsize nm_utils_hwaddr_len (int type) G_GNUC_PURE; + +char *nm_utils_hwaddr_ntoa (gconstpointer addr, gsize length); +GByteArray *nm_utils_hwaddr_atoba (const char *asc, gsize length); +guint8 *nm_utils_hwaddr_aton (const char *asc, gpointer buffer, gsize length); + +gboolean nm_utils_hwaddr_valid (const char *asc, gssize length); +char *nm_utils_hwaddr_canonical (const char *asc, gssize length); +gboolean nm_utils_hwaddr_matches (gconstpointer hwaddr1, + gssize hwaddr1_len, + gconstpointer hwaddr2, + gssize hwaddr2_len); char *nm_utils_bin2hexstr (const char *bytes, int len, int final_len); int nm_utils_hex2byte (const char *hex); diff --git a/libnm-core/tests/test-general.c b/libnm-core/tests/test-general.c index 32061426ae..6c111d04e5 100644 --- a/libnm-core/tests/test-general.c +++ b/libnm-core/tests/test-general.c @@ -2239,6 +2239,35 @@ test_hwaddr_equal (void) } static void +test_hwaddr_canonical (void) +{ + const char *string = "00:1A:2B:03:44:05"; + const char *lower_string = "00:1a:2b:03:44:05"; + const char *short_string = "0:1a:2b:3:44:5"; + const char *invalid_string = "00:1A:2B"; + char *canonical; + + canonical = nm_utils_hwaddr_canonical (string, ETH_ALEN); + g_assert_cmpstr (canonical, ==, string); + g_free (canonical); + + canonical = nm_utils_hwaddr_canonical (lower_string, ETH_ALEN); + g_assert_cmpstr (canonical, ==, string); + g_free (canonical); + + canonical = nm_utils_hwaddr_canonical (short_string, ETH_ALEN); + g_assert_cmpstr (canonical, ==, string); + g_free (canonical); + + canonical = nm_utils_hwaddr_canonical (invalid_string, ETH_ALEN); + g_assert_cmpstr (canonical, ==, NULL); + + canonical = nm_utils_hwaddr_canonical (invalid_string, -1); + g_assert_cmpstr (canonical, ==, invalid_string); + g_free (canonical); +} + +static void test_connection_changed_cb (NMConnection *connection, gboolean *data) { *data = TRUE; @@ -3372,6 +3401,7 @@ int main (int argc, char **argv) g_test_add_func ("/core/general/test_hwaddr_aton_no_leading_zeros", test_hwaddr_aton_no_leading_zeros); g_test_add_func ("/core/general/test_hwaddr_aton_malformed", test_hwaddr_aton_malformed); g_test_add_func ("/core/general/test_hwaddr_equal", test_hwaddr_equal); + g_test_add_func ("/core/general/test_hwaddr_canonical", test_hwaddr_canonical); g_test_add_func ("/core/general/test_ip4_prefix_to_netmask", test_ip4_prefix_to_netmask); g_test_add_func ("/core/general/test_ip4_netmask_to_prefix", test_ip4_netmask_to_prefix); |