1

Currently trying to implement a simple ping program to teach myself about network programming in C# and .NET.

I have managed to initialize a raw socket and correctly build an ICMP echo request packet. When running my program, Wireshark confirms that I am sending an Echo Request to the desired destination, however the remote machine will never send back an echo reply. I have tried sending to multiple machines all with the same result (and each of these machines can be pinged using the Windows ping utility). My code goes like this:

IcmpPacket echoReq = new IcmpPacket;
/*Some code to initialize packet*/
rawSocket.Send(echoReq, destinationIP); //syntax may be wrong, dont have the code infront of me sorry
rawSocket.ReceiveFrom(buffer, remoteEndpoint);

If anyone could suggest any reasons why the remote machines do not send any reply, I'd be very grateful.

Jeff LaFay
  • 12,882
  • 13
  • 71
  • 101
Mr. Spice
  • 1,552
  • 1
  • 15
  • 15
  • 1
    There must be something wrong with the packet even though Wireshark identifies it as an ICMP echo request. The most likely culprit that comes to mind is the header checksum (both IP & ICMP). Have you checked those? Wireshark should tell you if it thinks they are right or not. – Celada Jan 12 '12 at 16:07
  • 2
    Are there any differences between the requests you send and the requests the windows ping utility sends? You can sniff those with wireshark as well. If you spot the difference you'll probably find your issue. – AVee Jan 12 '12 at 16:08
  • You may find examining Microsoft's implementation of `Ping` to be informative. See `\Source\.Net\4.0\DEVDIV_TFS\Dev10\Releases\RTMRel\ndp\fx\src\Net\System\Net\NetworkInformation\ping.cs` from [the reference source](http://referencesource.microsoft.com/netframework.aspx) – Brian Jan 12 '12 at 18:13
  • Turned out that the checksum was not being correctly assigned to my packet. Thanks very much for everyone's suggestions! – Mr. Spice Jan 26 '12 at 21:08

2 Answers2

1

It's hard to know for sure from the information in your question. There are just too many things that can go wrong. But here are a few that I would start checking through.

  • The ICMP packet could be incorrectly formatted. I would use wireshark to compare the result of your custom ping packet to that of a known functioning utility to see if there are any differences
  • The destinationIP and remoteEndpoint values could point to different addresses. Seems unlikely but wanted to call it out
  • The IP in question could be simply rejecting the ping request. I would verify with another tools that it is returning pings
  • The firewall could be getting in the way. I would disable it temporarily and then rerun my program to see if that was the cause.
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 1
    I would recommend using the framework [`Ping`](http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.aspx) class implementation to verify the last two bullet points. If the framework `Ping` works, it's most likely a bug in your application. If the framework `Ping` fails, it's most likely a configuration problem. – Brian Jan 12 '12 at 18:18
0

Do you have to build your own packet? There is the Ping class otherwise

http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.aspx

EDIT:

Ping pingSender = new Ping ();
PingReply reply = pingSender.Send ("www.contoso.com");

if (reply.Status == IPStatus.Success)
{
  Console.WriteLine ("Address: {0}", reply.Address.ToString ());
  Console.WriteLine ("RoundTrip time: {0}", reply.RoundtripTime);
  Console.WriteLine ("Time to live: {0}", reply.Options.Ttl);
  Console.WriteLine ("Don't fragment: {0}", reply.Options.DontFragment);
  Console.WriteLine ("Buffer size: {0}", reply.Buffer.Length);
}
else
{
  Console.WriteLine (reply.Status);
}
trembon
  • 748
  • 7
  • 15
  • 1
    The OP appears to be doing this as a learning excercise. Using an existing class would defeat the purpose – JaredPar Jan 12 '12 at 16:07