diff --git a/components/esp_netif/include/esp_netif_ip_addr.h b/components/esp_netif/include/esp_netif_ip_addr.h index e4b126272..e673b399b 100644 --- a/components/esp_netif/include/esp_netif_ip_addr.h +++ b/components/esp_netif/include/esp_netif_ip_addr.h @@ -99,6 +99,24 @@ typedef struct _ip_addr { } u_addr; uint8_t type; } esp_ip_addr_t; + +typedef enum { + ESP_IP6_ADDR_IS_UNKNOWN, + ESP_IP6_ADDR_IS_GLOBAL, + ESP_IP6_ADDR_IS_LINK_LOCAL, + ESP_IP6_ADDR_IS_SITE_LOCAL, + ESP_IP6_ADDR_IS_UNIQUE_LOCAL, + ESP_IP6_ADDR_IS_IPV4_MAPPED_IPV6 +} esp_ip6_addr_type_t; + +/** + * @brief Get the IPv6 address type + * + * @param[in] ip6_addr IPv6 type + * + * @return IPv6 type in form of enum esp_ip6_addr_type_t + */ +esp_ip6_addr_type_t esp_netif_ip6_get_addr_type(esp_ip6_addr_t* ip6_addr); #ifdef __cplusplus } diff --git a/components/esp_netif/include/esp_netif_types.h b/components/esp_netif/include/esp_netif_types.h index 242ce53c3..b55d1650f 100644 --- a/components/esp_netif/include/esp_netif_types.h +++ b/components/esp_netif/include/esp_netif_types.h @@ -120,6 +120,7 @@ typedef struct { int if_index; /*!< Interface index for which the event is received (left for legacy compilation) */ esp_netif_t *esp_netif; /*!< Pointer to corresponding esp-netif object */ esp_netif_ip6_info_t ip6_info; /*!< IPv6 address of the interface */ + int ip_index; /*!< IPv6 address index */ } ip_event_got_ip6_t; /** Event structure for IP_EVENT_AP_STAIPASSIGNED event */ diff --git a/components/esp_netif/lwip/esp_netif_lwip.c b/components/esp_netif/lwip/esp_netif_lwip.c index b746a9664..83492e6d7 100644 --- a/components/esp_netif/lwip/esp_netif_lwip.c +++ b/components/esp_netif/lwip/esp_netif_lwip.c @@ -1370,7 +1370,26 @@ esp_err_t esp_netif_get_dns_info(esp_netif_t *esp_netif, esp_netif_dns_type_t ty return esp_netif_lwip_ipc_call(esp_netif_get_dns_info_api, esp_netif, (void *)&dns_param); } -static void esp_netif_nd6_cb(struct netif *p_netif, uint8_t ip_idex) +esp_ip6_addr_type_t esp_netif_ip6_get_addr_type(esp_ip6_addr_t* ip6_addr) +{ + ip6_addr_t* lwip_ip6_info = (ip6_addr_t*)ip6_addr; + + if (ip6_addr_isglobal(lwip_ip6_info)) { + return ESP_IP6_ADDR_IS_GLOBAL; + } else if (ip6_addr_islinklocal(lwip_ip6_info)) { + return ESP_IP6_ADDR_IS_LINK_LOCAL; + } else if (ip6_addr_issitelocal(lwip_ip6_info)) { + return ESP_IP6_ADDR_IS_SITE_LOCAL; + } else if (ip6_addr_isuniquelocal(lwip_ip6_info)) { + return ESP_IP6_ADDR_IS_UNIQUE_LOCAL; + } else if (ip6_addr_isipv4mappedipv6(lwip_ip6_info)) { + return ESP_IP6_ADDR_IS_IPV4_MAPPED_IPV6; + } + return ESP_IP6_ADDR_IS_UNKNOWN; + +} + +static void esp_netif_nd6_cb(struct netif *p_netif, uint8_t ip_index) { ESP_LOGD(TAG, "%s lwip-netif:%p", __func__, p_netif); if (!p_netif) { @@ -1381,9 +1400,9 @@ static void esp_netif_nd6_cb(struct netif *p_netif, uint8_t ip_idex) esp_netif_ip6_info_t ip6_info; ip6_addr_t lwip_ip6_info; //notify event - ip_event_got_ip6_t evt = { .esp_netif = p_netif->state, .if_index = -1 }; + ip_event_got_ip6_t evt = { .esp_netif = p_netif->state, .if_index = -1, .ip_index = ip_index }; - ip6_addr_set(&lwip_ip6_info, ip_2_ip6(&p_netif->ip6_addr[ip_idex])); + ip6_addr_set(&lwip_ip6_info, ip_2_ip6(&p_netif->ip6_addr[ip_index])); #if LWIP_IPV6_SCOPES memcpy(&ip6_info.ip, &lwip_ip6_info, sizeof(esp_ip6_addr_t)); #else diff --git a/examples/common_components/protocol_examples_common/connect.c b/examples/common_components/protocol_examples_common/connect.c index 9713edda2..de7e98208 100644 --- a/examples/common_components/protocol_examples_common/connect.c +++ b/examples/common_components/protocol_examples_common/connect.c @@ -73,6 +73,8 @@ static void on_got_ipv6(void *arg, esp_event_base_t event_base, ESP_LOGI(TAG, "Got IPv6 event!"); memcpy(&s_ipv6_addr, &event->ip6_info.ip, sizeof(s_ipv6_addr)); xEventGroupSetBits(s_connect_event_group, GOT_IPV6_BIT); + ESP_LOGI(TAG, "IPv6 address: " IPV6STR ", index: %d, type: %d", IPV62STR(s_ipv6_addr), event->ip_index, esp_ip6_get_addr_type(&s_ipv6_addr)); + } #endif // CONFIG_EXAMPLE_CONNECT_IPV6