Changeset View
Changeset View
Standalone View
Standalone View
src/netaddress.h
Show First 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | enum Network { | ||||
NET_UNROUTABLE = 0, | NET_UNROUTABLE = 0, | ||||
/// IPv4 | /// IPv4 | ||||
NET_IPV4, | NET_IPV4, | ||||
/// IPv6 | /// IPv6 | ||||
NET_IPV6, | NET_IPV6, | ||||
/// TORv2 | /// TOR (v2 or v3) | ||||
NET_ONION, | NET_ONION, | ||||
/// I2P | |||||
NET_I2P, | |||||
/// CJDNS | |||||
NET_CJDNS, | |||||
/// A set of addresses that represent the hash of a string or FQDN. We use | /// A set of addresses that represent the hash of a string or FQDN. We use | ||||
/// them in CAddrMan to keep track of which DNS seeds were used. | /// them in CAddrMan to keep track of which DNS seeds were used. | ||||
NET_INTERNAL, | NET_INTERNAL, | ||||
/// Dummy value to indicate the number of NET_* constants. | /// Dummy value to indicate the number of NET_* constants. | ||||
NET_MAX, | NET_MAX, | ||||
}; | }; | ||||
Show All 22 Lines | |||||
static constexpr size_t ADDR_IPV4_SIZE = 4; | static constexpr size_t ADDR_IPV4_SIZE = 4; | ||||
/// Size of IPv6 address (in bytes). | /// Size of IPv6 address (in bytes). | ||||
static constexpr size_t ADDR_IPV6_SIZE = 16; | static constexpr size_t ADDR_IPV6_SIZE = 16; | ||||
/// Size of TORv2 address (in bytes). | /// Size of TORv2 address (in bytes). | ||||
static constexpr size_t ADDR_TORV2_SIZE = 10; | static constexpr size_t ADDR_TORV2_SIZE = 10; | ||||
/// Size of TORv3 address (in bytes). This is the length of just the address | |||||
/// as used in BIP155, without the checksum and the version byte. | |||||
static constexpr size_t ADDR_TORV3_SIZE = 32; | |||||
/// Size of I2P address (in bytes). | |||||
static constexpr size_t ADDR_I2P_SIZE = 32; | |||||
/// Size of CJDNS address (in bytes). | |||||
static constexpr size_t ADDR_CJDNS_SIZE = 16; | |||||
/// Size of "internal" (NET_INTERNAL) address (in bytes). | /// Size of "internal" (NET_INTERNAL) address (in bytes). | ||||
static constexpr size_t ADDR_INTERNAL_SIZE = 10; | static constexpr size_t ADDR_INTERNAL_SIZE = 10; | ||||
/** | /** | ||||
* Network address. | * Network address. | ||||
*/ | */ | ||||
class CNetAddr { | class CNetAddr { | ||||
protected: | protected: | ||||
▲ Show 20 Lines • Show All 62 Lines • ▼ Show 20 Lines | public: | ||||
// IPv6 well-known prefix for IPv4-embedded address (64:FF9B::/96) | // IPv6 well-known prefix for IPv4-embedded address (64:FF9B::/96) | ||||
bool IsRFC6052() const; | bool IsRFC6052() const; | ||||
// IPv6 IPv4-translated address (::FFFF:0:0:0/96) (actually defined in | // IPv6 IPv4-translated address (::FFFF:0:0:0/96) (actually defined in | ||||
// RFC2765) | // RFC2765) | ||||
bool IsRFC6145() const; | bool IsRFC6145() const; | ||||
// IPv6 Hurricane Electric - https://he.net (2001:0470::/36) | // IPv6 Hurricane Electric - https://he.net (2001:0470::/36) | ||||
bool IsHeNet() const; | bool IsHeNet() const; | ||||
bool IsTor() const; | bool IsTor() const; | ||||
bool IsI2P() const; | |||||
bool IsCJDNS() const; | |||||
bool IsLocal() const; | bool IsLocal() const; | ||||
bool IsRoutable() const; | bool IsRoutable() const; | ||||
bool IsInternal() const; | bool IsInternal() const; | ||||
bool IsValid() const; | bool IsValid() const; | ||||
enum Network GetNetwork() const; | enum Network GetNetwork() const; | ||||
std::string ToString() const; | std::string ToString() const; | ||||
std::string ToStringIP() const; | std::string ToStringIP() const; | ||||
uint64_t GetHash() const; | uint64_t GetHash() const; | ||||
▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Lines | |||||
private: | private: | ||||
/** | /** | ||||
* BIP155 network ids recognized by this software. | * BIP155 network ids recognized by this software. | ||||
*/ | */ | ||||
enum BIP155Network : uint8_t { | enum BIP155Network : uint8_t { | ||||
IPV4 = 1, | IPV4 = 1, | ||||
IPV6 = 2, | IPV6 = 2, | ||||
TORV2 = 3, | TORV2 = 3, | ||||
TORV3 = 4, | |||||
I2P = 5, | |||||
CJDNS = 6, | |||||
}; | }; | ||||
/** | /** | ||||
* Size of CNetAddr when serialized as ADDRv1 (pre-BIP155) (in bytes). | * Size of CNetAddr when serialized as ADDRv1 (pre-BIP155) (in bytes). | ||||
*/ | */ | ||||
static constexpr size_t V1_SERIALIZATION_SIZE = ADDR_IPV6_SIZE; | static constexpr size_t V1_SERIALIZATION_SIZE = ADDR_IPV6_SIZE; | ||||
/** | /** | ||||
Show All 12 Lines | private: | ||||
/** | /** | ||||
* Set `m_net` from the provided BIP155 network id and size after | * Set `m_net` from the provided BIP155 network id and size after | ||||
* validation. | * validation. | ||||
* @retval true the network was recognized, is valid and `m_net` was set | * @retval true the network was recognized, is valid and `m_net` was set | ||||
* @retval false not recognised (from future?) and should be silently | * @retval false not recognised (from future?) and should be silently | ||||
* ignored | * ignored | ||||
* @throws std::ios_base::failure if the network is one of the BIP155 | * @throws std::ios_base::failure if the network is one of the BIP155 | ||||
* founding networks recognized by this software (id 1..3) and has wrong | * founding networks recognized by this software (id 1..6) with wrong | ||||
* address size. | * address size. | ||||
*/ | */ | ||||
bool SetNetFromBIP155Network(uint8_t possible_bip155_net, | bool SetNetFromBIP155Network(uint8_t possible_bip155_net, | ||||
size_t address_size); | size_t address_size); | ||||
/** | /** | ||||
* Serialize in pre-ADDRv2/BIP155 format to an array. | * Serialize in pre-ADDRv2/BIP155 format to an array. | ||||
* Some addresses (e.g. TORv3) cannot be serialized in pre-BIP155 format. | * Some addresses (e.g. TORv3) cannot be serialized in pre-BIP155 format. | ||||
majcosta: this is removed in the PR | |||||
*/ | */ | ||||
void SerializeV1Array(uint8_t (&arr)[V1_SERIALIZATION_SIZE]) const { | void SerializeV1Array(uint8_t (&arr)[V1_SERIALIZATION_SIZE]) const { | ||||
size_t prefix_size; | size_t prefix_size; | ||||
switch (m_net) { | switch (m_net) { | ||||
case NET_IPV6: | case NET_IPV6: | ||||
assert(m_addr.size() == sizeof(arr)); | assert(m_addr.size() == sizeof(arr)); | ||||
memcpy(arr, m_addr.data(), m_addr.size()); | memcpy(arr, m_addr.data(), m_addr.size()); | ||||
return; | return; | ||||
case NET_IPV4: | case NET_IPV4: | ||||
prefix_size = sizeof(IPV4_IN_IPV6_PREFIX); | prefix_size = sizeof(IPV4_IN_IPV6_PREFIX); | ||||
assert(prefix_size + m_addr.size() == sizeof(arr)); | assert(prefix_size + m_addr.size() == sizeof(arr)); | ||||
memcpy(arr, IPV4_IN_IPV6_PREFIX.data(), prefix_size); | memcpy(arr, IPV4_IN_IPV6_PREFIX.data(), prefix_size); | ||||
memcpy(arr + prefix_size, m_addr.data(), m_addr.size()); | memcpy(arr + prefix_size, m_addr.data(), m_addr.size()); | ||||
return; | return; | ||||
case NET_ONION: | case NET_ONION: | ||||
if (m_addr.size() == ADDR_TORV3_SIZE) { | |||||
break; | |||||
} | |||||
prefix_size = sizeof(TORV2_IN_IPV6_PREFIX); | prefix_size = sizeof(TORV2_IN_IPV6_PREFIX); | ||||
assert(prefix_size + m_addr.size() == sizeof(arr)); | assert(prefix_size + m_addr.size() == sizeof(arr)); | ||||
memcpy(arr, TORV2_IN_IPV6_PREFIX.data(), prefix_size); | memcpy(arr, TORV2_IN_IPV6_PREFIX.data(), prefix_size); | ||||
memcpy(arr + prefix_size, m_addr.data(), m_addr.size()); | memcpy(arr + prefix_size, m_addr.data(), m_addr.size()); | ||||
return; | return; | ||||
case NET_INTERNAL: | case NET_INTERNAL: | ||||
prefix_size = sizeof(INTERNAL_IN_IPV6_PREFIX); | prefix_size = sizeof(INTERNAL_IN_IPV6_PREFIX); | ||||
assert(prefix_size + m_addr.size() == sizeof(arr)); | assert(prefix_size + m_addr.size() == sizeof(arr)); | ||||
memcpy(arr, INTERNAL_IN_IPV6_PREFIX.data(), prefix_size); | memcpy(arr, INTERNAL_IN_IPV6_PREFIX.data(), prefix_size); | ||||
memcpy(arr + prefix_size, m_addr.data(), m_addr.size()); | memcpy(arr + prefix_size, m_addr.data(), m_addr.size()); | ||||
return; | return; | ||||
case NET_I2P: | |||||
break; | |||||
case NET_CJDNS: | |||||
break; | |||||
case NET_UNROUTABLE: | case NET_UNROUTABLE: | ||||
case NET_MAX: | case NET_MAX: | ||||
assert(false); | assert(false); | ||||
} // no default case, so the compiler can warn about missing cases | } // no default case, so the compiler can warn about missing cases | ||||
assert(false); | // Serialize TORv3, I2P and CJDNS as all-zeros. | ||||
memset(arr, 0x0, V1_SERIALIZATION_SIZE); | |||||
} | } | ||||
/** | /** | ||||
* Serialize in pre-ADDRv2/BIP155 format to a stream. | * Serialize in pre-ADDRv2/BIP155 format to a stream. | ||||
* Some addresses (e.g. TORv3) cannot be serialized in pre-BIP155 format. | |||||
*/ | */ | ||||
template <typename Stream> void SerializeV1Stream(Stream &s) const { | template <typename Stream> void SerializeV1Stream(Stream &s) const { | ||||
uint8_t serialized[V1_SERIALIZATION_SIZE]; | uint8_t serialized[V1_SERIALIZATION_SIZE]; | ||||
SerializeV1Array(serialized); | SerializeV1Array(serialized); | ||||
s << serialized; | s << serialized; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 182 Lines • Show Last 20 Lines |
this is removed in the PR