shared: add _nm_utils_hwaddr_aton_exact()

This is the same as libnm's nm_utils_hwaddr_aton(), which however
is public API.

We want to use this function also without libnm(-core). Hence add
the helper to "shared/nm-glib-aux".
This commit is contained in:
Thomas Haller 2021-01-08 19:01:28 +01:00
parent cda8badc57
commit 6aa6da2b08
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
3 changed files with 21 additions and 12 deletions

View file

@ -3995,16 +3995,13 @@ GByteArray *
nm_utils_hwaddr_atoba(const char *asc, gsize length)
{
GByteArray *ba;
gsize l;
g_return_val_if_fail(asc, NULL);
g_return_val_if_fail(length > 0 && length <= NM_UTILS_HWADDR_LEN_MAX, NULL);
ba = g_byte_array_sized_new(length);
g_byte_array_set_size(ba, length);
if (!_nm_utils_hwaddr_aton(asc, ba->data, length, &l))
goto fail;
if (length != l)
if (!_nm_utils_hwaddr_aton_exact(asc, ba->data, length))
goto fail;
return ba;
@ -4029,17 +4026,11 @@ fail:
guint8 *
nm_utils_hwaddr_aton(const char *asc, gpointer buffer, gsize length)
{
gsize l;
g_return_val_if_fail(asc, NULL);
g_return_val_if_fail(buffer, NULL);
g_return_val_if_fail(length > 0 && length <= NM_UTILS_HWADDR_LEN_MAX, NULL);
if (!_nm_utils_hwaddr_aton(asc, buffer, length, &l))
return NULL;
if (length != l)
return NULL;
return buffer;
return _nm_utils_hwaddr_aton_exact(asc, buffer, length);
}
/**

View file

@ -2092,6 +2092,24 @@ _nm_utils_hwaddr_aton(const char *asc, gpointer buffer, gsize buffer_length, gsi
out_length);
}
static inline guint8 *
_nm_utils_hwaddr_aton_exact(const char *asc, gpointer buffer, gsize buffer_length)
{
g_return_val_if_fail(asc, NULL);
g_return_val_if_fail(buffer, NULL);
g_return_val_if_fail(buffer_length > 0, NULL);
return nm_utils_hexstr2bin_full(asc,
FALSE,
TRUE,
FALSE,
":-",
buffer_length,
buffer,
buffer_length,
NULL);
}
static inline const char *
_nm_utils_hwaddr_ntoa(gconstpointer addr,
gsize addr_len,

View file

@ -1439,7 +1439,7 @@ nmp_utils_ethtool_set_wake_on_lan(int ifindex,
wol_info.wolopts |= WAKE_MAGIC;
if (wol_password) {
if (!nm_utils_hwaddr_aton(wol_password, wol_info.sopass, ETH_ALEN)) {
if (!_nm_utils_hwaddr_aton_exact(wol_password, wol_info.sopass, ETH_ALEN)) {
nm_log_dbg(LOGD_PLATFORM,
"ethtool[%d]: couldn't parse Wake-on-LAN password '%s'",
ifindex,