Introduce BigEndian wrapper and use it for netaddress ports

This commit is contained in:
Pieter Wuille
2018-04-08 09:39:05 -07:00
parent 97785863e2
commit ece88fd269
2 changed files with 50 additions and 8 deletions

View File

@@ -141,7 +141,7 @@ class CSubNet
class CService : public CNetAddr
{
protected:
unsigned short port; // host order
uint16_t port; // host order
public:
CService();
@@ -168,13 +168,7 @@ class CService : public CNetAddr
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(ip);
// TODO: introduce native support for BE serialization in serialize.h
unsigned short portN = htons(port);
READWRITE(Span<unsigned char>((unsigned char*)&portN, 2));
if (ser_action.ForRead()) {
port = ntohs(portN);
}
READWRITE(WrapBigEndian(port));
}
};