[CMAKE]
[reactos.git] / base / applications / network / tracert / tracert.c
index b04729c..e3c1048 100644 (file)
@@ -260,10 +260,10 @@ PreparePacket(PAPPINFO pInfo,
     pInfo->SendPacket->icmpheader.code     = 0;
     pInfo->SendPacket->icmpheader.checksum = 0;
     pInfo->SendPacket->icmpheader.id       = (USHORT)GetCurrentProcessId();
-    pInfo->SendPacket->icmpheader.seq      = iSeqNum;
+    pInfo->SendPacket->icmpheader.seq      = htons((USHORT)iSeqNum);
 
     /* calculate checksum of packet */
-    pInfo->SendPacket->icmpheader.checksum  = CheckSum((PUSHORT)&pInfo->SendPacket,
+    pInfo->SendPacket->icmpheader.checksum  = CheckSum((PUSHORT)&pInfo->SendPacket->icmpheader,
                                                        sizeof(ICMP_HEADER) + PACKET_SIZE);
 }
 
@@ -279,8 +279,8 @@ SendPacket(PAPPINFO pInfo)
     pInfo->lTimeStart = GetTime(pInfo);
 
     iSockRet = sendto(pInfo->icmpSock,              //socket
-                      (char *)pInfo->SendPacket,   //buffer
-                      PACKET_SIZE,                  //size of buffer
+                      (char *)&pInfo->SendPacket->icmpheader,//buffer
+                      sizeof(ICMP_HEADER) + PACKET_SIZE,//size of buffer
                       0,                            //flags
                       (SOCKADDR *)&pInfo->dest,     //destination
                       sizeof(pInfo->dest));         //address length
@@ -377,7 +377,6 @@ DecodeResponse(PAPPINFO pInfo)
 
     /* cast the recieved packet into an ECHO reply and a TTL Exceed and check the ID*/
     ECHO_REPLY_HEADER *IcmpHdr = (ECHO_REPLY_HEADER *)((char*)pInfo->RecvPacket + header_len);
-    TTL_EXCEED_HEADER *TTLExceedHdr = (TTL_EXCEED_HEADER *)((char *)pInfo->RecvPacket + header_len);
 
     /* Make sure the reply is ok */
     if (PACKET_SIZE < header_len + ICMP_MIN_SIZE)
@@ -389,14 +388,7 @@ DecodeResponse(PAPPINFO pInfo)
     switch (IcmpHdr->icmpheader.type)
     {
            case TTL_EXCEEDED :
-                if (TTLExceedHdr->OrigIcmpHeader.id != (USHORT)GetCurrentProcessId())
-                {
-                /* FIXME: our network stack shouldn't allow this... */
-                /* we've picked up a packet not related to this process probably from another local program. We ignore it */
-                    DebugPrint(_T("Rouge packet: header id,  process id  %d"), TTLExceedHdr->OrigIcmpHeader.id, GetCurrentProcessId());
-                    return -1;
-                }
-                _tprintf(_T("%3Ld ms"), (pInfo->lTimeEnd - pInfo->lTimeStart) / pInfo->TicksPerMs.QuadPart);
+                _tprintf(_T("%3ld ms"), (ULONG)((pInfo->lTimeEnd - pInfo->lTimeStart) / pInfo->TicksPerMs.QuadPart));
                 return 0;
 
            case ECHO_REPLY :
@@ -407,7 +399,7 @@ DecodeResponse(PAPPINFO pInfo)
                     DebugPrint(_T("Rouge packet: header id %d, process id  %d"), IcmpHdr->icmpheader.id, GetCurrentProcessId());
                     return -1;
                 }
-                _tprintf(_T("%3Ld ms"), (pInfo->lTimeEnd - pInfo->lTimeStart) / pInfo->TicksPerMs.QuadPart);
+                _tprintf(_T("%3ld ms"), (ULONG)((pInfo->lTimeEnd - pInfo->lTimeStart) / pInfo->TicksPerMs.QuadPart));
                 return 1;
 
            case DEST_UNREACHABLE :
@@ -415,7 +407,7 @@ DecodeResponse(PAPPINFO pInfo)
                 return 2;
     }
 
-    return 0;
+    return -3;
 }
 
 
@@ -430,7 +422,7 @@ AllocateBuffers(PAPPINFO pInfo)
 
     pInfo->RecvPacket = (PIPv4_HEADER)HeapAlloc(GetProcessHeap(),
                                                 0,
-                                                sizeof(IPv4_HEADER) + PACKET_SIZE);
+                                                MAX_PING_PACKET_SIZE);
     if (!pInfo->RecvPacket)
     {
         HeapFree(GetProcessHeap(),
@@ -513,11 +505,9 @@ Driver(PAPPINFO pInfo)
 
                         if (iRecieveReturn)
                         {
-                            DecodeResponse(pInfo);
+                            if (DecodeResponse(pInfo) < 0)
+                                bAwaitPacket = TRUE;
                         }
-                        else
-                            /* packet timed out. Don't wait for it again */
-                            bAwaitPacket = FALSE;
 
                     } while (bAwaitPacket);
                 }
@@ -559,6 +549,7 @@ Driver(PAPPINFO pInfo)
                   {
                       DebugPrint(_T("error: %d"), WSAGetLastError());
                       DebugPrint(_T(" getnameinfo failed: %d"), iNameInfoRet);
+                      _tprintf(_T("%s"), inet_ntoa(pInfo->source.sin_addr));
                   }
 
             }
@@ -598,7 +589,7 @@ Cleanup(PAPPINFO pInfo)
                  0,
                  pInfo->SendPacket);
 
-    if (pInfo->SendPacket)
+    if (pInfo->RecvPacket)
         HeapFree(GetProcessHeap(),
                  0,
                  pInfo->RecvPacket);