35 return BIP155Network::TORV3;
39 return BIP155Network::CJDNS;
51 switch (possible_bip155_net) {
57 throw std::ios_base::failure(
58 strprintf(
"BIP155 IPv4 address with length %u (should be %u)", address_size,
65 throw std::ios_base::failure(
66 strprintf(
"BIP155 IPv6 address with length %u (should be %u)", address_size,
68 case BIP155Network::TORV3:
73 throw std::ios_base::failure(
74 strprintf(
"BIP155 TORv3 address with length %u (should be %u)", address_size,
81 throw std::ios_base::failure(
82 strprintf(
"BIP155 I2P address with length %u (should be %u)", address_size,
84 case BIP155Network::CJDNS:
89 throw std::ios_base::failure(
90 strprintf(
"BIP155 CJDNS address with length %u (should be %u)", address_size,
110 switch (ipIn.
m_net) {
179 unsigned char hash[32] = {};
194 static const unsigned char prefix[] =
".onion checksum";
195 static constexpr
size_t prefix_len = 15;
200 hasher.
Write(addr_pubkey);
207 memcpy(checksum, checksum_full,
sizeof(checksum));
231 static const char* suffix{
".onion"};
232 static constexpr
size_t suffix_len{6};
234 if (addr.size() <= suffix_len || addr.substr(addr.size() - suffix_len) != suffix) {
239 const auto& input =
DecodeBase32(addr.substr(0, addr.size() - suffix_len).c_str(), &invalid);
257 if (input_checksum != calculated_checksum) {
262 m_addr.
assign(input_pubkey.begin(), input_pubkey.end());
272 static constexpr
size_t b32_len{52};
273 static const char* suffix{
".b32.i2p"};
274 static constexpr
size_t suffix_len{8};
276 if (addr.size() != b32_len + suffix_len ||
ToLower(addr.substr(b32_len)) != suffix) {
282 const std::string b32_padded = addr.substr(0, b32_len) +
"====";
285 const auto& address_bytes =
DecodeBase32(b32_padded.c_str(), &invalid);
292 m_addr.
assign(address_bytes.begin(), address_bytes.end());
300 const uint8_t* ptr =
reinterpret_cast<const uint8_t*
>(&ipv4Addr);
365 HasPrefix(
m_addr, std::array<uint8_t, 12>{0x00, 0x64, 0xFF, 0x9B, 0x00, 0x00,
366 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
377 0x00, 0x00, 0x00, 0x00});
388 HasPrefix(
m_addr, std::array<uint8_t, 12>{0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
389 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00});
395 (
m_addr[3] & 0xF0) == 0x10;
401 (
m_addr[3] & 0xF0) == 0x20;
433 static const unsigned char pchLocal[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
454 unsigned char ipNone6[16] = {};
473 if (addr == INADDR_ANY || addr == INADDR_NONE) {
537 return strprintf(
"%u.%u.%u.%u", a[0], a[1], a[2], a[3]);
545 const std::array groups{
559 size_t start_index{0};
565 ZeroSpan longest, current;
566 for (
size_t i{0}; i < groups.size(); ++i) {
567 if (groups[i] != 0) {
568 current = {i + 1, 0};
572 if (current.len > longest.len) {
579 for (
size_t i{0}; i < groups.size(); ++i) {
581 if (longest.len >= 2 && i >= longest.start_index && i < longest.start_index + longest.len) {
582 if (i == longest.start_index) {
587 r +=
strprintf(
"%s%x", ((!r.empty() && r.back() !=
':') ?
":" :
""), groups[i]);
727 if (asmap.size() == 0 || (net_class !=
NET_IPV4 && net_class !=
NET_IPV6)) {
730 std::vector<bool> ip_bits(128);
733 for (int8_t byte_i = 0; byte_i < 12; ++byte_i) {
734 for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
739 for (
int i = 0; i < 32; ++i) {
740 ip_bits[96 + i] = (ipv4 >> (31 - i)) & 1;
745 for (int8_t byte_i = 0; byte_i < 16; ++byte_i) {
746 uint8_t cur_byte =
m_addr[byte_i];
747 for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
748 ip_bits[byte_i * 8 + bit_i] = (cur_byte >> (7 - bit_i)) & 1;
752 uint32_t mapped_as =
Interpret(asmap, ip_bits);
768 std::vector<unsigned char> vchRet;
775 for (
int i = 0; i < 4; i++) {
776 vchRet.push_back((asn >> (8 * i)) & 0xFF);
781 vchRet.push_back(net_class);
794 vchRet.push_back((ipv4 >> 24) & 0xFF);
795 vchRet.push_back((ipv4 >> 16) & 0xFF);
808 const size_t num_bytes = nBits / 8;
814 vchRet.push_back(
m_addr[num_bytes] | ((1 << (8 - nBits)) - 1));
825 return {std::begin(serialized), std::end(serialized)};
834 memcpy(&nRet, &hash,
sizeof(nRet));
865 return REACH_UNREACHABLE;
874 default:
return REACH_DEFAULT;
879 default:
return REACH_DEFAULT;
882 case NET_IPV6:
return fTunnel ? REACH_IPV6_WEAK : REACH_IPV6_STRONG;
886 default:
return REACH_DEFAULT;
892 case NET_I2P:
return REACH_PRIVATE;
893 default:
return REACH_DEFAULT;
897 default:
return REACH_DEFAULT;
899 case NET_IPV6:
return REACH_IPV6_WEAK;
906 default:
return REACH_DEFAULT;
908 case NET_IPV6:
return REACH_IPV6_WEAK;
933 assert(addr.sin_family == AF_INET);
938 assert(addr.sin6_family == AF_INET6);
943 switch (paddr->sa_family) {
945 *
this =
CService(*(
const struct sockaddr_in*)paddr);
948 *
this =
CService(*(
const struct sockaddr_in6*)paddr);
962 return static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.
port == b.
port;
967 return static_cast<CNetAddr>(a) < static_cast<CNetAddr>(b) || (
static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.
port < b.
port);
985 if (*addrlen < (socklen_t)
sizeof(
struct sockaddr_in))
987 *addrlen =
sizeof(
struct sockaddr_in);
988 struct sockaddr_in *paddrin = (
struct sockaddr_in*)paddr;
989 memset(paddrin, 0, *addrlen);
992 paddrin->sin_family = AF_INET;
993 paddrin->sin_port = htons(
port);
997 if (*addrlen < (socklen_t)
sizeof(
struct sockaddr_in6))
999 *addrlen =
sizeof(
struct sockaddr_in6);
1000 struct sockaddr_in6 *paddrin6 = (
struct sockaddr_in6*)paddr;
1001 memset(paddrin6, 0, *addrlen);
1005 paddrin6->sin6_family = AF_INET6;
1006 paddrin6->sin6_port = htons(
port);
1018 key.push_back(
port / 0x100);
1019 key.push_back(
port & 0x0FF);
1062 const uint8_t bits = n < 8 ? n : 8;
1063 netmask[i] = (uint8_t)((uint8_t)0xFF << (8 - bits));
1076 case 0x00:
return 0;
1077 case 0x80:
return 1;
1078 case 0xc0:
return 2;
1079 case 0xe0:
return 3;
1080 case 0xf0:
return 4;
1081 case 0xf8:
return 5;
1082 case 0xfc:
return 6;
1083 case 0xfe:
return 7;
1084 case 0xff:
return 8;
1096 bool zeros_found =
false;
1097 for (
auto b : mask.
m_addr) {
1099 if (num_bits == -1 || (zeros_found && num_bits != 0)) {
1122 switch (addr.
m_net) {
1167 for (
size_t x = 0; x < addr.
m_addr.
size(); ++x) {
int GetReachabilityFrom(const CNetAddr *paddrPartner=nullptr) const
Calculates a metric for how reachable (*this) is from a given partner.
std::vector< unsigned char > GetGroup(const std::vector< bool > &asmap) const
Get the canonical identifier of our network group.
CSHA256 & Write(const unsigned char *data, size_t len)
std::string ToStringPort() const
bool HasLinkedIPv4() const
Whether this address has a linked IPv4 address (see GetLinkedIPv4()).
bool SetNetFromBIP155Network(uint8_t possible_bip155_net, size_t address_size)
Set m_net from the provided BIP155 network id and size after validation.
SHA3_256 & Write(Span< const unsigned char > data)
static std::string IPv6ToString(Span< const uint8_t > a, uint32_t scope_id)
void assign(size_type n, const T &val)
std::string ToLower(const std::string &str)
Returns the lowercase equivalent of the given string.
A set of addresses that represent the hash of a string or FQDN.
Dummy value to indicate the number of NET_* constants.
constexpr C * end() const noexcept
void SetIP(const CNetAddr &ip)
bool operator==(const CNetAddr &a, const CNetAddr &b)
static std::string IPv4ToString(Span< const uint8_t > a)
static constexpr size_t ADDR_TORV3_SIZE
Size of TORv3 address (in bytes).
static constexpr size_t MAX_ADDRV2_SIZE
Maximum size of an address as defined in BIP155 (in bytes).
static uint16_t ReadBE16(const unsigned char *ptr)
prevector< ADDR_IPV6_SIZE, uint8_t > m_addr
Raw representation of the network address.
static const int NET_UNKNOWN
bool SetTor(const std::string &addr)
Parse a Tor address and set this object to it.
constexpr std::size_t size() const noexcept
static constexpr size_t OUTPUT_SIZE
static constexpr size_t ADDR_IPV4_SIZE
Size of IPv4 address (in bytes).
std::vector< unsigned char > DecodeBase32(const char *p, bool *pf_invalid)
static const std::array< uint8_t, 6 > INTERNAL_IN_IPV6_PREFIX
Prefix of an IPv6 address when it contains an embedded "internal" address.
static const unsigned char VERSION[]
SHA3_256 & Finalize(Span< unsigned char > output)
bool SetI2P(const std::string &addr)
Parse an I2P address and set this object to it.
CNetAddr network
Network (base) address.
bool GetInAddr(struct in_addr *pipv4Addr) const
Try to get our IPv4 address.
bool SetSpecial(const std::string &addr)
Parse a Tor or I2P address and set this object to it.
std::string ToString() const
Network GetNetClass() const
bool GetIn6Addr(struct in6_addr *pipv6Addr) const
Try to get our IPv6 address.
enum Network GetNetwork() const
uint32_t m_scope_id
Scope id if scoped/link-local IPV6 address.
uint32_t GetMappedAS(const std::vector< bool > &asmap) const
static constexpr size_t ADDR_CJDNS_SIZE
Size of CJDNS address (in bytes).
bool IsCJDNS() const
Check whether this object represents a CJDNS address.
bool IsI2P() const
Check whether this object represents an I2P address.
static constexpr size_t ADDR_IPV6_SIZE
Size of IPv6 address (in bytes).
static int GetExtNetwork(const CNetAddr *addr)
void SerializeV1Array(uint8_t(&arr)[V1_SERIALIZATION_SIZE]) const
Serialize in pre-ADDRv2/BIP155 format to an array.
static constexpr size_t ADDR_I2P_SIZE
Size of I2P address (in bytes).
static constexpr size_t CHECKSUM_LEN
bool GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) const
Obtain the IPv4/6 socket address this represents.
std::string ToStringIP() const
BIP155Network GetBIP155Network() const
Get the BIP155 network id of this address.
BIP155Network
BIP155 network ids recognized by this software.
A combination of a network address (CNetAddr) and a (TCP) port.
bool HasPrefix(const T1 &obj, const std::array< uint8_t, PREFIX_LEN > &prefix)
Check whether a container begins with the given prefix.
std::vector< unsigned char > GetKey() const
static uint32_t ReadBE32(const unsigned char *ptr)
uint32_t Interpret(const std::vector< bool > &asmap, const std::vector< bool > &ip)
static void Checksum(Span< const uint8_t > addr_pubkey, uint8_t(&checksum)[CHECKSUM_LEN])
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
bool valid
Is this value valid? (only used to signal parse errors)
std::string EncodeBase32(Span< const unsigned char > input, bool pad)
Base32 encode.
bool Match(const CNetAddr &addr) const
static constexpr size_t TOTAL_LEN
bool operator<(const CNetAddr &a, const CNetAddr &b)
uint8_t netmask[16]
Netmask, in network byte order.
static int NetmaskBits(uint8_t x)
constexpr C * begin() const noexcept
std::string ToString() const
static const std::array< uint8_t, 6 > TORV2_IN_IPV6_PREFIX
Prefix of an IPv6 address when it contains an embedded TORv2 address.
constexpr C * data() const noexcept
static std::string OnionToString(Span< const uint8_t > addr)
std::string ToStringIPPort() const
CSubNet()
Construct an invalid subnet (empty, Match() always returns false).
static const int NET_TEREDO
bool ValidAsCString(const std::string &str) noexcept
Check if a string does not contain any embedded NUL (\0) characters.
std::string ToString() const
static const std::array< uint8_t, 12 > IPV4_IN_IPV6_PREFIX
Prefix of an IPv6 address when it contains an embedded IPv4 address.
static constexpr size_t ADDR_INTERNAL_SIZE
Size of "internal" (NET_INTERNAL) address (in bytes).
A Span is an object that can refer to a contiguous sequence of objects.
bool SanityCheckASMap(const std::vector< bool > &asmap)
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
uint32_t GetLinkedIPv4() const
For IPv4, mapped IPv4, SIIT translated IPv4, Teredo, 6to4 tunneled addresses, return the relevant IPv...
bool SetSockAddr(const struct sockaddr *paddr)
bool SetInternal(const std::string &name)
Create an "internal" address that represents a name or FQDN.
Network m_net
Network to which this address belongs.
A hasher class for SHA-256.
bool IsAddrV1Compatible() const
Check if the current object can be serialized in pre-ADDRv2/BIP155 format.
bool IsTor() const
Check whether this object represents a TOR address.
static constexpr size_t V1_SERIALIZATION_SIZE
Size of CNetAddr when serialized as ADDRv1 (pre-BIP155) (in bytes).
void SetLegacyIPv6(Span< const uint8_t > ipv6)
Set from a legacy IPv6 address.
std::vector< unsigned char > GetAddrBytes() const
Span< A > constexpr MakeSpan(A(&a)[N])
MakeSpan for arrays:
CNetAddr()
Construct an unspecified IPv6 network address (::/128).
Addresses from these networks are not publicly routable on the global Internet.