dhcp (dhclient): refactor config file generation

The function merge_dhclient_config was called only once from
create_dhclient_config. The content of both of them is short and simple,
so moving the content from merge_dhclient_config to the caller
improves the readability and makes the functions call chain easier to
follow. Also, both functions takes a long list of arguments which are
almost the same, so we can avoid having to pass them over and over in a
long call chain.
This commit is contained in:
Íñigo Huguet 2023-09-27 14:55:03 +02:00
parent 583e5c4fc0
commit 12f694902d

View file

@ -148,56 +148,6 @@ get_dhclient_leasefile(int addr_family,
return NULL;
}
static gboolean
merge_dhclient_config(NMDhcpDhclient *self,
int addr_family,
const char *iface,
const char *conf_file,
GBytes *client_id,
const char *anycast_address,
const char *hostname,
guint32 timeout,
gboolean use_fqdn,
NMDhcpHostnameFlags hostname_flags,
const char *mud_url,
const char *const *reject_servers,
const char *orig_path,
GBytes **out_new_client_id,
GError **error)
{
gs_free char *orig = NULL;
gs_free char *new = NULL;
g_return_val_if_fail(iface, FALSE);
g_return_val_if_fail(conf_file, FALSE);
if (orig_path && g_file_test(orig_path, G_FILE_TEST_EXISTS)) {
GError *read_error = NULL;
if (!g_file_get_contents(orig_path, &orig, NULL, &read_error)) {
_LOGW("error reading dhclient configuration %s: %s", orig_path, read_error->message);
g_error_free(read_error);
}
}
new = nm_dhcp_dhclient_create_config(iface,
addr_family,
client_id,
anycast_address,
hostname,
timeout,
use_fqdn,
hostname_flags,
mud_url,
reject_servers,
orig_path,
orig,
out_new_client_id);
nm_assert(new);
return g_file_set_contents(conf_file, new, -1, error);
}
static char *
find_existing_config(NMDhcpDhclient *self, int addr_family, const char *iface, const char *uuid)
{
@ -292,44 +242,55 @@ create_dhclient_config(NMDhcpDhclient *self,
const char *const *reject_servers,
GBytes **out_new_client_id)
{
gs_free char *orig = NULL;
char *new = NULL;
GError *error = NULL;
gs_free char *orig_path = NULL;
gs_free char *orig_content = NULL;
char *new_path = NULL;
gs_free char *new_content = NULL;
GError *error = NULL;
g_return_val_if_fail(iface != NULL, NULL);
new = g_strdup_printf(NMSTATEDIR "/dhclient%s-%s.conf",
_addr_family_to_path_part(addr_family),
iface);
new_path = g_strdup_printf(NMSTATEDIR "/dhclient%s-%s.conf",
_addr_family_to_path_part(addr_family),
iface);
_LOGD("creating composite dhclient config %s", new_path);
_LOGD("creating composite dhclient config %s", new);
orig = find_existing_config(self, addr_family, iface, uuid);
if (orig)
_LOGD("merging existing dhclient config %s", orig);
orig_path = find_existing_config(self, addr_family, iface, uuid);
if (orig_path)
_LOGD("merging existing dhclient config %s", orig_path);
else
_LOGD("no existing dhclient configuration to merge");
if (!merge_dhclient_config(self,
addr_family,
iface,
new,
client_id,
anycast_address,
hostname,
timeout,
use_fqdn,
hostname_flags,
mud_url,
reject_servers,
orig,
out_new_client_id,
&error)) {
_LOGW("error creating dhclient configuration: %s", error->message);
g_clear_error(&error);
if (orig_path && g_file_test(orig_path, G_FILE_TEST_EXISTS)) {
if (!g_file_get_contents(orig_path, &orig_content, NULL, &error)) {
_LOGW("error reading dhclient configuration %s: %s", orig_path, error->message);
g_error_free(error);
}
}
return new;
new_content = nm_dhcp_dhclient_create_config(iface,
addr_family,
client_id,
anycast_address,
hostname,
timeout,
use_fqdn,
hostname_flags,
mud_url,
reject_servers,
orig_path,
orig_content,
out_new_client_id);
nm_assert(new_content);
if (!g_file_set_contents(new_path, new_content, -1, &error)) {
_LOGW("error creating dhclient configuration: %s", error->message);
g_error_free(error);
g_free(new_path);
return NULL;
}
return new_path;
}
static gboolean
@ -547,6 +508,8 @@ ip4_start(NMDhcpClient *client, GError **error)
client_config = nm_dhcp_client_get_config(client);
nm_assert(client_config->addr_family == AF_INET);
priv->conf_file = create_dhclient_config(self,
AF_INET,
client_config->iface,
@ -584,6 +547,8 @@ ip6_start(NMDhcpClient *client, const struct in6_addr *ll_addr, GError **error)
config = nm_dhcp_client_get_config(client);
nm_assert(config->addr_family == AF_INET6);
if (config->v6.iaid_explicit)
_LOGW("dhclient does not support specifying an IAID for DHCPv6, it will be ignored");