From b569f4069a7bceea9432dc3b119c9bc41081ae14 Mon Sep 17 00:00:00 2001 From: xueyunfei Date: Mon, 6 Jun 2022 14:11:19 +0800 Subject: [PATCH] bugfix for add ttl for ping socket --- components/lwip/apps/ping/esp_ping.c | 19 +++++----------- components/lwip/apps/ping/ping_sock.c | 3 +++ components/lwip/include/apps/ping/ping_sock.h | 22 +++++++------------ .../icmp_echo/main/echo_example_main.c | 6 +++++ tools/ci/check_copyright_ignore.txt | 2 -- 5 files changed, 23 insertions(+), 29 deletions(-) diff --git a/components/lwip/apps/ping/esp_ping.c b/components/lwip/apps/ping/esp_ping.c index ece50cff14..7b493b6c44 100644 --- a/components/lwip/apps/ping/esp_ping.c +++ b/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 #include "esp_ping.h" @@ -25,6 +17,7 @@ typedef struct _ping_option { size_t ping_data_len; uint16_t ping_id; u8_t ping_tos; + u8_t ping_ttl; esp_ping_found_fn ping_res_fn; esp_ping_found ping_res; void *ping_reserve; diff --git a/components/lwip/apps/ping/ping_sock.c b/components/lwip/apps/ping/ping_sock.c index 6cfb9f26ad..cb247fe072 100644 --- a/components/lwip/apps/ping/ping_sock.c +++ b/components/lwip/apps/ping/ping_sock.c @@ -271,6 +271,9 @@ esp_err_t esp_ping_new_session(const esp_ping_config_t *config, const esp_ping_c /* set 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 */ if (IP_IS_V4(&config->target_addr)) { struct sockaddr_in *to4 = (struct sockaddr_in *)&ep->target_addr; diff --git a/components/lwip/include/apps/ping/ping_sock.h b/components/lwip/include/apps/ping/ping_sock.h index 57938480d5..3e07fd564c 100644 --- a/components/lwip/include/apps/ping/ping_sock.h +++ b/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 @@ -67,7 +59,8 @@ typedef struct { uint32_t interval_ms; /*!< Milliseconds between 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 */ - 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 */ uint32_t task_stack_size; /*!< Stack size of internal ping task */ uint32_t task_prio; /*!< Priority of internal ping task */ @@ -85,6 +78,7 @@ typedef struct { .timeout_ms = 1000, \ .data_size = 64, \ .tos = 0, \ + .ttl = IP_DEFAULT_TTL, \ .target_addr = *(IP_ANY_TYPE), \ .task_stack_size = 2048, \ .task_prio = 2, \ diff --git a/examples/protocols/icmp_echo/main/echo_example_main.c b/examples/protocols/icmp_echo/main/echo_example_main.c index 04eac2b10a..1530265bae 100644 --- a/examples/protocols/icmp_echo/main/echo_example_main.c +++ b/examples/protocols/icmp_echo/main/echo_example_main.c @@ -73,6 +73,7 @@ static struct { struct arg_int *data_size; struct arg_int *count; struct arg_int *tos; + struct arg_int *ttl; struct arg_str *host; struct arg_end *end; } ping_args; @@ -107,6 +108,10 @@ static int do_ping_cmd(int argc, char **argv) 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 struct sockaddr_in6 sock_addr6; ip_addr_t target_addr; @@ -156,6 +161,7 @@ static void register_ping(void) ping_args.data_size = arg_int0("s", "size", "", "Specify the number of data bytes to be sent"); ping_args.count = arg_int0("c", "count", "", "Stop after sending count packets"); ping_args.tos = arg_int0("Q", "tos", "", "Set Type of Service related bits in IP datagrams"); + ping_args.ttl = arg_int0("T", "ttl", "", "Set Time to Live related bits in IP datagrams"); ping_args.host = arg_str1(NULL, NULL, "", "Host address"); ping_args.end = arg_end(1); const esp_console_cmd_t ping_cmd = { diff --git a/tools/ci/check_copyright_ignore.txt b/tools/ci/check_copyright_ignore.txt index 5496dcddb3..606199e81c 100644 --- a/tools/ci/check_copyright_ignore.txt +++ b/tools/ci/check_copyright_ignore.txt @@ -874,13 +874,11 @@ components/linux/include/sys/queue.h components/log/esp_log_private.h components/log/host_test/log_test/main/log_test.cpp components/log/log_linux.c -components/lwip/apps/ping/esp_ping.c components/lwip/apps/ping/ping.c components/lwip/apps/sntp/sntp.c components/lwip/include/apps/dhcpserver/dhcpserver_options.h components/lwip/include/apps/esp_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/port/esp32/debug/lwip_debug.c components/lwip/port/esp32/freertos/sys_arch.c