mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
examples: Use CONFIG_LWIP_IPV6 to strip tcp_server example
This commit is contained in:
parent
31a20088af
commit
b06a588263
@ -5,6 +5,7 @@ cmake_minimum_required(VERSION 3.5)
|
|||||||
# (Not part of the boilerplate)
|
# (Not part of the boilerplate)
|
||||||
# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection.
|
# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection.
|
||||||
set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common)
|
set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common)
|
||||||
|
set(EXCLUDE_COMPONENTS "coap" "asio")
|
||||||
|
|
||||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||||
project(tcp_server)
|
project(tcp_server)
|
||||||
|
@ -6,5 +6,6 @@
|
|||||||
PROJECT_NAME := tcp_server
|
PROJECT_NAME := tcp_server
|
||||||
|
|
||||||
EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common
|
EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common
|
||||||
|
EXCLUDE_COMPONENTS = coap asio
|
||||||
|
|
||||||
include $(IDF_PATH)/make/project.mk
|
include $(IDF_PATH)/make/project.mk
|
||||||
|
@ -62,7 +62,7 @@ static void tcp_server_task(void *pvParameters)
|
|||||||
char addr_str[128];
|
char addr_str[128];
|
||||||
int addr_family = (int)pvParameters;
|
int addr_family = (int)pvParameters;
|
||||||
int ip_protocol = 0;
|
int ip_protocol = 0;
|
||||||
struct sockaddr_in6 dest_addr;
|
struct sockaddr_storage dest_addr;
|
||||||
|
|
||||||
if (addr_family == AF_INET) {
|
if (addr_family == AF_INET) {
|
||||||
struct sockaddr_in *dest_addr_ip4 = (struct sockaddr_in *)&dest_addr;
|
struct sockaddr_in *dest_addr_ip4 = (struct sockaddr_in *)&dest_addr;
|
||||||
@ -70,12 +70,16 @@ static void tcp_server_task(void *pvParameters)
|
|||||||
dest_addr_ip4->sin_family = AF_INET;
|
dest_addr_ip4->sin_family = AF_INET;
|
||||||
dest_addr_ip4->sin_port = htons(PORT);
|
dest_addr_ip4->sin_port = htons(PORT);
|
||||||
ip_protocol = IPPROTO_IP;
|
ip_protocol = IPPROTO_IP;
|
||||||
} else if (addr_family == AF_INET6) {
|
}
|
||||||
bzero(&dest_addr.sin6_addr.un, sizeof(dest_addr.sin6_addr.un));
|
#ifdef CONFIG_EXAMPLE_IPV6
|
||||||
dest_addr.sin6_family = AF_INET6;
|
else if (addr_family == AF_INET6) {
|
||||||
dest_addr.sin6_port = htons(PORT);
|
struct sockaddr_in6 *dest_addr_ip6 = (struct sockaddr_in6 *)&dest_addr;
|
||||||
|
bzero(&dest_addr_ip6->sin6_addr.un, sizeof(dest_addr_ip6->sin6_addr.un));
|
||||||
|
dest_addr_ip6->sin6_family = AF_INET6;
|
||||||
|
dest_addr_ip6->sin6_port = htons(PORT);
|
||||||
ip_protocol = IPPROTO_IPV6;
|
ip_protocol = IPPROTO_IPV6;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
int listen_sock = socket(addr_family, SOCK_STREAM, ip_protocol);
|
int listen_sock = socket(addr_family, SOCK_STREAM, ip_protocol);
|
||||||
if (listen_sock < 0) {
|
if (listen_sock < 0) {
|
||||||
@ -83,11 +87,11 @@ static void tcp_server_task(void *pvParameters)
|
|||||||
vTaskDelete(NULL);
|
vTaskDelete(NULL);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
int opt = 1;
|
||||||
|
setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
|
||||||
#if defined(CONFIG_EXAMPLE_IPV4) && defined(CONFIG_EXAMPLE_IPV6)
|
#if defined(CONFIG_EXAMPLE_IPV4) && defined(CONFIG_EXAMPLE_IPV6)
|
||||||
// Note that by default IPV6 binds to both protocols, it is must be disabled
|
// Note that by default IPV6 binds to both protocols, it is must be disabled
|
||||||
// if both protocols used at the same time (used in CI)
|
// if both protocols used at the same time (used in CI)
|
||||||
int opt = 1;
|
|
||||||
setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
|
|
||||||
setsockopt(listen_sock, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof(opt));
|
setsockopt(listen_sock, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof(opt));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -122,9 +126,12 @@ static void tcp_server_task(void *pvParameters)
|
|||||||
// Convert ip address to string
|
// Convert ip address to string
|
||||||
if (source_addr.ss_family == PF_INET) {
|
if (source_addr.ss_family == PF_INET) {
|
||||||
inet_ntoa_r(((struct sockaddr_in *)&source_addr)->sin_addr, addr_str, sizeof(addr_str) - 1);
|
inet_ntoa_r(((struct sockaddr_in *)&source_addr)->sin_addr, addr_str, sizeof(addr_str) - 1);
|
||||||
} else if (source_addr.ss_family == PF_INET6) {
|
}
|
||||||
|
#ifdef CONFIG_EXAMPLE_IPV6
|
||||||
|
else if (source_addr.ss_family == PF_INET6) {
|
||||||
inet6_ntoa_r(((struct sockaddr_in6 *)&source_addr)->sin6_addr, addr_str, sizeof(addr_str) - 1);
|
inet6_ntoa_r(((struct sockaddr_in6 *)&source_addr)->sin6_addr, addr_str, sizeof(addr_str) - 1);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
ESP_LOGI(TAG, "Socket accepted ip address: %s", addr_str);
|
ESP_LOGI(TAG, "Socket accepted ip address: %s", addr_str);
|
||||||
|
|
||||||
do_retransmit(sock);
|
do_retransmit(sock);
|
||||||
|
4
examples/protocols/sockets/tcp_server/sdkconfig.ci.1
Normal file
4
examples/protocols/sockets/tcp_server/sdkconfig.ci.1
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
CONFIG_EXAMPLE_IPV4=y
|
||||||
|
CONFIG_EXAMPLE_IPV6=n
|
||||||
|
CONFIG_EXAMPLE_CONNECT_IPV6=n
|
||||||
|
CONFIG_LWIP_IPV6=n
|
Loading…
x
Reference in New Issue
Block a user