Skip to content

Commit

Permalink
Prevent duplicate connections where one is by name and another by ip
Browse files Browse the repository at this point in the history
  • Loading branch information
sipa committed Jun 13, 2016
1 parent 1111b80 commit f9f5cfc
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,26 @@ CNode* ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure
return NULL;
}

if (pszDest && addrConnect.IsValid()) {
// It is possible that we already have a connection to the IP/port pszDest resolved to.
// In that case, drop the connection that was just created, and return the existing CNode instead.
// Also store the name we used to connect in that CNode, so that future FindNode() calls to that
// name catch this early.
CNode* pnode = FindNode((CService)addrConnect);
if (pnode)
{
pnode->AddRef();
{
LOCK(cs_vNodes);
if (pnode->addrName.empty()) {
pnode->addrName = std::string(pszDest);
}
}
CloseSocket(hSocket);
return pnode;
}
}

addrman.Attempt(addrConnect, fCountFailure);

// Add node
Expand Down

0 comments on commit f9f5cfc

Please sign in to comment.