当:: 1在接口1上时,为什么我可以将此套接字绑定到:: 1%2?

时间:2014-11-06 14:09:02

标签: c unix tcp

我试图理解sin6_scope_id如何在UNIX C套接字编程中处理IPv6地址。具体来说,我写了这个试图绑定到::1%2的程序(所以如果我做对了那么接口2上的环回地址),即使我的环回地址实际上是在接口1上。

我希望这会失败。但它成功结合了。为什么?

以下是ifconfig -a返回的前3个接口:

$ ifconfig -a

lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
    options=3<RXCSUM,TXCSUM>
    inet6 ::1 prefixlen 128
    inet 127.0.0.1 netmask 0xff000000
    inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1
    nd6 options=1<PERFORMNUD>
gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
stf0: flags=0<> mtu 1280

您可以使用以下命令编译此程序:

cc -Wall -Wextra main.c

以下是评论来源:

#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {
    // Allow any IP address on the interface with scope ID of 2.
    const char *hostname = "::1%2";

    // Say we want to bind on port 80 (for http).
    const char *servname = "1337";

    // Store some information about the IP address wanted.
    struct addrinfo hints;

    // Save addresses in here.
    struct addrinfo *addr_list_item = NULL;

    // Tell `getaddrinfo` that we want an address for IPv6 TCP.
    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = AF_INET6;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    if (getaddrinfo(hostname, servname, &hints, &addr_list_item) != 0) {
        printf("Could not read addresses.\n");
        exit(1);
    }

    // Create a socket and bind it to the address we found before. This should fail
    // but for some reason I don't understand it doesn't.
    if (addr_list_item) {
        int sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
        if (sock != -1) {
            if (bind(sock, addr_list_item->ai_addr, addr_list_item->ai_addrlen) != -1) {
                printf("Binded succesfully!\n");
            } else {
                perror(NULL);
            }
            close(sock);
        }
    }

    // Release memory.
    freeaddrinfo(addr_list_item);

    return (0);
}

1 个答案:

答案 0 :(得分:1)

根据您的unix版本,%2可能会被忽略。

在某些IBM systems上,文档说:

  

上述IPv6文本表单可能包含附加的区域指示符(如果前面带有%字符)和/或附加的前缀长度(如果前面带有/字符)。在这些情况下,%或/ 将被视为与空终止符相同。