Skip to content

Commit

Permalink
net, test: CNetAddr scoped ipv6 test coverage, rename scopeId to m_sc…
Browse files Browse the repository at this point in the history
…ope_id

backport of bitcoin/bitcoin#19951 to our
codebase.
  • Loading branch information
DeckerSU committed Feb 7, 2022
1 parent 59d6a49 commit 4cb98fc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/netbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion src/netbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
30 changes: 29 additions & 1 deletion src/test-komodo/test_net_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <memory>
#include <utility>
#include <vector>

namespace TestNetTests {

Expand All @@ -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
Expand Down Expand Up @@ -80,4 +81,31 @@ namespace TestNetTests {
ASSERT_TRUE(1);

}

TEST(TestNetTests, cnetaddr_basic_scoped_link_local) {

CNetAddr addr;
std::vector<CNetAddr> 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);
}
}

0 comments on commit 4cb98fc

Please sign in to comment.