diff --git a/examples/common_components/protocol_examples_common/Kconfig.projbuild b/examples/common_components/protocol_examples_common/Kconfig.projbuild index e3275ec3f4..5e2af8b8c3 100644 --- a/examples/common_components/protocol_examples_common/Kconfig.projbuild +++ b/examples/common_components/protocol_examples_common/Kconfig.projbuild @@ -189,6 +189,7 @@ menu "Example Connection Configuration" bool "Obtain IPv6 address" default y depends on EXAMPLE_CONNECT_WIFI || EXAMPLE_CONNECT_ETHERNET + select LWIP_IPV6 help By default, examples will wait until IPv4 and IPv6 local link addresses are obtained. Disable this option if the network does not support IPv6. diff --git a/examples/common_components/protocol_examples_common/addr_from_stdin.c b/examples/common_components/protocol_examples_common/addr_from_stdin.c index 0320a27bf3..c907ffd96e 100644 --- a/examples/common_components/protocol_examples_common/addr_from_stdin.c +++ b/examples/common_components/protocol_examples_common/addr_from_stdin.c @@ -10,7 +10,7 @@ #define HOST_IP_SIZE 128 -esp_err_t get_addr_from_stdin(int port, int sock_type, int *ip_protocol, int *addr_family, struct sockaddr_in6 *dest_addr) +esp_err_t get_addr_from_stdin(int port, int sock_type, int *ip_protocol, int *addr_family, struct sockaddr_storage *dest_addr) { char host_ip[HOST_IP_SIZE]; int len; @@ -49,15 +49,18 @@ esp_err_t get_addr_from_stdin(int port, int sock_type, int *ip_protocol, int *ad freeaddrinfo( addr_list ); return ESP_OK; - } else if (cur->ai_family == AF_INET6) { + } +#if CONFIG_LWIP_IPV6 + else if (cur->ai_family == AF_INET6) { *ip_protocol = IPPROTO_IPV6; *addr_family = AF_INET6; // add port and interface number and return on first IPv6 match - dest_addr->sin6_port = htons(port); - dest_addr->sin6_scope_id = esp_netif_get_netif_impl_index(EXAMPLE_INTERFACE); + ((struct sockaddr_in6*)dest_addr)->sin6_port = htons(port); + ((struct sockaddr_in6*)dest_addr)->sin6_scope_id = esp_netif_get_netif_impl_index(EXAMPLE_INTERFACE); freeaddrinfo( addr_list ); return ESP_OK; } +#endif } // no match found freeaddrinfo( addr_list ); diff --git a/examples/common_components/protocol_examples_common/include/addr_from_stdin.h b/examples/common_components/protocol_examples_common/include/addr_from_stdin.h index 959a7ed099..9a059c149c 100644 --- a/examples/common_components/protocol_examples_common/include/addr_from_stdin.h +++ b/examples/common_components/protocol_examples_common/include/addr_from_stdin.h @@ -24,20 +24,20 @@ extern "C" { * @brief Read and evaluate IP address from stdin * * This API reads stdin and parses the input address using getaddrinfo() - * to fill in struct sockaddr_in6 (for both IPv4 and IPv6) used to open + * to fill in struct sockaddr_storage (for both IPv4 and IPv6) used to open * a socket. IP protocol is guessed from the IP address string. * * @param[in] port port number of expected connection * @param[in] sock_type expected protocol: SOCK_STREAM or SOCK_DGRAM * @param[out] ip_protocol resultant IP protocol: IPPROTO_IP or IPPROTO_IP6 * @param[out] addr_family resultant address family: AF_INET or AF_INET6 - * @param[out] dest_addr sockaddr_in6 structure (for both IPv4 and IPv6) + * @param[out] dest_addr sockaddr_storage structure (for both IPv4 and IPv6) * @return ESP_OK on success, ESP_FAIL otherwise */ esp_err_t get_addr_from_stdin(int port, int sock_type, int *ip_protocol, int *addr_family, - struct sockaddr_in6 *dest_addr); + struct sockaddr_storage *dest_addr); #ifdef __cplusplus } diff --git a/examples/protocols/sockets/tcp_client/main/tcp_client.c b/examples/protocols/sockets/tcp_client/main/tcp_client.c index 7425ba9603..a1977c49d6 100644 --- a/examples/protocols/sockets/tcp_client/main/tcp_client.c +++ b/examples/protocols/sockets/tcp_client/main/tcp_client.c @@ -60,7 +60,7 @@ static void tcp_client_task(void *pvParameters) addr_family = AF_INET6; ip_protocol = IPPROTO_IPV6; #elif defined(CONFIG_EXAMPLE_SOCKET_IP_INPUT_STDIN) - struct sockaddr_in6 dest_addr = { 0 }; + struct sockaddr_storage dest_addr = { 0 }; ESP_ERROR_CHECK(get_addr_from_stdin(PORT, SOCK_STREAM, &ip_protocol, &addr_family, &dest_addr)); #endif int sock = socket(addr_family, SOCK_STREAM, ip_protocol); diff --git a/examples/protocols/sockets/udp_client/main/udp_client.c b/examples/protocols/sockets/udp_client/main/udp_client.c index f175fec469..7d39803311 100644 --- a/examples/protocols/sockets/udp_client/main/udp_client.c +++ b/examples/protocols/sockets/udp_client/main/udp_client.c @@ -64,7 +64,7 @@ static void udp_client_task(void *pvParameters) addr_family = AF_INET6; ip_protocol = IPPROTO_IPV6; #elif defined(CONFIG_EXAMPLE_SOCKET_IP_INPUT_STDIN) - struct sockaddr_in6 dest_addr = { 0 }; + struct sockaddr_storage dest_addr = { 0 }; ESP_ERROR_CHECK(get_addr_from_stdin(PORT, SOCK_DGRAM, &ip_protocol, &addr_family, &dest_addr)); #endif