IPv6 Scope:Link连接问题

Linux中设置的IPv6地址有两种类型,一种是Scope:Global,另一种为Scope:Link

。后者是有MAC地址通过一定的格式转换出来的全球唯一的本地链路地址。

~ # ifconfig
eth0      Link encap:Ethernet  HWaddr 1C:C3:16:63:B1:72  
          inet addr:192.168.9.51  Bcast:0.0.0.0  Mask:255.255.252.0
          inet6 addr: 2001:f80:754::152/64 Scope:Global
          inet6 addr: fe80::1ec3:16ff:fe63:b172/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:7131257 errors:0 dropped:823 overruns:0 frame:0
          TX packets:2902060 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:3880346601 (3.6 GiB)  TX bytes:1387657987 (1.2 GiB)
          Interrupt:55 

eth1      Link encap:Ethernet  HWaddr 1C:C3:16:63:B1:73  
          inet addr:192.168.10.200  Bcast:0.0.0.0  Mask:255.255.255.0
          inet6 addr: fe80::1ec3:16ff:fe63:b173/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:2111508 errors:0 dropped:1068 overruns:0 frame:0
          TX packets:103 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:160857475 (153.4 MiB)  TX bytes:19518 (19.0 KiB)
          Interrupt:55 Base address:0x4000 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:10 errors:0 dropped:0 overruns:0 frame:0
          TX packets:10 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:1294 (1.2 KiB)  TX bytes:1294 (1.2 KiB)

项目中需要连接到对方的物理IPv6地址,但是在实际使用中发现两个问题,1、不能直接ping通。2、ping通了但是connect不上。这是因为,Scope:Link地址必须绑定网卡。

root@IT-PC-135:/home/# ping6 fe80::1ec3:16ff:fe21:44bf
ping: unknown host fe80::1ec3:16ff:fe21:44bf

root@IT-PC-135:/home/# ping6 -I eth0 fe80::1ec3:16ff:fe21:44bf
PING fe80::1ec3:16ff:fe21:44bf(fe80::1ec3:16ff:fe21:44bf) from fe80::922b:34ff:fe4e:70f4 eth0: 56 data bytes
64 bytes from fe80::1ec3:16ff:fe21:44bf: icmp_seq=1 ttl=64 time=1.85 ms
64 bytes from fe80::1ec3:16ff:fe21:44bf: icmp_seq=2 ttl=64 time=0.868 ms
64 bytes from fe80::1ec3:16ff:fe21:44bf: icmp_seq=3 ttl=64 time=1.45 ms

实际代码中的解决办法呢?同理,需要再connect之前绑定网卡,具体实现代码为:

setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE, "eth0", sizeof("eth0"));

但是这里就出现了一个问题,如果是双网卡要绑定eth0呢还是eth1呢?哈哈,这里还没有想到好的办法,暂时就是遍历绑定网卡。

猜你喜欢

转载自blog.csdn.net/y7u8t6/article/details/79531801