Click here to Skip to main content
6,291,722 members and growing! (15,132 online)
Email Password   helpLost your password?
General Programming » Internet / Network » General     Intermediate License: The Code Project Open License (CPOL)

ICMP the ping-tracert

By zitun

Make an ICMP request to make a ping or tracert.
C#.NET 1.1, Win2K, WinXP, Win2003VS.NET2003, Dev
Posted:22 Feb 2006
Views:32,977
Bookmarked:21 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
12 votes for this article.
Popularity: 3.37 Rating: 3.12 out of 5
3 votes, 25.0%
1
1 vote, 8.3%
2
1 vote, 8.3%
3
3 votes, 25.0%
4
4 votes, 33.3%
5

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

About the Author

zitun


Member
I finished 6 years ago my studies. That was a great period of my live. So I spent 5 years to become a civil engineer in computer science in Belgium.

Then I worked in telecommunication society and developped new applications for the technical people and the telecommunication engineer of my enterprise.

If we think about future, we can be sure that everything will goes on thru a computer. So I tried to make easier the life of people by automatised boring task and make live better in general.
Occupation: Web Developer
Location: Belgium Belgium

Other popular Internet / Network articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
GeneralFILL ALL FIELDS OF ICMP ECHO REQUEST PACKET Pinmembernasirkhanjadoon1:11 29 Feb '08  
GeneralRe: FILL ALL FIELDS OF ICMP ECHO REQUEST PACKET PinmemberRichardMant2:41 13 Jul '08  
GeneralICMP Ping Bug PinmembertankSanju2:22 17 Nov '06  
GeneralWhat about this? Pinmemberneil young1:26 28 Feb '06  
GeneralRe: What about this? Pinmemberzitun7:09 28 Feb '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 22 Feb 2006
Editor: Rinish Biju
Copyright 2006 by zitun
Everything else Copyright © CodeProject, 1999-2009
Web11 | Advertise on the Code Project