aboutsummaryrefslogtreecommitdiff
path: root/src/rpc/net.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/rpc/net.cpp')
-rw-r--r--src/rpc/net.cpp96
1 files changed, 80 insertions, 16 deletions
diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp
index f7b6c68344..8d796b8e9b 100644
--- a/src/rpc/net.cpp
+++ b/src/rpc/net.cpp
@@ -45,6 +45,12 @@ const std::vector<std::string> CONNECTION_TYPE_DOC{
"feeler (short-lived automatic connection for testing addresses)"
};
+const std::vector<std::string> TRANSPORT_TYPE_DOC{
+ "detecting (peer could be v1 or v2)",
+ "v1 (plaintext transport protocol)",
+ "v2 (BIP324 encrypted transport protocol)"
+};
+
static RPCHelpMan getconnectioncount()
{
return RPCHelpMan{"getconnectioncount",
@@ -164,6 +170,8 @@ static RPCHelpMan getpeerinfo()
{RPCResult::Type::STR, "connection_type", "Type of connection: \n" + Join(CONNECTION_TYPE_DOC, ",\n") + ".\n"
"Please note this output is unlikely to be stable in upcoming releases as we iterate to\n"
"best capture connection behaviors."},
+ {RPCResult::Type::STR, "transport_protocol_type", "Type of transport protocol: \n" + Join(TRANSPORT_TYPE_DOC, ",\n") + ".\n"},
+ {RPCResult::Type::STR, "session_id", "The session ID for this connection, or \"\" if there is none (\"v2\" transport protocol only).\n"},
}},
}},
},
@@ -268,6 +276,8 @@ static RPCHelpMan getpeerinfo()
}
obj.pushKV("bytesrecv_per_msg", recvPerMsgType);
obj.pushKV("connection_type", ConnectionTypeAsString(stats.m_conn_type));
+ obj.pushKV("transport_protocol_type", TransportTypeAsString(stats.m_transport_type));
+ obj.pushKV("session_id", stats.m_session_id);
ret.push_back(obj);
}
@@ -287,20 +297,19 @@ static RPCHelpMan addnode()
strprintf("Addnode connections are limited to %u at a time", MAX_ADDNODE_CONNECTIONS) +
" and are counted separately from the -maxconnections limit.\n",
{
- {"node", RPCArg::Type::STR, RPCArg::Optional::NO, "The node (see getpeerinfo for nodes)"},
+ {"node", RPCArg::Type::STR, RPCArg::Optional::NO, "The address of the peer to connect to"},
{"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"},
+ {"v2transport", RPCArg::Type::BOOL, RPCArg::Default{false}, "Attempt to connect using BIP324 v2 transport protocol (ignored for 'remove' command)"},
},
RPCResult{RPCResult::Type::NONE, "", ""},
RPCExamples{
- HelpExampleCli("addnode", "\"192.168.0.6:8333\" \"onetry\"")
- + HelpExampleRpc("addnode", "\"192.168.0.6:8333\", \"onetry\"")
+ HelpExampleCli("addnode", "\"192.168.0.6:8333\" \"onetry\" true")
+ + HelpExampleRpc("addnode", "\"192.168.0.6:8333\", \"onetry\" true")
},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
- std::string strCommand;
- if (!request.params[1].isNull())
- strCommand = request.params[1].get_str();
- if (strCommand != "onetry" && strCommand != "add" && strCommand != "remove") {
+ const std::string command{request.params[1].get_str()};
+ if (command != "onetry" && command != "add" && command != "remove") {
throw std::runtime_error(
self.ToString());
}
@@ -308,24 +317,29 @@ static RPCHelpMan addnode()
NodeContext& node = EnsureAnyNodeContext(request.context);
CConnman& connman = EnsureConnman(node);
- std::string strNode = request.params[0].get_str();
+ const std::string node_arg{request.params[0].get_str()};
+ bool use_v2transport = self.Arg<bool>(2);
+
+ if (use_v2transport && !(node.connman->GetLocalServices() & NODE_P2P_V2)) {
+ throw JSONRPCError(RPC_INVALID_PARAMETER, "Error: v2transport requested but not enabled (see -v2transport)");
+ }
- if (strCommand == "onetry")
+ if (command == "onetry")
{
CAddress addr;
- connman.OpenNetworkConnection(addr, false, nullptr, strNode.c_str(), ConnectionType::MANUAL);
+ connman.OpenNetworkConnection(addr, /*fCountFailure=*/false, /*grant_outbound=*/{}, node_arg.c_str(), ConnectionType::MANUAL, use_v2transport);
return UniValue::VNULL;
}
- if (strCommand == "add")
+ if (command == "add")
{
- if (!connman.AddNode(strNode)) {
+ if (!connman.AddNode({node_arg, use_v2transport})) {
throw JSONRPCError(RPC_CLIENT_NODE_ALREADY_ADDED, "Error: Node already added");
}
}
- else if(strCommand == "remove")
+ else if (command == "remove")
{
- if (!connman.RemoveAddedNode(strNode)) {
+ if (!connman.RemoveAddedNode(node_arg)) {
throw JSONRPCError(RPC_CLIENT_NODE_NOT_ADDED, "Error: Node could not be removed. It has not been added previously.");
}
}
@@ -477,7 +491,7 @@ static RPCHelpMan getaddednodeinfo()
if (!request.params[0].isNull()) {
bool found = false;
for (const AddedNodeInfo& info : vInfo) {
- if (info.strAddedNode == request.params[0].get_str()) {
+ if (info.m_params.m_added_node == request.params[0].get_str()) {
vInfo.assign(1, info);
found = true;
break;
@@ -492,7 +506,7 @@ static RPCHelpMan getaddednodeinfo()
for (const AddedNodeInfo& info : vInfo) {
UniValue obj(UniValue::VOBJ);
- obj.pushKV("addednode", info.strAddedNode);
+ obj.pushKV("addednode", info.m_params.m_added_node);
obj.pushKV("connected", info.fConnected);
UniValue addresses(UniValue::VARR);
if (info.fConnected) {
@@ -1016,6 +1030,55 @@ static RPCHelpMan sendmsgtopeer()
};
}
+static RPCHelpMan getaddrmaninfo()
+{
+ return RPCHelpMan{"getaddrmaninfo",
+ "\nProvides information about the node's address manager by returning the number of "
+ "addresses in the `new` and `tried` tables and their sum for all networks.\n"
+ "This RPC is for testing only.\n",
+ {},
+ RPCResult{
+ RPCResult::Type::OBJ_DYN, "", "json object with network type as keys",
+ {
+ {RPCResult::Type::OBJ, "network", "the network (" + Join(GetNetworkNames(), ", ") + ")",
+ {
+ {RPCResult::Type::NUM, "new", "number of addresses in the new table, which represent potential peers the node has discovered but hasn't yet successfully connected to."},
+ {RPCResult::Type::NUM, "tried", "number of addresses in the tried table, which represent peers the node has successfully connected to in the past."},
+ {RPCResult::Type::NUM, "total", "total number of addresses in both new/tried tables"},
+ }},
+ }
+ },
+ RPCExamples{
+ HelpExampleCli("getaddrmaninfo", "")
+ + HelpExampleRpc("getaddrmaninfo", "")
+ },
+ [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
+ {
+ NodeContext& node = EnsureAnyNodeContext(request.context);
+ if (!node.addrman) {
+ throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Address manager functionality missing or disabled");
+ }
+
+ UniValue ret(UniValue::VOBJ);
+ for (int n = 0; n < NET_MAX; ++n) {
+ enum Network network = static_cast<enum Network>(n);
+ if (network == NET_UNROUTABLE || network == NET_INTERNAL) continue;
+ UniValue obj(UniValue::VOBJ);
+ obj.pushKV("new", node.addrman->Size(network, true));
+ obj.pushKV("tried", node.addrman->Size(network, false));
+ obj.pushKV("total", node.addrman->Size(network));
+ ret.pushKV(GetNetworkName(network), obj);
+ }
+ UniValue obj(UniValue::VOBJ);
+ obj.pushKV("new", node.addrman->Size(std::nullopt, true));
+ obj.pushKV("tried", node.addrman->Size(std::nullopt, false));
+ obj.pushKV("total", node.addrman->Size());
+ ret.pushKV("all_networks", obj);
+ return ret;
+ },
+ };
+}
+
void RegisterNetRPCCommands(CRPCTable& t)
{
static const CRPCCommand commands[]{
@@ -1035,6 +1098,7 @@ void RegisterNetRPCCommands(CRPCTable& t)
{"hidden", &addconnection},
{"hidden", &addpeeraddress},
{"hidden", &sendmsgtopeer},
+ {"hidden", &getaddrmaninfo},
};
for (const auto& c : commands) {
t.appendCommand(c.name, &c);