From 58580573843858068db69a378b6e9363889b1f6d Mon Sep 17 00:00:00 2001 From: practicalswift Date: Thu, 1 Apr 2021 08:02:21 +0000 Subject: [PATCH 1/2] net: Add IPv4ToString (we already have IPv6ToString) --- src/netaddress.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/netaddress.cpp b/src/netaddress.cpp index 69edc15c66..b2a7a29d06 100644 --- a/src/netaddress.cpp +++ b/src/netaddress.cpp @@ -551,6 +551,11 @@ enum Network CNetAddr::GetNetwork() const return m_net; } +static std::string IPv4ToString(Span a) +{ + return strprintf("%u.%u.%u.%u", a[0], a[1], a[2], a[3]); +} + static std::string IPv6ToString(Span a) { assert(a.size() == ADDR_IPV6_SIZE); @@ -582,7 +587,7 @@ std::string CNetAddr::ToStringIP() const return std::string(name); } if (m_net == NET_IPV4) { - return strprintf("%u.%u.%u.%u", m_addr[0], m_addr[1], m_addr[2], m_addr[3]); + return IPv4ToString(m_addr); } return IPv6ToString(m_addr); } From 58580a827d10c0675c3483e2aeca1d3ab8050ae0 Mon Sep 17 00:00:00 2001 From: practicalswift Date: Thu, 1 Apr 2021 08:01:33 +0000 Subject: [PATCH 2/2] net: Avoid calling getnameinfo when formatting IPv4 addresses in CNetAddr::ToStringIP --- src/netaddress.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/netaddress.cpp b/src/netaddress.cpp index b2a7a29d06..d56ae78e92 100644 --- a/src/netaddress.cpp +++ b/src/netaddress.cpp @@ -576,6 +576,7 @@ std::string CNetAddr::ToStringIP() const { switch (m_net) { case NET_IPV4: + return IPv4ToString(m_addr); case NET_IPV6: { CService serv(*this, 0); struct sockaddr_storage sockaddr; @@ -586,9 +587,6 @@ std::string CNetAddr::ToStringIP() const sizeof(name), nullptr, 0, NI_NUMERICHOST)) return std::string(name); } - if (m_net == NET_IPV4) { - return IPv4ToString(m_addr); - } return IPv6ToString(m_addr); } case NET_ONION: