From df23bbe8e17db38b540f5df5028b954eb77d1464 Mon Sep 17 00:00:00 2001 From: xueyunfei Date: Fri, 20 Nov 2020 16:08:03 +0800 Subject: [PATCH] backport bugfix lwip for v4.0 --- components/tcp_transport/transport_tcp.c | 22 ++++++++++--------- components/tcpip_adapter/tcpip_adapter_lwip.c | 3 +++ .../sockets/tcp_server/main/tcp_server.c | 10 ++++----- .../sockets/udp_client/main/udp_client.c | 2 +- .../main/udp_multicast_example_main.c | 10 ++++----- .../sockets/udp_server/main/udp_server.c | 10 ++++----- 6 files changed, 31 insertions(+), 26 deletions(-) diff --git a/components/tcp_transport/transport_tcp.c b/components/tcp_transport/transport_tcp.c index 9ccc79c5f8..eef6f96da9 100644 --- a/components/tcp_transport/transport_tcp.c +++ b/components/tcp_transport/transport_tcp.c @@ -32,20 +32,22 @@ typedef struct { int sock; } transport_tcp_t; -static int resolve_dns(const char *host, struct sockaddr_in *ip) { +static int resolve_dns(const char *host, struct sockaddr_in *ip) +{ + const struct addrinfo hints = { + .ai_family = AF_INET, + .ai_socktype = SOCK_STREAM, + }; + struct addrinfo *res; - struct hostent *he; - struct in_addr **addr_list; - he = gethostbyname(host); - if (he == NULL) { - return ESP_FAIL; - } - addr_list = (struct in_addr **)he->h_addr_list; - if (addr_list[0] == NULL) { + int err = getaddrinfo(host, NULL, &hints, &res); + if(err != 0 || res == NULL) { + ESP_LOGE(TAG, "DNS lookup failed err=%d res=%p", err, res); return ESP_FAIL; } ip->sin_family = AF_INET; - memcpy(&ip->sin_addr, addr_list[0], sizeof(ip->sin_addr)); + memcpy(&ip->sin_addr, &((struct sockaddr_in *)(res->ai_addr))->sin_addr, sizeof(ip->sin_addr)); + freeaddrinfo(res); return ESP_OK; } diff --git a/components/tcpip_adapter/tcpip_adapter_lwip.c b/components/tcpip_adapter/tcpip_adapter_lwip.c index 3eb02329db..e00cf47ff4 100644 --- a/components/tcpip_adapter/tcpip_adapter_lwip.c +++ b/components/tcpip_adapter/tcpip_adapter_lwip.c @@ -360,6 +360,9 @@ esp_err_t tcpip_adapter_down(tcpip_adapter_if_t tcpip_if) for(int8_t i = 0 ;i < LWIP_IPV6_NUM_ADDRESSES ;i++) { netif_ip6_addr_set(esp_netif[tcpip_if], i, IP6_ADDR_ANY6); + netif_ip6_addr_set_valid_life(esp_netif[tcpip_if], i, 0); + netif_ip6_addr_set_pref_life(esp_netif[tcpip_if], i, 0); + netif_ip6_addr_set_state(esp_netif[tcpip_if], i, IP6_ADDR_INVALID); } netif_set_addr(esp_netif[tcpip_if], IP4_ADDR_ANY4, IP4_ADDR_ANY4, IP4_ADDR_ANY4); netif_set_down(esp_netif[tcpip_if]); diff --git a/examples/protocols/sockets/tcp_server/main/tcp_server.c b/examples/protocols/sockets/tcp_server/main/tcp_server.c index 1e71096dce..0982ee0994 100644 --- a/examples/protocols/sockets/tcp_server/main/tcp_server.c +++ b/examples/protocols/sockets/tcp_server/main/tcp_server.c @@ -77,7 +77,7 @@ static void tcp_server_task(void *pvParameters) } ESP_LOGI(TAG, "Socket listening"); - struct sockaddr_in6 source_addr; // Large enough for both IPv4 or IPv6 + struct sockaddr_storage source_addr; // Large enough for both IPv4 or IPv6 uint addr_len = sizeof(source_addr); int sock = accept(listen_sock, (struct sockaddr *)&source_addr, &addr_len); if (sock < 0) { @@ -101,10 +101,10 @@ static void tcp_server_task(void *pvParameters) // Data received else { // Get the sender's ip address as string - if (source_addr.sin6_family == PF_INET) { - inet_ntoa_r(((struct sockaddr_in *)&source_addr)->sin_addr.s_addr, addr_str, sizeof(addr_str) - 1); - } else if (source_addr.sin6_family == PF_INET6) { - inet6_ntoa_r(source_addr.sin6_addr, addr_str, sizeof(addr_str) - 1); + 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) { + inet6_ntoa_r(((struct sockaddr_in6 *)&source_addr)->sin6_addr, addr_str, sizeof(addr_str) - 1); } rx_buffer[len] = 0; // Null-terminate whatever we received and treat like a string diff --git a/examples/protocols/sockets/udp_client/main/udp_client.c b/examples/protocols/sockets/udp_client/main/udp_client.c index dfa471231a..1e78cde4d2 100644 --- a/examples/protocols/sockets/udp_client/main/udp_client.c +++ b/examples/protocols/sockets/udp_client/main/udp_client.c @@ -88,7 +88,7 @@ static void udp_client_task(void *pvParameters) } ESP_LOGI(TAG, "Message sent"); - struct sockaddr_in source_addr; // Large enough for both IPv4 or IPv6 + struct sockaddr_storage source_addr; // Large enough for both IPv4 or IPv6 socklen_t socklen = sizeof(source_addr); int len = recvfrom(sock, rx_buffer, sizeof(rx_buffer) - 1, 0, (struct sockaddr *)&source_addr, &socklen); diff --git a/examples/protocols/sockets/udp_multicast/main/udp_multicast_example_main.c b/examples/protocols/sockets/udp_multicast/main/udp_multicast_example_main.c index bd2a4cbedf..8db8764b10 100644 --- a/examples/protocols/sockets/udp_multicast/main/udp_multicast_example_main.c +++ b/examples/protocols/sockets/udp_multicast/main/udp_multicast_example_main.c @@ -364,7 +364,7 @@ static void mcast_example_task(void *pvParameters) char recvbuf[48]; char raddr_name[32] = { 0 }; - struct sockaddr_in6 raddr; // Large enough for both IPv4 or IPv6 + struct sockaddr_storage raddr; // Large enough for both IPv4 or IPv6 socklen_t socklen = sizeof(raddr); int len = recvfrom(sock, recvbuf, sizeof(recvbuf)-1, 0, (struct sockaddr *)&raddr, &socklen); @@ -376,14 +376,14 @@ static void mcast_example_task(void *pvParameters) // Get the sender's address as a string #ifdef CONFIG_EXAMPLE_IPV4 - if (raddr.sin6_family == PF_INET) { - inet_ntoa_r(((struct sockaddr_in *)&raddr)->sin_addr.s_addr, + if (raddr.ss_family == PF_INET) { + inet_ntoa_r(((struct sockaddr_in *)&raddr)->sin_addr, raddr_name, sizeof(raddr_name)-1); } #endif #ifdef CONFIG_EXAMPLE_IPV6 - if (raddr.sin6_family == PF_INET6) { - inet6_ntoa_r(raddr.sin6_addr, raddr_name, sizeof(raddr_name)-1); + if (raddr.ss_family== PF_INET6) { + inet6_ntoa_r(((struct sockaddr_in6 *)&raddr)->sin6_addr, raddr_name, sizeof(raddr_name)-1); } #endif ESP_LOGI(TAG, "received %d bytes from %s:", len, raddr_name); diff --git a/examples/protocols/sockets/udp_server/main/udp_server.c b/examples/protocols/sockets/udp_server/main/udp_server.c index ce82168323..fbc81950fe 100644 --- a/examples/protocols/sockets/udp_server/main/udp_server.c +++ b/examples/protocols/sockets/udp_server/main/udp_server.c @@ -70,7 +70,7 @@ static void udp_server_task(void *pvParameters) while (1) { ESP_LOGI(TAG, "Waiting for data"); - struct sockaddr_in6 source_addr; // Large enough for both IPv4 or IPv6 + struct sockaddr_storage source_addr; // Large enough for both IPv4 or IPv6 socklen_t socklen = sizeof(source_addr); int len = recvfrom(sock, rx_buffer, sizeof(rx_buffer) - 1, 0, (struct sockaddr *)&source_addr, &socklen); @@ -82,10 +82,10 @@ static void udp_server_task(void *pvParameters) // Data received else { // Get the sender's ip address as string - if (source_addr.sin6_family == PF_INET) { - inet_ntoa_r(((struct sockaddr_in *)&source_addr)->sin_addr.s_addr, addr_str, sizeof(addr_str) - 1); - } else if (source_addr.sin6_family == PF_INET6) { - inet6_ntoa_r(source_addr.sin6_addr, addr_str, sizeof(addr_str) - 1); + 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) { + inet6_ntoa_r(((struct sockaddr_in6 *)&source_addr)->sin6_addr, addr_str, sizeof(addr_str) - 1); } rx_buffer[len] = 0; // Null-terminate whatever we received and treat like a string...