C# Traceroute using .net framework

If you have ever tried to create a traceroute program using one of the few available ICMP libraries freely available for C# you may have run into some issues mainly to do with the ICMP checksum not being correct. It seems that as of .NET 2.0 framework that microsoft included a Ping class that makes it really easy to then use it to create a traceroute utility. Here is some basic code to create a traceroute utility.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public string Traceroute(string ipAddressOrHostName)
{
    IPAddress ipAddress = Dns.GetHostEntry(ipAddressOrHostName).AddressList[0];
    StringBuilder traceResults = new StringBuilder();
  
    using(Ping pingSender = new Ping())
    {
        PingOptions pingOptions = new PingOptions();
        Stopwatch stopWatch = new Stopwatch();
        byte[] bytes = new byte[32];
  
        pingOptions.DontFragment = true;
        pingOptions.Ttl = 1;
        int maxHops = 30;
  
  
  
        traceResults.AppendLine(
            string.Format(
                "Tracing route to {0} over a maximum of {1} hops:",
                ipAddress,
                maxHops));
  
        traceResults.AppendLine();
  
        for(int i = 1; i < maxHops + 1; i++)
        {
            stopWatch.Reset();
            stopWatch.Start();
            PingReply pingReply = pingSender.Send(
                ipAddress,
                5000,
                new byte[32], pingOptions);
  
            stopWatch.Stop();
  
            traceResults.AppendLine(
                string.Format("{0}\t{1} ms\t{2}",
                i,
                stopWatch.ElapsedMilliseconds,
                pingReply.Address));
  
            if(pingReply.Status == IPStatus.Success)
            {
                traceResults.AppendLine();
                traceResults.AppendLine("Trace complete."); break;
            }
  
            pingOptions.Ttl++;
        }
  
    }
  
    return traceResults.ToString();
}

 

 

Currently rated 3.4 by 5 people

  • Currently 3.4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments (5) -

  • thanks! just what I was looking for
  • Thanks for your code I'm using it in my asp.net application, the code runs fine when I'm testing in on my PC (localhost), but when the application is uploaded to a web server, the traceroute prints only ttls e.g.,:

    1  5555ms
    2  5244ms
    .. and so on.

    Can you kindly guide me as to what the problem can be?
  • i wonder why if you trace route on a hostname it returns some macs or something while doing it on ips works fine
  • For those who are like me not familiar with the Stopwatch function: it's in the System.Diagnostics class, so either create a using statement or precede the Stopwatch creation with System.Diagnostics.

    Thanks for the code!
  • RobRdam, thanks for the comment. I will fixup the code example so that it shows all the using statements and makes it easier to copy and paste the code as I had an issue myself just now trying to get the code example out Wink

Pingbacks and trackbacks (1)+

Add comment

Loading