core: return remaining lifetime from nm_utils_lifetime_get()

nm_utils_lifetime_get() already has so many arguments.
Essentially, the function returned %TRUE if and only if the
lifetime was greater then zero.

Combine the return value and the output argument for the lifetime.

It also matches better the function name: to get the lifetime.
This commit is contained in:
Thomas Haller
2018-02-07 21:32:10 +01:00
parent 1de10532f2
commit 7459548f23
4 changed files with 43 additions and 42 deletions

View File

@@ -2840,8 +2840,9 @@ ndisc_set_router_config (NMNDisc *ndisc, NMDevice *self)
} else
base = now;
if (!nm_utils_lifetime_get (addr->timestamp, addr->lifetime, addr->preferred,
base, &lifetime, &preferred))
lifetime = nm_utils_lifetime_get (addr->timestamp, addr->lifetime, addr->preferred,
base, &preferred);
if (!lifetime)
continue;
g_array_set_size (addresses, addresses->len+1);

View File

@@ -3891,12 +3891,11 @@ nm_utils_lifetime_rebase_relative_time_on_now (guint32 timestamp,
return t;
}
gboolean
guint32
nm_utils_lifetime_get (guint32 timestamp,
guint32 lifetime,
guint32 preferred,
gint32 now,
guint32 *out_lifetime,
guint32 *out_preferred)
{
guint32 t_lifetime, t_preferred;
@@ -3904,38 +3903,39 @@ nm_utils_lifetime_get (guint32 timestamp,
nm_assert (now >= 0);
if (timestamp == 0 && lifetime == 0) {
/* We treat lifetime==0 && timestamp == 0 addresses as permanent addresses to allow easy
/* We treat lifetime==0 && timestamp==0 addresses as permanent addresses to allow easy
* creation of such addresses (without requiring to set the lifetime fields to
* NM_PLATFORM_LIFETIME_PERMANENT). The real lifetime==0 addresses (E.g. DHCP6 telling us
* to drop an address will have timestamp set.
*/
NM_SET_OUT (out_lifetime, NM_PLATFORM_LIFETIME_PERMANENT);
NM_SET_OUT (out_preferred, NM_PLATFORM_LIFETIME_PERMANENT);
g_return_val_if_fail (preferred == 0, TRUE);
} else {
if (now <= 0)
now = nm_utils_get_monotonic_timestamp_s ();
t_lifetime = nm_utils_lifetime_rebase_relative_time_on_now (timestamp, lifetime, now);
if (!t_lifetime) {
NM_SET_OUT (out_lifetime, 0);
NM_SET_OUT (out_preferred, 0);
return FALSE;
}
t_preferred = nm_utils_lifetime_rebase_relative_time_on_now (timestamp, preferred, now);
NM_SET_OUT (out_lifetime, t_lifetime);
NM_SET_OUT (out_preferred, MIN (t_preferred, t_lifetime));
/* Assert that non-permanent addresses have a (positive) @timestamp. nm_utils_lifetime_rebase_relative_time_on_now()
* treats addresses with timestamp 0 as *now*. Addresses passed to _address_get_lifetime() always
* should have a valid @timestamp, otherwise on every re-sync, their lifetime will be extended anew.
*/
g_return_val_if_fail ( timestamp != 0
|| ( lifetime == NM_PLATFORM_LIFETIME_PERMANENT
&& preferred == NM_PLATFORM_LIFETIME_PERMANENT), TRUE);
g_return_val_if_fail (t_preferred <= t_lifetime, TRUE);
g_return_val_if_fail (preferred == 0, NM_PLATFORM_LIFETIME_PERMANENT);
return NM_PLATFORM_LIFETIME_PERMANENT;
}
return TRUE;
if (now <= 0)
now = nm_utils_get_monotonic_timestamp_s ();
t_lifetime = nm_utils_lifetime_rebase_relative_time_on_now (timestamp, lifetime, now);
if (!t_lifetime) {
NM_SET_OUT (out_preferred, 0);
return 0;
}
t_preferred = nm_utils_lifetime_rebase_relative_time_on_now (timestamp, preferred, now);
NM_SET_OUT (out_preferred, MIN (t_preferred, t_lifetime));
/* Assert that non-permanent addresses have a (positive) @timestamp. nm_utils_lifetime_rebase_relative_time_on_now()
* treats addresses with timestamp 0 as *now*. Addresses passed to _address_get_lifetime() always
* should have a valid @timestamp, otherwise on every re-sync, their lifetime will be extended anew.
*/
g_return_val_if_fail ( timestamp != 0
|| ( lifetime == NM_PLATFORM_LIFETIME_PERMANENT
&& preferred == NM_PLATFORM_LIFETIME_PERMANENT), t_lifetime);
g_return_val_if_fail (t_preferred <= t_lifetime, t_lifetime);
return t_lifetime;
}
const char *

View File

@@ -408,12 +408,11 @@ guint32 nm_utils_lifetime_rebase_relative_time_on_now (guint32 timestamp,
guint32 duration,
gint32 now);
gboolean nm_utils_lifetime_get (guint32 timestamp,
guint32 lifetime,
guint32 preferred,
gint32 now,
guint32 *out_lifetime,
guint32 *out_preferred);
guint32 nm_utils_lifetime_get (guint32 timestamp,
guint32 lifetime,
guint32 preferred,
gint32 now,
guint32 *out_preferred);
gboolean nm_utils_ip4_address_is_link_local (in_addr_t addr);

View File

@@ -3145,7 +3145,6 @@ array_ip6_address_position (const GPtrArray *addresses,
candidate->lifetime,
candidate->preferred,
now,
NULL,
NULL))
return pos;
@@ -3348,7 +3347,7 @@ nm_platform_ip4_address_sync (NMPlatform *self,
known_address = NMP_OBJECT_CAST_IP4_ADDRESS (known_addresses->pdata[i]);
if (!nm_utils_lifetime_get (known_address->timestamp, known_address->lifetime, known_address->preferred,
now, NULL, NULL))
now, NULL))
goto delete_and_next;
if (G_UNLIKELY (!known_addresses_idx)) {
@@ -3466,8 +3465,9 @@ delete_and_next:
known_address = NMP_OBJECT_CAST_IP4_ADDRESS (o);
if (!nm_utils_lifetime_get (known_address->timestamp, known_address->lifetime, known_address->preferred,
now, &lifetime, &preferred))
lifetime = nm_utils_lifetime_get (known_address->timestamp, known_address->lifetime, known_address->preferred,
now, &preferred);
if (!lifetime)
goto delete_and_next2;
if (!nm_platform_ip4_address_add (self, ifindex, known_address->address, known_address->plen,
@@ -3578,8 +3578,9 @@ nm_platform_ip6_address_sync (NMPlatform *self,
continue;
}
if (!nm_utils_lifetime_get (known_address->timestamp, known_address->lifetime, known_address->preferred,
now, &lifetime, &preferred))
lifetime = nm_utils_lifetime_get (known_address->timestamp, known_address->lifetime, known_address->preferred,
now, &preferred);
if (!lifetime)
continue;
if (!nm_platform_ip6_address_add (self, ifindex, known_address->address,