Skip to content

Commit 7b87fca

Browse files
committed
Merge bitcoin/bitcoin#21756: Avoid calling getnameinfo when formatting IPv6 addresses in CNetAddr::ToStringIP
54548ba net: Avoid calling getnameinfo when formatting IPv6 addresses in CNetAddr::ToStringIP (practicalswift) c10f27f net: Make IPv6ToString do zero compression as described in RFC 5952 (practicalswift) Pull request description: Avoid calling `getnameinfo` when formatting IPv6 addresses in `CNetAddr::ToStringIP`. Fixes #21466. Fixes #21967. The IPv4 case was fixed in #21564. ACKs for top commit: laanwj: Code review ACK 54548ba vasild: ACK 54548ba Tree-SHA512: 8404e458b29efdb7bf78b91adc075d05e0385969d1532cccaa2c7cb69cd77411c42d95fcefc4000137b9f2076fe395731c7d9844b7d42b58a6d3bec69eed6fce
2 parents 1ef34ee + 54548ba commit 7b87fca

File tree

1 file changed

+48
-20
lines changed

1 file changed

+48
-20
lines changed

src/netaddress.cpp

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -556,20 +556,57 @@ static std::string IPv4ToString(Span<const uint8_t> a)
556556
return strprintf("%u.%u.%u.%u", a[0], a[1], a[2], a[3]);
557557
}
558558

559+
// Return an IPv6 address text representation with zero compression as described in RFC 5952
560+
// ("A Recommendation for IPv6 Address Text Representation").
559561
static std::string IPv6ToString(Span<const uint8_t> a)
560562
{
561563
assert(a.size() == ADDR_IPV6_SIZE);
562-
// clang-format off
563-
return strprintf("%x:%x:%x:%x:%x:%x:%x:%x",
564-
ReadBE16(&a[0]),
565-
ReadBE16(&a[2]),
566-
ReadBE16(&a[4]),
567-
ReadBE16(&a[6]),
568-
ReadBE16(&a[8]),
569-
ReadBE16(&a[10]),
570-
ReadBE16(&a[12]),
571-
ReadBE16(&a[14]));
572-
// clang-format on
564+
const std::array groups{
565+
ReadBE16(&a[0]),
566+
ReadBE16(&a[2]),
567+
ReadBE16(&a[4]),
568+
ReadBE16(&a[6]),
569+
ReadBE16(&a[8]),
570+
ReadBE16(&a[10]),
571+
ReadBE16(&a[12]),
572+
ReadBE16(&a[14]),
573+
};
574+
575+
// The zero compression implementation is inspired by Rust's std::net::Ipv6Addr, see
576+
// https://github.com/rust-lang/rust/blob/cc4103089f40a163f6d143f06359cba7043da29b/library/std/src/net/ip.rs#L1635-L1683
577+
struct ZeroSpan {
578+
size_t start_index{0};
579+
size_t len{0};
580+
};
581+
582+
// Find longest sequence of consecutive all-zero fields. Use first zero sequence if two or more
583+
// zero sequences of equal length are found.
584+
ZeroSpan longest, current;
585+
for (size_t i{0}; i < groups.size(); ++i) {
586+
if (groups[i] != 0) {
587+
current = {i + 1, 0};
588+
continue;
589+
}
590+
current.len += 1;
591+
if (current.len > longest.len) {
592+
longest = current;
593+
}
594+
}
595+
596+
std::string r;
597+
r.reserve(39);
598+
for (size_t i{0}; i < groups.size(); ++i) {
599+
// Replace the longest sequence of consecutive all-zero fields with two colons ("::").
600+
if (longest.len >= 2 && i >= longest.start_index && i < longest.start_index + longest.len) {
601+
if (i == longest.start_index) {
602+
r += "::";
603+
}
604+
continue;
605+
}
606+
r += strprintf("%s%x", ((!r.empty() && r.back() != ':') ? ":" : ""), groups[i]);
607+
}
608+
609+
return r;
573610
}
574611

575612
std::string CNetAddr::ToStringIP() const
@@ -578,15 +615,6 @@ std::string CNetAddr::ToStringIP() const
578615
case NET_IPV4:
579616
return IPv4ToString(m_addr);
580617
case NET_IPV6: {
581-
CService serv(*this, 0);
582-
struct sockaddr_storage sockaddr;
583-
socklen_t socklen = sizeof(sockaddr);
584-
if (serv.GetSockAddr((struct sockaddr*)&sockaddr, &socklen)) {
585-
char name[1025] = "";
586-
if (!getnameinfo((const struct sockaddr*)&sockaddr, socklen, name,
587-
sizeof(name), nullptr, 0, NI_NUMERICHOST))
588-
return std::string(name);
589-
}
590618
return IPv6ToString(m_addr);
591619
}
592620
case NET_ONION:

0 commit comments

Comments
 (0)