From afb9bb0dac24fdc12271b9632cb721045e9f92a0 Mon Sep 17 00:00:00 2001 From: Francesco Giudici Date: Fri, 23 Aug 2019 12:23:21 +0200 Subject: dhcp: add a shared function to retrieve the dhcp lease file For each plugin we try to come up with a lease file constructed in the same way, i.e., plugin name + iface + connection duid. If the file isn't already there, for some plugins (dhclient) we do extra checks in order to allow to use lease files generated outside of NetworkManager. Let's allow to generate the common NetworkManager dhcp lease file name in a shared function, reporting to the caller if the file isn't already there, so that further plugin specific checks can be performed if needed. --- src/dhcp/nm-dhcp-utils.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ src/dhcp/nm-dhcp-utils.h | 8 +++++++- 2 files changed, 60 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/dhcp/nm-dhcp-utils.c b/src/dhcp/nm-dhcp-utils.c index 3f9368f2dd..4723b129d5 100644 --- a/src/dhcp/nm-dhcp-utils.c +++ b/src/dhcp/nm-dhcp-utils.c @@ -25,6 +25,7 @@ #include "nm-dhcp-utils.h" #include "nm-utils.h" +#include "nm-config.h" #include "NetworkManagerUtils.h" #include "platform/nm-platform.h" #include "nm-dhcp-client-logging.h" @@ -762,3 +763,55 @@ nm_dhcp_utils_client_id_string_to_bytes (const char *client_id) return bytes; } +/** + * nm_dhcp_utils_get_leasefile_path: + * @addr_family: the IP address family + * @plugin_name: the name of the plugin part of the lease file name + * @iface: the interface name to which the lease relates to + * @uuid: uuid of the connection to which the lease relates to + * @out_leasefile_path: will store the computed lease file path + * + * Constructs the lease file name on the basis of the calling plugin, + * interface name and connection uuid. Then returns in @out_leasefile_path + * the full path of the lease filename. + * + * Returns: TRUE if the lease file already exists, FALSE otherwise. + */ +gboolean +nm_dhcp_utils_get_leasefile_path (int addr_family, + const char *plugin_name, + const char *iface, + const char *uuid, + char **out_leasefile_path) +{ + gs_free char *rundir_path = NULL; + gs_free char *statedir_path = NULL; + + rundir_path = g_strdup_printf (NMRUNDIR "/%s%s-%s-%s.lease", + plugin_name, + addr_family == AF_INET6 ? "6" : "", + uuid, + iface); + + if (g_file_test (rundir_path, G_FILE_TEST_EXISTS)) { + *out_leasefile_path = g_steal_pointer (&rundir_path); + return TRUE; + } + + statedir_path = g_strdup_printf (NMSTATEDIR "/%s%s-%s-%s.lease", + plugin_name, + addr_family == AF_INET6 ? "6" : "", + uuid, + iface); + + if (g_file_test (statedir_path, G_FILE_TEST_EXISTS)) { + *out_leasefile_path = g_steal_pointer (&statedir_path); + return TRUE; + } + + if (nm_config_get_configure_and_quit (nm_config_get ()) == NM_CONFIG_CONFIGURE_AND_QUIT_INITRD) + *out_leasefile_path = g_steal_pointer (&rundir_path); + else + *out_leasefile_path = g_steal_pointer (&statedir_path); + return FALSE; +} diff --git a/src/dhcp/nm-dhcp-utils.h b/src/dhcp/nm-dhcp-utils.h index 39ae76932b..36099b5f8f 100644 --- a/src/dhcp/nm-dhcp-utils.h +++ b/src/dhcp/nm-dhcp-utils.h @@ -40,7 +40,13 @@ NMPlatformIP6Address nm_dhcp_utils_ip6_prefix_from_options (GHashTable *options) char *nm_dhcp_utils_duid_to_string (GBytes *duid); -GBytes * nm_dhcp_utils_client_id_string_to_bytes (const char *client_id); +GBytes *nm_dhcp_utils_client_id_string_to_bytes (const char *client_id); + +gboolean nm_dhcp_utils_get_leasefile_path (int addr_family, + const char *plugin_name, + const char *iface, + const char *uuid, + char **out_leasefile_path); #endif /* __NETWORKMANAGER_DHCP_UTILS_H__ */ -- cgit v1.2.1