Skip to content

Commit

Permalink
implement i2p_pex, peer exchange support for i2p torrents
Browse files Browse the repository at this point in the history
  • Loading branch information
arvidn committed Jan 29, 2025
1 parent b5bbe05 commit 77a35ff
Show file tree
Hide file tree
Showing 22 changed files with 545 additions and 28 deletions.
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
2 changes: 2 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
2.1.0 not released

* support peer exchange for i2p torrents
* implement i2p_pex, peer exchange support for i2p torrents
* requires OpenSSL minimum version 1.1.0 with SNI support
* try harder to bind TCP and UDP sockets to the same port
* made disk_interface's status_t type a flags type
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
3 changes: 2 additions & 1 deletion include/libtorrent/aux_/torrent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,8 @@ 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; }
#endif

Expand Down
2 changes: 2 additions & 0 deletions include/libtorrent/aux_/torrent_peer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ namespace libtorrent::aux {
i2p_peer(i2p_peer&&) = default;
i2p_peer& operator=(i2p_peer&&) & = default;

// TODO: instead of keeping this as a string, make it a (dense)
// sha256_hash
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

0 comments on commit 77a35ff

Please sign in to comment.