From 3125fa6d42c1c7a5da3530d3c1caf27748b6138a Mon Sep 17 00:00:00 2001 From: David Lamparter Date: Fri, 8 Apr 2022 18:17:38 +0200 Subject: [PATCH] lib: prefix unionization Ahead of May 1st, the workers of the prefix have decided on more unionization. Signed-off-by: David Lamparter --- lib/prefix.c | 30 +++++++++++++++++++++--------- lib/prefix.h | 20 ++++++++++---------- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/lib/prefix.c b/lib/prefix.c index 4db0c2478b..1a3efd32b1 100644 --- a/lib/prefix.c +++ b/lib/prefix.c @@ -177,8 +177,10 @@ const char *safi2str(safi_t safi) } /* If n includes p prefix then return 1 else return 0. */ -int prefix_match(const struct prefix *n, const struct prefix *p) +int prefix_match(union prefixconstptr unet, union prefixconstptr upfx) { + const struct prefix *n = unet.p; + const struct prefix *p = upfx.p; int offset; int shift; const uint8_t *np, *pp; @@ -274,9 +276,11 @@ int evpn_type5_prefix_match(const struct prefix *n, const struct prefix *p) } /* If n includes p then return 1 else return 0. Prefix mask is not considered */ -int prefix_match_network_statement(const struct prefix *n, - const struct prefix *p) +int prefix_match_network_statement(union prefixconstptr unet, + union prefixconstptr upfx) { + const struct prefix *n = unet.p; + const struct prefix *p = upfx.p; int offset; int shift; const uint8_t *np, *pp; @@ -472,8 +476,10 @@ int prefix_cmp(union prefixconstptr up1, union prefixconstptr up2) * address families don't match, return -1; otherwise the return value is * in range 0 ... maximum prefix length for the address family. */ -int prefix_common_bits(const struct prefix *p1, const struct prefix *p2) +int prefix_common_bits(union prefixconstptr ua, union prefixconstptr ub) { + const struct prefix *p1 = ua.p; + const struct prefix *p2 = ub.p; int pos, bit; int length = 0; uint8_t xor ; @@ -509,8 +515,10 @@ int prefix_common_bits(const struct prefix *p1, const struct prefix *p2) } /* Return prefix family type string. */ -const char *prefix_family_str(const struct prefix *p) +const char *prefix_family_str(union prefixconstptr pu) { + const struct prefix *p = pu.p; + if (p->family == AF_INET) return "inet"; if (p->family == AF_INET6) @@ -815,14 +823,16 @@ void apply_mask_ipv6(struct prefix_ipv6 *p) } } -void apply_mask(struct prefix *p) +void apply_mask(union prefixptr pu) { + struct prefix *p = pu.p; + switch (p->family) { case AF_INET: - apply_mask_ipv4((struct prefix_ipv4 *)p); + apply_mask_ipv4(pu.p4); break; case AF_INET6: - apply_mask_ipv6((struct prefix_ipv6 *)p); + apply_mask_ipv6(pu.p6); break; default: break; @@ -868,8 +878,10 @@ void prefix2sockunion(const struct prefix *p, union sockunion *su) sizeof(struct in6_addr)); } -int prefix_blen(const struct prefix *p) +int prefix_blen(union prefixconstptr pu) { + const struct prefix *p = pu.p; + switch (p->family) { case AF_INET: return IPV4_MAX_BYTELEN; diff --git a/lib/prefix.h b/lib/prefix.h index b85f3ce312..e043d41d30 100644 --- a/lib/prefix.h +++ b/lib/prefix.h @@ -435,8 +435,8 @@ extern void prefix_free(struct prefix **p); * Function to handle prefix_free being used as a del function. */ extern void prefix_free_lists(void *arg); -extern const char *prefix_family_str(const struct prefix *); -extern int prefix_blen(const struct prefix *); +extern const char *prefix_family_str(union prefixconstptr pu); +extern int prefix_blen(union prefixconstptr pu); extern int str2prefix(const char *, struct prefix *); #define PREFIX2STR_BUFFER PREFIX_STRLEN @@ -447,14 +447,14 @@ extern const char *prefix_sg2str(const struct prefix_sg *sg, char *str); extern const char *prefix2str(union prefixconstptr, char *, int); extern int evpn_type5_prefix_match(const struct prefix *evpn_pfx, const struct prefix *match_pfx); -extern int prefix_match(const struct prefix *, const struct prefix *); -extern int prefix_match_network_statement(const struct prefix *, - const struct prefix *); -extern int prefix_same(union prefixconstptr, union prefixconstptr); -extern int prefix_cmp(union prefixconstptr, union prefixconstptr); -extern int prefix_common_bits(const struct prefix *, const struct prefix *); -extern void prefix_copy(union prefixptr, union prefixconstptr); -extern void apply_mask(struct prefix *); +extern int prefix_match(union prefixconstptr unet, union prefixconstptr upfx); +extern int prefix_match_network_statement(union prefixconstptr unet, + union prefixconstptr upfx); +extern int prefix_same(union prefixconstptr ua, union prefixconstptr ub); +extern int prefix_cmp(union prefixconstptr ua, union prefixconstptr ub); +extern int prefix_common_bits(union prefixconstptr ua, union prefixconstptr ub); +extern void prefix_copy(union prefixptr udst, union prefixconstptr usrc); +extern void apply_mask(union prefixptr pu); #ifdef __clang_analyzer__ /* clang-SA doesn't understand transparent unions, making it think that the -- 2.39.5