- Fix sending malformed ICMP packets because we read the wrong length from the wrong...
[reactos.git] / reactos / base / applications / network / tracert / tracert.c
index e41269e..97bc8b8 100644 (file)
-/*
- *  ReactOS Win32 Applications
- *  Copyright (C) 2005 ReactOS Team
+ /*
+ * PROJECT:     ReactOS trace route utility
+ * LICENSE:     GPL - See COPYING in the top level directory
+ * FILE:        base/applications/network/tracert.c
+ * PURPOSE:     Trace network paths through networks
+ * COPYRIGHT:   Copyright 2006 - 2007 Ged Murphy <gedmurphy@reactos.org>
  *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
-/*
- * COPYRIGHT:   See COPYING in the top level directory
- * PROJECT:     ReactOS traceroute utility
- * FILE:        apps/utils/net/tracert/tracert.c
- * PURPOSE:     trace a packets route through a network
- * PROGRAMMERS: Ged Murphy (gedmurphy@gmail.com)
- * REVISIONS:
- *   GM 03/05/05 Created
- *
- */
-
 
-#include <winsock2.h>
-#include <tchar.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <ws2tcpip.h>
-#include <string.h>
-#include <time.h>
 #include "tracert.h"
 
-#define WIN32_LEAN_AND_MEAN
+//#define TRACERT_DBG
 
-#ifdef DBG
-#undef DBG
-#endif
+CHAR cHostname[256];            // target hostname
+CHAR cDestIP[18];               // target IP
 
-/*
- * globals
- */
-SOCKET icmpSock;                // socket descriptor
-SOCKADDR_IN source, dest;       // source and destination address info
-ECHO_REPLY_HEADER sendpacket;   // ICMP echo packet
-IPv4_HEADER recvpacket;         // return reveive packet
 
-BOOL bUsePerformanceCounter;    // whether to use the high res performance counter
-LARGE_INTEGER TicksPerMs;       // number of millisecs in relation to proc freq
-LARGE_INTEGER TicksPerUs;       // number of microsecs in relation to proc freq
-LONGLONG lTimeStart;            // send packet, timer start
-LONGLONG lTimeEnd;              // receive packet, timer end
+static VOID
+DebugPrint(LPTSTR lpString, ...)
+{
+#ifdef TRACERT_DBG
+    va_list args;
+    va_start(args, lpString);
+    _vtprintf(lpString, args);
+    va_end(args);
+#else
+    UNREFERENCED_PARAMETER(lpString);
+#endif
+}
 
-CHAR cHostname[256];            // target hostname
-CHAR cDestIP[18];               // target IP
 
+static VOID
+Usage(VOID)
+{
+    _tprintf(_T("\nUsage: tracert [-d] [-h maximum_hops] [-j host-list] [-w timeout] target_name\n\n"
+                "Options:\n"
+                "    -d                 Do not resolve addresses to hostnames.\n"
+                "    -h maximum_hops    Maximum number of hops to search for target.\n"
+                "    -j host-list       Loose source route along host-list.\n"
+                "    -w timeout         Wait timeout milliseconds for each reply.\n\n"));
 
-/*
- * command line options
- */
-BOOL bResolveAddresses = TRUE;  // -d  MS ping defaults to true.
-INT iMaxHops = 30;              // -h  Max number of hops before trace ends
-INT iHostList;                  // -j  @UNIMPLEMENTED@
-INT iTimeOut = 2000;            // -w  time before packet times out
+    _tprintf(_T("NOTES\n-----\n"
+           "- Setting TTL values is not currently supported in ReactOS, so the trace will\n"
+           "  jump straight to the destination. This feature will be implemented soon.\n"
+           "- Host info is not currently available in ReactOS and will fail with strange\n"
+           "  results. Use -d to force it not to resolve IP's.\n"
+           "- For testing purposes, all should work as normal in a Windows environment\n\n"));
+}
 
 
-/*
- *
- * Parse command line parameters and set any options
- *
- */
-static BOOL ParseCmdline(int argc, char* argv[])
+static BOOL
+ParseCmdline(int argc,
+             LPCTSTR argv[],
+             PAPPINFO pInfo)
 {
-    int i;
+    INT i;
 
     if (argc < 2)
     {
        Usage();
        return FALSE;
     }
-
-    for (i = 1; i < argc; i++)
+    else
     {
-        if (argv[i][0] == '-')
+        for (i = 1; i < argc; i++)
         {
-            switch (argv[i][1])
+            if (argv[i][0] == _T('-'))
             {
-               case 'd': bResolveAddresses = FALSE;
-                         break;
-               case 'h': sscanf(argv[i+1], "%d", &iMaxHops);
-                         break;
-               case 'j': break; /* @unimplemented@ */
-               case 'w': sscanf(argv[i+1], "%d", &iTimeOut);
-                         break;
-               default:
-                  _tprintf(_T("%s is not a valid option.\n"), argv[i]);
-                  Usage();
-                  return FALSE;
-            }
-        }
-        else
-           /* copy target address */
-           strncpy(cHostname, argv[i], 255);
-    }
-
-    return TRUE;
-}
-
-
-
-/*
- *
- * Driver function, controls the traceroute program
- *
- */
-static INT Driver(VOID)
-{
-
-    INT iHopCount = 1;              // hop counter. default max is 30
-    USHORT iSeqNum = 0;                // initialise packet sequence number
-    INT iTTL = 1;                   // set initial packet TTL to 1
-    BOOL bFoundTarget = FALSE;      // Have we reached our destination yet
-    BOOL bAwaitPacket;              // indicates whether we have recieved a good packet
-    INT iDecRes;                    // DecodeResponse return value
-    INT iRecieveReturn;             // RecieveReturn return value
-    INT iNameInfoRet;               // getnameinfo return value
-    INT iPacketSize = PACKET_SIZE;  // packet size
-    WORD wHeaderLen;                // header length
-    PECHO_REPLY_HEADER icmphdr;
-
-
-    //temps for getting host name
-    CHAR cHost[256];
-    CHAR cServ[256];
-    CHAR *ip;
-
-    /* setup winsock */
-    WSADATA wsaData;
-
-    /* check for winsock 2 */
-    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
-    {
-#ifdef DBG
-        _tprintf(_T("WSAStartup failed.\n"));
-#endif /* DBG */
-        exit(1);
-    }
-
-    /* establish what timing method we can use */
-    SetupTimingMethod();
+                switch (argv[i][1])
+                {
+                    case _T('d'):
+                        pInfo->bResolveAddresses = FALSE;
+                    break;
 
-    /* setup target info */
-    ResolveHostname();
+                    case _T('h'):
+                        _stscanf(argv[i+1], _T("%d"), &pInfo->iMaxHops);
+                    break;
 
-    /* print standard tracing info to screen */
-    _tprintf(_T("\nTracing route to %s [%s]\n"), cHostname, cDestIP);
-    _tprintf(_T("over a maximum of %d hop"), iMaxHops);
-    iMaxHops > 1 ? _tprintf(_T("s:\n\n")) : _tprintf(_T(":\n\n"));
+                    case _T('j'):
+                        _tprintf(_T("-j is not yet implemented.\n"));
+                    break;
 
-    /* run until we hit either max hops, or we recieve 3 echo replys */
-    while ((iHopCount <= iMaxHops) && (bFoundTarget != TRUE))
-    {
-        INT i;
-
-        _tprintf(_T("%3d   "), iHopCount);
-        /* run 3 pings for each hop */
-        for (i=0; i<3; i++)
-        {
-            if (Setup(iTTL) != TRUE)
-            {
-#ifdef DBG
-                _tprintf(_T("error in Setup()\n"));
-#endif /* DBG */
-                WSACleanup();
-                exit(1);
-            }
-            PreparePacket(iPacketSize, iSeqNum);
-            if (SendPacket(iPacketSize) != SOCKET_ERROR)
-            {
-                /* loop until we get a good packet */
-                bAwaitPacket = TRUE;
-                while (bAwaitPacket)
-                {
-                    /* Receive replies until we either get a successful
-                     * read, or a fatal error occurs. */
-                    if ((iRecieveReturn = ReceivePacket(iPacketSize)) < 0)
-                    {
-                        /* check the sequence number in the packet
-                         * if it's bad, complain and wait for another packet
-                         * , otherwise break */
-                        wHeaderLen = recvpacket.h_len * 4;
-                        icmphdr = (ECHO_REPLY_HEADER *)((char*)&recvpacket + wHeaderLen);
-                        if (icmphdr->icmpheader.seq != iSeqNum)
-                        {
-                            _tprintf(_T("bad sequence number!\n"));
-                            continue;
-                        }
-                        else
-                            break;
-                    }
+                    case _T('w'):
+                        _stscanf(argv[i+1], _T("%d"), &pInfo->iTimeOut);
+                    break;
 
-                    /* if RecievePacket timed out we don't bother decoding */
-                    if (iRecieveReturn != 1)
+                    default:
                     {
-                        iDecRes = DecodeResponse(iPacketSize, iSeqNum);
-
-                        switch (iDecRes)
-                        {
-                           case 0 : bAwaitPacket = FALSE;  /* time exceeded */
-                                    break;
-                           case 1 : bAwaitPacket = FALSE;  /* echo reply */
-                                    break;
-                           case 2 : bAwaitPacket = FALSE;  /* destination unreachable */
-                                    break;
-#ifdef DBG
-                           case -1 :
-                                     _tprintf(_T("recieved foreign packet\n"));
-                                     break;
-                           case -2 :
-                                     _tprintf(_T("error in DecodeResponse\n"));
-                                     break;
-                           case -3 :
-                                     _tprintf(_T("unknown ICMP packet\n"));
-                                     break;
-#endif /* DBG */
-                           default : break;
-                        }
+                        _tprintf(_T("%s is not a valid option.\n"), argv[i]);
+                        Usage();
+                        return FALSE;
                     }
-                    else
-                        /* packet timed out. Don't wait for it again */
-                        bAwaitPacket = FALSE;
                 }
             }
-
-            iSeqNum++;
-            _tprintf(_T("   "));
+            else
+               /* copy target address */
+               _tcsncpy(cHostname, argv[i], 255);
         }
+    }
 
-        if(bResolveAddresses)
-        {
-           /* gethostbyaddr() and getnameinfo() are
-            * unimplemented in ROS at present.
-            * Alex has advised he will be implementing getnameinfo.
-            * I've used that for the time being for testing in Windows*/
-
-              //ip = inet_addr(inet_ntoa(source.sin_addr));
-              //host = gethostbyaddr((char *)&ip, 4, 0);
-
-              ip = inet_ntoa(source.sin_addr);
-
-              iNameInfoRet = getnameinfo((SOCKADDR *)&source,
-                                 sizeof(SOCKADDR),
-                                 cHost,
-                                 256,
-                                 cServ,
-                                 256,
-                                 NI_NUMERICSERV);
-              if (iNameInfoRet == 0)
-              {
-                 /* if IP address resolved to a hostname,
-                   * print the IP address after it */
-                  if (lstrcmpA(cHost, ip) != 0)
-                      _tprintf(_T("%s [%s]"), cHost, ip);
-                  else
-                      _tprintf(_T("%s"), cHost);
-              }
-              else
-              {
-                  _tprintf(_T("error: %d"), WSAGetLastError());
-#ifdef DBG
-                  _tprintf(_T(" getnameinfo failed: %d"), iNameInfoRet);
-#endif /* DBG */
-              }
+    return TRUE;
+}
 
-        }
-        else
-           _tprintf(_T("%s"), inet_ntoa(source.sin_addr));
 
-        _tprintf(_T("\n"));
+static WORD
+CheckSum(PUSHORT data,
+         UINT size)
+{
+    DWORD dwSum = 0;
 
-        /* check if we've arrived at the target */
-        if (strcmp(cDestIP, inet_ntoa(source.sin_addr)) == 0)
-            bFoundTarget = TRUE;
-        else
-        {
-            iTTL++;
-            iHopCount++;
-            Sleep(500);
-        }
+    while (size > 1)
+    {
+        dwSum += *data++;
+        size -= sizeof(USHORT);
     }
-    _tprintf(_T("\nTrace complete.\n"));
-    WSACleanup();
 
-    return 0;
+    if (size)
+        dwSum += *(UCHAR*)data;
+
+    dwSum = (dwSum >> 16) + (dwSum & 0xFFFF);
+    dwSum += (dwSum >> 16);
+
+    return (USHORT)(~dwSum);
 }
 
 
-/*
- * Establish if performance counters are available and
- * set up timing figures in relation to processor frequency.
- * If performance counters are not available, we'll be using
- * gettickcount, so set the figures to 1
- *
- */
-static VOID SetupTimingMethod(VOID)
+static VOID
+SetupTimingMethod(PAPPINFO pInfo)
 {
     LARGE_INTEGER PerformanceCounterFrequency;
 
     /* check if performance counters are available */
-    bUsePerformanceCounter = QueryPerformanceFrequency(&PerformanceCounterFrequency);
-    if (bUsePerformanceCounter)
+    pInfo->bUsePerformanceCounter = QueryPerformanceFrequency(&PerformanceCounterFrequency);
+
+    if (pInfo->bUsePerformanceCounter)
     {
         /* restrict execution to first processor on SMP systems */
         if (SetThreadAffinityMask(GetCurrentThread(), 1) == 0)
-            bUsePerformanceCounter = FALSE;
+            pInfo->bUsePerformanceCounter = FALSE;
 
-        TicksPerMs.QuadPart  = PerformanceCounterFrequency.QuadPart / 1000;
-        TicksPerUs.QuadPart  = PerformanceCounterFrequency.QuadPart / 1000000;
+        pInfo->TicksPerMs.QuadPart  = PerformanceCounterFrequency.QuadPart / 1000;
+        pInfo->TicksPerUs.QuadPart  = PerformanceCounterFrequency.QuadPart / 1000000;
     }
-
-    if (!bUsePerformanceCounter)
+    else
     {
-        TicksPerMs.QuadPart = 1;
-        TicksPerUs.QuadPart = 1;
+        pInfo->TicksPerMs.QuadPart = 1;
+        pInfo->TicksPerUs.QuadPart = 1;
     }
 }
 
 
-/*
- *
- * Check for a hostname or dotted deciamal for our target.
- * If we have a hostname, resolve to an IP and store it, else
- * just store the target IP address. Also set up other key
- * SOCKADDR_IN members needed for the connection.
- *
- */
-static VOID ResolveHostname(VOID)
+static BOOL
+ResolveHostname(PAPPINFO pInfo)
 {
     HOSTENT *hp;
     ULONG addr;
 
-    memset(&dest, 0, sizeof(dest));
+    ZeroMemory(&pInfo->dest, sizeof(pInfo->dest));
 
-    addr = inet_addr(cHostname);
     /* if address is not a dotted decimal */
-    if (addr == INADDR_NONE)
+    if ((addr = inet_addr(cHostname))== INADDR_NONE)
     {
-       hp = gethostbyname(cHostname);
-       if (hp != 0)
+       if ((hp = gethostbyname(cHostname)) != 0)
        {
-          memcpy(&dest.sin_addr, hp->h_addr, hp->h_length);
-          //dest.sin_addr = *((struct in_addr *)hp->h_addr);
-          dest.sin_family = hp->h_addrtype;
+          //CopyMemory(&pInfo->dest.sin_addr, hp->h_addr, hp->h_length);
+          pInfo->dest.sin_addr = *((struct in_addr *)hp->h_addr);
+          pInfo->dest.sin_family = hp->h_addrtype;
        }
        else
        {
           _tprintf(_T("Unable to resolve target system name %s.\n"), cHostname);
-          WSACleanup();
-          exit(1);
+          return FALSE;
        }
     }
     else
     {
-        dest.sin_addr.s_addr = addr;
-        dest.sin_family = AF_INET;
+        pInfo->dest.sin_addr.s_addr = addr;
+        pInfo->dest.sin_family = AF_INET;
     }
-    /* copy destination IP address into a string */
-    strcpy(cDestIP, inet_ntoa(dest.sin_addr));
-}
 
+    _tcscpy(cDestIP, inet_ntoa(pInfo->dest.sin_addr));
 
+    return TRUE;
+}
 
-/*
- *
- * Create our socket which will be used for sending and recieving,
- * Socket Type is raw, Protocol is ICMP. Also set the TTL value which will be
- * set in the outgoing IP packet.
- *
- */
-static INT Setup(INT iTTL)
+
+static LONGLONG
+GetTime(PAPPINFO pInfo)
 {
-    INT iSockRet;
+    LARGE_INTEGER Time;
 
-    /* create raw socket */
-    icmpSock = WSASocket(AF_INET, SOCK_RAW, IPPROTO_ICMP, 0, 0, 0);
-    if (icmpSock == INVALID_SOCKET)
+    /* Get the system time using preformance counters if available */
+    if (pInfo->bUsePerformanceCounter)
     {
-       _tprintf(_T("Could not create socket : %d.\n"), WSAGetLastError());
-       if (WSAGetLastError() == WSAEACCES)
-       {
-            _tprintf(_T("\n\nYou must be an administrator to run this program!\n\n"));
-            WSACleanup();
-            exit(1);
-       }
-       return FALSE;
+        if (QueryPerformanceCounter(&Time))
+        {
+            return Time.QuadPart;
+        }
     }
 
-    /* setup for TTL */
-    iSockRet = setsockopt(icmpSock, IPPROTO_IP, IP_TTL, (const char *)&iTTL, sizeof(iTTL));
-    if (iSockRet == SOCKET_ERROR)
+    /* otherwise fall back to GetTickCount */
+    Time.u.LowPart = (DWORD)GetTickCount();
+    Time.u.HighPart = 0;
+
+    return (LONGLONG)Time.u.LowPart;
+}
+
+
+static BOOL
+SetTTL(SOCKET sock,
+       INT iTTL)
+{
+    if (setsockopt(sock,
+                   IPPROTO_IP,
+                   IP_TTL,
+                   (const char *)&iTTL,
+                   sizeof(iTTL)) == SOCKET_ERROR)
     {
-       _tprintf(_T("TTL setsockopt failed : %d. \n"), WSAGetLastError());
+       DebugPrint(_T("TTL setsockopt failed : %d. \n"), WSAGetLastError());
        return FALSE;
     }
 
@@ -413,174 +224,165 @@ static INT Setup(INT iTTL)
 }
 
 
+static BOOL
+CreateSocket(PAPPINFO pInfo)
+{
+    pInfo->icmpSock = WSASocket(AF_INET,
+                                SOCK_RAW,
+                                IPPROTO_ICMP,
+                                0,
+                                0,
+                                0);
+
+    if (pInfo->icmpSock == INVALID_SOCKET)
+    {
+        INT err = WSAGetLastError();
+        DebugPrint(_T("Could not create socket : %d.\n"), err);
 
-/*
- * Prepare the ICMP echo request packet for sending.
- * Calculate the packet checksum
- *
- */
-static VOID PreparePacket(INT iPacketSize, USHORT iSeqNum)
+        if (err == WSAEACCES)
+        {
+            _tprintf(_T("\n\nYou must have access to raw sockets (admin) to run this program!\n\n"));
+        }
+
+        return FALSE;
+    }
+
+    return TRUE;
+}
+
+
+static VOID
+PreparePacket(PAPPINFO pInfo,
+              USHORT iSeqNum)
 {
     /* assemble ICMP echo request packet */
-    sendpacket.icmpheader.type      = ECHO_REQUEST;
-    sendpacket.icmpheader.code      = 0;
-    sendpacket.icmpheader.checksum  = 0;
-    sendpacket.icmpheader.id        = (USHORT)GetCurrentProcessId();
-    sendpacket.icmpheader.seq       = iSeqNum;
+    pInfo->SendPacket->icmpheader.type     = ECHO_REQUEST;
+    pInfo->SendPacket->icmpheader.code     = 0;
+    pInfo->SendPacket->icmpheader.checksum = 0;
+    pInfo->SendPacket->icmpheader.id       = (USHORT)GetCurrentProcessId();
+    pInfo->SendPacket->icmpheader.seq      = htons((USHORT)iSeqNum);
 
     /* calculate checksum of packet */
-    sendpacket.icmpheader.checksum  = CheckSum((PUSHORT)&sendpacket, sizeof(ICMP_HEADER) + iPacketSize);
+    pInfo->SendPacket->icmpheader.checksum  = CheckSum((PUSHORT)&pInfo->SendPacket->icmpheader,
+                                                       sizeof(ICMP_HEADER) + PACKET_SIZE);
 }
 
 
-
-/*
- *
- * Get the system time and send the ICMP packet to the destination
- * address.
- *
- */
-static INT SendPacket(INT datasize)
+static INT
+SendPacket(PAPPINFO pInfo)
 {
     INT iSockRet;
-    INT iPacketSize;
-
-    iPacketSize = sizeof(ECHO_REPLY_HEADER) + datasize;
 
-#ifdef DBG
-    _tprintf(_T("\nsending packet of %d bytes\n"), iPacketSize);
-#endif /* DBG */
+    DebugPrint(_T("\nsending packet of %d bytes... "), PACKET_SIZE);
 
     /* get time packet was sent */
-    lTimeStart = GetTime();
+    pInfo->lTimeStart = GetTime(pInfo);
 
-    iSockRet = sendto(icmpSock,              //socket
-                      (char *)&sendpacket,   //buffer
-                      iPacketSize,           //size of buffer
-                      0,                     //flags
-                      (SOCKADDR *)&dest,     //destination
-                      sizeof(dest));         //address length
+    iSockRet = sendto(pInfo->icmpSock,              //socket
+                      (char *)&pInfo->SendPacket->icmpheader,//buffer
+                      sizeof(ICMP_HEADER) + PACKET_SIZE,//size of buffer
+                      0,                            //flags
+                      (SOCKADDR *)&pInfo->dest,     //destination
+                      sizeof(pInfo->dest));         //address length
 
     if (iSockRet == SOCKET_ERROR)
     {
         if (WSAGetLastError() == WSAEACCES)
         {
+            /* FIXME: Is this correct? */
             _tprintf(_T("\n\nYou must be an administrator to run this program!\n\n"));
             WSACleanup();
-            exit(1);
+            HeapFree(GetProcessHeap(), 0, pInfo);
+            exit(-1);
         }
         else
         {
-#ifdef DBG
-            _tprintf(_T("sendto failed %d\n"), WSAGetLastError());
-#endif /* DBG */
-            return FALSE;
+            DebugPrint(_T("sendto failed %d\n"), WSAGetLastError());
         }
     }
-#ifdef DBG
-    _tprintf(_T("sent %d bytes\n"), iSockRet);
-#endif /* DBG */
+    else
+    {
+        DebugPrint(_T("sent %d bytes\n"), iSockRet);
+    }
 
-    /* return number of bytes sent */
     return iSockRet;
 }
 
 
-
-/*
- *
- * Set up a timeout value and put the socket in a select poll.
- * Wait until we recieve an IPv4 reply packet in reply to the ICMP
- * echo request packet and get the time the packet was recieved.
- * If we don't recieve a packet, do some checking to establish why.
- *
- */
-static INT ReceivePacket(INT datasize)
+static BOOL
+ReceivePacket(PAPPINFO pInfo)
 {
     TIMEVAL timeVal;
     FD_SET readFDS;
-    int iSockRet = 0, iSelRet;
-    int iFromLen;
-    int iPacketSize;
+    INT iSockRet = 0, iSelRet;
+    INT iFromLen;
+    BOOL bRet = FALSE;
 
-    /* allow for a larger recv buffer to store ICMP TTL
-     * exceed, IP header and orginal ICMP request */
-    iPacketSize = MAX_REC_SIZE + datasize;
+    iFromLen = sizeof(pInfo->source);
 
-    iFromLen = sizeof(source);
-
-#ifdef DBG
-    _tprintf(_T("receiving packet. Available buffer, %d bytes\n"), iPacketSize);
-#endif /* DBG */
+    DebugPrint(_T("Receiving packet. Available buffer, %d bytes... "), MAX_PING_PACKET_SIZE);
 
     /* monitor icmpSock for incomming connections */
     FD_ZERO(&readFDS);
-    FD_SET(icmpSock, &readFDS);
+    FD_SET(pInfo->icmpSock, &readFDS);
 
     /* set timeout values */
-    timeVal.tv_sec  = iTimeOut / 1000;
-    timeVal.tv_usec = iTimeOut % 1000;
+    timeVal.tv_sec  = pInfo->iTimeOut / 1000;
+    timeVal.tv_usec = pInfo->iTimeOut % 1000;
 
-    iSelRet = select(0, &readFDS, NULL, NULL, &timeVal);
+    iSelRet = select(0,
+                     &readFDS,
+                     NULL,
+                     NULL,
+                     &timeVal);
 
-    if ((iSelRet != SOCKET_ERROR) && (iSelRet != 0))
+    if (iSelRet == SOCKET_ERROR)
     {
-        iSockRet = recvfrom(icmpSock,              //socket
-                           (char *)&recvpacket,    //buffer
-                           iPacketSize,            //size of buffer
-                           0,                      //flags
-                           (SOCKADDR *)&source,    //source address
-                           &iFromLen);             //pointer to address length
-        /* get time packet was recieved */
-        lTimeEnd = GetTime();
-    /* if socket timed out */
+        DebugPrint(_T("select() failed in sendPacket() %d\n"), WSAGetLastError());
     }
-    else if (iSelRet == 0)
+    else if (iSelRet == 0) /* if socket timed out */
     {
         _tprintf(_T("   *  "));
-        return 1;
     }
-    else if (iSelRet == SOCKET_ERROR)
+    else if ((iSelRet != SOCKET_ERROR) && (iSelRet != 0))
     {
-        _tprintf(_T("select() failed in sendPacket() %d\n"), WSAGetLastError());
-        return -1;
-    }
-
-
-    if (iSockRet == SOCKET_ERROR)
-    {
-        _tprintf(_T("recvfrom failed: %d\n"), WSAGetLastError());
-        return -2;
+        iSockRet = recvfrom(pInfo->icmpSock,            // socket
+                           (char *)pInfo->RecvPacket,  // buffer
+                           MAX_PING_PACKET_SIZE,        // size of buffer
+                           0,                           // flags
+                           (SOCKADDR *)&pInfo->source,  // source address
+                           &iFromLen);                  // address length
+
+        if (iSockRet != SOCKET_ERROR)
+        {
+            /* get time packet was recieved */
+            pInfo->lTimeEnd = GetTime(pInfo);
+            DebugPrint(_T("reveived %d bytes\n"), iSockRet);
+            bRet = TRUE;
+        }
+        else
+        {
+            DebugPrint(_T("recvfrom failed: %d\n"), WSAGetLastError());
+        }
     }
-#ifdef DBG
-    else
-    _tprintf(_T("reveived %d bytes\n"), iSockRet);
-#endif /* DBG */
 
-    return 0;
+    return bRet;
 }
 
 
-
-/*
- *
- * Cast the IPv4 packet to an echo reply and to a TTL exceed.
- * Check the 'type' field to establish what was recieved, and
- * ensure the packet is related to the originating process.
- * It all is well, print the time taken for the round trip.
- *
- */
-static INT DecodeResponse(INT iPacketSize, USHORT iSeqNum)
+static INT
+DecodeResponse(PAPPINFO pInfo)
 {
-    unsigned short header_len = recvpacket.h_len * 4;
-    /* cast the recieved packet into an ECHO reply and a TTL Exceed so we can check the ID*/
-    ECHO_REPLY_HEADER *IcmpHdr = (ECHO_REPLY_HEADER *)((char*)&recvpacket + header_len);
-    TTL_EXCEED_HEADER *TTLExceedHdr = (TTL_EXCEED_HEADER *)((char *)&recvpacket + header_len);
+    unsigned short header_len = pInfo->RecvPacket->h_len * 4;
+
+    /* 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 (iPacketSize < header_len + ICMP_MIN_SIZE)
+    if (PACKET_SIZE < header_len + ICMP_MIN_SIZE)
     {
-        _tprintf(_T("too few bytes from %s\n"), inet_ntoa(dest.sin_addr));
+        DebugPrint(_T("too few bytes from %s\n"), inet_ntoa(pInfo->dest.sin_addr));
         return -2;
     }
 
@@ -589,131 +391,294 @@ static INT DecodeResponse(INT iPacketSize, USHORT iSeqNum)
            case TTL_EXCEEDED :
                 if (TTLExceedHdr->OrigIcmpHeader.id != (USHORT)GetCurrentProcessId())
                 {
-                /* FIXME */
-                /* we've picked up a packet not related to this process
-                 * probably from another local program. We ignore it */
-#ifdef DGB
-                    _tprintf(_T("header id,  process id  %d"), TTLExceedHdr->OrigIcmpHeader.id, GetCurrentProcessId());
-#endif /* DBG */
-                    //_tprintf(_T("oops ");
+                /* 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"), (lTimeEnd - lTimeStart) / TicksPerMs.QuadPart);
+                _tprintf(_T("%3ld ms"), (ULONG)((pInfo->lTimeEnd - pInfo->lTimeStart) / pInfo->TicksPerMs.QuadPart));
                 return 0;
+
            case ECHO_REPLY :
                 if (IcmpHdr->icmpheader.id != (USHORT)GetCurrentProcessId())
                 {
-                /* FIXME */
-                /* we've picked up a packet not related to this process
-                 * probably from another local program. We ignore it */
-#ifdef DGB
-                    _tprintf(_T("\nPicked up wrong packet. icmpheader.id = %d and process id = %d"), IcmpHdr->icmpheader.id, GetCurrentProcessId());
-#endif /* DBG */
-                    //_tprintf(_T("oops ");
+                /* 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 %d, process id  %d"), IcmpHdr->icmpheader.id, GetCurrentProcessId());
                     return -1;
                 }
-                _tprintf(_T("%3Ld ms"), (lTimeEnd - lTimeStart) / TicksPerMs.QuadPart);
+                _tprintf(_T("%3ld ms"), (ULONG)((pInfo->lTimeEnd - pInfo->lTimeStart) / pInfo->TicksPerMs.QuadPart));
                 return 1;
+
            case DEST_UNREACHABLE :
                 _tprintf(_T("  *  "));
                 return 2;
-           default :
-                /* unknown ICMP packet */
-                return -3;
     }
+
+    return 0;
 }
 
 
-/*
- *
- * Get the system time using preformance counters if available,
- * otherwise fall back to GetTickCount()
- *
- */
+static BOOL
+AllocateBuffers(PAPPINFO pInfo)
+{
+    pInfo->SendPacket = (PECHO_REPLY_HEADER)HeapAlloc(GetProcessHeap(),
+                                                      0,
+                                                      sizeof(ECHO_REPLY_HEADER) + PACKET_SIZE);
+    if (!pInfo->SendPacket)
+        return FALSE;
+
+    pInfo->RecvPacket = (PIPv4_HEADER)HeapAlloc(GetProcessHeap(),
+                                                0,
+                                                sizeof(IPv4_HEADER) + PACKET_SIZE);
+    if (!pInfo->RecvPacket)
+    {
+        HeapFree(GetProcessHeap(),
+                 0,
+                 pInfo->SendPacket);
 
-static LONGLONG GetTime(VOID)
+        return FALSE;
+    }
+
+    return TRUE;
+}
+
+
+static INT
+Driver(PAPPINFO pInfo)
 {
-    LARGE_INTEGER Time;
+    INT iHopCount = 1;              // hop counter. default max is 30
+    BOOL bFoundTarget = FALSE;      // Have we reached our destination yet
+    INT iRecieveReturn;             // RecieveReturn return value
+    PECHO_REPLY_HEADER icmphdr;
+    INT iTTL = 1;
+
+    INT ret = -1;
 
-    if (bUsePerformanceCounter)
+    //temps for getting host name
+    CHAR cHost[256];
+    CHAR cServ[256];
+    CHAR *ip;
+
+    SetupTimingMethod(pInfo);
+
+    if (AllocateBuffers(pInfo) &&
+        ResolveHostname(pInfo) &&
+        CreateSocket(pInfo))
     {
-        if (QueryPerformanceCounter(&Time) == 0)
+        /* print tracing info to screen */
+        _tprintf(_T("\nTracing route to %s [%s]\n"), cHostname, cDestIP);
+        _tprintf(_T("over a maximum of %d hop"), pInfo->iMaxHops);
+        pInfo->iMaxHops > 1 ? _tprintf(_T("s:\n\n")) : _tprintf(_T(":\n\n"));
+
+        /* run until we hit either max hops, or find the target */
+        while ((iHopCount <= pInfo->iMaxHops) &&
+               (bFoundTarget != TRUE))
         {
-            Time.u.LowPart = (DWORD)GetTickCount();
-            Time.u.HighPart = 0;
-            return (LONGLONG)Time.u.LowPart;
+            USHORT iSeqNum = 0;
+            INT i;
+
+            _tprintf(_T("%3d   "), iHopCount);
+
+            /* run 3 pings for each hop */
+            for (i = 0; i < 3; i++)
+            {
+                if (SetTTL(pInfo->icmpSock, iTTL) != TRUE)
+                {
+                    DebugPrint(_T("error in Setup()\n"));
+                    return ret;
+                }
+
+                PreparePacket(pInfo, iSeqNum);
+
+                if (SendPacket(pInfo) != SOCKET_ERROR)
+                {
+                    BOOL bAwaitPacket = FALSE; // indicates whether we have recieved a good packet
+
+                    do
+                    {
+                        /* Receive replies until we get a successful read, or a fatal error */
+                        if ((iRecieveReturn = ReceivePacket(pInfo)) < 0)
+                        {
+                            /* FIXME: consider moving this into RecievePacket */
+                            /* check the seq num in the packet, if it's bad wait for another */
+                            WORD hdrLen = pInfo->RecvPacket->h_len * 4;
+                            icmphdr = (ECHO_REPLY_HEADER *)((char*)&pInfo->RecvPacket + hdrLen);
+                            if (icmphdr->icmpheader.seq != iSeqNum)
+                            {
+                                _tprintf(_T("bad sequence number!\n"));
+                                continue;
+                            }
+                        }
+
+                        if (iRecieveReturn)
+                        {
+                            DecodeResponse(pInfo);
+                        }
+                        else
+                            /* packet timed out. Don't wait for it again */
+                            bAwaitPacket = FALSE;
+
+                    } while (bAwaitPacket);
+                }
+
+                iSeqNum++;
+                _tprintf(_T("   "));
+            }
+
+            if(pInfo->bResolveAddresses)
+            {
+                INT iNameInfoRet;               // getnameinfo return value
+               /* gethostbyaddr() and getnameinfo() are
+                * unimplemented in ROS at present.
+                * Alex has advised he will be implementing getnameinfo.
+                * I've used that for the time being for testing in Windows*/
+
+                  //ip = inet_addr(inet_ntoa(source.sin_addr));
+                  //host = gethostbyaddr((char *)&ip, 4, 0);
+
+                  ip = inet_ntoa(pInfo->source.sin_addr);
+
+                  iNameInfoRet = getnameinfo((SOCKADDR *)&pInfo->source,
+                                             sizeof(SOCKADDR),
+                                             cHost,
+                                             256,
+                                             cServ,
+                                             256,
+                                             NI_NUMERICSERV);
+                  if (iNameInfoRet == 0)
+                  {
+                     /* if IP address resolved to a hostname,
+                      * print the IP address after it */
+                      if (lstrcmpA(cHost, ip) != 0)
+                          _tprintf(_T("%s [%s]"), cHost, ip);
+                      else
+                          _tprintf(_T("%s"), cHost);
+                  }
+                  else
+                  {
+                      DebugPrint(_T("error: %d"), WSAGetLastError());
+                      DebugPrint(_T(" getnameinfo failed: %d"), iNameInfoRet);
+                  }
+
+            }
+            else
+               _tprintf(_T("%s"), inet_ntoa(pInfo->source.sin_addr));
+
+            _tprintf(_T("\n"));
+
+            /* check if we've arrived at the target */
+            if (strcmp(cDestIP, inet_ntoa(pInfo->source.sin_addr)) == 0)
+                bFoundTarget = TRUE;
+            else
+            {
+                iTTL++;
+                iHopCount++;
+                Sleep(500);
+            }
         }
+        _tprintf(_T("\nTrace complete.\n"));
+        ret = 0;
     }
-    else
-    {
-            Time.u.LowPart = (DWORD)GetTickCount();
-            Time.u.HighPart = 0;
-            return (LONGLONG)Time.u.LowPart;
-    }
-    return Time.QuadPart;
+
+    return ret;
 }
 
 
-/*
- *
- * Calculate packet checksum.
- *
- */
-static WORD CheckSum(PUSHORT data, UINT size)
+static VOID
+Cleanup(PAPPINFO pInfo)
 {
-    DWORD dwSum = 0;
-
-    while (size > 1)
-    {
-        dwSum += *data++;
-        size -= sizeof(USHORT);
-    }
+    if (pInfo->icmpSock)
+        closesocket(pInfo->icmpSock);
 
-    if (size)
-        dwSum += *(UCHAR*)data;
+    WSACleanup();
 
-    dwSum = (dwSum >> 16) + (dwSum & 0xFFFF);
-    dwSum += (dwSum >> 16);
+    if (pInfo->SendPacket)
+        HeapFree(GetProcessHeap(),
+                 0,
+                 pInfo->SendPacket);
 
-    return (USHORT)(~dwSum);
+    if (pInfo->RecvPacket)
+        HeapFree(GetProcessHeap(),
+                 0,
+                 pInfo->RecvPacket);
 }
 
 
-/*
- *
- * print program usage to screen
- *
- */
-static VOID Usage(VOID)
+#if defined(_UNICODE) && defined(__GNUC__)
+static
+#endif
+int _tmain(int argc, LPCTSTR argv[])
 {
-    _tprintf(_T("\nUsage: tracert [-d] [-h maximum_hops] [-j host-list] [-w timeout] target_name\n\n"
-                "Options:\n"
-                "    -d                 Do not resolve addresses to hostnames.\n"
-                "    -h maximum_hops    Maximum number of hops to search for target.\n"
-                "    -j host-list       Loose source route along host-list.\n"
-                "    -w timeout         Wait timeout milliseconds for each reply.\n\n"));
+    PAPPINFO pInfo;
+    WSADATA wsaData;
+    int ret = -1;
 
-    /* temp notes to stop user questions until getnameinfo/gethostbyaddr and getsockopt are implemented */
-    _tprintf(_T("NOTES\n-----\n"
-           "- Setting TTL values is not currently supported in ReactOS, so the trace will\n"
-           "  jump straight to the destination. This feature will be implemented soon.\n"
-           "- Host info is not currently available in ReactOS and will fail with strange\n"
-           "  results. Use -d to force it not to resolve IP's.\n"
-           "- For testing purposes, all should work as normal in a Windows environment\n\n"));
-}
+    pInfo = (PAPPINFO)HeapAlloc(GetProcessHeap(),
+                                HEAP_ZERO_MEMORY,
+                                sizeof(APPINFO));
+    if (pInfo)
+    {
+        pInfo->bResolveAddresses = TRUE;
+        pInfo->iMaxHops = 30;
+        pInfo->iTimeOut = 1000;
 
+        if (ParseCmdline(argc, argv, pInfo))
+        {
+            if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
+            {
+                DebugPrint(_T("WSAStartup failed.\n"));
+            }
+            else
+            {
+                ret = Driver(pInfo);
+                Cleanup(pInfo);
+            }
+        }
 
+        HeapFree(GetProcessHeap(),
+                 0,
+                 pInfo);
+    }
 
-/*
- *
- * Program entry point
- *
- */
-int main(int argc, char* argv[])
+    return ret;
+}
+
+
+#if defined(_UNICODE) && defined(__GNUC__)
+/* HACK - MINGW HAS NO OFFICIAL SUPPORT FOR wmain()!!! */
+int main( int argc, char **argv )
 {
-    if (!ParseCmdline(argc, argv)) return -1;
+    WCHAR **argvW;
+    int i, j, Ret = 1;
+
+    if ((argvW = malloc(argc * sizeof(WCHAR*))))
+    {
+        /* convert the arguments */
+        for (i = 0, j = 0; i < argc; i++)
+        {
+            if (!(argvW[i] = malloc((strlen(argv[i]) + 1) * sizeof(WCHAR))))
+            {
+                j++;
+            }
+            swprintf(argvW[i], L"%hs", argv[i]);
+        }
 
-    Driver();
+        if (j == 0)
+        {
+            /* no error converting the parameters, call wmain() */
+            Ret = wmain(argc, (LPCTSTR *)argvW);
+        }
 
-    return 0;
+        /* free the arguments */
+        for (i = 0; i < argc; i++)
+        {
+            if (argvW[i])
+                free(argvW[i]);
+        }
+        free(argvW);
+    }
+
+    return Ret;
 }
+#endif