From 4cb98fc3f329673f0de0acaf55418bdcf2b75419 Mon Sep 17 00:00:00 2001 From: DeckerSU Date: Mon, 7 Feb 2022 09:32:49 +0300 Subject: [PATCH] net, test: CNetAddr scoped ipv6 test coverage, rename scopeId to m_scope_id backport of https://github.com/bitcoin/bitcoin/pull/19951 to our codebase. --- src/netbase.cpp | 4 ++-- src/netbase.h | 6 +++++- src/test-komodo/test_net_tests.cpp | 30 +++++++++++++++++++++++++++++- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/netbase.cpp b/src/netbase.cpp index 67986524..40c5d952 100644 --- a/src/netbase.cpp +++ b/src/netbase.cpp @@ -714,7 +714,7 @@ CNetAddr::CNetAddr(const struct in_addr& ipv4Addr) CNetAddr::CNetAddr(const struct in6_addr& ipv6Addr, const uint32_t scope) { SetRaw(NET_IPV6, (const uint8_t*)&ipv6Addr); - scopeId = scope; + m_scope_id = scope; } CNetAddr::CNetAddr(const char *pszIp, bool fAllowLookup) @@ -1308,7 +1308,7 @@ bool CService::GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const memset(paddrin6, 0, *addrlen); if (!GetIn6Addr(&paddrin6->sin6_addr)) return false; - paddrin6->sin6_scope_id = scopeId; + paddrin6->sin6_scope_id = m_scope_id; paddrin6->sin6_family = AF_INET6; paddrin6->sin6_port = htons(port); return true; diff --git a/src/netbase.h b/src/netbase.h index f0d3df0a..27037b07 100644 --- a/src/netbase.h +++ b/src/netbase.h @@ -59,7 +59,11 @@ class CNetAddr { protected: unsigned char ip[16]; // in network byte order - 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: CNetAddr(); diff --git a/src/test-komodo/test_net_tests.cpp b/src/test-komodo/test_net_tests.cpp index 58161fe1..77520b2e 100644 --- a/src/test-komodo/test_net_tests.cpp +++ b/src/test-komodo/test_net_tests.cpp @@ -4,6 +4,7 @@ #include #include +#include namespace TestNetTests { @@ -17,7 +18,7 @@ namespace TestNetTests { class CChildCService : public CService { public: explicit CChildCService(const CService &ipIn) : CService(ipIn) {}; - uint32_t GetScopeId() { return scopeId; } + uint32_t GetScopeId() { return m_scope_id; } }; // https://github.com/bitcoin/bitcoin/pull/14728 - fix uninitialized read when stringifying an addrLocal @@ -80,4 +81,31 @@ namespace TestNetTests { ASSERT_TRUE(1); } + + TEST(TestNetTests, cnetaddr_basic_scoped_link_local) { + + CNetAddr addr; + std::vector vIP; + // 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"}; + ASSERT_TRUE(LookupHost(scoped_addr.c_str(), vIP, false)); + addr = vIP[0]; + ASSERT_TRUE(addr.IsValid()); + ASSERT_TRUE(addr.IsIPv6()); + //EXPECT_TRUE(!addr.IsBindAny()); + const std::string addr_str{addr.ToString()}; + EXPECT_TRUE(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. + const std::string link_local_zone{link_local + "%0"}; + ASSERT_TRUE(LookupHost(link_local_zone.c_str(), vIP, false)); + addr = vIP[0]; + ASSERT_TRUE(addr.IsValid()); + ASSERT_TRUE(addr.IsIPv6()); + //EXPECT_TRUE(!addr.IsBindAny()); + EXPECT_EQ(addr.ToString(), link_local); + } } \ No newline at end of file