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
This commit is contained in:
Jiang Jiang Jian
2022-07-05 17:09:53 +08:00
5 changed files with 30 additions and 29 deletions

View File

@@ -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 "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;

View File

@@ -49,6 +49,7 @@ typedef struct {
uint32_t elapsed_time_ms;
uint32_t total_time_ms;
uint8_t ttl;
uint8_t tos;
uint32_t flags;
void (*on_ping_success)(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)) {
ep->received++;
ep->ttl = iphdr->_ttl;
ep->tos = iphdr->_tos;
ep->recv_len = lwip_ntohs(IPH_LEN(iphdr)) - data_head; // The data portion of ICMP
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 */
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;
@@ -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;
copy_size = sizeof(ep->packet_hdr->seqno);
break;
case ESP_PING_PROF_TOS:
from = &ep->tos;
copy_size = sizeof(ep->tos);
break;
case ESP_PING_PROF_TTL:
from = &ep->ttl;
copy_size = sizeof(ep->ttl);

View File

@@ -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, \
@@ -99,6 +93,7 @@ typedef struct {
*/
typedef enum {
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_REQUEST, /*!< Number of request packets sent out */
ESP_PING_PROF_REPLY, /*!< Number of reply packets received */

View File

@@ -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", "<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.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.end = arg_end(1);
const esp_console_cmd_t ping_cmd = {

View File

@@ -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