From bc74a40a56128f81f11151d5966f53b82f19038c Mon Sep 17 00:00:00 2001 From: Vasil Dimov Date: Wed, 13 May 2020 18:45:39 +0200 Subject: [PATCH] net: improve encapsulation of CNetAddr Do not access `CNetAddr::ip` directly from `CService` methods. This improvement will help later when we change the type of `CNetAddr::ip` (in the BIP155 implementation). Co-authored-by: Carl Dong --- src/netaddress.cpp | 10 ++++------ src/netaddress.h | 6 +++++- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/netaddress.cpp b/src/netaddress.cpp index 6744391616e..0aaba440b85 100644 --- a/src/netaddress.cpp +++ b/src/netaddress.cpp @@ -726,12 +726,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; // most significant byte of our port - vKey[17] = port & 0x0FF; // least significant byte of our port - 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 c201012154b..f2daad7fb69 100644 --- a/src/netaddress.h +++ b/src/netaddress.h @@ -160,7 +160,11 @@ class CService : public CNetAddr CService(const struct in6_addr& ipv6Addr, uint16_t port); explicit CService(const struct sockaddr_in6& addr); - SERIALIZE_METHODS(CService, obj) { READWRITE(obj.ip, Using>(obj.port)); } + SERIALIZE_METHODS(CService, obj) + { + READWRITEAS(CNetAddr, obj); + READWRITE(Using>(obj.port)); + } }; bool SanityCheckASMap(const std::vector& asmap);