From a3653c159e4f5c887eec9ea608e474eaa299fc07 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Thu, 13 Aug 2020 16:19:27 +0200 Subject: cli: tally peer connections by type --- src/bitcoin-cli.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'src/bitcoin-cli.cpp') diff --git a/src/bitcoin-cli.cpp b/src/bitcoin-cli.cpp index 1fb02b1a49..037b5fd06d 100644 --- a/src/bitcoin-cli.cpp +++ b/src/bitcoin-cli.cpp @@ -336,6 +336,39 @@ public: const std::vector batch{JSONRPCProcessBatchReply(batch_in)}; if (!batch[ID_PEERINFO]["error"].isNull()) return batch[ID_PEERINFO]; if (!batch[ID_NETWORKINFO]["error"].isNull()) return batch[ID_NETWORKINFO]; + + // Count peer connection totals. + int ipv4_i{0}, ipv6_i{0}, onion_i{0}, block_relay_i{0}; // inbound conn counters + int ipv4_o{0}, ipv6_o{0}, onion_o{0}, block_relay_o{0}; // outbound conn counters + const UniValue& getpeerinfo{batch[ID_PEERINFO]["result"]}; + + for (const UniValue& peer : getpeerinfo.getValues()) { + const std::string addr{peer["addr"].get_str()}; + const std::string addr_local{peer["addrlocal"].isNull() ? "" : peer["addrlocal"].get_str()}; + const int mapped_as{peer["mapped_as"].isNull() ? 0 : peer["mapped_as"].get_int()}; + const bool is_block_relay{!peer["relaytxes"].get_bool()}; + const bool is_inbound{peer["inbound"].get_bool()}; + if (is_inbound) { + if (IsAddrIPv6(addr)) { + ++ipv6_i; + } else if (IsInboundOnion(addr_local, mapped_as)) { + ++onion_i; + } else { + ++ipv4_i; + } + if (is_block_relay) ++block_relay_i; + } else { + if (IsAddrIPv6(addr)) { + ++ipv6_o; + } else if (IsOutboundOnion(addr, mapped_as)) { + ++onion_o; + } else { + ++ipv4_o; + } + if (is_block_relay) ++block_relay_o; + } + } + std::string result; return JSONRPCReplyObj(UniValue{result}, NullUniValue, 1); } -- cgit v1.2.3