ICMP using raw socket not working on Vista

G

Guest

Hello

I have some code (see below) that sends an ICMP echo request and that works
fine on Windows XP and 2003 but fails on Vista. The code sends the echo
request but times out trying to receive the echo reply. If I use MS network
monitor I can see the echo request being sent and the echo reply being
returned but my app doesn't receive it.

I've turned off the Vista firewall and have confirmed that using ping.exe to
the same address works. Also, if I use the .NET Ping class this also works.
So, there must be something wrong with my code although, as I said, this
works fine on the other OS's.

My code is below. A SocketException is thrown in the ReceiveFrom.

How do I fix this code so it works on Vista too?

Thanks
Gavin

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace IcmpTest {
class Program {
// ICMP Echo request
static readonly byte[] bytesToSend = {
0x08, 0x00, 0x28, 0x01, 0xde, 0xfc, 0x00, 0x01,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
};

static void Main(string[] args) {
try {
Socket socket = new Socket(AddressFamily.InterNetwork,
SocketType.Raw, ProtocolType.Icmp);
socket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout, 5000);
socket.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.IpTimeToLive, 32);

socket.SendTo(bytesToSend, (EndPoint)new
IPEndPoint(IPAddress.Parse("64.26.22.64"), 0));

byte[] responseBytes = new byte[1024];

EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
int bytesReceivedCount = socket.ReceiveFrom(responseBytes,
ref remoteEndPoint);

Console.WriteLine(bytesReceivedCount);
}

catch (Exception ex) {
// ReceiveFrom throws a SocketException -
// A connection attempt failed because the connected party
did not properly respond
// after a period of time, or established connection failed
because connected host has failed to respond
Console.WriteLine(ex.ToString());
}
}
}
}
 
V

Vadym Stetsiak

Hello, Gavin!

..NET ping class uses Win32 IP Helper Functions from Iphlpapi.dll.
Internally, it can use something other then sockets...


Did you try to specify ProtocolType.Ip when creating socket?
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw,
ProtocolType.Ip);

--
With best regards, Vadym Stetsiak.
Blog: http://vadmyst.blogspot.com



You wrote on Fri, 7 Sep 2007 03:12:04 -0700:

G> Hello

G> I have some code (see below) that sends an ICMP echo request and that
G> works fine on Windows XP and 2003 but fails on Vista. The code sends
G> the echo request but times out trying to receive the echo reply. If
G> I use MS network monitor I can see the echo request being sent and
G> the echo reply being returned but my app doesn't receive it.

G> I've turned off the Vista firewall and have confirmed that using
G> ping.exe to the same address works. Also, if I use the .NET Ping
G> class this also works.
G> So, there must be something wrong with my code although, as I said,
G> this works fine on the other OS's.

G> My code is below. A SocketException is thrown in the ReceiveFrom.

G> How do I fix this code so it works on Vista too?

G> Thanks
G> Gavin

G> using System;
G> using System.Collections.Generic;
G> using System.Net;
G> using System.Net.Sockets;
G> using System.Text;

G> namespace IcmpTest {
G> class Program {
G> // ICMP Echo request static readonly byte[]
G> bytesToSend = {
G> 0x08, 0x00, 0x28, 0x01, 0xde, 0xfc, 0x00, 0x01,
G> 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
G> 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11,
G> 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a,
G> 0x1b, 0x1c, 0x1d, 0x1e, 0x1f };

G> static void Main(string[] args) {
G> try {
G> Socket socket = new
G> Socket(AddressFamily.InterNetwork,
G> SocketType.Raw, ProtocolType.Icmp);
G> socket.SetSocketOption(SocketOptionLevel.Socket,
G> SocketOptionName.ReceiveTimeout, 5000);
G> socket.SetSocketOption(SocketOptionLevel.IP,
G> SocketOptionName.IpTimeToLive, 32);

G> socket.SendTo(bytesToSend, (EndPoint)new
G> IPEndPoint(IPAddress.Parse("64.26.22.64"), 0));

G> byte[] responseBytes = new byte[1024];

G> EndPoint remoteEndPoint = new
G> IPEndPoint(IPAddress.Any, 0);
G> int bytesReceivedCount =
G> socket.ReceiveFrom(responseBytes, ref remoteEndPoint);

G> Console.WriteLine(bytesReceivedCount);
G> }

G> catch (Exception ex) {
G> // ReceiveFrom throws a SocketException -
G> // A connection attempt failed because the connected
G> party did not properly respond // after a period of
G> time, or established connection failed because connected host has
G> failed to respond
G> Console.WriteLine(ex.ToString());
G> }
G> }
G> }
G> }
 
G

Guest

Hello Vadym

I need to use ProtocolType.Icmp as I need access to the IP headers.

Regards
Gavin

Vadym Stetsiak said:
Hello, Gavin!

..NET ping class uses Win32 IP Helper Functions from Iphlpapi.dll.
Internally, it can use something other then sockets...


Did you try to specify ProtocolType.Ip when creating socket?
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw,
ProtocolType.Ip);

--
With best regards, Vadym Stetsiak.
Blog: http://vadmyst.blogspot.com



You wrote on Fri, 7 Sep 2007 03:12:04 -0700:

G> Hello

G> I have some code (see below) that sends an ICMP echo request and that
G> works fine on Windows XP and 2003 but fails on Vista. The code sends
G> the echo request but times out trying to receive the echo reply. If
G> I use MS network monitor I can see the echo request being sent and
G> the echo reply being returned but my app doesn't receive it.

G> I've turned off the Vista firewall and have confirmed that using
G> ping.exe to the same address works. Also, if I use the .NET Ping
G> class this also works.
G> So, there must be something wrong with my code although, as I said,
G> this works fine on the other OS's.

G> My code is below. A SocketException is thrown in the ReceiveFrom.

G> How do I fix this code so it works on Vista too?

G> Thanks
G> Gavin

G> using System;
G> using System.Collections.Generic;
G> using System.Net;
G> using System.Net.Sockets;
G> using System.Text;

G> namespace IcmpTest {
G> class Program {
G> // ICMP Echo request static readonly byte[]
G> bytesToSend = {
G> 0x08, 0x00, 0x28, 0x01, 0xde, 0xfc, 0x00, 0x01,
G> 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
G> 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11,
G> 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a,
G> 0x1b, 0x1c, 0x1d, 0x1e, 0x1f };

G> static void Main(string[] args) {
G> try {
G> Socket socket = new
G> Socket(AddressFamily.InterNetwork,
G> SocketType.Raw, ProtocolType.Icmp);
G> socket.SetSocketOption(SocketOptionLevel.Socket,
G> SocketOptionName.ReceiveTimeout, 5000);
G> socket.SetSocketOption(SocketOptionLevel.IP,
G> SocketOptionName.IpTimeToLive, 32);

G> socket.SendTo(bytesToSend, (EndPoint)new
G> IPEndPoint(IPAddress.Parse("64.26.22.64"), 0));

G> byte[] responseBytes = new byte[1024];

G> EndPoint remoteEndPoint = new
G> IPEndPoint(IPAddress.Any, 0);
G> int bytesReceivedCount =
G> socket.ReceiveFrom(responseBytes, ref remoteEndPoint);

G> Console.WriteLine(bytesReceivedCount);
G> }

G> catch (Exception ex) {
G> // ReceiveFrom throws a SocketException -
G> // A connection attempt failed because the connected
G> party did not properly respond // after a period of
G> time, or established connection failed because connected host has
G> failed to respond
G> Console.WriteLine(ex.ToString());
G> }
G> }
G> }
G> }
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top