Skip to content

Commit 3864219

Browse files
committed
Merge #19360: net: improve encapsulation of CNetAddr
bc74a40 net: improve encapsulation of CNetAddr (Vasil Dimov) Pull request description: 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). (chopped off from bitcoin/bitcoin#19031 to ease review) ACKs for top commit: dongcarl: ACK bc74a40 naumenkogs: ACK bc74a40 fjahr: Code review ACK bc74a40 laanwj: code review ACK bc74a40 jonatack: ACK bc74a40 jnewbery: ACK bc74a40 Tree-SHA512: 29a203905538e8311e3249b78565abe69ce36dc4ec239bec85c726c30e1a7b55b0aaf5c6659b676935008e068cfa53d716f7a598469064108daf130f94329a5d
2 parents 31bdd86 + bc74a40 commit 3864219

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/netaddress.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -726,12 +726,10 @@ bool CService::GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const
726726
*/
727727
std::vector<unsigned char> CService::GetKey() const
728728
{
729-
std::vector<unsigned char> vKey;
730-
vKey.resize(18);
731-
memcpy(vKey.data(), ip, 16);
732-
vKey[16] = port / 0x100; // most significant byte of our port
733-
vKey[17] = port & 0x0FF; // least significant byte of our port
734-
return vKey;
729+
auto key = GetAddrBytes();
730+
key.push_back(port / 0x100); // most significant byte of our port
731+
key.push_back(port & 0x0FF); // least significant byte of our port
732+
return key;
735733
}
736734

737735
std::string CService::ToStringPort() const

src/netaddress.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,11 @@ class CService : public CNetAddr
160160
CService(const struct in6_addr& ipv6Addr, uint16_t port);
161161
explicit CService(const struct sockaddr_in6& addr);
162162

163-
SERIALIZE_METHODS(CService, obj) { READWRITE(obj.ip, Using<BigEndianFormatter<2>>(obj.port)); }
163+
SERIALIZE_METHODS(CService, obj)
164+
{
165+
READWRITEAS(CNetAddr, obj);
166+
READWRITE(Using<BigEndianFormatter<2>>(obj.port));
167+
}
164168
};
165169

166170
bool SanityCheckASMap(const std::vector<bool>& asmap);

0 commit comments

Comments
 (0)