让libTorrent优先选择教育网IPv6地址

猫在教育网机子上挂PT, 但是机子同时是内网的IPv6网关(HE的/64), 结果rtorrent每次返回给tracker的&ipv6=后面总是我在HE的2001:470:xx:xx::1, 和教育网PT上其他用户之间连接效果差爆了.

在折腾了几种workaround无效后, 最后不得不改libtorrent的代码了! 由于ipv6部分本身就是由一个patch加上去的, 我只需要对这个patch再patch一次即可:
安装过程就不再赘述了, 满网上都是的…

--- A/libtorrent-0.12.6-ipv6-07.patch	2011-08-19 00:02:03.603735921 +0800
+++ B/libtorrent-0.12.6-ipv6-07.patch	2011-08-19 00:36:14.824983083 +0800
@@ -417,7 +417,7 @@
 diff -Nur libtorrent-0.12.6.orig/src/net/local_addr.cc libtorrent-0.12.6/src/net/local_addr.cc
 --- libtorrent-0.12.6.orig/src/net/local_addr.cc	1970-01-01 01:00:00.000000000 +0100
 +++ libtorrent-0.12.6/src/net/local_addr.cc	2010-07-23 14:44:51.000000000 +0200
-@@ -0,0 +1,336 @@
+@@ -0,0 +1,340 @@
 +// libTorrent - BitTorrent library
 +// Copyright (C) 2005-2007, Jari Sundell
 +//
@@ -502,29 +502,33 @@
 +#ifdef RAK_USE_INET6
 +// IPv6 priority, from highest to lowest:
 +//
-+//  1. Global address (2000::/16 not in 6to4 or Teredo)
-+//  2. 6to4 (2002::/16)
-+//  3. Teredo (2001::/32)
-+//  4. Empty/INADDR_ANY (::)
-+//  5. Everything else (link-local, ULA, etc.)
++//  1. cernet & cernet2 address (2001:250::/32 and 2001:da8::/32)
++//  2. Other global address (2000::/16 not in 6to4 or Teredo)
++//  3. 6to4 (2002::/16)
++//  4. Teredo (2001::/32)
++//  5. Empty/INADDR_ANY (::)
++//  6. Everything else (link-local, ULA, etc.)
 +int get_priority_ipv6(const in6_addr& addr) {
 +  const uint32_t *addr32 = reinterpret_cast(addr.s6_addr);
 +  if (addr32[0] == htonl(0) &&
 +      addr32[1] == htonl(0) &&
 +      addr32[2] == htonl(0) &&
 +      addr32[3] == htonl(0)) {
-+    return 4;
++    return 5;
 +  }
 +  if (addr32[0] == htonl(0x20010000)) {
-+    return 3;
++    return 4;
 +  }
 +  if ((addr32[0] & htonl(0xffff0000)) == htonl(0x20020000)) {
-+    return 2;
++    return 3;
 +  }
-+  if ((addr32[0] & htonl(0xe0000000)) == htonl(0x20000000)) {
++  if (addr32[0] == htonl(0x20010250) || addr32[0] == htonl(0x20010da8)) {
 +    return 1;
 +  }
-+  return 5;
++  if ((addr32[0] & htonl(0xe0000000)) == htonl(0x20000000)) {
++    return 2;
++  }
++  return 6;
 +}
 +#endif
 +

9 thoughts on “让libTorrent优先选择教育网IPv6地址”

    1. HE的v6 endpoint是VPN服务器出口
      我通过教育网IPv6连到vpn服务器, 然后把整个tunnel建在教育网内网…

Leave a Reply

Your email address will not be published. Required fields are marked *

QR Code Business Card