Commit 36856ba6 authored by Thomas Haller's avatar Thomas Haller

all: reuse _nm_utils_hwaddr_ntoa() for converting binary to string

parent 1c58ce0d
......@@ -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);
......
......@@ -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)
{
......
......@@ -10697,24 +10697,6 @@ deactivate_reset_hw_addr (NMDevice *self)
nm_device_hw_addr_reset (self, "deactivate");
}
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)
{
......@@ -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"));
......
......@@ -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);
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment