diff options
| author | Sebastien Merle <s.merle@gmail.com> | 2019-12-11 16:40:39 +0100 |
|---|---|---|
| committer | Sebastien Merle <sebastien@netdef.org> | 2020-08-07 11:08:49 +0200 |
| commit | 598b0dfcc969c048a70b03fcc9b70d44b69fcfd6 (patch) | |
| tree | d865195eae2b6483f5b2f2a72874787e7abeda30 /lib/ipaddr.h | |
| parent | 904807a6376d3d9672662364f63d400af6a93f90 (diff) | |
lib: add generic struct ipaddr comparison function
Signed-off-by: Sebastien Merle <sebastien@netdef.org>
Diffstat (limited to 'lib/ipaddr.h')
| -rw-r--r-- | lib/ipaddr.h | 25 |
1 files changed, 25 insertions, 0 deletions
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 |
