From e7e5805c56873ddfdcc09c407251185234caafbc Mon Sep 17 00:00:00 2001 From: Steven Siloti Date: Sun, 25 Mar 2018 13:31:26 -0700 Subject: [PATCH 1/3] support building python bindings for python 3 and 64 bit --- bindings/python/Jamfile | 37 +++++++++++++++++++++++++++++++++++-- bindings/python/setup.py | 29 +++++++++++++++++++++++++---- setup.py | 2 +- 3 files changed, 61 insertions(+), 7 deletions(-) mode change 100755 => 100644 setup.py diff --git a/bindings/python/Jamfile b/bindings/python/Jamfile index 64b6d246b..9539eb5cf 100644 --- a/bindings/python/Jamfile +++ b/bindings/python/Jamfile @@ -8,6 +8,8 @@ import modules ; use-project /torrent : ../.. ; BOOST_ROOT = [ modules.peek : BOOST_ROOT ] ; +# this is used to make bjam use the same version of python which is executing setup.py +LIBTORRENT_PYTHON_INTERPRETER = [ modules.peek : LIBTORRENT_PYTHON_INTERPRETER ] ; feature visibility : default hidden : composite ; feature.compose hidden : -fvisibility=hidden -fvisibility-inlines-hidden ; @@ -16,10 +18,20 @@ feature libtorrent-link : shared static : composite propagated ; feature libtorrent-python-pic : off on : composite propagated link-incompatible ; feature.compose on : -fPIC ; +# this is just to force boost build to pick the desired python target when using LIBTORRENT_PYTHON_INTERPRETER +feature libtorrent-python : on ; + +if $(LIBTORRENT_PYTHON_INTERPRETER) +{ + echo "using python interpreter at: " $(LIBTORRENT_PYTHON_INTERPRETER) ; + using python : : "$(LIBTORRENT_PYTHON_INTERPRETER)" : : : on ; +} + if $(BOOST_ROOT) { use-project /boost : $(BOOST_ROOT) ; alias boost_python : /boost/python//boost_python : : : $(BOOST_ROOT) ; + alias boost_python3 : /boost/python//boost_python3 : : : $(BOOST_ROOT) ; } else { @@ -40,9 +52,13 @@ else # the names are decorated in MacPorts lib boost_python : : darwin boost_python-mt : : $(boost-include-path) ; + lib boost_python3 : : darwin boost_python3-mt + : : $(boost-include-path) ; lib boost_python : : boost_python : : $(boost-include-path) ; + lib boost_python3 : : boost_python3 + : : $(boost-include-path) ; } @@ -79,16 +95,33 @@ rule libtorrent_linking ( properties * ) ECHO "WARNING: you cannot link statically against boost-python on linux, because it links against pthread statically in that case, which is not allowed" ; } + local boost_python_lib ; + + for local prop in $(properties) + { + switch $(prop) + { + case 2.* : boost_python_lib = boost_python ; + case 3.* : boost_python_lib = boost_python3 ; + } + } + + if ! $(boost_python_lib) + { + ECHO "WARNING: unknown python version" ; + boost_python_lib = boost_python ; + } + # linux must link dynamically against boost python because it pulls # in libpthread, which must be linked dynamically since we're building a .so # (the static build of libpthread is not position independent) if shared in $(properties) || linux in $(properties) { - result += boost_python/shared ; + result += $(boost_python_lib)/shared ; } else { - result += boost_python/static ; + result += $(boost_python_lib)/static ; } if shared in $(properties) diff --git a/bindings/python/setup.py b/bindings/python/setup.py index 482776ecf..3ea9c2c2d 100644 --- a/bindings/python/setup.py +++ b/bindings/python/setup.py @@ -74,15 +74,36 @@ if '--bjam' in sys.argv: file_ext = '.so' if platform.system() == 'Windows': - # msvc 9.0 (2008) is the official windows compiler for python 2.6 - # http://docs.python.org/whatsnew/2.6.html#build-and-c-api-changes - toolset = ' msvc-9.0' file_ext = '.pyd' + # See https://wiki.python.org/moin/WindowsCompilers for a table of msvc versions + # used for each python version + # Specify the full version number for 9.0 and 10.0 because apparently + # older versions of boost don't support only specifying the major number and + # there was only one version of msvc with those majors. + # Only specify the major for msvc-14 so that 14.1, 14.11, etc can be used. + # Hopefully people building with msvc-14 are using a new enough version of boost + # for this to work. + if sys.version_info[0:2] in ((2, 6), (2, 7), (3, 0), (3, 1), (3, 2)): + toolset = ' toolset=msvc-9.0' + elif sys.version_info[0:2] in ((3, 3), (3, 4)): + toolset = ' toolset=msvc-10.0' + elif sys.version_info[0:2] in ((3, 5), (3, 6)): + toolset = ' toolset=msvc-14' + else: + # unknown python version, lets hope the user has the right version of msvc configured + toolset = ' toolset=msvc' parallel_builds = ' -j%d' % multiprocessing.cpu_count() + if sys.maxsize > 2**32: + address_model = ' address-model=64' + else: + address_model = ' address-model=32' + # add extra quoting around the path to prevent bjam from parsing it as a list + # if the path has spaces + os.environ['LIBTORRENT_PYTHON_INTERPRETER'] = '"' + sys.executable + '"' # build libtorrent using bjam and build the installer with distutils - cmdline = 'b2 libtorrent-link=static boost-link=static release optimization=space stage_module --abbreviate-paths' + toolset + parallel_builds + cmdline = 'b2 libtorrent-link=static boost-link=static release optimization=space stage_module --abbreviate-paths' + address_model + toolset + parallel_builds print(cmdline) if os.system(cmdline) != 0: print('build failed') diff --git a/setup.py b/setup.py old mode 100755 new mode 100644 index 06f12e01a..28f3076b4 --- a/setup.py +++ b/setup.py @@ -2,4 +2,4 @@ import os os.chdir('bindings/python') -execfile('setup.py') +exec(open('setup.py').read()) From 6a2df1034af6e9ff6059e15124d6c981bd4ddab3 Mon Sep 17 00:00:00 2001 From: arvidn Date: Fri, 30 Mar 2018 01:04:21 +0300 Subject: [PATCH 2/3] add some asserts and checks preventing IPv6 addresses to make it into libtorrent when IPv6 support is disabled --- src/kademlia/dht_tracker.cpp | 6 ++++++ src/kademlia/routing_table.cpp | 3 +++ src/session_impl.cpp | 6 ++++++ 3 files changed, 15 insertions(+) diff --git a/src/kademlia/dht_tracker.cpp b/src/kademlia/dht_tracker.cpp index b535ccb62..1dd36d8a0 100644 --- a/src/kademlia/dht_tracker.cpp +++ b/src/kademlia/dht_tracker.cpp @@ -386,11 +386,17 @@ namespace libtorrent { namespace dht void dht_tracker::add_node(udp::endpoint node) { +#if !TORRENT_USE_IPV6 + TORRENT_ASSERT(node.address().is_v4()); +#endif m_dht.add_node(node); } void dht_tracker::add_router_node(udp::endpoint const& node) { +#if !TORRENT_USE_IPV6 + TORRENT_ASSERT(node.address().is_v4()); +#endif m_dht.add_router_node(node); } diff --git a/src/kademlia/routing_table.cpp b/src/kademlia/routing_table.cpp index 1ac00dedc..794cb2182 100644 --- a/src/kademlia/routing_table.cpp +++ b/src/kademlia/routing_table.cpp @@ -1153,6 +1153,9 @@ void routing_table::node_failed(node_id const& nid, udp::endpoint const& ep) void routing_table::add_router_node(udp::endpoint router) { +#if !TORRENT_USE_IPV6 + TORRENT_ASSERT(router.address().is_v4()); +#endif m_router_nodes.insert(router); } diff --git a/src/session_impl.cpp b/src/session_impl.cpp index d12df4698..36528fb80 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -3551,6 +3551,9 @@ retry: void session_impl::add_dht_node(udp::endpoint n) { TORRENT_ASSERT(is_single_thread()); +#if !TORRENT_USE_IPV6 + if (!n.address().is_v4()) return; +#endif if (m_dht) m_dht->add_node(n); else m_dht_nodes.push_back(n); @@ -5887,6 +5890,9 @@ retry: for (std::vector
::const_iterator i = addresses.begin() , end(addresses.end()); i != end; ++i) { +#if !TORRENT_USE_IPV6 + if (!i->is_v4()) continue; +#endif // router nodes should be added before the DHT is started (and bootstrapped) udp::endpoint ep(*i, port); if (m_dht) m_dht->add_router_node(ep); From 98d531359835ff4d8c8230313272df2ad6e34a0c Mon Sep 17 00:00:00 2001 From: arvidn Date: Sat, 24 Mar 2018 13:08:37 +0100 Subject: [PATCH 3/3] don't perform DNS lookups for the DHT bootstrap unless DHT is enabled --- ChangeLog | 1 + src/session_impl.cpp | 25 +++++++++++++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 7834e139f..b79799968 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,5 @@ + * don't perform DNS lookups for the DHT bootstrap unless DHT is enabled * fix issue where setting file/piece priority would stop checking * expose post_dht_stats() to python binding * fix backwards compatibility to downloads without partfiles diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 36528fb80..ee9e49bd0 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -895,7 +895,7 @@ namespace aux { } #ifndef TORRENT_DISABLE_DHT - if (need_update_dht) update_dht(); + if (need_update_dht) start_dht(); #endif #ifndef TORRENT_NO_DEPRECATE if (need_update_proxy) update_proxy(); @@ -5443,7 +5443,20 @@ retry: { #ifndef TORRENT_DISABLE_DHT if (m_settings.get_bool(settings_pack::enable_dht)) - start_dht(); + { + if (!m_settings.get_str(settings_pack::dht_bootstrap_nodes).empty() + && m_dht_router_nodes.empty()) + { + // if we have bootstrap nodes configured, make sure we initiate host + // name lookups. once these complete, the DHT will be started. + // they are tracked by m_outstanding_router_lookups + update_dht_bootstrap_nodes(); + } + else + { + start_dht(); + } + } else stop_dht(); #endif @@ -5452,6 +5465,8 @@ retry: void session_impl::update_dht_bootstrap_nodes() { #ifndef TORRENT_DISABLE_DHT + if (!m_settings.get_bool(settings_pack::enable_dht)) return; + std::string const& node_list = m_settings.get_str(settings_pack::dht_bootstrap_nodes); std::vector > nodes; parse_comma_separated_string_port(node_list, nodes); @@ -5763,6 +5778,8 @@ retry: stop_dht(); + if (!m_settings.get_bool(settings_pack::enable_dht)) return; + // postpone starting the DHT if we're still resolving the DHT router if (m_outstanding_router_lookups > 0) return; @@ -5882,7 +5899,7 @@ retry: m_alerts.emplace_alert( dht_error_alert::hostname_lookup, e); - if (m_outstanding_router_lookups == 0) update_dht(); + if (m_outstanding_router_lookups == 0) start_dht(); return; } @@ -5899,7 +5916,7 @@ retry: m_dht_router_nodes.push_back(ep); } - if (m_outstanding_router_lookups == 0) update_dht(); + if (m_outstanding_router_lookups == 0) start_dht(); } // callback for dht_immutable_get