Use bigger buffer to avoid stack corruption, as seen when running app built with...
[reactos.git] / reactos / apps / utils / net / ping / ping.c
index f91a898..5d2efba 100644 (file)
@@ -1,5 +1,4 @@
-/* $Id$
- *
+/*
  * COPYRIGHT:   See COPYING in the top level directory
  * PROJECT:     ReactOS ping utility
  * FILE:        apps/net/ping/ping.c
@@ -8,15 +7,14 @@
  * 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
@@ -33,8 +31,11 @@ typedef long long __int64;
 
 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 */
@@ -104,9 +105,9 @@ LARGE_INTEGER       TicksPerMs; /* Ticks per millisecond */
 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)
 {
@@ -123,9 +124,10 @@ VOID DisplayBuffer(
       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");
@@ -137,7 +139,7 @@ VOID Usage(VOID)
 }
 
 /* Reset configuration to default values */
-VOID Reset(VOID)
+static VOID Reset(VOID)
 {
     LARGE_INTEGER PerformanceCounterFrequency;
 
@@ -173,13 +175,13 @@ VOID Reset(VOID)
 }
 
 /* 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;
@@ -191,7 +193,7 @@ ULONG GetULONG(LPSTR String)
 }
 
 /* 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;
 
@@ -209,7 +211,7 @@ ULONG GetULONG2(LPSTR String1, LPSTR String2, PINT i)
 }
 
 /* Parse command line parameters */
-BOOL ParseCmdline(int argc, char* argv[])
+static BOOL ParseCmdline(int argc, char* argv[])
 {
     INT i;
     BOOL ShowUsage;
@@ -278,7 +280,7 @@ BOOL ParseCmdline(int argc, char* argv[])
 }
 
 /* Calculate checksum of data */
-WORD Checksum(PUSHORT data, UINT size)
+static WORD Checksum(PUSHORT data, UINT size)
 {
     ULONG sum = 0;
 
@@ -297,7 +299,7 @@ WORD Checksum(PUSHORT data, UINT size)
 }
 
 /* Prepare to ping target */
-BOOL Setup(VOID)
+static BOOL Setup(VOID)
 {
     WORD     wVersionRequested;
     WSADATA  WsaData;
@@ -306,10 +308,10 @@ BOOL Setup(VOID)
     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;
     }
 
@@ -327,9 +329,9 @@ BOOL Setup(VOID)
         if (phe == NULL) {
             printf("Unknown host %s.\n", TargetName);
             return FALSE;
-        } 
+        }
     }
-       
+
     if (phe != NULL) {
         CopyMemory(&Target.sin_addr, phe->h_addr, phe->h_length);
     } else {
@@ -341,7 +343,7 @@ BOOL Setup(VOID)
     } else {
         Target.sin_family = AF_INET;
     }
-       
+
     TargetIP           = inet_ntoa(Target.sin_addr);
     CurrentSeqNum      = 0;
     SentCount          = 0;
@@ -354,7 +356,7 @@ BOOL Setup(VOID)
 }
 
 /* Close socket */
-VOID Cleanup(VOID)
+static VOID Cleanup(VOID)
 {
     if (IcmpSock != INVALID_SOCKET)
         closesocket(IcmpSock);
@@ -362,7 +364,7 @@ VOID Cleanup(VOID)
     WSACleanup();
 }
 
-VOID QueryTime(PLARGE_INTEGER Time)
+static VOID QueryTime(PLARGE_INTEGER Time)
 {
     if (UsePerformanceCounter) {
         if (QueryPerformanceCounter(Time) == 0) {
@@ -384,13 +386,13 @@ VOID QueryTime(PLARGE_INTEGER Time)
     }
 }
 
-VOID TimeToMsString(LPSTR String, LARGE_INTEGER Time)
+static VOID TimeToMsString(LPSTR String, LARGE_INTEGER Time)
 {
     CHAR          Convstr[40];
     LARGE_INTEGER LargeTime;
 
     LargeTime.QuadPart = Time.QuadPart / TicksPerMs.QuadPart;
+
     _i64toa(LargeTime.QuadPart, Convstr, 10);
        strcpy(String, Convstr);
     strcat(String, "ms");
@@ -398,7 +400,7 @@ VOID TimeToMsString(LPSTR String, LARGE_INTEGER Time)
 
 /* 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;
@@ -406,7 +408,7 @@ BOOL DecodeResponse(PCHAR buffer, UINT size, PSOCKADDR_IN from)
     CHAR              Time[100];
     LARGE_INTEGER     RelativeTime;
     LARGE_INTEGER     LargeTime;
-    CHAR              Sign[1];
+    CHAR              Sign[2];
 
     IpHeader = (PIPv4_HEADER)buffer;
 
@@ -448,25 +450,26 @@ BOOL DecodeResponse(PCHAR buffer, UINT size, PSOCKADDR_IN from)
     }
 
 
-    printf("Reply from %s: bytes=%d time%s%s TTL=%d\n", inet_ntoa(from->sin_addr), 
+    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) {
-                 MinRTT.QuadPart = RelativeTime.QuadPart;
-      MinRTTSet = TRUE;
+    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;
@@ -585,7 +588,7 @@ int main(int argc, char* argv[])
 
         printf("\nPinging %s [%s] with %d bytes of data:\n\n",
             TargetName, TargetIP, DataSize);
-               
+
                Count = 0;
                while ((NeverStop) || (Count < PingCount)) {
                        Ping();