On Sun, Dec 14, 2025 at 08:54:31PM -0500, Jon Maloy wrote:
We add a helper function to calculate the default IPv4 prefix length based on address class. This is used to replace the current inline calculation in conf_ip4(), and is also a preparation for more uses of this functionality in the coming commits.
Signed-off-by: Jon Maloy
Reviewed-by: David Gibson
--- conf.c | 15 +++------------ ip.c | 21 +++++++++++++++++++++ ip.h | 2 ++ 3 files changed, 26 insertions(+), 12 deletions(-)
diff --git a/conf.c b/conf.c index 0e96f36..31acc20 100644 --- a/conf.c +++ b/conf.c @@ -733,18 +733,9 @@ static unsigned int conf_ip4(unsigned int ifi, struct ip4_ctx *ip4, }
/* Apply default prefix_len to first address if not set */ - if (!ip4->addrs[0].prefix_len) { - in_addr_t a = ntohl(ip4->addrs[0].addr.s_addr); - - if (IN_CLASSA(a)) - ip4->addrs[0].prefix_len = 8; - else if (IN_CLASSB(a)) - ip4->addrs[0].prefix_len = 16; - else if (IN_CLASSC(a)) - ip4->addrs[0].prefix_len = 24; - else - ip4->addrs[0].prefix_len = 32; - } + if (!ip4->addrs[0].prefix_len) + ip4->addrs[0].prefix_len = + ip4_default_prefix_len(&ip4->addrs[0].addr);
ip4->addr_seen = ip4->addrs[0].addr;
diff --git a/ip.c b/ip.c index 9a7f4c5..2519c71 100644 --- a/ip.c +++ b/ip.c @@ -13,6 +13,8 @@ */
#include
+#include + #include "util.h" #include "ip.h" @@ -67,3 +69,22 @@ found: *proto = nh; return true; } + +/** + * ip4_default_prefix_len() - Get default prefix length for IPv4 address + * @addr: IPv4 address + * + * Return: prefix length based on address class (8/16/24), or 32 for other + */ +int ip4_default_prefix_len(const struct in_addr *addr) +{ + in_addr_t a = ntohl(addr->s_addr); + + if (IN_CLASSA(a)) + return 8; + if (IN_CLASSB(a)) + return 16; + if (IN_CLASSC(a)) + return 24; + return 32; +} diff --git a/ip.h b/ip.h index 748cb1f..065b78b 100644 --- a/ip.h +++ b/ip.h @@ -139,6 +139,8 @@ static const struct in_addr in4addr_broadcast = { 0xffffffff }; #define IP4_MAX_ADDRS 8 #define IP6_MAX_ADDRS 16
+int ip4_default_prefix_len(const struct in_addr *addr); + /** * struct ip4_addr_entry - IPv4 address with prefix length * @addr: IPv4 address -- 2.51.1
-- David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson