[PING]
[reactos.git] / reactos / base / applications / network / ping / ping.c
index a82ce2a..bbeafa6 100644 (file)
@@ -19,7 +19,6 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
  */
-
 /*
  * PROJECT:     ReactOS Ping Command
  * LICENSE:     MIT
  * PROGRAMMERS: Tim Crawford <crawfxrd@gmail.com>
  */
 
+#include <stdlib.h>
+
 #define WIN32_LEAN_AND_MEAN
 
-#include <stdio.h>
-#include <stdlib.h>
+#define WIN32_NO_STATUS
+#include <windef.h>
+#include <winbase.h>
 #include <winsock2.h>
 #include <ws2tcpip.h>
 #include <iphlpapi.h>
 #include <icmpapi.h>
 
+#include <conutils.h>
+
 #include "resource.h"
 
 #define NDEBUG
 
 static BOOL ParseCmdLine(int argc, PWSTR argv[]);
 static BOOL ResolveTarget(PCWSTR target);
-static void Usage(void);
 static void Ping(void);
 static void PrintStats(void);
 static BOOL WINAPI ConsoleCtrlHandler(DWORD ControlType);
-static void PrintString(UINT id, ...);
 
-static HANDLE hStdOut;
 static HANDLE hIcmpFile = INVALID_HANDLE_VALUE;
 static ULONG Timeout = 4000;
 static int Family = AF_UNSPEC;
@@ -85,34 +86,30 @@ wmain(int argc, WCHAR *argv[])
     DWORD StrLen = 46;
     int Status;
 
-    IpOptions.Ttl = 128;
+    /* Initialize the Console Standard Streams */
+    ConInitStdStreams();
 
-    hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
+    IpOptions.Ttl = 128;
 
     if (!ParseCmdLine(argc, argv))
-    {
         return 1;
-    }
 
     if (!SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE))
     {
         DPRINT("Failed to set control handler: %lu\n", GetLastError());
-
         return 1;
     }
 
     Status = WSAStartup(MAKEWORD(2, 2), &wsaData);
     if (Status != 0)
     {
-        PrintString(IDS_WINSOCK_FAIL, Status);
-
+        ConResPrintf(StdErr, IDS_WINSOCK_FAIL, Status);
         return 1;
     }
 
     if (!ResolveTarget(TargetName))
     {
         WSACleanup();
-
         return 1;
     }
 
@@ -121,18 +118,13 @@ wmain(int argc, WCHAR *argv[])
         DPRINT("WSAAddressToStringW failed: %d\n", WSAGetLastError());
         FreeAddrInfoW(Target);
         WSACleanup();
-
         return 1;
     }
 
     if (Family == AF_INET6)
-    {
         hIcmpFile = Icmp6CreateFile();
-    }
     else
-    {
         hIcmpFile = IcmpCreateFile();
-    }
 
 
     if (hIcmpFile == INVALID_HANDLE_VALUE)
@@ -140,24 +132,19 @@ wmain(int argc, WCHAR *argv[])
         DPRINT("IcmpCreateFile failed: %lu\n", GetLastError());
         FreeAddrInfoW(Target);
         WSACleanup();
-
         return 1;
     }
 
     if (*CanonName)
-    {
-        PrintString(IDS_PINGING_HOSTNAME, CanonName, Address);
-    }
+        ConResPrintf(StdOut, IDS_PINGING_HOSTNAME, CanonName, Address);
     else
-    {
-        PrintString(IDS_PINGING_ADDRESS, Address);
-    }
+        ConResPrintf(StdOut, IDS_PINGING_ADDRESS, Address);
 
-    PrintString(IDS_PING_SIZE, RequestSize);
+    ConResPrintf(StdOut, IDS_PING_SIZE, RequestSize);
 
     Ping();
-    i = 1;
 
+    i = 1;
     while (i < PingCount)
     {
         Sleep(1000);
@@ -176,50 +163,6 @@ wmain(int argc, WCHAR *argv[])
     return 0;
 }
 
-static
-void
-PrintString(UINT id, ...)
-{
-    WCHAR Format[1024];
-    WCHAR Msg[1024];
-    DWORD Len, written;
-    va_list args;
-
-    if (!LoadStringW(GetModuleHandleW(NULL), id, Format, _countof(Format)))
-    {
-        DPRINT("LoadStringW failed: %lu\n", GetLastError());
-
-        return;
-    }
-
-    va_start(args, id);
-
-    Len = FormatMessageW(
-        FORMAT_MESSAGE_FROM_STRING,
-        Format, 0, 0,
-        Msg, 1024, &args);
-
-    if (Len == 0)
-    {
-        DPRINT("FormatMessageW failed: %lu\n", GetLastError());
-
-        va_end(args);
-        return;
-    }
-
-    // TODO: Handle writing to file.
-    WriteConsole(hStdOut, Msg, Len, &written, NULL);
-
-    va_end(args);
-}
-
-static
-void
-Usage(void)
-{
-    PrintString(IDS_USAGE);
-}
-
 static
 BOOL
 ParseCmdLine(int argc, PWSTR argv[])
@@ -228,8 +171,7 @@ ParseCmdLine(int argc, PWSTR argv[])
 
     if (argc < 2)
     {
-        Usage();
-
+        ConResPrintf(StdOut, IDS_USAGE);
         return FALSE;
     }
 
@@ -248,69 +190,66 @@ ParseCmdLine(int argc, PWSTR argv[])
                 break;
 
             case L'n':
+            {
                 if (i + 1 < argc)
                 {
                     PingForever = FALSE;
                     PingCount = wcstoul(argv[++i], NULL, 0);
-
                     if (PingCount == 0)
                     {
-                        PrintString(IDS_BAD_VALUE, argv[i - 1], 1, UINT_MAX);
-
+                        ConResPrintf(StdErr, IDS_BAD_VALUE, argv[i - 1], 1, UINT_MAX);
                         return FALSE;
                     }
                 }
                 else
                 {
-                    PrintString(IDS_MISSING_VALUE, argv[i]);
-
+                    ConResPrintf(StdErr, IDS_MISSING_VALUE, argv[i]);
                     return FALSE;
                 }
-
                 break;
+            }
 
             case L'l':
+            {
                 if (i + 1 < argc)
                 {
                     RequestSize = wcstoul(argv[++i], NULL, 0);
-
                     if (RequestSize > MAX_SEND_SIZE)
                     {
-                        PrintString(IDS_BAD_VALUE, argv[i - 1], 0, MAX_SEND_SIZE);
-
+                        ConResPrintf(StdErr, IDS_BAD_VALUE, argv[i - 1], 0, MAX_SEND_SIZE);
                         return FALSE;
                     }
                 }
                 else
                 {
-                    PrintString(IDS_MISSING_VALUE, argv[i]);
-
+                    ConResPrintf(StdErr, IDS_MISSING_VALUE, argv[i]);
                     return FALSE;
                 }
-
                 break;
+            }
 
             case L'f':
+            {
                 if (Family == AF_INET6)
                 {
-                    PrintString(IDS_WRONG_FAMILY, argv[i], L"IPv4");
-
+                    ConResPrintf(StdErr, IDS_WRONG_FAMILY, argv[i], L"IPv4");
                     return FALSE;
                 }
 
                 Family = AF_INET;
                 IpOptions.Flags |= IP_FLAG_DF;
                 break;
+            }
 
             case L'i':
+            {
                 if (i + 1 < argc)
                 {
                     ULONG Ttl = wcstoul(argv[++i], NULL, 0);
 
                     if ((Ttl == 0) || (Ttl > UCHAR_MAX))
                     {
-                        PrintString(IDS_BAD_VALUE, argv[i - 1], 1, UCHAR_MAX);
-
+                        ConResPrintf(StdErr, IDS_BAD_VALUE, argv[i - 1], 1, UCHAR_MAX);
                         return FALSE;
                     }
 
@@ -318,18 +257,17 @@ ParseCmdLine(int argc, PWSTR argv[])
                 }
                 else
                 {
-                    PrintString(IDS_MISSING_VALUE, argv[i]);
-
+                    ConResPrintf(StdErr, IDS_MISSING_VALUE, argv[i]);
                     return FALSE;
                 }
-
                 break;
+            }
 
             case L'v':
+            {
                 if (Family == AF_INET6)
                 {
-                    PrintString(IDS_WRONG_FAMILY, argv[i], L"IPv4");
-
+                    ConResPrintf(StdErr, IDS_WRONG_FAMILY, argv[i], L"IPv4");
                     return FALSE;
                 }
 
@@ -342,37 +280,34 @@ ParseCmdLine(int argc, PWSTR argv[])
                 }
                 else
                 {
-                    PrintString(IDS_MISSING_VALUE, argv[i]);
-
+                    ConResPrintf(StdErr, IDS_MISSING_VALUE, argv[i]);
                     return FALSE;
                 }
 
                 break;
+            }
 
             case L'w':
+            {
                 if (i + 1 < argc)
                 {
                     Timeout = wcstoul(argv[++i], NULL, 0);
-
                     if (Timeout < DEFAULT_TIMEOUT)
-                    {
                         Timeout = DEFAULT_TIMEOUT;
-                    }
                 }
                 else
                 {
-                    PrintString(IDS_MISSING_VALUE, argv[i]);
-
+                    ConResPrintf(StdErr, IDS_MISSING_VALUE, argv[i]);
                     return FALSE;
                 }
-
                 break;
+            }
 
             case L'R':
+            {
                 if (Family == AF_INET)
                 {
-                    PrintString(IDS_WRONG_FAMILY, argv[i], L"IPv6");
-
+                    ConResPrintf(StdErr, IDS_WRONG_FAMILY, argv[i], L"IPv6");
                     return FALSE;
                 }
 
@@ -380,38 +315,39 @@ ParseCmdLine(int argc, PWSTR argv[])
 
                 /* This option has been deprecated. Don't do anything. */
                 break;
+            }
 
             case L'4':
+            {
                 if (Family == AF_INET6)
                 {
-                    PrintString(IDS_WRONG_FAMILY, argv[i], L"IPv4");
-
+                    ConResPrintf(StdErr, IDS_WRONG_FAMILY, argv[i], L"IPv4");
                     return FALSE;
                 }
 
                 Family = AF_INET;
                 break;
+            }
 
             case L'6':
+            {
                 if (Family == AF_INET)
                 {
-                    PrintString(IDS_WRONG_FAMILY, argv[i], L"IPv6");
-
+                    ConResPrintf(StdErr, IDS_WRONG_FAMILY, argv[i], L"IPv6");
                     return FALSE;
                 }
 
                 Family = AF_INET6;
                 break;
+            }
 
             case L'?':
-                Usage();
-
+                ConResPrintf(StdOut, IDS_USAGE);
                 return FALSE;
 
             default:
-                PrintString(IDS_BAD_OPTION, argv[i]);
-                Usage();
-
+                ConResPrintf(StdErr, IDS_BAD_OPTION, argv[i]);
+                ConResPrintf(StdErr, IDS_USAGE);
                 return FALSE;
             }
         }
@@ -419,8 +355,7 @@ ParseCmdLine(int argc, PWSTR argv[])
         {
             if (TargetName != NULL)
             {
-                PrintString(IDS_BAD_PARAMETER, argv[i]);
-
+                ConResPrintf(StdErr, IDS_BAD_PARAMETER, argv[i]);
                 return FALSE;
             }
 
@@ -430,8 +365,7 @@ ParseCmdLine(int argc, PWSTR argv[])
 
     if (TargetName == NULL)
     {
-        PrintString(IDS_MISSING_ADDRESS);
-
+        ConResPrintf(StdErr, IDS_MISSING_ADDRESS);
         return FALSE;
     }
 
@@ -457,8 +391,7 @@ ResolveTarget(PCWSTR target)
         Status = GetAddrInfoW(target, NULL, &hints, &Target);
         if (Status != 0)
         {
-            PrintString(IDS_UNKNOWN_HOST, target);
-
+            ConResPrintf(StdOut, IDS_UNKNOWN_HOST, target);
             return FALSE;
         }
 
@@ -466,12 +399,10 @@ ResolveTarget(PCWSTR target)
     }
     else if (ResolveAddress)
     {
-        Status = GetNameInfoW(
-            Target->ai_addr, Target->ai_addrlen,
-            CanonName, _countof(CanonName),
-            NULL, 0,
-            NI_NAMEREQD);
-
+        Status = GetNameInfoW(Target->ai_addr, Target->ai_addrlen,
+                              CanonName, _countof(CanonName),
+                              NULL, 0,
+                              NI_NAMEREQD);
         if (Status != 0)
         {
             DPRINT("GetNameInfoW failed: %d\n", WSAGetLastError());
@@ -495,30 +426,24 @@ Ping(void)
     SendBuffer = malloc(RequestSize);
     if (SendBuffer == NULL)
     {
-        PrintString(IDS_NO_RESOURCES);
-
+        ConResPrintf(StdErr, IDS_NO_RESOURCES);
         exit(1);
     }
 
     ZeroMemory(SendBuffer, RequestSize);
 
     if (Family == AF_INET6)
-    {
         ReplySize += sizeof(ICMPV6_ECHO_REPLY);
-    }
     else
-    {
         ReplySize += sizeof(ICMP_ECHO_REPLY);
-    }
 
     ReplySize += RequestSize + SIZEOF_ICMP_ERROR + SIZEOF_IO_STATUS_BLOCK;
 
     ReplyBuffer = malloc(ReplySize);
     if (ReplyBuffer == NULL)
     {
-        PrintString(IDS_NO_RESOURCES);
+        ConResPrintf(StdErr, IDS_NO_RESOURCES);
         free(SendBuffer);
-
         exit(1);
     }
 
@@ -533,20 +458,18 @@ Ping(void)
         ZeroMemory(&Source, sizeof(Source));
         Source.sin6_family = AF_INET6;
 
-        Status = Icmp6SendEcho2(
-            hIcmpFile, NULL, NULL, NULL,
-            &Source,
-            (struct sockaddr_in6 *)Target->ai_addr,
-            SendBuffer, (USHORT)RequestSize, &IpOptions,
-            ReplyBuffer, ReplySize, Timeout);
+        Status = Icmp6SendEcho2(hIcmpFile, NULL, NULL, NULL,
+                                &Source,
+                                (struct sockaddr_in6 *)Target->ai_addr,
+                                SendBuffer, (USHORT)RequestSize, &IpOptions,
+                                ReplyBuffer, ReplySize, Timeout);
     }
     else
     {
-        Status = IcmpSendEcho2(
-            hIcmpFile, NULL, NULL, NULL,
-            ((PSOCKADDR_IN)Target->ai_addr)->sin_addr.s_addr,
-            SendBuffer, (USHORT)RequestSize, &IpOptions,
-            ReplyBuffer, ReplySize, Timeout);
+        Status = IcmpSendEcho2(hIcmpFile, NULL, NULL, NULL,
+                               ((PSOCKADDR_IN)Target->ai_addr)->sin_addr.s_addr,
+                               SendBuffer, (USHORT)RequestSize, &IpOptions,
+                               ReplyBuffer, ReplySize, Timeout);
     }
 
     free(SendBuffer);
@@ -557,19 +480,19 @@ Ping(void)
         switch (Status)
         {
         case IP_DEST_HOST_UNREACHABLE:
-            PrintString(IDS_DEST_HOST_UNREACHABLE);
+            ConResPrintf(StdOut, IDS_DEST_HOST_UNREACHABLE);
             break;
 
         case IP_DEST_NET_UNREACHABLE:
-            PrintString(IDS_DEST_NET_UNREACHABLE);
+            ConResPrintf(StdOut, IDS_DEST_NET_UNREACHABLE);
             break;
 
         case IP_REQ_TIMED_OUT:
-            PrintString(IDS_REQUEST_TIMED_OUT);
+            ConResPrintf(StdOut, IDS_REQUEST_TIMED_OUT);
             break;
 
         default:
-            PrintString(IDS_TRANSMIT_FAILED, Status);
+            ConResPrintf(StdOut, IDS_TRANSMIT_FAILED, Status);
             break;
         }
     }
@@ -577,7 +500,7 @@ Ping(void)
     {
         EchosReceived++;
 
-        PrintString(IDS_REPLY_FROM, Address);
+        ConResPrintf(StdOut, IDS_REPLY_FROM, Address);
 
         if (Family == AF_INET6)
         {
@@ -588,38 +511,32 @@ Ping(void)
             switch (pEchoReply->Status)
             {
             case IP_SUCCESS:
+            {
                 EchosSuccessful++;
 
                 if (pEchoReply->RoundTripTime == 0)
-                {
-                    PrintString(IDS_REPLY_TIME_0MS);
-                }
+                    ConResPrintf(StdOut, IDS_REPLY_TIME_0MS);
                 else
-                {
-                    PrintString(IDS_REPLY_TIME_MS, pEchoReply->RoundTripTime);
-                }
+                    ConResPrintf(StdOut, IDS_REPLY_TIME_MS, pEchoReply->RoundTripTime);
 
                 if (pEchoReply->RoundTripTime < RTTMin || RTTMin == 0)
-                {
                     RTTMin = pEchoReply->RoundTripTime;
-                }
 
                 if (pEchoReply->RoundTripTime > RTTMax || RTTMax == 0)
-                {
                     RTTMax = pEchoReply->RoundTripTime;
-                }
 
-                wprintf(L"\n");
+                ConPuts(StdOut, L"\n");
 
                 RTTTotal += pEchoReply->RoundTripTime;
                 break;
+            }
 
             case IP_TTL_EXPIRED_TRANSIT:
-                PrintString(IDS_TTL_EXPIRED);
+                ConResPrintf(StdOut, IDS_TTL_EXPIRED);
                 break;
 
             default:
-                PrintString(IDS_REPLY_STATUS, pEchoReply->Status);
+                ConResPrintf(StdOut, IDS_REPLY_STATUS, pEchoReply->Status);
                 break;
             }
         }
@@ -632,40 +549,34 @@ Ping(void)
             switch (pEchoReply->Status)
             {
             case IP_SUCCESS:
+            {
                 EchosSuccessful++;
 
-                PrintString(IDS_REPLY_BYTES, pEchoReply->DataSize);
+                ConResPrintf(StdOut, IDS_REPLY_BYTES, pEchoReply->DataSize);
 
                 if (pEchoReply->RoundTripTime == 0)
-                {
-                    PrintString(IDS_REPLY_TIME_0MS);
-                }
+                    ConResPrintf(StdOut, IDS_REPLY_TIME_0MS);
                 else
-                {
-                    PrintString(IDS_REPLY_TIME_MS, pEchoReply->RoundTripTime);
-                }
+                    ConResPrintf(StdOut, IDS_REPLY_TIME_MS, pEchoReply->RoundTripTime);
 
-                PrintString(IDS_REPLY_TTL, pEchoReply->Options.Ttl);
+                ConResPrintf(StdOut, IDS_REPLY_TTL, pEchoReply->Options.Ttl);
 
                 if (pEchoReply->RoundTripTime < RTTMin || RTTMin == 0)
-                {
                     RTTMin = pEchoReply->RoundTripTime;
-                }
 
                 if (pEchoReply->RoundTripTime > RTTMax || RTTMax == 0)
-                {
                     RTTMax = pEchoReply->RoundTripTime;
-                }
 
                 RTTTotal += pEchoReply->RoundTripTime;
                 break;
+            }
 
             case IP_TTL_EXPIRED_TRANSIT:
-                PrintString(IDS_TTL_EXPIRED);
+                ConResPrintf(StdOut, IDS_TTL_EXPIRED);
                 break;
 
             default:
-                PrintString(IDS_REPLY_STATUS, pEchoReply->Status);
+                ConResPrintf(StdOut, IDS_REPLY_STATUS, pEchoReply->Status);
                 break;
             }
         }
@@ -681,13 +592,12 @@ PrintStats(void)
     ULONG EchosLost = EchosSent - EchosReceived;
     ULONG PercentLost = (ULONG)((EchosLost / (double)EchosSent) * 100.0);
 
-    PrintString(IDS_STATISTICS, Address, EchosSent, EchosReceived, EchosLost, PercentLost);
+    ConResPrintf(StdOut, IDS_STATISTICS, Address, EchosSent, EchosReceived, EchosLost, PercentLost);
 
     if (EchosSuccessful > 0)
     {
         ULONG RTTAverage = RTTTotal / EchosSuccessful;
-
-        PrintString(IDS_APPROXIMATE_RTT, RTTMin, RTTMax, RTTAverage);
+        ConResPrintf(StdOut, IDS_APPROXIMATE_RTT, RTTMin, RTTMax, RTTAverage);
     }
 }
 
@@ -700,12 +610,12 @@ ConsoleCtrlHandler(DWORD ControlType)
     {
     case CTRL_C_EVENT:
         PrintStats();
-        PrintString(IDS_CTRL_C);
+        ConResPrintf(StdOut, IDS_CTRL_C);
         return FALSE;
 
     case CTRL_BREAK_EVENT:
         PrintStats();
-        PrintString(IDS_CTRL_BREAK);
+        ConResPrintf(StdOut, IDS_CTRL_BREAK);
         return TRUE;
 
     case CTRL_CLOSE_EVENT: