[PING] Fix error handling and response address decoding (#318)
authorStanislav Motylkov <x86corez@gmail.com>
Fri, 26 Jan 2018 18:38:47 +0000 (21:38 +0300)
committerGed Murphy <gedmurphy@reactos.org>
Fri, 26 Jan 2018 18:38:47 +0000 (18:38 +0000)
- Host/net unreachable error should be obtained from EchoReply->Status
- Use GetNameInfoW to decode actual response addresses (borrowed from tracert code)

CORE-14241 #resolve

base/applications/network/ping/ping.c

index bbeafa6..dd1262e 100644 (file)
@@ -479,14 +479,6 @@ Ping(void)
         Status = GetLastError();
         switch (Status)
         {
-        case IP_DEST_HOST_UNREACHABLE:
-            ConResPrintf(StdOut, IDS_DEST_HOST_UNREACHABLE);
-            break;
-
-        case IP_DEST_NET_UNREACHABLE:
-            ConResPrintf(StdOut, IDS_DEST_NET_UNREACHABLE);
-            break;
-
         case IP_REQ_TIMED_OUT:
             ConResPrintf(StdOut, IDS_REQUEST_TIMED_OUT);
             break;
@@ -498,16 +490,40 @@ Ping(void)
     }
     else
     {
+        SOCKADDR_IN6 SockAddrIn6;
+        SOCKADDR_IN SockAddrIn;
+        PSOCKADDR SockAddr;
+        socklen_t Size;
+
         EchosReceived++;
 
-        ConResPrintf(StdOut, IDS_REPLY_FROM, Address);
+        ZeroMemory(&SockAddrIn, sizeof(SockAddrIn));
+        ZeroMemory(&SockAddrIn6, sizeof(SockAddrIn6));
 
         if (Family == AF_INET6)
         {
             PICMPV6_ECHO_REPLY pEchoReply;
+            PIPV6_ADDRESS_EX Ipv6Addr;
 
             pEchoReply = (PICMPV6_ECHO_REPLY)ReplyBuffer;
 
+            Ipv6Addr = (PIPV6_ADDRESS_EX)&pEchoReply->Address;
+            SockAddrIn6.sin6_family = AF_INET6;
+            CopyMemory(SockAddrIn6.sin6_addr.u.Word, Ipv6Addr->sin6_addr, sizeof(SockAddrIn6.sin6_addr));
+            //SockAddrIn6.sin6_addr = Ipv6Addr->sin6_addr;
+            SockAddr = (PSOCKADDR)&SockAddrIn6;
+            Size = sizeof(SOCKADDR_IN6);
+
+            GetNameInfoW(SockAddr,
+                         Size,
+                         Address,
+                         NI_MAXHOST,
+                         NULL,
+                         0,
+                         NI_NUMERICHOST);
+
+            ConResPrintf(StdOut, IDS_REPLY_FROM, Address);
+
             switch (pEchoReply->Status)
             {
             case IP_SUCCESS:
@@ -531,6 +547,14 @@ Ping(void)
                 break;
             }
 
+            case IP_DEST_NET_UNREACHABLE:
+                ConResPrintf(StdOut, IDS_DEST_NET_UNREACHABLE);
+                break;
+
+            case IP_DEST_HOST_UNREACHABLE:
+                ConResPrintf(StdOut, IDS_DEST_HOST_UNREACHABLE);
+                break;
+
             case IP_TTL_EXPIRED_TRANSIT:
                 ConResPrintf(StdOut, IDS_TTL_EXPIRED);
                 break;
@@ -543,9 +567,26 @@ Ping(void)
         else
         {
             PICMP_ECHO_REPLY pEchoReply;
+            IPAddr *IP4Addr;
 
             pEchoReply = (PICMP_ECHO_REPLY)ReplyBuffer;
 
+            IP4Addr = (IPAddr *)&pEchoReply->Address;
+            SockAddrIn.sin_family = AF_INET;
+            SockAddrIn.sin_addr.S_un.S_addr = *IP4Addr;
+            SockAddr = (PSOCKADDR)&SockAddrIn;
+            Size = sizeof(SOCKADDR_IN);
+
+            GetNameInfoW(SockAddr,
+                         Size,
+                         Address,
+                         NI_MAXHOST,
+                         NULL,
+                         0,
+                         NI_NUMERICHOST);
+
+            ConResPrintf(StdOut, IDS_REPLY_FROM, Address);
+
             switch (pEchoReply->Status)
             {
             case IP_SUCCESS:
@@ -571,6 +612,14 @@ Ping(void)
                 break;
             }
 
+            case IP_DEST_NET_UNREACHABLE:
+                ConResPrintf(StdOut, IDS_DEST_NET_UNREACHABLE);
+                break;
+
+            case IP_DEST_HOST_UNREACHABLE:
+                ConResPrintf(StdOut, IDS_DEST_HOST_UNREACHABLE);
+                break;
+
             case IP_TTL_EXPIRED_TRANSIT:
                 ConResPrintf(StdOut, IDS_TTL_EXPIRED);
                 break;