aboutsummaryrefslogtreecommitdiff
path: root/src/netaddress.cpp
diff options
context:
space:
mode:
authorW. J. van der Laan <laanwj@protonmail.com>2021-05-17 17:01:44 +0200
committerW. J. van der Laan <laanwj@protonmail.com>2021-05-18 21:01:32 +0200
commit6c280adcd865ae3da4df53d630c9bf737283a56f (patch)
tree6bb5471e9a3f1eb17a79353eb3b0a0317d36979e /src/netaddress.cpp
parent6d1d33d33491a98bb0dbf64ea7e4743200e71474 (diff)
net: Return IPv6 scope id in `CNetAddr::ToStringIP()`
If a scope id is provided, return it back in the string representation. Also bring back the test. Closes #21982. Co-authored-by: Jon Atack <jon@atack.com>
Diffstat (limited to 'src/netaddress.cpp')
-rw-r--r--src/netaddress.cpp10
1 files changed, 7 insertions, 3 deletions
diff --git a/src/netaddress.cpp b/src/netaddress.cpp
index 112e216c09..352abae298 100644
--- a/src/netaddress.cpp
+++ b/src/netaddress.cpp
@@ -558,7 +558,7 @@ static std::string IPv4ToString(Span<const uint8_t> a)
// Return an IPv6 address text representation with zero compression as described in RFC 5952
// ("A Recommendation for IPv6 Address Text Representation").
-static std::string IPv6ToString(Span<const uint8_t> a)
+static std::string IPv6ToString(Span<const uint8_t> a, uint32_t scope_id)
{
assert(a.size() == ADDR_IPV6_SIZE);
const std::array groups{
@@ -606,6 +606,10 @@ static std::string IPv6ToString(Span<const uint8_t> a)
r += strprintf("%s%x", ((!r.empty() && r.back() != ':') ? ":" : ""), groups[i]);
}
+ if (scope_id != 0) {
+ r += strprintf("%%%u", scope_id);
+ }
+
return r;
}
@@ -615,7 +619,7 @@ std::string CNetAddr::ToStringIP() const
case NET_IPV4:
return IPv4ToString(m_addr);
case NET_IPV6: {
- return IPv6ToString(m_addr);
+ return IPv6ToString(m_addr, m_scope_id);
}
case NET_ONION:
switch (m_addr.size()) {
@@ -639,7 +643,7 @@ std::string CNetAddr::ToStringIP() const
case NET_I2P:
return EncodeBase32(m_addr, false /* don't pad with = */) + ".b32.i2p";
case NET_CJDNS:
- return IPv6ToString(m_addr);
+ return IPv6ToString(m_addr, 0);
case NET_INTERNAL:
return EncodeBase32(m_addr) + ".internal";
case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE