net: Add util to check if IPv4 address is part of a subnet
Change-Id: I6005861a4c4085b6c17ded03fda38a567a4e504a Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
This commit is contained in:
parent
79fa53e17d
commit
702ea43621
3 changed files with 47 additions and 0 deletions
|
@ -334,6 +334,15 @@ struct in6_addr *net_if_ipv6_get_ll(struct net_if *iface,
|
|||
*/
|
||||
struct net_if *net_if_get_default(void);
|
||||
|
||||
/**
|
||||
* @brief Check if the given IPv4 address belongs to local subnet.
|
||||
* @param iface Interface to use. Must be a valid pointer to an interface.
|
||||
* @param addr IPv4 address
|
||||
* @return True if address is part of local subnet, false otherwise.
|
||||
*/
|
||||
bool net_if_ipv4_addr_mask_cmp(struct net_if *iface,
|
||||
struct in_addr *addr);
|
||||
|
||||
/**
|
||||
* @brief Set IPv4 netmask for an interface.
|
||||
* @param iface Interface to use.
|
||||
|
|
|
@ -351,6 +351,21 @@ static inline const struct in_addr *net_ipv4_broadcast_address(void)
|
|||
return net_if_ipv4_broadcast_addr();
|
||||
}
|
||||
|
||||
struct net_if;
|
||||
extern bool net_if_ipv4_addr_mask_cmp(struct net_if *iface,
|
||||
struct in_addr *addr);
|
||||
|
||||
/** @brief Check if the given address belongs to same subnet that
|
||||
* has been configured for the interface.
|
||||
*
|
||||
* @return True if address is in same subnet, false otherwise.
|
||||
*/
|
||||
static inline bool net_ipv4_addr_mask_cmp(struct net_if *iface,
|
||||
struct in_addr *addr)
|
||||
{
|
||||
return net_if_ipv4_addr_mask_cmp(iface, addr);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -227,6 +227,29 @@ const struct in_addr *net_if_ipv4_broadcast_addr(void)
|
|||
#endif
|
||||
}
|
||||
|
||||
bool net_if_ipv4_addr_mask_cmp(struct net_if *iface,
|
||||
struct in_addr *addr)
|
||||
{
|
||||
#if defined(CONFIG_NET_IPV4)
|
||||
uint32_t subnet = ntohl(addr->s_addr[0]) &
|
||||
ntohl(iface->ipv4.netmask.s_addr[0]);
|
||||
int i;
|
||||
|
||||
for (i = 0; i < NET_IF_MAX_IPV4_ADDR; i++) {
|
||||
if (!iface->ipv4.unicast[i].is_used ||
|
||||
iface->ipv4.unicast[i].address.family != AF_INET) {
|
||||
continue;
|
||||
}
|
||||
if ((ntohl(iface->ipv4.unicast[i].address.in_addr.s_addr[0]) &
|
||||
ntohl(iface->ipv4.netmask.s_addr[0])) == subnet) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
struct in6_addr *net_if_ipv6_get_ll(struct net_if *iface,
|
||||
enum net_addr_state addr_state)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue