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.
This commit is contained in:
Francesco Giudici 2019-08-23 12:23:21 +02:00
parent 2ca8b511e6
commit afb9bb0dac
2 changed files with 60 additions and 1 deletions

View file

@ -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;
}

View file

@ -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__ */