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 if (!addr.ends_with(
".onion"))
return false;
232 addr.remove_suffix(6);
251 if (!std::ranges::equal(input_checksum, calculated_checksum)) {
256 m_addr.
assign(input_pubkey.begin(), input_pubkey.end());
266 static constexpr size_t b32_len{52};
267 static const char* suffix{
".b32.i2p"};
268 static constexpr size_t suffix_len{8};
270 if (addr.size() != b32_len + suffix_len ||
ToLower(addr.substr(b32_len)) != suffix) {
276 const std::string b32_padded{
tfm::format(
"%s====", addr.substr(0, b32_len))};
280 if (!address_bytes || address_bytes->size() !=
ADDR_I2P_SIZE) {
285 m_addr.
assign(address_bytes->begin(), address_bytes->end());
293 const uint8_t* ptr =
reinterpret_cast<const uint8_t*
>(&ipv4Addr);
299 SetLegacyIPv6({
reinterpret_cast<const uint8_t*
>(&ipv6Addr),
sizeof(ipv6Addr)});
354 HasPrefix(
m_addr, std::array<uint8_t, 12>{0x00, 0x64, 0xFF, 0x9B, 0x00, 0x00,
355 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
366 0x00, 0x00, 0x00, 0x00});
377 HasPrefix(
m_addr, std::array<uint8_t, 12>{0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
378 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00});
384 (
m_addr[3] & 0xF0) == 0x10;
390 (
m_addr[3] & 0xF0) == 0x20;
406 static const unsigned char pchLocal[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
427 unsigned char ipNone6[16] = {};
445 if (addr == INADDR_ANY || addr == INADDR_NONE) {
509 return strprintf(
"%u.%u.%u.%u", a[0], a[1], a[2], a[3]);
514static std::string
IPv6ToString(std::span<const uint8_t> a, uint32_t scope_id)
517 const std::array groups{
531 size_t start_index{0};
537 ZeroSpan longest, current;
538 for (
size_t i{0}; i < groups.size(); ++i) {
539 if (groups[i] != 0) {
540 current = {i + 1, 0};
544 if (current.len > longest.len) {
551 for (
size_t i{0}; i < groups.size(); ++i) {
553 if (longest.len >= 2 && i >= longest.start_index && i < longest.start_index + longest.len) {
554 if (i == longest.start_index) {
559 r +=
strprintf(
"%s%x", ((!r.empty() && r.back() !=
':') ?
":" :
""), groups[i]);
697 return {std::begin(serialized), std::end(serialized)};
726 return REACH_UNREACHABLE;
735 default:
return REACH_DEFAULT;
740 default:
return REACH_DEFAULT;
743 case NET_IPV6:
return fTunnel ? REACH_IPV6_WEAK : REACH_IPV6_STRONG;
747 default:
return REACH_DEFAULT;
753 case NET_I2P:
return REACH_PRIVATE;
754 default:
return REACH_DEFAULT;
759 default:
return REACH_DEFAULT;
763 default:
return REACH_DEFAULT;
765 case NET_IPV6:
return REACH_IPV6_WEAK;
771 default:
return REACH_DEFAULT;
773 case NET_IPV6:
return REACH_IPV6_WEAK;
798 assert(addr.sin_family == AF_INET);
803 assert(addr.sin6_family == AF_INET6);
808 switch (paddr->sa_family) {
810 if (addrlen !=
sizeof(
struct sockaddr_in))
return false;
811 *
this =
CService(*(
const struct sockaddr_in*)paddr);
814 if (addrlen !=
sizeof(
struct sockaddr_in6))
return false;
815 *
this =
CService(*(
const struct sockaddr_in6*)paddr);
865 if (*addrlen < (socklen_t)
sizeof(
struct sockaddr_in))
867 *addrlen =
sizeof(
struct sockaddr_in);
868 struct sockaddr_in *paddrin = (
struct sockaddr_in*)paddr;
869 memset(paddrin, 0, *addrlen);
872 paddrin->sin_family = AF_INET;
873 paddrin->sin_port = htons(
port);
877 if (*addrlen < (socklen_t)
sizeof(
struct sockaddr_in6))
879 *addrlen =
sizeof(
struct sockaddr_in6);
880 struct sockaddr_in6 *paddrin6 = (
struct sockaddr_in6*)paddr;
881 memset(paddrin6, 0, *addrlen);
885 paddrin6->sin6_family = AF_INET6;
886 paddrin6->sin6_port = htons(
port);
898 key.push_back(
port / 0x100);
899 key.push_back(
port & 0x0FF);
934 const uint8_t bits = n < 8 ? n : 8;
935 netmask[i] = (uint8_t)((uint8_t)0xFF << (8 - bits));
968 bool zeros_found =
false;
969 for (
auto b : mask.
m_addr) {
971 if (num_bits == -1 || (zeros_found && num_bits != 0)) {
994 switch (addr.
m_net) {
1039 for (
size_t x = 0; x < addr.
m_addr.
size(); ++x) {
Network GetNetClass() const
void SerializeV1Array(uint8_t(&arr)[V1_SERIALIZATION_SIZE]) const
Serialize in pre-ADDRv2/BIP155 format to an array.
std::string ToStringAddr() const
prevector< ADDR_IPV6_SIZE, uint8_t > m_addr
Raw representation of the network address.
void SetIP(const CNetAddr &ip)
bool GetIn6Addr(struct in6_addr *pipv6Addr) const
Try to get our IPv6 (or CJDNS) address.
std::vector< unsigned char > GetAddrBytes() const
bool GetInAddr(struct in_addr *pipv4Addr) const
Try to get our IPv4 address.
bool HasLinkedIPv4() const
Whether this address has a linked IPv4 address (see GetLinkedIPv4()).
bool HasCJDNSPrefix() const
Network m_net
Network to which this address belongs.
bool SetSpecial(std::string_view addr)
Parse a Tor or I2P address and set this object to it.
BIP155Network GetBIP155Network() const
Get the BIP155 network id of this address.
void SetLegacyIPv6(std::span< const uint8_t > ipv6)
Set from a legacy IPv6 address.
uint32_t GetLinkedIPv4() const
For IPv4, mapped IPv4, SIIT translated IPv4, Teredo, 6to4 tunneled addresses, return the relevant IPv...
uint32_t m_scope_id
Scope id if scoped/link-local IPV6 address.
bool SetI2P(std::string_view addr)
Parse an I2P address and set this object to it.
static constexpr size_t V1_SERIALIZATION_SIZE
Size of CNetAddr when serialized as ADDRv1 (pre-BIP155) (in bytes).
bool SetNetFromBIP155Network(uint8_t possible_bip155_net, size_t address_size)
Set m_net from the provided BIP155 network id and size after validation.
bool SetInternal(const std::string &name)
Create an "internal" address that represents a name or FQDN.
enum Network GetNetwork() const
int GetReachabilityFrom(const CNetAddr &paddrPartner) const
Calculates a metric for how reachable (*this) is from a given partner.
CNetAddr()
Construct an unspecified IPv6 network address (::/128).
bool SetTor(std::string_view addr)
Parse a Tor address and set this object to it.
bool IsAddrV1Compatible() const
Check if the current object can be serialized in pre-ADDRv2/BIP155 format.
BIP155Network
BIP155 network ids recognized by this software.
A hasher class for SHA-256.
void Finalize(unsigned char hash[OUTPUT_SIZE])
CSHA256 & Write(const unsigned char *data, size_t len)
A combination of a network address (CNetAddr) and a (TCP) port.
bool SetSockAddr(const struct sockaddr *paddr, socklen_t addrlen)
Set CService from a network sockaddr.
sa_family_t GetSAFamily() const
Get the address family.
bool GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) const
Obtain the IPv4/6 socket address this represents.
std::string ToStringAddrPort() const
std::vector< unsigned char > GetKey() const
bool valid
Is this value valid? (only used to signal parse errors)
CNetAddr network
Network (base) address.
uint8_t netmask[16]
Netmask, in network byte order.
std::string ToString() const
CSubNet()
Construct an invalid subnet (empty, Match() always returns false).
bool Match(const CNetAddr &addr) const
SHA3_256 & Write(std::span< const unsigned char > data)
SHA3_256 & Finalize(std::span< unsigned char > output)
static constexpr size_t OUTPUT_SIZE
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
void assign(size_type n, const T &val)
uint16_t ReadBE16(const B *ptr)
uint32_t ReadBE32(const B *ptr)
static const unsigned char VERSION[]
static void Checksum(std::span< const uint8_t > addr_pubkey, uint8_t(&checksum)[CHECKSUM_LEN])
static constexpr size_t CHECKSUM_LEN
static constexpr size_t TOTAL_LEN
bool HasPrefix(const T1 &obj, const std::array< uint8_t, PREFIX_LEN > &prefix)
Check whether a container begins with the given prefix.
bool ContainsNoNUL(std::string_view str) noexcept
Check if a string does not contain any embedded NUL (\0) characters.
static std::string IPv6ToString(std::span< const uint8_t > a, uint32_t scope_id)
static const int NET_TEREDO
static int NetmaskBits(uint8_t x)
bool operator==(const CNetAddr &a, const CNetAddr &b)
static std::string IPv4ToString(std::span< const uint8_t > a)
bool operator<(const CNetAddr &a, const CNetAddr &b)
std::string OnionToString(std::span< const uint8_t > addr)
static int GetExtNetwork(const CNetAddr &addr)
static constexpr size_t ADDR_CJDNS_SIZE
Size of CJDNS address (in bytes).
static constexpr size_t ADDR_TORV3_SIZE
Size of TORv3 address (in bytes).
static constexpr size_t ADDR_I2P_SIZE
Size of I2P address (in bytes).
static constexpr size_t ADDR_INTERNAL_SIZE
Size of "internal" (NET_INTERNAL) address (in bytes).
static const std::array< uint8_t, 6 > INTERNAL_IN_IPV6_PREFIX
Prefix of an IPv6 address when it contains an embedded "internal" address.
static constexpr size_t ADDR_IPV4_SIZE
Size of IPv4 address (in bytes).
static const std::array< uint8_t, 6 > TORV2_IN_IPV6_PREFIX
Prefix of an IPv6 address when it contains an embedded TORv2 address.
@ NET_MAX
Dummy value to indicate the number of NET_* constants.
@ NET_ONION
TOR (v2 or v3)
@ NET_UNROUTABLE
Addresses from these networks are not publicly routable on the global Internet.
@ NET_INTERNAL
A set of addresses that represent the hash of a string or FQDN.
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_IPV6_SIZE
Size of IPv6 address (in bytes).
std::optional< std::vector< unsigned char > > DecodeBase32(std::string_view str)
std::string EncodeBase32(std::span< const unsigned char > input, bool pad)
Base32 encode.
std::string ToLower(std::string_view str)
Returns the lowercase equivalent of the given string.