dhcp: preserve DHCPv4 client ID for later use

If we can, read the existing client ID from the leasefile and preserve
it for later use.
This commit is contained in:
Dan Williams
2014-11-03 14:39:25 -06:00
parent 318a8c2d72
commit e43174f368
9 changed files with 327 additions and 83 deletions

View File

@@ -401,6 +401,28 @@ nm_dhcp_systemd_get_lease_ip_configs (const char *iface,
/************************************************************/
static void
_save_client_id (NMDhcpSystemd *self,
uint8_t type,
const uint8_t *client_id,
size_t len)
{
gs_unref_bytes GBytes *b = NULL;
gs_free char *buf = NULL;
g_return_if_fail (self != NULL);
g_return_if_fail (client_id != NULL);
g_return_if_fail (len > 0);
if (!nm_dhcp_client_get_client_id (NM_DHCP_CLIENT (self))) {
buf = g_malloc (len + 1);
buf[0] = type;
memcpy (buf + 1, client_id, len);
b = g_bytes_new (buf, len + 1);
nm_dhcp_client_set_client_id (NM_DHCP_CLIENT (self), b);
}
}
static void
bound4_handle (NMDhcpSystemd *self)
{
@@ -428,9 +450,17 @@ bound4_handle (NMDhcpSystemd *self)
TRUE,
&error);
if (ip4_config) {
const uint8_t *client_id = NULL;
size_t client_id_len = 0;
uint8_t type = 0;
add_requests_to_options (options, dhcp4_requests);
sd_dhcp_lease_save (lease, priv->lease_file);
client_id = sd_dhcp_client_get_client_id(priv->client4, &type, &client_id_len);
if (client_id)
_save_client_id (self, type, client_id, client_id_len);
nm_dhcp_client_set_state (NM_DHCP_CLIENT (self),
NM_DHCP_STATE_BOUND,
G_OBJECT (ip4_config),
@@ -486,7 +516,6 @@ get_arp_type (const GByteArray *hwaddr)
static gboolean
ip4_start (NMDhcpClient *client,
const char *dhcp_client_id,
const char *dhcp_anycast_addr,
const char *hostname)
{
@@ -494,6 +523,7 @@ ip4_start (NMDhcpClient *client,
const char *iface = nm_dhcp_client_get_iface (client);
const GByteArray *hwaddr;
sd_dhcp_lease *lease = NULL;
GBytes *override_client_id;
const uint8_t *client_id = NULL;
size_t client_id_len = 0;
struct in_addr last_addr;
@@ -560,25 +590,25 @@ ip4_start (NMDhcpClient *client,
}
}
if (dhcp_client_id) {
gs_unref_bytes GBytes *b = NULL;
b = nm_dhcp_utils_client_id_string_to_bytes (dhcp_client_id);
if (b) {
client_id = (const guint8 *) g_bytes_get_data (b, &client_id_len);
g_assert (client_id && client_id_len);
sd_dhcp_client_set_client_id (priv->client4,
client_id[0],
client_id + 1,
client_id_len - 1);
}
} else {
override_client_id = nm_dhcp_client_get_client_id (client);
if (override_client_id) {
client_id = g_bytes_get_data (override_client_id, &client_id_len);
g_assert (client_id && client_id_len);
sd_dhcp_client_set_client_id (priv->client4,
client_id[0],
client_id + 1,
client_id_len - 1);
} else if (lease) {
r = sd_dhcp_lease_get_client_id (lease, &client_id, &client_id_len);
if (r == 0 && client_id_len) {
sd_dhcp_client_set_client_id (priv->client4,
client_id[0],
client_id + 1,
client_id_len - 1);
_save_client_id (NM_DHCP_SYSTEMD (client),
client_id[0],
client_id + 1,
client_id_len - 1);
}
}