-/* $Id: ping.c,v 1.7 2004/11/21 22:26:14 chorns Exp $
- *
+/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS ping utility
* FILE: apps/net/ping/ping.c
* REVISIONS:
* CSH 01/09/2000 Created
*/
-//#include <windows.h>
+
#include <winsock2.h>
#include <tchar.h>
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
-#ifndef _MSC_VER
-//#define DBG
+#ifndef _MSC_VER
/* FIXME: Where should this be? */
#ifdef CopyMemory
char * _i64toa(__int64 value, char *string, int radix);
-#endif
+#endif /* _MSC_VER */
+#ifdef DBG
+#undef DBG
+#endif
/* General ICMP constants */
#define ICMP_MINSIZE 8 /* Minimum ICMP packet size */
LARGE_INTEGER TicksPerUs; /* Ticks per microsecond */
BOOL UsePerformanceCounter;
-
+#ifdef DBG
/* Display the contents of a buffer */
-VOID DisplayBuffer(
+static VOID DisplayBuffer(
PVOID Buffer,
DWORD Size)
{
UINT i;
PCHAR p;
- printf("Buffer (0x%X) Size (0x%X).\n", Buffer, Size);
+ printf("Buffer (0x%p) Size (0x%lX).\n", Buffer, Size);
p = (PCHAR)Buffer;
for (i = 0; i < Size; i++) {
printf("%02X ", (p[i]) & 0xFF);
}
}
+#endif /* DBG */
/* Display usage information on screen */
-VOID Usage(VOID)
+static VOID Usage(VOID)
{
printf("\nUsage: ping [-t] [-n count] [-l size] [-w timeout] destination-host\n\n");
printf("Options:\n");
}
/* Reset configuration to default values */
-VOID Reset(VOID)
+static VOID Reset(VOID)
{
LARGE_INTEGER PerformanceCounterFrequency;
}
/* Return ULONG in a string */
-ULONG GetULONG(LPSTR String)
+static ULONG GetULONG(LPSTR String)
{
UINT i, Length;
ULONG Value;
i = 0;
- Length = strlen(String);
+ Length = (UINT)_tcslen(String);
while ((i < Length) && ((String[i] < '0') || (String[i] > '9'))) i++;
if ((i >= Length) || ((String[i] < '0') || (String[i] > '9'))) {
InvalidOption = TRUE;
}
/* Return ULONG in a string. Try next paramter if not successful */
-ULONG GetULONG2(LPSTR String1, LPSTR String2, PINT i)
+static ULONG GetULONG2(LPSTR String1, LPSTR String2, PINT i)
{
ULONG Value;
}
/* Parse command line parameters */
-BOOL ParseCmdline(int argc, char* argv[])
+static BOOL ParseCmdline(int argc, char* argv[])
{
INT i;
BOOL ShowUsage;
}
/* Calculate checksum of data */
-WORD Checksum(PUSHORT data, UINT size)
+static WORD Checksum(PUSHORT data, UINT size)
{
ULONG sum = 0;
}
/* Prepare to ping target */
-BOOL Setup(VOID)
+static BOOL Setup(VOID)
{
WORD wVersionRequested;
WSADATA WsaData;
PHOSTENT phe;
wVersionRequested = MAKEWORD(2, 2);
-
+
Status = WSAStartup(wVersionRequested, &WsaData);
if (Status != 0) {
- printf("Could not initialize winsock dll.\n");
+ printf("Could not initialize winsock dll.\n");
return FALSE;
}
if (phe == NULL) {
printf("Unknown host %s.\n", TargetName);
return FALSE;
- }
+ }
}
-
+
if (phe != NULL) {
- CopyMemory(&Target.sin_addr, phe->h_addr_list, phe->h_length);
+ CopyMemory(&Target.sin_addr, phe->h_addr, phe->h_length);
} else {
Target.sin_addr.s_addr = Addr;
}
} else {
Target.sin_family = AF_INET;
}
-
+
TargetIP = inet_ntoa(Target.sin_addr);
CurrentSeqNum = 0;
SentCount = 0;
}
/* Close socket */
-VOID Cleanup(VOID)
+static VOID Cleanup(VOID)
{
if (IcmpSock != INVALID_SOCKET)
closesocket(IcmpSock);
WSACleanup();
}
-VOID QueryTime(PLARGE_INTEGER Time)
+static VOID QueryTime(PLARGE_INTEGER Time)
{
if (UsePerformanceCounter) {
if (QueryPerformanceCounter(Time) == 0) {
}
}
-VOID TimeToMsString(LPSTR String, LARGE_INTEGER Time)
+static VOID TimeToMsString(LPSTR String, LARGE_INTEGER Time)
{
- UINT i, Length;
CHAR Convstr[40];
LARGE_INTEGER LargeTime;
LargeTime.QuadPart = Time.QuadPart / TicksPerMs.QuadPart;
- _i64toa(LargeTime.QuadPart, Convstr, 10);
- strcpy(String, Convstr);
- strcat(String, ",");
- LargeTime.QuadPart = (Time.QuadPart % TicksPerMs.QuadPart) / TicksPerUs.QuadPart;
_i64toa(LargeTime.QuadPart, Convstr, 10);
- Length = strlen(Convstr);
- if (Length < 4) {
- for (i = 0; i < 4 - Length; i++)
- strcat(String, "0");
- }
-
- strcat(String, Convstr);
+ strcpy(String, Convstr);
strcat(String, "ms");
}
/* Locate the ICMP data and print it. Returns TRUE if the packet was good,
FALSE if not */
-BOOL DecodeResponse(PCHAR buffer, UINT size, PSOCKADDR_IN from)
+static BOOL DecodeResponse(PCHAR buffer, UINT size, PSOCKADDR_IN from)
{
PIPv4_HEADER IpHeader;
PICMP_ECHO_PACKET Icmp;
CHAR Time[100];
LARGE_INTEGER RelativeTime;
LARGE_INTEGER LargeTime;
+ CHAR Sign[2];
IpHeader = (PIPv4_HEADER)buffer;
RelativeTime.QuadPart = (LargeTime.QuadPart - Icmp->Timestamp.QuadPart);
- TimeToMsString(Time, RelativeTime);
+ if ((RelativeTime.QuadPart / TicksPerMs.QuadPart) < 1) {
+ strcpy(Sign, "<");
+ strcpy(Time, "1ms");
+ } else {
+ strcpy(Sign, "=");
+ TimeToMsString(Time, RelativeTime);
+ }
+
- printf("Reply from %s: bytes=%d time=%s TTL=%d\n", inet_ntoa(from->sin_addr),
- size - IphLength - sizeof(ICMP_ECHO_PACKET), Time, IpHeader->TTL);
- if (RelativeTime.QuadPart < MinRTT.QuadPart) {
- MinRTT.QuadPart = RelativeTime.QuadPart;
- MinRTTSet = TRUE;
+ printf("Reply from %s: bytes=%d time%s%s TTL=%d\n", inet_ntoa(from->sin_addr),
+ size - IphLength - sizeof(ICMP_ECHO_PACKET), Sign, Time, IpHeader->TTL);
+ if (RelativeTime.QuadPart < MinRTT.QuadPart || !MinRTTSet) {
+ MinRTT.QuadPart = RelativeTime.QuadPart;
+ MinRTTSet = TRUE;
}
- if (RelativeTime.QuadPart > MaxRTT.QuadPart)
- MaxRTT.QuadPart = RelativeTime.QuadPart;
+ if (RelativeTime.QuadPart > MaxRTT.QuadPart)
+ MaxRTT.QuadPart = RelativeTime.QuadPart;
+
SumRTT.QuadPart += RelativeTime.QuadPart;
return TRUE;
}
/* Send and receive one ping */
-BOOL Ping(VOID)
+static BOOL Ping(VOID)
{
INT Status;
SOCKADDR From;
- UINT Length;
+ INT Length;
PVOID Buffer;
UINT Size;
PICMP_ECHO_PACKET Packet;
printf("\nPinging %s [%s] with %d bytes of data:\n\n",
TargetName, TargetIP, DataSize);
-
+
Count = 0;
while ((NeverStop) || (Count < PingCount)) {
Ping();