net: if: Add function to obtain IPv4 netmask

Add a helper function to obtain IPv4 netmask configured on an interface.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
This commit is contained in:
Robert Lubos 2024-01-16 17:38:16 +01:00 committed by Fabio Baltieri
parent 1d14f13d75
commit db80ed3e8d
2 changed files with 30 additions and 0 deletions

View file

@ -2279,6 +2279,15 @@ struct in_addr *net_if_ipv4_get_ll(struct net_if *iface,
struct in_addr *net_if_ipv4_get_global_addr(struct net_if *iface,
enum net_addr_state addr_state);
/**
* @brief Get IPv4 netmask of an interface.
*
* @param iface Interface to use.
*
* @return The netmask set on the interface, unspecified address if not found.
*/
struct in_addr net_if_ipv4_get_netmask(struct net_if *iface);
/**
* @brief Set IPv4 netmask for an interface.
*

View file

@ -3619,6 +3619,27 @@ static inline int z_vrfy_net_if_ipv4_addr_lookup_by_index(
#include <syscalls/net_if_ipv4_addr_lookup_by_index_mrsh.c>
#endif
struct in_addr net_if_ipv4_get_netmask(struct net_if *iface)
{
struct in_addr netmask = { 0 };
net_if_lock(iface);
if (net_if_config_ipv4_get(iface, NULL) < 0) {
goto out;
}
if (!iface->config.ip.ipv4) {
goto out;
}
netmask = iface->config.ip.ipv4->netmask;
out:
net_if_unlock(iface);
return netmask;
}
void net_if_ipv4_set_netmask(struct net_if *iface,
const struct in_addr *netmask)
{