From 598b0dfcc969c048a70b03fcc9b70d44b69fcfd6 Mon Sep 17 00:00:00 2001 From: Sebastien Merle Date: Wed, 11 Dec 2019 16:40:39 +0100 Subject: [PATCH] lib: add generic struct ipaddr comparison function Signed-off-by: Sebastien Merle --- lib/ipaddr.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lib/ipaddr.h b/lib/ipaddr.h index f2b75c1306..aa5b4a3217 100644 --- a/lib/ipaddr.h +++ b/lib/ipaddr.h @@ -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 -- 2.39.5