diff --git a/ChangeLog b/ChangeLog index 411eedc59..59dc7c0ef 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,7 @@ 1.1.1 release * fixed parsing of IPv6 endpoint with invalid port character separator + * added limited support for new x.pe parameter from BEP 9 * fixed dht stats counters that weren't being updated * make sure add_torrent_alert is always posted before other alerts for the torrent diff --git a/include/libtorrent/magnet_uri.hpp b/include/libtorrent/magnet_uri.hpp index fdda46255..9b0f7b66b 100644 --- a/include/libtorrent/magnet_uri.hpp +++ b/include/libtorrent/magnet_uri.hpp @@ -78,6 +78,9 @@ namespace libtorrent // This function parses out information from the magnet link and populates the // add_torrent_params object. TORRENT_EXPORT void parse_magnet_uri(std::string const& uri, add_torrent_params& p, error_code& ec); + + // internal, delete when merge in master + TORRENT_EXTRA_EXPORT void parse_magnet_uri_peers(std::string const& uri, std::vector& peers); } #endif diff --git a/src/magnet_uri.cpp b/src/magnet_uri.cpp index e962f199c..4ea45fd13 100644 --- a/src/magnet_uri.cpp +++ b/src/magnet_uri.cpp @@ -40,6 +40,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/announce_entry.hpp" #include +#include "libtorrent/socket_io.hpp" namespace libtorrent { @@ -270,6 +271,24 @@ namespace libtorrent p.info_hash = info_hash; if (!name.empty()) p.name = name; } + + void parse_magnet_uri_peers(std::string const& uri, std::vector& peers) + { + std::string::size_type peer_pos = std::string::npos; + std::string peer = url_has_argument(uri, "x.pe", &peer_pos); + while (!peer.empty()) + { + error_code e; + tcp::endpoint endp = parse_endpoint(peer, e); + if (!e) + peers.push_back(endp); + + peer_pos = uri.find("&x.pe=", peer_pos); + if (peer_pos == std::string::npos) break; + peer_pos += 6; + peer = uri.substr(peer_pos, uri.find('&', peer_pos) - peer_pos); + } + } } diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 13cc7a3b2..12ed54496 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -4785,6 +4785,19 @@ retry: add_torrent_params params = p; boost::shared_ptr const torrent_ptr = add_torrent_impl(params, ec); + // --- PEERS --- (delete when merged to master) + std::vector peers; + parse_magnet_uri_peers(p.url, peers); + + for (std::vector::const_iterator i = peers.begin() + , end(peers.end()); i != end; ++i) + { + torrent_ptr->add_peer(*i , peer_info::resume_data); + } + + if (!peers.empty()) + torrent_ptr->update_want_peers(); + torrent_handle const handle(torrent_ptr); m_alerts.emplace_alert(handle, params, ec); diff --git a/src/torrent.cpp b/src/torrent.cpp index 8769d2b55..77b2d4113 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -11579,6 +11579,7 @@ namespace libtorrent { if (!m_apply_ip_filter) return; if (!m_peer_list) return; + if (!m_ip_filter) return; torrent_state st = get_peer_list_state(); std::vector
banned; diff --git a/test/test_magnet.cpp b/test/test_magnet.cpp index 62494038d..ecad3c3d8 100644 --- a/test/test_magnet.cpp +++ b/test/test_magnet.cpp @@ -31,6 +31,7 @@ POSSIBILITY OF SUCH DAMAGE. */ #include "test.hpp" +#include "setup_transfer.hpp" #include "libtorrent/magnet_uri.hpp" #include "libtorrent/session.hpp" #include "libtorrent/torrent_handle.hpp" @@ -276,6 +277,20 @@ TORRENT_TEST(parse_space_hash) ec.clear(); } +TORRENT_TEST(parse_peer) +{ + std::vector peers; + parse_magnet_uri_peers("magnet:?xt=urn:btih:cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd&dn=foo&x.pe=127.0.0.1:43&x.pe=&x.pe=:100&x.pe=[::1]:45", peers); +#if TORRENT_USE_IPV6 + TEST_EQUAL(peers.size(), 2); + TEST_EQUAL(peers[0], ep("127.0.0.1", 43)); + TEST_EQUAL(peers[1], ep("::1", 45)); +#else + TEST_EQUAL(peers.size(), 1); + TEST_EQUAL(peers[0], ep("127.0.0.1", 43)); +#endif +} + #ifndef TORRENT_DISABLE_DHT TORRENT_TEST(parse_dht_node) { diff --git a/test/test_torrent.cpp b/test/test_torrent.cpp index e87137cff..dfc87951b 100644 --- a/test/test_torrent.cpp +++ b/test/test_torrent.cpp @@ -38,6 +38,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/alert_types.hpp" #include "libtorrent/thread.hpp" #include "libtorrent/torrent.hpp" +#include "libtorrent/peer_info.hpp" #include #include #include @@ -212,6 +213,36 @@ TORRENT_TEST(total_wanted) TEST_EQUAL(st.total_wanted_done, 0); } +TORRENT_TEST(added_peers) +{ + file_storage fs; + + fs.add_file("test_torrent_dir4/tmp1", 1024); + + libtorrent::create_torrent t(fs, 1024); + std::vector tmp; + bencode(std::back_inserter(tmp), t.generate()); + error_code ec; + boost::shared_ptr info(boost::make_shared( + &tmp[0], tmp.size(), boost::ref(ec))); + + settings_pack pack; + pack.set_str(settings_pack::listen_interfaces, "0.0.0.0:48130"); + pack.set_int(settings_pack::max_retry_port_bind, 10); + lt::session ses(pack); + + add_torrent_params p; + p.ti = info; + p.save_path = "."; + p.url = "?x.pe=127.0.0.1:48081&x.pe=127.0.0.2:48082"; + + torrent_handle h = ses.add_torrent(p); + + std::vector v; + h.get_full_peer_list(v); + TEST_EQUAL(v.size(), 2); +} + TORRENT_TEST(torrent) { /* {