diff options
author | Thomas Haller <thaller@redhat.com> | 2014-04-03 11:42:00 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2014-05-03 03:44:23 +0200 |
commit | 9cd7b40a047bb4bbc2e9470158470c43badcc109 (patch) | |
tree | 67038675fc9539bbca08ef8a4d7f769114e743d3 | |
parent | a8c17a2517017b9282e3b13bba41c73dbaf0e858 (diff) | |
download | NetworkManager-9cd7b40a047bb4bbc2e9470158470c43badcc109.tar.gz |
platform: add nm_platform_ip_address_cmp_expiry()
This compares two addresses and returns which one has a longer
remaining life (i.e. a later expiry timestamp).
Signed-off-by: Thomas Haller <thaller@redhat.com>
-rw-r--r-- | src/platform/nm-platform.c | 52 | ||||
-rw-r--r-- | src/platform/nm-platform.h | 2 |
2 files changed, 53 insertions, 1 deletions
diff --git a/src/platform/nm-platform.c b/src/platform/nm-platform.c index 7fd7642a23..083359eec6 100644 --- a/src/platform/nm-platform.c +++ b/src/platform/nm-platform.c @@ -2339,10 +2339,60 @@ nm_platform_ip6_route_cmp (const NMPlatformIP6Route *a, const NMPlatformIP6Route return 0; } -#undef _CMP_POINTER #undef _CMP_FIELD #undef _CMP_FIELD_MEMCMP +/** + * nm_platform_ip_address_cmp_expiry: + * @a: a NMPlatformIPAddress to compare + * @b: the other NMPlatformIPAddress to compare + * + * Compares two addresses and returns which one has a longer remaining lifetime. + * If both addresses have the same lifetime, look at the remaining preferred time. + * + * For comparison, only the timestamp, lifetime and preferred fields are considered. + * If they compare equal (== 0), their other fields were not considered. + * + * Returns: -1, 0, or 1 according to the comparison + **/ +int +nm_platform_ip_address_cmp_expiry (const NMPlatformIPAddress *a, const NMPlatformIPAddress *b) +{ + gint64 ta, tb; + + _CMP_POINTER (a, b); + + if (a->lifetime == NM_PLATFORM_LIFETIME_PERMANENT || a->lifetime == 0) + ta = G_MAXINT64; + else + ta = ((gint64) a->timestamp) + a->lifetime; + + if (b->lifetime == NM_PLATFORM_LIFETIME_PERMANENT || b->lifetime == 0) + tb = G_MAXINT64; + else + tb = ((gint64) b->timestamp) + b->lifetime; + + if (ta == tb) { + /* if the lifetime is equal, compare the preferred time. */ + + if (a->preferred == NM_PLATFORM_LIFETIME_PERMANENT || a->lifetime == 0 /* liftime==0 means permanent! */) + ta = G_MAXINT64; + else + ta = ((gint64) a->timestamp) + a->preferred; + + if (b->preferred == NM_PLATFORM_LIFETIME_PERMANENT|| b->lifetime == 0) + tb = G_MAXINT64; + else + tb = ((gint64) b->timestamp) + b->preferred; + + if (ta == tb) + return 0; + } + + return ta < tb ? -1 : 1; +} + +#undef _CMP_POINTER static const char * _change_type_to_string (NMPlatformSignalChangeType change_type) diff --git a/src/platform/nm-platform.h b/src/platform/nm-platform.h index f30af17153..b07d74ee51 100644 --- a/src/platform/nm-platform.h +++ b/src/platform/nm-platform.h @@ -596,6 +596,8 @@ gboolean nm_platform_check_support_kernel_extended_ifa_flags (void); void nm_platform_addr_flags2str (int flags, char *buf, size_t size); +int nm_platform_ip_address_cmp_expiry (const NMPlatformIPAddress *a, const NMPlatformIPAddress *b); + #define auto_g_free __attribute__((cleanup(put_g_free))) static void __attribute__((unused)) put_g_free (void *ptr) |