2014-02-08 22:50:24 +01:00
// Copyright (c) 2009-2014 The Bitcoin developers
2012-06-29 05:18:38 +02:00
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2013-11-18 01:25:17 +01:00
2013-12-09 10:12:08 +01:00
# include "rpcserver.h"
2013-11-18 01:25:17 +01:00
# include "main.h"
2013-04-13 07:13:08 +02:00
# include "net.h"
# include "netbase.h"
# include "protocol.h"
# include "sync.h"
2014-06-19 15:08:37 +02:00
# include "timedata.h"
2013-04-13 07:13:08 +02:00
# include "util.h"
2014-09-15 03:54:32 +02:00
# include "version.h"
2013-04-13 07:13:08 +02:00
# include <boost/foreach.hpp>
2014-09-14 12:43:56 +02:00
2013-04-13 07:13:08 +02:00
# include "json/json_spirit_value.h"
2012-06-29 05:18:38 +02:00
using namespace json_spirit ;
using namespace std ;
Value getconnectioncount ( const Array & params , bool fHelp )
{
if ( fHelp | | params . size ( ) ! = 0 )
throw runtime_error (
" getconnectioncount \n "
2013-10-29 12:29:44 +01:00
" \n Returns the number of connections to other nodes. \n "
" \n bResult: \n "
" n (numeric) The connection count \n "
" \n Examples: \n "
+ HelpExampleCli ( " getconnectioncount " , " " )
+ HelpExampleRpc ( " getconnectioncount " , " " )
) ;
2012-06-29 05:18:38 +02:00
2012-06-29 23:24:53 +02:00
LOCK ( cs_vNodes ) ;
2012-06-29 05:18:38 +02:00
return ( int ) vNodes . size ( ) ;
}
2013-08-22 13:34:33 +02:00
Value ping ( const Array & params , bool fHelp )
{
if ( fHelp | | params . size ( ) ! = 0 )
throw runtime_error (
" ping \n "
2013-10-29 12:29:44 +01:00
" \n Requests that a ping be sent to all other nodes, to measure ping time. \n "
2013-08-22 13:34:33 +02:00
" Results provided in getpeerinfo, pingtime and pingwait fields are decimal seconds. \n "
2014-01-30 14:13:30 +01:00
" Ping command is handled in queue with all other commands, so it measures processing backlog, not just network ping. \n "
2013-10-29 12:29:44 +01:00
" \n Examples: \n "
+ HelpExampleCli ( " ping " , " " )
+ HelpExampleRpc ( " ping " , " " )
) ;
2013-10-23 16:19:49 +02:00
2013-08-22 13:34:33 +02:00
// Request that each node send a ping during next message processing pass
LOCK ( cs_vNodes ) ;
BOOST_FOREACH ( CNode * pNode , vNodes ) {
pNode - > fPingQueued = true ;
}
return Value : : null ;
}
2012-06-29 23:24:53 +02:00
static void CopyNodeStats ( std : : vector < CNodeStats > & vstats )
{
vstats . clear ( ) ;
LOCK ( cs_vNodes ) ;
vstats . reserve ( vNodes . size ( ) ) ;
BOOST_FOREACH ( CNode * pnode , vNodes ) {
CNodeStats stats ;
pnode - > copyStats ( stats ) ;
vstats . push_back ( stats ) ;
}
}
Value getpeerinfo ( const Array & params , bool fHelp )
{
if ( fHelp | | params . size ( ) ! = 0 )
throw runtime_error (
" getpeerinfo \n "
2013-10-29 12:29:44 +01:00
" \n Returns data about each connected network node as a json array of objects. \n "
" \n bResult: \n "
" [ \n "
" { \n "
2014-07-15 16:13:10 +02:00
" \" id \" : n, (numeric) Peer index \n "
2013-10-29 12:29:44 +01:00
" \" addr \" : \" host:port \" , (string) The ip address and port of the peer \n "
" \" addrlocal \" : \" ip:port \" , (string) local address \n "
2014-06-06 09:15:55 +02:00
" \" services \" : \" xxxxxxxxxxxxxxxx \" , (string) The services offered \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 "
" \" pingtime \" : n, (numeric) ping time \n "
" \" pingwait \" : n, (numeric) ping wait \n "
" \" version \" : v, (numeric) The peer version, such as 7001 \n "
" \" subver \" : \" /Satoshi:0.8.5/ \" , (string) The string version \n "
" \" inbound \" : true|false, (boolean) Inbound (true) or Outbound (false) \n "
" \" 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-09-18 02:15:09 +02:00
" \" syncnode \" : true|false (boolean) if sync node \n "
2013-10-29 12:29:44 +01:00
" } \n "
" ,... \n "
2014-07-17 15:09:03 +02:00
" ] \n "
2013-10-29 12:29:44 +01:00
" \n Examples: \n "
+ HelpExampleCli ( " getpeerinfo " , " " )
+ HelpExampleRpc ( " getpeerinfo " , " " )
) ;
2012-06-29 23:24:53 +02:00
vector < CNodeStats > vstats ;
CopyNodeStats ( vstats ) ;
Array ret ;
BOOST_FOREACH ( const CNodeStats & stats , vstats ) {
Object obj ;
2013-11-18 01:25:17 +01:00
CNodeStateStats statestats ;
bool fStateStats = GetNodeStateStats ( stats . nodeid , statestats ) ;
2014-07-15 16:13:10 +02:00
obj . push_back ( Pair ( " id " , stats . nodeid ) ) ;
2012-06-29 23:24:53 +02:00
obj . push_back ( Pair ( " addr " , stats . addrName ) ) ;
2013-08-22 07:50:19 +02:00
if ( ! ( stats . addrLocal . empty ( ) ) )
obj . push_back ( Pair ( " addrlocal " , stats . addrLocal ) ) ;
2014-06-06 09:15:55 +02:00
obj . push_back ( Pair ( " services " , strprintf ( " %016x " , stats . nServices ) ) ) ;
2014-05-06 14:58:43 +02:00
obj . push_back ( Pair ( " lastsend " , stats . nLastSend ) ) ;
obj . push_back ( Pair ( " lastrecv " , stats . nLastRecv ) ) ;
obj . push_back ( Pair ( " bytessent " , stats . nSendBytes ) ) ;
obj . push_back ( Pair ( " bytesrecv " , stats . nRecvBytes ) ) ;
obj . push_back ( Pair ( " conntime " , stats . nTimeConnected ) ) ;
2013-08-22 13:34:33 +02:00
obj . push_back ( Pair ( " pingtime " , stats . dPingTime ) ) ;
if ( stats . dPingWait > 0.0 )
obj . push_back ( Pair ( " pingwait " , stats . dPingWait ) ) ;
2012-06-29 23:24:53 +02:00
obj . push_back ( Pair ( " version " , stats . nVersion ) ) ;
2013-11-26 12:52:21 +01:00
// Use the sanitized form of subver here, to avoid tricksy remote peers from
// corrupting or modifiying the JSON output by putting special characters in
// their ver message.
obj . push_back ( Pair ( " subver " , stats . cleanSubVer ) ) ;
2012-06-29 23:24:53 +02:00
obj . push_back ( Pair ( " inbound " , stats . fInbound ) ) ;
2012-07-01 05:40:26 +02:00
obj . push_back ( Pair ( " startingheight " , stats . nStartingHeight ) ) ;
2013-11-18 01:25:17 +01:00
if ( fStateStats ) {
obj . push_back ( Pair ( " banscore " , statestats . nMisbehavior ) ) ;
2014-06-23 00:00:26 +02:00
obj . push_back ( Pair ( " syncheight " , statestats . nSyncHeight ) ) ;
2013-11-18 01:25:17 +01:00
}
2014-02-16 18:44:49 +01:00
obj . push_back ( Pair ( " syncnode " , stats . fSyncNode ) ) ;
2014-06-21 13:34:36 +02:00
obj . push_back ( Pair ( " whitelisted " , stats . fWhitelisted ) ) ;
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
2013-01-23 17:45:00 +01:00
Value addnode ( const Array & params , bool fHelp )
{
string strCommand ;
if ( params . size ( ) = = 2 )
strCommand = params [ 1 ] . get_str ( ) ;
if ( fHelp | | params . size ( ) ! = 2 | |
( strCommand ! = " onetry " & & strCommand ! = " add " & & strCommand ! = " remove " ) )
throw runtime_error (
2013-10-29 12:29:44 +01:00
" addnode \" node \" \" add|remove|onetry \" \n "
" \n Attempts add or remove a node from the addnode list. \n "
" Or try a connection to a node once. \n "
" \n Arguments: \n "
" 1. \" node \" (string, required) The node (see getpeerinfo for nodes) \n "
" 2. \" command \" (string, required) '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 \n "
" \n Examples: \n "
+ HelpExampleCli ( " addnode " , " \" 192.168.0.6:8333 \" \" onetry \" " )
+ HelpExampleRpc ( " addnode " , " \" 192.168.0.6:8333 \" , \" onetry \" " )
) ;
2013-01-23 17:45:00 +01:00
string strNode = params [ 0 ] . get_str ( ) ;
if ( strCommand = = " onetry " )
{
CAddress addr ;
2014-05-10 00:39:49 +02:00
OpenNetworkConnection ( addr , NULL , strNode . c_str ( ) ) ;
2013-01-23 17:45:00 +01:00
return Value : : null ;
}
LOCK ( cs_vAddedNodes ) ;
vector < string > : : iterator it = vAddedNodes . begin ( ) ;
for ( ; it ! = vAddedNodes . end ( ) ; it + + )
if ( strNode = = * it )
break ;
if ( strCommand = = " add " )
{
if ( it ! = vAddedNodes . end ( ) )
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
vAddedNodes . push_back ( strNode ) ;
}
else if ( strCommand = = " remove " )
{
if ( it = = vAddedNodes . end ( ) )
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
vAddedNodes . erase ( it ) ;
}
return Value : : null ;
}
2013-01-23 17:48:17 +01:00
Value getaddednodeinfo ( const Array & params , bool fHelp )
{
if ( fHelp | | params . size ( ) < 1 | | params . size ( ) > 2 )
throw runtime_error (
2013-10-29 12:29:44 +01:00
" getaddednodeinfo dns ( \" node \" ) \n "
" \n Returns information about the given added node, or all added nodes \n "
2013-01-23 17:48:17 +01:00
" (note that onetry addnodes are not listed here) \n "
" If dns is false, only a list of added nodes will be provided, \n "
2013-10-29 12:29:44 +01:00
" otherwise connected information will also be available. \n "
" \n Arguments: \n "
" 1. dns (boolean, required) If false, only a list of added nodes will be provided, otherwise connected information will also be available. \n "
" 2. \" node \" (string, optional) If provided, return information about this specific node, otherwise all nodes are returned. \n "
" \n Result: \n "
" [ \n "
" { \n "
" \" addednode \" : \" 192.168.0.201 \" , (string) The node ip address \n "
" \" connected \" : true|false, (boolean) If connected \n "
" \" addresses \" : [ \n "
" { \n "
" \" address \" : \" 192.168.0.201:8333 \" , (string) The bitcoin server host and port \n "
" \" connected \" : \" outbound \" (string) connection, inbound or outbound \n "
" } \n "
" ,... \n "
" ] \n "
" } \n "
" ,... \n "
" ] \n "
" \n Examples: \n "
+ HelpExampleCli ( " getaddednodeinfo " , " true " )
+ HelpExampleCli ( " getaddednodeinfo " , " true \" 192.168.0.201 \" " )
+ HelpExampleRpc ( " getaddednodeinfo " , " true, \" 192.168.0.201 \" " )
) ;
2013-01-23 17:48:17 +01:00
bool fDns = params [ 0 ] . get_bool ( ) ;
list < string > laddedNodes ( 0 ) ;
if ( params . size ( ) = = 1 )
{
LOCK ( cs_vAddedNodes ) ;
BOOST_FOREACH ( string & strAddNode , vAddedNodes )
laddedNodes . push_back ( strAddNode ) ;
}
else
{
string strNode = params [ 1 ] . get_str ( ) ;
LOCK ( cs_vAddedNodes ) ;
BOOST_FOREACH ( string & strAddNode , vAddedNodes )
if ( strAddNode = = strNode )
{
laddedNodes . push_back ( strAddNode ) ;
break ;
}
if ( laddedNodes . size ( ) = = 0 )
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:48:17 +01:00
}
2014-01-27 10:31:22 +01:00
Array ret ;
2013-01-23 17:48:17 +01:00
if ( ! fDns )
{
BOOST_FOREACH ( string & strAddNode , laddedNodes )
2014-01-27 10:31:22 +01:00
{
Object obj ;
obj . push_back ( Pair ( " addednode " , strAddNode ) ) ;
ret . push_back ( obj ) ;
}
2013-01-23 17:48:17 +01:00
return ret ;
}
list < pair < string , vector < CService > > > laddedAddreses ( 0 ) ;
BOOST_FOREACH ( string & strAddNode , laddedNodes )
{
vector < CService > vservNode ( 0 ) ;
2013-05-07 15:16:25 +02:00
if ( Lookup ( strAddNode . c_str ( ) , vservNode , Params ( ) . GetDefaultPort ( ) , fNameLookup , 0 ) )
2013-01-23 17:48:17 +01:00
laddedAddreses . push_back ( make_pair ( strAddNode , vservNode ) ) ;
else
{
Object obj ;
obj . push_back ( Pair ( " addednode " , strAddNode ) ) ;
obj . push_back ( Pair ( " connected " , false ) ) ;
Array addresses ;
obj . push_back ( Pair ( " addresses " , addresses ) ) ;
}
}
LOCK ( cs_vNodes ) ;
for ( list < pair < string , vector < CService > > > : : iterator it = laddedAddreses . begin ( ) ; it ! = laddedAddreses . end ( ) ; it + + )
{
Object obj ;
obj . push_back ( Pair ( " addednode " , it - > first ) ) ;
Array addresses ;
bool fConnected = false ;
BOOST_FOREACH ( CService & addrNode , it - > second )
{
bool fFound = false ;
Object node ;
node . push_back ( Pair ( " address " , addrNode . ToString ( ) ) ) ;
BOOST_FOREACH ( CNode * pnode , vNodes )
if ( pnode - > addr = = addrNode )
{
fFound = true ;
fConnected = true ;
node . push_back ( Pair ( " connected " , pnode - > fInbound ? " inbound " : " outbound " ) ) ;
break ;
}
if ( ! fFound )
node . push_back ( Pair ( " connected " , " false " ) ) ;
addresses . push_back ( node ) ;
}
obj . push_back ( Pair ( " connected " , fConnected ) ) ;
obj . push_back ( Pair ( " addresses " , addresses ) ) ;
ret . push_back ( obj ) ;
}
return ret ;
}
2013-08-22 18:09:32 +02:00
Value getnettotals ( const Array & params , bool fHelp )
{
if ( fHelp | | params . size ( ) > 0 )
throw runtime_error (
" getnettotals \n "
2013-10-29 12:29:44 +01:00
" \n Returns information about network traffic, including bytes in, bytes out, \n "
" and current time. \n "
" \n Result: \n "
" { \n "
" \" totalbytesrecv \" : n, (numeric) Total bytes received \n "
2013-12-05 08:46:50 +01:00
" \" totalbytessent \" : n, (numeric) Total bytes sent \n "
2013-10-29 12:29:44 +01:00
" \" timemillis \" : t (numeric) Total cpu time \n "
" } \n "
" \n Examples: \n "
+ HelpExampleCli ( " getnettotals " , " " )
+ HelpExampleRpc ( " getnettotals " , " " )
) ;
2013-08-22 18:09:32 +02:00
Object obj ;
2014-05-06 14:58:43 +02:00
obj . push_back ( Pair ( " totalbytesrecv " , CNode : : GetTotalBytesRecv ( ) ) ) ;
obj . push_back ( Pair ( " totalbytessent " , CNode : : GetTotalBytesSent ( ) ) ) ;
obj . push_back ( Pair ( " timemillis " , GetTimeMillis ( ) ) ) ;
2013-08-22 18:09:32 +02:00
return obj ;
}
2014-05-05 13:22:28 +02:00
2014-07-30 15:35:14 +02:00
static Array GetNetworksInfo ( )
{
Array networks ;
for ( int n = 0 ; n < NET_MAX ; + + n )
{
enum Network network = static_cast < enum Network > ( n ) ;
if ( network = = NET_UNROUTABLE )
continue ;
proxyType proxy ;
Object obj ;
GetProxy ( network , proxy ) ;
obj . push_back ( Pair ( " name " , GetNetworkName ( network ) ) ) ;
obj . push_back ( Pair ( " limited " , IsLimited ( network ) ) ) ;
obj . push_back ( Pair ( " reachable " , IsReachable ( network ) ) ) ;
obj . push_back ( Pair ( " proxy " , proxy . IsValid ( ) ? proxy . ToStringIPPort ( ) : string ( ) ) ) ;
networks . push_back ( obj ) ;
}
return networks ;
}
2014-05-05 13:22:28 +02:00
Value getnetworkinfo ( const Array & params , bool fHelp )
{
if ( fHelp | | params . size ( ) ! = 0 )
throw runtime_error (
" getnetworkinfo \n "
" Returns an object containing various state info regarding P2P networking. \n "
" \n Result: \n "
" { \n "
2014-09-25 11:56:05 +02:00
" \" version \" : xxxxx, (numeric) the server version \n "
" \" subversion \" : \" /Satoshi:x.x.x/ \" , (string) the server subversion string \n "
" \" protocolversion \" : xxxxx, (numeric) the protocol version \n "
" \" localservices \" : \" xxxxxxxxxxxxxxxx \" , (string) the services we offer to the network \n "
" \" timeoffset \" : xxxxx, (numeric) the time offset \n "
" \" connections \" : xxxxx, (numeric) the number of connections \n "
" \" 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 "
" } \n "
" ,... \n "
2014-07-30 15:35:14 +02:00
" ], \n "
2014-09-25 11:56:05 +02:00
" \" relayfee \" : x.xxxxxxxx, (numeric) minimum relay fee for non-free transactions in btc/kb \n "
" \" 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 "
" } \n "
" \n Examples: \n "
+ HelpExampleCli ( " getnetworkinfo " , " " )
+ HelpExampleRpc ( " getnetworkinfo " , " " )
) ;
Object obj ;
2014-09-25 11:56:05 +02:00
obj . push_back ( Pair ( " version " , CLIENT_VERSION ) ) ;
2014-09-15 03:54:32 +02:00
obj . push_back ( Pair ( " subversion " ,
FormatSubVersion ( CLIENT_NAME , CLIENT_VERSION , std : : vector < string > ( ) ) ) ) ;
2014-09-25 11:56:05 +02:00
obj . push_back ( Pair ( " protocolversion " , PROTOCOL_VERSION ) ) ;
2014-06-06 09:15:55 +02:00
obj . push_back ( Pair ( " localservices " , strprintf ( " %016x " , nLocalServices ) ) ) ;
2014-05-06 14:58:43 +02:00
obj . push_back ( Pair ( " timeoffset " , GetTimeOffset ( ) ) ) ;
2014-05-05 13:22:28 +02:00
obj . push_back ( Pair ( " connections " , ( int ) vNodes . size ( ) ) ) ;
2014-07-30 15:35:14 +02:00
obj . push_back ( Pair ( " networks " , GetNetworksInfo ( ) ) ) ;
2014-07-03 20:25:32 +02:00
obj . push_back ( Pair ( " relayfee " , ValueFromAmount ( : : minRelayTxFee . GetFeePerK ( ) ) ) ) ;
2014-05-05 13:22:28 +02:00
Array localAddresses ;
{
LOCK ( cs_mapLocalHost ) ;
BOOST_FOREACH ( const PAIRTYPE ( CNetAddr , LocalServiceInfo ) & item , mapLocalHost )
{
Object rec ;
rec . push_back ( Pair ( " address " , item . first . ToString ( ) ) ) ;
rec . push_back ( Pair ( " port " , item . second . nPort ) ) ;
rec . push_back ( Pair ( " score " , item . second . nScore ) ) ;
localAddresses . push_back ( rec ) ;
}
}
obj . push_back ( Pair ( " localaddresses " , localAddresses ) ) ;
return obj ;
}