Changeset View
Changeset View
Standalone View
Standalone View
src/netaddress.h
| Show All 20 Lines | |||||
| #include <array> | #include <array> | ||||
| #include <cstdint> | #include <cstdint> | ||||
| #include <ios> | #include <ios> | ||||
| #include <string> | #include <string> | ||||
| #include <vector> | #include <vector> | ||||
| /** | /** | ||||
| * A flag that is ORed into the protocol version to designate that addresses | |||||
| * should be serialized in (unserialized from) v2 format (BIP155). | |||||
| * Make sure that this does not collide with any of the values in `version.h`. | |||||
| */ | |||||
| static constexpr int ADDRV2_FORMAT = 0x20000000; | |||||
| /** | |||||
| * A network type. | * A network type. | ||||
| * @note An address may belong to more than one network, for example `10.0.0.1` | * @note An address may belong to more than one network, for example `10.0.0.1` | ||||
| * belongs to both `NET_UNROUTABLE` and `NET_IPV4`. | * belongs to both `NET_UNROUTABLE` and `NET_IPV4`. | ||||
| * Keep these sequential starting from 0 and `NET_MAX` as the last entry. | * Keep these sequential starting from 0 and `NET_MAX` as the last entry. | ||||
| * We have loops like `for (int i = 0; i < NET_MAX; ++i)` that expect to iterate | * We have loops like `for (int i = 0; i < NET_MAX; ++i)` that expect to iterate | ||||
| * over all enum values and also `GetExtNetwork()` "extends" this enum by | * over all enum values and also `GetExtNetwork()` "extends" this enum by | ||||
| * introducing standalone constants starting from `NET_MAX`. | * introducing standalone constants starting from `NET_MAX`. | ||||
| */ | */ | ||||
| ▲ Show 20 Lines • Show All 202 Lines • ▼ Show 20 Lines | public: | ||||
| friend bool operator<(const CNetAddr &a, const CNetAddr &b); | friend bool operator<(const CNetAddr &a, const CNetAddr &b); | ||||
| /** | /** | ||||
| * Whether this address should be relayed to other peers even if we can't | * Whether this address should be relayed to other peers even if we can't | ||||
| * reach it ourselves. | * reach it ourselves. | ||||
| */ | */ | ||||
| bool IsRelayable() const { return IsIPv4() || IsIPv6() || IsTor(); } | bool IsRelayable() const { return IsIPv4() || IsIPv6() || IsTor(); } | ||||
| enum class Encoding { | |||||
| V1, | |||||
| //! BIP155 encoding | |||||
| V2, | |||||
| }; | |||||
| struct SerParams { | |||||
| const Encoding enc; | |||||
| }; | |||||
| static constexpr SerParams V1{Encoding::V1}; | |||||
| static constexpr SerParams V2{Encoding::V2}; | |||||
| /** | /** | ||||
| * Serialize to a stream. | * Serialize to a stream. | ||||
| */ | */ | ||||
| template <typename Stream> void Serialize(Stream &s) const { | template <typename Stream> void Serialize(Stream &s) const { | ||||
| if (s.GetVersion() & ADDRV2_FORMAT) { | if (s.GetParams().enc == Encoding::V2) { | ||||
| SerializeV2Stream(s); | SerializeV2Stream(s); | ||||
| } else { | } else { | ||||
| SerializeV1Stream(s); | SerializeV1Stream(s); | ||||
| } | } | ||||
| } | } | ||||
| /** | /** | ||||
| * Unserialize from a stream. | * Unserialize from a stream. | ||||
| */ | */ | ||||
| template <typename Stream> void Unserialize(Stream &s) { | template <typename Stream> void Unserialize(Stream &s) { | ||||
| if (s.GetVersion() & ADDRV2_FORMAT) { | if (s.GetParams().enc == Encoding::V2) { | ||||
| UnserializeV2Stream(s); | UnserializeV2Stream(s); | ||||
| } else { | } else { | ||||
| UnserializeV1Stream(s); | UnserializeV1Stream(s); | ||||
| } | } | ||||
| } | } | ||||
| friend class CSubNet; | friend class CSubNet; | ||||
| ▲ Show 20 Lines • Show All 348 Lines • Show Last 20 Lines | |||||