⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tracert.c

📁 ReactOS是一些高手根据Windows XP的内核编写出的类XP。内核实现机理和API函数调用几乎相同。甚至可以兼容XP的程序。喜欢研究系统内核的人可以看一看。
💻 C
📖 第 1 页 / 共 2 页
字号:
 /*
 * 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>
 *
 */

#include "tracert.h"

//#define TRACERT_DBG

CHAR cHostname[256];            // target hostname
CHAR cDestIP[18];               // target IP


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
}


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"));

    _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"));
}


static BOOL
ParseCmdline(int argc,
             LPCTSTR argv[],
             PAPPINFO pInfo)
{
    INT i;

    if (argc < 2)
    {
       Usage();
       return FALSE;
    }
    else
    {
        for (i = 1; i < argc; i++)
        {
            if (argv[i][0] == _T('-'))
            {
                switch (argv[i][1])
                {
                    case _T('d'):
                        pInfo->bResolveAddresses = FALSE;
                    break;

                    case _T('h'):
                        _stscanf(argv[i+1], _T("%d"), &pInfo->iMaxHops);
                    break;

                    case _T('j'):
                        _tprintf(_T("-j is not yet implemented.\n"));
                    break;

                    case _T('w'):
                        _stscanf(argv[i+1], _T("%d"), &pInfo->iTimeOut);
                    break;

                    default:
                    {
                        _tprintf(_T("%s is not a valid option.\n"), argv[i]);
                        Usage();
                        return FALSE;
                    }
                }
            }
            else
               /* copy target address */
               _tcsncpy(cHostname, argv[i], 255);
        }
    }

    return TRUE;
}


static WORD
CheckSum(PUSHORT data,
         UINT size)
{
    DWORD dwSum = 0;

    while (size > 1)
    {
        dwSum += *data++;
        size -= sizeof(USHORT);
    }

    if (size)
        dwSum += *(UCHAR*)data;

    dwSum = (dwSum >> 16) + (dwSum & 0xFFFF);
    dwSum += (dwSum >> 16);

    return (USHORT)(~dwSum);
}


static VOID
SetupTimingMethod(PAPPINFO pInfo)
{
    LARGE_INTEGER PerformanceCounterFrequency;

    /* check if performance counters are available */
    pInfo->bUsePerformanceCounter = QueryPerformanceFrequency(&PerformanceCounterFrequency);

    if (pInfo->bUsePerformanceCounter)
    {
        /* restrict execution to first processor on SMP systems */
        if (SetThreadAffinityMask(GetCurrentThread(), 1) == 0)
            pInfo->bUsePerformanceCounter = FALSE;

        pInfo->TicksPerMs.QuadPart  = PerformanceCounterFrequency.QuadPart / 1000;
        pInfo->TicksPerUs.QuadPart  = PerformanceCounterFrequency.QuadPart / 1000000;
    }
    else
    {
        pInfo->TicksPerMs.QuadPart = 1;
        pInfo->TicksPerUs.QuadPart = 1;
    }
}


static BOOL
ResolveHostname(PAPPINFO pInfo)
{
    HOSTENT *hp;
    ULONG addr;

    ZeroMemory(&pInfo->dest, sizeof(pInfo->dest));

    /* if address is not a dotted decimal */
    if ((addr = inet_addr(cHostname))== INADDR_NONE)
    {
       if ((hp = gethostbyname(cHostname)) != 0)
       {
          //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);
          return FALSE;
       }
    }
    else
    {
        pInfo->dest.sin_addr.s_addr = addr;
        pInfo->dest.sin_family = AF_INET;
    }

    _tcscpy(cDestIP, inet_ntoa(pInfo->dest.sin_addr));

    return TRUE;
}


static LONGLONG
GetTime(PAPPINFO pInfo)
{
    LARGE_INTEGER Time;

    /* Get the system time using preformance counters if available */
    if (pInfo->bUsePerformanceCounter)
    {
        if (QueryPerformanceCounter(&Time))
        {
            return Time.QuadPart;
        }
    }

    /* 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)
    {
       DebugPrint(_T("TTL setsockopt failed : %d. \n"), WSAGetLastError());
       return FALSE;
    }

    return TRUE;
}


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);

        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 */
    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      = iSeqNum;

    /* calculate checksum of packet */
    pInfo->SendPacket->icmpheader.checksum  = CheckSum((PUSHORT)&pInfo->SendPacket,
                                                       sizeof(ICMP_HEADER) + PACKET_SIZE);
}


static INT
SendPacket(PAPPINFO pInfo)
{
    INT iSockRet;

    DebugPrint(_T("\nsending packet of %d bytes... "), PACKET_SIZE);

    /* get time packet was sent */
    pInfo->lTimeStart = GetTime(pInfo);

    iSockRet = sendto(pInfo->icmpSock,              //socket
                      (char *)pInfo->SendPacket,   //buffer
                      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();
            HeapFree(GetProcessHeap(), 0, pInfo);
            exit(-1);
        }
        else
        {
            DebugPrint(_T("sendto failed %d\n"), WSAGetLastError());
        }
    }
    else
    {
        DebugPrint(_T("sent %d bytes\n"), iSockRet);
    }

    return iSockRet;
}


static BOOL
ReceivePacket(PAPPINFO pInfo)
{
    TIMEVAL timeVal;
    FD_SET readFDS;
    INT iSockRet = 0, iSelRet;
    INT iFromLen;
    BOOL bRet = FALSE;

    iFromLen = sizeof(pInfo->source);

    DebugPrint(_T("Receiving packet. Available buffer, %d bytes... "), MAX_PING_PACKET_SIZE);

    /* monitor icmpSock for incomming connections */
    FD_ZERO(&readFDS);
    FD_SET(pInfo->icmpSock, &readFDS);

    /* set timeout values */
    timeVal.tv_sec  = pInfo->iTimeOut / 1000;
    timeVal.tv_usec = pInfo->iTimeOut % 1000;

    iSelRet = select(0,
                     &readFDS,
                     NULL,
                     NULL,
                     &timeVal);

    if (iSelRet == SOCKET_ERROR)
    {
        DebugPrint(_T("select() failed in sendPacket() %d\n"), WSAGetLastError());
    }
    else if (iSelRet == 0) /* if socket timed out */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -