mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-03 12:44:33 +02:00
common_connect: add support for getting multiple IPv6 addresses
This commit is contained in:
@@ -175,9 +175,44 @@ menu "Example Connection Configuration"
|
|||||||
endif
|
endif
|
||||||
|
|
||||||
config EXAMPLE_CONNECT_IPV6
|
config EXAMPLE_CONNECT_IPV6
|
||||||
bool "Obtain IPv6 link-local address"
|
bool "Obtain IPv6 address"
|
||||||
default y
|
default y
|
||||||
help
|
help
|
||||||
By default, examples will wait until IPv4 and IPv6 addresses are obtained.
|
By default, examples will wait until IPv4 and IPv6 local link addresses are obtained.
|
||||||
Disable this option if the network does not support IPv6.
|
Disable this option if the network does not support IPv6.
|
||||||
|
Choose the preferred IPv6 address type if the connection code should wait until other than
|
||||||
|
the local link address gets assigned.
|
||||||
|
|
||||||
|
if EXAMPLE_CONNECT_IPV6
|
||||||
|
choice EXAMPLE_CONNECT_PREFERRED_IPV6
|
||||||
|
prompt "Preferred IPv6 Type"
|
||||||
|
default EXAMPLE_CONNECT_IPV6_PREF_LOCAL_LINK
|
||||||
|
help
|
||||||
|
Select which kind of IPv6 address the connect logic waits for.
|
||||||
|
|
||||||
|
config EXAMPLE_CONNECT_IPV6_PREF_LOCAL_LINK
|
||||||
|
bool "Local Link Address"
|
||||||
|
help
|
||||||
|
Blocks until Local link address assigned.
|
||||||
|
|
||||||
|
config EXAMPLE_CONNECT_IPV6_PREF_GLOBAL
|
||||||
|
bool "Global Address"
|
||||||
|
help
|
||||||
|
Blocks until Global address assigned.
|
||||||
|
|
||||||
|
config EXAMPLE_CONNECT_IPV6_PREF_SITE_LOCAL
|
||||||
|
bool "Site Local Address"
|
||||||
|
help
|
||||||
|
Blocks until Site link address assigned.
|
||||||
|
|
||||||
|
config EXAMPLE_CONNECT_IPV6_PREF_UNIQUE_LOCAL
|
||||||
|
bool "Unique Local Link Address"
|
||||||
|
help
|
||||||
|
Blocks until Unique local address assigned.
|
||||||
|
|
||||||
|
endchoice
|
||||||
|
|
||||||
|
endif
|
||||||
|
|
||||||
|
|
||||||
endmenu
|
endmenu
|
||||||
|
@@ -30,6 +30,17 @@
|
|||||||
|
|
||||||
#ifdef CONFIG_EXAMPLE_CONNECT_IPV6
|
#ifdef CONFIG_EXAMPLE_CONNECT_IPV6
|
||||||
#define CONNECTED_BITS (GOT_IPV4_BIT | GOT_IPV6_BIT)
|
#define CONNECTED_BITS (GOT_IPV4_BIT | GOT_IPV6_BIT)
|
||||||
|
|
||||||
|
#if defined(CONFIG_EXAMPLE_CONNECT_IPV6_PREF_LOCAL_LINK)
|
||||||
|
#define EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_LINK_LOCAL
|
||||||
|
#elif defined(CONFIG_EXAMPLE_CONNECT_IPV6_PREF_GLOBAL)
|
||||||
|
#define EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_GLOBAL
|
||||||
|
#elif defined(CONFIG_EXAMPLE_CONNECT_IPV6_PREF_SITE_LOCAL)
|
||||||
|
#define EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_SITE_LOCAL
|
||||||
|
#elif defined(CONFIG_EXAMPLE_CONNECT_IPV6_PREF_UNIQUE_LOCAL)
|
||||||
|
#define EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_UNIQUE_LOCAL
|
||||||
|
#endif // if-elif CONFIG_EXAMPLE_CONNECT_IPV6_PREF_...
|
||||||
|
|
||||||
#else
|
#else
|
||||||
#define CONNECTED_BITS (GOT_IPV4_BIT)
|
#define CONNECTED_BITS (GOT_IPV4_BIT)
|
||||||
#endif
|
#endif
|
||||||
@@ -41,6 +52,16 @@ static esp_netif_t *s_example_esp_netif = NULL;
|
|||||||
|
|
||||||
#ifdef CONFIG_EXAMPLE_CONNECT_IPV6
|
#ifdef CONFIG_EXAMPLE_CONNECT_IPV6
|
||||||
static esp_ip6_addr_t s_ipv6_addr;
|
static esp_ip6_addr_t s_ipv6_addr;
|
||||||
|
|
||||||
|
/* types of ipv6 addresses to be displayed on ipv6 events */
|
||||||
|
static const char *s_ipv6_addr_types[] = {
|
||||||
|
"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"
|
||||||
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static const char *TAG = "example_connect";
|
static const char *TAG = "example_connect";
|
||||||
@@ -70,11 +91,12 @@ static void on_got_ipv6(void *arg, esp_event_base_t event_base,
|
|||||||
ESP_LOGD(TAG, "Got IPv6 from another netif: ignored");
|
ESP_LOGD(TAG, "Got IPv6 from another netif: ignored");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ESP_LOGI(TAG, "Got IPv6 event!");
|
esp_ip6_addr_type_t ipv6_type = esp_netif_ip6_get_addr_type(&event->ip6_info.ip);
|
||||||
memcpy(&s_ipv6_addr, &event->ip6_info.ip, sizeof(s_ipv6_addr));
|
ESP_LOGI(TAG, "Got IPv6 address: " IPV6STR ", type: %s", IPV62STR(event->ip6_info.ip), s_ipv6_addr_types[ipv6_type]);
|
||||||
xEventGroupSetBits(s_connect_event_group, GOT_IPV6_BIT);
|
if (ipv6_type == EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE) {
|
||||||
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));
|
memcpy(&s_ipv6_addr, &event->ip6_info.ip, sizeof(s_ipv6_addr));
|
||||||
|
xEventGroupSetBits(s_connect_event_group, GOT_IPV6_BIT);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // CONFIG_EXAMPLE_CONNECT_IPV6
|
#endif // CONFIG_EXAMPLE_CONNECT_IPV6
|
||||||
|
@@ -71,6 +71,26 @@ IP_VERSION = 'IPv4'
|
|||||||
IPV4 = '192.168.0.167'
|
IPV4 = '192.168.0.167'
|
||||||
IPV6 = 'FE80::32AE:A4FF:FE80:5288'
|
IPV6 = 'FE80::32AE:A4FF:FE80:5288'
|
||||||
```
|
```
|
||||||
|
### Note about IPv6 addresses
|
||||||
|
|
||||||
|
Examples are configured to obtain multiple IPv6 addresses. The actual behavior may differ depending on the local network, typically the ESP gets assigned these two addresses
|
||||||
|
|
||||||
|
* Local Link address
|
||||||
|
|
||||||
|
* Unique Local address
|
||||||
|
|
||||||
|
The value and type of the IPv6 address is displayed in the terminal, for example:
|
||||||
|
|
||||||
|
Please make sure that when using the Local Link address, an interface id is included in the configuration:
|
||||||
|
|
||||||
|
* In the embedded code
|
||||||
|
```
|
||||||
|
dest_addr.sin6_scope_id = esp_netif_get_netif_impl_index(esp_netif_instance);
|
||||||
|
```
|
||||||
|
* On the host
|
||||||
|
|
||||||
|
- Interface name suffix is present when passing the address as a string, for example `fe80::260a:XXX:XXX:XXX%en0`
|
||||||
|
- The interface id is present when passing the endpoint as tupple, for example `socket.connect(('fd00::260a:XXXX:XXXX:XXXX', 3333, 0, 3))`
|
||||||
|
|
||||||
## Hardware Required
|
## Hardware Required
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user