Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement i2p_pex, peer exchange support for i2p torrents #7831

Merged
merged 1 commit into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ set(libtorrent_extensions_include_files
smart_ban.hpp
ut_metadata.hpp
ut_pex.hpp
i2p_pex.hpp
)

set(libtorrent_aux_include_files
Expand Down Expand Up @@ -443,6 +444,7 @@ set(sources
# -- extensions --
smart_ban.cpp
ut_pex.cpp
i2p_pex.cpp
ut_metadata.cpp
)

Expand Down
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
2.1.0 not released

* implement i2p_pex, peer exchange support for i2p torrents
* deprecate torrent_alert::torrent_name()
* requires OpenSSL minimum version 1.1.0 with SNI support
* try harder to bind TCP and UDP sockets to the same port
Expand Down
1 change: 1 addition & 0 deletions Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,7 @@ SOURCES =

# -- extensions --
ut_pex
i2p_pex
ut_metadata
smart_ban
;
Expand Down
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ SOURCES = \
upnp.cpp \
ut_metadata.cpp \
ut_pex.cpp \
i2p_pex.cpp \
utf8.cpp \
utp_socket_manager.cpp \
utp_stream.cpp \
Expand Down Expand Up @@ -702,6 +703,7 @@ HEADERS = \
extensions/smart_ban.hpp \
extensions/ut_metadata.hpp \
extensions/ut_pex.hpp \
extensions/i2p_pex.hpp \
\
kademlia/announce_flags.hpp \
kademlia/dht_observer.hpp \
Expand Down
6 changes: 4 additions & 2 deletions include/libtorrent/aux_/bt_peer_connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,12 @@ namespace libtorrent::aux {
// metadata_msg = 2,
upload_only_msg = 3,
holepunch_msg = 4,
// recommend_msg = 5,
// comment_msg = 6,
// i2p_pex_msg = 5,
dont_have_msg = 7,
share_mode_msg = 8

// recommend_msg = x,
// comment_msg = x,
};

~bt_peer_connection() override;
Expand Down
1 change: 1 addition & 0 deletions include/libtorrent/aux_/peer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ namespace libtorrent::aux {
};

#if TORRENT_USE_I2P
// TODO: it seems unnecessary to wrap this i2p address in a struct
struct i2p_peer_entry
{
sha256_hash destination;
Expand Down
1 change: 1 addition & 0 deletions include/libtorrent/aux_/session_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,7 @@ namespace aux {
#endif

#if TORRENT_USE_I2P
i2p_connection& i2p_conn() override { return m_i2p_conn; }
char const* i2p_session() const override { return m_i2p_conn.session_id(); }
std::string const& local_i2p_endpoint() const override { return m_i2p_conn.local_endpoint(); }

Expand Down
1 change: 1 addition & 0 deletions include/libtorrent/aux_/session_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ namespace libtorrent::aux {
virtual proxy_settings proxy() const = 0;

#if TORRENT_USE_I2P
virtual i2p_connection& i2p_conn() = 0;
virtual char const* i2p_session() const = 0;
virtual std::string const& local_i2p_endpoint() const = 0;
#endif
Expand Down
5 changes: 4 additions & 1 deletion include/libtorrent/aux_/torrent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -921,8 +921,11 @@ namespace libtorrent::aux {
void completed();

#if TORRENT_USE_I2P
void on_i2p_resolve(error_code const& ec, char const* dest);
void on_i2p_resolve(error_code const& ec, char const* dest, peer_source_flags_t const source);
void add_i2p_peer(sha256_hash const& dest, peer_source_flags_t source);
bool is_i2p() const { return m_i2p; }
#else
bool is_i2p() const { return false; }
#endif

// this is the asio callback that is called when a name
Expand Down
3 changes: 3 additions & 0 deletions include/libtorrent/aux_/torrent_peer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ namespace libtorrent::aux {
i2p_peer(i2p_peer&&) = default;
i2p_peer& operator=(i2p_peer&&) & = default;

// TODO: This destination should probably be kept in binary form (rather than
// base64). The peers should primarily be handled by their sha256 hash
// of the destination, that should be kept in here as well.
aux::string_ptr destination;
};
#endif
Expand Down
37 changes: 37 additions & 0 deletions include/libtorrent/extensions/i2p_pex.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Copyright (c) 2025, 2019-2021, Arvid Norberg
All rights reserved.
You may use, distribute and modify this code under the terms of the BSD license,
see LICENSE file.
*/

#ifndef TORRENT_I2P_PEX_EXTENSION_HPP_INCLUDED
#define TORRENT_I2P_PEX_EXTENSION_HPP_INCLUDED

#ifndef TORRENT_DISABLE_EXTENSIONS
#if TORRENT_USE_I2P

#include "libtorrent/config.hpp"

#include <memory>

namespace libtorrent {

struct torrent_plugin;
struct torrent_handle;
struct client_data_t;

// The i2p_pex extension gossips i2p peer addresses, only on i2p torrents.
// The extension will not activate for non-i2p torrents.
//
// This can either be passed in the add_torrent_params::extensions field, or
// via torrent_handle::add_extension().
TORRENT_EXPORT std::shared_ptr<torrent_plugin> create_i2p_pex_plugin(torrent_handle const&, client_data_t);
}

#endif
#endif // TORRENT_DISABLE_EXTENSIONS

#endif // TORRENT_I2P_PEX_EXTENSION_HPP_INCLUDED
2 changes: 1 addition & 1 deletion include/libtorrent/i2p_stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ struct i2p_stream : aux::proxy_base
state_t m_state;
};

class i2p_connection
class TORRENT_EXTRA_EXPORT i2p_connection
{
public:
explicit i2p_connection(io_context& ios);
Expand Down
1 change: 1 addition & 0 deletions include/libtorrent/libtorrent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "libtorrent/error.hpp"
#include "libtorrent/error_code.hpp"
#include "libtorrent/extensions.hpp"
#include "libtorrent/extensions/i2p_pex.hpp"
#include "libtorrent/extensions/smart_ban.hpp"
#include "libtorrent/extensions/ut_metadata.hpp"
#include "libtorrent/extensions/ut_pex.hpp"
Expand Down
2 changes: 2 additions & 0 deletions include/libtorrent/torrent_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,8 @@ TORRENT_VERSION_NAMESPACE_3
// or not it has a tracker whose URL domain name ends with ".i2p". i2p
// torrents disable the DHT and local peer discovery as well as talking
// to peers over anything other than the i2p network.
// This is not reliably set for torrents created via resume data or
// magnet links. Prefer using torrent::is_i2p() instead.
bool is_i2p() const { return bool(m_flags & i2p); }

// internal
Expand Down
Loading
Loading