summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2013-09-06 09:58:55 +0200
committerThomas Haller <thaller@redhat.com>2013-09-25 23:12:37 +0200
commitb1113a0a596afb5de4b24a6de3abd390fa769356 (patch)
tree76e84edead1f1c6af24c7a5afbb7d5fcd3e71ffd
parenta7c7b9f5aa99f17626f036445ee7dcac8008bae5 (diff)
downloadNetworkManager-b1113a0a596afb5de4b24a6de3abd390fa769356.tar.gz
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 <thaller@redhat.com>
-rw-r--r--src/platform/nm-platform.c81
-rw-r--r--src/platform/nm-platform.h5
2 files changed, 86 insertions, 0 deletions
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)