From 098b500849bf2d0524bddca2e1a1b5422863e812 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Sat, 28 May 2016 16:22:02 +0200 Subject: [PATCH] Prevent duplicate connections where one is by name and another by ip --- src/net.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/net.cpp b/src/net.cpp index 9bb179a64..d38172b77 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -410,6 +410,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