Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

ICMP the ping-tracert

0.00/5 (No votes)
22 Feb 2006 1  
Make an ICMP request to make a ping or tracert.

Introduction

I needed a ping server that is fully configurable (weight of package, update period, timeout etc.), and while lots of solutions exist, none of them are free. Moreover, I wanted a tracert for some other reason: I wanted to learn how it works.

This is not an impressive project; however, it can give a helping hand to people who want to implement in a single command, a ping or a tracert.

First look!

Console.WriteLine("**************TEST PING****************\r\n");
IcmpRequest myRequest = 
    new IcmpRequest(new ToolsCommandRequest(
        "www.yahoo.com","Hello",CommandType.ping,4,32,1000,1000,128));

Console.WriteLine("\r\n\r\n***********TEST TRACERT***********\r\n");
myRequest = new IcmpRequest(new ToolsCommandRequest("www.yahoo.com",
                     "Hello",CommandType.tracert,4,32,1000,1000,128));
**********************TEST PING**********************

  Pinging www.yahoo.com [68.142.226.38] with 32 bytes of data:
  Reply from 68.142.226.38: bytes=32 time=94ms TTL 51
  Reply from 68.142.226.38: bytes=32 time=78ms TTL 51
  Reply from 68.142.226.38: bytes=32 time=78ms TTL 51
  Reply from 68.142.226.38: bytes=32 time=78ms TTL 51

**********************TEST TRACERT*******************

  Tracing route to www.yahoo.com [68.142.226.38]
  over a maximum of 128 hops:
  1 1 ms 254.16.30.217.globaltt.com [217.30.16.254]
  2 1 ms t2a4-p5-1.be-bru.eu.bt.net [166.49.224.149]
  3 1 ms t2c1-ge7-0.be-bru.eu.bt.net [166.49.190.43]
  4 16 ms t2c2-p3-1.uk-lon2.eu.bt.net [166.49.208.41]
  5 16 ms t2c1-ge6-1.uk-lon2.eu.bt.net [166.49.164.213]
  6 1 ms 166-49-195-133.eu.bt.net [166.49.195.133]
  7 78 ms t2c1-p4-0.us-nyb.eu.bt.net [166.49.208.161]
  8 78 ms ixp1-p6-0.us-nyb.eu.bt.net [166.49.163.54]
  9 109 ms equinixexchange-nyc.yahoo.com [206.223.131.16]
  10 78 ms ge-2-0-9.p558.pat1.dce.yahoo.com [216.115.97.17]
  11 94 ms ge-3-1-0.p440-msr1.re1.yahoo.com [216.115.96.189]
  12 78 ms t-2-1.bas1.re2.yahoo.com [206.190.33.89]
  13 94 ms p7.www.re2.yahoo.com [68.142.226.38]

  Trace complete.

In just two lines you have what you wanted.

ICMP

You can find a lot of documentation on ICMP packet format, usage, etc., on the web. However, what you need to know is: All ICMP packets have 32 bits of header (8 bits for type, 8 bits for code and 16 bits for checksum). When you send an ICMP packet you have to define the TTL (time to live). It's the maximum number of hops that the request can make. If this number is too small to reach the host, an error occurs, and the ping won't be successful. Each time the packet transits to a host, the TTL is reduced by one unit.

By the way, you can define the TTL in Windows command prompt using the command: ping www.yahoo.com -i 5. You can also define it using this tool by creating the object ToolsCommandRequest. Here are its parameters:

//Default values


//Period in millisecond between 2 ping

public int periodUpdate = 1000;

//Type of request, can be ping or tracert                            

public CommandType myCommandType = CommandType.tracert;
 
//Host we want to reach

public string host = "127.0.0.1";

//Weight of the packet in byte (should be minimum 1)

public int weightPacket = 32;

//Not used in this demo

public string sessionID;

//Number of times the request will be done on the host

public int nbrEcho = 4;

//Time out of the ping. If we don't have answer

//after that time, the ping triggers an error

public int timeout = 1000;

//TTL (see above for explanation)

public int timeToLiveMax = 128;

When you send an ICMP request, you must define the time to live in the IP level (not into the ICMP packet). But, when you receive the answer from the ICMP packet, the TTL is written into the ICMP packet (actually it's defined by the 8th byte).

Tracert or traceroute

The tracert uses the TTL property. The algorithm is as follows:

Send a ping with a TTL = 2, 
    the ping failed and return the host reached.
Send a ping with a TTL = 3, if the ping failed, 
    it return the host reached. If the ping succeed, 
    the tracert is completed
Send a ping with a TTL = 4, if the ping failed, 
    it return the host reached. If the ping succeed, 
    the tracert is completed
...

Conclusion

The mechanism of the ICMP DLL is quite simple. I took it from here and modified it to get the weight packet and the time to live properties. With that module you can easily configure a monitoring system based on ping.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here