Search code examples
c#udpwindows-firewall

Windows firewall blocking ICMP port unreachable?


I'm attempting to determine whether or not a remote host is listening on a particular UDP port number. I do this (in C#) as follows:

 Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  int sent = socket.SendTo(bufsend, bufsend.Length, SocketFlags.None, endpoint);
  int recv = socket.ReceiveFrom(bufrecv, ref endpoint);

If no one is listening, I get an ICMP "port unreachable" which is reported on the receive as a socket exception with ErrorCode WSAECONNRESET (10054). All that makes sense, BUT it only happens when the Windows firewall is disabled. If the firewall is enabled -- even if I make an exception for the program -- I get ErrorCode WSAETIMEDOUT (10060), presumably because the firewall has eaten the ICMP "port unreachable".

Any suggestions? I've examined the firewall "advanced security" settings, but to no avail.

Thanks...


Solution

  • See the comment above. You must apply the rule opening the firewall to "destination unreachable" messages to ALL programs, not just to the executable that implements the SendTo / ReceiveFrom test.