]> git.puffer.fish Git - matthieu/frr.git/commitdiff
lib: add generic struct ipaddr comparison function
authorSebastien Merle <s.merle@gmail.com>
Wed, 11 Dec 2019 15:40:39 +0000 (16:40 +0100)
committerSebastien Merle <sebastien@netdef.org>
Fri, 7 Aug 2020 09:08:49 +0000 (11:08 +0200)
Signed-off-by: Sebastien Merle <sebastien@netdef.org>
lib/ipaddr.h

index f2b75c1306d82a70fe9da572d3a70d69e04066f1..aa5b4a321750b35812459de4f74da7db0bd4e373 100644 (file)
@@ -131,6 +131,31 @@ static inline bool ipaddr_isset(struct ipaddr *ip)
        return (0 != memcmp(&a, ip, sizeof(struct ipaddr)));
 }
 
+/*
+ * generic ordering comparison between IP addresses
+ */
+static inline int ipaddr_cmp(const struct ipaddr *a, const struct ipaddr *b)
+{
+       uint32_t va, vb;
+       va = a->ipa_type;
+       vb = b->ipa_type;
+       if (va != vb)
+               return (va < vb) ? -1 : 1;
+       switch (a->ipa_type) {
+       case IPADDR_V4:
+               va = ntohl(a->ipaddr_v4.s_addr);
+               vb = ntohl(b->ipaddr_v4.s_addr);
+               if (va != vb)
+                       return (va < vb) ? -1 : 1;
+               return 0;
+       case IPADDR_V6:
+               return memcmp((void *)&a->ipaddr_v6, (void *)&b->ipaddr_v6,
+                             sizeof(a->ipaddr_v6));
+       default:
+               return 0;
+       }
+}
+
 #ifdef _FRR_ATTRIBUTE_PRINTFRR
 #pragma FRR printfrr_ext "%pIA"  (struct ipaddr *)
 #endif