forked from TrezarCoin/TrezarCoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpcnet.cpp
275 lines (233 loc) · 9.26 KB
/
rpcnet.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Copyright (c) 2009-2012 Bitcoin Developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "bitcoinrpc.h"
#include "net.h"
#include "alert.h"
#include "wallet.h"
#include "db.h"
#include "walletdb.h"
using namespace json_spirit;
using namespace std;
Value getconnectioncount(const Array& params, bool fHelp)
{
if (fHelp || params.size() != 0)
throw runtime_error(
"getconnectioncount\n"
"Returns the number of connections to other nodes.");
LOCK(cs_vNodes);
return (int)vNodes.size();
}
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"
"Returns data about each connected network node.");
vector<CNodeStats> vstats;
CopyNodeStats(vstats);
Array ret;
BOOST_FOREACH(const CNodeStats& stats, vstats) {
Object obj;
obj.push_back(Pair("addr", stats.addrName));
obj.push_back(Pair("services", strprintf("%08" PRI64x, stats.nServices)));
obj.push_back(Pair("lastsend", (boost::int64_t)stats.nLastSend));
obj.push_back(Pair("lastrecv", (boost::int64_t)stats.nLastRecv));
obj.push_back(Pair("conntime", (boost::int64_t)stats.nTimeConnected));
obj.push_back(Pair("version", stats.nVersion));
obj.push_back(Pair("subver", stats.strSubVer));
obj.push_back(Pair("inbound", stats.fInbound));
obj.push_back(Pair("releasetime", (boost::int64_t)stats.nReleaseTime));
obj.push_back(Pair("startingheight", stats.nStartingHeight));
obj.push_back(Pair("txbytes", (boost::int64_t)stats.nTxBytes));
obj.push_back(Pair("rxbytes", (boost::int64_t)stats.nRxBytes));
obj.push_back(Pair("banscore", stats.nMisbehavior));
ret.push_back(obj);
}
return ret;
}
extern CCriticalSection cs_mapAlerts;
extern map<uint256, CAlert> mapAlerts;
// ppcoin: send alert.
// There is a known deadlock situation with ThreadMessageHandler
// ThreadMessageHandler: holds cs_vSend and acquiring cs_main in SendMessages()
// ThreadRPCServer: holds cs_main and acquiring cs_vSend in alert.RelayTo()/PushMessage()/BeginMessage()
Value sendalert(const Array& params, bool fHelp)
{
if (fHelp || params.size() < 6)
throw runtime_error(
"sendalert <message> <privatekey> <minver> <maxver> <priority> <id> [cancelupto]\n"
"<message> is the alert text message\n"
"<privatekey> is hex string of alert master private key\n"
"<minver> is the minimum applicable internal client version\n"
"<maxver> is the maximum applicable internal client version\n"
"<priority> is integer priority number\n"
"<id> is the alert id\n"
"[cancelupto] cancels all alert id's up to this number\n"
"Returns true or false.");
CAlert alert;
CKey key;
alert.strStatusBar = params[0].get_str();
alert.nMinVer = params[2].get_int();
alert.nMaxVer = params[3].get_int();
alert.nPriority = params[4].get_int();
alert.nID = params[5].get_int();
if (params.size() > 6)
alert.nCancel = params[6].get_int();
alert.nVersion = PROTOCOL_VERSION;
alert.nRelayUntil = GetAdjustedTime() + 365*24*60*60;
alert.nExpiration = GetAdjustedTime() + 365*24*60*60;
CDataStream sMsg(SER_NETWORK, PROTOCOL_VERSION);
sMsg << (CUnsignedAlert)alert;
alert.vchMsg = vector<unsigned char>(sMsg.begin(), sMsg.end());
vector<unsigned char> vchPrivKey = ParseHex(params[1].get_str());
key.SetPrivKey(CPrivKey(vchPrivKey.begin(), vchPrivKey.end())); // if key is not correct openssl may crash
if (!key.Sign(Hash(alert.vchMsg.begin(), alert.vchMsg.end()), alert.vchSig))
throw runtime_error(
"Unable to sign alert, check private key?\n");
if(!alert.ProcessAlert())
throw runtime_error(
"Failed to process alert.\n");
// Relay alert
{
LOCK(cs_vNodes);
BOOST_FOREACH(CNode* pnode, vNodes)
alert.RelayTo(pnode);
}
Object result;
result.push_back(Pair("strStatusBar", alert.strStatusBar));
result.push_back(Pair("nVersion", alert.nVersion));
result.push_back(Pair("nMinVer", alert.nMinVer));
result.push_back(Pair("nMaxVer", alert.nMaxVer));
result.push_back(Pair("nPriority", alert.nPriority));
result.push_back(Pair("nID", alert.nID));
if (alert.nCancel > 0)
result.push_back(Pair("nCancel", alert.nCancel));
return result;
}
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(
"addnode <node:port> <add|remove|onetry>\n"
"Attempts to add or remove a node from the list or try a connection to the node once.");
string strNode = params[0].get_str();
if(strCommand == "onetry") {
CAddress addr;
ConnectNode(addr, strNode.c_str());
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())
throw JSONRPCError(-23, "Error: Node has been added already");
vAddedNodes.push_back(strNode);
}
else if(strCommand == "remove") {
if(it == vAddedNodes.end())
throw JSONRPCError(-24, "Error: Node has not been added");
vAddedNodes.erase(it);
}
return(Value::null);
}
Value getaddednodeinfo(const Array& params, bool fHelp) {
if(fHelp || (params.size() < 1) || (params.size() > 2))
throw runtime_error(
"getaddednodeinfo <extinfo> [node:port]\n"
"Returns information about the given added node or all added nodes\n"
"<extinfo> is true or false to provide extended information or not\n"
"(note that onetry addnodes are not listed)\n");
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)
throw JSONRPCError(-24, "Error: Node has not been added");
}
if(!fDns) {
Object ret;
BOOST_FOREACH(string& strAddNode, laddedNodes)
ret.push_back(Pair("addednode", strAddNode));
return(ret);
}
Array ret;
list<pair<string, vector<CService> > > laddedAddreses(0);
BOOST_FOREACH(string& strAddNode, laddedNodes) {
vector<CService> vservNode(0);
if(Lookup(strAddNode.c_str(), vservNode, GetDefaultPort(), fNameLookup, 0))
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);
}
Value getnettotals(const Array ¶ms, bool fHelp) {
if(fHelp || (params.size() > 0))
throw(runtime_error(
"getnettotals\n"
"Returns information about network traffic including bytes received,\n"
"bytes transmitted and current time."));
Object obj;
obj.push_back(Pair("rxbytestotal", (boost::uint64_t)CNode::GetTotalBytesRx()));
obj.push_back(Pair("txbytestotal", (boost::uint64_t)CNode::GetTotalBytesTx()));
obj.push_back(Pair("timemillis", (boost::int64_t)GetTimeMillis()));
return(obj);
}