Click here to Skip to main content
15,881,757 members
Articles / Programming Languages / C#
Article

ICMP the ping-tracert

Rate me:
Please Sign up or sign in to vote.
3.24/5 (16 votes)
22 Feb 2006CPOL2 min read 88.9K   5.2K   42   8
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!

C#
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));
C#
**********************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:

C#
//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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Belgium Belgium
Discover www.plugandtable.com

You'll be surprise about this incredible tool !

Comments and Discussions

 
QuestionAdministrator Pin
dhenry_il13-Dec-12 4:01
dhenry_il13-Dec-12 4:01 
GeneralMy vote of 1 Pin
OmgShitNo14-Dec-09 3:06
OmgShitNo14-Dec-09 3:06 
GeneralRe: My vote of 1 Pin
wknopf29-Jan-11 15:40
wknopf29-Jan-11 15:40 
GeneralFILL ALL FIELDS OF ICMP ECHO REQUEST PACKET Pin
nasirkhanjadoon29-Feb-08 0:11
nasirkhanjadoon29-Feb-08 0:11 
GeneralRe: FILL ALL FIELDS OF ICMP ECHO REQUEST PACKET Pin
Rachel Mant13-Jul-08 1:41
Rachel Mant13-Jul-08 1:41 
GeneralICMP Ping Bug Pin
tankSanju17-Nov-06 1:22
tankSanju17-Nov-06 1:22 
QuestionWhat about this? Pin
neil young28-Feb-06 0:26
neil young28-Feb-06 0:26 
AnswerRe: What about this? Pin
zitun28-Feb-06 6:09
zitun28-Feb-06 6:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.