Commit 76f35aca authored by Beniamino Galvani's avatar Beniamino Galvani

dhcp: nettools: fix lease time computation

n_dhcp4_client_lease_get_lifetime() returns the lifetime since
CLOCK_BOOTTIME.

Fixes: 6adade6f ('dhcp: add nettools dhcp4 client')
parent 27d380b7
......@@ -365,11 +365,13 @@ lease_parse_address (NDhcp4ClientLease *lease,
GError **error)
{
char addr_str[NM_UTILS_INET_ADDRSTRLEN];
const gint64 ts_boot = nm_utils_clock_gettime_ns (CLOCK_BOOTTIME);
const gint64 ts = nm_utils_get_monotonic_timestamp_ns ();
struct in_addr a_address;
struct in_addr a_netmask;
guint32 a_plen;
guint64 a_lifetime;
guint32 a_lifetime_sec;
n_dhcp4_client_lease_get_yiaddr (lease, &a_address);
if (a_address.s_addr == INADDR_ANY) {
......@@ -379,6 +381,10 @@ lease_parse_address (NDhcp4ClientLease *lease,
/* n_dhcp4_client_lease_get_lifetime() never fails */
n_dhcp4_client_lease_get_lifetime (lease, &a_lifetime);
if (a_lifetime == (uint64_t)-1)
a_lifetime_sec = NM_PLATFORM_LIFETIME_PERMANENT;
else
a_lifetime_sec = (a_lifetime - ts_boot + NM_UTILS_NS_PER_SECOND/2 - 1) / NM_UTILS_NS_PER_SECOND;
if (!lease_get_in_addr (lease, NM_DHCP_OPTION_DHCP4_SUBNET_MASK, &a_netmask)) {
nm_utils_error_set_literal (error, NM_UTILS_ERROR_UNKNOWN, "could not get netmask from lease");
......@@ -398,12 +404,11 @@ lease_parse_address (NDhcp4ClientLease *lease,
NM_DHCP_OPTION_DHCP4_SUBNET_MASK,
nm_utils_inet4_ntop (a_netmask.s_addr, addr_str));
LOG_LEASE (LOGD_DHCP4, "expires in %u seconds",
(guint) ((a_lifetime - ts)/1000000000));
LOG_LEASE (LOGD_DHCP4, "expires in %u seconds", a_lifetime_sec);
nm_dhcp_option_add_option_u64 (options,
_nm_dhcp_option_dhcp4_options,
NM_DHCP_OPTION_DHCP4_IP_ADDRESS_LEASE_TIME,
(guint64) (a_lifetime / 1000000000));
a_lifetime_sec);
nm_ip4_config_add_address (ip4_config,
&((const NMPlatformIP4Address) {
......@@ -411,9 +416,9 @@ lease_parse_address (NDhcp4ClientLease *lease,
.peer_address = a_address.s_addr,
.plen = a_plen,
.addr_source = NM_IP_CONFIG_SOURCE_DHCP,
.timestamp = ts / 1000000000,
.lifetime = (a_lifetime - ts) / 1000000000,
.preferred = (a_lifetime - ts) / 1000000000,
.timestamp = ts / NM_UTILS_NS_PER_SECOND,
.lifetime = a_lifetime_sec,
.preferred = a_lifetime_sec,
}));
return TRUE;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment