Commit b0e98561 authored by Thomas Haller's avatar Thomas Haller
Browse files

dhcp: refactor type of NMDhcpClient hwaddr to be GBytes

GByteArray is a mutable array of bytes. For every practical purpose, the hwaddr
property of NMDhcpClient is an immutable sequence of bytes. Thus, make it a
GBytes.
parent 1f08b017
......@@ -5745,8 +5745,7 @@ ipv4_dad_start (NMDevice *self, NMIP4Config **configs, ArpingCallback cb)
ArpingData *data;
guint timeout;
gboolean ret, addr_found;
const guint8 *hw_addr;
size_t hw_addr_len = 0;
const guint8 *hwaddr_arr;
GError *error = NULL;
guint i;
......@@ -5762,13 +5761,12 @@ ipv4_dad_start (NMDevice *self, NMIP4Config **configs, ArpingCallback cb)
}
timeout = get_ipv4_dad_timeout (self);
hw_addr = nm_platform_link_get_address (nm_device_get_platform (self),
nm_device_get_ip_ifindex (self),
&hw_addr_len);
hwaddr_arr = nm_platform_link_get_address (nm_device_get_platform (self),
nm_device_get_ip_ifindex (self),
NULL);
if ( !timeout
|| !hw_addr
|| !hw_addr_len
|| !hwaddr_arr
|| !addr_found
|| nm_device_sys_iface_state_is_external_or_assume (self)) {
......@@ -6399,9 +6397,7 @@ dhcp4_start (NMDevice *self)
{
NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
NMSettingIPConfig *s_ip4;
const guint8 *hw_addr;
size_t hw_addr_len = 0;
GByteArray *tmp = NULL;
gs_unref_bytes GBytes *hwaddr = NULL;
NMConnection *connection;
connection = nm_device_get_applied_connection (self);
......@@ -6413,11 +6409,8 @@ dhcp4_start (NMDevice *self)
nm_exported_object_clear_and_unexport (&priv->dhcp4.config);
priv->dhcp4.config = nm_dhcp4_config_new ();
hw_addr = nm_platform_link_get_address (nm_device_get_platform (self), nm_device_get_ip_ifindex (self), &hw_addr_len);
if (hw_addr_len) {
tmp = g_byte_array_sized_new (hw_addr_len);
g_byte_array_append (tmp, hw_addr, hw_addr_len);
}
hwaddr = nm_platform_link_get_address_as_bytes (nm_device_get_platform (self),
nm_device_get_ip_ifindex (self));
/* Begin DHCP on the interface */
g_warn_if_fail (priv->dhcp4.client == NULL);
......@@ -6425,7 +6418,7 @@ dhcp4_start (NMDevice *self)
nm_netns_get_multi_idx (nm_device_get_netns (self)),
nm_device_get_ip_iface (self),
nm_device_get_ip_ifindex (self),
tmp,
hwaddr,
nm_connection_get_uuid (connection),
nm_device_get_route_table (self, AF_INET, TRUE),
nm_device_get_route_metric (self, AF_INET),
......@@ -6437,9 +6430,6 @@ dhcp4_start (NMDevice *self)
priv->dhcp_anycast_address,
NULL);
if (tmp)
g_byte_array_free (tmp, TRUE);
if (!priv->dhcp4.client)
return NM_ACT_STAGE_RETURN_FAILURE;
......@@ -7128,9 +7118,7 @@ dhcp6_start_with_link_ready (NMDevice *self, NMConnection *connection)
{
NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
NMSettingIPConfig *s_ip6;
GByteArray *tmp = NULL;
const guint8 *hw_addr;
size_t hw_addr_len = 0;
gs_unref_bytes GBytes *hwaddr = NULL;
const NMPlatformIP6Address *ll_addr = NULL;
g_assert (connection);
......@@ -7145,17 +7133,14 @@ dhcp6_start_with_link_ready (NMDevice *self, NMConnection *connection)
return FALSE;
}
hw_addr = nm_platform_link_get_address (nm_device_get_platform (self), nm_device_get_ip_ifindex (self), &hw_addr_len);
if (hw_addr_len) {
tmp = g_byte_array_sized_new (hw_addr_len);
g_byte_array_append (tmp, hw_addr, hw_addr_len);
}
hwaddr = nm_platform_link_get_address_as_bytes (nm_device_get_platform (self),
nm_device_get_ip_ifindex (self));
priv->dhcp6.client = nm_dhcp_manager_start_ip6 (nm_dhcp_manager_get (),
nm_device_get_multi_index (self),
nm_device_get_ip_iface (self),
nm_device_get_ip_ifindex (self),
tmp,
hwaddr,
&ll_addr->address,
nm_connection_get_uuid (connection),
nm_device_get_route_table (self, AF_INET6, TRUE),
......@@ -7167,8 +7152,6 @@ dhcp6_start_with_link_ready (NMDevice *self, NMConnection *connection)
(priv->dhcp6.mode == NM_NDISC_DHCP_LEVEL_OTHERCONF) ? TRUE : FALSE,
nm_setting_ip6_config_get_ip6_privacy (NM_SETTING_IP6_CONFIG (s_ip6)),
priv->dhcp6.needed_prefixes);
if (tmp)
g_byte_array_free (tmp, TRUE);
if (priv->dhcp6.client) {
priv->dhcp6.state_sigid = g_signal_connect (priv->dhcp6.client,
......
......@@ -67,7 +67,7 @@ NM_GOBJECT_PROPERTIES_DEFINE_BASE (
typedef struct _NMDhcpClientPrivate {
NMDedupMultiIndex *multi_idx;
char * iface;
GByteArray * hwaddr;
GBytes * hwaddr;
char * uuid;
GByteArray * duid;
GBytes * client_id;
......@@ -147,7 +147,7 @@ nm_dhcp_client_get_duid (NMDhcpClient *self)
return NM_DHCP_CLIENT_GET_PRIVATE (self)->duid;
}
const GByteArray *
GBytes *
nm_dhcp_client_get_hw_addr (NMDhcpClient *self)
{
g_return_val_if_fail (NM_IS_DHCP_CLIENT (self), NULL);
......@@ -1018,11 +1018,7 @@ dispose (GObject *object)
g_clear_pointer (&priv->hostname, g_free);
g_clear_pointer (&priv->uuid, g_free);
g_clear_pointer (&priv->client_id, g_bytes_unref);
if (priv->hwaddr) {
g_byte_array_free (priv->hwaddr, TRUE);
priv->hwaddr = NULL;
}
g_clear_pointer (&priv->hwaddr, g_bytes_unref);
if (priv->duid) {
g_byte_array_free (priv->duid, TRUE);
......@@ -1068,7 +1064,7 @@ nm_dhcp_client_class_init (NMDhcpClientClass *client_class)
obj_properties[PROP_HWADDR] =
g_param_spec_boxed (NM_DHCP_CLIENT_HWADDR, "", "",
G_TYPE_BYTE_ARRAY,
G_TYPE_BYTES,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS);
......
......@@ -127,7 +127,7 @@ const char *nm_dhcp_client_get_uuid (NMDhcpClient *self);
const GByteArray *nm_dhcp_client_get_duid (NMDhcpClient *self);
const GByteArray *nm_dhcp_client_get_hw_addr (NMDhcpClient *self);
GBytes *nm_dhcp_client_get_hw_addr (NMDhcpClient *self);
guint32 nm_dhcp_client_get_route_table (NMDhcpClient *self);
......
......@@ -158,7 +158,7 @@ client_start (NMDhcpManager *self,
NMDedupMultiIndex *multi_idx,
const char *iface,
int ifindex,
const GByteArray *hwaddr,
GBytes *hwaddr,
const char *uuid,
guint32 route_table,
guint32 route_metric,
......@@ -233,7 +233,7 @@ nm_dhcp_manager_start_ip4 (NMDhcpManager *self,
NMDedupMultiIndex *multi_idx,
const char *iface,
int ifindex,
const GByteArray *hwaddr,
GBytes *hwaddr,
const char *uuid,
guint32 route_table,
guint32 route_metric,
......@@ -289,7 +289,7 @@ nm_dhcp_manager_start_ip6 (NMDhcpManager *self,
NMDedupMultiIndex *multi_idx,
const char *iface,
int ifindex,
const GByteArray *hwaddr,
GBytes *hwaddr,
const struct in6_addr *ll_addr,
const char *uuid,
guint32 route_table,
......
......@@ -49,7 +49,7 @@ NMDhcpClient * nm_dhcp_manager_start_ip4 (NMDhcpManager *manager,
struct _NMDedupMultiIndex *multi_idx,
const char *iface,
int ifindex,
const GByteArray *hwaddr,
GBytes *hwaddr,
const char *uuid,
guint32 route_table,
guint32 route_metric,
......@@ -65,7 +65,7 @@ NMDhcpClient * nm_dhcp_manager_start_ip6 (NMDhcpManager *manager,
struct _NMDedupMultiIndex *multi_idx,
const char *iface,
int ifindex,
const GByteArray *hwaddr,
GBytes *hwaddr,
const struct in6_addr *ll_addr,
const char *uuid,
guint32 route_table,
......
......@@ -583,14 +583,16 @@ dhcp_event_cb (sd_dhcp_client *client, int event, gpointer user_data)
}
static guint16
get_arp_type (const GByteArray *hwaddr)
get_arp_type (GBytes *hwaddr)
{
if (hwaddr->len == ETH_ALEN)
switch (g_bytes_get_size (hwaddr)) {
case ETH_ALEN:
return ARPHRD_ETHER;
else if (hwaddr->len == INFINIBAND_ALEN)
case INFINIBAND_ALEN:
return ARPHRD_INFINIBAND;
else
default:
return ARPHRD_NONE;
}
}
static gboolean
......@@ -599,7 +601,7 @@ ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr, const char *last
NMDhcpSystemd *self = NM_DHCP_SYSTEMD (client);
NMDhcpSystemdPrivate *priv = NM_DHCP_SYSTEMD_GET_PRIVATE (self);
const char *iface = nm_dhcp_client_get_iface (client);
const GByteArray *hwaddr;
GBytes *hwaddr;
sd_dhcp_lease *lease = NULL;
GBytes *override_client_id;
const uint8_t *client_id = NULL;
......@@ -608,7 +610,6 @@ ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr, const char *last
const char *hostname;
int r, i;
gboolean success = FALSE;
guint16 arp_type;
g_assert (priv->client4 == NULL);
g_assert (priv->client6 == NULL);
......@@ -632,16 +633,14 @@ ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr, const char *last
hwaddr = nm_dhcp_client_get_hw_addr (client);
if (hwaddr) {
arp_type= get_arp_type (hwaddr);
if (arp_type == ARPHRD_NONE) {
_LOGW ("failed to determine ARP type");
goto error;
}
const uint8_t *data;
gsize len;
data = g_bytes_get_data (hwaddr, &len);
r = sd_dhcp_client_set_mac (priv->client4,
hwaddr->data,
hwaddr->len,
arp_type);
data,
len,
get_arp_type (hwaddr));
if (r < 0) {
_LOGW ("failed to set MAC address (%d)", r);
goto error;
......@@ -906,7 +905,7 @@ ip6_start (NMDhcpClient *client,
NMDhcpSystemd *self = NM_DHCP_SYSTEMD (client);
NMDhcpSystemdPrivate *priv = NM_DHCP_SYSTEMD_GET_PRIVATE (self);
const char *iface = nm_dhcp_client_get_iface (client);
const GByteArray *hwaddr;
GBytes *hwaddr;
const char *hostname;
int r, i;
......@@ -953,9 +952,13 @@ ip6_start (NMDhcpClient *client,
hwaddr = nm_dhcp_client_get_hw_addr (client);
if (hwaddr) {
const uint8_t *data;
gsize len;
data = g_bytes_get_data (hwaddr, &len);
r = sd_dhcp6_client_set_mac (priv->client6,
hwaddr->data,
hwaddr->len,
data,
len,
get_arp_type (hwaddr));
if (r < 0) {
_LOGW ("failed to set MAC address (%d)", r);
......
......@@ -340,9 +340,7 @@ main (int argc, char *argv[])
gs_free char *pidfile = NULL;
gs_unref_object NMDhcpClient *dhcp4_client = NULL;
gs_unref_object NMNDisc *ndisc = NULL;
GByteArray *hwaddr = NULL;
size_t hwaddr_len = 0;
gconstpointer tmp;
gs_unref_bytes GBytes *hwaddr = NULL;
gs_free NMUtilsIPv6IfaceId *iid = NULL;
guint sd_id;
char sysctl_path_buf[NM_UTILS_SYSCTL_IP_CONF_PATH_BUFSIZE];
......@@ -429,11 +427,7 @@ main (int argc, char *argv[])
/* Set up platform interaction layer */
nm_linux_platform_setup ();
tmp = nm_platform_link_get_address (NM_PLATFORM_GET, gl.ifindex, &hwaddr_len);
if (tmp) {
hwaddr = g_byte_array_sized_new (hwaddr_len);
g_byte_array_append (hwaddr, tmp, hwaddr_len);
}
hwaddr = nm_platform_link_get_address_as_bytes (NM_PLATFORM_GET, gl.ifindex);
if (global_opt.iid_str) {
GBytes *bytes;
......@@ -521,8 +515,6 @@ main (int argc, char *argv[])
g_main_loop_run (gl.main_loop);
g_clear_pointer (&hwaddr, g_byte_array_unref);
if (pidfile && wrote_pidfile)
unlink (pidfile);
......
......@@ -1080,7 +1080,21 @@ gboolean nm_platform_link_is_connected (NMPlatform *self, int ifindex);
gboolean nm_platform_link_uses_arp (NMPlatform *self, int ifindex);
guint32 nm_platform_link_get_mtu (NMPlatform *self, int ifindex);
gboolean nm_platform_link_get_user_ipv6ll_enabled (NMPlatform *self, int ifindex);
gconstpointer nm_platform_link_get_address (NMPlatform *self, int ifindex, size_t *length);
static inline GBytes *
nm_platform_link_get_address_as_bytes (NMPlatform *self, int ifindex)
{
gconstpointer p;
gsize l;
p = nm_platform_link_get_address (self, ifindex, &l);
return p
? g_bytes_new (p, l)
: NULL;
}
int nm_platform_link_get_master (NMPlatform *self, int slave);
gboolean nm_platform_link_can_assume (NMPlatform *self, int ifindex);
......
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