libnm-util: add nm_utils_hwaddr_valid()

This commit is contained in:
Dan Winship 2013-06-11 11:20:44 -03:00
parent bc1a764e1a
commit 9a73db17d5
3 changed files with 28 additions and 0 deletions

View file

@ -546,6 +546,7 @@ global:
nm_utils_hwaddr_ntoa;
nm_utils_hwaddr_ntoa_len;
nm_utils_hwaddr_type;
nm_utils_hwaddr_valid;
nm_utils_iface_valid_name;
nm_utils_init;
nm_utils_ip4_addresses_from_gvalue;

View file

@ -2063,6 +2063,31 @@ nm_utils_hwaddr_ntoa_len (gconstpointer addr, gsize length)
return g_string_free (out, FALSE);
}
/**
* nm_utils_hwaddr_valid:
* @asc: the ASCII representation of a hardware address
*
* Parses @asc to see if it is a valid hardware address of some type.
*
* Return value: %TRUE if @asc appears to be a valid hardware address
* of some type, %FALSE if not.
*
* Since: 0.9.10
*/
gboolean
nm_utils_hwaddr_valid (const char *asc)
{
guint8 buf[NM_UTILS_HWADDR_LEN_MAX];
int in_len = strlen (asc), out_len;
if ((in_len + 1) % 3 != 0)
return FALSE;
out_len = (in_len + 1) / 3;
if (out_len > NM_UTILS_HWADDR_LEN_MAX)
return FALSE;
return nm_utils_hwaddr_aton_len (asc, buf, out_len) != NULL;
}
/**
* nm_utils_bin2hexstr:
* @bytes: an array of bytes

View file

@ -144,6 +144,8 @@ guint8 *nm_utils_hwaddr_aton (const char *asc, int type, gpointer buffer);
char *nm_utils_hwaddr_ntoa_len (gconstpointer addr, gsize length);
guint8 *nm_utils_hwaddr_aton_len (const char *asc, gpointer buffer, gsize length);
gboolean nm_utils_hwaddr_valid (const char *asc);
char *nm_utils_bin2hexstr (const char *bytes, int len, int final_len);
int nm_utils_hex2byte (const char *hex);
char *nm_utils_hexstr2bin (const char *hex, size_t len);