1

Platform

Winows 11

What I want to do

I'm writing a UDP ping tool by using rfc1122 - 3.2.2.1 Destination Unreachable, which is the same method as traceroute UDP mode on Linux. Here is the procedure:

  1. Send UDP packet to target host
  2. The target host recv the UDP packet (but on a non-service port)
  3. The target host send a ICMP port unreachable packet back (type 3, code 3)
  4. Recv ICMP port unreachable packet from target host

Problem

According to Microsoft document - recv, recv() should return SOCKET_ERROR when getting the ICMP port unreachable, but it doesn't.

Checks

ICMP packet actually comes. (by WireShark)

Pseudo Code

int main(void)
{
    SOCKET sk;
    struct sockaddr_in dst_sockaddr;
    char buf[1280];
    int ret;

    /* specified the destination */
    dst_sockaddr.sin_family = AF_INET;
    dst_sockaddr.sin_port = MY_DST_PORT;  // a non-service port
    dst_sockaddr.sin_addr.S_un.S_addr = MY_DST_IP;

    /* create socket */
    sk = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    connect(sk, (struct sockaddr *)&dst_addr, sizeof(struct sockaddr_in));  // for using recv()

    /* send */
    send(sk, "hello", 5, 0);

    /* recv */
    ret = recv(sk, buf, sizeof(buf), 0);  // Blocked here!!
    printf("recv() returns %d\n", ret);

    closesocket(sk);
    return 0;
}

Similar Issues from Others

  1. x/net/icmp: listen icmp in Windows not work properly
    Not for UDP socket, it's for raw socket.
  2. Windows UDP sockets: recvfrom() fails with error 10054
    The command SIO_UDP_CONNRESET is for Windows XP. (ref)

Questions

I want to get SOCKET_ERROR if I get a ICMP port unreachable packet. Is there any details I miss? Or some socket options, IO controls I need to enable? Thanks for reading my question.

Defn_Lee
  • 21
  • 1
  • 5

1 Answers1

1

Possible reason (in my case):
ICMP unreachable packets blocked by firewall (even WireShark captures it).

Solution to this case:
Add allow inbound ICMPv4 (type3, code3) and ICMPv6 (type1, code4) traffic (for all apps) to firewall whitelist. (Specified an app isn't effective)

References:
WireShark: Ethernet capture setup
Can firewall block packets visible in Wireshark?

Defn_Lee
  • 21
  • 1
  • 5