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:
- Send UDP packet to target host
- The target host recv the UDP packet (but on a non-service port)
- The target host send a ICMP port unreachable packet back (type 3, code 3)
- 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
- x/net/icmp: listen icmp in Windows not work properly
Not for UDP socket, it's for raw socket. - Windows UDP sockets: recvfrom() fails with error 10054
The commandSIO_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.