diff options
| author | Rafael Zalamena <rzalamena@opensourcerouting.org> | 2021-07-08 14:09:20 -0300 |
|---|---|---|
| committer | Rafael Zalamena <rzalamena@opensourcerouting.org> | 2021-07-09 07:55:25 -0300 |
| commit | 1fe59b44fc94337f0b508bc762a401a9dbe9c1a3 (patch) | |
| tree | 8a525db11e5c2b61c3b8cb29b9eb55c4ed9bd6f5 /lib/prefix.h | |
| parent | f45f6a22b052988a6ca3399fde66249e8b6e00cc (diff) | |
lib,ospfd,ospf6d: remove duplicated function
Move `is_default_prefix` variations to `lib/prefix.h` and make the code
use the library version instead of implementing it again.
NOTE
----
The function was split into per family versions to cover all types.
Using `union prefixconstptr` is not possible due to static analyzer
warnings which cause CI to fail.
The specific cases that would cause this failure were:
- Caller used `struct prefix_ipv4` and called the generic function.
- `is_default_prefix` with signature using `const struct prefix *` or
`union prefixconstptr`.
The compiler would complain about reading bytes outside of the memory
bounds even though it did not take into account the `prefix->family`
part.
Signed-off-by: Rafael Zalamena <rzalamena@opensourcerouting.org>
Diffstat (limited to 'lib/prefix.h')
| -rw-r--r-- | lib/prefix.h | 32 |
1 files changed, 22 insertions, 10 deletions
diff --git a/lib/prefix.h b/lib/prefix.h index bc4cb7f441..944c94f57f 100644 --- a/lib/prefix.h +++ b/lib/prefix.h @@ -537,20 +537,32 @@ static inline int ipv4_martian(struct in_addr *addr) return 0; } -static inline int is_default_prefix(const struct prefix *p) +static inline bool is_default_prefix4(const struct prefix_ipv4 *p) { - if (!p) - return 0; + return p && p->family == AF_INET && p->prefixlen == 0 + && p->prefix.s_addr == INADDR_ANY; +} - if ((p->family == AF_INET) && (p->u.prefix4.s_addr == INADDR_ANY) - && (p->prefixlen == 0)) - return 1; +static inline bool is_default_prefix6(const struct prefix_ipv6 *p) +{ + return p && p->family == AF_INET6 && p->prefixlen == 0 + && memcmp(&p->prefix, &in6addr_any, sizeof(struct in6_addr)) + == 0; +} - if ((p->family == AF_INET6) && (p->prefixlen == 0) - && (!memcmp(&p->u.prefix6, &in6addr_any, sizeof(struct in6_addr)))) - return 1; +static inline bool is_default_prefix(const struct prefix *p) +{ + if (p == NULL) + return false; + + switch (p->family) { + case AF_INET: + return is_default_prefix4((const struct prefix_ipv4 *)p); + case AF_INET6: + return is_default_prefix6((const struct prefix_ipv6 *)p); + } - return 0; + return false; } static inline int is_host_route(const struct prefix *p) |
