Skip to content

Commit

Permalink
Merge branch 'feat/add_get_all_preferred_ip6_addr' into 'master'
Browse files Browse the repository at this point in the history
feat(esp_netif): add an API to get all preferred ip6 addresses

See merge request espressif/esp-idf!32417
  • Loading branch information
zwx1995esp committed Aug 27, 2024
2 parents bd95e69 + 47f7306 commit 997512a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
11 changes: 11 additions & 0 deletions components/esp_netif/include/esp_netif.h
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,17 @@ esp_err_t esp_netif_get_ip6_global(esp_netif_t *esp_netif, esp_ip6_addr_t *if_ip
*/
int esp_netif_get_all_ip6(esp_netif_t *esp_netif, esp_ip6_addr_t if_ip6[]);

/**
* @brief Get all preferred IPv6 addresses of the specified interface
*
* @param[in] esp_netif Handle to esp-netif instance
* @param[out] if_ip6 Array of IPv6 addresses will be copied to the argument
*
* @return
* number of returned IPv6 addresses
*/
int esp_netif_get_all_preferred_ip6(esp_netif_t *esp_netif, esp_ip6_addr_t if_ip6[]);

/**
* @brief Cause the TCP/IP stack to add an IPv6 address to the interface
*
Expand Down
24 changes: 24 additions & 0 deletions components/esp_netif/lwip/esp_netif_lwip.c
Original file line number Diff line number Diff line change
Expand Up @@ -2272,6 +2272,30 @@ int esp_netif_get_all_ip6(esp_netif_t *esp_netif, esp_ip6_addr_t if_ip6[])
}
return addr_count;
}

int esp_netif_get_all_preferred_ip6(esp_netif_t *esp_netif, esp_ip6_addr_t if_ip6[])
{
ESP_LOGV(TAG, "%s esp-netif:%p", __func__, esp_netif);

if (esp_netif == NULL || if_ip6 == NULL) {
return 0;
}

int addr_count = 0;
struct netif *p_netif = esp_netif->lwip_netif;

if (p_netif != NULL && netif_is_up(p_netif)) {
for (int i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
// Only return the IPs which are:
// 1. the state is preferred
// 2. not the IP6_ADDR_ANY(all bits are `0`)
if (ip6_addr_ispreferred(netif_ip6_addr_state(p_netif, i)) && !ip_addr_cmp(&p_netif->ip6_addr[i], IP6_ADDR_ANY)) {
memcpy(&if_ip6[addr_count++], &p_netif->ip6_addr[i], sizeof(ip6_addr_t));
}
}
}
return addr_count;
}
#endif

esp_netif_flags_t esp_netif_get_flags(esp_netif_t *esp_netif)
Expand Down

0 comments on commit 997512a

Please sign in to comment.