diff --git a/src/netaddress.cpp b/src/netaddress.cpp index a930f5d2318ff..6e61557c6dcdb 100644 --- a/src/netaddress.cpp +++ b/src/netaddress.cpp @@ -636,7 +636,7 @@ uint32_t CNetAddr::GetLinkedIPv4() const assert(false); } -uint32_t CNetAddr::GetNetClass() const +Network CNetAddr::GetNetClass() const { // Make sure that if we return NET_IPV6, then IsIPv6() is true. The callers expect that. diff --git a/src/netaddress.h b/src/netaddress.h index 7c5fbd26166ea..373a99d9a0182 100644 --- a/src/netaddress.h +++ b/src/netaddress.h @@ -188,7 +188,7 @@ class CNetAddr std::string ToStringIP() const; uint64_t GetHash() const; bool GetInAddr(struct in_addr* pipv4Addr) const; - uint32_t GetNetClass() const; + Network GetNetClass() const; //! For IPv4, mapped IPv4, SIIT translated IPv4, Teredo, 6to4 tunneled addresses, return the relevant IPv4 address as a uint32. uint32_t GetLinkedIPv4() const; diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp index 3afbd4e780cf0..078b34a9da580 100644 --- a/src/rpc/net.cpp +++ b/src/rpc/net.cpp @@ -572,10 +572,11 @@ static UniValue getnodeaddresses(const JSONRPCRequest& request) "\nResult:\n" "[\n" " {\n" - " \"time\": ttt, (numeric) Timestamp in seconds since epoch (Jan 1 1970 GMT) keeping track of when the node was last seen\n" - " \"services\": n, (numeric) The services offered\n" + " \"time\": ttt, (numeric) Timestamp in seconds since epoch (Jan 1 1970 GMT) when the node was last seen\n" + " \"services\": n, (numeric) The services offered by the node\n" " \"address\": \"host\", (string) The address of the node\n" - " \"port\": n (numeric) The port of the node\n" + " \"port\": n, (numeric) The port number of the node\n" + " \"network\": \"xxxx\" (string) The network (ipv4, ipv6, onion) the node connected through\n" " }\n" " ,...\n" "]\n" @@ -589,15 +590,11 @@ static UniValue getnodeaddresses(const JSONRPCRequest& request) throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled"); } - int count = 1; - if (!request.params[0].isNull()) { - count = request.params[0].get_int(); - if (count < 0) { - throw JSONRPCError(RPC_INVALID_PARAMETER, "Address count out of range"); - } - } + const int count{request.params[0].isNull() ? 1 : request.params[0].get_int()}; + if (count < 0) throw JSONRPCError(RPC_INVALID_PARAMETER, "Address count out of range"); + // returns a shuffled list of CAddress - std::vector vAddr = g_connman->GetAddresses(count, /* max_pct */ 0); + const std::vector vAddr{g_connman->GetAddresses(count, /* max_pct */ 0)}; UniValue ret(UniValue::VARR); for (const CAddress& addr : vAddr) { @@ -606,6 +603,7 @@ static UniValue getnodeaddresses(const JSONRPCRequest& request) obj.pushKV("services", (uint64_t)addr.nServices); obj.pushKV("address", addr.ToStringIP()); obj.pushKV("port", addr.GetPort()); + obj.pushKV("network", GetNetworkName(addr.GetNetClass())); ret.push_back(obj); } return ret; diff --git a/test/functional/rpc_net.py b/test/functional/rpc_net.py index 47dd10fe34471..a59bd2bcb2dac 100755 --- a/test/functional/rpc_net.py +++ b/test/functional/rpc_net.py @@ -107,7 +107,7 @@ def _test_getnodeaddresses(self): for i in range(10000): first_octet = i >> 8 second_octet = i % 256 - a = "{}.{}.1.1".format(first_octet, second_octet) + a = "{}.{}.1.1".format(first_octet, second_octet) # IPv4 imported_addrs.append(a) self.nodes[0].addpeeraddress(a, 51472) @@ -124,6 +124,7 @@ def _test_getnodeaddresses(self): assert_equal(a["services"], NODE_NETWORK) assert a["address"] in imported_addrs assert_equal(a["port"], 51472) + assert_equal(a["network"], "ipv4") node_addresses = self.nodes[0].getnodeaddresses(1) assert_equal(len(node_addresses), 1)