From 02e15202f227e5c803ebb50983b0852b9c4bd10c Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sun, 7 Nov 2010 19:18:16 +0000 Subject: [PATCH] support dht nodes in magnet links --- ChangeLog | 1 + src/magnet_uri.cpp | 20 ++++++++++++++++++++ src/session_impl.cpp | 18 +++++++++++------- test/test_primitives.cpp | 2 +- 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/ChangeLog b/ChangeLog index 810c1fccb..3736d3d14 100644 --- a/ChangeLog +++ b/ChangeLog @@ -55,6 +55,7 @@ * added more detailed instrumentation of the disk I/O thread + * support dht nodes in magnet links * support 100 Continue HTTP responses * changed default choker behavior to use 8 unchoke slots (instead of being rate based) * fixed error reporting issue in disk I/O thread diff --git a/src/magnet_uri.cpp b/src/magnet_uri.cpp index f379c3023..a46b41cca 100644 --- a/src/magnet_uri.cpp +++ b/src/magnet_uri.cpp @@ -163,6 +163,26 @@ namespace libtorrent return torrent_handle(); } +#ifndef TORRENT_DISABLE_DHT + std::string::size_type node_pos = std::string::npos; + std::string node = url_has_argument(uri, "dht", &node_pos); + while (!node.empty()) + { + std::string::size_type divider = node.find_last_of(':'); + if (divider != std::string::npos) + { + int port = atoi(node.c_str()+divider+1); + if (port != 0) + ses.add_dht_node(std::make_pair(node.substr(0, divider), port)); + } + + node_pos = uri.find("&dht=", node_pos); + if (node_pos == std::string::npos) break; + node_pos += 5; + node = uri.substr(node_pos, uri.find('&', node_pos) - node_pos); + } +#endif + sha1_hash info_hash; if (btih.size() == 40 + 9) from_hex(&btih[9], 40, (char*)&info_hash[0]); else info_hash.assign(base32decode(btih.substr(9))); diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 50a07a016..36b37be23 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -3678,8 +3678,7 @@ namespace aux { void session_impl::add_dht_node_name(std::pair const& node) { - TORRENT_ASSERT(m_dht); - m_dht->add_node(node); + if (m_dht) m_dht->add_node(node); } void session_impl::add_dht_router(std::pair const& node) @@ -3694,11 +3693,16 @@ namespace aux { void session_impl::on_dht_router_name_lookup(error_code const& e , tcp::resolver::iterator host) { - if (e || host == tcp::resolver::iterator()) return; - // router nodes should be added before the DHT is started (and bootstrapped) - udp::endpoint ep(host->endpoint().address(), host->endpoint().port()); - if (m_dht) m_dht->add_router_node(ep); - m_dht_router_nodes.push_back(ep); + // TODO: report errors as alerts + if (e) return; + while (host != tcp::resolver::iterator()) + { + // router nodes should be added before the DHT is started (and bootstrapped) + udp::endpoint ep(host->endpoint().address(), host->endpoint().port()); + if (m_dht) m_dht->add_router_node(ep); + m_dht_router_nodes.push_back(ep); + ++host; + } } #endif diff --git a/test/test_primitives.cpp b/test/test_primitives.cpp index 0dd271b18..efcc9a35b 100644 --- a/test/test_primitives.cpp +++ b/test/test_primitives.cpp @@ -431,7 +431,7 @@ int test_main() p.save_path = "."; error_code ec; const char* magnet_uri = "magnet:?xt=urn:btih:cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd" - "&tr=http://1&tr=http://2&tr=http://3&dn=foo"; + "&tr=http://1&tr=http://2&tr=http://3&dn=foo&dht=127.0.0.1:43"; torrent_handle t = add_magnet_uri(*s, magnet_uri, p, ec); TEST_CHECK(!ec); if (ec) fprintf(stderr, "%s\n", ec.message().c_str());