c - udp 连接套接字上的 ICMP "destination unreachable"数据包

标签 c linux sockets network-programming udp

我已经用这个函数创建了连接的UDP套接字

/* Creates connected udp socket */
int
udp_connect( const char *host, const char *serv)
{
    int             sockfd, n;
    struct addrinfo hints, *res, *ressave;

    bzero(&hints, sizeof(struct addrinfo));
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_DGRAM;

    if ( ( n = getaddrinfo( host, serv, &hints, &res)) != 0)
        err_quit( "udp_connect error for %s, %s: %s",
                 host, serv, gai_strerror(n));
    ressave = res;
    do {
        sockfd = socket( res->ai_family, res->ai_socktype, res->ai_protocol);
        if ( sockfd < 0)
            continue;   /* ignore this one */
                /* The call to connect with a UDP socket does not send anything
                 * to the peer. If something is wrong ( the peer is unreachable
                 * or there is no server at the specified port), the caller 
                 * does not discover that until it sends 
                 * a datagram to the peer */
        if ( connect( sockfd, res->ai_addr, res->ai_addrlen) == 0)
            break;      /* success */
        Close( sockfd); /* ignore this one */
    } while ( ( res = res->ai_next) != NULL);

    if ( res == NULL)   /* errno set from final connect() */
        err_sys( "udp_connect error for %s, %s", host, serv);

    freeaddrinfo( ressave);
    return( sockfd);
}

我想测试一下当对等点实际上无法访问时它的行为如何。由于调用 connect() 无法产生此信息,因此我们需要实际发送一些内容。我将在以下代码片段中描述我要做什么以及我会得到什么:

printf( "sending to %s\n", Sock_ntop_host( sa, salen));
// prints: sending to 127.0.0.1

Sendto( sockfd, "", 1, 0, sa, salen);   /* send 1-byte datagram */
// prints: nbytes:1
// it is sent, I check via tcpdump or with Wireshark that datagram
// has been sent and ICMP "destination unreachable" comes back from host

printf( "sent, errno:%d,%s\n", errno, strerror(errno));
// prints: sent, errno:0,Success

n = Recvfrom( sockfd, recvline, MAXLINE, 0, NULL, NULL);
// never gets here
printf( "received n=%d\n", n);

Sendto 函数是 sendto 的包装器,仅打印错误并退出:

void
Sendto(int fd, const void *ptr, size_t nbytes, int flags,
       const struct sockaddr *sa, socklen_t salen)
{
    if ( sendto(fd, ptr, nbytes, flags, sa, salen) == -1)
        exit(-1);//err_sys("sendto error");
    printf( "nbytes:%d\n",nbytes); // prints: nbytes:1
}

ssize_t
Recvfrom(int fd, void *ptr, size_t nbytes, int flags,
         struct sockaddr *sa, socklen_t *salenptr)
{
    ssize_t     n;

    if ( (n = recvfrom(fd, ptr, nbytes, flags, sa, salenptr)) < 0)
        err_sys("recvfrom error");
    return(n);
}

因此,对 Recvfrom 的调用将永远阻塞,而 Sendto 返回 errno 以及代码 Success那么我应该如何编码以获得有关 ICMP 响应的通知?在套接字上没有超时的情况下这可能吗?

最佳答案

connect() 一个 UDP 套接字后,您可以使用 send()recv() 而不是 sendto( )recvfrom()recv() 将为已连接的套接字报告 ICMP 错误。

关于c - udp 连接套接字上的 ICMP "destination unreachable"数据包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24117719/

相关文章:

c - 将参数作为引用

c - C 中的堆没有释放

linux - Centos Apache - 在进行身份验证之前检测是否插入了智能卡

java - 如何在 Java 中监听 IP 和端口?与服务器套接字?

客户端重新连接到服务器后,从服务器向客户端发送 Java 消息

c - 程序需要加CFLAGS=-D_GNU_SOURCE才能编译,这个应该放哪里?

c - 在C中,如何创建多个子进程(不知道需要多少个)?

c - 如何使用 Linux 驱动程序使键盘 LED 闪烁?

linux - 如何让 Concourse Linux 机器读取可执行文件? (chmod 样式)

java - 安卓socket连接失败?