dhcp: simplify how hostname and FQDN are passed down to backends

Since they are mutually exclusive, pass a string and a boolean to
indicate whether we want to use the hostname or the FQDN option.

(cherry picked from commit d286aa9dfa)
This commit is contained in:
Beniamino Galvani 2017-04-26 11:49:30 +02:00
parent f4226e750f
commit 099b790d2b
10 changed files with 113 additions and 111 deletions

View file

@ -12207,7 +12207,7 @@ nm_device_spawn_iface_helper (NMDevice *self)
g_ptr_array_add (argv, g_strdup ("--dhcp4-required"));
if (priv->dhcp4.client) {
const char *hostname, *fqdn;
const char *hostname;
GBytes *client_id;
client_id = nm_dhcp_client_get_client_id (priv->dhcp4.client);
@ -12221,15 +12221,12 @@ nm_device_spawn_iface_helper (NMDevice *self)
hostname = nm_dhcp_client_get_hostname (priv->dhcp4.client);
if (hostname) {
g_ptr_array_add (argv, g_strdup ("--dhcp4-hostname"));
if (nm_dhcp_client_get_use_fqdn (priv->dhcp4.client))
g_ptr_array_add (argv, g_strdup ("--dhcp4-fqdn"));
else
g_ptr_array_add (argv, g_strdup ("--dhcp4-hostname"));
g_ptr_array_add (argv, g_strdup (hostname));
}
fqdn = nm_dhcp_client_get_fqdn (priv->dhcp4.client);
if (fqdn) {
g_ptr_array_add (argv, g_strdup ("--dhcp4-fqdn"));
g_ptr_array_add (argv, g_strdup (fqdn));
}
}
configured = TRUE;

View file

@ -68,7 +68,7 @@ typedef struct _NMDhcpClientPrivate {
GByteArray * duid;
GBytes * client_id;
char * hostname;
char * fqdn;
gboolean use_fqdn;
NMDhcpState state;
pid_t pid;
@ -186,12 +186,12 @@ nm_dhcp_client_get_hostname (NMDhcpClient *self)
return NM_DHCP_CLIENT_GET_PRIVATE (self)->hostname;
}
const char *
nm_dhcp_client_get_fqdn (NMDhcpClient *self)
gboolean
nm_dhcp_client_get_use_fqdn (NMDhcpClient *self)
{
g_return_val_if_fail (NM_IS_DHCP_CLIENT (self), NULL);
g_return_val_if_fail (NM_IS_DHCP_CLIENT (self), FALSE);
return NM_DHCP_CLIENT_GET_PRIVATE (self)->fqdn;
return NM_DHCP_CLIENT_GET_PRIVATE (self)->use_fqdn;
}
/*****************************************************************************/
@ -416,7 +416,7 @@ nm_dhcp_client_start_ip4 (NMDhcpClient *self,
const char *dhcp_client_id,
const char *dhcp_anycast_addr,
const char *hostname,
const char *fqdn,
gboolean use_fqdn,
const char *last_ip4_address)
{
NMDhcpClientPrivate *priv;
@ -437,8 +437,7 @@ nm_dhcp_client_start_ip4 (NMDhcpClient *self,
g_clear_pointer (&priv->hostname, g_free);
priv->hostname = g_strdup (hostname);
g_free (priv->fqdn);
priv->fqdn = g_strdup (fqdn);
priv->use_fqdn = use_fqdn;
return NM_DHCP_CLIENT_GET_CLASS (self)->ip4_start (self, dhcp_anycast_addr, last_ip4_address);
}
@ -911,7 +910,6 @@ dispose (GObject *object)
g_clear_pointer (&priv->iface, g_free);
g_clear_pointer (&priv->hostname, g_free);
g_clear_pointer (&priv->fqdn, g_free);
g_clear_pointer (&priv->uuid, g_free);
g_clear_pointer (&priv->client_id, g_bytes_unref);

View file

@ -123,13 +123,13 @@ GBytes *nm_dhcp_client_get_client_id (NMDhcpClient *self);
const char *nm_dhcp_client_get_hostname (NMDhcpClient *self);
const char *nm_dhcp_client_get_fqdn (NMDhcpClient *self);
gboolean nm_dhcp_client_get_use_fqdn (NMDhcpClient *self);
gboolean nm_dhcp_client_start_ip4 (NMDhcpClient *self,
const char *dhcp_client_id,
const char *dhcp_anycast_addr,
const char *hostname,
const char *fqdn,
gboolean use_fqdn,
const char *last_ip4_address);
gboolean nm_dhcp_client_start_ip6 (NMDhcpClient *self,

View file

@ -93,29 +93,31 @@ grab_request_options (GPtrArray *store, const char* line)
static void
add_hostname4 (GString *str, const char *hostname, const char *fqdn)
add_hostname4 (GString *str, const char *hostname, gboolean use_fqdn)
{
char *plain_hostname, *dot;
if (fqdn) {
g_string_append_printf (str, FQDN_FORMAT "\n", fqdn);
g_string_append (str,
"send fqdn.encoded on;\n"
"send fqdn.server-update on;\n");
} else if (hostname) {
plain_hostname = g_strdup (hostname);
dot = strchr (plain_hostname, '.');
/* get rid of the domain */
if (dot)
*dot = '\0';
if (hostname) {
if (use_fqdn) {
g_string_append_printf (str, FQDN_FORMAT "\n", hostname);
g_string_append (str,
"send fqdn.encoded on;\n"
"send fqdn.server-update on;\n");
} else {
plain_hostname = g_strdup (hostname);
dot = strchr (plain_hostname, '.');
/* get rid of the domain */
if (dot)
*dot = '\0';
g_string_append_printf (str, HOSTNAME4_FORMAT "\n", plain_hostname);
g_free (plain_hostname);
g_string_append_printf (str, HOSTNAME4_FORMAT "\n", plain_hostname);
g_free (plain_hostname);
}
}
}
static void
add_ip4_config (GString *str, GBytes *client_id, const char *hostname, const char *fqdn)
add_ip4_config (GString *str, GBytes *client_id, const char *hostname, gboolean use_fqdn)
{
if (client_id) {
const char *p;
@ -150,7 +152,7 @@ add_ip4_config (GString *str, GBytes *client_id, const char *hostname, const cha
g_string_append (str, "; # added by NetworkManager\n");
}
add_hostname4 (str, hostname, fqdn);
add_hostname4 (str, hostname, use_fqdn);
g_string_append_c (str, '\n');
@ -271,7 +273,7 @@ nm_dhcp_dhclient_create_config (const char *interface,
GBytes *client_id,
const char *anycast_addr,
const char *hostname,
const char *fqdn,
gboolean use_fqdn,
const char *orig_path,
const char *orig_contents,
GBytes **out_new_client_id)
@ -328,7 +330,7 @@ nm_dhcp_dhclient_create_config (const char *interface,
}
/* Override config file hostname and use one from the connection */
if (hostname || fqdn) {
if (hostname) {
if (strncmp (p, HOSTNAME4_TAG, strlen (HOSTNAME4_TAG)) == 0)
continue;
if (strncmp (p, FQDN_TAG, strlen (FQDN_TAG)) == 0)
@ -388,7 +390,7 @@ nm_dhcp_dhclient_create_config (const char *interface,
add_request (reqs, "dhcp6.domain-search");
add_request (reqs, "dhcp6.client-id");
} else {
add_ip4_config (new_contents, client_id, hostname, fqdn);
add_ip4_config (new_contents, client_id, hostname, use_fqdn);
add_request (reqs, "rfc3442-classless-static-routes");
add_request (reqs, "ms-classless-static-routes");
add_request (reqs, "static-routes");

View file

@ -27,7 +27,7 @@ char *nm_dhcp_dhclient_create_config (const char *interface,
GBytes *client_id,
const char *anycast_addr,
const char *hostname,
const char *fqdn,
gboolean use_fqdn,
const char *orig_path,
const char *orig_contents,
GBytes **out_new_client_id);

View file

@ -182,7 +182,7 @@ merge_dhclient_config (NMDhcpDhclient *self,
GBytes *client_id,
const char *anycast_addr,
const char *hostname,
const char *fqdn,
gboolean use_fqdn,
const char *orig_path,
GBytes **out_new_client_id,
GError **error)
@ -206,7 +206,7 @@ merge_dhclient_config (NMDhcpDhclient *self,
if (is_ip6 && hostname && !strchr (hostname, '.'))
_LOGW ("hostname is not a FQDN, it will be ignored");
new = nm_dhcp_dhclient_create_config (iface, is_ip6, client_id, anycast_addr, hostname, fqdn, orig_path, orig, out_new_client_id);
new = nm_dhcp_dhclient_create_config (iface, is_ip6, client_id, anycast_addr, hostname, use_fqdn, orig_path, orig, out_new_client_id);
g_assert (new);
success = g_file_set_contents (conf_file, new, -1, error);
g_free (new);
@ -294,7 +294,7 @@ create_dhclient_config (NMDhcpDhclient *self,
GBytes *client_id,
const char *dhcp_anycast_addr,
const char *hostname,
const char *fqdn,
gboolean use_fqdn,
GBytes **out_new_client_id)
{
char *orig = NULL, *new = NULL;
@ -314,7 +314,7 @@ create_dhclient_config (NMDhcpDhclient *self,
error = NULL;
success = merge_dhclient_config (self, iface, new, is_ip6, client_id, dhcp_anycast_addr,
hostname, fqdn, orig, out_new_client_id, &error);
hostname, use_fqdn, orig, out_new_client_id, &error);
if (!success) {
_LOGW ("error creating dhclient configuration: %s", error->message);
g_error_free (error);
@ -505,17 +505,18 @@ ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr, const char *last
NMDhcpDhclientPrivate *priv = NM_DHCP_DHCLIENT_GET_PRIVATE (self);
GBytes *client_id;
gs_unref_bytes GBytes *new_client_id = NULL;
const char *iface, *uuid, *hostname, *fqdn;
const char *iface, *uuid, *hostname;
gboolean success = FALSE;
gboolean use_fqdn;
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);
fqdn = nm_dhcp_client_get_fqdn (client);
use_fqdn = nm_dhcp_client_get_use_fqdn (client);
priv->conf_file = create_dhclient_config (self, iface, FALSE, uuid, client_id, dhcp_anycast_addr,
hostname, fqdn, &new_client_id);
hostname, use_fqdn, &new_client_id);
if (priv->conf_file) {
if (new_client_id)
nm_dhcp_client_set_client_id (client, new_client_id);
@ -543,7 +544,7 @@ ip6_start (NMDhcpClient *client,
uuid = nm_dhcp_client_get_uuid (client);
hostname = nm_dhcp_client_get_hostname (client);
priv->conf_file = create_dhclient_config (self, iface, TRUE, uuid, NULL, dhcp_anycast_addr, hostname, NULL, NULL);
priv->conf_file = create_dhclient_config (self, iface, TRUE, uuid, NULL, dhcp_anycast_addr, hostname, TRUE, NULL);
if (!priv->conf_file) {
_LOGW ("error creating dhclient configuration file");
return FALSE;

View file

@ -89,7 +89,7 @@ ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr, const char *last
pid_t pid = -1;
GError *error = NULL;
char *pid_contents = NULL, *binary_name, *cmd_str, *dot;
const char *iface, *dhcpcd_path, *hostname, *fqdn;
const char *iface, *dhcpcd_path, *hostname;
gs_free char *prefix = NULL;
g_return_val_if_fail (priv->pid_file == NULL, FALSE);
@ -138,22 +138,23 @@ ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr, const char *last
#endif
hostname = nm_dhcp_client_get_hostname (client);
fqdn = nm_dhcp_client_get_fqdn (client);
if (fqdn) {
g_ptr_array_add (argv, (gpointer) "-h");
g_ptr_array_add (argv, (gpointer) fqdn);
g_ptr_array_add (argv, (gpointer) "-F");
g_ptr_array_add (argv, (gpointer) "both");
} else if (hostname) {
prefix = strdup (hostname);
dot = strchr (prefix, '.');
/* get rid of the domain */
if (dot)
*dot = '\0';
if (hostname) {
if (nm_dhcp_client_get_use_fqdn (client)) {
g_ptr_array_add (argv, (gpointer) "-h");
g_ptr_array_add (argv, (gpointer) hostname);
g_ptr_array_add (argv, (gpointer) "-F");
g_ptr_array_add (argv, (gpointer) "both");
} else {
prefix = strdup (hostname);
dot = strchr (prefix, '.');
/* get rid of the domain */
if (dot)
*dot = '\0';
g_ptr_array_add (argv, (gpointer) "-h"); /* Send hostname to DHCP server */
g_ptr_array_add (argv, (gpointer) prefix);
g_ptr_array_add (argv, (gpointer) "-h"); /* Send hostname to DHCP server */
g_ptr_array_add (argv, (gpointer) prefix);
}
}
g_ptr_array_add (argv, (gpointer) iface);

View file

@ -163,7 +163,7 @@ client_start (NMDhcpManager *self,
guint32 timeout,
const char *dhcp_anycast_addr,
const char *hostname,
const char *fqdn,
gboolean hostname_use_fqdn,
gboolean info_only,
NMSettingIP6ConfigPrivacy privacy,
const char *last_ip4_address,
@ -209,7 +209,7 @@ client_start (NMDhcpManager *self,
if (ipv6)
success = nm_dhcp_client_start_ip6 (client, dhcp_anycast_addr, ipv6_ll_addr, hostname, info_only, privacy, needed_prefixes);
else
success = nm_dhcp_client_start_ip4 (client, dhcp_client_id, dhcp_anycast_addr, hostname, fqdn, last_ip4_address);
success = nm_dhcp_client_start_ip4 (client, dhcp_client_id, dhcp_anycast_addr, hostname, hostname_use_fqdn, last_ip4_address);
if (!success) {
remove_client (self, client);
@ -245,17 +245,21 @@ nm_dhcp_manager_start_ip4 (NMDhcpManager *self,
const char *last_ip_address)
{
const char *hostname = NULL;
const char *fqdn = NULL;
gboolean use_fqdn = FALSE;
g_return_val_if_fail (NM_IS_DHCP_MANAGER (self), NULL);
if (send_hostname) {
hostname = get_send_hostname (self, dhcp_hostname);
fqdn = dhcp_fqdn;
if (dhcp_fqdn) {
hostname = dhcp_fqdn;
use_fqdn = TRUE;
} else
hostname = get_send_hostname (self, dhcp_hostname);
}
return client_start (self, iface, ifindex, hwaddr, uuid, priority, FALSE, NULL,
dhcp_client_id, timeout, dhcp_anycast_addr, hostname,
fqdn, FALSE, 0, last_ip_address, 0);
use_fqdn, FALSE, 0, last_ip_address, 0);
}
/* Caller owns a reference to the NMDhcpClient on return */
@ -282,7 +286,7 @@ nm_dhcp_manager_start_ip6 (NMDhcpManager *self,
if (send_hostname)
hostname = get_send_hostname (self, dhcp_hostname);
return client_start (self, iface, ifindex, hwaddr, uuid, priority, TRUE,
ll_addr, NULL, timeout, dhcp_anycast_addr, hostname, NULL, info_only,
ll_addr, NULL, timeout, dhcp_anycast_addr, hostname, TRUE, info_only,
privacy, NULL, needed_prefixes);
}

View file

@ -580,7 +580,7 @@ ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr, const char *last
const uint8_t *client_id = NULL;
size_t client_id_len = 0;
struct in_addr last_addr = { 0 };
const char *hostname, *fqdn;
const char *hostname;
int r, i;
gboolean success = FALSE;
guint16 arp_type;
@ -687,29 +687,28 @@ ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr, const char *last
hostname = nm_dhcp_client_get_hostname (client);
if (hostname) {
char *prefix, *dot;
if (nm_dhcp_client_get_use_fqdn (client)) {
r = sd_dhcp_client_set_hostname (priv->client4, hostname);
if (r < 0) {
_LOGW ("failed to set DHCP FQDN (%d)", r);
goto error;
}
} else {
char *prefix, *dot;
prefix = strdup (hostname);
dot = strchr (prefix, '.');
/* get rid of the domain */
if (dot)
*dot = '\0';
prefix = strdup (hostname);
dot = strchr (prefix, '.');
/* get rid of the domain */
if (dot)
*dot = '\0';
r = sd_dhcp_client_set_hostname (priv->client4, prefix);
free (prefix);
r = sd_dhcp_client_set_hostname (priv->client4, prefix);
free (prefix);
if (r < 0) {
_LOGW ("failed to set DHCP hostname (%d)", r);
goto error;
}
}
fqdn = nm_dhcp_client_get_fqdn (client);
if (fqdn) {
r = sd_dhcp_client_set_hostname (priv->client4, fqdn);
if (r < 0) {
_LOGW ("failed to set DHCP FQDN (%d)", r);
goto error;
if (r < 0) {
_LOGW ("failed to set DHCP hostname (%d)", r);
goto error;
}
}
}

View file

@ -40,7 +40,7 @@ test_config (const char *orig,
const char *expected,
gboolean ipv6,
const char *hostname,
const char *fqdn,
gboolean use_fqdn,
const char *dhcp_client_id,
GBytes *expected_new_client_id,
const char *iface,
@ -60,7 +60,7 @@ test_config (const char *orig,
client_id,
anycast_addr,
hostname,
fqdn,
use_fqdn,
"/path/to/dhclient.conf",
orig,
&new_client_id);
@ -105,7 +105,7 @@ static const char *orig_missing_expected = \
static void
test_orig_missing (void)
{
test_config (NULL, orig_missing_expected, FALSE, NULL, NULL, NULL, NULL, "eth0", NULL);
test_config (NULL, orig_missing_expected, FALSE, NULL, FALSE, NULL, NULL, "eth0", NULL);
}
/*****************************************************************************/
@ -134,7 +134,7 @@ static void
test_override_client_id (void)
{
test_config (override_client_id_orig, override_client_id_expected,
FALSE, NULL, NULL,
FALSE, NULL, FALSE,
"11:22:33:44:55:66",
NULL,
"eth0",
@ -163,7 +163,7 @@ static void
test_quote_client_id (void)
{
test_config (NULL, quote_client_id_expected,
FALSE, NULL, NULL,
FALSE, NULL, FALSE,
"1234",
NULL,
"eth0",
@ -192,7 +192,7 @@ static void
test_ascii_client_id (void)
{
test_config (NULL, ascii_client_id_expected,
FALSE, NULL, NULL,
FALSE, NULL, FALSE,
"qb:cd:ef:12:34:56",
NULL,
"eth0",
@ -221,7 +221,7 @@ static void
test_hex_single_client_id (void)
{
test_config (NULL, hex_single_client_id_expected,
FALSE, NULL, NULL,
FALSE, NULL, FALSE,
"ab:cd:e:12:34:56",
NULL,
"eth0",
@ -258,7 +258,7 @@ test_existing_hex_client_id (void)
new_client_id = g_bytes_new (bytes, sizeof (bytes));
test_config (existing_hex_client_id_orig, existing_hex_client_id_expected,
FALSE, NULL, NULL,
FALSE, NULL, FALSE,
NULL,
new_client_id,
"eth0",
@ -298,7 +298,7 @@ test_existing_ascii_client_id (void)
memcpy (buf + 1, EACID, NM_STRLEN (EACID));
new_client_id = g_bytes_new (buf, sizeof (buf));
test_config (existing_ascii_client_id_orig, existing_ascii_client_id_expected,
FALSE, NULL, NULL,
FALSE, NULL, FALSE,
NULL,
new_client_id,
"eth0",
@ -327,8 +327,8 @@ static void
test_fqdn (void)
{
test_config (NULL, fqdn_expected,
FALSE, NULL,
"foo.bar.com", NULL,
FALSE, "foo.bar.com",
TRUE, NULL,
NULL,
"eth0",
NULL);
@ -367,8 +367,8 @@ test_fqdn_options_override (void)
{
test_config (fqdn_options_override_orig,
fqdn_options_override_expected,
FALSE, NULL,
"example2.com", NULL,
FALSE, "example2.com",
TRUE, NULL,
NULL,
"eth0",
NULL);
@ -400,7 +400,7 @@ static void
test_override_hostname (void)
{
test_config (override_hostname_orig, override_hostname_expected,
FALSE, "blahblah", NULL,
FALSE, "blahblah", FALSE,
NULL,
NULL,
"eth0",
@ -429,7 +429,7 @@ static void
test_override_hostname6 (void)
{
test_config (override_hostname6_orig, override_hostname6_expected,
TRUE, "blahblah.local", NULL,
TRUE, "blahblah.local", TRUE,
NULL,
NULL,
"eth0",
@ -452,7 +452,7 @@ test_nonfqdn_hostname6 (void)
/* Non-FQDN hostname can't be used with dhclient */
test_config (NULL, nonfqdn_hostname6_expected,
TRUE, "blahblah",
NULL, NULL,
TRUE, NULL,
NULL,
"eth0",
NULL);
@ -487,7 +487,7 @@ test_existing_alsoreq (void)
{
test_config (existing_alsoreq_orig, existing_alsoreq_expected,
FALSE, NULL,
NULL,
FALSE,
NULL,
NULL,
"eth0",
@ -526,7 +526,7 @@ test_existing_req (void)
{
test_config (existing_req_orig, existing_req_expected,
FALSE, NULL,
NULL,
FALSE,
NULL,
NULL,
"eth0",
@ -565,7 +565,7 @@ static void
test_existing_multiline_alsoreq (void)
{
test_config (existing_multiline_alsoreq_orig, existing_multiline_alsoreq_expected,
FALSE, NULL, NULL,
FALSE, NULL, FALSE,
NULL,
NULL,
"eth0",
@ -779,7 +779,7 @@ static void
test_interface1 (void)
{
test_config (interface1_orig, interface1_expected,
FALSE, NULL, NULL,
FALSE, NULL, FALSE,
NULL,
NULL,
"eth0",
@ -824,7 +824,7 @@ static void
test_interface2 (void)
{
test_config (interface2_orig, interface2_expected,
FALSE, NULL, NULL,
FALSE, NULL, FALSE,
NULL,
NULL,
"eth1",