From ed5abe1c64009b5ca599996cfa172e6ba4c5c180 Mon Sep 17 00:00:00 2001 From: furszy Date: Sun, 6 Jun 2021 12:36:36 -0300 Subject: [PATCH] Net: Proper CService deserialization + GetIn6Addr return false if addr isn't an IPv6 addr --- src/netaddress.cpp | 15 ++++++++++++++- src/netaddress.h | 6 +++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/netaddress.cpp b/src/netaddress.cpp index 07e1be2473636..86e2083b5ab0b 100644 --- a/src/netaddress.cpp +++ b/src/netaddress.cpp @@ -318,7 +318,7 @@ bool operator==(const CNetAddr& a, const CNetAddr& b) bool operator!=(const CNetAddr& a, const CNetAddr& b) { - return (memcmp(a.ip, b.ip, 16) != 0); + return a.m_net != b.m_net || (memcmp(a.ip, b.ip, 16) != 0); } bool operator<(const CNetAddr& a, const CNetAddr& b) @@ -334,8 +334,21 @@ bool CNetAddr::GetInAddr(struct in_addr* pipv4Addr) const return true; } +/** + * Try to get our IPv6 address. + * + * @param[out] pipv6Addr The in6_addr struct to which to copy. + * + * @returns Whether or not the operation was successful, in particular, whether + * or not our address was an IPv6 address. + * + * @see CNetAddr::IsIPv6() + */ bool CNetAddr::GetIn6Addr(struct in6_addr* pipv6Addr) const { + if (!IsIPv6()) { + return false; + } memcpy(pipv6Addr, ip, 16); return true; } diff --git a/src/netaddress.h b/src/netaddress.h index a24c21577b017..93ee4f744e305 100644 --- a/src/netaddress.h +++ b/src/netaddress.h @@ -223,7 +223,11 @@ class CService : public CNetAddr CService(const struct in6_addr& ipv6Addr, uint16_t port); 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)); + } }; #endif // PIVX_NETADDRESS_H