Skip to content

Commit 54799b6

Browse files
committed
cli: add ipv6 and onion address type detection helpers
1 parent 12242b1 commit 54799b6

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/bitcoin-cli.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ static const char DEFAULT_RPCCONNECT[] = "127.0.0.1";
3939
static const int DEFAULT_HTTP_CLIENT_TIMEOUT=900;
4040
static const bool DEFAULT_NAMED=false;
4141
static const int CONTINUE_EXECUTION=-1;
42+
static const std::string ONION{".onion"};
43+
static const size_t ONION_LEN{ONION.size()};
4244

4345
/** Default number of blocks to generate for RPC generatetoaddress. */
4446
static const std::string DEFAULT_NBLOCKS = "1";
@@ -297,6 +299,21 @@ class GetinfoRequestHandler: public BaseRequestHandler
297299
class NetinfoRequestHandler : public BaseRequestHandler
298300
{
299301
private:
302+
bool IsAddrIPv6(const std::string& addr) const
303+
{
304+
return !addr.empty() && addr.front() == '[';
305+
}
306+
bool IsInboundOnion(const std::string& addr_local, int mapped_as) const
307+
{
308+
return mapped_as == 0 && addr_local.find(ONION) != std::string::npos;
309+
}
310+
bool IsOutboundOnion(const std::string& addr, int mapped_as) const
311+
{
312+
const size_t addr_len{addr.size()};
313+
const size_t onion_pos{addr.rfind(ONION)};
314+
return mapped_as == 0 && onion_pos != std::string::npos && addr_len > ONION_LEN &&
315+
(onion_pos == addr_len - ONION_LEN || onion_pos == addr.find_last_of(":") - ONION_LEN);
316+
}
300317
bool m_verbose{false}; //!< Whether user requested verbose -netinfo report
301318
public:
302319
const int ID_PEERINFO = 0;

0 commit comments

Comments
 (0)