Merge bitcoin/bitcoin#21564: net: Avoid calling getnameinfo when formatting IPv4 addresses in CNetAddr::ToStringIP

58580a827d net: Avoid calling getnameinfo when formatting IPv4 addresses in CNetAddr::ToStringIP (practicalswift)
5858057384 net: Add IPv4ToString (we already have IPv6ToString) (practicalswift)

Pull request description:

  Avoid calling `getnameinfo` when formatting IPv4 addresses in `CNetAddr::ToStringIP`.

ACKs for top commit:
  naumenkogs:
    ACK 58580a827d
  0xB10C:
    ACK 58580a827d
  vasild:
    ACK 58580a827d

Tree-SHA512: 25e3c416acb74908d001baf1cf64c04cbc0d94ce8e7ce5a601f1343062d5d748cb406a3404e6f2b6e7e979c6300b38439e1bfd70ea90ec8c0ec2d7568f09fbcd
pull/826/head
MarcoFalke 4 years ago
commit aaf66413e1
No known key found for this signature in database
GPG Key ID: CE2B75697E69A548

@ -551,6 +551,11 @@ enum Network CNetAddr::GetNetwork() const
return m_net;
}
static std::string IPv4ToString(Span<const uint8_t> a)
{
return strprintf("%u.%u.%u.%u", a[0], a[1], a[2], a[3]);
}
static std::string IPv6ToString(Span<const uint8_t> a)
{
assert(a.size() == ADDR_IPV6_SIZE);
@ -571,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;
@ -581,9 +587,6 @@ std::string CNetAddr::ToStringIP() const
sizeof(name), nullptr, 0, NI_NUMERICHOST))
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 IPv6ToString(m_addr);
}
case NET_ONION:

Loading…
Cancel
Save