From b06a588263915cfe8aad3f5df6bcc1ab04239afd Mon Sep 17 00:00:00 2001 From: yuanjm Date: Tue, 26 Jan 2021 16:17:56 +0800 Subject: [PATCH] examples: Use CONFIG_LWIP_IPV6 to strip tcp_server example --- .../sockets/tcp_server/CMakeLists.txt | 1 + .../protocols/sockets/tcp_server/Makefile | 1 + .../sockets/tcp_server/main/tcp_server.c | 23 ++++++++++++------- .../sockets/tcp_server/sdkconfig.ci.1 | 4 ++++ 4 files changed, 21 insertions(+), 8 deletions(-) create mode 100644 examples/protocols/sockets/tcp_server/sdkconfig.ci.1 diff --git a/examples/protocols/sockets/tcp_server/CMakeLists.txt b/examples/protocols/sockets/tcp_server/CMakeLists.txt index 9865464bd8..5761720556 100644 --- a/examples/protocols/sockets/tcp_server/CMakeLists.txt +++ b/examples/protocols/sockets/tcp_server/CMakeLists.txt @@ -5,6 +5,7 @@ cmake_minimum_required(VERSION 3.5) # (Not part of the boilerplate) # 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(EXCLUDE_COMPONENTS "coap" "asio") include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(tcp_server) diff --git a/examples/protocols/sockets/tcp_server/Makefile b/examples/protocols/sockets/tcp_server/Makefile index 318e78a775..344b8aa424 100644 --- a/examples/protocols/sockets/tcp_server/Makefile +++ b/examples/protocols/sockets/tcp_server/Makefile @@ -6,5 +6,6 @@ PROJECT_NAME := tcp_server EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common +EXCLUDE_COMPONENTS = coap asio include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/sockets/tcp_server/main/tcp_server.c b/examples/protocols/sockets/tcp_server/main/tcp_server.c index 434e46a228..1d57c751e4 100644 --- a/examples/protocols/sockets/tcp_server/main/tcp_server.c +++ b/examples/protocols/sockets/tcp_server/main/tcp_server.c @@ -62,7 +62,7 @@ static void tcp_server_task(void *pvParameters) char addr_str[128]; int addr_family = (int)pvParameters; int ip_protocol = 0; - struct sockaddr_in6 dest_addr; + struct sockaddr_storage dest_addr; if (addr_family == AF_INET) { 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_port = htons(PORT); ip_protocol = IPPROTO_IP; - } else if (addr_family == AF_INET6) { - bzero(&dest_addr.sin6_addr.un, sizeof(dest_addr.sin6_addr.un)); - dest_addr.sin6_family = AF_INET6; - dest_addr.sin6_port = htons(PORT); + } +#ifdef CONFIG_EXAMPLE_IPV6 + else if (addr_family == AF_INET6) { + 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; } +#endif int listen_sock = socket(addr_family, SOCK_STREAM, ip_protocol); if (listen_sock < 0) { @@ -83,11 +87,11 @@ static void tcp_server_task(void *pvParameters) vTaskDelete(NULL); return; } + int opt = 1; + setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); #if defined(CONFIG_EXAMPLE_IPV4) && defined(CONFIG_EXAMPLE_IPV6) // 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) - int opt = 1; - setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); setsockopt(listen_sock, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof(opt)); #endif @@ -122,9 +126,12 @@ static void tcp_server_task(void *pvParameters) // Convert ip address to string if (source_addr.ss_family == PF_INET) { 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); } +#endif ESP_LOGI(TAG, "Socket accepted ip address: %s", addr_str); do_retransmit(sock); diff --git a/examples/protocols/sockets/tcp_server/sdkconfig.ci.1 b/examples/protocols/sockets/tcp_server/sdkconfig.ci.1 new file mode 100644 index 0000000000..f4d66cf5ec --- /dev/null +++ b/examples/protocols/sockets/tcp_server/sdkconfig.ci.1 @@ -0,0 +1,4 @@ +CONFIG_EXAMPLE_IPV4=y +CONFIG_EXAMPLE_IPV6=n +CONFIG_EXAMPLE_CONNECT_IPV6=n +CONFIG_LWIP_IPV6=n