Skip to content

Commit 91ff57a

Browse files
vasildhebasto
authored andcommitted
net: distinguish default port per network
Change `CChainParams::GetDefaultPort()` to return 0 if the network is I2P.
1 parent 0d4337a commit 91ff57a

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/chainparams.h

+11
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/net.cpp

+5-3
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

0 commit comments

Comments
 (0)