Merge branch 'feat/add_get_all_preferred_ip6_addr' into 'master'

feat(esp_netif): add an API to get all preferred ip6 addresses

See merge request espressif/esp-idf!32417
This commit is contained in:
Zhang Wen Xu 2024-08-27 16:00:16 +08:00
commit 997512ace2
2 changed files with 35 additions and 0 deletions

View File

@ -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
*

View File

@ -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)