Determining IP:port corresponding to an ICMP port unreachable

From: Erik De Bonte (
Date: Mon Jul 30 2001 - 18:08:56 EST

When an ICMP port unreachable message is received and corresponds to a UDP
socket, is there a way to determine the corresponding unreachable IP and
port? I'm able to retrieve the IP, but not the port. From looking through
the kernel source, it appears that the port is never extracted from the
payload section of the ICMP message. If this is indeed a limitation of the
kernel, is there a plan to "fix" it in the future?

Here are the details on my particular situation:

I'm working on a game server which interacts with a large number of clients
via a single UDP socket. Occasionally, one of the clients will die without
sending a disconnect message. When this happens, I'd like to remove the
client as quickly as possible to avoid leaving a ghost in the world that
other players will see. In the worst case scenario, the session will time
out after the server hasn't heard from the client in x seconds. However, if
I watch for ICMP port unreachable messages, I should frequently be able to
react more quickly.

With Winsock, this is easy to do. Recvfrom fails, an error code tells me
that an ICMP port unreachable was received, and the address parameter of
recvfrom is filled in with the dead client's IP and port. On Linux (I'm
using 2.2.16, but 2.4.x code appears to be the same in this respect),
recvfrom fails and errno is set to ECONNREFUSED indicating an ICMP port
unreachable was received. However, the address is not filled in. I'm able
to retrieve the IP via recvmsg with the MSG_ERRQUEUE flag (and the
IP_RECVERR sockopt), but the port that it gives me is bogus.

I apologize if this seems too application specific for linux-kernel, but
this appears to be a limitation of the kernel, and I haven't been able to
find any info elsewhere.


Erik L. De Bonte
Lead Server Programmer
LithTech, Inc. -
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to
More majordomo info at
Please read the FAQ at

This archive was generated by hypermail 2b29 : Tue Jul 31 2001 - 21:00:47 EST