From 74236f0b295c060e9e95368ce16dac13e8e56247 Mon Sep 17 00:00:00 2001 From: Xue Yun Fei Date: Thu, 12 Nov 2020 19:41:16 +0800 Subject: [PATCH] bugfix for ipv6_address_value_issue Closes https://github.com/espressif/esp-idf/issues/5663 --- .../protocols/slip/slip_udp/main/slip_client_main.c | 8 ++++---- .../protocols/sockets/tcp_server/main/tcp_server.c | 10 +++++----- .../protocols/sockets/udp_client/main/udp_client.c | 2 +- .../udp_multicast/main/udp_multicast_example_main.c | 10 +++++----- .../protocols/sockets/udp_server/main/udp_server.c | 10 +++++----- 5 files changed, 20 insertions(+), 20 deletions(-) diff --git a/examples/protocols/slip/slip_udp/main/slip_client_main.c b/examples/protocols/slip/slip_udp/main/slip_client_main.c index be169a63a6..8bb131f986 100644 --- a/examples/protocols/slip/slip_udp/main/slip_client_main.c +++ b/examples/protocols/slip/slip_udp/main/slip_client_main.c @@ -32,7 +32,7 @@ static void udp_rx_tx_task(void *arg) int sock = (int)arg; - struct sockaddr_in6 source_addr; + struct sockaddr_storage source_addr; socklen_t socklen = sizeof(source_addr); @@ -47,10 +47,10 @@ static void udp_rx_tx_task(void *arg) } // Parse out address to string - if (source_addr.sin6_family == PF_INET) { + if (source_addr.ss_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); + } else if (source_addr.ss_family == PF_INET6) { + inet6_ntoa_r(((struct sockaddr_in6 *)&source_addr)->sin6_addr, addr_str, sizeof(addr_str) - 1); } // Force null termination of received data and print diff --git a/examples/protocols/sockets/tcp_server/main/tcp_server.c b/examples/protocols/sockets/tcp_server/main/tcp_server.c index b3a04bd6e7..96385cdd12 100644 --- a/examples/protocols/sockets/tcp_server/main/tcp_server.c +++ b/examples/protocols/sockets/tcp_server/main/tcp_server.c @@ -111,7 +111,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) { @@ -120,10 +120,10 @@ static void tcp_server_task(void *pvParameters) } // Convert ip address to 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); } ESP_LOGI(TAG, "Socket accepted ip address: %s", addr_str); diff --git a/examples/protocols/sockets/udp_client/main/udp_client.c b/examples/protocols/sockets/udp_client/main/udp_client.c index 08a15cfab2..f175fec469 100644 --- a/examples/protocols/sockets/udp_client/main/udp_client.c +++ b/examples/protocols/sockets/udp_client/main/udp_client.c @@ -84,7 +84,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 6215e73270..2eed56f510 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 86a283d402..26ec46bc5f 100644 --- a/examples/protocols/sockets/udp_server/main/udp_server.c +++ b/examples/protocols/sockets/udp_server/main/udp_server.c @@ -76,7 +76,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); @@ -88,10 +88,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...