Skip to content

Commit

Permalink
gnrc_netif: fix timeout if gnrc_netif_ipv6_wait_for_global_address()
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed Jan 16, 2025
1 parent f4fcac2 commit c5ccefa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions sys/include/net/gnrc/netif.h
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,7 @@ static inline msg_bus_t* gnrc_netif_get_bus(gnrc_netif_t *netif,
* May be NULL, then this checks for a global address
* on *any* interface.
* @param timeout_ms Time to wait for an address to become available, in ms.
* Use `UINT32_MAX` to wait indefinitely.
*
* @return true if a global address is configured
*/
Expand Down
23 changes: 20 additions & 3 deletions sys/net/gnrc/netif/gnrc_netif.c
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,14 @@ static void _netif_bus_msg_global(gnrc_netif_t *netif, msg_t* m, bool *has_globa
}
}

static void _unset_arg(void *arg)
{
ztimer_t *timer = arg;
if (timer) {
timer->arg = NULL;
}
}

bool gnrc_netif_ipv6_wait_for_global_address(gnrc_netif_t *netif,
uint32_t timeout_ms)
{
Expand All @@ -1396,6 +1404,7 @@ bool gnrc_netif_ipv6_wait_for_global_address(gnrc_netif_t *netif,

if (netif != NULL) {
if (_has_global_addr(netif)) {
DEBUG("gnrc_netif: %u already has a global address\n", netif->pid);
return true;
}

Expand All @@ -1406,6 +1415,7 @@ bool gnrc_netif_ipv6_wait_for_global_address(gnrc_netif_t *netif,
(netif = gnrc_netif_iter(netif));
count++) {
if (_has_global_addr(netif)) {
DEBUG("gnrc_netif: %u already has a global address\n", netif->pid);
has_global = true;
}

Expand All @@ -1415,15 +1425,22 @@ bool gnrc_netif_ipv6_wait_for_global_address(gnrc_netif_t *netif,

/* wait for global address */
msg_t m;
ztimer_now_t t_stop = ztimer_now(ZTIMER_MSEC) + timeout_ms;
ztimer_t _timeout = { .callback = _unset_arg, .arg = &_timeout };
if (timeout_ms != UINT32_MAX) {
ztimer_set(ZTIMER_MSEC, &_timeout, timeout_ms);
}

while (!has_global) {
/* stop if timeout reached */
if (ztimer_now(ZTIMER_MSEC) > t_stop) {
if (!_timeout.arg) {
DEBUG_PUTS("gnrc_netif: timeout reached");
break;
}

/* waiting for any message */
if (ztimer_msg_receive_timeout(ZTIMER_MSEC, &m, timeout_ms) < 0) {
if (timeout_ms == UINT32_MAX) {
msg_receive(&m);
} else if (ztimer_msg_receive_timeout(ZTIMER_MSEC, &m, timeout_ms) < 0) {
DEBUG_PUTS("gnrc_netif: timeout waiting for prefix");
break;
}
Expand Down

0 comments on commit c5ccefa

Please sign in to comment.