Skip to content

Commit

Permalink
net: ip: Add iface IPv4/IPv6 multicast foreach handlers
Browse files Browse the repository at this point in the history
Add helper functions to iterate IPv4/IPv6 multicast addresses.

Signed-off-by: Pieter De Gendt <pieter.degendt@basalte.be>
  • Loading branch information
pdgendt authored and dleach02 committed Feb 28, 2024
1 parent 7c80746 commit d9d710e
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
34 changes: 34 additions & 0 deletions include/zephyr/net/net_if.h
Original file line number Diff line number Diff line change
Expand Up @@ -1412,6 +1412,29 @@ struct net_if_mcast_addr *net_if_ipv6_maddr_add(struct net_if *iface,
*/
bool net_if_ipv6_maddr_rm(struct net_if *iface, const struct in6_addr *addr);

/**
* @typedef net_if_ip_maddr_cb_t
* @brief Callback used while iterating over network interface multicast IP addresses
*
* @param iface Pointer to the network interface the address belongs to
* @param maddr Pointer to current multicast IP address
* @param user_data A valid pointer to user data or NULL
*/
typedef void (*net_if_ip_maddr_cb_t)(struct net_if *iface,
struct net_if_mcast_addr *maddr,
void *user_data);

/**
* @brief Go through all IPv6 multicast addresses on a network interface and call
* callback for each used address.
*
* @param iface Pointer to the network interface
* @param cb User-supplied callback function to call
* @param user_data User specified data
*/
void net_if_ipv6_maddr_foreach(struct net_if *iface, net_if_ip_maddr_cb_t cb,
void *user_data);

/**
* @brief Check if this IPv6 multicast address belongs to a specific interface
* or one of the interfaces.
Expand Down Expand Up @@ -2099,6 +2122,17 @@ struct net_if_mcast_addr *net_if_ipv4_maddr_add(struct net_if *iface,
*/
bool net_if_ipv4_maddr_rm(struct net_if *iface, const struct in_addr *addr);

/**
* @brief Go through all IPv4 multicast addresses on a network interface and call
* callback for each used address.
*
* @param iface Pointer to the network interface
* @param cb User-supplied callback function to call
* @param user_data User specified data
*/
void net_if_ipv4_maddr_foreach(struct net_if *iface, net_if_ip_maddr_cb_t cb,
void *user_data);

/**
* @brief Check if this IPv4 multicast address belongs to a specific interface
* or one of the interfaces.
Expand Down
54 changes: 54 additions & 0 deletions subsys/net/ip/net_if.c
Original file line number Diff line number Diff line change
Expand Up @@ -2123,6 +2123,33 @@ bool net_if_ipv6_maddr_rm(struct net_if *iface, const struct in6_addr *addr)
return ret;
}

void net_if_ipv6_maddr_foreach(struct net_if *iface, net_if_ip_maddr_cb_t cb,
void *user_data)
{
struct net_if_ipv6 *ipv6;

NET_ASSERT(iface);
NET_ASSERT(cb);

net_if_lock(iface);

ipv6 = iface->config.ip.ipv6;
if (!ipv6) {
goto out;
}

for (int i = 0; i < NET_IF_MAX_IPV6_MADDR; i++) {
if (!ipv6->mcast[i].is_used) {
continue;
}

cb(iface, &ipv6->mcast[i], user_data);
}

out:
net_if_unlock(iface);
}

struct net_if_mcast_addr *net_if_ipv6_maddr_lookup(const struct in6_addr *maddr,
struct net_if **ret)
{
Expand Down Expand Up @@ -4136,6 +4163,33 @@ bool net_if_ipv4_maddr_rm(struct net_if *iface, const struct in_addr *addr)
return ret;
}

void net_if_ipv4_maddr_foreach(struct net_if *iface, net_if_ip_maddr_cb_t cb,
void *user_data)
{
struct net_if_ipv4 *ipv4;

NET_ASSERT(iface);
NET_ASSERT(cb);

net_if_lock(iface);

ipv4 = iface->config.ip.ipv4;
if (!ipv4) {
goto out;
}

for (int i = 0; i < NET_IF_MAX_IPV4_MADDR; i++) {
if (!ipv4->mcast[i].is_used) {
continue;
}

cb(iface, &ipv4->mcast[i], user_data);
}

out:
net_if_unlock(iface);
}

struct net_if_mcast_addr *net_if_ipv4_maddr_lookup(const struct in_addr *maddr,
struct net_if **ret)
{
Expand Down

0 comments on commit d9d710e

Please sign in to comment.