diff options
author | Thomas Haller <thaller@redhat.com> | 2016-07-10 13:42:13 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2016-07-10 13:44:58 +0200 |
commit | 36856ba610f8b829836f6271e4e3b8dcb91f2399 (patch) | |
tree | 2bdf6cd2f0d18f9d59ed105f81cc359f025a8ea7 | |
parent | 1c58ce0d748174b5b3a27dd077192b45fed2d0a4 (diff) | |
download | NetworkManager-36856ba610f8b829836f6271e4e3b8dcb91f2399.tar.gz |
all: reuse _nm_utils_hwaddr_ntoa() for converting binary to string
-rw-r--r-- | libnm-core/nm-core-internal.h | 2 | ||||
-rw-r--r-- | libnm-core/nm-utils.c | 48 | ||||
-rw-r--r-- | src/devices/nm-device.c | 33 | ||||
-rw-r--r-- | src/dhcp-manager/nm-dhcp-utils.c | 12 |
4 files changed, 47 insertions, 48 deletions
diff --git a/libnm-core/nm-core-internal.h b/libnm-core/nm-core-internal.h index d29eb23cb5..e3c5bcc3dd 100644 --- a/libnm-core/nm-core-internal.h +++ b/libnm-core/nm-core-internal.h @@ -126,6 +126,8 @@ gboolean _nm_setting_get_property (NMSetting *setting, const char *name, GValue guint _nm_utils_hwaddr_length (const char *asc); +char *_nm_utils_bin2str (gconstpointer addr, gsize length, gboolean upper_case); + GSList * _nm_utils_hash_values_to_slist (GHashTable *hash); GHashTable *_nm_utils_copy_strdict (GHashTable *strdict); diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c index 50575b30c9..fffde23091 100644 --- a/libnm-core/nm-utils.c +++ b/libnm-core/nm-utils.c @@ -3067,24 +3067,14 @@ nm_utils_hwaddr_aton (const char *asc, gpointer buffer, gsize length) return NULL; } -/** - * nm_utils_hwaddr_ntoa: - * @addr: (type guint8) (array length=length): a binary hardware address - * @length: the length of @addr - * - * Converts @addr to textual form. - * - * Return value: (transfer full): the textual form of @addr - */ -char * -nm_utils_hwaddr_ntoa (gconstpointer addr, gsize length) +static char * +_bin2str (gconstpointer addr, gsize length, const char *LOOKUP) { const guint8 *in = addr; char *out, *result; - const char *LOOKUP = "0123456789ABCDEF"; g_return_val_if_fail (addr != NULL, g_strdup ("")); - g_return_val_if_fail (length > 0 && length <= NM_UTILS_HWADDR_LEN_MAX, g_strdup ("")); + g_return_val_if_fail (length > 0, g_strdup ("")); result = out = g_malloc (length * 3); while (length--) { @@ -3100,6 +3090,38 @@ nm_utils_hwaddr_ntoa (gconstpointer addr, gsize length) return result; } +/** + * nm_utils_hwaddr_ntoa: + * @addr: (type guint8) (array length=length): a binary hardware address + * @length: the length of @addr + * + * Converts @addr to textual form. + * + * Return value: (transfer full): the textual form of @addr + */ +char * +nm_utils_hwaddr_ntoa (gconstpointer addr, gsize length) +{ + return _bin2str (addr, length, "0123456789ABCDEF"); +} + +/** + * _nm_utils_bin2str: + * @addr: (type guint8) (array length=length): a binary hardware address + * @length: the length of @addr + * @upper_case: the case for the hexadecimal digits. + * + * Converts @addr to textual form. + * + * Return value: (transfer full): the textual form of @addr + */ +char * +_nm_utils_bin2str (gconstpointer addr, gsize length, gboolean upper_case) +{ + return _bin2str (addr, length, + upper_case ? "0123456789ABCDEF" : "0123456789abcdef"); +} + static int hwaddr_binary_len (const char *asc) { diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c index 7237b04227..177080d9c5 100644 --- a/src/devices/nm-device.c +++ b/src/devices/nm-device.c @@ -10698,24 +10698,6 @@ deactivate_reset_hw_addr (NMDevice *self) } static char * -bin2hexstr (const char *bytes, gsize len) -{ - GString *str; - int i; - - g_return_val_if_fail (bytes != NULL, NULL); - g_return_val_if_fail (len > 0, NULL); - - str = g_string_sized_new (len * 2 + 1); - for (i = 0; i < len; i++) { - if (str->len) - g_string_append_c (str, ':'); - g_string_append_printf (str, "%02x", (guint8) bytes[i]); - } - return g_string_free (str, FALSE); -} - -static char * find_dhcp4_address (NMDevice *self) { NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); @@ -10787,7 +10769,6 @@ nm_device_spawn_iface_helper (NMDevice *self) method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP4_CONFIG); if (g_strcmp0 (method, NM_SETTING_IP4_CONFIG_METHOD_AUTO) == 0) { NMSettingIPConfig *s_ip4; - char *hex_client_id; s_ip4 = nm_connection_get_setting_ip4_config (connection); g_assert (s_ip4); @@ -10807,9 +10788,10 @@ nm_device_spawn_iface_helper (NMDevice *self) client_id = nm_dhcp_client_get_client_id (priv->dhcp4.client); if (client_id) { g_ptr_array_add (argv, g_strdup ("--dhcp4-clientid")); - hex_client_id = bin2hexstr (g_bytes_get_data (client_id, NULL), - g_bytes_get_size (client_id)); - g_ptr_array_add (argv, hex_client_id); + g_ptr_array_add (argv, + _nm_utils_bin2str (g_bytes_get_data (client_id, NULL), + g_bytes_get_size (client_id), + FALSE)); } hostname = nm_dhcp_client_get_hostname (priv->dhcp4.client); @@ -10831,7 +10813,6 @@ nm_device_spawn_iface_helper (NMDevice *self) method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP6_CONFIG); if (g_strcmp0 (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO) == 0) { NMSettingIPConfig *s_ip6; - char *hex_iid; NMUtilsIPv6IfaceId iid = NM_UTILS_IPV6_IFACE_ID_INIT; s_ip6 = nm_connection_get_setting_ip6_config (connection); @@ -10850,8 +10831,10 @@ nm_device_spawn_iface_helper (NMDevice *self) if (nm_device_get_ip_iface_identifier (self, &iid, FALSE)) { g_ptr_array_add (argv, g_strdup ("--iid")); - hex_iid = bin2hexstr ((const char *) iid.id_u8, sizeof (NMUtilsIPv6IfaceId)); - g_ptr_array_add (argv, hex_iid); + g_ptr_array_add (argv, + _nm_utils_bin2str (iid.id_u8, + sizeof (NMUtilsIPv6IfaceId), + FALSE)); } g_ptr_array_add (argv, g_strdup ("--addr-gen-mode")); diff --git a/src/dhcp-manager/nm-dhcp-utils.c b/src/dhcp-manager/nm-dhcp-utils.c index a6a0f9bf92..593d8c5d91 100644 --- a/src/dhcp-manager/nm-dhcp-utils.c +++ b/src/dhcp-manager/nm-dhcp-utils.c @@ -29,6 +29,7 @@ #include "NetworkManagerUtils.h" #include "nm-platform.h" #include "nm-dhcp-client-logging.h" +#include "nm-core-internal.h" /********************************************/ @@ -695,18 +696,9 @@ error: char * nm_dhcp_utils_duid_to_string (const GByteArray *duid) { - guint32 i = 0; - GString *s; - g_return_val_if_fail (duid != NULL, NULL); - s = g_string_sized_new (MIN (duid->len * 3, 50)); - while (i < duid->len) { - if (s->len) - g_string_append_c (s, ':'); - g_string_append_printf (s, "%02x", duid->data[i++]); - } - return g_string_free (s, FALSE); + return _nm_utils_bin2str (duid->data, duid->len, FALSE); } /** |