dhcp: preserve hostname for later use

This commit is contained in:
Dan Williams 2014-11-03 18:12:25 -06:00
parent e43174f368
commit 034917e129
5 changed files with 31 additions and 21 deletions

View file

@ -46,6 +46,7 @@ typedef struct {
guint32 timeout;
GByteArray * duid;
GBytes * client_id;
char * hostname;
NMDhcpState state;
pid_t pid;
@ -167,6 +168,14 @@ nm_dhcp_client_set_client_id (NMDhcpClient *self, GBytes *client_id)
priv->client_id = client_id ? g_bytes_ref (client_id) : NULL;
}
const char *
nm_dhcp_client_get_hostname (NMDhcpClient *self)
{
g_return_val_if_fail (NM_IS_DHCP_CLIENT (self), NULL);
return NM_DHCP_CLIENT_GET_PRIVATE (self)->hostname;
}
/********************************************/
static const char *state_table[NM_DHCP_STATE_MAX + 1] = {
@ -400,7 +409,10 @@ nm_dhcp_client_start_ip4 (NMDhcpClient *self,
nm_dhcp_client_set_client_id (self, dhcp_client_id ? nm_dhcp_utils_client_id_string_to_bytes (dhcp_client_id) : NULL);
return NM_DHCP_CLIENT_GET_CLASS (self)->ip4_start (self, dhcp_anycast_addr, hostname);
g_clear_pointer (&priv->hostname, g_free);
priv->hostname = g_strdup (hostname);
return NM_DHCP_CLIENT_GET_CLASS (self)->ip4_start (self, dhcp_anycast_addr);
}
/* uuid_parse does not work for machine-id, so we use our own converter */
@ -544,6 +556,9 @@ nm_dhcp_client_start_ip6 (NMDhcpClient *self,
g_free (str);
}
g_clear_pointer (&priv->hostname, g_free);
priv->hostname = g_strdup (hostname);
priv->info_only = info_only;
nm_log_info (LOGD_DHCP, "Activation (%s) Beginning DHCPv6 transaction (timeout in %d seconds)",
@ -551,7 +566,6 @@ nm_dhcp_client_start_ip6 (NMDhcpClient *self,
return NM_DHCP_CLIENT_GET_CLASS (self)->ip6_start (self,
dhcp_anycast_addr,
hostname,
info_only,
privacy,
priv->duid);

View file

@ -64,12 +64,10 @@ typedef struct {
/* Methods */
gboolean (*ip4_start) (NMDhcpClient *self,
const char *anycast_addr,
const char *hostname);
const char *anycast_addr);
gboolean (*ip6_start) (NMDhcpClient *self,
const char *anycast_addr,
const char *hostname,
gboolean info_only,
NMSettingIP6ConfigPrivacy privacy,
const GByteArray *duid);
@ -127,6 +125,8 @@ guint32 nm_dhcp_client_get_priority (NMDhcpClient *self);
GBytes *nm_dhcp_client_get_client_id (NMDhcpClient *self);
const char *nm_dhcp_client_get_hostname (NMDhcpClient *self);
gboolean nm_dhcp_client_start_ip4 (NMDhcpClient *self,
const char *dhcp_client_id,
const char *dhcp_anycast_addr,

View file

@ -477,19 +477,18 @@ dhclient_start (NMDhcpClient *client,
}
static gboolean
ip4_start (NMDhcpClient *client,
const char *dhcp_anycast_addr,
const char *hostname)
ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr)
{
NMDhcpDhclientPrivate *priv = NM_DHCP_DHCLIENT_GET_PRIVATE (client);
GBytes *client_id;
gs_unref_bytes GBytes *new_client_id = NULL;
const char *iface, *uuid;
const char *iface, *uuid, *hostname;
gboolean success = FALSE;
iface = nm_dhcp_client_get_iface (client);
uuid = nm_dhcp_client_get_uuid (client);
client_id = nm_dhcp_client_get_client_id (client);
hostname = nm_dhcp_client_get_hostname (client);
priv->conf_file = create_dhclient_config (iface, FALSE, uuid, client_id, dhcp_anycast_addr, hostname, &new_client_id);
if (priv->conf_file) {
@ -505,16 +504,16 @@ ip4_start (NMDhcpClient *client,
static gboolean
ip6_start (NMDhcpClient *client,
const char *dhcp_anycast_addr,
const char *hostname,
gboolean info_only,
NMSettingIP6ConfigPrivacy privacy,
const GByteArray *duid)
{
NMDhcpDhclientPrivate *priv = NM_DHCP_DHCLIENT_GET_PRIVATE (client);
const char *iface, *uuid;
const char *iface, *uuid, *hostname;
iface = nm_dhcp_client_get_iface (client);
uuid = nm_dhcp_client_get_uuid (client);
hostname = nm_dhcp_client_get_hostname (client);
priv->conf_file = create_dhclient_config (iface, TRUE, uuid, NULL, dhcp_anycast_addr, hostname, NULL);
if (!priv->conf_file) {

View file

@ -73,16 +73,14 @@ dhcpcd_child_setup (gpointer user_data G_GNUC_UNUSED)
}
static gboolean
ip4_start (NMDhcpClient *client,
const char *dhcp_anycast_addr,
const char *hostname)
ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr)
{
NMDhcpDhcpcdPrivate *priv = NM_DHCP_DHCPCD_GET_PRIVATE (client);
GPtrArray *argv = NULL;
pid_t pid = -1;
GError *error = NULL;
char *pid_contents = NULL, *binary_name, *cmd_str;
const char *iface, *dhcpcd_path = NULL;
const char *iface, *dhcpcd_path, *hostname;
g_return_val_if_fail (priv->pid_file == NULL, FALSE);
@ -129,7 +127,8 @@ ip4_start (NMDhcpClient *client,
g_ptr_array_add (argv, (gpointer) "-4");
#endif
if (hostname && strlen (hostname)) {
hostname = nm_dhcp_client_get_hostname (client);
if (hostname) {
g_ptr_array_add (argv, (gpointer) "-h"); /* Send hostname to DHCP server */
g_ptr_array_add (argv, (gpointer) hostname );
}
@ -160,7 +159,6 @@ ip4_start (NMDhcpClient *client,
static gboolean
ip6_start (NMDhcpClient *client,
const char *dhcp_anycast_addr,
const char *hostname,
gboolean info_only,
NMSettingIP6ConfigPrivacy privacy,
const GByteArray *duid)

View file

@ -515,9 +515,7 @@ get_arp_type (const GByteArray *hwaddr)
}
static gboolean
ip4_start (NMDhcpClient *client,
const char *dhcp_anycast_addr,
const char *hostname)
ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr)
{
NMDhcpSystemdPrivate *priv = NM_DHCP_SYSTEMD_GET_PRIVATE (client);
const char *iface = nm_dhcp_client_get_iface (client);
@ -527,6 +525,7 @@ ip4_start (NMDhcpClient *client,
const uint8_t *client_id = NULL;
size_t client_id_len = 0;
struct in_addr last_addr;
const char *hostname;
int r, i;
g_assert (priv->client4 == NULL);
@ -621,6 +620,7 @@ ip4_start (NMDhcpClient *client,
sd_dhcp_client_set_request_option (priv->client4, dhcp4_requests[i].num);
}
hostname = nm_dhcp_client_get_hostname (client);
if (hostname) {
r = sd_dhcp_client_set_hostname (priv->client4, hostname);
if (r < 0) {
@ -683,7 +683,6 @@ dhcp6_event_cb (sd_dhcp6_client *client, int event, gpointer user_data)
static gboolean
ip6_start (NMDhcpClient *client,
const char *dhcp_anycast_addr,
const char *hostname,
gboolean info_only,
NMSettingIP6ConfigPrivacy privacy,
const GByteArray *duid)