From 3ced893fd0d57c717fce070bd4acb836d42ba8d3 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Mon, 3 Aug 2009 02:15:17 +0000 Subject: [PATCH] - Keep waiting for a reply if we get one that is not ours or invalid - Make sure the packet we got is from our target otherwise discard it svn path=/trunk/; revision=42356 --- reactos/base/applications/network/ping/ping.c | 68 ++++++++++--------- 1 file changed, 36 insertions(+), 32 deletions(-) diff --git a/reactos/base/applications/network/ping/ping.c b/reactos/base/applications/network/ping/ping.c index e8135086daa..3901d26243c 100644 --- a/reactos/base/applications/network/ping/ping.c +++ b/reactos/base/applications/network/ping/ping.c @@ -437,6 +437,14 @@ static BOOL DecodeResponse(PCHAR buffer, UINT size, PSOCKADDR_IN from) return FALSE; } + if (from->sin_addr.s_addr != Target.sin_addr.s_addr) + { +#ifndef NDEBUG + printf("Bad source address (%s should be %s)\n", inet_ntoa(from->sin_addr), inet_ntoa(Target.sin_addr)); +#endif /* !NDEBUG */ + return FALSE; + } + QueryTime(&LargeTime); RelativeTime.QuadPart = (LargeTime.QuadPart - Icmp->Timestamp.QuadPart); @@ -543,44 +551,40 @@ static BOOL Ping(VOID) Timeval.tv_sec = Timeout / 1000; Timeval.tv_usec = Timeout % 1000; - Status = select(0, &Fds, NULL, NULL, &Timeval); - if ((Status != SOCKET_ERROR) && (Status != 0)) - { - Length = sizeof(From); - Status = recvfrom(IcmpSock, Buffer, Size, 0, &From, &Length); + do { + Status = select(0, &Fds, NULL, NULL, &Timeval); + if ((Status != SOCKET_ERROR) && (Status != 0)) + { + Length = sizeof(From); + Status = recvfrom(IcmpSock, Buffer, Size, 0, &From, &Length); #ifndef NDEBUG - printf("Received packet\n"); - DisplayBuffer(Buffer, Status); - printf("\n"); + printf("Received packet\n"); + DisplayBuffer(Buffer, Status); + printf("\n"); #endif /* !NDEBUG */ - } - else - LostCount++; - if (Status == SOCKET_ERROR) - { - if (WSAGetLastError() != WSAETIMEDOUT) - { - printf("Could not receive data (%d).\n", WSAGetLastError()); - GlobalFree(Buffer); - return FALSE; } - Status = 0; - } + else + LostCount++; + if (Status == SOCKET_ERROR) + { + if (WSAGetLastError() != WSAETIMEDOUT) + { + printf("Could not receive data (%d).\n", WSAGetLastError()); + GlobalFree(Buffer); + return FALSE; + } + Status = 0; + } - if (Status == 0) - { - printf("Request timed out.\n"); - GlobalFree(Buffer); - return TRUE; - } + if (Status == 0) + { + printf("Request timed out.\n"); + GlobalFree(Buffer); + return TRUE; + } - if (!DecodeResponse(Buffer, Status, (PSOCKADDR_IN)&From)) - { - /* FIXME: Wait again as it could be another ICMP message type */ - printf("Request timed out (incomplete datagram received).\n"); - LostCount++; - } + } while (!DecodeResponse(Buffer, Status, (PSOCKADDR_IN)&From)); GlobalFree(Buffer); return TRUE;