merge bitcoin#19951: CNetAddr scoped ipv6 test coverage, rename scopeId to m_scope_id

This commit is contained in:
Kittywhiskers Van Gogh 2020-09-13 11:04:50 +02:00 committed by PastaPastaPasta
parent aa76506bc9
commit 546f7557d8
3 changed files with 27 additions and 4 deletions

View File

@ -323,7 +323,7 @@ CNetAddr::CNetAddr(const struct in_addr& ipv4Addr)
CNetAddr::CNetAddr(const struct in6_addr& ipv6Addr, const uint32_t scope) CNetAddr::CNetAddr(const struct in6_addr& ipv6Addr, const uint32_t scope)
{ {
SetLegacyIPv6(Span<const uint8_t>(reinterpret_cast<const uint8_t*>(&ipv6Addr), sizeof(ipv6Addr))); SetLegacyIPv6(Span<const uint8_t>(reinterpret_cast<const uint8_t*>(&ipv6Addr), sizeof(ipv6Addr)));
scopeId = scope; m_scope_id = scope;
} }
bool CNetAddr::IsBindAny() const bool CNetAddr::IsBindAny() const
@ -1002,7 +1002,7 @@ bool CService::GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const
memset(paddrin6, 0, *addrlen); memset(paddrin6, 0, *addrlen);
if (!GetIn6Addr(&paddrin6->sin6_addr)) if (!GetIn6Addr(&paddrin6->sin6_addr))
return false; return false;
paddrin6->sin6_scope_id = scopeId; paddrin6->sin6_scope_id = m_scope_id;
paddrin6->sin6_family = AF_INET6; paddrin6->sin6_family = AF_INET6;
paddrin6->sin6_port = htons(port); paddrin6->sin6_port = htons(port);
return true; return true;

View File

@ -134,7 +134,11 @@ class CNetAddr
*/ */
Network m_net{NET_IPV6}; Network m_net{NET_IPV6};
uint32_t scopeId{0}; // for scoped/link-local ipv6 addresses /**
* Scope id if scoped/link-local IPV6 address.
* See https://tools.ietf.org/html/rfc4007
*/
uint32_t m_scope_id{0};
public: public:
CNetAddr(); CNetAddr();
@ -433,7 +437,7 @@ class CNetAddr
"Address too long: %u > %u", address_size, MAX_ADDRV2_SIZE)); "Address too long: %u > %u", address_size, MAX_ADDRV2_SIZE));
} }
scopeId = 0; m_scope_id = 0;
if (SetNetFromBIP155Network(bip155_net, address_size)) { if (SetNetFromBIP155Network(bip155_net, address_size)) {
m_addr.resize(address_size); m_addr.resize(address_size);

View File

@ -262,6 +262,25 @@ BOOST_AUTO_TEST_CASE(cnetaddr_basic)
BOOST_CHECK(addr.IsAddrV1Compatible()); BOOST_CHECK(addr.IsAddrV1Compatible());
BOOST_CHECK_EQUAL(addr.ToString(), "1122:3344:5566:7788:9900:aabb:ccdd:eeff"); BOOST_CHECK_EQUAL(addr.ToString(), "1122:3344:5566:7788:9900:aabb:ccdd:eeff");
// IPv6, scoped/link-local. See https://tools.ietf.org/html/rfc4007
// We support non-negative decimal integers (uint32_t) as zone id indices.
// Test with a fairly-high value, e.g. 32, to avoid locally reserved ids.
const std::string link_local{"fe80::1"};
const std::string scoped_addr{link_local + "%32"};
BOOST_REQUIRE(LookupHost(scoped_addr, addr, false));
BOOST_REQUIRE(addr.IsValid());
BOOST_REQUIRE(addr.IsIPv6());
BOOST_CHECK(!addr.IsBindAny());
const std::string addr_str{addr.ToString()};
BOOST_CHECK(addr_str == scoped_addr || addr_str == "fe80:0:0:0:0:0:0:1");
// The fallback case "fe80:0:0:0:0:0:0:1" is needed for macOS 10.14/10.15 and (probably) later.
// Test that the delimiter "%" and default zone id of 0 can be omitted for the default scope.
BOOST_REQUIRE(LookupHost(link_local + "%0", addr, false));
BOOST_REQUIRE(addr.IsValid());
BOOST_REQUIRE(addr.IsIPv6());
BOOST_CHECK(!addr.IsBindAny());
BOOST_CHECK_EQUAL(addr.ToString(), link_local);
// TORv2 // TORv2
BOOST_REQUIRE(addr.SetSpecial("6hzph5hv6337r6p2.onion")); BOOST_REQUIRE(addr.SetSpecial("6hzph5hv6337r6p2.onion"));
BOOST_REQUIRE(addr.IsValid()); BOOST_REQUIRE(addr.IsValid());