Browse Source

Merge branch 'bugfix/add_ttl_for_ping_socket' into 'master'

lw-ip:bugfix for add ttl for ping socket

Closes WIFI-4340

See merge request espressif/esp-idf!18376
Jiang Jiang Jian 3 years ago
parent
commit
f747ffd63a

+ 6 - 13
components/lwip/apps/ping/esp_ping.c

@@ -1,16 +1,8 @@
-// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-
-//     http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
+/*
+ * SPDX-FileCopyrightText: 2019-2021 Espressif Systems (Shanghai) CO LTD
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
 
 
 #include <string.h>
 #include <string.h>
 #include "esp_ping.h"
 #include "esp_ping.h"
@@ -25,6 +17,7 @@ typedef struct _ping_option {
     size_t ping_data_len;
     size_t ping_data_len;
     uint16_t ping_id;
     uint16_t ping_id;
     u8_t ping_tos;
     u8_t ping_tos;
+    u8_t ping_ttl;
     esp_ping_found_fn ping_res_fn;
     esp_ping_found_fn ping_res_fn;
     esp_ping_found    ping_res;
     esp_ping_found    ping_res;
     void    *ping_reserve;
     void    *ping_reserve;

+ 9 - 0
components/lwip/apps/ping/ping_sock.c

@@ -49,6 +49,7 @@ typedef struct {
     uint32_t elapsed_time_ms;
     uint32_t elapsed_time_ms;
     uint32_t total_time_ms;
     uint32_t total_time_ms;
     uint8_t ttl;
     uint8_t ttl;
+    uint8_t tos;
     uint32_t flags;
     uint32_t flags;
     void (*on_ping_success)(esp_ping_handle_t hdl, void *args);
     void (*on_ping_success)(esp_ping_handle_t hdl, void *args);
     void (*on_ping_timeout)(esp_ping_handle_t hdl, void *args);
     void (*on_ping_timeout)(esp_ping_handle_t hdl, void *args);
@@ -113,6 +114,7 @@ static int esp_ping_receive(esp_ping_t *ep)
                 if ((iecho->id == ep->packet_hdr->id) && (iecho->seqno == ep->packet_hdr->seqno)) {
                 if ((iecho->id == ep->packet_hdr->id) && (iecho->seqno == ep->packet_hdr->seqno)) {
                     ep->received++;
                     ep->received++;
                     ep->ttl = iphdr->_ttl;
                     ep->ttl = iphdr->_ttl;
+                    ep->tos = iphdr->_tos;
                     ep->recv_len = lwip_ntohs(IPH_LEN(iphdr)) - data_head;  // The data portion of ICMP
                     ep->recv_len = lwip_ntohs(IPH_LEN(iphdr)) - data_head;  // The data portion of ICMP
                     return len;
                     return len;
                 }
                 }
@@ -271,6 +273,9 @@ esp_err_t esp_ping_new_session(const esp_ping_config_t *config, const esp_ping_c
     /* set tos */
     /* set tos */
     setsockopt(ep->sock, IPPROTO_IP, IP_TOS, &config->tos, sizeof(config->tos));
     setsockopt(ep->sock, IPPROTO_IP, IP_TOS, &config->tos, sizeof(config->tos));
 
 
+    /* set ttl */
+    setsockopt(ep->sock, IPPROTO_IP, IP_TTL, &config->ttl, sizeof(config->ttl));
+
     /* set socket address */
     /* set socket address */
     if (IP_IS_V4(&config->target_addr)) {
     if (IP_IS_V4(&config->target_addr)) {
         struct sockaddr_in *to4 = (struct sockaddr_in *)&ep->target_addr;
         struct sockaddr_in *to4 = (struct sockaddr_in *)&ep->target_addr;
@@ -353,6 +358,10 @@ esp_err_t esp_ping_get_profile(esp_ping_handle_t hdl, esp_ping_profile_t profile
         from = &ep->packet_hdr->seqno;
         from = &ep->packet_hdr->seqno;
         copy_size = sizeof(ep->packet_hdr->seqno);
         copy_size = sizeof(ep->packet_hdr->seqno);
         break;
         break;
+    case ESP_PING_PROF_TOS:
+        from = &ep->tos;
+        copy_size = sizeof(ep->tos);
+        break;
     case ESP_PING_PROF_TTL:
     case ESP_PING_PROF_TTL:
         from = &ep->ttl;
         from = &ep->ttl;
         copy_size = sizeof(ep->ttl);
         copy_size = sizeof(ep->ttl);

+ 9 - 14
components/lwip/include/apps/ping/ping_sock.h

@@ -1,16 +1,8 @@
-// Copyright 2019 Espressif Systems (Shanghai) PTE LTD
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//     http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
+/*
+ * SPDX-FileCopyrightText: 2019-2021 Espressif Systems (Shanghai) CO LTD
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
 
 
 #pragma once
 #pragma once
 
 
@@ -67,7 +59,8 @@ typedef struct {
     uint32_t interval_ms;     /*!< Milliseconds between each ping procedure */
     uint32_t interval_ms;     /*!< Milliseconds between each ping procedure */
     uint32_t timeout_ms;      /*!< Timeout value (in milliseconds) of each ping procedure */
     uint32_t timeout_ms;      /*!< Timeout value (in milliseconds) of each ping procedure */
     uint32_t data_size;       /*!< Size of the data next to ICMP packet header */
     uint32_t data_size;       /*!< Size of the data next to ICMP packet header */
-    uint8_t tos;              /*!< Type of Service, a field specified in the IP header */
+    int tos;                  /*!< Type of Service, a field specified in the IP header */
+    int ttl;                  /*!< Time to Live,a field specified in the IP header */
     ip_addr_t target_addr;    /*!< Target IP address, either IPv4 or IPv6 */
     ip_addr_t target_addr;    /*!< Target IP address, either IPv4 or IPv6 */
     uint32_t task_stack_size; /*!< Stack size of internal ping task */
     uint32_t task_stack_size; /*!< Stack size of internal ping task */
     uint32_t task_prio;       /*!< Priority of internal ping task */
     uint32_t task_prio;       /*!< Priority of internal ping task */
@@ -85,6 +78,7 @@ typedef struct {
         .timeout_ms = 1000,              \
         .timeout_ms = 1000,              \
         .data_size = 64,                 \
         .data_size = 64,                 \
         .tos = 0,                        \
         .tos = 0,                        \
+        .ttl = IP_DEFAULT_TTL,           \
         .target_addr = *(IP_ANY_TYPE),   \
         .target_addr = *(IP_ANY_TYPE),   \
         .task_stack_size = 2048,         \
         .task_stack_size = 2048,         \
         .task_prio = 2,                  \
         .task_prio = 2,                  \
@@ -99,6 +93,7 @@ typedef struct {
 */
 */
 typedef enum {
 typedef enum {
     ESP_PING_PROF_SEQNO,   /*!< Sequence number of a ping procedure */
     ESP_PING_PROF_SEQNO,   /*!< Sequence number of a ping procedure */
+    ESP_PING_PROF_TOS,     /*!< Type of service of a ping procedure */
     ESP_PING_PROF_TTL,     /*!< Time to live of a ping procedure */
     ESP_PING_PROF_TTL,     /*!< Time to live of a ping procedure */
     ESP_PING_PROF_REQUEST, /*!< Number of request packets sent out */
     ESP_PING_PROF_REQUEST, /*!< Number of request packets sent out */
     ESP_PING_PROF_REPLY,   /*!< Number of reply packets received */
     ESP_PING_PROF_REPLY,   /*!< Number of reply packets received */

+ 6 - 0
examples/protocols/icmp_echo/main/echo_example_main.c

@@ -73,6 +73,7 @@ static struct {
     struct arg_int *data_size;
     struct arg_int *data_size;
     struct arg_int *count;
     struct arg_int *count;
     struct arg_int *tos;
     struct arg_int *tos;
+    struct arg_int *ttl;
     struct arg_str *host;
     struct arg_str *host;
     struct arg_end *end;
     struct arg_end *end;
 } ping_args;
 } ping_args;
@@ -107,6 +108,10 @@ static int do_ping_cmd(int argc, char **argv)
         config.tos = (uint32_t)(ping_args.tos->ival[0]);
         config.tos = (uint32_t)(ping_args.tos->ival[0]);
     }
     }
 
 
+    if (ping_args.ttl->count > 0) {
+        config.ttl = (uint32_t)(ping_args.ttl->ival[0]);
+    }
+
     // parse IP address
     // parse IP address
     struct sockaddr_in6 sock_addr6;
     struct sockaddr_in6 sock_addr6;
     ip_addr_t target_addr;
     ip_addr_t target_addr;
@@ -156,6 +161,7 @@ static void register_ping(void)
     ping_args.data_size = arg_int0("s", "size", "<n>", "Specify the number of data bytes to be sent");
     ping_args.data_size = arg_int0("s", "size", "<n>", "Specify the number of data bytes to be sent");
     ping_args.count = arg_int0("c", "count", "<n>", "Stop after sending count packets");
     ping_args.count = arg_int0("c", "count", "<n>", "Stop after sending count packets");
     ping_args.tos = arg_int0("Q", "tos", "<n>", "Set Type of Service related bits in IP datagrams");
     ping_args.tos = arg_int0("Q", "tos", "<n>", "Set Type of Service related bits in IP datagrams");
+    ping_args.ttl = arg_int0("T", "ttl", "<n>", "Set Time to Live related bits in IP datagrams");
     ping_args.host = arg_str1(NULL, NULL, "<host>", "Host address");
     ping_args.host = arg_str1(NULL, NULL, "<host>", "Host address");
     ping_args.end = arg_end(1);
     ping_args.end = arg_end(1);
     const esp_console_cmd_t ping_cmd = {
     const esp_console_cmd_t ping_cmd = {

+ 0 - 2
tools/ci/check_copyright_ignore.txt

@@ -874,13 +874,11 @@ components/linux/include/sys/queue.h
 components/log/esp_log_private.h
 components/log/esp_log_private.h
 components/log/host_test/log_test/main/log_test.cpp
 components/log/host_test/log_test/main/log_test.cpp
 components/log/log_linux.c
 components/log/log_linux.c
-components/lwip/apps/ping/esp_ping.c
 components/lwip/apps/ping/ping.c
 components/lwip/apps/ping/ping.c
 components/lwip/apps/sntp/sntp.c
 components/lwip/apps/sntp/sntp.c
 components/lwip/include/apps/dhcpserver/dhcpserver_options.h
 components/lwip/include/apps/dhcpserver/dhcpserver_options.h
 components/lwip/include/apps/esp_ping.h
 components/lwip/include/apps/esp_ping.h
 components/lwip/include/apps/ping/ping.h
 components/lwip/include/apps/ping/ping.h
-components/lwip/include/apps/ping/ping_sock.h
 components/lwip/include/apps/sntp/sntp.h
 components/lwip/include/apps/sntp/sntp.h
 components/lwip/port/esp32/debug/lwip_debug.c
 components/lwip/port/esp32/debug/lwip_debug.c
 components/lwip/port/esp32/freertos/sys_arch.c
 components/lwip/port/esp32/freertos/sys_arch.c