summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--clients/cloud-setup/nm-cloud-setup-utils.c4
-rw-r--r--libnm-core/nm-core-internal.h3
-rw-r--r--libnm-core/nm-utils.c106
-rw-r--r--shared/nm-glib-aux/nm-shared-utils.h42
-rw-r--r--src/devices/nm-device.c2
-rw-r--r--src/platform/nm-platform.c14
6 files changed, 83 insertions, 88 deletions
diff --git a/clients/cloud-setup/nm-cloud-setup-utils.c b/clients/cloud-setup/nm-cloud-setup-utils.c
index 14f60fe4b9..55a9aebf0e 100644
--- a/clients/cloud-setup/nm-cloud-setup-utils.c
+++ b/clients/cloud-setup/nm-cloud-setup-utils.c
@@ -501,7 +501,9 @@ nmcs_utils_hwaddr_normalize (const char *hwaddr, gssize len)
nm_assert (hwaddr);
hwaddr = nm_strndup_a (300, hwaddr, len, &hwaddr_clone);
}
- if(!nm_utils_hexstr2bin_full (hwaddr, FALSE, FALSE, FALSE, ":-", sizeof (buf), buf, sizeof (buf), NULL))
+ /* we cannot use _nm_utils_hwaddr_aton() because that requires a delimiter.
+ * Azure exposes MAC addresses without delimiter, so accept that too. */
+ if (!nm_utils_hexstr2bin_full (hwaddr, FALSE, FALSE, FALSE, ":-", sizeof (buf), buf, sizeof (buf), NULL))
return NULL;
return nm_utils_hwaddr_ntoa (buf, sizeof (buf));
diff --git a/libnm-core/nm-core-internal.h b/libnm-core/nm-core-internal.h
index 79f545446e..9cdafbf8b3 100644
--- a/libnm-core/nm-core-internal.h
+++ b/libnm-core/nm-core-internal.h
@@ -339,9 +339,6 @@ guint nm_setting_ethtool_init_features (NMSettingEthtool *setting,
#define NM_UTILS_HWADDR_LEN_MAX_STR (NM_UTILS_HWADDR_LEN_MAX * 3)
-guint8 *_nm_utils_hwaddr_aton (const char *asc, gpointer buffer, gsize buffer_length, gsize *out_length);
-const char *nm_utils_hwaddr_ntoa_buf (gconstpointer addr, gsize addr_len, gboolean upper_case, char *buf, gsize buf_len);
-
gboolean nm_utils_is_valid_iface_name_utf8safe (const char *utf8safe_name);
GSList * _nm_utils_hash_values_to_slist (GHashTable *hash);
diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c
index e066d00cd7..58ec444c39 100644
--- a/libnm-core/nm-utils.c
+++ b/libnm-core/nm-utils.c
@@ -4001,20 +4001,22 @@ nm_utils_wifi_strength_bars (guint8 strength)
*
* Returns the length in octets of a hardware address of type @type.
*
- * It is an error to call this function with any value other than
+ * Before 1.28, it was an error to call this function with any value other than
* <literal>ARPHRD_ETHER</literal> or <literal>ARPHRD_INFINIBAND</literal>.
*
- * Return value: the length.
+ * Return value: the length or zero if the type is unrecognized.
*/
gsize
nm_utils_hwaddr_len (int type)
{
- if (type == ARPHRD_ETHER)
+ switch (type) {
+ case ARPHRD_ETHER:
return ETH_ALEN;
- else if (type == ARPHRD_INFINIBAND)
+ case ARPHRD_INFINIBAND:
return INFINIBAND_ALEN;
-
- g_return_val_if_reached (0);
+ default:
+ return 0;
+ }
}
/**
@@ -4041,8 +4043,6 @@ nm_utils_hexstr2bin (const char *hex)
return g_bytes_new_take (buffer, len);
}
-#define hwaddr_aton(asc, buffer, buffer_len, out_len) nm_utils_hexstr2bin_full ((asc), FALSE, TRUE, FALSE, ":-", 0, (buffer), (buffer_len), (out_len))
-
/**
* nm_utils_hwaddr_atoba:
* @asc: the ASCII representation of a hardware address
@@ -4065,7 +4065,7 @@ nm_utils_hwaddr_atoba (const char *asc, gsize length)
ba = g_byte_array_sized_new (length);
g_byte_array_set_size (ba, length);
- if (!hwaddr_aton (asc, ba->data, length, &l))
+ if (!_nm_utils_hwaddr_aton (asc, ba->data, length, &l))
goto fail;
if (length != l)
goto fail;
@@ -4077,36 +4077,6 @@ fail:
}
/**
- * _nm_utils_hwaddr_aton:
- * @asc: the ASCII representation of a hardware address
- * @buffer: buffer to store the result into. Must have
- * at least a size of @buffer_length.
- * @buffer_length: the length of the input buffer @buffer.
- * The result must fit into that buffer, otherwise
- * the function fails and returns %NULL.
- * @out_length: the output length in case of success.
- *
- * Parses @asc and converts it to binary form in @buffer.
- * Bytes in @asc can be separated by colons (:), or hyphens (-), but not mixed.
- *
- * It is like nm_utils_hwaddr_aton(), but contrary to that it
- * can parse addresses of any length. That is, you don't need
- * to know the length before-hand.
- *
- * Return value: @buffer, or %NULL if @asc couldn't be parsed.
- */
-guint8 *
-_nm_utils_hwaddr_aton (const char *asc, gpointer buffer, gsize buffer_length, gsize *out_length)
-{
- g_return_val_if_fail (asc, NULL);
- g_return_val_if_fail (buffer, NULL);
- g_return_val_if_fail (buffer_length > 0, NULL);
- g_return_val_if_fail (out_length, NULL);
-
- return hwaddr_aton (asc, buffer, buffer_length, out_length);
-}
-
-/**
* nm_utils_hwaddr_aton:
* @asc: the ASCII representation of a hardware address
* @buffer: (type guint8) (array length=length): buffer to store the result into
@@ -4128,7 +4098,7 @@ nm_utils_hwaddr_aton (const char *asc, gpointer buffer, gsize length)
g_return_val_if_fail (buffer, NULL);
g_return_val_if_fail (length > 0 && length <= NM_UTILS_HWADDR_LEN_MAX, NULL);
- if (!hwaddr_aton (asc, buffer, length, &l))
+ if (!_nm_utils_hwaddr_aton (asc, buffer, length, &l))
return NULL;
if (length != l)
return NULL;
@@ -4186,18 +4156,6 @@ nm_utils_hwaddr_ntoa (gconstpointer addr, gsize length)
return nm_utils_bin2hexstr_full (addr, length, ':', TRUE, NULL);
}
-const char *
-nm_utils_hwaddr_ntoa_buf (gconstpointer addr, gsize addr_len, gboolean upper_case, char *buf, gsize buf_len)
-{
- g_return_val_if_fail (addr, NULL);
- g_return_val_if_fail (addr_len > 0, NULL);
- g_return_val_if_fail (buf, NULL);
- if (buf_len < addr_len * 3)
- g_return_val_if_reached (NULL);
-
- return nm_utils_bin2hexstr_full (addr, addr_len, ':', upper_case, buf);
-}
-
/**
* nm_utils_hwaddr_valid:
* @asc: the ASCII representation of a hardware address
@@ -4217,17 +4175,17 @@ nm_utils_hwaddr_valid (const char *asc, gssize length)
gsize l;
g_return_val_if_fail (asc != NULL, FALSE);
+ g_return_val_if_fail ( length >= -1
+ && length <= NM_UTILS_HWADDR_LEN_MAX, FALSE);
- if (length > 0 && length <= NM_UTILS_HWADDR_LEN_MAX) {
- if (!hwaddr_aton (asc, buf, length, &l))
- return FALSE;
- return length == l;
- } else if (length == -1)
- return !!hwaddr_aton (asc, buf, sizeof (buf), &l);
- else if (length == 0)
+ if (length == 0)
return FALSE;
- else
- g_return_val_if_reached (FALSE);
+
+ if (!_nm_utils_hwaddr_aton (asc, buf, sizeof (buf), &l))
+ return FALSE;
+
+ return length == -1
+ || length == (gssize) l;
}
/**
@@ -4250,19 +4208,15 @@ nm_utils_hwaddr_canonical (const char *asc, gssize length)
gsize l;
g_return_val_if_fail (asc, NULL);
- g_return_val_if_fail (length == -1 || (length > 0 && length <= NM_UTILS_HWADDR_LEN_MAX), NULL);
-
- if (length > 0 && length <= NM_UTILS_HWADDR_LEN_MAX) {
- if (!hwaddr_aton (asc, buf, length, &l))
- return NULL;
- if (l != length)
- return NULL;
- } else if (length == -1) {
- if (!hwaddr_aton (asc, buf, NM_UTILS_HWADDR_LEN_MAX, &l))
- return NULL;
- } else
- g_return_val_if_reached (NULL);
+ g_return_val_if_fail ( length == -1
+ || ( length > 0
+ && length <= NM_UTILS_HWADDR_LEN_MAX), NULL);
+ if (!_nm_utils_hwaddr_aton (asc, buf, sizeof (buf), &l))
+ return NULL;
+ if ( length != -1
+ && length != (gssize) l)
+ return NULL;
return nm_utils_hwaddr_ntoa (buf, l);
}
@@ -4367,7 +4321,7 @@ nm_utils_hwaddr_matches (gconstpointer hwaddr1,
if (hwaddr1_len == -1) {
if (hwaddr1 == NULL) {
hwaddr1_len = 0;
- } else if (hwaddr_aton (hwaddr1, buf1, sizeof (buf1), &l)) {
+ } else if (_nm_utils_hwaddr_aton (hwaddr1, buf1, sizeof (buf1), &l)) {
hwaddr1 = buf1;
hwaddr1_len = l;
} else {
@@ -4387,7 +4341,7 @@ nm_utils_hwaddr_matches (gconstpointer hwaddr1,
if (hwaddr2_len == -1) {
if (hwaddr2 == NULL)
l = 0;
- else if (!hwaddr_aton (hwaddr2, buf2, sizeof (buf2), &l))
+ else if (!_nm_utils_hwaddr_aton (hwaddr2, buf2, sizeof (buf2), &l))
return FALSE;
if (l != hwaddr1_len)
return FALSE;
@@ -4431,7 +4385,7 @@ _nm_utils_hwaddr_to_dbus_impl (const char *str)
if (!str)
return NULL;
- if (!hwaddr_aton (str, buf, sizeof (buf), &len))
+ if (!_nm_utils_hwaddr_aton (str, buf, sizeof (buf), &len))
return NULL;
return g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, buf, len, 1);
diff --git a/shared/nm-glib-aux/nm-shared-utils.h b/shared/nm-glib-aux/nm-shared-utils.h
index 363859d7aa..28a519ae44 100644
--- a/shared/nm-glib-aux/nm-shared-utils.h
+++ b/shared/nm-glib-aux/nm-shared-utils.h
@@ -2037,6 +2037,48 @@ guint8 *nm_utils_hexstr2bin_alloc (const char *hexstr,
gsize required_len,
gsize *out_len);
+/**
+ * _nm_utils_hwaddr_aton:
+ * @asc: the ASCII representation of a hardware address
+ * @buffer: buffer to store the result into. Must have
+ * at least a size of @buffer_length.
+ * @buffer_length: the length of the input buffer @buffer.
+ * The result must fit into that buffer, otherwise
+ * the function fails and returns %NULL.
+ * @out_length: the output length in case of success.
+ *
+ * Parses @asc and converts it to binary form in @buffer.
+ * Bytes in @asc can be separated by colons (:), or hyphens (-), but not mixed.
+ *
+ * It is like nm_utils_hwaddr_aton(), but contrary to that it
+ * can parse addresses of any length. That is, you don't need
+ * to know the length before-hand.
+ *
+ * Return value: @buffer, or %NULL if @asc couldn't be parsed.
+ */
+static inline guint8 *
+_nm_utils_hwaddr_aton (const char *asc, gpointer buffer, gsize buffer_length, gsize *out_length)
+{
+ g_return_val_if_fail (asc, NULL);
+ g_return_val_if_fail (buffer, NULL);
+ g_return_val_if_fail (buffer_length > 0, NULL);
+ g_return_val_if_fail (out_length, NULL);
+
+ return nm_utils_hexstr2bin_full (asc, FALSE, TRUE, FALSE, ":-", 0, buffer, buffer_length, out_length);
+}
+
+static inline const char *
+_nm_utils_hwaddr_ntoa (gconstpointer addr, gsize addr_len, gboolean upper_case, char *buf, gsize buf_len)
+{
+ g_return_val_if_fail (addr, NULL);
+ g_return_val_if_fail (addr_len > 0, NULL);
+ g_return_val_if_fail (buf, NULL);
+ if (buf_len < addr_len * 3)
+ g_return_val_if_reached (NULL);
+
+ return nm_utils_bin2hexstr_full (addr, addr_len, ':', upper_case, buf);
+}
+
/*****************************************************************************/
#define _NM_UTILS_STRING_TABLE_LOOKUP_DEFINE(fcn_name, \
diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c
index bd14099015..bb309e0372 100644
--- a/src/devices/nm-device.c
+++ b/src/devices/nm-device.c
@@ -16990,7 +16990,7 @@ nm_device_update_hw_address (NMDevice *self)
_LOGD (LOGD_PLATFORM | LOGD_DEVICE,
"hw-addr: read a MAC address with differing length (%s vs. %s)",
priv->hw_addr,
- nm_utils_hwaddr_ntoa_buf (hwaddr, hwaddrlen, TRUE, s_buf, sizeof (s_buf)));
+ _nm_utils_hwaddr_ntoa (hwaddr, hwaddrlen, TRUE, s_buf, sizeof (s_buf)));
return FALSE;
}
diff --git a/src/platform/nm-platform.c b/src/platform/nm-platform.c
index 5a842d8778..d944a52d02 100644
--- a/src/platform/nm-platform.c
+++ b/src/platform/nm-platform.c
@@ -62,11 +62,11 @@ _nmp_link_address_to_string (const NMPLinkAddress *addr,
nm_assert (addr);
if (addr->len > 0) {
- if (!nm_utils_hwaddr_ntoa_buf (addr->data,
- addr->len,
- TRUE,
- buf,
- NM_UTILS_HWADDR_LEN_MAX * 3)) {
+ if (!_nm_utils_hwaddr_ntoa (addr->data,
+ addr->len,
+ TRUE,
+ buf,
+ NM_UTILS_HWADDR_LEN_MAX * 3)) {
buf[0] = '\0';
g_return_val_if_reached (buf);
}
@@ -1221,7 +1221,7 @@ nm_platform_link_add (NMPlatform *self,
parent > 0 ? ", parent " : "",
parent > 0 ? nm_sprintf_buf (parent_buf, "%d", parent) : "",
address ? ", address: " : "",
- address ? nm_utils_hwaddr_ntoa_buf (address, address_len, FALSE, addr_buf, sizeof (addr_buf)) : "",
+ address ? _nm_utils_hwaddr_ntoa (address, address_len, FALSE, addr_buf, sizeof (addr_buf)) : "",
({
char *buf_p = buf;
gsize buf_len = sizeof (buf);
@@ -6875,7 +6875,7 @@ nm_platform_vf_to_string (const NMPlatformVF *vf, char *buf, gsize len)
return buf;
if (vf->mac.len) {
- nm_utils_hwaddr_ntoa_buf (vf->mac.data, vf->mac.len, TRUE, mac, sizeof (mac));
+ _nm_utils_hwaddr_ntoa (vf->mac.data, vf->mac.len, TRUE, mac, sizeof (mac));
nm_sprintf_buf (str_mac, " mac %s", mac);
} else
str_mac[0] = '\0';