筆記-TCP/IP IPv6遇到的問題
linux平臺
問題:IPv6地址可ping通,但connect不上
原因:linux防火牆,開放端口,不僅有iptables(IPv4),還有ip6tables(IPv6)
問題: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
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
解決:在connect之前綁定網卡。具體實現代碼爲:
setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE, "eth0", sizeof("eth0"));
但是又出現了一個問題,如果是雙網卡要綁定eth0呢還是eth1呢?這裏還沒有想到好的辦法,暫時就是遍歷綁定網卡。