Add addnode RPC command.
[novacoin.git] / src / rpcnet.cpp
1 // Copyright (c) 2009-2012 Bitcoin Developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5 #include "bitcoinrpc.h"
6 #include "alert.h"
7 #include "wallet.h"
8 #include "db.h"
9 #include "walletdb.h"
10 #include "net.h"
11
12 using namespace json_spirit;
13 using namespace std;
14
15 Value getconnectioncount(const Array& params, bool fHelp)
16 {
17     if (fHelp || params.size() != 0)
18         throw runtime_error(
19             "getconnectioncount\n"
20             "Returns the number of connections to other nodes.");
21
22     LOCK(cs_vNodes);
23     return (int)vNodes.size();
24 }
25
26 static void CopyNodeStats(std::vector<CNodeStats>& vstats)
27 {
28     vstats.clear();
29
30     LOCK(cs_vNodes);
31     vstats.reserve(vNodes.size());
32     BOOST_FOREACH(CNode* pnode, vNodes) {
33         CNodeStats stats;
34         pnode->copyStats(stats);
35         vstats.push_back(stats);
36     }
37 }
38
39 Value getpeerinfo(const Array& params, bool fHelp)
40 {
41     if (fHelp || params.size() != 0)
42         throw runtime_error(
43             "getpeerinfo\n"
44             "Returns data about each connected network node.");
45
46     vector<CNodeStats> vstats;
47     CopyNodeStats(vstats);
48
49     Array ret;
50
51     BOOST_FOREACH(const CNodeStats& stats, vstats) {
52         Object obj;
53
54         obj.push_back(Pair("addr", stats.addrName));
55         obj.push_back(Pair("services", strprintf("%08" PRIx64, stats.nServices)));
56         obj.push_back(Pair("lastsend", (boost::int64_t)stats.nLastSend));
57         obj.push_back(Pair("lastrecv", (boost::int64_t)stats.nLastRecv));
58         obj.push_back(Pair("bytessent", (boost::int64_t)stats.nSendBytes));
59         obj.push_back(Pair("bytesrecv", (boost::int64_t)stats.nRecvBytes));
60         obj.push_back(Pair("conntime", (boost::int64_t)stats.nTimeConnected));
61         obj.push_back(Pair("version", stats.nVersion));
62         obj.push_back(Pair("subver", stats.strSubVer));
63         obj.push_back(Pair("inbound", stats.fInbound));
64         obj.push_back(Pair("releasetime", (boost::int64_t)stats.nReleaseTime));
65         obj.push_back(Pair("startingheight", stats.nStartingHeight));
66         obj.push_back(Pair("banscore", stats.nMisbehavior));
67         if (stats.fSyncNode)
68             obj.push_back(Pair("syncnode", true));
69         ret.push_back(obj);
70     }
71
72     return ret;
73 }
74
75 Value addnode(const Array& params, bool fHelp)
76 {
77     string strCommand;
78     if (params.size() == 2)
79         strCommand = params[1].get_str();
80     if (fHelp || params.size() != 2 ||
81         (strCommand != "onetry" && strCommand != "add" && strCommand != "remove"))
82         throw runtime_error(
83             "addnode <node> <add|remove|onetry>\n"
84             "Attempts add or remove <node> from the addnode list or try a connection to <node> once.");
85
86     string strNode = params[0].get_str();
87
88     if (strCommand == "onetry")
89     {
90         CAddress addr;
91         ConnectNode(addr, strNode.c_str());
92         return Value::null;
93     }
94
95     LOCK(cs_vAddedNodes);
96     vector<string>::iterator it = vAddedNodes.begin();
97     for(; it != vAddedNodes.end(); it++)
98         if (strNode == *it)
99             break;
100
101     if (strCommand == "add")
102     {
103         if (it != vAddedNodes.end())
104             throw JSONRPCError(-23, "Error: Node already added");
105         vAddedNodes.push_back(strNode);
106     }
107     else if(strCommand == "remove")
108     {
109         if (it == vAddedNodes.end())
110             throw JSONRPCError(-24, "Error: Node has not been added.");
111         vAddedNodes.erase(it);
112     }
113
114     return Value::null;
115 }
116
117
118 extern CCriticalSection cs_mapAlerts;
119 extern map<uint256, CAlert> mapAlerts;
120  
121 // ppcoin: send alert.  
122 // There is a known deadlock situation with ThreadMessageHandler
123 // ThreadMessageHandler: holds cs_vSend and acquiring cs_main in SendMessages()
124 // ThreadRPCServer: holds cs_main and acquiring cs_vSend in alert.RelayTo()/PushMessage()/BeginMessage()
125 Value sendalert(const Array& params, bool fHelp)
126 {
127     if (fHelp || params.size() < 6)
128         throw runtime_error(
129             "sendalert <message> <privatekey> <minver> <maxver> <priority> <id> [cancelupto]\n"
130             "<message> is the alert text message\n"
131             "<privatekey> is hex string of alert master private key\n"
132             "<minver> is the minimum applicable internal client version\n"
133             "<maxver> is the maximum applicable internal client version\n"
134             "<priority> is integer priority number\n"
135             "<id> is the alert id\n"
136             "[cancelupto] cancels all alert id's up to this number\n"
137             "Returns true or false.");
138
139     CAlert alert;
140     CKey key;
141
142     alert.strStatusBar = params[0].get_str();
143     alert.nMinVer = params[2].get_int();
144     alert.nMaxVer = params[3].get_int();
145     alert.nPriority = params[4].get_int();
146     alert.nID = params[5].get_int();
147     if (params.size() > 6)
148         alert.nCancel = params[6].get_int();
149     alert.nVersion = PROTOCOL_VERSION;
150     alert.nRelayUntil = GetAdjustedTime() + 365*24*60*60;
151     alert.nExpiration = GetAdjustedTime() + 365*24*60*60;
152
153     CDataStream sMsg(SER_NETWORK, PROTOCOL_VERSION);
154     sMsg << (CUnsignedAlert)alert;
155     alert.vchMsg = vector<unsigned char>(sMsg.begin(), sMsg.end());
156
157     vector<unsigned char> vchPrivKey = ParseHex(params[1].get_str());
158     key.SetPrivKey(CPrivKey(vchPrivKey.begin(), vchPrivKey.end())); // if key is not correct openssl may crash
159     if (!key.Sign(Hash(alert.vchMsg.begin(), alert.vchMsg.end()), alert.vchSig))
160         throw runtime_error(
161             "Unable to sign alert, check private key?\n");  
162     if(!alert.ProcessAlert()) 
163         throw runtime_error(
164             "Failed to process alert.\n");
165     // Relay alert
166     {
167         LOCK(cs_vNodes);
168         BOOST_FOREACH(CNode* pnode, vNodes)
169             alert.RelayTo(pnode);
170     }
171
172     Object result;
173     result.push_back(Pair("strStatusBar", alert.strStatusBar));
174     result.push_back(Pair("nVersion", alert.nVersion));
175     result.push_back(Pair("nMinVer", alert.nMinVer));
176     result.push_back(Pair("nMaxVer", alert.nMaxVer));
177     result.push_back(Pair("nPriority", alert.nPriority));
178     result.push_back(Pair("nID", alert.nID));
179     if (alert.nCancel > 0)
180         result.push_back(Pair("nCancel", alert.nCancel));
181     return result;
182 }
183
184 Value getnettotals(const Array& params, bool fHelp)
185 {
186     if (fHelp || params.size() > 0)
187         throw runtime_error(
188             "getnettotals\n"
189             "Returns information about network traffic, including bytes in, bytes out,\n"
190             "and current time.");
191
192     Object obj;
193     obj.push_back(Pair("totalbytesrecv", static_cast< boost::uint64_t>(CNode::GetTotalBytesRecv())));
194     obj.push_back(Pair("totalbytessent", static_cast<boost::uint64_t>(CNode::GetTotalBytesSent())));
195     obj.push_back(Pair("timemillis", static_cast<boost::int64_t>(GetTimeMillis())));
196     return obj;
197 }