1

根据我从手册页的理解,当设置 MSG_DONTROUTE 时,数据包应该只发送到直接连接到源的目的地。但是,当我尝试将数据包发送到间接连接的目的地(需要路由)时,send() 仍然返回一个正值。我有什么误解吗,谢谢?

这是我的测试代码:

int main (int argc, char *argv[]) {

    const char* ip = argv[1] ? argv[1] : "74.125.31.100"; // google

    int fd = socket (AF_INET, SOCK_STREAM, 0);
    if (fd == -1)
        ErrAndExit("socket");

    struct sockaddr_in addr;
    memset (&addr, 0, sizeof(sockaddr_in));
    addr.sin_family = AF_INET;
    addr.sin_port = htons (80);
    if (inet_pton (AF_INET, ip, &addr.sin_addr) != 1)
        ErrAndExit ("inet_pton");

    if (connect (fd, (struct sockaddr*) &addr, sizeof(addr)) == -1)
        ErrAndExit("connect");

    // int optval = 1;
    // int optlen = sizeof(optval);
    // if (setsockopt (fd, SOL_SOCKET, SO_DONTROUTE, (void*) &optval, optlen) == -1)
    //     ErrAndExit ("setsockopt");

    ssize_t written = send (fd, "apple", 6, MSG_DONTROUTE);
    if (written == -1)
        ErrAndExit ("send");
    else
        printf ("wrote %ld bytes\n\n", written);

    return 0;
}
4

0 回答 0