|
1 | 1 | /* |
2 | | - * SPDX-FileCopyrightText: 2019-2021 Espressif Systems (Shanghai) CO LTD |
| 2 | + * SPDX-FileCopyrightText: 2019-2024 Espressif Systems (Shanghai) CO LTD |
3 | 3 | * |
4 | 4 | * SPDX-License-Identifier: Apache-2.0 |
5 | 5 | */ |
@@ -35,6 +35,8 @@ const static char *TAG = "ping_sock"; |
35 | 35 | #define PING_FLAGS_INIT (1 << 0) |
36 | 36 | #define PING_FLAGS_START (1 << 1) |
37 | 37 |
|
| 38 | +#define IP_ICMP_HDR_SIZE (64) // 64 bytes are enough to cover IP header and ICMP header |
| 39 | + |
38 | 40 | typedef struct { |
39 | 41 | int sock; |
40 | 42 | struct sockaddr_storage target_addr; |
@@ -85,50 +87,54 @@ static esp_err_t esp_ping_send(esp_ping_t *ep) |
85 | 87 |
|
86 | 88 | static int esp_ping_receive(esp_ping_t *ep) |
87 | 89 | { |
88 | | - char buf[64]; // 64 bytes are enough to cover IP header and ICMP header |
| 90 | + char buf[IP_ICMP_HDR_SIZE]; |
89 | 91 | int len = 0; |
90 | 92 | struct sockaddr_storage from; |
91 | 93 | int fromlen = sizeof(from); |
92 | 94 | uint16_t data_head = 0; |
| 95 | + ip_addr_t recv_addr; |
| 96 | + ip_addr_copy(recv_addr, *IP_ADDR_ANY); |
93 | 97 |
|
94 | 98 | while ((len = recvfrom(ep->sock, buf, sizeof(buf), 0, (struct sockaddr *)&from, (socklen_t *)&fromlen)) > 0) { |
95 | 99 | #if CONFIG_LWIP_IPV4 |
96 | 100 | if (from.ss_family == AF_INET) { |
97 | 101 | // IPv4 |
98 | 102 | struct sockaddr_in *from4 = (struct sockaddr_in *)&from; |
99 | | - inet_addr_to_ip4addr(ip_2_ip4(&ep->recv_addr), &from4->sin_addr); |
100 | | - IP_SET_TYPE_VAL(ep->recv_addr, IPADDR_TYPE_V4); |
| 103 | + inet_addr_to_ip4addr(ip_2_ip4(&recv_addr), &from4->sin_addr); |
| 104 | + IP_SET_TYPE_VAL(recv_addr, IPADDR_TYPE_V4); |
101 | 105 | data_head = (uint16_t)(sizeof(struct ip_hdr) + sizeof(struct icmp_echo_hdr)); |
102 | 106 | } |
103 | 107 | #endif |
104 | 108 | #if CONFIG_LWIP_IPV6 |
105 | 109 | if (from.ss_family == AF_INET6) { |
106 | 110 | // IPv6 |
107 | 111 | struct sockaddr_in6 *from6 = (struct sockaddr_in6 *)&from; |
108 | | - inet6_addr_to_ip6addr(ip_2_ip6(&ep->recv_addr), &from6->sin6_addr); |
109 | | - IP_SET_TYPE_VAL(ep->recv_addr, IPADDR_TYPE_V6); |
| 112 | + inet6_addr_to_ip6addr(ip_2_ip6(&recv_addr), &from6->sin6_addr); |
| 113 | + IP_SET_TYPE_VAL(recv_addr, IPADDR_TYPE_V6); |
110 | 114 | data_head = (uint16_t)(sizeof(struct ip6_hdr) + sizeof(struct icmp6_echo_hdr)); |
111 | 115 | } |
112 | 116 | #endif |
113 | 117 | if (len >= data_head) { |
114 | 118 | #if CONFIG_LWIP_IPV4 |
115 | | - if (IP_IS_V4_VAL(ep->recv_addr)) { // Currently we process IPv4 |
| 119 | + if (IP_IS_V4_VAL(recv_addr)) { // Currently we process IPv4 |
116 | 120 | struct ip_hdr *iphdr = (struct ip_hdr *)buf; |
117 | | - struct icmp_echo_hdr *iecho = (struct icmp_echo_hdr *)(buf + (IPH_HL(iphdr) * 4)); |
| 121 | + struct icmp_echo_hdr *iecho = (struct icmp_echo_hdr *)(buf + (IPH_HL_BYTES(iphdr))); |
118 | 122 | if ((iecho->id == ep->packet_hdr->id) && (iecho->seqno == ep->packet_hdr->seqno)) { |
| 123 | + ip_addr_copy(ep->recv_addr, recv_addr); |
119 | 124 | ep->received++; |
120 | | - ep->ttl = iphdr->_ttl; |
121 | | - ep->tos = iphdr->_tos; |
| 125 | + ep->ttl = IPH_TTL(iphdr); |
| 126 | + ep->tos = IPH_TOS(iphdr); |
122 | 127 | ep->recv_len = lwip_ntohs(IPH_LEN(iphdr)) - data_head; // The data portion of ICMP |
123 | 128 | return len; |
124 | 129 | } |
125 | 130 | } |
126 | 131 | #endif // CONFIG_LWIP_IPV4 |
127 | 132 | #if CONFIG_LWIP_IPV6 |
128 | | - if (IP_IS_V6_VAL(ep->recv_addr)) { // Currently we process IPv6 |
| 133 | + if (IP_IS_V6_VAL(recv_addr)) { // Currently we process IPv6 |
129 | 134 | struct ip6_hdr *iphdr = (struct ip6_hdr *)buf; |
130 | 135 | struct icmp6_echo_hdr *iecho6 = (struct icmp6_echo_hdr *)(buf + sizeof(struct ip6_hdr)); // IPv6 head length is 40 |
131 | 136 | if ((iecho6->id == ep->packet_hdr->id) && (iecho6->seqno == ep->packet_hdr->seqno)) { |
| 137 | + ip_addr_copy(ep->recv_addr, recv_addr); |
132 | 138 | ep->received++; |
133 | 139 | ep->recv_len = IP6H_PLEN(iphdr) - sizeof(struct icmp6_echo_hdr); //The data portion of ICMPv6 |
134 | 140 | return len; |
|
0 commit comments