Skip to content

Commit

Permalink
refactor: Extract duplicate code to method
Browse files Browse the repository at this point in the history
  • Loading branch information
xnumad committed Sep 20, 2024
1 parent b93611b commit 2a9d62b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 32 deletions.
48 changes: 27 additions & 21 deletions sys/net/gnrc/network_layer/ipv6/nib/_nib-slaac.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,32 @@ int32_t _generate_temporary_addr(gnrc_netif_t *netif, const ipv6_addr_t *pfx,
return 0;
}

int _regen_temp_addr(gnrc_netif_t *netif, const ipv6_addr_t *addr, uint8_t retries,
const char *caller_description) {
//find associated prefix
uint32_t slaac_prefix_pref_until;
if (!_get_slaac_prefix_pref_until(netif, addr, &slaac_prefix_pref_until)) {
// at least one match is expected,
LOG_WARNING("nib: The temporary address smh outlived the SLAAC prefix valid lft.");
assert(false);
return -1;
}

uint32_t now = evtimer_now_msec();
assert(now < slaac_prefix_pref_until);
// else the temporary address smh outlived the SLAAC prefix preferred lft

if (_generate_temporary_addr(netif, addr,
slaac_prefix_pref_until - now,
retries,
NULL) < 0) {
LOG_WARNING("nib: Temporary address regeneration failed%s.\n", caller_description);
return -1;
}

return 0;
}

bool is_temporary_addr(const gnrc_netif_t *netif, const ipv6_addr_t *addr) {
return gnrc_ipv6_nib_pl_has_prefix(netif->pid, addr, IPV6_ADDR_BIT_LEN);
}
Expand Down Expand Up @@ -327,27 +353,7 @@ void _remove_tentative_addr(gnrc_netif_t *netif, const ipv6_addr_t *addr)
retries++;
DEBUG("Trying regeneration of temporary address. (%u/%u)\n", retries, TEMP_IDGEN_RETRIES);

//find associated prefix
uint32_t slaac_prefix_pref_until;
if (!_get_slaac_prefix_pref_until(netif, addr, &slaac_prefix_pref_until)) {
// at least one match is expected,
LOG_WARNING("nib: The temporary address smh outlived the SLAAC prefix valid lft.");
assert(false);
return;
}

uint32_t now = evtimer_now_msec();
assert(now < slaac_prefix_pref_until);
// else the temporary address smh outlived the SLAAC prefix preferred lft

if (_generate_temporary_addr(netif, addr,
slaac_prefix_pref_until - now,
retries,
NULL) < 0) {
LOG_WARNING("nib: Temporary address regeneration failed after DAD failure.\n");
return;
}

_regen_temp_addr(netif, addr, retries, " after DAD failure");
return;
}
#endif
Expand Down
11 changes: 11 additions & 0 deletions sys/net/gnrc/network_layer/ipv6/nib/_nib-slaac.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,17 @@ int32_t _generate_temporary_addr(gnrc_netif_t *netif, const ipv6_addr_t *pfx,
const uint8_t retries,
int *idx);

/**
* @brief Regenerate a temporary address
* @param netif Network interface of the temporary address and the SLAAC prefix.
* @param addr Current temporary address (or any address in the SLAAC prefix for that matter)
* @param retries passed as-is to @ref _generate_temporary_addr
* @param caller_description Text detail for logging
* @return -1 on failure, 0 on success
*/
int _regen_temp_addr(gnrc_netif_t *netif, const ipv6_addr_t *addr, uint8_t retries,
const char *caller_description);

/**
* @brief Check if the address is a temporary address.
* (Assuming the provided address is a configured address.
Expand Down
12 changes: 1 addition & 11 deletions sys/net/gnrc/network_layer/ipv6/nib/nib.c
Original file line number Diff line number Diff line change
Expand Up @@ -1460,17 +1460,7 @@ static void _handle_regen_temp_addr(_nib_offl_entry_t *pfx) {
gnrc_netif_t *netif = gnrc_netif_get_by_pid(_nib_onl_get_if(pfx->next_hop));
assert(netif != NULL);

uint32_t slaac_prefix_pref_until;
if (!_get_slaac_prefix_pref_until(netif, &pfx->pfx, &slaac_prefix_pref_until)) {
return;
}
assert(evtimer_now_msec() < slaac_prefix_pref_until); /*SLAAC prefix still preferred*/
if (_generate_temporary_addr(netif, &pfx->pfx,
slaac_prefix_pref_until - evtimer_now_msec(), /*must be that of SLAAC prefix*/
0, NULL) < 0) {
LOG_WARNING("nib: Temporary address regeneration failed.\n");
return;
}
_regen_temp_addr(netif, &pfx->pfx, 0, "");
}
#endif

Expand Down

0 comments on commit 2a9d62b

Please sign in to comment.