简体   繁体   English

连接到IPv4映射的IPv6地址失败

[英]connect to IPv4 mapped IPv6 address fails

I am attempting to use an IPv6 socket to connect to an IPv4 address using an IPv4 mapped IPv6 address, on linux (debian-lenny-64 2.6.26-2-amd64) 我正在尝试在Linux(debian-lenny-64 2.6.26-2-amd64)上使用IPv6套接字使用IPv4映射的IPv6地址连接到IPv4地址。

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

int main(int argc, void **argv)
{
    struct addrinfo *sa;
    struct addrinfo *ra;

    int err = getaddrinfo("2001:DB8::2", 0, 0, &sa);

    int fd = socket(sa->ai_family, SOCK_DGRAM, 0);

    int v6only = 0;     

    err = setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&v6only, sizeof(v6only)); 

    err = bind(fd, sa->ai_addr, sa->ai_addrlen);

    err = getaddrinfo("::ffff:192.168.0.1", "9", 0, &ra);

//  err = getaddrinfo("2001:DB8::1", "9", 0, &ra);
//  err = getaddrinfo("::ffff:127.0.0.1", "9", 0, &ra);

    err = connect(fd, (struct sockaddr *)ra->ai_addr, sizeof(struct sockaddr_in6));
}

(I've removed error testing from the pasted code) (我已从粘贴的代码中删除了错误测试)

2001:DB8::2 and 192.168.0.2 are local addresses (both on the same interface). 2001:DB8 :: 2和192.168.0.2是本地地址(都在同一接口上)。
2001:DB8::1 and 192.168.0.1 are the remote addresses (both on the same interface). 2001:DB8 :: 1和192.168.0.1是远程地址(都在同一接口上)。

I change the remote address for the connect call and get the following: 我更改了connect呼叫的远程地址并获得以下信息:

  • connect to ::ffff:127.0.0.1 success (localhost) 连接到:: ffff:127.0.0.1成功(本地主机)
  • connect to 2001:DB8::1 success (remote IPv6 address) 连接到2001:DB8 :: 1成功(远程IPv6地址)
  • connect to ::ffff:192.168.0.2 success (local IPv4 address) 连接到:: ffff:192.168.0.2成功(本地IPv4地址)
  • connect to ::ffff:192.168.0.1 failure (22 Invalid argument - remote IPv4) 连接到:: ffff:192.168.0.1失败(22个无效参数-远程IPv4)

If I change to doing IPv4 connects then the connects also work. 如果我更改为进行IPv4连接,则连接也可以使用。

I think there must be an issue with routing somewhere but I'm at a loss to work out what I need to change. 我认为在某处路由肯定存在问题,但是我很茫然,无法确定需要更改的内容。 Firstly should I in theory be able to do this? 首先,我理论上应该能够做到这一点吗?
Any ideas what is going wrong? 任何想法出了什么问题?

  • See the function "getaddrinfo()" as 参见函数“ getaddrinfo()”
      ~$ man getaddrinfo 
    .
  • The ' hints ' paramter of the function has attribute ' ai_flags ', if you set it to ' AI_PASSIVE '. 该函数的“ 提示 ”放慢参数有属性“ 填上ai_flags”,如果你把它设置为“AI_PASSIVE”。 It can work for both IPv4 and IPv6 addressing modes. 它可以同时用于IPv4和IPv6寻址模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM