Skip to content

Commit d8f1e13

Browse files
committed
Merge bitcoin#22112: Force port 0 in I2P
4101ec9 doc: mention that we enforce port=0 in I2P (Vasil Dimov) e0a2b39 addrman: reset I2P ports to 0 when loading from disk (Vasil Dimov) 41cda9d test: ensure I2P ports are handled as expected (Vasil Dimov) 4f432bd net: do not connect to I2P hosts on port!=0 (Vasil Dimov) 1f096f0 net: distinguish default port per network (Vasil Dimov) aeac3bc net: change I2P seeds' ports to 0 (Vasil Dimov) 38f9002 net: change assumed I2P port to 0 (Vasil Dimov) Pull request description: _This is an alternative to bitcoin#21514, inspired by bitcoin#21514 (comment). They are mutually exclusive. Just one of them should be merged._ Change assumed ports for I2P to 0 (instead of the default 8333) as this is closer to what actually happens underneath with SAM 3.1 (bitcoin#21514 (comment), bitcoin#21514 (comment)). Don't connect to I2P peers with advertised port != 0 (we don't specify a port to our SAM 3.1 proxy and it always connects to port = 0). Note, this change: * Keeps I2P addresses with port != 0 in addrman and relays them to others via P2P gossip. There may be non-bitcoin-core-22.0 peers using SAM 3.2 and for them such addresses may be useful. * Silently refuses to connect to I2P hosts with port != 0. This is ok for automatically chosen peers from addrman. Not so ok for peers provided via `-addnode` or `-connect` - a user who specifies `foo.b32.i2p:1234` (non zero port) may wonder why "nothing is happening". Fixes bitcoin#21389 ACKs for top commit: laanwj: Code review ACK 4101ec9 jonatack: re-ACK 4101ec9 per `git range-diff efff9c3 0b0ee03 4101ec9`, built with DDEBUG_ADDRMAN, did fairly extensive testing on mainnet both with and without a peers.dat / -dnsseeds=0 to test boostrapping. Tree-SHA512: 0e3c019e1dc05e54f559275859d3450e0c735596d179e30b66811aad9d5b5fabe3dcc44571e8f7b99f9fe16453eee393d6e153454dd873b9ff14907d4e6354fe
2 parents 7e1ba37 + 4101ec9 commit d8f1e13

13 files changed

+329
-22
lines changed

contrib/seeds/nodes_main.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -678,11 +678,11 @@ vi5bnbxkleeqi6hfccjochnn65lcxlfqs4uwgmhudph554zibiusqnad.onion:8333
678678
xqt25cobm5zqucac3634zfght72he6u3eagfyej5ellbhcdgos7t2had.onion:8333
679679

680680
# manually added 2021-05 for minimal i2p bootstrap support
681-
72l3ucjkuscrbiiepoehuwqgknyzgo7zuix5ty4puwrkyhtmnsga.b32.i2p:8333
682-
c4gfnttsuwqomiygupdqqqyy5y5emnk5c73hrfvatri67prd7vyq.b32.i2p:8333
683-
gehtac45oaghz54ypyopim64mql7oad2bqclla74l6tfeolzmodq.b32.i2p:8333
684-
h3r6bkn46qxftwja53pxiykntegfyfjqtnzbm6iv6r5mungmqgmq.b32.i2p:8333
685-
hnbbyjpxx54623l555sta7pocy3se4sdgmuebi5k6reesz5rjp6q.b32.i2p:8333
686-
pjs7or2ctvteeo5tu4bwyrtydeuhqhvdprtujn4daxr75jpebjxa.b32.i2p:8333
687-
wwbw7nqr3ahkqv62cuqfwgtneekvvpnuc4i4f6yo7tpoqjswvcwa.b32.i2p:8333
688-
zsxwyo6qcn3chqzwxnseusqgsnuw3maqnztkiypyfxtya4snkoka.b32.i2p:8333
681+
72l3ucjkuscrbiiepoehuwqgknyzgo7zuix5ty4puwrkyhtmnsga.b32.i2p:0
682+
c4gfnttsuwqomiygupdqqqyy5y5emnk5c73hrfvatri67prd7vyq.b32.i2p:0
683+
gehtac45oaghz54ypyopim64mql7oad2bqclla74l6tfeolzmodq.b32.i2p:0
684+
h3r6bkn46qxftwja53pxiykntegfyfjqtnzbm6iv6r5mungmqgmq.b32.i2p:0
685+
hnbbyjpxx54623l555sta7pocy3se4sdgmuebi5k6reesz5rjp6q.b32.i2p:0
686+
pjs7or2ctvteeo5tu4bwyrtydeuhqhvdprtujn4daxr75jpebjxa.b32.i2p:0
687+
wwbw7nqr3ahkqv62cuqfwgtneekvvpnuc4i4f6yo7tpoqjswvcwa.b32.i2p:0
688+
zsxwyo6qcn3chqzwxnseusqgsnuw3maqnztkiypyfxtya4snkoka.b32.i2p:0

doc/i2p.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,18 @@ RPC.
7070

7171
Bitcoin Core uses the [SAM v3.1](https://geti2p.net/en/docs/api/samv3) protocol
7272
to connect to the I2P network. Any I2P router that supports it can be used.
73+
74+
## Ports in I2P and Bitcoin Core
75+
76+
Bitcoin Core uses the [SAM v3.1](https://geti2p.net/en/docs/api/samv3)
77+
protocol. One particularity of SAM v3.1 is that it does not support ports,
78+
unlike newer versions of SAM (v3.2 and up) that do support them and default the
79+
port numbers to 0. From the point of view of peers that use newer versions of
80+
SAM or other protocols that support ports, a SAM v3.1 peer is connecting to them
81+
on port 0, from source port 0.
82+
83+
To allow future upgrades to newer versions of SAM, Bitcoin Core sets its
84+
listening port to 0 when listening for incoming I2P connections and advertises
85+
its own I2P address with port 0. Furthermore, it will not attempt to connect to
86+
I2P addresses with a non-zero port number because with SAM v3.1 the destination
87+
port (`TO_PORT`) is always set to 0 and is not in the control of Bitcoin Core.

src/addrman.cpp

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <addrman.h>
77

88
#include <hash.h>
9+
#include <i2p.h>
910
#include <logging.h>
1011
#include <netaddress.h>
1112
#include <serialize.h>
@@ -731,3 +732,100 @@ std::vector<bool> CAddrMan::DecodeAsmap(fs::path path)
731732
}
732733
return bits;
733734
}
735+
736+
void CAddrMan::ResetI2PPorts()
737+
{
738+
for (int bucket = 0; bucket < ADDRMAN_NEW_BUCKET_COUNT; ++bucket) {
739+
for (int i = 0; i < ADDRMAN_BUCKET_SIZE; ++i) {
740+
const auto id = vvNew[bucket][i];
741+
if (id == -1) {
742+
continue;
743+
}
744+
auto it = mapInfo.find(id);
745+
if (it == mapInfo.end()) {
746+
return;
747+
}
748+
auto& addr_info = it->second;
749+
if (!addr_info.IsI2P() || addr_info.GetPort() == I2P_SAM31_PORT) {
750+
continue;
751+
}
752+
753+
auto addr_info_newport = addr_info;
754+
// The below changes addr_info_newport.GetKey(), which is used in finding a
755+
// bucket and a position within that bucket. So a re-bucketing may be necessary.
756+
addr_info_newport.port = I2P_SAM31_PORT;
757+
758+
// Reposition entries of vvNew within the same bucket because we don't know the source
759+
// address which led to the decision to store the entry in vvNew[bucket] so we can't
760+
// re-evaluate that decision, but even if we could, CAddrInfo::GetNewBucket() does not
761+
// use CAddrInfo::GetKey() so it would end up in the same bucket as before the port
762+
// change.
763+
const auto i_target = addr_info_newport.GetBucketPosition(nKey, true, bucket);
764+
765+
if (i_target == i) { // No need to re-position.
766+
addr_info = addr_info_newport;
767+
continue;
768+
}
769+
770+
// Reposition from i to i_target, removing the entry from i_target (if any).
771+
ClearNew(bucket, i_target);
772+
vvNew[bucket][i_target] = id;
773+
vvNew[bucket][i] = -1;
774+
addr_info = addr_info_newport;
775+
}
776+
}
777+
778+
for (int bucket = 0; bucket < ADDRMAN_TRIED_BUCKET_COUNT; ++bucket) {
779+
for (int i = 0; i < ADDRMAN_BUCKET_SIZE; ++i) {
780+
const auto id = vvTried[bucket][i];
781+
if (id == -1) {
782+
continue;
783+
}
784+
auto it = mapInfo.find(id);
785+
if (it == mapInfo.end()) {
786+
return;
787+
}
788+
auto& addr_info = it->second;
789+
if (!addr_info.IsI2P() || addr_info.GetPort() == I2P_SAM31_PORT) {
790+
continue;
791+
}
792+
793+
auto addr_info_newport = addr_info;
794+
// The below changes addr_info_newport.GetKey(), which is used in finding a
795+
// bucket and a position within that bucket. So a re-bucketing may be necessary.
796+
addr_info_newport.port = I2P_SAM31_PORT;
797+
798+
const auto bucket_target = addr_info_newport.GetTriedBucket(nKey, m_asmap);
799+
const auto i_target = addr_info_newport.GetBucketPosition(nKey, false, bucket_target);
800+
801+
if (bucket_target == bucket && i_target == i) { // No need to re-position.
802+
addr_info = addr_info_newport;
803+
continue;
804+
}
805+
806+
// Reposition from (bucket, i) to (bucket_target, i_target). If the latter is
807+
// occupied, then move the entry from there to vvNew.
808+
809+
const auto old_target_id = vvTried[bucket_target][i_target];
810+
if (old_target_id != -1) {
811+
CAddrInfo& old_target_info = mapInfo[old_target_id];
812+
813+
old_target_info.fInTried = false;
814+
vvTried[bucket_target][i_target] = -1;
815+
--nTried;
816+
817+
const auto new_bucket = old_target_info.GetNewBucket(nKey, m_asmap);
818+
const auto new_bucket_i = old_target_info.GetBucketPosition(nKey, true, new_bucket);
819+
ClearNew(new_bucket, new_bucket_i);
820+
821+
old_target_info.nRefCount = 1;
822+
vvNew[new_bucket][new_bucket_i] = old_target_id;
823+
++nNew;
824+
}
825+
826+
vvTried[bucket_target][i_target] = id;
827+
vvTried[bucket][i] = -1;
828+
addr_info = addr_info_newport;
829+
}
830+
}
831+
}

src/addrman.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,8 @@ class CAddrMan
452452

453453
RemoveInvalid();
454454

455+
ResetI2PPorts();
456+
455457
Check();
456458
}
457459

@@ -767,6 +769,14 @@ class CAddrMan
767769
//! Remove invalid addresses.
768770
void RemoveInvalid() EXCLUSIVE_LOCKS_REQUIRED(cs);
769771

772+
/**
773+
* Reset the ports of I2P peers to 0.
774+
* This is needed as a temporary measure because now we enforce port 0 and
775+
* only connect to I2P hosts if the port is 0, but in the early days some
776+
* I2P addresses with port 8333 were rumoured and persisted into addrmans.
777+
*/
778+
void ResetI2PPorts() EXCLUSIVE_LOCKS_REQUIRED(cs);
779+
770780
friend class CAddrManTest;
771781
};
772782

src/chainparams.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88

99
#include <chainparamsbase.h>
1010
#include <consensus/params.h>
11+
#include <netaddress.h>
1112
#include <primitives/block.h>
1213
#include <protocol.h>
1314
#include <util/hash_type.h>
1415

1516
#include <memory>
17+
#include <string>
1618
#include <vector>
1719

1820
typedef std::map<int, uint256> MapCheckpoints;
@@ -80,6 +82,15 @@ class CChainParams
8082
const Consensus::Params& GetConsensus() const { return consensus; }
8183
const CMessageHeader::MessageStartChars& MessageStart() const { return pchMessageStart; }
8284
uint16_t GetDefaultPort() const { return nDefaultPort; }
85+
uint16_t GetDefaultPort(Network net) const
86+
{
87+
return net == NET_I2P ? I2P_SAM31_PORT : GetDefaultPort();
88+
}
89+
uint16_t GetDefaultPort(const std::string& addr) const
90+
{
91+
CNetAddr a;
92+
return a.SetSpecial(addr) ? GetDefaultPort(a.GetNetwork()) : GetDefaultPort();
93+
}
8394

8495
const CBlock& GenesisBlock() const { return genesis; }
8596
/** Default value for -checkmempool and -checkblockindex argument */

src/chainparamsseeds.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -683,14 +683,14 @@ static const uint8_t chainparams_seed_main[] = {
683683
0x04,0x20,0x98,0xc6,0x44,0x27,0x90,0x41,0xa6,0x98,0xf9,0x25,0x6c,0x59,0x0f,0x06,0x6d,0x44,0x59,0x0e,0xb2,0x46,0xb0,0xa4,0x37,0x88,0x69,0x8f,0xc1,0x32,0xcd,0x9f,0x15,0xd7,0x20,0x8d,
684684
0x04,0x20,0xaa,0x3a,0x16,0x86,0xea,0x59,0x09,0x04,0x78,0xe5,0x10,0x92,0xe1,0x1d,0xad,0xf7,0x56,0x2b,0xac,0xb0,0x97,0x29,0x63,0x30,0xf4,0x1b,0xcf,0xde,0xf3,0x28,0x0a,0x29,0x20,0x8d,
685685
0x04,0x20,0xbc,0x27,0xae,0x89,0xc1,0x67,0x73,0x0a,0x08,0x02,0xdf,0xb7,0xcc,0x94,0xc7,0x9f,0xf4,0x72,0x7a,0x9b,0x20,0x0c,0x5c,0x11,0x3d,0x22,0xd6,0x13,0x88,0x66,0x74,0xbf,0x20,0x8d,
686-
0x05,0x20,0xfe,0x97,0xba,0x09,0x2a,0xa4,0x85,0x10,0xa1,0x04,0x7b,0x88,0x7a,0x5a,0x06,0x53,0x71,0x93,0x3b,0xf9,0xa2,0x2f,0xd9,0xe3,0x8f,0xa5,0xa2,0xac,0x1e,0x6c,0x6c,0x8c,0x20,0x8d,
687-
0x05,0x20,0x17,0x0c,0x56,0xce,0x72,0xa5,0xa0,0xe6,0x23,0x06,0xa3,0xc7,0x08,0x43,0x18,0xee,0x3a,0x46,0x35,0x5d,0x17,0xf6,0x78,0x96,0xa0,0x9c,0x51,0xef,0xbe,0x23,0xfd,0x71,0x20,0x8d,
688-
0x05,0x20,0x31,0x0f,0x30,0x0b,0x9d,0x70,0x0c,0x7c,0xf7,0x98,0x7e,0x1c,0xf4,0x33,0xdc,0x64,0x17,0xf7,0x00,0x7a,0x0c,0x04,0xb5,0x83,0xfc,0x5f,0xa6,0x52,0x39,0x79,0x63,0x87,0x20,0x8d,
689-
0x05,0x20,0x3e,0xe3,0xe0,0xa9,0xbc,0xf4,0x2e,0x59,0xd9,0x20,0xee,0xdf,0x74,0x61,0x4d,0x99,0x0c,0x5c,0x15,0x30,0x9b,0x72,0x16,0x79,0x15,0xf4,0x7a,0xca,0x34,0xcc,0x81,0x99,0x20,0x8d,
690-
0x05,0x20,0x3b,0x42,0x1c,0x25,0xf7,0xbf,0x79,0xed,0x6d,0x7d,0xef,0x65,0x30,0x7d,0xee,0x16,0x37,0x22,0x72,0x43,0x33,0x28,0x40,0xa3,0xaa,0xf4,0x48,0x49,0x67,0xb1,0x4b,0xfd,0x20,0x8d,
691-
0x05,0x20,0x7a,0x65,0xf7,0x47,0x42,0x9d,0x66,0x42,0x3b,0xb3,0xa7,0x03,0x6c,0x46,0x78,0x19,0x28,0x78,0x1e,0xa3,0x7c,0x67,0x44,0xb7,0x83,0x05,0xe3,0xfe,0xa5,0xe4,0x0a,0x6e,0x20,0x8d,
692-
0x05,0x20,0xb5,0x83,0x6f,0xb6,0x11,0xd8,0x0e,0xa8,0x57,0xda,0x15,0x20,0x5b,0x1a,0x6d,0x21,0x15,0x5a,0xbd,0xb4,0x17,0x11,0xc2,0xfb,0x0e,0xfc,0xde,0xe8,0x26,0x56,0xa8,0xac,0x20,0x8d,
693-
0x05,0x20,0xcc,0xaf,0x6c,0x3b,0xd0,0x13,0x76,0x23,0xc3,0x36,0xbb,0x64,0x4a,0x4a,0x06,0x93,0x69,0x6d,0xb0,0x10,0x6e,0x66,0xa4,0x61,0xf8,0x2d,0xe7,0x80,0x72,0x4d,0x53,0x94,0x20,0x8d,
686+
0x05,0x20,0xfe,0x97,0xba,0x09,0x2a,0xa4,0x85,0x10,0xa1,0x04,0x7b,0x88,0x7a,0x5a,0x06,0x53,0x71,0x93,0x3b,0xf9,0xa2,0x2f,0xd9,0xe3,0x8f,0xa5,0xa2,0xac,0x1e,0x6c,0x6c,0x8c,0x00,0x00,
687+
0x05,0x20,0x17,0x0c,0x56,0xce,0x72,0xa5,0xa0,0xe6,0x23,0x06,0xa3,0xc7,0x08,0x43,0x18,0xee,0x3a,0x46,0x35,0x5d,0x17,0xf6,0x78,0x96,0xa0,0x9c,0x51,0xef,0xbe,0x23,0xfd,0x71,0x00,0x00,
688+
0x05,0x20,0x31,0x0f,0x30,0x0b,0x9d,0x70,0x0c,0x7c,0xf7,0x98,0x7e,0x1c,0xf4,0x33,0xdc,0x64,0x17,0xf7,0x00,0x7a,0x0c,0x04,0xb5,0x83,0xfc,0x5f,0xa6,0x52,0x39,0x79,0x63,0x87,0x00,0x00,
689+
0x05,0x20,0x3e,0xe3,0xe0,0xa9,0xbc,0xf4,0x2e,0x59,0xd9,0x20,0xee,0xdf,0x74,0x61,0x4d,0x99,0x0c,0x5c,0x15,0x30,0x9b,0x72,0x16,0x79,0x15,0xf4,0x7a,0xca,0x34,0xcc,0x81,0x99,0x00,0x00,
690+
0x05,0x20,0x3b,0x42,0x1c,0x25,0xf7,0xbf,0x79,0xed,0x6d,0x7d,0xef,0x65,0x30,0x7d,0xee,0x16,0x37,0x22,0x72,0x43,0x33,0x28,0x40,0xa3,0xaa,0xf4,0x48,0x49,0x67,0xb1,0x4b,0xfd,0x00,0x00,
691+
0x05,0x20,0x7a,0x65,0xf7,0x47,0x42,0x9d,0x66,0x42,0x3b,0xb3,0xa7,0x03,0x6c,0x46,0x78,0x19,0x28,0x78,0x1e,0xa3,0x7c,0x67,0x44,0xb7,0x83,0x05,0xe3,0xfe,0xa5,0xe4,0x0a,0x6e,0x00,0x00,
692+
0x05,0x20,0xb5,0x83,0x6f,0xb6,0x11,0xd8,0x0e,0xa8,0x57,0xda,0x15,0x20,0x5b,0x1a,0x6d,0x21,0x15,0x5a,0xbd,0xb4,0x17,0x11,0xc2,0xfb,0x0e,0xfc,0xde,0xe8,0x26,0x56,0xa8,0xac,0x00,0x00,
693+
0x05,0x20,0xcc,0xaf,0x6c,0x3b,0xd0,0x13,0x76,0x23,0xc3,0x36,0xbb,0x64,0x4a,0x4a,0x06,0x93,0x69,0x6d,0xb0,0x10,0x6e,0x66,0xa4,0x61,0xf8,0x2d,0xe7,0x80,0x72,0x4d,0x53,0x94,0x00,0x00,
694694
};
695695

696696
static const uint8_t chainparams_seed_test[] = {

src/i2p.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ bool Session::Accept(Connection& conn)
159159
const std::string& peer_dest =
160160
conn.sock->RecvUntilTerminator('\n', MAX_WAIT_FOR_IO, *m_interrupt, MAX_MSG_SIZE);
161161

162-
conn.peer = CService(DestB64ToAddr(peer_dest), Params().GetDefaultPort());
162+
conn.peer = CService(DestB64ToAddr(peer_dest), I2P_SAM31_PORT);
163163

164164
return true;
165165
}
@@ -172,6 +172,13 @@ bool Session::Accept(Connection& conn)
172172

173173
bool Session::Connect(const CService& to, Connection& conn, bool& proxy_error)
174174
{
175+
// Refuse connecting to arbitrary ports. We don't specify any destination port to the SAM proxy
176+
// when connecting (SAM 3.1 does not use ports) and it forces/defaults it to I2P_SAM31_PORT.
177+
if (to.GetPort() != I2P_SAM31_PORT) {
178+
proxy_error = false;
179+
return false;
180+
}
181+
175182
proxy_error = true;
176183

177184
std::string session_id;
@@ -366,7 +373,7 @@ void Session::CreateIfNotCreatedAlready()
366373
SendRequestAndGetReply(*sock, strprintf("SESSION CREATE STYLE=STREAM ID=%s DESTINATION=%s",
367374
session_id, private_key_b64));
368375

369-
m_my_addr = CService(DestBinToAddr(MyDestination()), Params().GetDefaultPort());
376+
m_my_addr = CService(DestBinToAddr(MyDestination()), I2P_SAM31_PORT);
370377
m_session_id = session_id;
371378
m_control_sock = std::move(sock);
372379

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ void SetupServerArgs(ArgsManager& argsman)
442442
argsman.AddArg("-peerbloomfilters", strprintf("Support filtering of blocks and transaction with bloom filters (default: %u)", DEFAULT_PEERBLOOMFILTERS), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
443443
argsman.AddArg("-peerblockfilters", strprintf("Serve compact block filters to peers per BIP 157 (default: %u)", DEFAULT_PEERBLOCKFILTERS), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
444444
argsman.AddArg("-permitbaremultisig", strprintf("Relay non-P2SH multisig (default: %u)", DEFAULT_PERMIT_BAREMULTISIG), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
445-
argsman.AddArg("-port=<port>", strprintf("Listen for connections on <port>. Nodes not using the default ports (default: %u, testnet: %u, signet: %u, regtest: %u) are unlikely to get incoming connections.", defaultChainParams->GetDefaultPort(), testnetChainParams->GetDefaultPort(), signetChainParams->GetDefaultPort(), regtestChainParams->GetDefaultPort()), ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::CONNECTION);
445+
argsman.AddArg("-port=<port>", strprintf("Listen for connections on <port>. Nodes not using the default ports (default: %u, testnet: %u, signet: %u, regtest: %u) are unlikely to get incoming connections. Not relevant for I2P (see doc/i2p.md).", defaultChainParams->GetDefaultPort(), testnetChainParams->GetDefaultPort(), signetChainParams->GetDefaultPort(), regtestChainParams->GetDefaultPort()), ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::CONNECTION);
446446
argsman.AddArg("-proxy=<ip:port>", "Connect through SOCKS5 proxy, set -noproxy to disable (default: disabled)", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
447447
argsman.AddArg("-proxyrandomize", strprintf("Randomize credentials for every proxy connection. This enables Tor stream isolation (default: %u)", DEFAULT_PROXYRANDOMIZE), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
448448
argsman.AddArg("-seednode=<ip>", "Connect to a node to retrieve peer addresses, and disconnect. This option can be specified multiple times to connect to multiple nodes.", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);

src/net.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,8 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
402402
pszDest ? 0.0 : (double)(GetAdjustedTime() - addrConnect.nTime)/3600.0);
403403

404404
// Resolve
405-
const uint16_t default_port{Params().GetDefaultPort()};
405+
const uint16_t default_port{pszDest != nullptr ? Params().GetDefaultPort(pszDest) :
406+
Params().GetDefaultPort()};
406407
if (pszDest) {
407408
std::vector<CService> resolved;
408409
if (Lookup(pszDest, resolved, default_port, fNameLookup && !HaveNameProxy(), 256) && !resolved.empty()) {
@@ -2059,8 +2060,9 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
20592060
// from advertising themselves as a service on another host and
20602061
// port, causing a DoS attack as nodes around the network attempt
20612062
// to connect to it fruitlessly.
2062-
if (addr.GetPort() != Params().GetDefaultPort() && nTries < 50)
2063+
if (addr.GetPort() != Params().GetDefaultPort(addr.GetNetwork()) && nTries < 50) {
20632064
continue;
2065+
}
20642066

20652067
addrConnect = addr;
20662068
break;
@@ -2123,7 +2125,7 @@ std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo() const
21232125
}
21242126

21252127
for (const std::string& strAddNode : lAddresses) {
2126-
CService service(LookupNumeric(strAddNode, Params().GetDefaultPort()));
2128+
CService service(LookupNumeric(strAddNode, Params().GetDefaultPort(strAddNode)));
21272129
AddedNodeInfo addedNode{strAddNode, CService(), false, false};
21282130
if (service.IsValid()) {
21292131
// strAddNode is an IP:port

src/netaddress.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ static constexpr size_t ADDR_CJDNS_SIZE = 16;
112112
/// Size of "internal" (NET_INTERNAL) address (in bytes).
113113
static constexpr size_t ADDR_INTERNAL_SIZE = 10;
114114

115+
/// SAM 3.1 and earlier do not support specifying ports and force the port to 0.
116+
static constexpr uint16_t I2P_SAM31_PORT{0};
117+
115118
/**
116119
* Network address.
117120
*/

0 commit comments

Comments
 (0)