16
16
#include < crypto/common.h>
17
17
#include < crypto/sha256.h>
18
18
#include < netbase.h>
19
+ #include < net_permissions.h>
19
20
#include < primitives/transaction.h>
20
21
#include < scheduler.h>
21
22
#include < ui_interface.h>
@@ -67,7 +68,6 @@ enum BindFlags {
67
68
BF_NONE = 0 ,
68
69
BF_EXPLICIT = (1U << 0 ),
69
70
BF_REPORT_ERROR = (1U << 1 ),
70
- BF_WHITELIST = (1U << 2 ),
71
71
};
72
72
73
73
// The set of sockets cannot be modified while waiting
@@ -459,12 +459,10 @@ void CNode::CloseSocketDisconnect()
459
459
}
460
460
}
461
461
462
- bool CConnman::IsWhitelistedRange (const CNetAddr &addr) {
463
- for (const CSubNet& subnet : vWhitelistedRange) {
464
- if (subnet.Match (addr))
465
- return true ;
462
+ void CConnman::AddWhitelistPermissionFlags (NetPermissionFlags& flags, const CNetAddr &addr) const {
463
+ for (const auto & subnet : vWhitelistedRange) {
464
+ if (subnet.m_subnet .Match (addr)) NetPermissions::AddFlag (flags, subnet.m_flags );
466
465
}
467
- return false ;
468
466
}
469
467
470
468
std::string CNode::GetAddrName () const {
@@ -529,6 +527,7 @@ void CNode::copyStats(CNodeStats &stats)
529
527
X (nRecvBytes);
530
528
}
531
529
X (fWhitelisted );
530
+ X (m_permissionFlags);
532
531
{
533
532
LOCK (cs_feeFilter);
534
533
X (minFeeFilter);
@@ -904,7 +903,20 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
904
903
}
905
904
}
906
905
907
- bool whitelisted = hListenSocket.whitelisted || IsWhitelistedRange (addr);
906
+ NetPermissionFlags permissionFlags = NetPermissionFlags::PF_NONE;
907
+ hListenSocket.AddSocketPermissionFlags (permissionFlags);
908
+ AddWhitelistPermissionFlags (permissionFlags, addr);
909
+ const bool noban = NetPermissions::HasFlag (permissionFlags, NetPermissionFlags::PF_NOBAN);
910
+ bool legacyWhitelisted = false ;
911
+ if (NetPermissions::HasFlag (permissionFlags, NetPermissionFlags::PF_ISIMPLICIT)) {
912
+ NetPermissions::ClearFlag (permissionFlags, PF_ISIMPLICIT);
913
+ if (gArgs .GetBoolArg (" -whitelistforcerelay" , false )) NetPermissions::AddFlag (permissionFlags, PF_FORCERELAY);
914
+ if (gArgs .GetBoolArg (" -whitelistrelay" , false )) NetPermissions::AddFlag (permissionFlags, PF_RELAY);
915
+ NetPermissions::AddFlag (permissionFlags, PF_MEMPOOL);
916
+ NetPermissions::AddFlag (permissionFlags, PF_NOBAN);
917
+ legacyWhitelisted = true ;
918
+ }
919
+
908
920
{
909
921
LOCK (cs_vNodes);
910
922
for (const CNode* pnode : vNodes) {
@@ -941,7 +953,7 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
941
953
942
954
// Don't accept connections from banned peers, but if our inbound slots aren't almost full, accept
943
955
// if the only banning reason was an automatic misbehavior ban.
944
- if (!whitelisted && bannedlevel > ((nInbound + 1 < nMaxInbound) ? 1 : 0 ))
956
+ if (!noban && bannedlevel > ((nInbound + 1 < nMaxInbound) ? 1 : 0 ))
945
957
{
946
958
LogPrint (BCLog::NET, " connection from %s dropped (banned)\n " , addr.ToString ());
947
959
CloseSocket (hSocket);
@@ -962,9 +974,15 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
962
974
uint64_t nonce = GetDeterministicRandomizer (RANDOMIZER_ID_LOCALHOSTNONCE).Write (id).Finalize ();
963
975
CAddress addr_bind = GetBindAddress (hSocket);
964
976
965
- CNode* pnode = new CNode (id, nLocalServices, GetBestHeight (), hSocket, addr, CalculateKeyedNetGroup (addr), nonce, addr_bind, " " , true );
977
+ ServiceFlags nodeServices = nLocalServices;
978
+ if (NetPermissions::HasFlag (permissionFlags, PF_BLOOMFILTER)) {
979
+ nodeServices = static_cast <ServiceFlags>(nodeServices | NODE_BLOOM);
980
+ }
981
+ CNode* pnode = new CNode (id, nodeServices, GetBestHeight (), hSocket, addr, CalculateKeyedNetGroup (addr), nonce, addr_bind, " " , true );
966
982
pnode->AddRef ();
967
- pnode->fWhitelisted = whitelisted;
983
+ pnode->m_permissionFlags = permissionFlags;
984
+ // If this flag is present, the user probably expect that RPC and QT report it as whitelisted (backward compatibility)
985
+ pnode->fWhitelisted = legacyWhitelisted;
968
986
pnode->m_prefer_evict = bannedlevel > 0 ;
969
987
m_msgproc->InitializeNode (pnode);
970
988
@@ -1983,7 +2001,7 @@ void CConnman::ThreadMessageHandler()
1983
2001
1984
2002
1985
2003
1986
- bool CConnman::BindListenPort (const CService & addrBind, std::string& strError, bool fWhitelisted )
2004
+ bool CConnman::BindListenPort (const CService& addrBind, std::string& strError, NetPermissionFlags permissions )
1987
2005
{
1988
2006
strError = " " ;
1989
2007
int nOne = 1 ;
@@ -2044,9 +2062,9 @@ bool CConnman::BindListenPort(const CService &addrBind, std::string& strError, b
2044
2062
return false ;
2045
2063
}
2046
2064
2047
- vhListenSocket.push_back (ListenSocket (hListenSocket, fWhitelisted ));
2065
+ vhListenSocket.push_back (ListenSocket (hListenSocket, permissions ));
2048
2066
2049
- if (addrBind.IsRoutable () && fDiscover && ! fWhitelisted )
2067
+ if (addrBind.IsRoutable () && fDiscover && (permissions & PF_NOBAN) == 0 )
2050
2068
AddLocal (addrBind, LOCAL_BIND);
2051
2069
2052
2070
return true ;
@@ -2130,11 +2148,11 @@ NodeId CConnman::GetNewNodeId()
2130
2148
}
2131
2149
2132
2150
2133
- bool CConnman::Bind (const CService &addr, unsigned int flags) {
2151
+ bool CConnman::Bind (const CService &addr, unsigned int flags, NetPermissionFlags permissions ) {
2134
2152
if (!(flags & BF_EXPLICIT) && !IsReachable (addr))
2135
2153
return false ;
2136
2154
std::string strError;
2137
- if (!BindListenPort (addr, strError, (flags & BF_WHITELIST) != 0 )) {
2155
+ if (!BindListenPort (addr, strError, permissions )) {
2138
2156
if ((flags & BF_REPORT_ERROR) && clientInterface) {
2139
2157
clientInterface->ThreadSafeMessageBox (strError, " " , CClientUIInterface::MSG_ERROR);
2140
2158
}
@@ -2143,20 +2161,21 @@ bool CConnman::Bind(const CService &addr, unsigned int flags) {
2143
2161
return true ;
2144
2162
}
2145
2163
2146
- bool CConnman::InitBinds (const std::vector<CService>& binds, const std::vector<CService>& whiteBinds) {
2164
+ bool CConnman::InitBinds (const std::vector<CService>& binds, const std::vector<NetWhitebindPermissions>& whiteBinds)
2165
+ {
2147
2166
bool fBound = false ;
2148
2167
for (const auto & addrBind : binds) {
2149
- fBound |= Bind (addrBind, (BF_EXPLICIT | BF_REPORT_ERROR));
2168
+ fBound |= Bind (addrBind, (BF_EXPLICIT | BF_REPORT_ERROR), NetPermissionFlags::PF_NONE );
2150
2169
}
2151
2170
for (const auto & addrBind : whiteBinds) {
2152
- fBound |= Bind (addrBind, (BF_EXPLICIT | BF_REPORT_ERROR | BF_WHITELIST) );
2171
+ fBound |= Bind (addrBind. m_service , (BF_EXPLICIT | BF_REPORT_ERROR), addrBind. m_flags );
2153
2172
}
2154
2173
if (binds.empty () && whiteBinds.empty ()) {
2155
2174
struct in_addr inaddr_any;
2156
2175
inaddr_any.s_addr = INADDR_ANY;
2157
2176
struct in6_addr inaddr6_any = IN6ADDR_ANY_INIT;
2158
- fBound |= Bind (CService (inaddr6_any, GetListenPort ()), BF_NONE);
2159
- fBound |= Bind (CService (inaddr_any, GetListenPort ()), !fBound ? BF_REPORT_ERROR : BF_NONE);
2177
+ fBound |= Bind (CService (inaddr6_any, GetListenPort ()), BF_NONE, NetPermissionFlags::PF_NONE );
2178
+ fBound |= Bind (CService (inaddr_any, GetListenPort ()), !fBound ? BF_REPORT_ERROR : BF_NONE, NetPermissionFlags::PF_NONE );
2160
2179
}
2161
2180
return fBound ;
2162
2181
}
0 commit comments