Skip to content

Commit f9f5cfc

Browse files
committed
Prevent duplicate connections where one is by name and another by ip
1 parent 1111b80 commit f9f5cfc

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/net.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,26 @@ CNode* ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure
400400
return NULL;
401401
}
402402

403+
if (pszDest && addrConnect.IsValid()) {
404+
// It is possible that we already have a connection to the IP/port pszDest resolved to.
405+
// In that case, drop the connection that was just created, and return the existing CNode instead.
406+
// Also store the name we used to connect in that CNode, so that future FindNode() calls to that
407+
// name catch this early.
408+
CNode* pnode = FindNode((CService)addrConnect);
409+
if (pnode)
410+
{
411+
pnode->AddRef();
412+
{
413+
LOCK(cs_vNodes);
414+
if (pnode->addrName.empty()) {
415+
pnode->addrName = std::string(pszDest);
416+
}
417+
}
418+
CloseSocket(hSocket);
419+
return pnode;
420+
}
421+
}
422+
403423
addrman.Attempt(addrConnect, fCountFailure);
404424

405425
// Add node

0 commit comments

Comments
 (0)