2017-09-03 15:29:10 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
|
|
|
// Copyright (c) 2009-2015 The Bitcoin Core developers
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <netaddress.h>
|
|
|
|
#include <netbase.h>
|
2021-05-25 15:43:13 +02:00
|
|
|
#include <crypto/common.h>
|
|
|
|
#include <crypto/sha3.h>
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <hash.h>
|
2021-05-25 15:43:13 +02:00
|
|
|
#include <prevector.h>
|
|
|
|
#include <tinyformat.h>
|
2021-06-27 08:33:13 +02:00
|
|
|
#include <util/asmap.h>
|
|
|
|
#include <util/strencodings.h>
|
|
|
|
#include <util/string.h>
|
2017-09-03 15:29:10 +02:00
|
|
|
|
2020-08-24 21:03:31 +02:00
|
|
|
#include <algorithm>
|
|
|
|
#include <array>
|
|
|
|
#include <cstdint>
|
2021-05-25 15:43:13 +02:00
|
|
|
#include <ios>
|
2020-08-24 21:03:31 +02:00
|
|
|
#include <tuple>
|
2017-09-03 15:29:10 +02:00
|
|
|
|
2020-08-24 21:03:31 +02:00
|
|
|
constexpr size_t CNetAddr::V1_SERIALIZATION_SIZE;
|
2021-05-25 15:43:13 +02:00
|
|
|
constexpr size_t CNetAddr::MAX_ADDRV2_SIZE;
|
|
|
|
|
|
|
|
CNetAddr::BIP155Network CNetAddr::GetBIP155Network() const
|
|
|
|
{
|
|
|
|
switch (m_net) {
|
|
|
|
case NET_IPV4:
|
|
|
|
return BIP155Network::IPV4;
|
|
|
|
case NET_IPV6:
|
|
|
|
return BIP155Network::IPV6;
|
|
|
|
case NET_ONION:
|
|
|
|
switch (m_addr.size()) {
|
|
|
|
case ADDR_TORV2_SIZE:
|
|
|
|
return BIP155Network::TORV2;
|
|
|
|
case ADDR_TORV3_SIZE:
|
|
|
|
return BIP155Network::TORV3;
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
case NET_I2P:
|
|
|
|
return BIP155Network::I2P;
|
|
|
|
case NET_CJDNS:
|
|
|
|
return BIP155Network::CJDNS;
|
|
|
|
case NET_INTERNAL: // should have been handled before calling this function
|
|
|
|
case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
|
|
|
|
case NET_MAX: // m_net is never and should not be set to NET_MAX
|
|
|
|
assert(false);
|
|
|
|
} // no default case, so the compiler can warn about missing cases
|
|
|
|
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CNetAddr::SetNetFromBIP155Network(uint8_t possible_bip155_net, size_t address_size)
|
|
|
|
{
|
|
|
|
switch (possible_bip155_net) {
|
|
|
|
case BIP155Network::IPV4:
|
|
|
|
if (address_size == ADDR_IPV4_SIZE) {
|
|
|
|
m_net = NET_IPV4;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
throw std::ios_base::failure(
|
|
|
|
strprintf("BIP155 IPv4 address with length %u (should be %u)", address_size,
|
|
|
|
ADDR_IPV4_SIZE));
|
|
|
|
case BIP155Network::IPV6:
|
|
|
|
if (address_size == ADDR_IPV6_SIZE) {
|
|
|
|
m_net = NET_IPV6;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
throw std::ios_base::failure(
|
|
|
|
strprintf("BIP155 IPv6 address with length %u (should be %u)", address_size,
|
|
|
|
ADDR_IPV6_SIZE));
|
|
|
|
case BIP155Network::TORV2:
|
|
|
|
if (address_size == ADDR_TORV2_SIZE) {
|
|
|
|
m_net = NET_ONION;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
throw std::ios_base::failure(
|
|
|
|
strprintf("BIP155 TORv2 address with length %u (should be %u)", address_size,
|
|
|
|
ADDR_TORV2_SIZE));
|
|
|
|
case BIP155Network::TORV3:
|
|
|
|
if (address_size == ADDR_TORV3_SIZE) {
|
|
|
|
m_net = NET_ONION;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
throw std::ios_base::failure(
|
|
|
|
strprintf("BIP155 TORv3 address with length %u (should be %u)", address_size,
|
|
|
|
ADDR_TORV3_SIZE));
|
|
|
|
case BIP155Network::I2P:
|
|
|
|
if (address_size == ADDR_I2P_SIZE) {
|
|
|
|
m_net = NET_I2P;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
throw std::ios_base::failure(
|
|
|
|
strprintf("BIP155 I2P address with length %u (should be %u)", address_size,
|
|
|
|
ADDR_I2P_SIZE));
|
|
|
|
case BIP155Network::CJDNS:
|
|
|
|
if (address_size == ADDR_CJDNS_SIZE) {
|
|
|
|
m_net = NET_CJDNS;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
throw std::ios_base::failure(
|
|
|
|
strprintf("BIP155 CJDNS address with length %u (should be %u)", address_size,
|
|
|
|
ADDR_CJDNS_SIZE));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't throw on addresses with unknown network ids (maybe from the future).
|
|
|
|
// Instead silently drop them and have the unserialization code consume
|
|
|
|
// subsequent ones which may be known to us.
|
|
|
|
return false;
|
|
|
|
}
|
2017-06-24 12:16:41 +02:00
|
|
|
|
2017-12-20 12:45:01 +01:00
|
|
|
bool fAllowPrivateNet = DEFAULT_ALLOWPRIVATENET;
|
|
|
|
|
2020-08-24 21:03:31 +02:00
|
|
|
/**
|
|
|
|
* Construct an unspecified IPv6 network address (::/128).
|
|
|
|
*
|
|
|
|
* @note This address is considered invalid by CNetAddr::IsValid()
|
|
|
|
*/
|
|
|
|
CNetAddr::CNetAddr() {}
|
2017-09-03 15:29:10 +02:00
|
|
|
|
|
|
|
void CNetAddr::SetIP(const CNetAddr& ipIn)
|
|
|
|
{
|
2020-08-24 21:03:31 +02:00
|
|
|
// Size check.
|
|
|
|
switch (ipIn.m_net) {
|
|
|
|
case NET_IPV4:
|
|
|
|
assert(ipIn.m_addr.size() == ADDR_IPV4_SIZE);
|
|
|
|
break;
|
|
|
|
case NET_IPV6:
|
|
|
|
assert(ipIn.m_addr.size() == ADDR_IPV6_SIZE);
|
|
|
|
break;
|
|
|
|
case NET_ONION:
|
2021-05-25 15:43:13 +02:00
|
|
|
assert(ipIn.m_addr.size() == ADDR_TORV2_SIZE || ipIn.m_addr.size() == ADDR_TORV3_SIZE);
|
|
|
|
break;
|
|
|
|
case NET_I2P:
|
|
|
|
assert(ipIn.m_addr.size() == ADDR_I2P_SIZE);
|
|
|
|
break;
|
|
|
|
case NET_CJDNS:
|
|
|
|
assert(ipIn.m_addr.size() == ADDR_CJDNS_SIZE);
|
2020-08-24 21:03:31 +02:00
|
|
|
break;
|
|
|
|
case NET_INTERNAL:
|
|
|
|
assert(ipIn.m_addr.size() == ADDR_INTERNAL_SIZE);
|
|
|
|
break;
|
|
|
|
case NET_UNROUTABLE:
|
|
|
|
case NET_MAX:
|
|
|
|
assert(false);
|
|
|
|
} // no default case, so the compiler can warn about missing cases
|
|
|
|
|
2020-07-29 13:31:10 +02:00
|
|
|
m_net = ipIn.m_net;
|
2020-08-24 21:03:31 +02:00
|
|
|
m_addr = ipIn.m_addr;
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
|
|
|
|
2020-08-24 21:03:31 +02:00
|
|
|
void CNetAddr::SetLegacyIPv6(Span<const uint8_t> ipv6)
|
|
|
|
{
|
|
|
|
assert(ipv6.size() == ADDR_IPV6_SIZE);
|
|
|
|
|
|
|
|
size_t skip{0};
|
|
|
|
|
|
|
|
if (HasPrefix(ipv6, IPV4_IN_IPV6_PREFIX)) {
|
|
|
|
// IPv4-in-IPv6
|
2020-07-29 13:31:10 +02:00
|
|
|
m_net = NET_IPV4;
|
2020-08-24 21:03:31 +02:00
|
|
|
skip = sizeof(IPV4_IN_IPV6_PREFIX);
|
|
|
|
} else if (HasPrefix(ipv6, TORV2_IN_IPV6_PREFIX)) {
|
|
|
|
// TORv2-in-IPv6
|
2020-07-29 13:31:10 +02:00
|
|
|
m_net = NET_ONION;
|
2020-08-24 21:03:31 +02:00
|
|
|
skip = sizeof(TORV2_IN_IPV6_PREFIX);
|
|
|
|
} else if (HasPrefix(ipv6, INTERNAL_IN_IPV6_PREFIX)) {
|
|
|
|
// Internal-in-IPv6
|
2020-07-29 13:31:10 +02:00
|
|
|
m_net = NET_INTERNAL;
|
2020-08-24 21:03:31 +02:00
|
|
|
skip = sizeof(INTERNAL_IN_IPV6_PREFIX);
|
2020-07-29 13:31:10 +02:00
|
|
|
} else {
|
2020-08-24 21:03:31 +02:00
|
|
|
// IPv6
|
2020-07-29 13:31:10 +02:00
|
|
|
m_net = NET_IPV6;
|
|
|
|
}
|
|
|
|
|
2020-08-24 21:03:31 +02:00
|
|
|
m_addr.assign(ipv6.begin() + skip, ipv6.end());
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
|
|
|
|
2020-08-24 21:03:31 +02:00
|
|
|
/**
|
|
|
|
* Create an "internal" address that represents a name or FQDN. CAddrMan uses
|
|
|
|
* these fake addresses to keep track of which DNS seeds were used.
|
|
|
|
* @returns Whether or not the operation was successful.
|
|
|
|
* @see NET_INTERNAL, INTERNAL_IN_IPV6_PREFIX, CNetAddr::IsInternal(), CNetAddr::IsRFC4193()
|
|
|
|
*/
|
2017-06-24 12:16:41 +02:00
|
|
|
bool CNetAddr::SetInternal(const std::string &name)
|
|
|
|
{
|
|
|
|
if (name.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-07-29 13:31:10 +02:00
|
|
|
m_net = NET_INTERNAL;
|
2017-06-24 12:16:41 +02:00
|
|
|
unsigned char hash[32] = {};
|
|
|
|
CSHA256().Write((const unsigned char*)name.data(), name.size()).Finalize(hash);
|
2020-08-24 21:03:31 +02:00
|
|
|
m_addr.assign(hash, hash + ADDR_INTERNAL_SIZE);
|
2017-06-24 12:16:41 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-25 15:43:13 +02:00
|
|
|
namespace torv3 {
|
|
|
|
// https://gitweb.torproject.org/torspec.git/tree/rend-spec-v3.txt#n2135
|
|
|
|
static constexpr size_t CHECKSUM_LEN = 2;
|
|
|
|
static const unsigned char VERSION[] = {3};
|
|
|
|
static constexpr size_t TOTAL_LEN = ADDR_TORV3_SIZE + CHECKSUM_LEN + sizeof(VERSION);
|
|
|
|
|
|
|
|
static void Checksum(Span<const uint8_t> addr_pubkey, uint8_t (&checksum)[CHECKSUM_LEN])
|
|
|
|
{
|
|
|
|
// TORv3 CHECKSUM = H(".onion checksum" | PUBKEY | VERSION)[:2]
|
|
|
|
static const unsigned char prefix[] = ".onion checksum";
|
|
|
|
static constexpr size_t prefix_len = 15;
|
|
|
|
|
|
|
|
SHA3_256 hasher;
|
|
|
|
|
|
|
|
hasher.Write(MakeSpan(prefix).first(prefix_len));
|
|
|
|
hasher.Write(addr_pubkey);
|
|
|
|
hasher.Write(VERSION);
|
|
|
|
|
|
|
|
uint8_t checksum_full[SHA3_256::OUTPUT_SIZE];
|
|
|
|
|
|
|
|
hasher.Finalize(checksum_full);
|
|
|
|
|
|
|
|
memcpy(checksum, checksum_full, sizeof(checksum));
|
|
|
|
}
|
|
|
|
|
|
|
|
}; // namespace torv3
|
|
|
|
|
2020-08-24 21:03:31 +02:00
|
|
|
/**
|
2021-05-25 15:43:13 +02:00
|
|
|
* Parse a TOR address and set this object to it.
|
2020-08-24 21:03:31 +02:00
|
|
|
*
|
|
|
|
* @returns Whether or not the operation was successful.
|
|
|
|
*
|
|
|
|
* @see CNetAddr::IsTor()
|
|
|
|
*/
|
2021-05-25 15:43:13 +02:00
|
|
|
bool CNetAddr::SetSpecial(const std::string& str)
|
2017-09-03 15:29:10 +02:00
|
|
|
{
|
2021-05-25 15:43:13 +02:00
|
|
|
static const char* suffix{".onion"};
|
|
|
|
static constexpr size_t suffix_len{6};
|
|
|
|
|
|
|
|
if (!ValidAsCString(str) || str.size() <= suffix_len ||
|
|
|
|
str.substr(str.size() - suffix_len) != suffix) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool invalid;
|
|
|
|
const auto& input = DecodeBase32(str.substr(0, str.size() - suffix_len).c_str(), &invalid);
|
|
|
|
|
|
|
|
if (invalid) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (input.size()) {
|
|
|
|
case ADDR_TORV2_SIZE:
|
|
|
|
m_net = NET_ONION;
|
|
|
|
m_addr.assign(input.begin(), input.end());
|
|
|
|
return true;
|
|
|
|
case torv3::TOTAL_LEN: {
|
|
|
|
Span<const uint8_t> input_pubkey{input.data(), ADDR_TORV3_SIZE};
|
|
|
|
Span<const uint8_t> input_checksum{input.data() + ADDR_TORV3_SIZE, torv3::CHECKSUM_LEN};
|
|
|
|
Span<const uint8_t> input_version{input.data() + ADDR_TORV3_SIZE + torv3::CHECKSUM_LEN, sizeof(torv3::VERSION)};
|
|
|
|
|
2020-11-17 06:56:11 +01:00
|
|
|
if (input_version != torv3::VERSION) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-25 15:43:13 +02:00
|
|
|
uint8_t calculated_checksum[torv3::CHECKSUM_LEN];
|
|
|
|
torv3::Checksum(input_pubkey, calculated_checksum);
|
|
|
|
|
2020-11-17 06:56:11 +01:00
|
|
|
if (input_checksum != calculated_checksum) {
|
2017-09-03 15:29:10 +02:00
|
|
|
return false;
|
2020-08-24 21:03:31 +02:00
|
|
|
}
|
2021-05-25 15:43:13 +02:00
|
|
|
|
2020-07-29 13:31:10 +02:00
|
|
|
m_net = NET_ONION;
|
2021-05-25 15:43:13 +02:00
|
|
|
m_addr.assign(input_pubkey.begin(), input_pubkey.end());
|
2017-09-03 15:29:10 +02:00
|
|
|
return true;
|
|
|
|
}
|
2021-05-25 15:43:13 +02:00
|
|
|
}
|
|
|
|
|
2017-09-03 15:29:10 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CNetAddr::CNetAddr(const struct in_addr& ipv4Addr)
|
|
|
|
{
|
2020-08-24 21:03:31 +02:00
|
|
|
m_net = NET_IPV4;
|
|
|
|
const uint8_t* ptr = reinterpret_cast<const uint8_t*>(&ipv4Addr);
|
|
|
|
m_addr.assign(ptr, ptr + ADDR_IPV4_SIZE);
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
|
|
|
|
2016-04-08 14:11:33 +02:00
|
|
|
CNetAddr::CNetAddr(const struct in6_addr& ipv6Addr, const uint32_t scope)
|
2017-09-03 15:29:10 +02:00
|
|
|
{
|
2020-08-24 21:03:31 +02:00
|
|
|
SetLegacyIPv6(Span<const uint8_t>(reinterpret_cast<const uint8_t*>(&ipv6Addr), sizeof(ipv6Addr)));
|
2016-04-08 14:11:33 +02:00
|
|
|
scopeId = scope;
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
|
|
|
|
2018-11-22 10:51:39 +01:00
|
|
|
bool CNetAddr::IsBindAny() const
|
|
|
|
{
|
2020-08-24 21:03:31 +02:00
|
|
|
if (!IsIPv4() && !IsIPv6()) {
|
|
|
|
return false;
|
2018-11-22 10:51:39 +01:00
|
|
|
}
|
2020-08-24 21:03:31 +02:00
|
|
|
return std::all_of(m_addr.begin(), m_addr.end(), [](uint8_t b) { return b == 0; });
|
2018-11-22 10:51:39 +01:00
|
|
|
}
|
|
|
|
|
2020-07-29 13:31:10 +02:00
|
|
|
bool CNetAddr::IsIPv4() const { return m_net == NET_IPV4; }
|
2017-09-03 15:29:10 +02:00
|
|
|
|
2020-07-29 13:31:10 +02:00
|
|
|
bool CNetAddr::IsIPv6() const { return m_net == NET_IPV6; }
|
2017-09-03 15:29:10 +02:00
|
|
|
|
|
|
|
bool CNetAddr::IsRFC1918() const
|
|
|
|
{
|
|
|
|
return IsIPv4() && (
|
2020-08-24 21:03:31 +02:00
|
|
|
m_addr[0] == 10 ||
|
|
|
|
(m_addr[0] == 192 && m_addr[1] == 168) ||
|
|
|
|
(m_addr[0] == 172 && m_addr[1] >= 16 && m_addr[1] <= 31));
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CNetAddr::IsRFC2544() const
|
|
|
|
{
|
2020-08-24 21:03:31 +02:00
|
|
|
return IsIPv4() && m_addr[0] == 198 && (m_addr[1] == 18 || m_addr[1] == 19);
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CNetAddr::IsRFC3927() const
|
|
|
|
{
|
2020-08-24 21:03:31 +02:00
|
|
|
return IsIPv4() && HasPrefix(m_addr, std::array<uint8_t, 2>{169, 254});
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CNetAddr::IsRFC6598() const
|
|
|
|
{
|
2020-08-24 21:03:31 +02:00
|
|
|
return IsIPv4() && m_addr[0] == 100 && m_addr[1] >= 64 && m_addr[1] <= 127;
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CNetAddr::IsRFC5737() const
|
|
|
|
{
|
2020-08-24 21:03:31 +02:00
|
|
|
return IsIPv4() && (HasPrefix(m_addr, std::array<uint8_t, 3>{192, 0, 2}) ||
|
|
|
|
HasPrefix(m_addr, std::array<uint8_t, 3>{198, 51, 100}) ||
|
|
|
|
HasPrefix(m_addr, std::array<uint8_t, 3>{203, 0, 113}));
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CNetAddr::IsRFC3849() const
|
|
|
|
{
|
2020-08-24 21:03:31 +02:00
|
|
|
return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x0D, 0xB8});
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CNetAddr::IsRFC3964() const
|
|
|
|
{
|
2020-08-24 21:03:31 +02:00
|
|
|
return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 2>{0x20, 0x02});
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CNetAddr::IsRFC6052() const
|
|
|
|
{
|
2020-08-24 21:03:31 +02:00
|
|
|
return IsIPv6() &&
|
|
|
|
HasPrefix(m_addr, std::array<uint8_t, 12>{0x00, 0x64, 0xFF, 0x9B, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CNetAddr::IsRFC4380() const
|
|
|
|
{
|
2020-08-24 21:03:31 +02:00
|
|
|
return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x00, 0x00});
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CNetAddr::IsRFC4862() const
|
|
|
|
{
|
2020-08-24 21:03:31 +02:00
|
|
|
return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 8>{0xFE, 0x80, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00});
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CNetAddr::IsRFC4193() const
|
|
|
|
{
|
2020-08-24 21:03:31 +02:00
|
|
|
return IsIPv6() && (m_addr[0] & 0xFE) == 0xFC;
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CNetAddr::IsRFC6145() const
|
|
|
|
{
|
2020-08-24 21:03:31 +02:00
|
|
|
return IsIPv6() &&
|
|
|
|
HasPrefix(m_addr, std::array<uint8_t, 12>{0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00});
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CNetAddr::IsRFC4843() const
|
|
|
|
{
|
2020-08-24 21:03:31 +02:00
|
|
|
return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 3>{0x20, 0x01, 0x00}) &&
|
|
|
|
(m_addr[3] & 0xF0) == 0x10;
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
|
|
|
|
2019-06-06 12:52:10 +02:00
|
|
|
bool CNetAddr::IsRFC7343() const
|
|
|
|
{
|
2020-08-24 21:03:31 +02:00
|
|
|
return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 3>{0x20, 0x01, 0x00}) &&
|
|
|
|
(m_addr[3] & 0xF0) == 0x20;
|
2019-06-06 12:52:10 +02:00
|
|
|
}
|
|
|
|
|
2021-05-18 18:30:48 +02:00
|
|
|
bool CNetAddr::IsHeNet() const
|
|
|
|
{
|
2020-08-24 21:03:31 +02:00
|
|
|
return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x04, 0x70});
|
2021-05-18 18:30:48 +02:00
|
|
|
}
|
|
|
|
|
2021-05-25 15:43:13 +02:00
|
|
|
/**
|
|
|
|
* Check whether this object represents a TOR address.
|
|
|
|
* @see CNetAddr::SetSpecial(const std::string &)
|
|
|
|
*/
|
|
|
|
bool CNetAddr::IsTor() const { return m_net == NET_ONION; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether this object represents an I2P address.
|
|
|
|
*/
|
|
|
|
bool CNetAddr::IsI2P() const { return m_net == NET_I2P; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether this object represents a CJDNS address.
|
|
|
|
*/
|
|
|
|
bool CNetAddr::IsCJDNS() const { return m_net == NET_CJDNS; }
|
|
|
|
|
2017-09-03 15:29:10 +02:00
|
|
|
bool CNetAddr::IsLocal() const
|
|
|
|
{
|
2020-08-24 21:03:31 +02:00
|
|
|
// IPv4 loopback (127.0.0.0/8 or 0.0.0.0/8)
|
|
|
|
if (IsIPv4() && (m_addr[0] == 127 || m_addr[0] == 0)) {
|
|
|
|
return true;
|
|
|
|
}
|
2017-09-03 15:29:10 +02:00
|
|
|
|
2020-08-24 21:03:31 +02:00
|
|
|
// IPv6 loopback (::1/128)
|
|
|
|
static const unsigned char pchLocal[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
|
|
|
|
if (IsIPv6() && memcmp(m_addr.data(), pchLocal, sizeof(pchLocal)) == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
2017-09-03 15:29:10 +02:00
|
|
|
|
2019-02-06 22:42:26 +01:00
|
|
|
return false;
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CNetAddr::IsValid() const
|
|
|
|
{
|
|
|
|
// unspecified IPv6 address (::/128)
|
2016-09-02 12:32:49 +02:00
|
|
|
unsigned char ipNone6[16] = {};
|
2020-08-24 21:03:31 +02:00
|
|
|
if (IsIPv6() && memcmp(m_addr.data(), ipNone6, sizeof(ipNone6)) == 0) {
|
2017-09-03 15:29:10 +02:00
|
|
|
return false;
|
2020-08-24 21:03:31 +02:00
|
|
|
}
|
2017-09-03 15:29:10 +02:00
|
|
|
|
2020-12-15 17:51:35 +01:00
|
|
|
// CJDNS addresses always start with 0xfc
|
|
|
|
if (IsCJDNS() && (m_addr[0] != 0xFC)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-03 15:29:10 +02:00
|
|
|
// documentation IPv6 address
|
|
|
|
if (IsRFC3849())
|
|
|
|
return false;
|
|
|
|
|
2017-06-24 12:16:41 +02:00
|
|
|
if (IsInternal())
|
|
|
|
return false;
|
|
|
|
|
2020-08-24 21:03:31 +02:00
|
|
|
if (IsIPv4()) {
|
|
|
|
const uint32_t addr = ReadBE32(m_addr.data());
|
|
|
|
if (addr == INADDR_ANY || addr == INADDR_NONE) {
|
2017-09-03 15:29:10 +02:00
|
|
|
return false;
|
2020-08-24 21:03:31 +02:00
|
|
|
}
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CNetAddr::IsRoutable() const
|
|
|
|
{
|
2017-12-20 12:45:01 +01:00
|
|
|
if (!IsValid())
|
|
|
|
return false;
|
|
|
|
if (!fAllowPrivateNet && IsRFC1918())
|
|
|
|
return false;
|
2019-06-06 12:52:10 +02:00
|
|
|
return !(IsRFC2544() || IsRFC3927() || IsRFC4862() || IsRFC6598() || IsRFC5737() || (IsRFC4193() && !IsTor()) || IsRFC4843() || IsRFC7343() || IsLocal() || IsInternal());
|
2017-06-24 12:16:41 +02:00
|
|
|
}
|
|
|
|
|
2020-08-24 21:03:31 +02:00
|
|
|
/**
|
|
|
|
* @returns Whether or not this is a dummy address that represents a name.
|
|
|
|
*
|
|
|
|
* @see CNetAddr::SetInternal(const std::string &)
|
|
|
|
*/
|
2017-06-24 12:16:41 +02:00
|
|
|
bool CNetAddr::IsInternal() const
|
|
|
|
{
|
2020-07-29 13:31:10 +02:00
|
|
|
return m_net == NET_INTERNAL;
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
|
|
|
|
2021-05-29 22:24:52 +02:00
|
|
|
bool CNetAddr::IsAddrV1Compatible() const
|
|
|
|
{
|
|
|
|
switch (m_net) {
|
|
|
|
case NET_IPV4:
|
|
|
|
case NET_IPV6:
|
|
|
|
case NET_INTERNAL:
|
|
|
|
return true;
|
|
|
|
case NET_ONION:
|
|
|
|
return m_addr.size() == ADDR_TORV2_SIZE;
|
|
|
|
case NET_I2P:
|
|
|
|
case NET_CJDNS:
|
|
|
|
return false;
|
|
|
|
case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
|
|
|
|
case NET_MAX: // m_net is never and should not be set to NET_MAX
|
|
|
|
assert(false);
|
|
|
|
} // no default case, so the compiler can warn about missing cases
|
|
|
|
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
|
2017-09-03 15:29:10 +02:00
|
|
|
enum Network CNetAddr::GetNetwork() const
|
|
|
|
{
|
2017-06-24 12:16:41 +02:00
|
|
|
if (IsInternal())
|
|
|
|
return NET_INTERNAL;
|
|
|
|
|
2017-09-03 15:29:10 +02:00
|
|
|
if (!IsRoutable())
|
|
|
|
return NET_UNROUTABLE;
|
|
|
|
|
2020-07-29 13:31:10 +02:00
|
|
|
return m_net;
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
|
|
|
|
2021-04-22 12:29:20 +02:00
|
|
|
static std::string IPv4ToString(Span<const uint8_t> a)
|
|
|
|
{
|
|
|
|
return strprintf("%u.%u.%u.%u", a[0], a[1], a[2], a[3]);
|
|
|
|
}
|
|
|
|
|
2021-05-25 15:43:13 +02:00
|
|
|
static std::string IPv6ToString(Span<const uint8_t> a)
|
|
|
|
{
|
|
|
|
assert(a.size() == ADDR_IPV6_SIZE);
|
|
|
|
// clang-format off
|
|
|
|
return strprintf("%x:%x:%x:%x:%x:%x:%x:%x",
|
|
|
|
ReadBE16(&a[0]),
|
|
|
|
ReadBE16(&a[2]),
|
|
|
|
ReadBE16(&a[4]),
|
|
|
|
ReadBE16(&a[6]),
|
|
|
|
ReadBE16(&a[8]),
|
|
|
|
ReadBE16(&a[10]),
|
|
|
|
ReadBE16(&a[12]),
|
|
|
|
ReadBE16(&a[14]));
|
|
|
|
// clang-format on
|
|
|
|
}
|
|
|
|
|
2017-09-03 15:29:10 +02:00
|
|
|
std::string CNetAddr::ToStringIP(bool fUseGetnameinfo) const
|
|
|
|
{
|
2021-05-25 15:43:13 +02:00
|
|
|
switch (m_net) {
|
|
|
|
case NET_IPV4:
|
2021-04-22 12:29:20 +02:00
|
|
|
return IPv4ToString(m_addr);
|
2021-05-25 15:43:13 +02:00
|
|
|
case NET_IPV6: {
|
|
|
|
if (fUseGetnameinfo) {
|
|
|
|
CService serv(*this, 0);
|
|
|
|
struct sockaddr_storage sockaddr;
|
|
|
|
socklen_t socklen = sizeof(sockaddr);
|
|
|
|
if (serv.GetSockAddr((struct sockaddr*)&sockaddr, &socklen)) {
|
|
|
|
char name[1025] = "";
|
|
|
|
if (!getnameinfo((const struct sockaddr*)&sockaddr, socklen, name,
|
|
|
|
sizeof(name), nullptr, 0, NI_NUMERICHOST))
|
|
|
|
return std::string(name);
|
2020-08-24 21:03:31 +02:00
|
|
|
}
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
2021-05-25 15:43:13 +02:00
|
|
|
return IPv6ToString(m_addr);
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
2021-05-25 15:43:13 +02:00
|
|
|
case NET_ONION:
|
|
|
|
switch (m_addr.size()) {
|
|
|
|
case ADDR_TORV2_SIZE:
|
|
|
|
return EncodeBase32(m_addr) + ".onion";
|
|
|
|
case ADDR_TORV3_SIZE: {
|
|
|
|
|
|
|
|
uint8_t checksum[torv3::CHECKSUM_LEN];
|
|
|
|
torv3::Checksum(m_addr, checksum);
|
|
|
|
|
|
|
|
// TORv3 onion_address = base32(PUBKEY | CHECKSUM | VERSION) + ".onion"
|
|
|
|
prevector<torv3::TOTAL_LEN, uint8_t> address{m_addr.begin(), m_addr.end()};
|
|
|
|
address.insert(address.end(), checksum, checksum + torv3::CHECKSUM_LEN);
|
|
|
|
address.insert(address.end(), torv3::VERSION, torv3::VERSION + sizeof(torv3::VERSION));
|
|
|
|
|
|
|
|
return EncodeBase32(address) + ".onion";
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
case NET_I2P:
|
|
|
|
return EncodeBase32(m_addr, false /* don't pad with = */) + ".b32.i2p";
|
|
|
|
case NET_CJDNS:
|
|
|
|
return IPv6ToString(m_addr);
|
|
|
|
case NET_INTERNAL:
|
|
|
|
return EncodeBase32(m_addr) + ".internal";
|
|
|
|
case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
|
|
|
|
case NET_MAX: // m_net is never and should not be set to NET_MAX
|
|
|
|
assert(false);
|
|
|
|
} // no default case, so the compiler can warn about missing cases
|
|
|
|
|
|
|
|
assert(false);
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string CNetAddr::ToString() const
|
|
|
|
{
|
|
|
|
return ToStringIP();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const CNetAddr& a, const CNetAddr& b)
|
|
|
|
{
|
2020-08-24 21:03:31 +02:00
|
|
|
return a.m_net == b.m_net && a.m_addr == b.m_addr;
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(const CNetAddr& a, const CNetAddr& b)
|
|
|
|
{
|
2020-08-24 21:03:31 +02:00
|
|
|
return std::tie(a.m_net, a.m_addr) < std::tie(b.m_net, b.m_addr);
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CNetAddr::GetInAddr(struct in_addr* pipv4Addr) const
|
|
|
|
{
|
|
|
|
if (!IsIPv4())
|
|
|
|
return false;
|
2020-08-24 21:03:31 +02:00
|
|
|
assert(sizeof(*pipv4Addr) == m_addr.size());
|
|
|
|
memcpy(pipv4Addr, m_addr.data(), m_addr.size());
|
2017-09-03 15:29:10 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CNetAddr::GetIn6Addr(struct in6_addr* pipv6Addr) const
|
|
|
|
{
|
2018-07-31 20:47:34 +02:00
|
|
|
if (!IsIPv6()) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-08-24 21:03:31 +02:00
|
|
|
assert(sizeof(*pipv6Addr) == m_addr.size());
|
|
|
|
memcpy(pipv6Addr, m_addr.data(), m_addr.size());
|
2017-09-03 15:29:10 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-01-29 22:55:40 +01:00
|
|
|
bool CNetAddr::HasLinkedIPv4() const
|
|
|
|
{
|
|
|
|
return IsRoutable() && (IsIPv4() || IsRFC6145() || IsRFC6052() || IsRFC3964() || IsRFC4380());
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t CNetAddr::GetLinkedIPv4() const
|
|
|
|
{
|
2020-08-24 21:03:31 +02:00
|
|
|
if (IsIPv4()) {
|
|
|
|
return ReadBE32(m_addr.data());
|
|
|
|
} else if (IsRFC6052() || IsRFC6145()) {
|
|
|
|
// mapped IPv4, SIIT translated IPv4: the IPv4 address is the last 4 bytes of the address
|
|
|
|
return ReadBE32(MakeSpan(m_addr).last(ADDR_IPV4_SIZE).data());
|
2020-01-29 22:55:40 +01:00
|
|
|
} else if (IsRFC3964()) {
|
|
|
|
// 6to4 tunneled IPv4: the IPv4 address is in bytes 2-6
|
2020-08-24 21:03:31 +02:00
|
|
|
return ReadBE32(MakeSpan(m_addr).subspan(2, ADDR_IPV4_SIZE).data());
|
2020-01-29 22:55:40 +01:00
|
|
|
} else if (IsRFC4380()) {
|
|
|
|
// Teredo tunneled IPv4: the IPv4 address is in the last 4 bytes of the address, but bitflipped
|
2020-08-24 21:03:31 +02:00
|
|
|
return ~ReadBE32(MakeSpan(m_addr).last(ADDR_IPV4_SIZE).data());
|
2020-01-29 22:55:40 +01:00
|
|
|
}
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
|
2021-05-25 15:43:13 +02:00
|
|
|
uint32_t CNetAddr::GetNetClass() const
|
|
|
|
{
|
|
|
|
// Make sure that if we return NET_IPV6, then IsIPv6() is true. The callers expect that.
|
|
|
|
|
|
|
|
// Check for "internal" first because such addresses are also !IsRoutable()
|
|
|
|
// and we don't want to return NET_UNROUTABLE in that case.
|
2021-05-11 17:11:07 +02:00
|
|
|
if (IsInternal()) {
|
2021-05-25 15:43:13 +02:00
|
|
|
return NET_INTERNAL;
|
|
|
|
}
|
|
|
|
if (!IsRoutable()) {
|
|
|
|
return NET_UNROUTABLE;
|
2021-05-11 17:11:07 +02:00
|
|
|
}
|
2021-05-25 15:43:13 +02:00
|
|
|
if (HasLinkedIPv4()) {
|
|
|
|
return NET_IPV4;
|
|
|
|
}
|
|
|
|
return m_net;
|
2021-05-11 17:11:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t CNetAddr::GetMappedAS(const std::vector<bool> &asmap) const {
|
|
|
|
uint32_t net_class = GetNetClass();
|
|
|
|
if (asmap.size() == 0 || (net_class != NET_IPV4 && net_class != NET_IPV6)) {
|
|
|
|
return 0; // Indicates not found, safe because AS0 is reserved per RFC7607.
|
|
|
|
}
|
|
|
|
std::vector<bool> ip_bits(128);
|
2020-01-29 22:55:40 +01:00
|
|
|
if (HasLinkedIPv4()) {
|
2020-08-24 21:03:31 +02:00
|
|
|
// For lookup, treat as if it was just an IPv4 address (IPV4_IN_IPV6_PREFIX + IPv4 bits)
|
2020-01-29 22:55:40 +01:00
|
|
|
for (int8_t byte_i = 0; byte_i < 12; ++byte_i) {
|
|
|
|
for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
|
2020-08-24 21:03:31 +02:00
|
|
|
ip_bits[byte_i * 8 + bit_i] = (IPV4_IN_IPV6_PREFIX[byte_i] >> (7 - bit_i)) & 1;
|
2020-01-29 22:55:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
uint32_t ipv4 = GetLinkedIPv4();
|
|
|
|
for (int i = 0; i < 32; ++i) {
|
|
|
|
ip_bits[96 + i] = (ipv4 >> (31 - i)) & 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Use all 128 bits of the IPv6 address otherwise
|
2020-08-24 21:03:31 +02:00
|
|
|
assert(IsIPv6());
|
2020-01-29 22:55:40 +01:00
|
|
|
for (int8_t byte_i = 0; byte_i < 16; ++byte_i) {
|
2020-08-24 21:03:31 +02:00
|
|
|
uint8_t cur_byte = m_addr[byte_i];
|
2020-01-29 22:55:40 +01:00
|
|
|
for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
|
|
|
|
ip_bits[byte_i * 8 + bit_i] = (cur_byte >> (7 - bit_i)) & 1;
|
|
|
|
}
|
2021-05-11 17:11:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
uint32_t mapped_as = Interpret(asmap, ip_bits);
|
|
|
|
return mapped_as;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the canonical identifier of our network group
|
|
|
|
*
|
|
|
|
* The groups are assigned in a way where it should be costly for an attacker to
|
|
|
|
* obtain addresses with many different group identifiers, even if it is cheap
|
|
|
|
* to obtain addresses with the same identifier.
|
|
|
|
*
|
|
|
|
* @note No two connections will be attempted to addresses with the same network
|
|
|
|
* group.
|
|
|
|
*/
|
|
|
|
std::vector<unsigned char> CNetAddr::GetGroup(const std::vector<bool> &asmap) const
|
2017-09-03 15:29:10 +02:00
|
|
|
{
|
|
|
|
std::vector<unsigned char> vchRet;
|
2021-05-11 17:11:07 +02:00
|
|
|
uint32_t net_class = GetNetClass();
|
|
|
|
// If non-empty asmap is supplied and the address is IPv4/IPv6,
|
|
|
|
// return ASN to be used for bucketing.
|
|
|
|
uint32_t asn = GetMappedAS(asmap);
|
|
|
|
if (asn != 0) { // Either asmap was empty, or address has non-asmappable net class (e.g. TOR).
|
|
|
|
vchRet.push_back(NET_IPV6); // IPv4 and IPv6 with same ASN should be in the same bucket
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
vchRet.push_back((asn >> (8 * i)) & 0xFF);
|
|
|
|
}
|
|
|
|
return vchRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
vchRet.push_back(net_class);
|
2020-08-24 21:03:31 +02:00
|
|
|
int nBits{0};
|
2017-09-03 15:29:10 +02:00
|
|
|
|
2020-01-29 22:55:40 +01:00
|
|
|
if (IsLocal()) {
|
|
|
|
// all local addresses belong to the same group
|
|
|
|
} else if (IsInternal()) {
|
|
|
|
// all internal-usage addresses get their own group
|
2020-08-24 21:03:31 +02:00
|
|
|
nBits = ADDR_INTERNAL_SIZE * 8;
|
2020-01-29 22:55:40 +01:00
|
|
|
} else if (!IsRoutable()) {
|
|
|
|
// all other unroutable addresses belong to the same group
|
|
|
|
} else if (HasLinkedIPv4()) {
|
|
|
|
// IPv4 addresses (and mapped IPv4 addresses) use /16 groups
|
|
|
|
uint32_t ipv4 = GetLinkedIPv4();
|
|
|
|
vchRet.push_back((ipv4 >> 24) & 0xFF);
|
|
|
|
vchRet.push_back((ipv4 >> 16) & 0xFF);
|
2017-09-03 15:29:10 +02:00
|
|
|
return vchRet;
|
2021-05-25 15:43:13 +02:00
|
|
|
} else if (IsTor() || IsI2P() || IsCJDNS()) {
|
2017-09-03 15:29:10 +02:00
|
|
|
nBits = 4;
|
2021-05-18 18:30:48 +02:00
|
|
|
} else if (IsHeNet()) {
|
|
|
|
// for he.net, use /36 groups
|
2017-09-03 15:29:10 +02:00
|
|
|
nBits = 36;
|
2020-01-29 22:55:40 +01:00
|
|
|
} else {
|
|
|
|
// for the rest of the IPv6 network, use /32 groups
|
2017-09-03 15:29:10 +02:00
|
|
|
nBits = 32;
|
2020-01-29 22:55:40 +01:00
|
|
|
}
|
2017-09-03 15:29:10 +02:00
|
|
|
|
2020-08-24 21:03:31 +02:00
|
|
|
// Push our address onto vchRet.
|
|
|
|
const size_t num_bytes = nBits / 8;
|
|
|
|
vchRet.insert(vchRet.end(), m_addr.begin(), m_addr.begin() + num_bytes);
|
|
|
|
nBits %= 8;
|
|
|
|
// ...for the last byte, push nBits and for the rest of the byte push 1's
|
|
|
|
if (nBits > 0) {
|
|
|
|
assert(num_bytes < m_addr.size());
|
|
|
|
vchRet.push_back(m_addr[num_bytes] | ((1 << (8 - nBits)) - 1));
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return vchRet;
|
|
|
|
}
|
|
|
|
|
2020-08-24 21:03:31 +02:00
|
|
|
std::vector<unsigned char> CNetAddr::GetAddrBytes() const
|
|
|
|
{
|
2021-05-29 22:24:52 +02:00
|
|
|
if (IsAddrV1Compatible()) {
|
|
|
|
uint8_t serialized[V1_SERIALIZATION_SIZE];
|
|
|
|
SerializeV1Array(serialized);
|
|
|
|
return {std::begin(serialized), std::end(serialized)};
|
|
|
|
}
|
|
|
|
return std::vector<unsigned char>(m_addr.begin(), m_addr.end());
|
2020-08-24 21:03:31 +02:00
|
|
|
}
|
|
|
|
|
2017-09-03 15:29:10 +02:00
|
|
|
uint64_t CNetAddr::GetHash() const
|
|
|
|
{
|
2020-08-07 19:55:51 +02:00
|
|
|
uint256 hash = Hash(m_addr.begin(), m_addr.end());
|
2017-09-03 15:29:10 +02:00
|
|
|
uint64_t nRet;
|
|
|
|
memcpy(&nRet, &hash, sizeof(nRet));
|
|
|
|
return nRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
// private extensions to enum Network, only returned by GetExtNetwork,
|
|
|
|
// and only used in GetReachabilityFrom
|
|
|
|
static const int NET_UNKNOWN = NET_MAX + 0;
|
|
|
|
static const int NET_TEREDO = NET_MAX + 1;
|
|
|
|
int static GetExtNetwork(const CNetAddr *addr)
|
|
|
|
{
|
2019-08-06 05:08:33 +02:00
|
|
|
if (addr == nullptr)
|
2017-09-03 15:29:10 +02:00
|
|
|
return NET_UNKNOWN;
|
|
|
|
if (addr->IsRFC4380())
|
|
|
|
return NET_TEREDO;
|
|
|
|
return addr->GetNetwork();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Calculates a metric for how reachable (*this) is from a given partner */
|
|
|
|
int CNetAddr::GetReachabilityFrom(const CNetAddr *paddrPartner) const
|
|
|
|
{
|
|
|
|
enum Reachability {
|
|
|
|
REACH_UNREACHABLE,
|
|
|
|
REACH_DEFAULT,
|
|
|
|
REACH_TEREDO,
|
|
|
|
REACH_IPV6_WEAK,
|
|
|
|
REACH_IPV4,
|
|
|
|
REACH_IPV6_STRONG,
|
|
|
|
REACH_PRIVATE
|
|
|
|
};
|
|
|
|
|
2017-06-24 12:16:41 +02:00
|
|
|
if (!IsRoutable() || IsInternal())
|
2017-09-03 15:29:10 +02:00
|
|
|
return REACH_UNREACHABLE;
|
|
|
|
|
|
|
|
int ourNet = GetExtNetwork(this);
|
|
|
|
int theirNet = GetExtNetwork(paddrPartner);
|
|
|
|
bool fTunnel = IsRFC3964() || IsRFC6052() || IsRFC6145();
|
|
|
|
|
|
|
|
switch(theirNet) {
|
|
|
|
case NET_IPV4:
|
|
|
|
switch(ourNet) {
|
|
|
|
default: return REACH_DEFAULT;
|
|
|
|
case NET_IPV4: return REACH_IPV4;
|
|
|
|
}
|
|
|
|
case NET_IPV6:
|
|
|
|
switch(ourNet) {
|
|
|
|
default: return REACH_DEFAULT;
|
|
|
|
case NET_TEREDO: return REACH_TEREDO;
|
|
|
|
case NET_IPV4: return REACH_IPV4;
|
|
|
|
case NET_IPV6: return fTunnel ? REACH_IPV6_WEAK : REACH_IPV6_STRONG; // only prefer giving our IPv6 address if it's not tunnelled
|
|
|
|
}
|
2020-04-16 07:21:49 +02:00
|
|
|
case NET_ONION:
|
2017-09-03 15:29:10 +02:00
|
|
|
switch(ourNet) {
|
|
|
|
default: return REACH_DEFAULT;
|
|
|
|
case NET_IPV4: return REACH_IPV4; // Tor users can connect to IPv4 as well
|
2020-04-16 07:21:49 +02:00
|
|
|
case NET_ONION: return REACH_PRIVATE;
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
|
|
|
case NET_TEREDO:
|
|
|
|
switch(ourNet) {
|
|
|
|
default: return REACH_DEFAULT;
|
|
|
|
case NET_TEREDO: return REACH_TEREDO;
|
|
|
|
case NET_IPV6: return REACH_IPV6_WEAK;
|
|
|
|
case NET_IPV4: return REACH_IPV4;
|
|
|
|
}
|
|
|
|
case NET_UNKNOWN:
|
|
|
|
case NET_UNROUTABLE:
|
|
|
|
default:
|
|
|
|
switch(ourNet) {
|
|
|
|
default: return REACH_DEFAULT;
|
|
|
|
case NET_TEREDO: return REACH_TEREDO;
|
|
|
|
case NET_IPV6: return REACH_IPV6_WEAK;
|
|
|
|
case NET_IPV4: return REACH_IPV4;
|
2020-04-16 07:21:49 +02:00
|
|
|
case NET_ONION: return REACH_PRIVATE; // either from Tor, or don't care about our address
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-19 14:24:26 +02:00
|
|
|
CService::CService() : port(0)
|
2017-09-03 15:29:10 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CService::CService(const CNetAddr& cip, unsigned short portIn) : CNetAddr(cip), port(portIn)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CService::CService(const struct in_addr& ipv4Addr, unsigned short portIn) : CNetAddr(ipv4Addr), port(portIn)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CService::CService(const struct in6_addr& ipv6Addr, unsigned short portIn) : CNetAddr(ipv6Addr), port(portIn)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CService::CService(const struct sockaddr_in& addr) : CNetAddr(addr.sin_addr), port(ntohs(addr.sin_port))
|
|
|
|
{
|
|
|
|
assert(addr.sin_family == AF_INET);
|
|
|
|
}
|
|
|
|
|
2016-04-08 14:11:33 +02:00
|
|
|
CService::CService(const struct sockaddr_in6 &addr) : CNetAddr(addr.sin6_addr, addr.sin6_scope_id), port(ntohs(addr.sin6_port))
|
2017-09-03 15:29:10 +02:00
|
|
|
{
|
|
|
|
assert(addr.sin6_family == AF_INET6);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CService::SetSockAddr(const struct sockaddr *paddr)
|
|
|
|
{
|
|
|
|
switch (paddr->sa_family) {
|
|
|
|
case AF_INET:
|
|
|
|
*this = CService(*(const struct sockaddr_in*)paddr);
|
|
|
|
return true;
|
|
|
|
case AF_INET6:
|
|
|
|
*this = CService(*(const struct sockaddr_in6*)paddr);
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned short CService::GetPort() const
|
|
|
|
{
|
|
|
|
return port;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const CService& a, const CService& b)
|
|
|
|
{
|
2018-02-07 22:15:16 +01:00
|
|
|
return static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port == b.port;
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(const CService& a, const CService& b)
|
|
|
|
{
|
2018-02-07 22:15:16 +01:00
|
|
|
return static_cast<CNetAddr>(a) < static_cast<CNetAddr>(b) || (static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port < b.port);
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CService::GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const
|
|
|
|
{
|
|
|
|
if (IsIPv4()) {
|
|
|
|
if (*addrlen < (socklen_t)sizeof(struct sockaddr_in))
|
|
|
|
return false;
|
|
|
|
*addrlen = sizeof(struct sockaddr_in);
|
|
|
|
struct sockaddr_in *paddrin = (struct sockaddr_in*)paddr;
|
|
|
|
memset(paddrin, 0, *addrlen);
|
|
|
|
if (!GetInAddr(&paddrin->sin_addr))
|
|
|
|
return false;
|
|
|
|
paddrin->sin_family = AF_INET;
|
|
|
|
paddrin->sin_port = htons(port);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (IsIPv6()) {
|
|
|
|
if (*addrlen < (socklen_t)sizeof(struct sockaddr_in6))
|
|
|
|
return false;
|
|
|
|
*addrlen = sizeof(struct sockaddr_in6);
|
|
|
|
struct sockaddr_in6 *paddrin6 = (struct sockaddr_in6*)paddr;
|
|
|
|
memset(paddrin6, 0, *addrlen);
|
|
|
|
if (!GetIn6Addr(&paddrin6->sin6_addr))
|
|
|
|
return false;
|
2016-04-08 14:11:33 +02:00
|
|
|
paddrin6->sin6_scope_id = scopeId;
|
2017-09-03 15:29:10 +02:00
|
|
|
paddrin6->sin6_family = AF_INET6;
|
|
|
|
paddrin6->sin6_port = htons(port);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<unsigned char> CService::GetKey() const
|
|
|
|
{
|
2020-07-15 20:59:00 +02:00
|
|
|
auto key = GetAddrBytes();
|
|
|
|
key.push_back(port / 0x100);
|
|
|
|
key.push_back(port & 0x0FF);
|
|
|
|
return key;
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string CService::ToStringPort() const
|
|
|
|
{
|
|
|
|
return strprintf("%u", port);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CService::ToStringIPPort(bool fUseGetnameinfo) const
|
|
|
|
{
|
2021-05-25 15:43:13 +02:00
|
|
|
if (IsIPv4() || IsTor() || IsI2P() || IsInternal()) {
|
2017-09-03 15:29:10 +02:00
|
|
|
return ToStringIP(fUseGetnameinfo) + ":" + ToStringPort();
|
|
|
|
} else {
|
|
|
|
return "[" + ToStringIP(fUseGetnameinfo) + "]:" + ToStringPort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CService::ToString(bool fUseGetnameinfo) const
|
|
|
|
{
|
|
|
|
return ToStringIPPort(fUseGetnameinfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CService::SetPort(unsigned short portIn)
|
|
|
|
{
|
|
|
|
port = portIn;
|
|
|
|
}
|
|
|
|
|
|
|
|
CSubNet::CSubNet():
|
|
|
|
valid(false)
|
|
|
|
{
|
|
|
|
memset(netmask, 0, sizeof(netmask));
|
|
|
|
}
|
|
|
|
|
2020-08-24 21:03:31 +02:00
|
|
|
CSubNet::CSubNet(const CNetAddr& addr, uint8_t mask) : CSubNet()
|
2017-09-03 15:29:10 +02:00
|
|
|
{
|
2020-08-24 21:03:31 +02:00
|
|
|
valid = (addr.IsIPv4() && mask <= ADDR_IPV4_SIZE * 8) ||
|
|
|
|
(addr.IsIPv6() && mask <= ADDR_IPV6_SIZE * 8);
|
|
|
|
if (!valid) {
|
|
|
|
return;
|
|
|
|
}
|
2017-09-03 15:29:10 +02:00
|
|
|
|
2020-08-24 21:03:31 +02:00
|
|
|
assert(mask <= sizeof(netmask) * 8);
|
2017-09-03 15:29:10 +02:00
|
|
|
|
2020-08-24 21:03:31 +02:00
|
|
|
network = addr;
|
2017-09-03 15:29:10 +02:00
|
|
|
|
2020-08-24 21:03:31 +02:00
|
|
|
uint8_t n = mask;
|
|
|
|
for (size_t i = 0; i < network.m_addr.size(); ++i) {
|
|
|
|
const uint8_t bits = n < 8 ? n : 8;
|
|
|
|
netmask[i] = (uint8_t)((uint8_t)0xFF << (8 - bits)); // Set first bits.
|
|
|
|
network.m_addr[i] &= netmask[i]; // Normalize network according to netmask.
|
|
|
|
n -= bits;
|
|
|
|
}
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
|
|
|
|
2020-08-24 21:03:31 +02:00
|
|
|
/**
|
|
|
|
* @returns The number of 1-bits in the prefix of the specified subnet mask. If
|
|
|
|
* the specified subnet mask is not a valid one, -1.
|
|
|
|
*/
|
|
|
|
static inline int NetmaskBits(uint8_t x)
|
2017-09-03 15:29:10 +02:00
|
|
|
{
|
2020-08-24 21:03:31 +02:00
|
|
|
switch(x) {
|
|
|
|
case 0x00: return 0;
|
|
|
|
case 0x80: return 1;
|
|
|
|
case 0xc0: return 2;
|
|
|
|
case 0xe0: return 3;
|
|
|
|
case 0xf0: return 4;
|
|
|
|
case 0xf8: return 5;
|
|
|
|
case 0xfc: return 6;
|
|
|
|
case 0xfe: return 7;
|
|
|
|
case 0xff: return 8;
|
|
|
|
default: return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CSubNet::CSubNet(const CNetAddr& addr, const CNetAddr& mask) : CSubNet()
|
|
|
|
{
|
|
|
|
valid = (addr.IsIPv4() || addr.IsIPv6()) && addr.m_net == mask.m_net;
|
|
|
|
if (!valid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Check if `mask` contains 1-bits after 0-bits (which is an invalid netmask).
|
|
|
|
bool zeros_found = false;
|
|
|
|
for (auto b : mask.m_addr) {
|
|
|
|
const int num_bits = NetmaskBits(b);
|
|
|
|
if (num_bits == -1 || (zeros_found && num_bits != 0)) {
|
|
|
|
valid = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (num_bits < 8) {
|
|
|
|
zeros_found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(mask.m_addr.size() <= sizeof(netmask));
|
2017-09-03 15:29:10 +02:00
|
|
|
|
2020-08-24 21:03:31 +02:00
|
|
|
memcpy(netmask, mask.m_addr.data(), mask.m_addr.size());
|
2017-09-03 15:29:10 +02:00
|
|
|
|
2020-08-24 21:03:31 +02:00
|
|
|
network = addr;
|
2017-09-03 15:29:10 +02:00
|
|
|
|
|
|
|
// Normalize network according to netmask
|
2020-08-24 21:03:31 +02:00
|
|
|
for (size_t x = 0; x < network.m_addr.size(); ++x) {
|
|
|
|
network.m_addr[x] &= netmask[x];
|
|
|
|
}
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
|
|
|
|
2020-08-24 21:03:31 +02:00
|
|
|
CSubNet::CSubNet(const CNetAddr& addr) : CSubNet()
|
2017-09-03 15:29:10 +02:00
|
|
|
{
|
2020-08-24 21:03:31 +02:00
|
|
|
valid = addr.IsIPv4() || addr.IsIPv6();
|
|
|
|
if (!valid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(addr.m_addr.size() <= sizeof(netmask));
|
|
|
|
|
|
|
|
memset(netmask, 0xFF, addr.m_addr.size());
|
|
|
|
|
2017-09-03 15:29:10 +02:00
|
|
|
network = addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CSubNet::Match(const CNetAddr &addr) const
|
|
|
|
{
|
2020-07-29 13:31:10 +02:00
|
|
|
if (!valid || !addr.IsValid() || network.m_net != addr.m_net)
|
2017-09-03 15:29:10 +02:00
|
|
|
return false;
|
2020-08-24 21:03:31 +02:00
|
|
|
assert(network.m_addr.size() == addr.m_addr.size());
|
|
|
|
for (size_t x = 0; x < addr.m_addr.size(); ++x) {
|
|
|
|
if ((addr.m_addr[x] & netmask[x]) != network.m_addr[x]) {
|
2017-09-03 15:29:10 +02:00
|
|
|
return false;
|
2020-08-24 21:03:31 +02:00
|
|
|
}
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
2020-08-24 21:03:31 +02:00
|
|
|
return true;
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string CSubNet::ToString() const
|
|
|
|
{
|
2020-08-24 21:03:31 +02:00
|
|
|
assert(network.m_addr.size() <= sizeof(netmask));
|
|
|
|
|
|
|
|
uint8_t cidr = 0;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < network.m_addr.size(); ++i) {
|
|
|
|
if (netmask[i] == 0x00) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cidr += NetmaskBits(netmask[i]);
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
|
|
|
|
2020-08-24 21:03:31 +02:00
|
|
|
return network.ToString() + strprintf("/%u", cidr);
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CSubNet::IsValid() const
|
|
|
|
{
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
2020-10-15 11:22:52 +02:00
|
|
|
bool CSubNet::SanityCheck() const
|
|
|
|
{
|
|
|
|
if (!(network.IsIPv4() || network.IsIPv6())) return false;
|
|
|
|
|
|
|
|
for (size_t x = 0; x < network.m_addr.size(); ++x) {
|
|
|
|
if (network.m_addr[x] & ~netmask[x]) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-03 15:29:10 +02:00
|
|
|
bool operator==(const CSubNet& a, const CSubNet& b)
|
|
|
|
{
|
|
|
|
return a.valid == b.valid && a.network == b.network && !memcmp(a.netmask, b.netmask, 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(const CSubNet& a, const CSubNet& b)
|
|
|
|
{
|
|
|
|
return (a.network < b.network || (a.network == b.network && memcmp(a.netmask, b.netmask, 16) < 0));
|
|
|
|
}
|