lib: add generic struct ipaddr comparison function

Signed-off-by: Sebastien Merle <sebastien@netdef.org>
This commit is contained in:
Sebastien Merle 2019-12-11 16:40:39 +01:00 committed by Sebastien Merle
parent 904807a637
commit 598b0dfcc9

View 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