设置UDP套接字选项:
on = IP_PMTUDISC_DO; // sets the Don't-Fragment flag
setsockopt(fd, SOL_IP, IP_MTU_DISCOVER, &on, sizeof(on)); // Set or receive the Path MTU Discovery setting for a socket. When enabled, Linux will perform Path MTU Discovery
setsockopt(fd, SOL_IP, IP_RECVERR, &on, sizeof(on)); // Enable extended reliable error message passing. When enabled on a datagram socket, all generated errors will be queued in a per-socket error queue.
// When the user receives an error from a socket operation, the errors can be received by calling recvmsg(2) with the MSG_ERRQUEUE flag set.
setsockopt(fd, SOL_IP, IP_RECVTTL, &on, sizeof(on)); // When this flag is set, pass a IP_TTL control message with the time to live field of the received packet as a byte. Not supported for SOCK_STREAM sockets.
看来,然后我就可以从那里得到TTL_Exceeded错误消息在sock_extended_err结构形式复制。