日期:2009-06-28  浏览次数:20385 次

/******************************************************************************************************************

*    Class:                Trace
*    Description:        Traces path of an ip packet with its respond time
*    Author:                Sanjay Ahuja
*    Date:                5/15/2002
*    Copyright?            ?2002, Sanjay Ahuja (lparam@hotmail.com). Use it as you want till you leave my name intact
/******************************************************************************************************************/


using System;
using System.Net;
using System.Net.Sockets;


//ICMP constants
struct ICMPConstants
{
    public const int ICMP_ECHOREPLY= 0;            // Echo reply query
    public const int ICMP_TIMEEXCEEDED= 11;        // TTL exceeded error
    public const int ICMP_ECHOREQ=    8;            // Echo request query
    public const int MAX_TTL= 256;                // Max TTL
}

//ICMP header, size is 8 bytes
struct ICMP
{
    public byte    type;                // Type
    public byte    code;                // Code
    public ushort    checksum;        // Checksum
    public ushort    id;                // Identification
    public ushort    seq;            // Sequence
}

// ICMP Echo Request, size is 12+ 32 (PACKET_SIZE as defined in class Trace)= 44 bytes
struct REQUEST
{
    public ICMP    m_icmp;
    public byte    []m_data;
}

class Trace
{
    const int PACKET_SIZE= 32;

    public static void Main(string[] args)
    {
        try
        {
            //verify command line
            if(args.Length== 0)
            {
                Console.WriteLine("usage: trace <hostname>");
                return;