diff options
-rw-r--r-- | lib/prefix.h | 3 | ||||
-rw-r--r-- | pimd/pim_addr.h | 56 |
2 files changed, 54 insertions, 5 deletions
diff --git a/lib/prefix.h b/lib/prefix.h index f2773240d..b3545a72b 100644 --- a/lib/prefix.h +++ b/lib/prefix.h @@ -341,9 +341,6 @@ union prefixconstptr { prefixtype(prefixconstptr, const struct prefix_rd, rd) } TRANSPARENT_UNION; -#undef prefixtype -#undef TRANSPARENT_UNION - #ifndef INET_ADDRSTRLEN #define INET_ADDRSTRLEN 16 #endif /* INET_ADDRSTRLEN */ diff --git a/pimd/pim_addr.h b/pimd/pim_addr.h index c7b37b02c..3c4b5a84d 100644 --- a/pimd/pim_addr.h +++ b/pimd/pim_addr.h @@ -21,6 +21,9 @@ #define _PIMD_PIM_ADDR_H #include "jhash.h" +#include "prefix.h" + +/* clang-format off */ /* temporarily disable IPv6 types to keep code compiling. * Defining PIM_V6_TEMP_BREAK will show a lot of compile errors - they are @@ -28,10 +31,39 @@ */ #if PIM_IPV == 4 || !defined(PIM_V6_TEMP_BREAK) typedef struct in_addr pim_addr; -#define PIM_ADDRSTRLEN INET_ADDRSTRLEN + +#define PIM_ADDRSTRLEN INET_ADDRSTRLEN +#define PIM_AF AF_INET +#define PIM_AFI AFI_IP +#define PIM_MAX_BITLEN IPV4_MAX_BITLEN + +union pimprefixptr { + prefixtype(pimprefixptr, struct prefix, p) + prefixtype(pimprefixptr, struct prefix_ipv4, p4) +} TRANSPARENT_UNION; + +union pimprefixconstptr { + prefixtype(pimprefixconstptr, const struct prefix, p) + prefixtype(pimprefixconstptr, const struct prefix_ipv4, p4) +} TRANSPARENT_UNION; + #else typedef struct in6_addr pim_addr; -#define PIM_ADDRSTRLEN INET6_ADDRSTRLEN + +#define PIM_ADDRSTRLEN INET6_ADDRSTRLEN +#define PIM_AF AF_INET6 +#define PIM_AFI AFI_IP6 +#define PIM_MAX_BITLEN IPV6_MAX_BITLEN + +union pimprefixptr { + prefixtype(pimprefixptr, struct prefix, p) + prefixtype(pimprefixptr, struct prefix_ipv6, p6) +} TRANSPARENT_UNION; + +union pimprefixconstptr { + prefixtype(pimprefixconstptr, const struct prefix, p) + prefixtype(pimprefixconstptr, const struct prefix_ipv6, p6) +} TRANSPARENT_UNION; #endif /* for assignment/initialization (C99 compound literal) @@ -39,6 +71,8 @@ typedef struct in6_addr pim_addr; */ #define PIMADDR_ANY (pim_addr){ } +/* clang-format on */ + static inline bool pim_addr_is_any(pim_addr addr) { pim_addr zero = {}; @@ -51,6 +85,24 @@ static inline int pim_addr_cmp(pim_addr a, pim_addr b) return memcmp(&a, &b, sizeof(a)); } +static inline void pim_addr_to_prefix(union pimprefixptr out, pim_addr in) +{ + out.p->family = PIM_AF; + out.p->prefixlen = PIM_MAX_BITLEN; + memcpy(out.p->u.val, &in, sizeof(in)); +} + +static inline pim_addr pim_addr_from_prefix(union pimprefixconstptr in) +{ + pim_addr ret; + + if (in.p->family != PIM_AF) + return PIMADDR_ANY; + + memcpy(&ret, in.p->u.val, sizeof(ret)); + return ret; +} + /* don't use this struct directly, use the pim_sgaddr typedef */ struct _pim_sgaddr { pim_addr grp; |