Merge branch 'bugfix/fix_ping_specify_length_issue_v4.0' into 'release/v4.0'

bugfix: fix ICMP specify length issue (backport v4.0)

See merge request espressif/esp-idf!11838
This commit is contained in:
Jiang Jiang Jian
2021-01-05 10:45:16 +08:00

View File

@ -188,9 +188,11 @@ ping_recv(int s)
struct icmp_echo_hdr *iecho; struct icmp_echo_hdr *iecho;
int fromlen = sizeof(from); int fromlen = sizeof(from);
struct timeval now; struct timeval now;
uint16_t data_head = (uint16_t)(sizeof(struct ip_hdr) + sizeof(struct icmp_echo_hdr));
int data_len = 0;
while((len = recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*)&from, (socklen_t*)&fromlen)) > 0) { while((len = recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*)&from, (socklen_t*)&fromlen)) > 0) {
if (len >= (int)(sizeof(struct ip_hdr)+sizeof(struct icmp_echo_hdr))) { if (len >= data_head) {
if (from.sin_family != AF_INET) { if (from.sin_family != AF_INET) {
/* Ping is not IPv4 */ /* Ping is not IPv4 */
LWIP_DEBUGF( PING_DEBUG, ("ping: invalid sin_family\n")); LWIP_DEBUGF( PING_DEBUG, ("ping: invalid sin_family\n"));
@ -199,6 +201,7 @@ ping_recv(int s)
inet_addr_to_ip4addr(&fromaddr, &from.sin_addr); inet_addr_to_ip4addr(&fromaddr, &from.sin_addr);
iphdr = (struct ip_hdr *)buf; iphdr = (struct ip_hdr *)buf;
iecho = (struct icmp_echo_hdr *)(buf + (IPH_HL(iphdr) * 4)); iecho = (struct icmp_echo_hdr *)(buf + (IPH_HL(iphdr) * 4));
data_len = lwip_ntohs(IPH_LEN(iphdr)) - data_head;
LWIP_DEBUGF( PING_DEBUG, ("ping: recv seq=%d ", ntohs(iecho->seqno))); LWIP_DEBUGF( PING_DEBUG, ("ping: recv seq=%d ", ntohs(iecho->seqno)));
ip4_addr_debug_print(PING_DEBUG, &fromaddr); ip4_addr_debug_print(PING_DEBUG, &fromaddr);
@ -208,7 +211,7 @@ ping_recv(int s)
if ((iecho->id == PING_ID) && (iecho->seqno == htons(ping_seq_num))) { if ((iecho->id == PING_ID) && (iecho->seqno == htons(ping_seq_num))) {
/* do some ping result processing */ /* do some ping result processing */
#ifdef ESP_PING #ifdef ESP_PING
esp_ping_result((ICMPH_TYPE(iecho) == ICMP_ER), len, PING_TIME_DIFF_MS(now, ping_time)); esp_ping_result((ICMPH_TYPE(iecho) == ICMP_ER), data_len, PING_TIME_DIFF_MS(now, ping_time));
#else #else
PING_RESULT((ICMPH_TYPE(iecho) == ICMP_ER)); PING_RESULT((ICMPH_TYPE(iecho) == ICMP_ER));
#endif #endif
@ -228,7 +231,7 @@ ping_recv(int s)
/* do some ping result processing */ /* do some ping result processing */
#ifdef ESP_PING #ifdef ESP_PING
esp_ping_result(0, len, PING_TIME_DIFF_MS(now, ping_time)); esp_ping_result(0, data_len, PING_TIME_DIFF_MS(now, ping_time));
#else #else
PING_RESULT(0); PING_RESULT(0);
#endif #endif