-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Allow multiple connections from the same Peer ID #8215
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
Merged
arvidn
merged 13 commits into
arvidn:RC_2_0
from
lzhzh1:allow_multiple_connections_per_pid
Apr 1, 2026
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1db1019
if the value of settings_pack::allow_multiple_connections_per_ip is t…
lzhzh1 2b8ceeb
add new setting
lzhzh1 b2bbf0b
No change to ABI
lzhzh1 14e47e2
Update test file
lzhzh1 d838ae3
Remove invalid tests in test_peer_list.cpp as allow_multiple_connecti…
lzhzh1 5b218d5
Merge branch 'arvidn:RC_2_0' into allow_multiple_connections_per_pid
lzhzh1 fdf7f09
Restore the test file
lzhzh1 031a6f5
Merge branch 'allow_multiple_connections_per_pid' of https://github.c…
lzhzh1 2b4e150
add new test
lzhzh1 05d993b
add new test
lzhzh1 82af41e
fix peer_list.hpp
lzhzh1 dd44cbf
Delete the currently unnecessary fields
lzhzh1 c6119a2
Merge test code
lzhzh1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| /* | ||
|
|
||
|
|
||
|
|
||
|
|
||
| Redistribution and use in source and binary forms, with or without | ||
| modification, are permitted provided that the following conditions | ||
| are met: | ||
|
|
||
| * Redistributions of source code must retain the above copyright | ||
| notice, this list of conditions and the following disclaimer. | ||
| * Redistributions in binary form must reproduce the above copyright | ||
| notice, this list of conditions and the following disclaimer in | ||
| the documentation and/or other materials provided with the distribution. | ||
| * Neither the name of the author nor the names of its | ||
| contributors may be used to endorse or promote products derived | ||
| from this software without specific prior written permission. | ||
|
|
||
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| POSSIBILITY OF SUCH DAMAGE. | ||
|
|
||
| */ | ||
|
|
||
| #include "libtorrent/session.hpp" | ||
| #include "libtorrent/torrent_handle.hpp" | ||
| #include "libtorrent/settings_pack.hpp" | ||
| #include "libtorrent/alert_types.hpp" | ||
| #include "libtorrent/disabled_disk_io.hpp" | ||
| #include "libtorrent/torrent_flags.hpp" | ||
| #include "settings.hpp" | ||
| #include "fake_peer.hpp" | ||
| #include "utils.hpp" | ||
| #include "test_utils.hpp" | ||
| #include "setup_transfer.hpp" | ||
| #include "create_torrent.hpp" | ||
| #include "simulator/simulator.hpp" | ||
| #include "simulator/utils.hpp" | ||
|
|
||
| namespace { | ||
|
|
||
| struct test_result | ||
| { | ||
| std::vector<lt::error_code> disconnects; | ||
| std::vector<lt::tcp::endpoint> connects; | ||
| }; | ||
|
|
||
| test_result test_allow_multiple_connections_per_pid(bool allow | ||
| , lt::peer_id const& pid | ||
| , char const* peer1_ip | ||
| , char const* peer2_ip) | ||
| { | ||
| // setup the simulation | ||
| sim::default_config cfg; | ||
| sim::simulation sim{cfg}; | ||
| auto ios = std::make_unique<sim::asio::io_context>(sim, lt::make_address_v4("50.0.0.1")); | ||
| lt::session_proxy zombie; | ||
|
|
||
| // setup settings pack | ||
| lt::session_params sp; | ||
| sp.settings = settings(); | ||
| sp.settings.set_int(lt::settings_pack::alert_mask | ||
| , lt::alert_category::all & ~lt::alert_category::stats); | ||
| sp.settings.set_bool(lt::settings_pack::allow_multiple_connections_per_pid, allow); | ||
| sp.disk_io_constructor = lt::disabled_disk_io_constructor; | ||
|
|
||
| // create session | ||
| std::shared_ptr<lt::session> ses = std::make_shared<lt::session>(sp, *ios); | ||
|
|
||
| // add torrent | ||
| lt::add_torrent_params params = ::create_torrent(0, false); | ||
| lt::sha1_hash const info_hash = params.ti->info_hash(); | ||
| params.flags &= ~lt::torrent_flags::auto_managed; | ||
| params.flags &= ~lt::torrent_flags::paused; | ||
| ses->async_add_torrent(std::move(params)); | ||
|
|
||
| // create two fake peers with the same peer-id but different IPs | ||
| auto peer1 = std::make_unique<fake_peer>(sim, peer1_ip); | ||
| auto peer2 = std::make_unique<fake_peer>(sim, peer2_ip); | ||
| peer1->set_peer_id(pid); | ||
| peer2->set_peer_id(pid); | ||
|
|
||
| test_result result; | ||
|
|
||
| // set up a timer to fire later, to shut down | ||
| sim::timer t2(sim, lt::seconds(5) | ||
| , [&](boost::system::error_code const&) | ||
| { | ||
| zombie = ses->abort(); | ||
| ses.reset(); | ||
| }); | ||
|
|
||
| print_alerts(*ses, [&](lt::session&, lt::alert const* a) | ||
| { | ||
| auto* pd = lt::alert_cast<lt::peer_disconnected_alert>(a); | ||
| if (pd) result.disconnects.push_back(pd->error); | ||
|
|
||
| auto* pa = lt::alert_cast<lt::peer_connect_alert>(a); | ||
| if (pa) result.connects.push_back(pa->endpoint); | ||
|
|
||
| if (lt::alert_cast<lt::add_torrent_alert>(a)) | ||
| { | ||
| // both peers connect immediately after the torrent is added | ||
| peer1->connect_to(ep("50.0.0.1", 6881), info_hash); | ||
| peer2->connect_to(ep("50.0.0.1", 6881), info_hash); | ||
| } | ||
| }); | ||
|
|
||
| sim.run(); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| bool has_duplicate_peer_id_error(std::vector<lt::error_code> const& errors) | ||
| { | ||
| for (auto const& err : errors) | ||
| if (err == lt::errors::duplicate_peer_id) | ||
| return true; | ||
| return false; | ||
| } | ||
|
|
||
| bool has_connected(std::vector<lt::tcp::endpoint> const& endpoints | ||
| , lt::address_v4 const& addr) | ||
| { | ||
| for (auto const& ep : endpoints) | ||
| if (ep.address() == addr) | ||
| return true; | ||
| return false; | ||
| } | ||
|
|
||
| } // anonymous namespace | ||
|
|
||
| // allow_multiple_connections_per_pid = false: | ||
| // a second connection from a different IP but the same peer-id should be rejected | ||
| TORRENT_TEST(allow_multiple_connections_per_pid_false) | ||
| { | ||
| lt::peer_id pid; | ||
| std::fill(pid.data(), pid.data() + 20, char(0xAA)); | ||
|
|
||
| auto result = test_allow_multiple_connections_per_pid(false, pid | ||
| , "60.0.0.1", "60.0.0.2"); | ||
|
|
||
| // verify that a duplicate_peer_id disconnect occurred | ||
| TEST_CHECK(has_duplicate_peer_id_error(result.disconnects)); | ||
| } | ||
|
|
||
| // allow_multiple_connections_per_pid = true: | ||
| // a second connection from a different IP but the same peer-id should be allowed | ||
| TORRENT_TEST(allow_multiple_connections_per_pid_true) | ||
| { | ||
| lt::peer_id pid; | ||
| std::fill(pid.data(), pid.data() + 20, char(0xBB)); | ||
|
|
||
| auto result = test_allow_multiple_connections_per_pid(true, pid | ||
| , "60.0.0.3", "60.0.0.4"); | ||
|
|
||
| // verify no duplicate_peer_id error occurred | ||
| TEST_CHECK(!has_duplicate_peer_id_error(result.disconnects)); | ||
|
|
||
| // verify that the second peer connected successfully | ||
| TEST_CHECK(has_connected(result.connects, lt::make_address_v4("60.0.0.4"))); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: this should be renamed
to make it consistent with the rest of the codebase
libtorrent/include/libtorrent/close_reason.hpp
Line 49 in cb6fe6b
fixed in #8373