Click here to Skip to main content
Licence CPOL
First Posted 22 Feb 2006
Views 49,809
Bookmarked 34 times

ICMP the ping-tracert

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

About the Author

zitun

Web Developer

Belgium Belgium

Member

Discover www.plugandtable.com
 
You'll be surprise about this incredible tool !


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 PinmemberOmgShitNo3:06 14 Dec '09  
GeneralRe: My vote of 1 Pinmemberwknopf15:40 29 Jan '11  
GeneralFILL ALL FIELDS OF ICMP ECHO REQUEST PACKET Pinmembernasirkhanjadoon0:11 29 Feb '08  
GeneralRe: FILL ALL FIELDS OF ICMP ECHO REQUEST PACKET PinmemberRichardMant1:41 13 Jul '08  
in relpy to your request,
 
an ICMP packet actually only contains the ICMP Header and the data following it, what you're wanting is to get the structures for the Ethernet header, IP header, and ICMP header, create a raw socket via a PInvoke to ws2_32.dll by calling the socket() function with AF_INET as the base proto (to use the TCP/IP stack) type, SOCK_RAW as the socket type, and IPPROTO_RAW as the packet format type.
 
Once you have a pointer to a socket from that, you'll want to fill out each of the headers, copy them to a buffer long enough for the entire packet, and send them via sendto() with the socket as created above.
 
What you ask is not easy in C/C++, so it'll be even more difficult in C# as you'll have to set up all the structures, all the imports, and more. I hope this helps though.
 
The worst thing about the darkness is the light at the end - DX-MON

GeneralICMP Ping Bug PinmembertankSanju1:22 17 Nov '06  
QuestionWhat about this? PinPopularmemberneil young0:26 28 Feb '06  
AnswerRe: What about this? Pinmemberzitun6:09 28 Feb '06  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 23 Feb 2006
Article Copyright 2006 by zitun
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid