使用IN6ADDR_SETV4MAPPED和双堆栈套接字

时间:2013-05-26 19:31:08

标签: sockets tcp ipv6 ipv4

这是Connecting IPv4 client to IPv6 server: connection refused的延续。我正在尝试使用双堆栈套接字并尝试了解哪些setsockopt与IPV6_V6ONLY有用。在链接的问题上,我被告知“如果您还将服务器绑定到IPv6映射的IPv4地址,则将IPV6_V6ONLY设置为0会很有用”。我在下面做了这个,并期望我的服务器能够接受来自IPv6和IPv4客户端的连接。但令人震惊的是,当我使用V4和V6插槽运行我的客户端时,两者都无法连接!

有人可以告诉我我做错了什么,还是我误解了IPv6双栈功能?

服务器:

void ConvertToV4MappedAddressIfNeeded(PSOCKADDR pAddr)
{
// if v4 address, convert to v4 mapped v6 address
if (AF_INET == pAddr->sa_family)
{
    IN_ADDR In4addr;
    SCOPE_ID scope = INETADDR_SCOPE_ID(pAddr);
    USHORT port = INETADDR_PORT(pAddr);
    In4addr = *(IN_ADDR*)INETADDR_ADDRESS(pAddr);
    ZeroMemory(pAddr, sizeof(SOCKADDR_STORAGE));
    IN6ADDR_SETV4MAPPED(
        (PSOCKADDR_IN6)pAddr,
        &In4addr,
        scope,
        port
        );
    }
} 

addrinfo* result, hints;

memset(&hints, 0, sizeof hints); 
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;

int nRet = getaddrinfo("powerhouse", "82", &hints, &result);

SOCKET sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);

int no = 0;
if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&no, sizeof(no)) != 0)
    return -1;

ConvertToV4MappedAddressIfNeeded(result->ai_addr);

if (bind(sock, result->ai_addr, 28/*result->ai_addrlen*/) ==  SOCKET_ERROR)
    return -1;

if (listen(sock, SOMAXCONN) == SOCKET_ERROR)
    return -1;

SOCKET sockClient = accept(sock, NULL, NULL);
printf("Got one!\n");

客户端:

addrinfo* result, *pCurrent, hints;
char szIPAddress[INET6_ADDRSTRLEN];

memset(&hints, 0, sizeof hints);    // Must do this!
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;

const char* pszPort = "82";

if (getaddrinfo("powerhouse", "82", &hints, &result) != 0)
    return -1;

SOCKET sock = socket(AF_INET, result->ai_socktype, result->ai_protocol);
int nRet = connect(sock, result->ai_addr, result->ai_addrlen);  

1 个答案:

答案 0 :(得分:7)

我的C技能有点生疏,所以这是一个用Python编写的反例。我的本地IPv4地址是37.77.56.75,这就是我要绑定的地址。我让它尽可能简单地专注于概念。

这是服务器端:

#!/usr/bin/env python
import socket

# We bind to an IPv6 address, which contains an IPv6-mapped-IPv4-address,
# port 5000 and we leave the flowinfo (an ID that identifies a flow, not used
# a lot) and the scope-id (basically the interface, necessary if using
# link-local addresses)
host = '::ffff:37.77.56.75'
port = 5000
flowinfo = 0
scopeid = 0
sockaddr = (host, port, flowinfo, scopeid)

# Create an IPv6 socket, set IPV6_V6ONLY=0 and bind to the mapped address
sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM, 0)
sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)
sock.bind(sockaddr)

# Listen and accept a connection
sock.listen(0)
conn = sock.accept()

# Print the remote address
print conn[1]

这里我们绑定代码中的IPv6地址,但该地址实际上是IPv6映射的IPv4地址,因此实际上我们绑定到IPv4地址。在查看ie netstat时可以看到这一点:

$ netstat -an | fgrep 5000
tcp4       0      0  37.77.56.75.5000       *.*                    LISTEN     

然后我们可以使用IPv4客户端连接到此服务器:

#!/usr/bin/env python
import socket

# Connect to an IPv4 address on port 5000
host = '37.77.56.75'
port = 5000
sockaddr = (host, port)                   

# Create an IPv4 socket and connect
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) 
conn = sock.connect(sockaddr)

服务器将使用IPv6地址表示向我们显示连接的人:

('::ffff:37.77.56.76', 50887, 0, 0)

在此示例中,我从IPv4主机37.77.56.76连接,并选择要连接的端口50887

在此示例中,我们仅侦听IPv4地址(使用IPv6套接字,但它仍然是IPv4地址),因此仅IPv6客户端将无法连接。具有IPv4和IPv6的客户端当然可以使用IPv6套接字和IPv6映射的IPv4地址,但它实际上不会使用IPv6,只是IPv4连接的IPv6表示。

双栈服务器必须:

  1. 侦听通配符地址,这将使操作系统接受任何地址(IPv4和IPv6)上的连接
  2. 侦听IPv6地址和IPv4地址(通过创建IPv4套接字,或创建IPv6套接字并侦听IPv6映射的IPv4地址,如上所示)
  3. 使用通配符地址是最简单的。只需使用上面的服务器示例并替换主机名:

    # We bind to the wildcard IPv6 address, which will make the OS listen on both
    # IPv4 and IPv6
    host = '::'
    port = 5000
    flowinfo = 0
    scopeid = 0
    sockaddr = (host, port, flowinfo, scopeid)
    

    我的Mac OS X框显示为:

    $ netstat -an | fgrep 5000
    tcp46      0      0  *.5000                 *.*                    LISTEN     
    

    注意tcp46表示它侦听两个地址系列。不幸的是,在Linux上它只显示tcp6,即使在收听这两个家庭时也是如此。

    现在是最复杂的例子:收听多个套接字。

    #!/usr/bin/env python
    import select
    import socket
    
    # We bind to an IPv6 address, which contains an IPv6-mapped-IPv4-address
    sockaddr1 = ('::ffff:37.77.56.75', 5001, 0, 0)
    
    sock1 = socket.socket(socket.AF_INET6, socket.SOCK_STREAM, 0)
    sock1.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)
    sock1.bind(sockaddr1)
    sock1.listen(0)
    
    # And we bind to a real IPv6 address
    sockaddr2 = ('2a00:8640:1::224:36ff:feef:1d89', 5001, 0, 0)
    
    sock2 = socket.socket(socket.AF_INET6, socket.SOCK_STREAM, 0)
    sock2.bind(sockaddr2)
    sock2.listen(0)
    
    # Select sockets that become active
    sockets = [sock1, sock2]
    readable, writable, exceptional = select.select(sockets, [], sockets)
    for sock in readable:
        # Accept the connection
        conn = sock.accept()
    
        # Print the remote address
        print conn[1]
    

    运行此示例时,两个套接字都可见:

    $ netstat -an | fgrep 5000
    tcp6       0      0  2a00:8640:1::224.5000  *.*                    LISTEN     
    tcp4       0      0  37.77.56.75.5000       *.*                    LISTEN     
    

    现在,仅限IPv6的客户端可以连接到2a00:8640:1::224:36ff:feef:1d89,而仅限IPv4的客户端可以连接到37.77.56.75。双栈客户端可以选择他们想要使用的协议。