Skip to content

Commit

Permalink
🍒gnrc_netif_ipv6_addr_pfx_idx
Browse files Browse the repository at this point in the history
  • Loading branch information
xnumad committed Feb 10, 2024
1 parent d63f0e6 commit e583f0d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
22 changes: 22 additions & 0 deletions sys/include/net/gnrc/netif/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,28 @@ void gnrc_netif_ipv6_addr_remove_internal(gnrc_netif_t *netif,
int gnrc_netif_ipv6_addr_idx(gnrc_netif_t *netif,
const ipv6_addr_t *addr);

/**
* @brief Returns the index of the first pfx
* in gnrc_netif_t::ipv6_addrs of @p netif
* where the first @p pfx_len bits match with @p pfx
*
* @pre `(netif != NULL) && (pfx != NULL)`
*
* Can be used to check if an address is assigned to an interface.
*
* @param[in] netif the network interface
* @param[in] pfx the address to check
* @param[in] pfx_len the amount of bits to compare
*
* @note Only available with @ref net_gnrc_ipv6 "gnrc_ipv6".
*
* @return index of the first matching address
* in gnrc_netif_t::ipv6_addrs of @p netif
* @return -1, if no matching address found for @p netif
*/
int gnrc_netif_ipv6_addr_pfx_idx(gnrc_netif_t *netif,
const ipv6_addr_t *pfx, uint8_t pfx_len);

/**
* @brief Gets state from address flags
*
Expand Down
44 changes: 44 additions & 0 deletions sys/net/gnrc/netif/gnrc_netif.c
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,9 @@ void gnrc_netif_release(gnrc_netif_t *netif)

#if IS_USED(MODULE_GNRC_NETIF_IPV6)
static int _addr_idx(const gnrc_netif_t *netif, const ipv6_addr_t *addr);
static int _addr_pfx_idx(const gnrc_netif_t *netif,
const ipv6_addr_t *pfx,
uint8_t pfx_len);
static int _group_idx(const gnrc_netif_t *netif, const ipv6_addr_t *addr);

static char addr_str[IPV6_ADDR_MAX_STR_LEN];
Expand Down Expand Up @@ -730,6 +733,23 @@ int gnrc_netif_ipv6_addr_idx(gnrc_netif_t *netif,
return idx;
}

int gnrc_netif_ipv6_addr_pfx_idx(gnrc_netif_t *netif,
const ipv6_addr_t *pfx, uint8_t pfx_len)
{
int idx;

assert((netif != NULL) && (pfx != NULL));
DEBUG("gnrc_netif: get index of first address matching %s/%u"
" from interface %" PRIkernel_pid "\n",
ipv6_addr_to_str(addr_str, pfx, sizeof(addr_str)),
pfx_len,
netif->pid);
gnrc_netif_acquire(netif);
idx = _addr_pfx_idx(netif, pfx, pfx_len);
gnrc_netif_release(netif);
return idx;
}

int gnrc_netif_ipv6_addr_match(gnrc_netif_t *netif,
const ipv6_addr_t *addr)
{
Expand Down Expand Up @@ -947,11 +967,35 @@ static int _idx(const gnrc_netif_t *netif, const ipv6_addr_t *addr, bool mcast)
return -1;
}

static int _pfx_idx(const gnrc_netif_t *netif, const ipv6_addr_t *pfx, uint8_t pfx_len, bool mcast)
{
//same as function @ref _idx above, but with generalized condition
if (!ipv6_addr_is_unspecified(pfx)) {
const ipv6_addr_t *iplist = (mcast) ? netif->ipv6.groups :
netif->ipv6.addrs;
unsigned ipmax = (mcast) ? GNRC_NETIF_IPV6_GROUPS_NUMOF :
CONFIG_GNRC_NETIF_IPV6_ADDRS_NUMOF;
for (unsigned i = 0; i < ipmax; i++) {
if (ipv6_addr_match_prefix(&iplist[i], pfx) >= pfx_len) {
return i;
}
}
}
return -1;
}

static inline int _addr_idx(const gnrc_netif_t *netif, const ipv6_addr_t *addr)
{
return _idx(netif, addr, false);
}

static inline int _addr_pfx_idx(const gnrc_netif_t *netif,
const ipv6_addr_t *pfx,
uint8_t pfx_len)
{
return _pfx_idx(netif, pfx, pfx_len, false);
}

static inline int _group_idx(const gnrc_netif_t *netif, const ipv6_addr_t *addr)
{
return _idx(netif, addr, true);
Expand Down

0 comments on commit e583f0d

Please sign in to comment.