IPV6 绑定失败错误:无法分配请求的地址

IPV6 绑定失败错误:无法分配请求的地址

我已为 Ubuntu Linux 系统配置了以下接口并分配了 IPV6 地址,如下所示:

    **Eth0:** Link encap:Ethernet HWaddr 00:50:56:8d:57:64
    inet addr:192.168.254.10 Bcast:0.0.0.0 Mask:255.255.255.0
    inet6 addr: fe80::250:56ff:fe8d:5764/64 Scope:Link
    UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
    RX packets:74231424 errors:0 dropped:1 overruns:0 frame:0
    TX packets:400372550 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000
    RX bytes:24514286488 (24.5 GB) TX bytes:115992171490 (115.9 GB)

    **Eth1:** Link encap:Ethernet HWaddr 00:50:56:8d:7c:4c
    inet addr:192.168.1.10 Bcast:0.0.0.0 Mask:255.255.255.0
    inet6 addr: fe80::250:56ff:fe8d:7c4c/64 Scope:Link
    inet6 addr: fc00:1234:1::10/120 Scope:Global
    UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
    RX packets:434933479 errors:0 dropped:1 overruns:0 frame:0
    TX packets:39666183 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000
    RX bytes:126065364448 (126.0 GB) TX bytes:14437801257 (14.4 GB)

**Eth2:** Link encap:Ethernet HWaddr 00:50:56:8d:56:14
inet addr:192.168.2.10 Bcast:0.0.0.0 Mask:255.255.255.0
inet6 addr: fc00:1234:2::10/120 Scope:Global
inet6 addr: fe80::250:56ff:fe8d:5614/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:480068741 errors:0 dropped:0 overruns:0 frame:0
TX packets:34145702 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000
RX bytes:146795537550 (146.7 GB) TX bytes:10045338657 (10.0 GB) 

2.我想使用 IPV6 套接字进行套接字编程。我编写的代码片段如下:

struct sockaddr_in6 sin
Ipv6_fdr = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP)

bzero(&sin, sizeof(sin));  
sin.sin6_family = AF_INET6;  
sin.sin6_port = htons(2152);  
if ((setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int))) < 0)  
memcpy(&(sin.sin6_addr), "fc00:1234:1::10",sizeof(sin.sin6_addr));  
if ((bind(sock, (struct sockaddr*)&sin, sizeof(sin)))< 0)

3.编译成功后,出现 IPV6 绑定失败错误,错误编号和名称如下:

EADDRNOTAVAIL 99 /* Cannot assign requested address.

尝试 IPV6 绑定一定次数后,会抛出段错误。

如果有人能帮助我解决我在这里犯了什么错误以及为什么 IPV6 绑定失败,那就太好了?

答案1

线路

memcpy(&(sin.sin6_addr), "fc00:1234:1::10",sizeof(sin.sin6_addr));

是错误的;这绝对不是设置套接字地址和端口的方式。请参阅man 7 ipv6问题“如何将 sockaddr_in6::sin6_addr 字节顺序设置为网络字节顺序“在 StackOverflow 上。对于您的用例,您可能希望使用inet_pton将地址从文本转换为二进制形式。

相关内容