Skip to content

Commit a3653c1

Browse files
committed
cli: tally peer connections by type
1 parent 54799b6 commit a3653c1

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/bitcoin-cli.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,39 @@ class NetinfoRequestHandler : public BaseRequestHandler
336336
const std::vector<UniValue> batch{JSONRPCProcessBatchReply(batch_in)};
337337
if (!batch[ID_PEERINFO]["error"].isNull()) return batch[ID_PEERINFO];
338338
if (!batch[ID_NETWORKINFO]["error"].isNull()) return batch[ID_NETWORKINFO];
339+
340+
// Count peer connection totals.
341+
int ipv4_i{0}, ipv6_i{0}, onion_i{0}, block_relay_i{0}; // inbound conn counters
342+
int ipv4_o{0}, ipv6_o{0}, onion_o{0}, block_relay_o{0}; // outbound conn counters
343+
const UniValue& getpeerinfo{batch[ID_PEERINFO]["result"]};
344+
345+
for (const UniValue& peer : getpeerinfo.getValues()) {
346+
const std::string addr{peer["addr"].get_str()};
347+
const std::string addr_local{peer["addrlocal"].isNull() ? "" : peer["addrlocal"].get_str()};
348+
const int mapped_as{peer["mapped_as"].isNull() ? 0 : peer["mapped_as"].get_int()};
349+
const bool is_block_relay{!peer["relaytxes"].get_bool()};
350+
const bool is_inbound{peer["inbound"].get_bool()};
351+
if (is_inbound) {
352+
if (IsAddrIPv6(addr)) {
353+
++ipv6_i;
354+
} else if (IsInboundOnion(addr_local, mapped_as)) {
355+
++onion_i;
356+
} else {
357+
++ipv4_i;
358+
}
359+
if (is_block_relay) ++block_relay_i;
360+
} else {
361+
if (IsAddrIPv6(addr)) {
362+
++ipv6_o;
363+
} else if (IsOutboundOnion(addr, mapped_as)) {
364+
++onion_o;
365+
} else {
366+
++ipv4_o;
367+
}
368+
if (is_block_relay) ++block_relay_o;
369+
}
370+
}
371+
339372
std::string result;
340373
return JSONRPCReplyObj(UniValue{result}, NullUniValue, 1);
341374
}

0 commit comments

Comments
 (0)