From 3337219ca8711fd2248e06e14df48c2aec87acc2 Mon Sep 17 00:00:00 2001 From: furszy Date: Mon, 7 Jun 2021 00:49:46 -0300 Subject: [PATCH] net: improve encapsulation of CNetAddr. This improvement will help later when we change the type of `CNetAddr::ip` (in the BIP155 implementation). Adaptation of btc@bc74a40a56128f81f11151d5966f53b82f19038c --- src/netaddress.cpp | 10 ++++------ src/netaddress.h | 1 + 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/netaddress.cpp b/src/netaddress.cpp index 7c0628cafdcb8..771c88ce94de6 100644 --- a/src/netaddress.cpp +++ b/src/netaddress.cpp @@ -666,12 +666,10 @@ bool CService::GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const std::vector CService::GetKey() const { - std::vector vKey; - vKey.resize(18); - memcpy(vKey.data(), ip, 16); - vKey[16] = port / 0x100; - vKey[17] = port & 0x0FF; - return vKey; + auto key = GetAddrBytes(); + key.push_back(port / 0x100); // most significant byte of our port + key.push_back(port & 0x0FF); // least significant byte of our port + return key; } std::string CService::ToStringPort() const diff --git a/src/netaddress.h b/src/netaddress.h index 770a7b1ef760e..11fff346c3fb9 100644 --- a/src/netaddress.h +++ b/src/netaddress.h @@ -133,6 +133,7 @@ class CNetAddr uint32_t GetMappedAS(const std::vector &asmap) const; std::vector GetGroup(const std::vector &asmap) const; + std::vector GetAddrBytes() const { return {std::begin(ip), std::end(ip)}; } int GetReachabilityFrom(const CNetAddr* paddrPartner = nullptr) const; CNetAddr(const struct in6_addr& pipv6Addr, const uint32_t scope = 0);