#define NDEBUG
-#ifndef _MSC_VER
-
-/* Should be in the header files somewhere (exported by ntdll.dll) */
-long atol(const char *str);
-
-#ifndef __int64
-typedef long long __int64;
-#endif
-
-char * _i64toa(__int64 value, char *string, int radix);
-
-#endif /* _MSC_VER */
-
/* General ICMP constants */
#define ICMP_MINSIZE 8 /* Minimum ICMP packet size */
#define ICMP_MAXSIZE 65535 /* Maximum ICMP packet size */
DataSize = GetULONG2(&argv[i][2], argv[i + 1], &i);
if (DataSize > ICMP_MAXSIZE - sizeof(ICMP_ECHO_PACKET))
{
- printf("Bad value for option -l, valid range is from 0 to %I64d.\n",
- ICMP_MAXSIZE - sizeof(ICMP_ECHO_PACKET));
+ printf("Bad value for option -l, valid range is from 0 to %d.\n",
+ ICMP_MAXSIZE - (int)sizeof(ICMP_ECHO_PACKET));
return FALSE;
}
break;
Target.sin_family = AF_INET;
TargetIP = inet_ntoa(Target.sin_addr);
- CurrentSeqNum = 0;
+ CurrentSeqNum = 1;
SentCount = 0;
LostCount = 0;
MinRTT.QuadPart = 0;
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);
}
- printf("Reply from %s: bytes=%I64d time%s%s TTL=%d\n", inet_ntoa(from->sin_addr),
- size - IphLength - sizeof(ICMP_ECHO_PACKET), Sign, Time, IpHeader->TTL);
+ printf("Reply from %s: bytes=%d time%s%s TTL=%d\n", inet_ntoa(from->sin_addr),
+ size - IphLength - (int)sizeof(ICMP_ECHO_PACKET), Sign, Time, IpHeader->TTL);
if (RelativeTime.QuadPart < MinRTT.QuadPart || !MinRTTSet)
{
MinRTT.QuadPart = RelativeTime.QuadPart;
}
if (Status == SOCKET_ERROR)
{
+ LostCount++;
if (WSAGetLastError() == WSAEHOSTUNREACH)
printf("Destination host unreachable.\n");
else
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 */
- }
- if (Status == SOCKET_ERROR)
- {
- if (WSAGetLastError() != WSAETIMEDOUT)
+ }
+ else
+ LostCount++;
+ if (Status == SOCKET_ERROR)
{
- printf("Could not receive data (%d).\n", WSAGetLastError());
- GlobalFree(Buffer);
- return FALSE;
+ if (WSAGetLastError() != WSAETIMEDOUT)
+ {
+ printf("Could not receive data (%d).\n", WSAGetLastError());
+ GlobalFree(Buffer);
+ return FALSE;
+ }
+ Status = 0;
}
- Status = 0;
- }
- if (Status == 0)
- {
- printf("Request timed out.\n");
- LostCount++;
- 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;
printf("\nPing statistics for %s:\n", TargetIP);
printf(" Packets: Sent = %d, Received = %d, Lost = %d (%d%% loss),\n",
SentCount, SentCount - LostCount, LostCount, Count);
- printf("Approximate round trip times in milli-seconds:\n");
- printf(" Minimum = %s, Maximum = %s, Average = %s\n",
- MinTime, MaxTime, AvgTime);
+ /* Print approximate times or NO approximate times if 100% loss */
+ if ((SentCount - LostCount) > 0)
+ {
+ printf("Approximate round trip times in milli-seconds:\n");
+ printf(" Minimum = %s, Maximum = %s, Average = %s\n",
+ MinTime, MaxTime, AvgTime);
+ }
}
return 0;
}