Skip to content

Commit 2fc111b

Browse files
committed
Merge bitcoin/bitcoin#21985: net: Return IPv6 scope id in CNetAddr::ToStringIP()
6c280ad net: Return IPv6 scope id in `CNetAddr::ToStringIP()` (W. J. van der Laan) Pull request description: If a scope id is provided, return it back in the string representation. Also bring back the test (now in platform independent fashion). Closes #21982. Includes #21961 (apart from the MacOS remark). ACKs for top commit: practicalswift: cr ACK 6c280ad Tree-SHA512: 77792c35679b6c3545fd3a8d3d74c4f515ac2ee9f02d983251aeaaac715d55c122bbb0141abbeac272011f15520b439bd2db4ec8541a58df9b366921d212ca5f
2 parents 2fa3f30 + 6c280ad commit 2fc111b

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/netaddress.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ static std::string IPv4ToString(Span<const uint8_t> a)
558558

559559
// Return an IPv6 address text representation with zero compression as described in RFC 5952
560560
// ("A Recommendation for IPv6 Address Text Representation").
561-
static std::string IPv6ToString(Span<const uint8_t> a)
561+
static std::string IPv6ToString(Span<const uint8_t> a, uint32_t scope_id)
562562
{
563563
assert(a.size() == ADDR_IPV6_SIZE);
564564
const std::array groups{
@@ -606,6 +606,10 @@ static std::string IPv6ToString(Span<const uint8_t> a)
606606
r += strprintf("%s%x", ((!r.empty() && r.back() != ':') ? ":" : ""), groups[i]);
607607
}
608608

609+
if (scope_id != 0) {
610+
r += strprintf("%%%u", scope_id);
611+
}
612+
609613
return r;
610614
}
611615

@@ -615,7 +619,7 @@ std::string CNetAddr::ToStringIP() const
615619
case NET_IPV4:
616620
return IPv4ToString(m_addr);
617621
case NET_IPV6: {
618-
return IPv6ToString(m_addr);
622+
return IPv6ToString(m_addr, m_scope_id);
619623
}
620624
case NET_ONION:
621625
switch (m_addr.size()) {
@@ -639,7 +643,7 @@ std::string CNetAddr::ToStringIP() const
639643
case NET_I2P:
640644
return EncodeBase32(m_addr, false /* don't pad with = */) + ".b32.i2p";
641645
case NET_CJDNS:
642-
return IPv6ToString(m_addr);
646+
return IPv6ToString(m_addr, 0);
643647
case NET_INTERNAL:
644648
return EncodeBase32(m_addr) + ".internal";
645649
case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE

src/test/net_tests.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,13 +300,17 @@ BOOST_AUTO_TEST_CASE(cnetaddr_basic)
300300

301301
// IPv6, scoped/link-local. See https://tools.ietf.org/html/rfc4007
302302
// We support non-negative decimal integers (uint32_t) as zone id indices.
303-
// Test with a fairly-high value, e.g. 32, to avoid locally reserved ids.
303+
// Normal link-local scoped address functionality is to append "%" plus the
304+
// zone id, for example, given a link-local address of "fe80::1" and a zone
305+
// id of "32", return the address as "fe80::1%32".
304306
const std::string link_local{"fe80::1"};
305307
const std::string scoped_addr{link_local + "%32"};
306308
BOOST_REQUIRE(LookupHost(scoped_addr, addr, false));
307309
BOOST_REQUIRE(addr.IsValid());
308310
BOOST_REQUIRE(addr.IsIPv6());
309311
BOOST_CHECK(!addr.IsBindAny());
312+
BOOST_CHECK_EQUAL(addr.ToString(), scoped_addr);
313+
310314
// Test that the delimiter "%" and default zone id of 0 can be omitted for the default scope.
311315
BOOST_REQUIRE(LookupHost(link_local + "%0", addr, false));
312316
BOOST_REQUIRE(addr.IsValid());

0 commit comments

Comments
 (0)