@@ -348,8 +348,10 @@ bool CConnman::CheckIncomingNonce(uint64_t nonce)
348348CNode* CConnman::ConnectNode (CAddress addrConnect, const  char  *pszDest, bool  fCountFailure )
349349{
350350    if  (pszDest == NULL ) {
351-         if  (IsLocal (addrConnect))
351+         bool  fAllowLocal  = Params ().AllowMultiplePorts () && addrConnect.GetPort () != GetListenPort ();
352+         if  (!fAllowLocal  && IsLocal (addrConnect)) {
352353            return  NULL ;
354+         }
353355
354356        //  Look for an existing connection
355357        CNode* pnode = FindNode ((CService)addrConnect);
@@ -1786,7 +1788,12 @@ void CConnman::ThreadOpenConnections()
17861788            CAddrInfo addr = addrman.Select (fFeeler );
17871789
17881790            //  if we selected an invalid address, restart
1789-             if  (!addr.IsValid () || setConnected.count (addr.GetGroup ()) || IsLocal (addr))
1791+             if  (!addr.IsValid () || setConnected.count (addr.GetGroup ()))
1792+                 break ;
1793+ 
1794+             //  if we selected a local address, restart (local addresses are allowed in regtest and devnet)
1795+             bool  fAllowLocal  = Params ().AllowMultiplePorts () && addrConnect.GetPort () != GetListenPort ();
1796+             if  (!fAllowLocal  && IsLocal (addrConnect))
17901797                break ;
17911798
17921799            //  If we didn't find an appropriate destination after trying 100 addresses fetched from addrman,
@@ -1818,7 +1825,7 @@ void CConnman::ThreadOpenConnections()
18181825            }
18191826
18201827            //  do not allow non-default ports, unless after 50 invalid addresses selected already
1821-             if  (addr.GetPort () != Params ().GetDefaultPort () && nTries < 50 )
1828+             if  (addr.GetPort () != Params ().GetDefaultPort () && addr. GetPort () !=  GetListenPort () &&  nTries < 50 )
18221829                continue ;
18231830
18241831            addrConnect = addr;
@@ -1990,9 +1997,16 @@ bool CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
19901997        return  false ;
19911998    }
19921999    if  (!pszDest) {
1993-         if  (IsLocal (addrConnect) ||
1994-             FindNode ((CNetAddr)addrConnect) || IsBanned (addrConnect) ||
1995-             FindNode (addrConnect.ToStringIPPort ()))
2000+         //  banned or exact match?
2001+         if  (IsBanned (addrConnect) || FindNode (addrConnect.ToStringIPPort ()))
2002+             return  false ;
2003+         //  local and not a connection to itself?
2004+         bool  fAllowLocal  = Params ().AllowMultiplePorts () && addrConnect.GetPort () != GetListenPort ();
2005+         if  (!fAllowLocal  && IsLocal (addrConnect))
2006+             return  false ;
2007+         //  if multiple ports for same IP are allowed, search for IP:PORT match, otherwise search for IP-only match
2008+         if  ((!Params ().AllowMultiplePorts () && FindNode ((CNetAddr)addrConnect)) ||
2009+             (Params ().AllowMultiplePorts () && FindNode ((CService)addrConnect)))
19962010            return  false ;
19972011    } else  if  (FindNode (std::string (pszDest)))
19982012        return  false ;
@@ -2238,7 +2252,9 @@ void CConnman::SetNetworkActive(bool active)
22382252    uiInterface.NotifyNetworkActiveChanged (fNetworkActive );
22392253}
22402254
2241- CConnman::CConnman (uint64_t  nSeed0In, uint64_t  nSeed1In) : nSeed0(nSeed0In), nSeed1(nSeed1In)
2255+ CConnman::CConnman (uint64_t  nSeed0In, uint64_t  nSeed1In) :
2256+         nSeed0(nSeed0In), nSeed1(nSeed1In),
2257+         addrman(Params().AllowMultiplePorts())
22422258{
22432259    fNetworkActive  = true ;
22442260    setBannedIsDirty = false ;
0 commit comments