Skip to content

Commit 94e8882

Browse files
committed
rpc: Prevents adding the same ip more than once when formatted differently
Currently it is possible to add the same node twice when formatting IPs in different, yet equivalent, manner. This applies to both ipv4 and ipv6, e.g: 127.0.0.1 = 127.1 | [::1] = [0:0:0:0:0:0:0:1] `addnode` will accept both and display both as connected (given they translate to the same IP). This will not result in multiple connections to the same node, but will report redundant info when querying `getaddednodeinfo` and populate `m_added_nodes` with redundant data. This can be avoided performing comparing the contents of `m_added_addr` and the address to be added as `CServices` instead of as strings.
1 parent 2574b7e commit 94e8882

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/net.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3471,9 +3471,12 @@ std::vector<CAddress> CConnman::GetAddresses(CNode& requestor, size_t max_addres
34713471

34723472
bool CConnman::AddNode(const AddedNodeParams& add)
34733473
{
3474+
const CService resolved(LookupNumeric(add.m_added_node, GetDefaultPort(add.m_added_node)));
3475+
const bool resolved_is_valid{resolved.IsValid()};
3476+
34743477
LOCK(m_added_nodes_mutex);
34753478
for (const auto& it : m_added_node_params) {
3476-
if (add.m_added_node == it.m_added_node) return false;
3479+
if (add.m_added_node == it.m_added_node || (resolved_is_valid && resolved == LookupNumeric(it.m_added_node, GetDefaultPort(it.m_added_node)))) return false;
34773480
}
34783481

34793482
m_added_node_params.push_back(add);

test/functional/rpc_net.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,11 @@ def test_addnode_getaddednodeinfo(self):
215215
# add a node (node2) to node0
216216
ip_port = "127.0.0.1:{}".format(p2p_port(2))
217217
self.nodes[0].addnode(node=ip_port, command='add')
218+
# try to add an equivalent ip
219+
ip_port2 = "127.1:{}".format(p2p_port(2))
220+
assert_raises_rpc_error(-23, "Node already added", self.nodes[0].addnode, node=ip_port2, command='add')
218221
# check that the node has indeed been added
219-
added_nodes = self.nodes[0].getaddednodeinfo(ip_port)
222+
added_nodes = self.nodes[0].getaddednodeinfo()
220223
assert_equal(len(added_nodes), 1)
221224
assert_equal(added_nodes[0]['addednode'], ip_port)
222225
# check that node cannot be added again

0 commit comments

Comments
 (0)