2019-01-07 15:41:14 +01:00
// Copyright (c) 2009-2019 The Bitcoin Core developers
2021-04-20 21:33:02 +02:00
// Copyright (c) 2014-2021 The Dash Core developers
2014-11-20 03:19:29 +01:00
// Distributed under the MIT software license, see the accompanying
2012-06-29 05:18:38 +02:00
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2013-11-18 01:25:17 +01:00
2020-03-19 23:46:56 +01:00
# include <rpc/server.h>
2013-11-18 01:25:17 +01:00
2019-01-21 18:45:59 +01:00
# include <banman.h>
2020-03-19 23:46:56 +01:00
# include <clientversion.h>
# include <core_io.h>
# include <net.h>
Merge #16248: Make whitebind/whitelist permissions more flexible
c5b404e8f1973afe071a07c63ba1038eefe13f0f Add functional tests for flexible whitebind/list (nicolas.dorier)
d541fa391844f658bd7035659b5b16695733dd56 Replace the use of fWhitelisted by permission checks (nicolas.dorier)
ecd5cf7ea4c3644a30092100ffc399e30e193275 Do not disconnect peer for asking mempool if it has NO_BAN permission (nicolas.dorier)
e5b26deaaa6842f7dd7c4537ede000f965ea0189 Make whitebind/whitelist permissions more flexible (nicolas.dorier)
Pull request description:
# Motivation
In 0.19, bloom filter will be disabled by default. I tried to make [a PR](https://github.com/bitcoin/bitcoin/pull/16176) to enable bloom filter for whitelisted peers regardless of `-peerbloomfilters`.
Bloom filter have non existent privacy and server can omit filter's matches. However, both problems are completely irrelevant when you connect to your own node. If you connect to your own node, bloom filters are the most bandwidth efficient way to synchronize your light client without the need of some middleware like Electrum.
It is also a superior alternative to BIP157 as it does not require to maintain an additional index and it would work well on pruned nodes.
When I attempted to allow bloom filters for whitelisted peer, my proposal has been NACKed in favor of [a more flexible approach](https://github.com/bitcoin/bitcoin/pull/16176#issuecomment-500762907) which should allow node operator to set fine grained permissions instead of a global `whitelisted` attribute.
Doing so will also make follow up idea very easy to implement in a backward compatible way.
# Implementation details
The PR propose a new format for `--white{list,bind}`. I added a way to specify permissions granted to inbound connection matching `white{list,bind}`.
The following permissions exists:
* ForceRelay
* Relay
* NoBan
* BloomFilter
* Mempool
Example:
* `-whitelist=bloomfilter@127.0.0.1/32`.
* `-whitebind=bloomfilter,relay,noban@127.0.0.1:10020`.
If no permissions are specified, `NoBan | Mempool` is assumed. (making this PR backward compatible)
When we receive an inbound connection, we calculate the effective permissions for this peer by fetching the permissions granted from `whitelist` and add to it the permissions granted from `whitebind`.
To keep backward compatibility, if no permissions are specified in `white{list,bind}` (e.g. `--whitelist=127.0.0.1`) then parameters `-whitelistforcerelay` and `-whiterelay` will add the permissions `ForceRelay` and `Relay` to the inbound node.
`-whitelistforcerelay` and `-whiterelay` are ignored if the permissions flags are explicitly set in `white{bind,list}`.
# Follow up idea
Based on this PR, other changes become quite easy to code in a trivially review-able, backward compatible way:
* Changing `connect` at rpc and config file level to understand the permissions flags.
* Changing the permissions of a peer at RPC level.
ACKs for top commit:
laanwj:
re-ACK c5b404e8f1973afe071a07c63ba1038eefe13f0f
Tree-SHA512: adfefb373d09e68cae401247c8fc64034e305694cdef104bdcdacb9f1704277bd53b18f52a2427a5cffdbc77bda410d221aed252bc2ece698ffbb9cf1b830577
2019-08-14 16:35:54 +02:00
# include <net_permissions.h>
2019-11-04 13:18:19 +01:00
# include <net_processing.h>
# include <net_types.h> // For banmap_t
2020-03-19 23:46:56 +01:00
# include <netbase.h>
2021-11-16 16:19:47 +01:00
# include <policy/settings.h>
2020-03-19 23:46:56 +01:00
# include <rpc/protocol.h>
2018-11-13 18:34:43 +01:00
# include <rpc/util.h>
2020-03-19 23:46:56 +01:00
# include <sync.h>
# include <timedata.h>
2021-06-27 08:33:13 +02:00
# include <util/strencodings.h>
2018-11-13 18:34:43 +01:00
# include <util/system.h>
# include <validation.h>
2020-03-19 23:46:56 +01:00
# include <version.h>
# include <warnings.h>
2014-09-14 12:43:56 +02:00
2015-09-04 16:11:34 +02:00
# include <univalue.h>
2012-06-29 05:18:38 +02:00
2018-05-04 22:42:39 +02:00
static UniValue getconnectioncount ( const JSONRPCRequest & request )
2012-06-29 05:18:38 +02:00
{
2016-10-19 15:01:33 +02:00
if ( request . fHelp | | request . params . size ( ) ! = 0 )
2017-03-09 08:14:27 +01:00
throw std : : runtime_error (
2021-10-11 23:55:23 +02:00
RPCHelpMan { " getconnectioncount " ,
2019-01-29 15:55:37 +01:00
" \n Returns the number of connections to other nodes. \n " ,
{ } ,
RPCResult {
2013-10-29 12:29:44 +01:00
" n (numeric) The connection count \n "
2019-01-29 15:55:37 +01:00
} ,
RPCExamples {
HelpExampleCli ( " getconnectioncount " , " " )
2013-10-29 12:29:44 +01:00
+ HelpExampleRpc ( " getconnectioncount " , " " )
2019-01-29 15:55:37 +01:00
} ,
} . ToString ( ) ) ;
2012-06-29 05:18:38 +02:00
Backport Bitcoin PR#8085: p2p: Begin encapsulation (#1537)
* net: move CBanDB and CAddrDB out of net.h/cpp
This will eventually solve a circular dependency
* net: Create CConnman to encapsulate p2p connections
* net: Move socket binding into CConnman
* net: move OpenNetworkConnection into CConnman
* net: move ban and addrman functions into CConnman
* net: Add oneshot functions to CConnman
* net: move added node functions to CConnman
* net: Add most functions needed for vNodes to CConnman
* net: handle nodesignals in CConnman
* net: Pass CConnection to wallet rather than using the global
* net: Add rpc error for missing/disabled p2p functionality
* net: Pass CConnman around as needed
* gui: add NodeID to the peer table
* net: create generic functor accessors and move vNodes to CConnman
* net: move whitelist functions into CConnman
* net: move nLastNodeId to CConnman
* net: move nLocalHostNonce to CConnman
This behavior seems to have been quite racy and broken.
Move nLocalHostNonce into CNode, and check received nonces against all
non-fully-connected nodes. If there's a match, assume we've connected
to ourself.
* net: move messageHandlerCondition to CConnman
* net: move send/recv statistics to CConnman
* net: move SendBufferSize/ReceiveFloodSize to CConnman
* net: move nLocalServices/nRelevantServices to CConnman
These are in-turn passed to CNode at connection time. This allows us to offer
different services to different peers (or test the effects of doing so).
* net: move semOutbound and semMasternodeOutbound to CConnman
* net: SocketSendData returns written size
* net: move max/max-outbound to CConnman
* net: Pass best block known height into CConnman
CConnman then passes the current best height into CNode at creation time.
This way CConnman/CNode have no dependency on main for height, and the signals
only move in one direction.
This also helps to prevent identity leakage a tiny bit. Before this change, an
attacker could theoretically make 2 connections on different interfaces. They
would connect fully on one, and only establish the initial connection on the
other. Once they receive a new block, they would relay it to your first
connection, and immediately commence the version handshake on the second. Since
the new block height is reflected immediately, they could attempt to learn
whether the two connections were correlated.
This is, of course, incredibly unlikely to work due to the small timings
involved and receipt from other senders. But it doesn't hurt to lock-in
nBestHeight at the time of connection, rather than letting the remote choose
the time.
* net: pass CClientUIInterface into CConnman
* net: Drop StartNode/StopNode and use CConnman directly
* net: Introduce CConnection::Options to avoid passing so many params
* net: add nSendBufferMaxSize/nReceiveFloodSize to CConnection::Options
* net: move vNodesDisconnected into CConnman
* Made the ForEachNode* functions in src/net.cpp more pragmatic and self documenting
* Convert ForEachNode* functions to take a templated function argument rather than a std::function to eliminate std::function overhead
* net: move MAX_FEELER_CONNECTIONS into connman
2017-07-21 11:35:19 +02:00
if ( ! g_connman )
throw JSONRPCError ( RPC_CLIENT_P2P_DISABLED , " Error: Peer-to-peer functionality missing or disabled " ) ;
2014-10-19 10:46:17 +02:00
Backport Bitcoin PR#8085: p2p: Begin encapsulation (#1537)
* net: move CBanDB and CAddrDB out of net.h/cpp
This will eventually solve a circular dependency
* net: Create CConnman to encapsulate p2p connections
* net: Move socket binding into CConnman
* net: move OpenNetworkConnection into CConnman
* net: move ban and addrman functions into CConnman
* net: Add oneshot functions to CConnman
* net: move added node functions to CConnman
* net: Add most functions needed for vNodes to CConnman
* net: handle nodesignals in CConnman
* net: Pass CConnection to wallet rather than using the global
* net: Add rpc error for missing/disabled p2p functionality
* net: Pass CConnman around as needed
* gui: add NodeID to the peer table
* net: create generic functor accessors and move vNodes to CConnman
* net: move whitelist functions into CConnman
* net: move nLastNodeId to CConnman
* net: move nLocalHostNonce to CConnman
This behavior seems to have been quite racy and broken.
Move nLocalHostNonce into CNode, and check received nonces against all
non-fully-connected nodes. If there's a match, assume we've connected
to ourself.
* net: move messageHandlerCondition to CConnman
* net: move send/recv statistics to CConnman
* net: move SendBufferSize/ReceiveFloodSize to CConnman
* net: move nLocalServices/nRelevantServices to CConnman
These are in-turn passed to CNode at connection time. This allows us to offer
different services to different peers (or test the effects of doing so).
* net: move semOutbound and semMasternodeOutbound to CConnman
* net: SocketSendData returns written size
* net: move max/max-outbound to CConnman
* net: Pass best block known height into CConnman
CConnman then passes the current best height into CNode at creation time.
This way CConnman/CNode have no dependency on main for height, and the signals
only move in one direction.
This also helps to prevent identity leakage a tiny bit. Before this change, an
attacker could theoretically make 2 connections on different interfaces. They
would connect fully on one, and only establish the initial connection on the
other. Once they receive a new block, they would relay it to your first
connection, and immediately commence the version handshake on the second. Since
the new block height is reflected immediately, they could attempt to learn
whether the two connections were correlated.
This is, of course, incredibly unlikely to work due to the small timings
involved and receipt from other senders. But it doesn't hurt to lock-in
nBestHeight at the time of connection, rather than letting the remote choose
the time.
* net: pass CClientUIInterface into CConnman
* net: Drop StartNode/StopNode and use CConnman directly
* net: Introduce CConnection::Options to avoid passing so many params
* net: add nSendBufferMaxSize/nReceiveFloodSize to CConnection::Options
* net: move vNodesDisconnected into CConnman
* Made the ForEachNode* functions in src/net.cpp more pragmatic and self documenting
* Convert ForEachNode* functions to take a templated function argument rather than a std::function to eliminate std::function overhead
* net: move MAX_FEELER_CONNECTIONS into connman
2017-07-21 11:35:19 +02:00
return ( int ) g_connman - > GetNodeCount ( CConnman : : CONNECTIONS_ALL ) ;
2012-06-29 05:18:38 +02:00
}
2018-05-04 22:42:39 +02:00
static UniValue ping ( const JSONRPCRequest & request )
2013-08-22 13:34:33 +02:00
{
2016-10-19 15:01:33 +02:00
if ( request . fHelp | | request . params . size ( ) ! = 0 )
2017-03-09 08:14:27 +01:00
throw std : : runtime_error (
2021-10-11 23:55:23 +02:00
RPCHelpMan { " ping " ,
" \n Requests that a ping be sent to all other nodes, to measure ping time. \n "
" Results provided in getpeerinfo, pingtime and pingwait fields are decimal seconds. \n "
" Ping command is handled in queue with all other commands, so it measures processing backlog, not just network ping. \n " ,
2019-01-29 15:55:37 +01:00
{ } ,
RPCResults { } ,
RPCExamples {
HelpExampleCli ( " ping " , " " )
2013-10-29 12:29:44 +01:00
+ HelpExampleRpc ( " ping " , " " )
2019-01-29 15:55:37 +01:00
} ,
} . ToString ( ) ) ;
2013-10-23 16:19:49 +02:00
Backport Bitcoin PR#8085: p2p: Begin encapsulation (#1537)
* net: move CBanDB and CAddrDB out of net.h/cpp
This will eventually solve a circular dependency
* net: Create CConnman to encapsulate p2p connections
* net: Move socket binding into CConnman
* net: move OpenNetworkConnection into CConnman
* net: move ban and addrman functions into CConnman
* net: Add oneshot functions to CConnman
* net: move added node functions to CConnman
* net: Add most functions needed for vNodes to CConnman
* net: handle nodesignals in CConnman
* net: Pass CConnection to wallet rather than using the global
* net: Add rpc error for missing/disabled p2p functionality
* net: Pass CConnman around as needed
* gui: add NodeID to the peer table
* net: create generic functor accessors and move vNodes to CConnman
* net: move whitelist functions into CConnman
* net: move nLastNodeId to CConnman
* net: move nLocalHostNonce to CConnman
This behavior seems to have been quite racy and broken.
Move nLocalHostNonce into CNode, and check received nonces against all
non-fully-connected nodes. If there's a match, assume we've connected
to ourself.
* net: move messageHandlerCondition to CConnman
* net: move send/recv statistics to CConnman
* net: move SendBufferSize/ReceiveFloodSize to CConnman
* net: move nLocalServices/nRelevantServices to CConnman
These are in-turn passed to CNode at connection time. This allows us to offer
different services to different peers (or test the effects of doing so).
* net: move semOutbound and semMasternodeOutbound to CConnman
* net: SocketSendData returns written size
* net: move max/max-outbound to CConnman
* net: Pass best block known height into CConnman
CConnman then passes the current best height into CNode at creation time.
This way CConnman/CNode have no dependency on main for height, and the signals
only move in one direction.
This also helps to prevent identity leakage a tiny bit. Before this change, an
attacker could theoretically make 2 connections on different interfaces. They
would connect fully on one, and only establish the initial connection on the
other. Once they receive a new block, they would relay it to your first
connection, and immediately commence the version handshake on the second. Since
the new block height is reflected immediately, they could attempt to learn
whether the two connections were correlated.
This is, of course, incredibly unlikely to work due to the small timings
involved and receipt from other senders. But it doesn't hurt to lock-in
nBestHeight at the time of connection, rather than letting the remote choose
the time.
* net: pass CClientUIInterface into CConnman
* net: Drop StartNode/StopNode and use CConnman directly
* net: Introduce CConnection::Options to avoid passing so many params
* net: add nSendBufferMaxSize/nReceiveFloodSize to CConnection::Options
* net: move vNodesDisconnected into CConnman
* Made the ForEachNode* functions in src/net.cpp more pragmatic and self documenting
* Convert ForEachNode* functions to take a templated function argument rather than a std::function to eliminate std::function overhead
* net: move MAX_FEELER_CONNECTIONS into connman
2017-07-21 11:35:19 +02:00
if ( ! g_connman )
throw JSONRPCError ( RPC_CLIENT_P2P_DISABLED , " Error: Peer-to-peer functionality missing or disabled " ) ;
2013-08-22 13:34:33 +02:00
Backport Bitcoin PR#8085: p2p: Begin encapsulation (#1537)
* net: move CBanDB and CAddrDB out of net.h/cpp
This will eventually solve a circular dependency
* net: Create CConnman to encapsulate p2p connections
* net: Move socket binding into CConnman
* net: move OpenNetworkConnection into CConnman
* net: move ban and addrman functions into CConnman
* net: Add oneshot functions to CConnman
* net: move added node functions to CConnman
* net: Add most functions needed for vNodes to CConnman
* net: handle nodesignals in CConnman
* net: Pass CConnection to wallet rather than using the global
* net: Add rpc error for missing/disabled p2p functionality
* net: Pass CConnman around as needed
* gui: add NodeID to the peer table
* net: create generic functor accessors and move vNodes to CConnman
* net: move whitelist functions into CConnman
* net: move nLastNodeId to CConnman
* net: move nLocalHostNonce to CConnman
This behavior seems to have been quite racy and broken.
Move nLocalHostNonce into CNode, and check received nonces against all
non-fully-connected nodes. If there's a match, assume we've connected
to ourself.
* net: move messageHandlerCondition to CConnman
* net: move send/recv statistics to CConnman
* net: move SendBufferSize/ReceiveFloodSize to CConnman
* net: move nLocalServices/nRelevantServices to CConnman
These are in-turn passed to CNode at connection time. This allows us to offer
different services to different peers (or test the effects of doing so).
* net: move semOutbound and semMasternodeOutbound to CConnman
* net: SocketSendData returns written size
* net: move max/max-outbound to CConnman
* net: Pass best block known height into CConnman
CConnman then passes the current best height into CNode at creation time.
This way CConnman/CNode have no dependency on main for height, and the signals
only move in one direction.
This also helps to prevent identity leakage a tiny bit. Before this change, an
attacker could theoretically make 2 connections on different interfaces. They
would connect fully on one, and only establish the initial connection on the
other. Once they receive a new block, they would relay it to your first
connection, and immediately commence the version handshake on the second. Since
the new block height is reflected immediately, they could attempt to learn
whether the two connections were correlated.
This is, of course, incredibly unlikely to work due to the small timings
involved and receipt from other senders. But it doesn't hurt to lock-in
nBestHeight at the time of connection, rather than letting the remote choose
the time.
* net: pass CClientUIInterface into CConnman
* net: Drop StartNode/StopNode and use CConnman directly
* net: Introduce CConnection::Options to avoid passing so many params
* net: add nSendBufferMaxSize/nReceiveFloodSize to CConnection::Options
* net: move vNodesDisconnected into CConnman
* Made the ForEachNode* functions in src/net.cpp more pragmatic and self documenting
* Convert ForEachNode* functions to take a templated function argument rather than a std::function to eliminate std::function overhead
* net: move MAX_FEELER_CONNECTIONS into connman
2017-07-21 11:35:19 +02:00
// Request that each node send a ping during next message processing pass
g_connman - > ForEachNode ( [ ] ( CNode * pnode ) {
pnode - > fPingQueued = true ;
} ) ;
2014-08-20 21:15:16 +02:00
return NullUniValue ;
2013-08-22 13:34:33 +02:00
}
2018-05-04 22:42:39 +02:00
static UniValue getpeerinfo ( const JSONRPCRequest & request )
2012-06-29 23:24:53 +02:00
{
2016-10-19 15:01:33 +02:00
if ( request . fHelp | | request . params . size ( ) ! = 0 )
2017-03-09 08:14:27 +01:00
throw std : : runtime_error (
2021-10-11 23:55:23 +02:00
RPCHelpMan { " getpeerinfo " ,
2019-01-29 15:55:37 +01:00
" \n Returns data about each connected network node as a json array of objects. \n " ,
{ } ,
RPCResult {
2013-10-29 12:29:44 +01:00
" [ \n "
" { \n "
2014-07-15 16:13:10 +02:00
" \" id \" : n, (numeric) Peer index \n "
2017-06-22 20:36:16 +02:00
" \" addr \" : \" host:port \" , (string) The IP address and port of the peer \n "
2017-06-05 15:39:11 +02:00
" \" addrbind \" : \" ip:port \" , (string) Bind address of the connection to the peer \n "
" \" addrlocal \" : \" ip:port \" , (string) Local address as reported by the peer \n "
2021-05-11 17:11:07 +02:00
" \" mapped_as \" : \" mapped_as \" , (string) The AS in the BGP route to the peer used for diversifying peer selection \n "
2014-06-06 09:15:55 +02:00
" \" services \" : \" xxxxxxxxxxxxxxxx \" , (string) The services offered \n "
2021-07-19 12:39:22 +02:00
" \" verified_proregtx_hash \" : h, (hex) Only present when the peer is a masternode and successfully \n "
2021-01-22 23:12:22 +01:00
" authenticated via MNAUTH. In this case, this field contains the \n "
2019-12-06 10:05:58 +01:00
" protx hash of the masternode \n "
2021-07-19 12:39:22 +02:00
" \" verified_pubkey_hash \" : h, (hex) Only present when the peer is a masternode and successfully \n "
2021-01-22 23:12:22 +01:00
" authenticated via MNAUTH. In this case, this field contains the \n "
" hash of the masternode's operator public key \n "
2019-03-01 17:06:27 +01:00
" \" servicesnames \" :[ (array) the services offered, in human-readable form \n "
" \" SERVICE_NAME \" , (string) the service name if it is recognised \n "
" ... \n "
" ], \n "
2015-11-21 00:51:44 +01:00
" \" relaytxes \" :true|false, (boolean) Whether peer has asked us to relay transactions to it \n "
2013-10-29 12:29:44 +01:00
" \" lastsend \" : ttt, (numeric) The time in seconds since epoch (Jan 1 1970 GMT) of the last send \n "
" \" lastrecv \" : ttt, (numeric) The time in seconds since epoch (Jan 1 1970 GMT) of the last receive \n "
" \" bytessent \" : n, (numeric) The total bytes sent \n "
" \" bytesrecv \" : n, (numeric) The total bytes received \n "
" \" conntime \" : ttt, (numeric) The connection time in seconds since epoch (Jan 1 1970 GMT) \n "
2014-12-15 11:06:15 +01:00
" \" timeoffset \" : ttt, (numeric) The time offset in seconds \n "
2017-09-09 09:04:02 +02:00
" \" pingtime \" : n, (numeric) ping time (if available) \n "
" \" minping \" : n, (numeric) minimum observed ping time (if any at all) \n "
" \" pingwait \" : n, (numeric) ping wait (if non-zero) \n "
2018-02-27 15:56:25 +01:00
" \" version \" : v, (numeric) The peer version, such as 70001 \n "
2015-07-07 00:40:38 +02:00
" \" subver \" : \" /Dash Core:x.x.x/ \" , (string) The string version \n "
2013-10-29 12:29:44 +01:00
" \" inbound \" : true|false, (boolean) Inbound (true) or Outbound (false) \n "
2017-10-14 00:25:16 +02:00
" \" addnode \" : true|false, (boolean) Whether connection was due to addnode/-connect or if it was an automatic/inbound connection \n "
2020-03-20 12:22:05 +01:00
" \" masternode \" : true|false, (boolean) Whether connection was due to masternode connection attempt \n "
2013-10-29 12:29:44 +01:00
" \" startingheight \" : n, (numeric) The starting height (block) of the peer \n "
2014-09-16 09:00:36 +02:00
" \" banscore \" : n, (numeric) The ban score \n "
2014-07-12 00:03:10 +02:00
" \" synced_headers \" : n, (numeric) The last header we have in common with this peer \n "
" \" synced_blocks \" : n, (numeric) The last block we have in common with this peer \n "
" \" inflight \" : [ \n "
" n, (numeric) The heights of blocks we're currently asking from this peer \n "
" ... \n "
2017-01-12 02:09:29 +01:00
" ], \n "
2017-03-09 08:14:27 +01:00
" \" whitelisted \" : true|false, (boolean) Whether the peer is whitelisted \n "
2017-06-29 03:51:10 +02:00
" \" bytessent_per_msg \" : { \n "
2019-01-07 15:41:14 +01:00
" \" msg \" : n, (numeric) The total bytes sent aggregated by message type \n "
" When a message type is not listed in this json object, the bytes sent are 0. \n "
" Only known message types can appear as keys in the object. \n "
2017-06-29 03:51:10 +02:00
" ... \n "
2017-01-12 02:09:29 +01:00
" }, \n "
2017-06-29 03:51:10 +02:00
" \" bytesrecv_per_msg \" : { \n "
2019-01-07 15:41:14 +01:00
" \" msg \" : n, (numeric) The total bytes received aggregated by message type \n "
" When a message type is not listed in this json object, the bytes received are 0. \n "
" Only known message types can appear as keys in the object and all bytes received of unknown message types are listed under ' " + NET_MESSAGE_COMMAND_OTHER + " '. \n "
2017-06-29 03:51:10 +02:00
" ... \n "
" } \n "
2013-10-29 12:29:44 +01:00
" } \n "
" ,... \n "
2014-07-17 15:09:03 +02:00
" ] \n "
2019-01-29 15:55:37 +01:00
} ,
RPCExamples {
HelpExampleCli ( " getpeerinfo " , " " )
2013-10-29 12:29:44 +01:00
+ HelpExampleRpc ( " getpeerinfo " , " " )
2019-01-29 15:55:37 +01:00
} ,
} . ToString ( ) ) ;
2012-06-29 23:24:53 +02:00
Backport Bitcoin PR#8085: p2p: Begin encapsulation (#1537)
* net: move CBanDB and CAddrDB out of net.h/cpp
This will eventually solve a circular dependency
* net: Create CConnman to encapsulate p2p connections
* net: Move socket binding into CConnman
* net: move OpenNetworkConnection into CConnman
* net: move ban and addrman functions into CConnman
* net: Add oneshot functions to CConnman
* net: move added node functions to CConnman
* net: Add most functions needed for vNodes to CConnman
* net: handle nodesignals in CConnman
* net: Pass CConnection to wallet rather than using the global
* net: Add rpc error for missing/disabled p2p functionality
* net: Pass CConnman around as needed
* gui: add NodeID to the peer table
* net: create generic functor accessors and move vNodes to CConnman
* net: move whitelist functions into CConnman
* net: move nLastNodeId to CConnman
* net: move nLocalHostNonce to CConnman
This behavior seems to have been quite racy and broken.
Move nLocalHostNonce into CNode, and check received nonces against all
non-fully-connected nodes. If there's a match, assume we've connected
to ourself.
* net: move messageHandlerCondition to CConnman
* net: move send/recv statistics to CConnman
* net: move SendBufferSize/ReceiveFloodSize to CConnman
* net: move nLocalServices/nRelevantServices to CConnman
These are in-turn passed to CNode at connection time. This allows us to offer
different services to different peers (or test the effects of doing so).
* net: move semOutbound and semMasternodeOutbound to CConnman
* net: SocketSendData returns written size
* net: move max/max-outbound to CConnman
* net: Pass best block known height into CConnman
CConnman then passes the current best height into CNode at creation time.
This way CConnman/CNode have no dependency on main for height, and the signals
only move in one direction.
This also helps to prevent identity leakage a tiny bit. Before this change, an
attacker could theoretically make 2 connections on different interfaces. They
would connect fully on one, and only establish the initial connection on the
other. Once they receive a new block, they would relay it to your first
connection, and immediately commence the version handshake on the second. Since
the new block height is reflected immediately, they could attempt to learn
whether the two connections were correlated.
This is, of course, incredibly unlikely to work due to the small timings
involved and receipt from other senders. But it doesn't hurt to lock-in
nBestHeight at the time of connection, rather than letting the remote choose
the time.
* net: pass CClientUIInterface into CConnman
* net: Drop StartNode/StopNode and use CConnman directly
* net: Introduce CConnection::Options to avoid passing so many params
* net: add nSendBufferMaxSize/nReceiveFloodSize to CConnection::Options
* net: move vNodesDisconnected into CConnman
* Made the ForEachNode* functions in src/net.cpp more pragmatic and self documenting
* Convert ForEachNode* functions to take a templated function argument rather than a std::function to eliminate std::function overhead
* net: move MAX_FEELER_CONNECTIONS into connman
2017-07-21 11:35:19 +02:00
if ( ! g_connman )
throw JSONRPCError ( RPC_CLIENT_P2P_DISABLED , " Error: Peer-to-peer functionality missing or disabled " ) ;
2014-10-19 10:46:17 +02:00
2017-03-09 08:14:27 +01:00
std : : vector < CNodeStats > vstats ;
Backport Bitcoin PR#8085: p2p: Begin encapsulation (#1537)
* net: move CBanDB and CAddrDB out of net.h/cpp
This will eventually solve a circular dependency
* net: Create CConnman to encapsulate p2p connections
* net: Move socket binding into CConnman
* net: move OpenNetworkConnection into CConnman
* net: move ban and addrman functions into CConnman
* net: Add oneshot functions to CConnman
* net: move added node functions to CConnman
* net: Add most functions needed for vNodes to CConnman
* net: handle nodesignals in CConnman
* net: Pass CConnection to wallet rather than using the global
* net: Add rpc error for missing/disabled p2p functionality
* net: Pass CConnman around as needed
* gui: add NodeID to the peer table
* net: create generic functor accessors and move vNodes to CConnman
* net: move whitelist functions into CConnman
* net: move nLastNodeId to CConnman
* net: move nLocalHostNonce to CConnman
This behavior seems to have been quite racy and broken.
Move nLocalHostNonce into CNode, and check received nonces against all
non-fully-connected nodes. If there's a match, assume we've connected
to ourself.
* net: move messageHandlerCondition to CConnman
* net: move send/recv statistics to CConnman
* net: move SendBufferSize/ReceiveFloodSize to CConnman
* net: move nLocalServices/nRelevantServices to CConnman
These are in-turn passed to CNode at connection time. This allows us to offer
different services to different peers (or test the effects of doing so).
* net: move semOutbound and semMasternodeOutbound to CConnman
* net: SocketSendData returns written size
* net: move max/max-outbound to CConnman
* net: Pass best block known height into CConnman
CConnman then passes the current best height into CNode at creation time.
This way CConnman/CNode have no dependency on main for height, and the signals
only move in one direction.
This also helps to prevent identity leakage a tiny bit. Before this change, an
attacker could theoretically make 2 connections on different interfaces. They
would connect fully on one, and only establish the initial connection on the
other. Once they receive a new block, they would relay it to your first
connection, and immediately commence the version handshake on the second. Since
the new block height is reflected immediately, they could attempt to learn
whether the two connections were correlated.
This is, of course, incredibly unlikely to work due to the small timings
involved and receipt from other senders. But it doesn't hurt to lock-in
nBestHeight at the time of connection, rather than letting the remote choose
the time.
* net: pass CClientUIInterface into CConnman
* net: Drop StartNode/StopNode and use CConnman directly
* net: Introduce CConnection::Options to avoid passing so many params
* net: add nSendBufferMaxSize/nReceiveFloodSize to CConnection::Options
* net: move vNodesDisconnected into CConnman
* Made the ForEachNode* functions in src/net.cpp more pragmatic and self documenting
* Convert ForEachNode* functions to take a templated function argument rather than a std::function to eliminate std::function overhead
* net: move MAX_FEELER_CONNECTIONS into connman
2017-07-21 11:35:19 +02:00
g_connman - > GetNodeStats ( vstats ) ;
2012-06-29 23:24:53 +02:00
2015-05-10 14:48:35 +02:00
UniValue ret ( UniValue : : VARR ) ;
2012-06-29 23:24:53 +02:00
2019-07-05 09:06:28 +02:00
for ( const CNodeStats & stats : vstats ) {
2015-05-10 14:48:35 +02:00
UniValue obj ( UniValue : : VOBJ ) ;
2013-11-18 01:25:17 +01:00
CNodeStateStats statestats ;
bool fStateStats = GetNodeStateStats ( stats . nodeid , statestats ) ;
2020-06-18 11:17:23 +02:00
obj . pushKV ( " id " , stats . nodeid ) ;
obj . pushKV ( " addr " , stats . addrName ) ;
2013-08-22 07:50:19 +02:00
if ( ! ( stats . addrLocal . empty ( ) ) )
2020-06-18 11:17:23 +02:00
obj . pushKV ( " addrlocal " , stats . addrLocal ) ;
2017-06-05 15:39:11 +02:00
if ( stats . addrBind . IsValid ( ) )
2020-06-18 11:17:23 +02:00
obj . pushKV ( " addrbind " , stats . addrBind . ToString ( ) ) ;
2021-05-11 17:11:07 +02:00
if ( stats . m_mapped_as ! = 0 ) {
obj . pushKV ( " mapped_as " , uint64_t ( stats . m_mapped_as ) ) ;
}
2020-06-18 11:17:23 +02:00
obj . pushKV ( " services " , strprintf ( " %016x " , stats . nServices ) ) ;
2019-12-06 10:05:58 +01:00
if ( ! stats . verifiedProRegTxHash . IsNull ( ) ) {
2020-06-18 11:17:23 +02:00
obj . pushKV ( " verified_proregtx_hash " , stats . verifiedProRegTxHash . ToString ( ) ) ;
2019-12-06 10:05:58 +01:00
}
2021-01-22 23:12:22 +01:00
if ( ! stats . verifiedPubKeyHash . IsNull ( ) ) {
obj . pushKV ( " verified_pubkey_hash " , stats . verifiedPubKeyHash . ToString ( ) ) ;
}
2019-03-01 17:06:27 +01:00
obj . pushKV ( " servicesnames " , GetServicesNames ( stats . nServices ) ) ;
2020-06-18 11:17:23 +02:00
obj . pushKV ( " relaytxes " , stats . fRelayTxes ) ;
obj . pushKV ( " lastsend " , stats . nLastSend ) ;
obj . pushKV ( " lastrecv " , stats . nLastRecv ) ;
obj . pushKV ( " bytessent " , stats . nSendBytes ) ;
obj . pushKV ( " bytesrecv " , stats . nRecvBytes ) ;
obj . pushKV ( " conntime " , stats . nTimeConnected ) ;
obj . pushKV ( " timeoffset " , stats . nTimeOffset ) ;
2020-03-03 14:35:01 +01:00
if ( stats . m_ping_usec > 0 ) {
obj . pushKV ( " pingtime " , ( ( double ) stats . m_ping_usec ) / 1e6 ) ;
}
if ( stats . m_min_ping_usec < std : : numeric_limits < int64_t > : : max ( ) ) {
obj . pushKV ( " minping " , ( ( double ) stats . m_min_ping_usec ) / 1e6 ) ;
}
if ( stats . m_ping_wait_usec > 0 ) {
obj . pushKV ( " pingwait " , ( ( double ) stats . m_ping_wait_usec ) / 1e6 ) ;
}
2020-06-18 11:17:23 +02:00
obj . pushKV ( " version " , stats . nVersion ) ;
2013-11-26 12:52:21 +01:00
// Use the sanitized form of subver here, to avoid tricksy remote peers from
2017-01-29 16:05:06 +01:00
// corrupting or modifying the JSON output by putting special characters in
2013-11-26 12:52:21 +01:00
// their ver message.
2020-06-18 11:17:23 +02:00
obj . pushKV ( " subver " , stats . cleanSubVer ) ;
obj . pushKV ( " inbound " , stats . fInbound ) ;
obj . pushKV ( " addnode " , stats . m_manual_connection ) ;
2021-01-14 20:59:18 +01:00
obj . pushKV ( " masternode " , stats . m_masternode_connection ) ;
2020-06-18 11:17:23 +02:00
obj . pushKV ( " startingheight " , stats . nStartingHeight ) ;
2013-11-18 01:25:17 +01:00
if ( fStateStats ) {
2020-06-18 11:17:23 +02:00
obj . pushKV ( " banscore " , statestats . nMisbehavior ) ;
obj . pushKV ( " synced_headers " , statestats . nSyncHeight ) ;
obj . pushKV ( " synced_blocks " , statestats . nCommonHeight ) ;
2015-05-10 14:48:35 +02:00
UniValue heights ( UniValue : : VARR ) ;
2018-09-04 15:36:09 +02:00
for ( const int height : statestats . vHeightInFlight ) {
2014-07-12 00:03:10 +02:00
heights . push_back ( height ) ;
}
2020-06-18 11:17:23 +02:00
obj . pushKV ( " inflight " , heights ) ;
2013-11-18 01:25:17 +01:00
}
Merge #16248: Make whitebind/whitelist permissions more flexible
c5b404e8f1973afe071a07c63ba1038eefe13f0f Add functional tests for flexible whitebind/list (nicolas.dorier)
d541fa391844f658bd7035659b5b16695733dd56 Replace the use of fWhitelisted by permission checks (nicolas.dorier)
ecd5cf7ea4c3644a30092100ffc399e30e193275 Do not disconnect peer for asking mempool if it has NO_BAN permission (nicolas.dorier)
e5b26deaaa6842f7dd7c4537ede000f965ea0189 Make whitebind/whitelist permissions more flexible (nicolas.dorier)
Pull request description:
# Motivation
In 0.19, bloom filter will be disabled by default. I tried to make [a PR](https://github.com/bitcoin/bitcoin/pull/16176) to enable bloom filter for whitelisted peers regardless of `-peerbloomfilters`.
Bloom filter have non existent privacy and server can omit filter's matches. However, both problems are completely irrelevant when you connect to your own node. If you connect to your own node, bloom filters are the most bandwidth efficient way to synchronize your light client without the need of some middleware like Electrum.
It is also a superior alternative to BIP157 as it does not require to maintain an additional index and it would work well on pruned nodes.
When I attempted to allow bloom filters for whitelisted peer, my proposal has been NACKed in favor of [a more flexible approach](https://github.com/bitcoin/bitcoin/pull/16176#issuecomment-500762907) which should allow node operator to set fine grained permissions instead of a global `whitelisted` attribute.
Doing so will also make follow up idea very easy to implement in a backward compatible way.
# Implementation details
The PR propose a new format for `--white{list,bind}`. I added a way to specify permissions granted to inbound connection matching `white{list,bind}`.
The following permissions exists:
* ForceRelay
* Relay
* NoBan
* BloomFilter
* Mempool
Example:
* `-whitelist=bloomfilter@127.0.0.1/32`.
* `-whitebind=bloomfilter,relay,noban@127.0.0.1:10020`.
If no permissions are specified, `NoBan | Mempool` is assumed. (making this PR backward compatible)
When we receive an inbound connection, we calculate the effective permissions for this peer by fetching the permissions granted from `whitelist` and add to it the permissions granted from `whitebind`.
To keep backward compatibility, if no permissions are specified in `white{list,bind}` (e.g. `--whitelist=127.0.0.1`) then parameters `-whitelistforcerelay` and `-whiterelay` will add the permissions `ForceRelay` and `Relay` to the inbound node.
`-whitelistforcerelay` and `-whiterelay` are ignored if the permissions flags are explicitly set in `white{bind,list}`.
# Follow up idea
Based on this PR, other changes become quite easy to code in a trivially review-able, backward compatible way:
* Changing `connect` at rpc and config file level to understand the permissions flags.
* Changing the permissions of a peer at RPC level.
ACKs for top commit:
laanwj:
re-ACK c5b404e8f1973afe071a07c63ba1038eefe13f0f
Tree-SHA512: adfefb373d09e68cae401247c8fc64034e305694cdef104bdcdacb9f1704277bd53b18f52a2427a5cffdbc77bda410d221aed252bc2ece698ffbb9cf1b830577
2019-08-14 16:35:54 +02:00
obj . pushKV ( " whitelisted " , stats . m_legacyWhitelisted ) ;
UniValue permissions ( UniValue : : VARR ) ;
for ( const auto & permission : NetPermissions : : ToStrings ( stats . m_permissionFlags ) ) {
permissions . push_back ( permission ) ;
}
obj . pushKV ( " permissions " , permissions ) ;
2012-06-29 23:24:53 +02:00
2017-06-29 03:51:10 +02:00
UniValue sendPerMsgCmd ( UniValue : : VOBJ ) ;
2019-01-07 15:41:14 +01:00
for ( const auto & i : stats . mapSendBytesPerMsgCmd ) {
2017-06-29 03:51:10 +02:00
if ( i . second > 0 )
2020-06-18 11:17:23 +02:00
sendPerMsgCmd . pushKV ( i . first , i . second ) ;
2017-06-29 03:51:10 +02:00
}
2020-06-18 11:17:23 +02:00
obj . pushKV ( " bytessent_per_msg " , sendPerMsgCmd ) ;
2017-06-29 03:51:10 +02:00
UniValue recvPerMsgCmd ( UniValue : : VOBJ ) ;
2019-01-07 15:41:14 +01:00
for ( const auto & i : stats . mapRecvBytesPerMsgCmd ) {
2017-06-29 03:51:10 +02:00
if ( i . second > 0 )
2020-06-18 11:17:23 +02:00
recvPerMsgCmd . pushKV ( i . first , i . second ) ;
2017-06-29 03:51:10 +02:00
}
2020-06-18 11:17:23 +02:00
obj . pushKV ( " bytesrecv_per_msg " , recvPerMsgCmd ) ;
2017-06-29 03:51:10 +02:00
2012-06-29 23:24:53 +02:00
ret . push_back ( obj ) ;
}
2012-09-18 21:07:58 +02:00
2012-06-29 23:24:53 +02:00
return ret ;
}
2012-06-29 05:18:38 +02:00
2018-05-04 22:42:39 +02:00
static UniValue addnode ( const JSONRPCRequest & request )
2013-01-23 17:45:00 +01:00
{
2017-03-09 08:14:27 +01:00
std : : string strCommand ;
2017-08-22 09:24:31 +02:00
if ( ! request . params [ 1 ] . isNull ( ) )
2016-10-19 15:01:33 +02:00
strCommand = request . params [ 1 ] . get_str ( ) ;
if ( request . fHelp | | request . params . size ( ) ! = 2 | |
2013-01-23 17:45:00 +01:00
( strCommand ! = " onetry " & & strCommand ! = " add " & & strCommand ! = " remove " ) )
2017-03-09 08:14:27 +01:00
throw std : : runtime_error (
2021-10-11 23:55:23 +02:00
RPCHelpMan { " addnode " ,
" \n Attempts to add or remove a node from the addnode list. \n "
" Or try a connection to a node once. \n "
" Nodes added using addnode (or -connect) are protected from DoS disconnection and are not required to be \n "
" full nodes as other outbound peers are (though such peers will not be synced from). \n " ,
{
2019-02-13 00:42:50 +01:00
{ " node " , RPCArg : : Type : : STR , RPCArg : : Optional : : NO , " The node (see getpeerinfo for nodes) " } ,
{ " command " , RPCArg : : Type : : STR , RPCArg : : Optional : : NO , " 'add' to add a node to the list, 'remove' to remove a node from the list, 'onetry' to try a connection to the node once " } ,
2019-01-29 15:55:37 +01:00
} ,
RPCResults { } ,
RPCExamples {
HelpExampleCli ( " addnode " , " \" 192.168.0.6:9999 \" \" onetry \" " )
2014-11-27 18:09:11 +01:00
+ HelpExampleRpc ( " addnode " , " \" 192.168.0.6:9999 \" , \" onetry \" " )
2019-01-29 15:55:37 +01:00
} ,
} . ToString ( ) ) ;
2013-01-23 17:45:00 +01:00
Backport Bitcoin PR#8085: p2p: Begin encapsulation (#1537)
* net: move CBanDB and CAddrDB out of net.h/cpp
This will eventually solve a circular dependency
* net: Create CConnman to encapsulate p2p connections
* net: Move socket binding into CConnman
* net: move OpenNetworkConnection into CConnman
* net: move ban and addrman functions into CConnman
* net: Add oneshot functions to CConnman
* net: move added node functions to CConnman
* net: Add most functions needed for vNodes to CConnman
* net: handle nodesignals in CConnman
* net: Pass CConnection to wallet rather than using the global
* net: Add rpc error for missing/disabled p2p functionality
* net: Pass CConnman around as needed
* gui: add NodeID to the peer table
* net: create generic functor accessors and move vNodes to CConnman
* net: move whitelist functions into CConnman
* net: move nLastNodeId to CConnman
* net: move nLocalHostNonce to CConnman
This behavior seems to have been quite racy and broken.
Move nLocalHostNonce into CNode, and check received nonces against all
non-fully-connected nodes. If there's a match, assume we've connected
to ourself.
* net: move messageHandlerCondition to CConnman
* net: move send/recv statistics to CConnman
* net: move SendBufferSize/ReceiveFloodSize to CConnman
* net: move nLocalServices/nRelevantServices to CConnman
These are in-turn passed to CNode at connection time. This allows us to offer
different services to different peers (or test the effects of doing so).
* net: move semOutbound and semMasternodeOutbound to CConnman
* net: SocketSendData returns written size
* net: move max/max-outbound to CConnman
* net: Pass best block known height into CConnman
CConnman then passes the current best height into CNode at creation time.
This way CConnman/CNode have no dependency on main for height, and the signals
only move in one direction.
This also helps to prevent identity leakage a tiny bit. Before this change, an
attacker could theoretically make 2 connections on different interfaces. They
would connect fully on one, and only establish the initial connection on the
other. Once they receive a new block, they would relay it to your first
connection, and immediately commence the version handshake on the second. Since
the new block height is reflected immediately, they could attempt to learn
whether the two connections were correlated.
This is, of course, incredibly unlikely to work due to the small timings
involved and receipt from other senders. But it doesn't hurt to lock-in
nBestHeight at the time of connection, rather than letting the remote choose
the time.
* net: pass CClientUIInterface into CConnman
* net: Drop StartNode/StopNode and use CConnman directly
* net: Introduce CConnection::Options to avoid passing so many params
* net: add nSendBufferMaxSize/nReceiveFloodSize to CConnection::Options
* net: move vNodesDisconnected into CConnman
* Made the ForEachNode* functions in src/net.cpp more pragmatic and self documenting
* Convert ForEachNode* functions to take a templated function argument rather than a std::function to eliminate std::function overhead
* net: move MAX_FEELER_CONNECTIONS into connman
2017-07-21 11:35:19 +02:00
if ( ! g_connman )
throw JSONRPCError ( RPC_CLIENT_P2P_DISABLED , " Error: Peer-to-peer functionality missing or disabled " ) ;
2017-03-09 08:14:27 +01:00
std : : string strNode = request . params [ 0 ] . get_str ( ) ;
2013-01-23 17:45:00 +01:00
if ( strCommand = = " onetry " )
{
CAddress addr ;
2017-10-14 00:25:16 +02:00
g_connman - > OpenNetworkConnection ( addr , false , nullptr , strNode . c_str ( ) , false , false , true ) ;
2014-08-20 21:15:16 +02:00
return NullUniValue ;
2013-01-23 17:45:00 +01:00
}
if ( strCommand = = " add " )
{
Backport Bitcoin PR#8085: p2p: Begin encapsulation (#1537)
* net: move CBanDB and CAddrDB out of net.h/cpp
This will eventually solve a circular dependency
* net: Create CConnman to encapsulate p2p connections
* net: Move socket binding into CConnman
* net: move OpenNetworkConnection into CConnman
* net: move ban and addrman functions into CConnman
* net: Add oneshot functions to CConnman
* net: move added node functions to CConnman
* net: Add most functions needed for vNodes to CConnman
* net: handle nodesignals in CConnman
* net: Pass CConnection to wallet rather than using the global
* net: Add rpc error for missing/disabled p2p functionality
* net: Pass CConnman around as needed
* gui: add NodeID to the peer table
* net: create generic functor accessors and move vNodes to CConnman
* net: move whitelist functions into CConnman
* net: move nLastNodeId to CConnman
* net: move nLocalHostNonce to CConnman
This behavior seems to have been quite racy and broken.
Move nLocalHostNonce into CNode, and check received nonces against all
non-fully-connected nodes. If there's a match, assume we've connected
to ourself.
* net: move messageHandlerCondition to CConnman
* net: move send/recv statistics to CConnman
* net: move SendBufferSize/ReceiveFloodSize to CConnman
* net: move nLocalServices/nRelevantServices to CConnman
These are in-turn passed to CNode at connection time. This allows us to offer
different services to different peers (or test the effects of doing so).
* net: move semOutbound and semMasternodeOutbound to CConnman
* net: SocketSendData returns written size
* net: move max/max-outbound to CConnman
* net: Pass best block known height into CConnman
CConnman then passes the current best height into CNode at creation time.
This way CConnman/CNode have no dependency on main for height, and the signals
only move in one direction.
This also helps to prevent identity leakage a tiny bit. Before this change, an
attacker could theoretically make 2 connections on different interfaces. They
would connect fully on one, and only establish the initial connection on the
other. Once they receive a new block, they would relay it to your first
connection, and immediately commence the version handshake on the second. Since
the new block height is reflected immediately, they could attempt to learn
whether the two connections were correlated.
This is, of course, incredibly unlikely to work due to the small timings
involved and receipt from other senders. But it doesn't hurt to lock-in
nBestHeight at the time of connection, rather than letting the remote choose
the time.
* net: pass CClientUIInterface into CConnman
* net: Drop StartNode/StopNode and use CConnman directly
* net: Introduce CConnection::Options to avoid passing so many params
* net: add nSendBufferMaxSize/nReceiveFloodSize to CConnection::Options
* net: move vNodesDisconnected into CConnman
* Made the ForEachNode* functions in src/net.cpp more pragmatic and self documenting
* Convert ForEachNode* functions to take a templated function argument rather than a std::function to eliminate std::function overhead
* net: move MAX_FEELER_CONNECTIONS into connman
2017-07-21 11:35:19 +02:00
if ( ! g_connman - > AddNode ( strNode ) )
2013-10-23 16:19:49 +02:00
throw JSONRPCError ( RPC_CLIENT_NODE_ALREADY_ADDED , " Error: Node already added " ) ;
2013-01-23 17:45:00 +01:00
}
else if ( strCommand = = " remove " )
{
Backport Bitcoin PR#8085: p2p: Begin encapsulation (#1537)
* net: move CBanDB and CAddrDB out of net.h/cpp
This will eventually solve a circular dependency
* net: Create CConnman to encapsulate p2p connections
* net: Move socket binding into CConnman
* net: move OpenNetworkConnection into CConnman
* net: move ban and addrman functions into CConnman
* net: Add oneshot functions to CConnman
* net: move added node functions to CConnman
* net: Add most functions needed for vNodes to CConnman
* net: handle nodesignals in CConnman
* net: Pass CConnection to wallet rather than using the global
* net: Add rpc error for missing/disabled p2p functionality
* net: Pass CConnman around as needed
* gui: add NodeID to the peer table
* net: create generic functor accessors and move vNodes to CConnman
* net: move whitelist functions into CConnman
* net: move nLastNodeId to CConnman
* net: move nLocalHostNonce to CConnman
This behavior seems to have been quite racy and broken.
Move nLocalHostNonce into CNode, and check received nonces against all
non-fully-connected nodes. If there's a match, assume we've connected
to ourself.
* net: move messageHandlerCondition to CConnman
* net: move send/recv statistics to CConnman
* net: move SendBufferSize/ReceiveFloodSize to CConnman
* net: move nLocalServices/nRelevantServices to CConnman
These are in-turn passed to CNode at connection time. This allows us to offer
different services to different peers (or test the effects of doing so).
* net: move semOutbound and semMasternodeOutbound to CConnman
* net: SocketSendData returns written size
* net: move max/max-outbound to CConnman
* net: Pass best block known height into CConnman
CConnman then passes the current best height into CNode at creation time.
This way CConnman/CNode have no dependency on main for height, and the signals
only move in one direction.
This also helps to prevent identity leakage a tiny bit. Before this change, an
attacker could theoretically make 2 connections on different interfaces. They
would connect fully on one, and only establish the initial connection on the
other. Once they receive a new block, they would relay it to your first
connection, and immediately commence the version handshake on the second. Since
the new block height is reflected immediately, they could attempt to learn
whether the two connections were correlated.
This is, of course, incredibly unlikely to work due to the small timings
involved and receipt from other senders. But it doesn't hurt to lock-in
nBestHeight at the time of connection, rather than letting the remote choose
the time.
* net: pass CClientUIInterface into CConnman
* net: Drop StartNode/StopNode and use CConnman directly
* net: Introduce CConnection::Options to avoid passing so many params
* net: add nSendBufferMaxSize/nReceiveFloodSize to CConnection::Options
* net: move vNodesDisconnected into CConnman
* Made the ForEachNode* functions in src/net.cpp more pragmatic and self documenting
* Convert ForEachNode* functions to take a templated function argument rather than a std::function to eliminate std::function overhead
* net: move MAX_FEELER_CONNECTIONS into connman
2017-07-21 11:35:19 +02:00
if ( ! g_connman - > RemoveAddedNode ( strNode ) )
2013-10-23 16:19:49 +02:00
throw JSONRPCError ( RPC_CLIENT_NODE_NOT_ADDED , " Error: Node has not been added. " ) ;
2013-01-23 17:45:00 +01:00
}
2014-08-20 21:15:16 +02:00
return NullUniValue ;
2013-01-23 17:45:00 +01:00
}
2018-05-04 22:42:39 +02:00
static UniValue disconnectnode ( const JSONRPCRequest & request )
2015-06-12 05:20:54 +02:00
{
2017-04-20 11:46:37 +02:00
if ( request . fHelp | | request . params . size ( ) = = 0 | | request . params . size ( ) > = 3 )
2017-03-09 08:14:27 +01:00
throw std : : runtime_error (
2021-10-11 23:55:23 +02:00
RPCHelpMan { " disconnectnode " ,
" \n Immediately disconnects from the specified peer node. \n "
" \n Strictly one out of 'address' and 'nodeid' can be provided to identify the node. \n "
" \n To disconnect by nodeid, either set 'address' to the empty string, or call using the named 'nodeid' argument only. \n " ,
{
2019-02-13 00:42:50 +01:00
{ " address " , RPCArg : : Type : : STR , /* default */ " fallback to nodeid " , " The IP address/port of the node " } ,
{ " nodeid " , RPCArg : : Type : : NUM , /* default */ " fallback to address " , " The node ID (see getpeerinfo for node IDs) " } ,
2019-01-29 15:55:37 +01:00
} ,
RPCResults { } ,
RPCExamples {
HelpExampleCli ( " disconnectnode " , " \" 192.168.0.6:9999 \" " )
2017-04-20 11:46:37 +02:00
+ HelpExampleCli ( " disconnectnode " , " \" \" 1 " )
2017-12-05 23:17:45 +01:00
+ HelpExampleRpc ( " disconnectnode " , " \" 192.168.0.6:9999 \" " )
2017-04-20 11:46:37 +02:00
+ HelpExampleRpc ( " disconnectnode " , " \" \" , 1 " )
2019-01-29 15:55:37 +01:00
} ,
} . ToString ( ) ) ;
2015-06-12 05:20:54 +02:00
Backport Bitcoin PR#8085: p2p: Begin encapsulation (#1537)
* net: move CBanDB and CAddrDB out of net.h/cpp
This will eventually solve a circular dependency
* net: Create CConnman to encapsulate p2p connections
* net: Move socket binding into CConnman
* net: move OpenNetworkConnection into CConnman
* net: move ban and addrman functions into CConnman
* net: Add oneshot functions to CConnman
* net: move added node functions to CConnman
* net: Add most functions needed for vNodes to CConnman
* net: handle nodesignals in CConnman
* net: Pass CConnection to wallet rather than using the global
* net: Add rpc error for missing/disabled p2p functionality
* net: Pass CConnman around as needed
* gui: add NodeID to the peer table
* net: create generic functor accessors and move vNodes to CConnman
* net: move whitelist functions into CConnman
* net: move nLastNodeId to CConnman
* net: move nLocalHostNonce to CConnman
This behavior seems to have been quite racy and broken.
Move nLocalHostNonce into CNode, and check received nonces against all
non-fully-connected nodes. If there's a match, assume we've connected
to ourself.
* net: move messageHandlerCondition to CConnman
* net: move send/recv statistics to CConnman
* net: move SendBufferSize/ReceiveFloodSize to CConnman
* net: move nLocalServices/nRelevantServices to CConnman
These are in-turn passed to CNode at connection time. This allows us to offer
different services to different peers (or test the effects of doing so).
* net: move semOutbound and semMasternodeOutbound to CConnman
* net: SocketSendData returns written size
* net: move max/max-outbound to CConnman
* net: Pass best block known height into CConnman
CConnman then passes the current best height into CNode at creation time.
This way CConnman/CNode have no dependency on main for height, and the signals
only move in one direction.
This also helps to prevent identity leakage a tiny bit. Before this change, an
attacker could theoretically make 2 connections on different interfaces. They
would connect fully on one, and only establish the initial connection on the
other. Once they receive a new block, they would relay it to your first
connection, and immediately commence the version handshake on the second. Since
the new block height is reflected immediately, they could attempt to learn
whether the two connections were correlated.
This is, of course, incredibly unlikely to work due to the small timings
involved and receipt from other senders. But it doesn't hurt to lock-in
nBestHeight at the time of connection, rather than letting the remote choose
the time.
* net: pass CClientUIInterface into CConnman
* net: Drop StartNode/StopNode and use CConnman directly
* net: Introduce CConnection::Options to avoid passing so many params
* net: add nSendBufferMaxSize/nReceiveFloodSize to CConnection::Options
* net: move vNodesDisconnected into CConnman
* Made the ForEachNode* functions in src/net.cpp more pragmatic and self documenting
* Convert ForEachNode* functions to take a templated function argument rather than a std::function to eliminate std::function overhead
* net: move MAX_FEELER_CONNECTIONS into connman
2017-07-21 11:35:19 +02:00
if ( ! g_connman )
throw JSONRPCError ( RPC_CLIENT_P2P_DISABLED , " Error: Peer-to-peer functionality missing or disabled " ) ;
2015-06-12 05:20:54 +02:00
2017-04-20 11:46:37 +02:00
bool success ;
const UniValue & address_arg = request . params [ 0 ] ;
2017-08-22 09:24:31 +02:00
const UniValue & id_arg = request . params [ 1 ] ;
2017-04-20 11:46:37 +02:00
if ( ! address_arg . isNull ( ) & & id_arg . isNull ( ) ) {
/* handle disconnect-by-address */
success = g_connman - > DisconnectNode ( address_arg . get_str ( ) ) ;
} else if ( ! id_arg . isNull ( ) & & ( address_arg . isNull ( ) | | ( address_arg . isStr ( ) & & address_arg . get_str ( ) . empty ( ) ) ) ) {
/* handle disconnect-by-id */
NodeId nodeid = ( NodeId ) id_arg . get_int64 ( ) ;
success = g_connman - > DisconnectNode ( nodeid ) ;
} else {
throw JSONRPCError ( RPC_INVALID_PARAMS , " Only one of address and nodeid should be provided. " ) ;
}
if ( ! success ) {
Backport Bitcoin PR#8085: p2p: Begin encapsulation (#1537)
* net: move CBanDB and CAddrDB out of net.h/cpp
This will eventually solve a circular dependency
* net: Create CConnman to encapsulate p2p connections
* net: Move socket binding into CConnman
* net: move OpenNetworkConnection into CConnman
* net: move ban and addrman functions into CConnman
* net: Add oneshot functions to CConnman
* net: move added node functions to CConnman
* net: Add most functions needed for vNodes to CConnman
* net: handle nodesignals in CConnman
* net: Pass CConnection to wallet rather than using the global
* net: Add rpc error for missing/disabled p2p functionality
* net: Pass CConnman around as needed
* gui: add NodeID to the peer table
* net: create generic functor accessors and move vNodes to CConnman
* net: move whitelist functions into CConnman
* net: move nLastNodeId to CConnman
* net: move nLocalHostNonce to CConnman
This behavior seems to have been quite racy and broken.
Move nLocalHostNonce into CNode, and check received nonces against all
non-fully-connected nodes. If there's a match, assume we've connected
to ourself.
* net: move messageHandlerCondition to CConnman
* net: move send/recv statistics to CConnman
* net: move SendBufferSize/ReceiveFloodSize to CConnman
* net: move nLocalServices/nRelevantServices to CConnman
These are in-turn passed to CNode at connection time. This allows us to offer
different services to different peers (or test the effects of doing so).
* net: move semOutbound and semMasternodeOutbound to CConnman
* net: SocketSendData returns written size
* net: move max/max-outbound to CConnman
* net: Pass best block known height into CConnman
CConnman then passes the current best height into CNode at creation time.
This way CConnman/CNode have no dependency on main for height, and the signals
only move in one direction.
This also helps to prevent identity leakage a tiny bit. Before this change, an
attacker could theoretically make 2 connections on different interfaces. They
would connect fully on one, and only establish the initial connection on the
other. Once they receive a new block, they would relay it to your first
connection, and immediately commence the version handshake on the second. Since
the new block height is reflected immediately, they could attempt to learn
whether the two connections were correlated.
This is, of course, incredibly unlikely to work due to the small timings
involved and receipt from other senders. But it doesn't hurt to lock-in
nBestHeight at the time of connection, rather than letting the remote choose
the time.
* net: pass CClientUIInterface into CConnman
* net: Drop StartNode/StopNode and use CConnman directly
* net: Introduce CConnection::Options to avoid passing so many params
* net: add nSendBufferMaxSize/nReceiveFloodSize to CConnection::Options
* net: move vNodesDisconnected into CConnman
* Made the ForEachNode* functions in src/net.cpp more pragmatic and self documenting
* Convert ForEachNode* functions to take a templated function argument rather than a std::function to eliminate std::function overhead
* net: move MAX_FEELER_CONNECTIONS into connman
2017-07-21 11:35:19 +02:00
throw JSONRPCError ( RPC_CLIENT_NODE_NOT_CONNECTED , " Node not found in connected nodes " ) ;
2017-04-20 11:46:37 +02:00
}
2015-06-12 05:20:54 +02:00
return NullUniValue ;
}
2018-05-04 22:42:39 +02:00
static UniValue getaddednodeinfo ( const JSONRPCRequest & request )
2013-01-23 17:48:17 +01:00
{
2016-10-19 15:01:33 +02:00
if ( request . fHelp | | request . params . size ( ) > 1 )
2017-03-09 08:14:27 +01:00
throw std : : runtime_error (
2021-10-11 23:55:23 +02:00
RPCHelpMan { " getaddednodeinfo " ,
" \n Returns information about the given added node, or all added nodes \n "
" (note that onetry addnodes are not listed here) \n " ,
{
2019-02-13 00:42:50 +01:00
{ " node " , RPCArg : : Type : : STR , /* default */ " all nodes " , " If provided, return information about this specific node, otherwise all nodes are returned. " } ,
2019-01-29 15:55:37 +01:00
} ,
RPCResult {
2013-10-29 12:29:44 +01:00
" [ \n "
" { \n "
2017-06-22 20:36:16 +02:00
" \" addednode \" : \" 192.168.0.201 \" , (string) The node IP address or name (as provided to addnode) \n "
2013-10-29 12:29:44 +01:00
" \" connected \" : true|false, (boolean) If connected \n "
2017-07-14 18:58:57 +02:00
" \" addresses \" : [ (list of objects) Only when connected = true \n "
2013-10-29 12:29:44 +01:00
" { \n "
2017-07-14 18:58:57 +02:00
" \" address \" : \" 192.168.0.201:9999 \" , (string) The dash server IP and port we're connected to \n "
2013-10-29 12:29:44 +01:00
" \" connected \" : \" outbound \" (string) connection, inbound or outbound \n "
" } \n "
" ] \n "
" } \n "
" ,... \n "
" ] \n "
2019-01-29 15:55:37 +01:00
} ,
RPCExamples {
HelpExampleCli ( " getaddednodeinfo " , " " )
+ HelpExampleCli ( " getaddednodeinfo " , " \" 192.168.0.201 \" " )
2018-02-22 08:41:08 +01:00
+ HelpExampleRpc ( " getaddednodeinfo " , " \" 192.168.0.201 \" " )
2019-01-29 15:55:37 +01:00
} ,
} . ToString ( ) ) ;
2013-01-23 17:48:17 +01:00
Backport Bitcoin PR#8085: p2p: Begin encapsulation (#1537)
* net: move CBanDB and CAddrDB out of net.h/cpp
This will eventually solve a circular dependency
* net: Create CConnman to encapsulate p2p connections
* net: Move socket binding into CConnman
* net: move OpenNetworkConnection into CConnman
* net: move ban and addrman functions into CConnman
* net: Add oneshot functions to CConnman
* net: move added node functions to CConnman
* net: Add most functions needed for vNodes to CConnman
* net: handle nodesignals in CConnman
* net: Pass CConnection to wallet rather than using the global
* net: Add rpc error for missing/disabled p2p functionality
* net: Pass CConnman around as needed
* gui: add NodeID to the peer table
* net: create generic functor accessors and move vNodes to CConnman
* net: move whitelist functions into CConnman
* net: move nLastNodeId to CConnman
* net: move nLocalHostNonce to CConnman
This behavior seems to have been quite racy and broken.
Move nLocalHostNonce into CNode, and check received nonces against all
non-fully-connected nodes. If there's a match, assume we've connected
to ourself.
* net: move messageHandlerCondition to CConnman
* net: move send/recv statistics to CConnman
* net: move SendBufferSize/ReceiveFloodSize to CConnman
* net: move nLocalServices/nRelevantServices to CConnman
These are in-turn passed to CNode at connection time. This allows us to offer
different services to different peers (or test the effects of doing so).
* net: move semOutbound and semMasternodeOutbound to CConnman
* net: SocketSendData returns written size
* net: move max/max-outbound to CConnman
* net: Pass best block known height into CConnman
CConnman then passes the current best height into CNode at creation time.
This way CConnman/CNode have no dependency on main for height, and the signals
only move in one direction.
This also helps to prevent identity leakage a tiny bit. Before this change, an
attacker could theoretically make 2 connections on different interfaces. They
would connect fully on one, and only establish the initial connection on the
other. Once they receive a new block, they would relay it to your first
connection, and immediately commence the version handshake on the second. Since
the new block height is reflected immediately, they could attempt to learn
whether the two connections were correlated.
This is, of course, incredibly unlikely to work due to the small timings
involved and receipt from other senders. But it doesn't hurt to lock-in
nBestHeight at the time of connection, rather than letting the remote choose
the time.
* net: pass CClientUIInterface into CConnman
* net: Drop StartNode/StopNode and use CConnman directly
* net: Introduce CConnection::Options to avoid passing so many params
* net: add nSendBufferMaxSize/nReceiveFloodSize to CConnection::Options
* net: move vNodesDisconnected into CConnman
* Made the ForEachNode* functions in src/net.cpp more pragmatic and self documenting
* Convert ForEachNode* functions to take a templated function argument rather than a std::function to eliminate std::function overhead
* net: move MAX_FEELER_CONNECTIONS into connman
2017-07-21 11:35:19 +02:00
if ( ! g_connman )
throw JSONRPCError ( RPC_CLIENT_P2P_DISABLED , " Error: Peer-to-peer functionality missing or disabled " ) ;
std : : vector < AddedNodeInfo > vInfo = g_connman - > GetAddedNodeInfo ( ) ;
2013-01-23 17:48:17 +01:00
2017-08-22 09:24:31 +02:00
if ( ! request . params [ 0 ] . isNull ( ) ) {
2017-07-14 18:58:57 +02:00
bool found = false ;
for ( const AddedNodeInfo & info : vInfo ) {
2016-10-19 15:01:33 +02:00
if ( info . strAddedNode = = request . params [ 0 ] . get_str ( ) ) {
2017-07-14 18:58:57 +02:00
vInfo . assign ( 1 , info ) ;
found = true ;
2013-01-23 17:48:17 +01:00
break ;
}
2015-05-31 15:36:44 +02:00
}
2017-07-14 18:58:57 +02:00
if ( ! found ) {
2013-10-23 16:19:49 +02:00
throw JSONRPCError ( RPC_CLIENT_NODE_NOT_ADDED , " Error: Node has not been added. " ) ;
2014-01-27 10:31:22 +01:00
}
2013-01-23 17:48:17 +01:00
}
2017-07-14 18:58:57 +02:00
UniValue ret ( UniValue : : VARR ) ;
2013-01-23 17:48:17 +01:00
2017-07-14 18:58:57 +02:00
for ( const AddedNodeInfo & info : vInfo ) {
2015-05-10 14:48:35 +02:00
UniValue obj ( UniValue : : VOBJ ) ;
2020-06-18 11:17:23 +02:00
obj . pushKV ( " addednode " , info . strAddedNode ) ;
obj . pushKV ( " connected " , info . fConnected ) ;
2015-05-10 14:48:35 +02:00
UniValue addresses ( UniValue : : VARR ) ;
2017-07-14 18:58:57 +02:00
if ( info . fConnected ) {
UniValue address ( UniValue : : VOBJ ) ;
2020-06-18 11:17:23 +02:00
address . pushKV ( " address " , info . resolvedAddress . ToString ( ) ) ;
address . pushKV ( " connected " , info . fInbound ? " inbound " : " outbound " ) ;
2017-07-14 18:58:57 +02:00
addresses . push_back ( address ) ;
2013-01-23 17:48:17 +01:00
}
2020-06-18 11:17:23 +02:00
obj . pushKV ( " addresses " , addresses ) ;
2013-01-23 17:48:17 +01:00
ret . push_back ( obj ) ;
}
return ret ;
}
2018-05-04 22:42:39 +02:00
static UniValue getnettotals ( const JSONRPCRequest & request )
2013-08-22 18:09:32 +02:00
{
2016-10-19 15:01:33 +02:00
if ( request . fHelp | | request . params . size ( ) > 0 )
2017-03-09 08:14:27 +01:00
throw std : : runtime_error (
2021-10-11 23:55:23 +02:00
RPCHelpMan { " getnettotals " ,
" \n Returns information about network traffic, including bytes in, bytes out, \n "
" and current time. \n " ,
2019-01-29 15:55:37 +01:00
{ } ,
RPCResult {
2013-10-29 12:29:44 +01:00
" { \n "
" \" totalbytesrecv \" : n, (numeric) Total bytes received \n "
2013-12-05 08:46:50 +01:00
" \" totalbytessent \" : n, (numeric) Total bytes sent \n "
2016-11-10 11:05:56 +01:00
" \" timemillis \" : t, (numeric) Current UNIX time in milliseconds \n "
2015-11-12 13:14:54 +01:00
" \" uploadtarget \" : \n "
" { \n "
" \" timeframe \" : n, (numeric) Length of the measuring timeframe in seconds \n "
" \" target \" : n, (numeric) Target in bytes \n "
" \" target_reached \" : true|false, (boolean) True if target is reached \n "
" \" serve_historical_blocks \" : true|false, (boolean) True if serving historical blocks \n "
" \" bytes_left_in_cycle \" : t, (numeric) Bytes left in current time cycle \n "
" \" time_left_in_cycle \" : t (numeric) Seconds left in current time cycle \n "
" } \n "
2013-10-29 12:29:44 +01:00
" } \n "
2019-01-29 15:55:37 +01:00
} ,
RPCExamples {
HelpExampleCli ( " getnettotals " , " " )
2013-10-29 12:29:44 +01:00
+ HelpExampleRpc ( " getnettotals " , " " )
2019-01-29 15:55:37 +01:00
} ,
} . ToString ( ) ) ;
Backport Bitcoin PR#8085: p2p: Begin encapsulation (#1537)
* net: move CBanDB and CAddrDB out of net.h/cpp
This will eventually solve a circular dependency
* net: Create CConnman to encapsulate p2p connections
* net: Move socket binding into CConnman
* net: move OpenNetworkConnection into CConnman
* net: move ban and addrman functions into CConnman
* net: Add oneshot functions to CConnman
* net: move added node functions to CConnman
* net: Add most functions needed for vNodes to CConnman
* net: handle nodesignals in CConnman
* net: Pass CConnection to wallet rather than using the global
* net: Add rpc error for missing/disabled p2p functionality
* net: Pass CConnman around as needed
* gui: add NodeID to the peer table
* net: create generic functor accessors and move vNodes to CConnman
* net: move whitelist functions into CConnman
* net: move nLastNodeId to CConnman
* net: move nLocalHostNonce to CConnman
This behavior seems to have been quite racy and broken.
Move nLocalHostNonce into CNode, and check received nonces against all
non-fully-connected nodes. If there's a match, assume we've connected
to ourself.
* net: move messageHandlerCondition to CConnman
* net: move send/recv statistics to CConnman
* net: move SendBufferSize/ReceiveFloodSize to CConnman
* net: move nLocalServices/nRelevantServices to CConnman
These are in-turn passed to CNode at connection time. This allows us to offer
different services to different peers (or test the effects of doing so).
* net: move semOutbound and semMasternodeOutbound to CConnman
* net: SocketSendData returns written size
* net: move max/max-outbound to CConnman
* net: Pass best block known height into CConnman
CConnman then passes the current best height into CNode at creation time.
This way CConnman/CNode have no dependency on main for height, and the signals
only move in one direction.
This also helps to prevent identity leakage a tiny bit. Before this change, an
attacker could theoretically make 2 connections on different interfaces. They
would connect fully on one, and only establish the initial connection on the
other. Once they receive a new block, they would relay it to your first
connection, and immediately commence the version handshake on the second. Since
the new block height is reflected immediately, they could attempt to learn
whether the two connections were correlated.
This is, of course, incredibly unlikely to work due to the small timings
involved and receipt from other senders. But it doesn't hurt to lock-in
nBestHeight at the time of connection, rather than letting the remote choose
the time.
* net: pass CClientUIInterface into CConnman
* net: Drop StartNode/StopNode and use CConnman directly
* net: Introduce CConnection::Options to avoid passing so many params
* net: add nSendBufferMaxSize/nReceiveFloodSize to CConnection::Options
* net: move vNodesDisconnected into CConnman
* Made the ForEachNode* functions in src/net.cpp more pragmatic and self documenting
* Convert ForEachNode* functions to take a templated function argument rather than a std::function to eliminate std::function overhead
* net: move MAX_FEELER_CONNECTIONS into connman
2017-07-21 11:35:19 +02:00
if ( ! g_connman )
throw JSONRPCError ( RPC_CLIENT_P2P_DISABLED , " Error: Peer-to-peer functionality missing or disabled " ) ;
2013-08-22 18:09:32 +02:00
2015-05-10 14:48:35 +02:00
UniValue obj ( UniValue : : VOBJ ) ;
2020-06-18 11:17:23 +02:00
obj . pushKV ( " totalbytesrecv " , g_connman - > GetTotalBytesRecv ( ) ) ;
obj . pushKV ( " totalbytessent " , g_connman - > GetTotalBytesSent ( ) ) ;
obj . pushKV ( " timemillis " , GetTimeMillis ( ) ) ;
2015-09-02 17:03:27 +02:00
UniValue outboundLimit ( UniValue : : VOBJ ) ;
2020-06-18 11:17:23 +02:00
outboundLimit . pushKV ( " timeframe " , g_connman - > GetMaxOutboundTimeframe ( ) ) ;
outboundLimit . pushKV ( " target " , g_connman - > GetMaxOutboundTarget ( ) ) ;
outboundLimit . pushKV ( " target_reached " , g_connman - > OutboundTargetReached ( false ) ) ;
outboundLimit . pushKV ( " serve_historical_blocks " , ! g_connman - > OutboundTargetReached ( true ) ) ;
outboundLimit . pushKV ( " bytes_left_in_cycle " , g_connman - > GetOutboundTargetBytesLeft ( ) ) ;
outboundLimit . pushKV ( " time_left_in_cycle " , g_connman - > GetMaxOutboundTimeLeftInCycle ( ) ) ;
obj . pushKV ( " uploadtarget " , outboundLimit ) ;
2013-08-22 18:09:32 +02:00
return obj ;
}
2014-05-05 13:22:28 +02:00
2015-05-13 21:29:19 +02:00
static UniValue GetNetworksInfo ( )
2014-07-30 15:35:14 +02:00
{
2015-05-10 14:48:35 +02:00
UniValue networks ( UniValue : : VARR ) ;
2014-07-30 15:35:14 +02:00
for ( int n = 0 ; n < NET_MAX ; + + n )
{
enum Network network = static_cast < enum Network > ( n ) ;
2017-06-24 12:16:41 +02:00
if ( network = = NET_UNROUTABLE | | network = = NET_INTERNAL )
2014-07-30 15:35:14 +02:00
continue ;
proxyType proxy ;
2015-05-10 14:48:35 +02:00
UniValue obj ( UniValue : : VOBJ ) ;
2014-07-30 15:35:14 +02:00
GetProxy ( network , proxy ) ;
2020-06-18 11:17:23 +02:00
obj . pushKV ( " name " , GetNetworkName ( network ) ) ;
2019-01-14 14:22:47 +01:00
obj . pushKV ( " limited " , ! IsReachable ( network ) ) ;
2020-06-18 11:17:23 +02:00
obj . pushKV ( " reachable " , IsReachable ( network ) ) ;
obj . pushKV ( " proxy " , proxy . IsValid ( ) ? proxy . proxy . ToStringIPPort ( ) : std : : string ( ) ) ;
obj . pushKV ( " proxy_randomize_credentials " , proxy . randomize_credentials ) ;
2014-07-30 15:35:14 +02:00
networks . push_back ( obj ) ;
}
return networks ;
}
2018-05-04 22:42:39 +02:00
static UniValue getnetworkinfo ( const JSONRPCRequest & request )
2014-05-05 13:22:28 +02:00
{
2016-10-19 15:01:33 +02:00
if ( request . fHelp | | request . params . size ( ) ! = 0 )
2017-03-09 08:14:27 +01:00
throw std : : runtime_error (
2021-10-11 23:55:23 +02:00
RPCHelpMan { " getnetworkinfo " ,
2019-01-29 15:55:37 +01:00
" Returns an object containing various state info regarding P2P networking. \n " ,
{ } ,
RPCResult {
2014-05-05 13:22:28 +02:00
" { \n "
2014-09-25 11:56:05 +02:00
" \" version \" : xxxxx, (numeric) the server version \n "
2021-01-11 20:34:42 +01:00
" \" buildversion \" : \" x.x.x.x-xxx \" , (string) the server build version including RC info or commit as relevant \n "
" \" subversion \" : \" /Dash Core:x.x.x.x/ \" , (string) the server subversion string \n "
2014-09-25 11:56:05 +02:00
" \" protocolversion \" : xxxxx, (numeric) the protocol version \n "
" \" localservices \" : \" xxxxxxxxxxxxxxxx \" , (string) the services we offer to the network \n "
2019-03-01 17:06:27 +01:00
" \" localservicesnames \" : [ (array) the services we offer to the network, in human-readable form \n "
" \" SERVICE_NAME \" , (string) the service name \n "
" ... \n "
" ], \n "
2017-07-21 20:31:47 +02:00
" \" localrelay \" : true|false, (bool) true if transaction relay is requested from peers \n "
2014-09-25 11:56:05 +02:00
" \" timeoffset \" : xxxxx, (numeric) the time offset \n "
" \" connections \" : xxxxx, (numeric) the number of connections \n "
2017-09-11 15:38:14 +02:00
" \" networkactive \" : true|false, (bool) whether p2p networking is enabled \n "
2020-12-30 20:34:42 +01:00
" \" socketevents \" : \" xxx/ \" , (string) the socket events mode, either kqueue, epoll, poll or select \n "
2014-09-25 11:56:05 +02:00
" \" networks \" : [ (array) information per network \n "
" { \n "
" \" name \" : \" xxx \" , (string) network (ipv4, ipv6 or onion) \n "
" \" limited \" : true|false, (boolean) is the network limited using -onlynet? \n "
" \" reachable \" : true|false, (boolean) is the network reachable? \n "
" \" proxy \" : \" host:port \" (string) the proxy that is used for this network, or empty if none \n "
2017-01-12 02:09:29 +01:00
" \" proxy_randomize_credentials \" : true|false, (string) Whether randomized credentials are used \n "
2014-09-25 11:56:05 +02:00
" } \n "
" ,... \n "
2014-07-30 15:35:14 +02:00
" ], \n "
2019-03-14 15:44:42 +01:00
" \" relayfee \" : x.xxxxxxxx, (numeric) minimum relay fee for transactions in " + CURRENCY_UNIT + " /kB \n "
2020-06-11 10:39:04 +02:00
" \" incrementalfee \" : x.xxxxxxxx, (numeric) minimum fee increment for mempool limiting in " + CURRENCY_UNIT + " /kB \n "
2014-09-25 11:56:05 +02:00
" \" localaddresses \" : [ (array) list of local addresses \n "
" { \n "
" \" address \" : \" xxxx \" , (string) network address \n "
" \" port \" : xxx, (numeric) network port \n "
" \" score \" : xxx (numeric) relative score \n "
" } \n "
" ,... \n "
2014-05-05 13:22:28 +02:00
" ] \n "
2017-09-28 13:46:00 +02:00
" \" warnings \" : \" ... \" (string) any network and blockchain warnings \n "
2014-05-05 13:22:28 +02:00
" } \n "
2019-01-29 15:55:37 +01:00
} ,
RPCExamples {
HelpExampleCli ( " getnetworkinfo " , " " )
2014-05-05 13:22:28 +02:00
+ HelpExampleRpc ( " getnetworkinfo " , " " )
2019-01-29 15:55:37 +01:00
} ,
} . ToString ( ) ) ;
2014-05-05 13:22:28 +02:00
2014-10-19 10:46:17 +02:00
LOCK ( cs_main ) ;
2015-05-10 14:48:35 +02:00
UniValue obj ( UniValue : : VOBJ ) ;
2020-06-18 11:17:23 +02:00
obj . pushKV ( " version " , CLIENT_VERSION ) ;
2021-01-11 20:34:42 +01:00
obj . pushKV ( " buildversion " , FormatFullVersion ( ) ) ;
2020-06-18 11:17:23 +02:00
obj . pushKV ( " subversion " , strSubVersion ) ;
obj . pushKV ( " protocolversion " , PROTOCOL_VERSION ) ;
2019-03-01 17:06:27 +01:00
if ( g_connman ) {
ServiceFlags services = g_connman - > GetLocalServices ( ) ;
obj . pushKV ( " localservices " , strprintf ( " %016x " , services ) ) ;
obj . pushKV ( " localservicesnames " , GetServicesNames ( services ) ) ;
}
2019-05-16 18:44:54 +02:00
obj . pushKV ( " localrelay " , g_relay_txes ) ;
2020-06-18 11:17:23 +02:00
obj . pushKV ( " timeoffset " , GetTimeOffset ( ) ) ;
2017-09-11 15:38:14 +02:00
if ( g_connman ) {
2020-06-18 11:17:23 +02:00
obj . pushKV ( " networkactive " , g_connman - > GetNetworkActive ( ) ) ;
obj . pushKV ( " connections " , ( int ) g_connman - > GetNodeCount ( CConnman : : CONNECTIONS_ALL ) ) ;
2020-04-16 10:11:26 +02:00
std : : string strSocketEvents ;
switch ( g_connman - > GetSocketEventsMode ( ) ) {
case CConnman : : SOCKETEVENTS_SELECT :
strSocketEvents = " select " ;
break ;
case CConnman : : SOCKETEVENTS_POLL :
strSocketEvents = " poll " ;
break ;
2020-04-07 17:58:38 +02:00
case CConnman : : SOCKETEVENTS_EPOLL :
strSocketEvents = " epoll " ;
break ;
2020-12-30 20:34:42 +01:00
case CConnman : : SOCKETEVENTS_KQUEUE :
strSocketEvents = " kqueue " ;
break ;
2020-04-16 10:11:26 +02:00
default :
assert ( false ) ;
}
2020-06-18 11:17:23 +02:00
obj . pushKV ( " socketevents " , strSocketEvents ) ;
2017-09-11 15:38:14 +02:00
}
2020-06-18 11:17:23 +02:00
obj . pushKV ( " networks " , GetNetworksInfo ( ) ) ;
obj . pushKV ( " relayfee " , ValueFromAmount ( : : minRelayTxFee . GetFeePerK ( ) ) ) ;
obj . pushKV ( " incrementalfee " , ValueFromAmount ( : : incrementalRelayFee . GetFeePerK ( ) ) ) ;
2015-05-10 14:48:35 +02:00
UniValue localAddresses ( UniValue : : VARR ) ;
2014-05-05 13:22:28 +02:00
{
LOCK ( cs_mapLocalHost ) ;
2020-07-07 18:46:21 +02:00
for ( const std : : pair < const CNetAddr , LocalServiceInfo > & item : mapLocalHost )
2014-05-05 13:22:28 +02:00
{
2015-05-10 14:48:35 +02:00
UniValue rec ( UniValue : : VOBJ ) ;
2020-06-18 11:17:23 +02:00
rec . pushKV ( " address " , item . first . ToString ( ) ) ;
rec . pushKV ( " port " , item . second . nPort ) ;
rec . pushKV ( " score " , item . second . nScore ) ;
2014-05-05 13:22:28 +02:00
localAddresses . push_back ( rec ) ;
}
}
2020-06-18 11:17:23 +02:00
obj . pushKV ( " localaddresses " , localAddresses ) ;
obj . pushKV ( " warnings " , GetWarnings ( " statusbar " ) ) ;
2014-05-05 13:22:28 +02:00
return obj ;
}
2015-05-19 10:07:46 +02:00
2018-05-04 22:42:39 +02:00
static UniValue setban ( const JSONRPCRequest & request )
2015-05-19 10:07:46 +02:00
{
2021-12-12 14:38:12 +01:00
const RPCHelpMan help { " setban " ,
2021-10-11 23:55:23 +02:00
" \n Attempts to add or remove an IP/Subnet from the banned list. \n " ,
{
2019-02-13 00:42:50 +01:00
{ " subnet " , RPCArg : : Type : : STR , RPCArg : : Optional : : NO , " The IP/Subnet (see getpeerinfo for nodes IP) with an optional netmask (default is /32 = single IP) " } ,
{ " command " , RPCArg : : Type : : STR , RPCArg : : Optional : : NO , " 'add' to add an IP/Subnet to the list, 'remove' to remove an IP/Subnet from the list " } ,
{ " bantime " , RPCArg : : Type : : NUM , /* default */ " 0 " , " time in seconds how long (or until when if [absolute] is set) the IP is banned (0 or empty means using the default time of 24h which can also be overwritten by the -bantime startup argument) " } ,
{ " absolute " , RPCArg : : Type : : BOOL , /* default */ " false " , " If set, the bantime must be an absolute timestamp in seconds since epoch (Jan 1 1970 GMT) " } ,
2019-01-29 15:55:37 +01:00
} ,
RPCResults { } ,
RPCExamples {
HelpExampleCli ( " setban " , " \" 192.168.0.6 \" \" add \" 86400 " )
2015-05-25 20:03:51 +02:00
+ HelpExampleCli ( " setban " , " \" 192.168.0.0/24 \" \" add \" " )
2016-08-17 12:01:38 +02:00
+ HelpExampleRpc ( " setban " , " \" 192.168.0.6 \" , \" add \" , 86400 " )
2019-01-29 15:55:37 +01:00
} ,
2021-12-12 14:38:12 +01:00
} ;
std : : string strCommand ;
if ( ! request . params [ 1 ] . isNull ( ) )
strCommand = request . params [ 1 ] . get_str ( ) ;
if ( request . fHelp | | ! help . IsValidNumArgs ( request . params . size ( ) ) | | ( strCommand ! = " add " & & strCommand ! = " remove " ) ) {
throw std : : runtime_error ( help . ToString ( ) ) ;
}
2019-01-21 18:45:59 +01:00
if ( ! g_banman ) {
throw JSONRPCError ( RPC_DATABASE_ERROR , " Error: Ban database not loaded " ) ;
}
2015-05-19 10:07:46 +02:00
2015-05-25 20:03:51 +02:00
CSubNet subNet ;
CNetAddr netAddr ;
bool isSubnet = false ;
2018-01-29 13:51:03 +01:00
if ( request . params [ 0 ] . get_str ( ) . find ( ' / ' ) ! = std : : string : : npos )
2015-05-25 20:03:51 +02:00
isSubnet = true ;
2017-09-03 15:29:10 +02:00
if ( ! isSubnet ) {
CNetAddr resolved ;
2016-10-19 15:01:33 +02:00
LookupHost ( request . params [ 0 ] . get_str ( ) . c_str ( ) , resolved , false ) ;
2017-09-03 15:29:10 +02:00
netAddr = resolved ;
}
2015-05-25 20:03:51 +02:00
else
2016-10-19 15:01:33 +02:00
LookupSubNet ( request . params [ 0 ] . get_str ( ) . c_str ( ) , subNet ) ;
2015-05-25 20:03:51 +02:00
if ( ! ( isSubnet ? subNet . IsValid ( ) : netAddr . IsValid ( ) ) )
2017-03-09 10:02:13 +01:00
throw JSONRPCError ( RPC_CLIENT_INVALID_IP_OR_SUBNET , " Error: Invalid IP/Subnet " ) ;
2015-05-19 10:07:46 +02:00
if ( strCommand = = " add " )
{
2019-01-21 18:45:59 +01:00
if ( isSubnet ? g_banman - > IsBanned ( subNet ) : g_banman - > IsBanned ( netAddr ) ) {
2015-05-25 20:03:51 +02:00
throw JSONRPCError ( RPC_CLIENT_NODE_ALREADY_ADDED , " Error: IP/Subnet already banned " ) ;
2019-01-21 18:45:59 +01:00
}
2015-05-19 10:07:46 +02:00
int64_t banTime = 0 ; //use standard bantime if not specified
2017-08-22 09:24:31 +02:00
if ( ! request . params [ 2 ] . isNull ( ) )
2016-10-19 15:01:33 +02:00
banTime = request . params [ 2 ] . get_int64 ( ) ;
2015-05-19 10:07:46 +02:00
2015-06-12 18:31:47 +02:00
bool absolute = false ;
2017-08-22 09:24:31 +02:00
if ( request . params [ 3 ] . isTrue ( ) )
2015-06-12 18:31:47 +02:00
absolute = true ;
2019-01-21 18:45:59 +01:00
if ( isSubnet ) {
g_banman - > Ban ( subNet , BanReasonManuallyAdded , banTime , absolute ) ;
if ( g_connman ) {
g_connman - > DisconnectNode ( subNet ) ;
}
} else {
g_banman - > Ban ( netAddr , BanReasonManuallyAdded , banTime , absolute ) ;
if ( g_connman ) {
g_connman - > DisconnectNode ( netAddr ) ;
}
}
2015-05-19 10:07:46 +02:00
}
else if ( strCommand = = " remove " )
{
2019-01-21 18:45:59 +01:00
if ( ! ( isSubnet ? g_banman - > Unban ( subNet ) : g_banman - > Unban ( netAddr ) ) ) {
2017-03-09 10:02:13 +01:00
throw JSONRPCError ( RPC_CLIENT_INVALID_IP_OR_SUBNET , " Error: Unban failed. Requested address/subnet was not previously banned. " ) ;
2019-01-21 18:45:59 +01:00
}
2015-05-19 10:07:46 +02:00
}
2015-06-12 18:31:47 +02:00
return NullUniValue ;
2015-05-19 10:07:46 +02:00
}
2018-05-04 22:42:39 +02:00
static UniValue listbanned ( const JSONRPCRequest & request )
2015-05-19 10:07:46 +02:00
{
2016-10-19 15:01:33 +02:00
if ( request . fHelp | | request . params . size ( ) ! = 0 )
2017-03-09 08:14:27 +01:00
throw std : : runtime_error (
2021-10-11 23:55:23 +02:00
RPCHelpMan { " listbanned " ,
2019-01-29 15:55:37 +01:00
" \n List all banned IPs/Subnets. \n " ,
{ } ,
RPCResults { } ,
RPCExamples {
HelpExampleCli ( " listbanned " , " " )
2015-05-19 10:07:46 +02:00
+ HelpExampleRpc ( " listbanned " , " " )
2019-01-29 15:55:37 +01:00
} ,
} . ToString ( ) ) ;
2015-05-19 10:07:46 +02:00
2019-01-21 18:45:59 +01:00
if ( ! g_banman ) {
throw JSONRPCError ( RPC_DATABASE_ERROR , " Error: Ban database not loaded " ) ;
}
Backport Bitcoin PR#8085: p2p: Begin encapsulation (#1537)
* net: move CBanDB and CAddrDB out of net.h/cpp
This will eventually solve a circular dependency
* net: Create CConnman to encapsulate p2p connections
* net: Move socket binding into CConnman
* net: move OpenNetworkConnection into CConnman
* net: move ban and addrman functions into CConnman
* net: Add oneshot functions to CConnman
* net: move added node functions to CConnman
* net: Add most functions needed for vNodes to CConnman
* net: handle nodesignals in CConnman
* net: Pass CConnection to wallet rather than using the global
* net: Add rpc error for missing/disabled p2p functionality
* net: Pass CConnman around as needed
* gui: add NodeID to the peer table
* net: create generic functor accessors and move vNodes to CConnman
* net: move whitelist functions into CConnman
* net: move nLastNodeId to CConnman
* net: move nLocalHostNonce to CConnman
This behavior seems to have been quite racy and broken.
Move nLocalHostNonce into CNode, and check received nonces against all
non-fully-connected nodes. If there's a match, assume we've connected
to ourself.
* net: move messageHandlerCondition to CConnman
* net: move send/recv statistics to CConnman
* net: move SendBufferSize/ReceiveFloodSize to CConnman
* net: move nLocalServices/nRelevantServices to CConnman
These are in-turn passed to CNode at connection time. This allows us to offer
different services to different peers (or test the effects of doing so).
* net: move semOutbound and semMasternodeOutbound to CConnman
* net: SocketSendData returns written size
* net: move max/max-outbound to CConnman
* net: Pass best block known height into CConnman
CConnman then passes the current best height into CNode at creation time.
This way CConnman/CNode have no dependency on main for height, and the signals
only move in one direction.
This also helps to prevent identity leakage a tiny bit. Before this change, an
attacker could theoretically make 2 connections on different interfaces. They
would connect fully on one, and only establish the initial connection on the
other. Once they receive a new block, they would relay it to your first
connection, and immediately commence the version handshake on the second. Since
the new block height is reflected immediately, they could attempt to learn
whether the two connections were correlated.
This is, of course, incredibly unlikely to work due to the small timings
involved and receipt from other senders. But it doesn't hurt to lock-in
nBestHeight at the time of connection, rather than letting the remote choose
the time.
* net: pass CClientUIInterface into CConnman
* net: Drop StartNode/StopNode and use CConnman directly
* net: Introduce CConnection::Options to avoid passing so many params
* net: add nSendBufferMaxSize/nReceiveFloodSize to CConnection::Options
* net: move vNodesDisconnected into CConnman
* Made the ForEachNode* functions in src/net.cpp more pragmatic and self documenting
* Convert ForEachNode* functions to take a templated function argument rather than a std::function to eliminate std::function overhead
* net: move MAX_FEELER_CONNECTIONS into connman
2017-07-21 11:35:19 +02:00
2015-06-26 21:38:33 +02:00
banmap_t banMap ;
2019-01-21 18:45:59 +01:00
g_banman - > GetBanned ( banMap ) ;
2015-05-19 10:07:46 +02:00
2015-06-12 18:31:47 +02:00
UniValue bannedAddresses ( UniValue : : VARR ) ;
2017-11-30 23:09:44 +01:00
for ( const auto & entry : banMap )
2015-05-19 10:07:46 +02:00
{
2017-11-30 23:09:44 +01:00
const CBanEntry & banEntry = entry . second ;
2015-06-12 18:31:47 +02:00
UniValue rec ( UniValue : : VOBJ ) ;
2020-06-18 11:17:23 +02:00
rec . pushKV ( " address " , entry . first . ToString ( ) ) ;
rec . pushKV ( " banned_until " , banEntry . nBanUntil ) ;
rec . pushKV ( " ban_created " , banEntry . nCreateTime ) ;
rec . pushKV ( " ban_reason " , banEntry . banReasonToString ( ) ) ;
2015-06-26 21:38:33 +02:00
2015-05-19 10:07:46 +02:00
bannedAddresses . push_back ( rec ) ;
}
return bannedAddresses ;
}
2018-05-04 22:42:39 +02:00
static UniValue clearbanned ( const JSONRPCRequest & request )
2015-05-19 10:07:46 +02:00
{
2016-10-19 15:01:33 +02:00
if ( request . fHelp | | request . params . size ( ) ! = 0 )
2017-03-09 08:14:27 +01:00
throw std : : runtime_error (
2021-10-11 23:55:23 +02:00
RPCHelpMan { " clearbanned " ,
2019-01-29 15:55:37 +01:00
" \n Clear all banned IPs. \n " ,
{ } ,
RPCResults { } ,
RPCExamples {
HelpExampleCli ( " clearbanned " , " " )
2015-05-19 10:07:46 +02:00
+ HelpExampleRpc ( " clearbanned " , " " )
2019-01-29 15:55:37 +01:00
} ,
} . ToString ( ) ) ;
2019-01-21 18:45:59 +01:00
if ( ! g_banman ) {
throw JSONRPCError ( RPC_DATABASE_ERROR , " Error: Ban database not loaded " ) ;
}
2015-05-19 10:07:46 +02:00
2019-01-21 18:45:59 +01:00
g_banman - > ClearBanned ( ) ;
2015-05-19 10:07:46 +02:00
2015-06-12 18:31:47 +02:00
return NullUniValue ;
2015-05-19 10:07:46 +02:00
}
2017-09-11 15:38:14 +02:00
2018-05-04 22:42:39 +02:00
static UniValue setnetworkactive ( const JSONRPCRequest & request )
2017-09-11 15:38:14 +02:00
{
2016-10-19 15:01:33 +02:00
if ( request . fHelp | | request . params . size ( ) ! = 1 ) {
2017-03-09 08:14:27 +01:00
throw std : : runtime_error (
2021-10-11 23:55:23 +02:00
RPCHelpMan { " setnetworkactive " ,
" \n Disable/enable all p2p network activity. \n " ,
{
2019-02-13 00:42:50 +01:00
{ " state " , RPCArg : : Type : : BOOL , RPCArg : : Optional : : NO , " true to enable networking, false to disable " } ,
2019-01-29 15:55:37 +01:00
} ,
RPCResults { } ,
RPCExamples { " " } ,
} . ToString ( )
2017-09-11 15:38:14 +02:00
) ;
}
if ( ! g_connman ) {
throw JSONRPCError ( RPC_CLIENT_P2P_DISABLED , " Error: Peer-to-peer functionality missing or disabled " ) ;
}
2016-10-19 15:01:33 +02:00
g_connman - > SetNetworkActive ( request . params [ 0 ] . get_bool ( ) ) ;
2017-09-11 15:38:14 +02:00
return g_connman - > GetNetworkActive ( ) ;
}
2016-03-31 10:55:06 +02:00
2021-09-16 00:28:55 +02:00
static UniValue getnodeaddresses ( const JSONRPCRequest & request )
{
if ( request . fHelp | | request . params . size ( ) > 1 ) {
throw std : : runtime_error (
2021-10-11 23:55:23 +02:00
RPCHelpMan { " getnodeaddresses " ,
" \n Return known addresses which can potentially be used to find new nodes in the network \n " ,
{
2019-02-13 00:42:50 +01:00
{ " count " , RPCArg : : Type : : NUM , /* default */ " 1 " , " How many addresses to return. Limited to the smaller of " + std : : to_string ( ADDRMAN_GETADDR_MAX ) + " or " + std : : to_string ( ADDRMAN_GETADDR_MAX_PCT ) + " % of all known addresses. " } ,
2019-01-29 15:55:37 +01:00
} ,
RPCResult {
2021-09-16 00:28:55 +02:00
" [ \n "
" { \n "
" \" time \" : ttt, (numeric) Timestamp in seconds since epoch (Jan 1 1970 GMT) keeping track of when the node was last seen \n "
" \" services \" : n, (numeric) The services offered \n "
" \" address \" : \" host \" , (string) The address of the node \n "
" \" port \" : n (numeric) The port of the node \n "
" } \n "
" ,.... \n "
" ] \n "
2019-01-29 15:55:37 +01:00
} ,
RPCExamples {
HelpExampleCli ( " getnodeaddresses " , " 8 " )
2021-09-16 00:28:55 +02:00
+ HelpExampleRpc ( " getnodeaddresses " , " 8 " )
2019-01-29 15:55:37 +01:00
} ,
} . ToString ( ) ) ;
2021-09-16 00:28:55 +02:00
}
if ( ! g_connman ) {
throw JSONRPCError ( RPC_CLIENT_P2P_DISABLED , " Error: Peer-to-peer functionality missing or disabled " ) ;
}
int count = 1 ;
if ( ! request . params [ 0 ] . isNull ( ) ) {
count = request . params [ 0 ] . get_int ( ) ;
if ( count < = 0 ) {
throw JSONRPCError ( RPC_INVALID_PARAMETER , " Address count out of range " ) ;
}
}
// returns a shuffled list of CAddress
std : : vector < CAddress > vAddr = g_connman - > GetAddresses ( ) ;
UniValue ret ( UniValue : : VARR ) ;
int address_return_count = std : : min < int > ( count , vAddr . size ( ) ) ;
for ( int i = 0 ; i < address_return_count ; + + i ) {
UniValue obj ( UniValue : : VOBJ ) ;
const CAddress & addr = vAddr [ i ] ;
obj . pushKV ( " time " , ( int ) addr . nTime ) ;
obj . pushKV ( " services " , ( uint64_t ) addr . nServices ) ;
obj . pushKV ( " address " , addr . ToStringIP ( ) ) ;
obj . pushKV ( " port " , addr . GetPort ( ) ) ;
ret . push_back ( obj ) ;
}
return ret ;
}
2018-09-10 18:13:11 +02:00
// clang-format off
2016-03-31 10:55:06 +02:00
static const CRPCCommand commands [ ] =
2017-09-05 18:43:07 +02:00
{ // category name actor (function) argNames
2016-03-31 10:55:06 +02:00
// --------------------- ------------------------ ----------------------- ----------
2017-09-05 18:43:07 +02:00
{ " network " , " getconnectioncount " , & getconnectioncount , { } } ,
{ " network " , " ping " , & ping , { } } ,
{ " network " , " getpeerinfo " , & getpeerinfo , { } } ,
{ " network " , " addnode " , & addnode , { " node " , " command " } } ,
{ " network " , " disconnectnode " , & disconnectnode , { " address " , " nodeid " } } ,
{ " network " , " getaddednodeinfo " , & getaddednodeinfo , { " node " } } ,
{ " network " , " getnettotals " , & getnettotals , { } } ,
{ " network " , " getnetworkinfo " , & getnetworkinfo , { } } ,
{ " network " , " setban " , & setban , { " subnet " , " command " , " bantime " , " absolute " } } ,
{ " network " , " listbanned " , & listbanned , { } } ,
{ " network " , " clearbanned " , & clearbanned , { } } ,
{ " network " , " setnetworkactive " , & setnetworkactive , { " state " } } ,
2021-09-16 00:28:55 +02:00
{ " network " , " getnodeaddresses " , & getnodeaddresses , { " count " } } ,
2016-03-31 10:55:06 +02:00
} ;
2018-09-10 18:13:11 +02:00
// clang-format on
2016-03-31 10:55:06 +02:00
2016-08-31 16:04:22 +02:00
void RegisterNetRPCCommands ( CRPCTable & t )
2016-03-31 10:55:06 +02:00
{
for ( unsigned int vcidx = 0 ; vcidx < ARRAYLEN ( commands ) ; vcidx + + )
2016-08-31 16:04:22 +02:00
t . appendCommand ( commands [ vcidx ] . name , & commands [ vcidx ] ) ;
2016-03-31 10:55:06 +02:00
}