Skip to content

Commit

Permalink
net: improve encapsulation of CNetAddr
Browse files Browse the repository at this point in the history
Summary:
```
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).
```

Backport of core [[bitcoin/bitcoin#19360 | PR19360]].

Depends on D9169.

Test Plan:
  ninja all check-all

Reviewers: #bitcoin_abc, majcosta

Reviewed By: #bitcoin_abc, majcosta

Differential Revision: https://reviews.bitcoinabc.org/D9171
  • Loading branch information
vasild authored and Fabcien committed Feb 5, 2021
1 parent aefb3d7 commit 2184cc6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
10 changes: 4 additions & 6 deletions src/netaddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -755,14 +755,12 @@ bool CService::GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) const {
* @returns An identifier unique to this service's address and port number.
*/
std::vector<uint8_t> CService::GetKey() const {
std::vector<uint8_t> vKey;
vKey.resize(18);
memcpy(vKey.data(), ip, 16);
auto key = GetAddrBytes();
// most significant byte of our port
vKey[16] = port / 0x100;
key.push_back(port / 0x100);
// least significant byte of our port
vKey[17] = port & 0x0FF;
return vKey;
key.push_back(port & 0x0FF);
return key;
}

std::string CService::ToStringPort() const {
Expand Down
3 changes: 2 additions & 1 deletion src/netaddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ class CService : public CNetAddr {
explicit CService(const struct sockaddr_in6 &addr);

SERIALIZE_METHODS(CService, obj) {
READWRITE(obj.ip, Using<BigEndianFormatter<2>>(obj.port));
READWRITEAS(CNetAddr, obj);
READWRITE(Using<BigEndianFormatter<2>>(obj.port));
}
};

Expand Down

0 comments on commit 2184cc6

Please sign in to comment.