From de9b87f55863f29eb7965d8a772f8a34f195f4ac Mon Sep 17 00:00:00 2001 From: Pavel TvrdĂ­k Date: Thu, 7 Jan 2016 18:17:16 +0100 Subject: Add NET ROA4/6 structures --- lib/net.h | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 4 deletions(-) (limited to 'lib/net.h') diff --git a/lib/net.h b/lib/net.h index b0ab2153..c5fbe38b 100644 --- a/lib/net.h +++ b/lib/net.h @@ -17,7 +17,9 @@ #define NET_IP6 2 #define NET_VPN4 3 #define NET_VPN6 4 -#define NET_MAX 5 +#define NET_ROA4 5 +#define NET_ROA6 6 +#define NET_MAX 7 typedef struct net_addr { u8 type; @@ -57,6 +59,23 @@ typedef struct net_addr_vpn6 { u64 rd; } net_addr_vpn6; +typedef struct net_addr_roa4 { + u8 type; + u8 pxlen; + u16 length; + ip4_addr prefix; + u32 asn; + u8 src; +} net_addr_roa4; + +typedef struct net_addr_roa6 { + u8 type; + u8 pxlen; + u16 length; + ip6_addr prefix; + u32 asn; + u8 src; +} net_addr_roa6; typedef union net_addr_union { net_addr n; @@ -64,6 +83,8 @@ typedef union net_addr_union { net_addr_ip6 ip6; net_addr_vpn4 vpn4; net_addr_vpn6 vpn6; + net_addr_roa4 roa4; + net_addr_roa6 roa6; } net_addr_union; @@ -130,10 +151,17 @@ static inline ip_addr net_prefix(const net_addr *a) switch (a->type) { case NET_IP4: - case NET_VPN4: return ipa_from_ip4(net4_prefix(a)); + case NET_VPN4: + case NET_ROA4: + return ipa_from_ip4(net4_prefix(a)); + case NET_IP6: - case NET_VPN6: return ipa_from_ip6(net6_prefix(a)); - default: return IPA_NONE; + case NET_VPN6: + case NET_ROA6: + return ipa_from_ip6(net6_prefix(a)); + + default: + return IPA_NONE; } } @@ -164,6 +192,12 @@ static inline int net_equal_vpn4(const net_addr_vpn4 *a, const net_addr_vpn4 *b) static inline int net_equal_vpn6(const net_addr_vpn6 *a, const net_addr_vpn6 *b) { return !memcmp(a, b, sizeof(net_addr_vpn6)); } +static inline int net_equal_roa4(const net_addr_roa4 *a, const net_addr_roa4 *b) +{ return !memcmp(a, b, sizeof(net_addr_roa4)); } + +static inline int net_equal_roa6(const net_addr_roa6 *a, const net_addr_roa6 *b) +{ return !memcmp(a, b, sizeof(net_addr_roa6)); } + static inline int net_zero_ip4(const net_addr_ip4 *a) { return !a->pxlen && ip4_zero(a->prefix); } @@ -177,6 +211,12 @@ static inline int net_zero_vpn4(const net_addr_vpn4 *a) static inline int net_zero_vpn6(const net_addr_vpn6 *a) { return !a->pxlen && ip6_zero(a->prefix) && !a->rd; } +static inline int net_zero_roa4(const net_addr_roa4 *a) +{ return !a->pxlen && ip4_zero(a->prefix) && !a->asn; } + +static inline int net_zero_roa6(const net_addr_roa6 *a) +{ return !a->pxlen && ip6_zero(a->prefix) && !a->asn; } + static inline int net_compare_ip4(const net_addr_ip4 *a, const net_addr_ip4 *b) { return ip4_compare(a->prefix, b->prefix) ?: uint_cmp(a->pxlen, b->pxlen); } @@ -190,6 +230,12 @@ static inline int net_compare_vpn4(const net_addr_vpn4 *a, const net_addr_vpn4 * static inline int net_compare_vpn6(const net_addr_vpn6 *a, const net_addr_vpn6 *b) { return u64_cmp(a->rd, b->rd) ?: ip6_compare(a->prefix, b->prefix) ?: uint_cmp(a->pxlen, b->pxlen); } +static inline int net_compare_roa4(const net_addr_roa4 *a, const net_addr_roa4 *b) +{ return ip4_compare(a->prefix, b->prefix) ?: uint_cmp(a->pxlen, b->pxlen) ?: uint_cmp(a->asn, b->asn); } + +static inline int net_compare_roa6(const net_addr_roa6 *a, const net_addr_roa6 *b) +{ return ip6_compare(a->prefix, b->prefix) ?: uint_cmp(a->pxlen, b->pxlen) ?: uint_cmp(a->asn, b->asn); } + int net_compare(const net_addr *a, const net_addr *b); @@ -208,6 +254,12 @@ static inline void net_copy_vpn4(net_addr_vpn4 *dst, const net_addr_vpn4 *src) static inline void net_copy_vpn6(net_addr_vpn6 *dst, const net_addr_vpn6 *src) { memcpy(dst, src, sizeof(net_addr_vpn6)); } +static inline void net_copy_roa4(net_addr_roa4 *dst, const net_addr_roa4 *src) +{ memcpy(dst, src, sizeof(net_addr_roa4)); } + +static inline void net_copy_roa6(net_addr_roa6 *dst, const net_addr_roa6 *src) +{ memcpy(dst, src, sizeof(net_addr_roa6)); } + static inline u32 net_hash_ip4(const net_addr_ip4 *n) { return ip4_hash(n->prefix) ^ ((u32) n->pxlen << 26); } @@ -225,6 +277,12 @@ static inline u32 net_hash_vpn4(const net_addr_vpn4 *n) static inline u32 net_hash_vpn6(const net_addr_vpn6 *n) { return ip6_hash(n->prefix) ^ ((u32) n->pxlen << 26) ^ u64_hash(n->rd); } +static inline u32 net_hash_roa4(const net_addr_roa4 *n) +{ return ip4_hash(n->prefix) ^ ((u32) n->pxlen << 26) ^ u32_hash(n->asn); } + +static inline u32 net_hash_roa6(const net_addr_roa6 *n) +{ return ip6_hash(n->prefix) ^ ((u32) n->pxlen << 26) ^ u32_hash(n->asn); } + static inline int net_validate_ip4(const net_addr_ip4 *n) { -- cgit v1.2.3