mirror of
https://git.proxmox.com/git/mirror_frr
synced 2025-08-06 14:29:47 +00:00
lib: add generic struct ipaddr comparison function
Signed-off-by: Sebastien Merle <sebastien@netdef.org>
This commit is contained in:
parent
904807a637
commit
598b0dfcc9
25
lib/ipaddr.h
25
lib/ipaddr.h
@ -131,6 +131,31 @@ static inline bool ipaddr_isset(struct ipaddr *ip)
|
|||||||
return (0 != memcmp(&a, ip, sizeof(struct ipaddr)));
|
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
|
#ifdef _FRR_ATTRIBUTE_PRINTFRR
|
||||||
#pragma FRR printfrr_ext "%pIA" (struct ipaddr *)
|
#pragma FRR printfrr_ext "%pIA" (struct ipaddr *)
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user