Icmp: get tos parameter in icmp reply

bugfix for add ttl for ping socket
This commit is contained in:
xueyunfei
2022-07-11 19:40:27 +08:00
committed by BOT
parent 55e69bf9cb
commit 61b1b2ac12
4 changed files with 20 additions and 1 deletions

View File

@ -25,6 +25,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

@ -67,6 +67,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);
@ -131,6 +132,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;
}
@ -289,6 +291,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;
@ -371,6 +376,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

@ -67,7 +67,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 +86,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 +101,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 = {