From b1113a0a596afb5de4b24a6de3abd390fa769356 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 6 Sep 2013 09:58:55 +0200 Subject: [PATCH] core: add nm_platform_ip[46]_*_cmp functions New functions to compare two instances of NMPlatformIP4Address, NMPlatformIP6Address, NMPlatformIP4Route, NMPlatformIP6Route, respectively. These functions return -1, 0 or 1 as result of the comparison. This is similar to strcmp with the additional restriction, that only one of these 3 values will be returned. Signed-off-by: Thomas Haller --- src/platform/nm-platform.c | 81 ++++++++++++++++++++++++++++++++++++++ src/platform/nm-platform.h | 5 +++ 2 files changed, 86 insertions(+) diff --git a/src/platform/nm-platform.c b/src/platform/nm-platform.c index bc382d8f3d..f5a4c9b0f3 100644 --- a/src/platform/nm-platform.c +++ b/src/platform/nm-platform.c @@ -1690,6 +1690,87 @@ nm_platform_ip6_route_to_string (const NMPlatformIP6Route *route) return buffer; } +#define _CMP_POINTER(a, b) \ + G_STMT_START { \ + if ((a) == (b)) \ + return 0; \ + if (!(a)) \ + return -1; \ + if (!(b)) \ + return 1; \ + } G_STMT_END + +#define _CMP_FIELD(a, b, field) \ + G_STMT_START { \ + if (((a)->field) != ((b)->field)) \ + return (((a)->field) < ((b)->field)) ? -1 : 1; \ + } G_STMT_END + +#define _CMP_FIELD_MEMCMP(a, b, field) \ + G_STMT_START { \ + int c = memcmp (&((a)->field), &((b)->field), \ + sizeof ((a)->field)); \ + if (c != 0) \ + return c < 0 ? -1 : 1; \ + } G_STMT_END + +int +nm_platform_ip4_address_cmp (const NMPlatformIP4Address *a, const NMPlatformIP4Address *b) +{ + _CMP_POINTER (a, b); + _CMP_FIELD_MEMCMP (a, b, address); + _CMP_FIELD (a, b, ifindex); + _CMP_FIELD (a, b, plen); + _CMP_FIELD (a, b, timestamp); + _CMP_FIELD (a, b, lifetime); + _CMP_FIELD (a, b, preferred); + return 0; +} + +int +nm_platform_ip6_address_cmp (const NMPlatformIP6Address *a, const NMPlatformIP6Address *b) +{ + _CMP_POINTER (a, b); + _CMP_FIELD (a, b, ifindex); + _CMP_FIELD_MEMCMP (a, b, address); + _CMP_FIELD (a, b, plen); + _CMP_FIELD (a, b, timestamp); + _CMP_FIELD (a, b, lifetime); + _CMP_FIELD (a, b, preferred); + return 0; +} + +int +nm_platform_ip4_route_cmp (const NMPlatformIP4Route *a, const NMPlatformIP4Route *b) +{ + _CMP_POINTER (a, b); + _CMP_FIELD (a, b, ifindex); + _CMP_FIELD_MEMCMP (a, b, network); + _CMP_FIELD (a, b, plen); + _CMP_FIELD_MEMCMP (a, b, gateway); + _CMP_FIELD (a, b, metric); + _CMP_FIELD (a, b, mss); + return 0; +} + +int +nm_platform_ip6_route_cmp (const NMPlatformIP6Route *a, const NMPlatformIP6Route *b) +{ + _CMP_POINTER (a, b); + _CMP_FIELD (a, b, ifindex); + _CMP_FIELD_MEMCMP (a, b, network); + _CMP_FIELD (a, b, plen); + _CMP_FIELD_MEMCMP (a, b, gateway); + _CMP_FIELD (a, b, metric); + _CMP_FIELD (a, b, mss); + return 0; +} + +#undef _CMP_POINTER +#undef _CMP_FIELD +#undef _CMP_FIELD_MEMCMP + + static void log_link (NMPlatformLink *device, const char *change_type) { diff --git a/src/platform/nm-platform.h b/src/platform/nm-platform.h index b881abc8e5..68d2fdce32 100644 --- a/src/platform/nm-platform.h +++ b/src/platform/nm-platform.h @@ -426,6 +426,11 @@ const char *nm_platform_ip6_address_to_string (const NMPlatformIP6Address *addre const char *nm_platform_ip4_route_to_string (const NMPlatformIP4Route *route); const char *nm_platform_ip6_route_to_string (const NMPlatformIP6Route *route); +int nm_platform_ip4_address_cmp (const NMPlatformIP4Address *a, const NMPlatformIP4Address *b); +int nm_platform_ip6_address_cmp (const NMPlatformIP6Address *a, const NMPlatformIP6Address *b); +int nm_platform_ip4_route_cmp (const NMPlatformIP4Route *a, const NMPlatformIP4Route *b); +int nm_platform_ip6_route_cmp (const NMPlatformIP6Route *a, const NMPlatformIP6Route *b); + #define auto_g_free __attribute__((cleanup(put_g_free))) static void __attribute__((unused)) put_g_free (void *ptr)