Changeset View
Changeset View
Standalone View
Standalone View
src/rpc/net.cpp
Show First 20 Lines • Show All 975 Lines • ▼ Show 20 Lines | for (const CAddress &addr : vAddr) { | ||||
obj.pushKV("services", uint64_t(addr.nServices)); | obj.pushKV("services", uint64_t(addr.nServices)); | ||||
obj.pushKV("address", addr.ToStringIP()); | obj.pushKV("address", addr.ToStringIP()); | ||||
obj.pushKV("port", addr.GetPort()); | obj.pushKV("port", addr.GetPort()); | ||||
ret.push_back(obj); | ret.push_back(obj); | ||||
} | } | ||||
return ret; | return ret; | ||||
} | } | ||||
static UniValue addpeeraddress(const Config &config, | |||||
const JSONRPCRequest &request) { | |||||
RPCHelpMan{ | |||||
"addpeeraddress", | |||||
"\nAdd the address of a potential peer to the address manager. This " | |||||
"RPC is for testing only.\n", | |||||
{ | |||||
{"address", RPCArg::Type::STR, RPCArg::Optional::NO, | |||||
"The IP address of the peer"}, | |||||
{"port", RPCArg::Type::NUM, RPCArg::Optional::NO, | |||||
"The port of the peer"}, | |||||
}, | |||||
RPCResult{ | |||||
RPCResult::Type::OBJ, | |||||
"", | |||||
"", | |||||
{ | |||||
{RPCResult::Type::BOOL, "success", | |||||
"whether the peer address was successfully added to the " | |||||
"address manager"}, | |||||
}, | |||||
}, | |||||
RPCExamples{HelpExampleCli("addpeeraddress", "\"1.2.3.4\" 8333") + | |||||
HelpExampleRpc("addpeeraddress", "\"1.2.3.4\", 8333")}, | |||||
} | |||||
.Check(request); | |||||
NodeContext &node = EnsureNodeContext(request.context); | |||||
if (!node.connman) { | |||||
throw JSONRPCError( | |||||
RPC_CLIENT_P2P_DISABLED, | |||||
"Error: Peer-to-peer functionality missing or disabled"); | |||||
} | |||||
UniValue obj(UniValue::VOBJ); | |||||
std::string addr_string = request.params[0].get_str(); | |||||
uint16_t port = request.params[1].get_int(); | |||||
CNetAddr net_addr; | |||||
if (!LookupHost(addr_string, net_addr, false)) { | |||||
obj.pushKV("success", false); | |||||
return obj; | |||||
} | |||||
CAddress address = CAddress({net_addr, port}, ServiceFlags(NODE_NETWORK)); | |||||
address.nTime = GetAdjustedTime(); | |||||
// The source address is set equal to the address. This is equivalent to the | |||||
// peer announcing itself. | |||||
if (!node.connman->AddNewAddresses({address}, address)) { | |||||
obj.pushKV("success", false); | |||||
return obj; | |||||
} | |||||
obj.pushKV("success", true); | |||||
return obj; | |||||
} | |||||
void RegisterNetRPCCommands(CRPCTable &t) { | void RegisterNetRPCCommands(CRPCTable &t) { | ||||
// clang-format off | // clang-format off | ||||
static const CRPCCommand commands[] = { | static const CRPCCommand commands[] = { | ||||
// category name actor (function) argNames | // category name actor (function) argNames | ||||
// ------------------- ------------------------ ---------------------- ---------- | // ------------------- ------------------------ ---------------------- ---------- | ||||
{ "network", "getconnectioncount", getconnectioncount, {} }, | { "network", "getconnectioncount", getconnectioncount, {} }, | ||||
{ "network", "ping", ping, {} }, | { "network", "ping", ping, {} }, | ||||
{ "network", "getpeerinfo", getpeerinfo, {} }, | { "network", "getpeerinfo", getpeerinfo, {} }, | ||||
{ "network", "addnode", addnode, {"node","command"} }, | { "network", "addnode", addnode, {"node","command"} }, | ||||
{ "network", "disconnectnode", disconnectnode, {"address", "nodeid"} }, | { "network", "disconnectnode", disconnectnode, {"address", "nodeid"} }, | ||||
{ "network", "getaddednodeinfo", getaddednodeinfo, {"node"} }, | { "network", "getaddednodeinfo", getaddednodeinfo, {"node"} }, | ||||
{ "network", "getnettotals", getnettotals, {} }, | { "network", "getnettotals", getnettotals, {} }, | ||||
{ "network", "getnetworkinfo", getnetworkinfo, {} }, | { "network", "getnetworkinfo", getnetworkinfo, {} }, | ||||
{ "network", "setban", setban, {"subnet", "command", "bantime", "absolute"} }, | { "network", "setban", setban, {"subnet", "command", "bantime", "absolute"} }, | ||||
{ "network", "listbanned", listbanned, {} }, | { "network", "listbanned", listbanned, {} }, | ||||
{ "network", "clearbanned", clearbanned, {} }, | { "network", "clearbanned", clearbanned, {} }, | ||||
{ "network", "setnetworkactive", setnetworkactive, {"state"} }, | { "network", "setnetworkactive", setnetworkactive, {"state"} }, | ||||
{ "network", "getnodeaddresses", getnodeaddresses, {"count"} }, | { "network", "getnodeaddresses", getnodeaddresses, {"count"} }, | ||||
{ "hidden", "addpeeraddress", addpeeraddress, {"address", "port"} }, | |||||
}; | }; | ||||
// clang-format on | // clang-format on | ||||
for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++) { | for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++) { | ||||
t.appendCommand(commands[vcidx].name, &commands[vcidx]); | t.appendCommand(commands[vcidx].name, &commands[vcidx]); | ||||
} | } | ||||
} | } |