I have an application that uses sendto() with UDP to check resources on different ports. How can I get the ICMP port unreachable packet using recvfrom()?
My recvfrom() is like this:
SOCKADDR_STORAGE saFrom;
int nFromLen = sizeof( SOCKADDR_STORAGE );
nReceived = recvfrom ( Socket, Buffer, BufferSize, 0, ( struct sockaddr * ) &saFrom, (socklen_t *)&nFromLen );
When an ICMP port unreachable occurs, nReceived = -1, and WSAGetLastError() returns WSAECONNRESET(10054).
But since I'm checking on different ports, I'd like a way of determining which port triggered the ICMP port unreachable message. How can I determine that? (Maybe I can set some socket options?)
Edit: As per Len's suggestion(use ICMP only), I tried to create a socket by socket (AF_INET, SOCK_RAW, IPPROTO_ICMP), then call recvfrom() on it. recvfrom() returns 10022(WSAEINVAL) error because the socket is not bound. How should I fix that?