37 return BIP155Network::TORV2;
39 return BIP155Network::TORV3;
44 return BIP155Network::I2P;
46 return BIP155Network::CJDNS;
58 switch (possible_bip155_net) {
64 throw std::ios_base::failure(
65 strprintf(
"BIP155 IPv4 address with length %u (should be %u)", address_size,
72 throw std::ios_base::failure(
73 strprintf(
"BIP155 IPv6 address with length %u (should be %u)", address_size,
75 case BIP155Network::TORV2:
80 throw std::ios_base::failure(
81 strprintf(
"BIP155 TORv2 address with length %u (should be %u)", address_size,
83 case BIP155Network::TORV3:
88 throw std::ios_base::failure(
89 strprintf(
"BIP155 TORv3 address with length %u (should be %u)", address_size,
91 case BIP155Network::I2P:
96 throw std::ios_base::failure(
97 strprintf(
"BIP155 I2P address with length %u (should be %u)", address_size,
99 case BIP155Network::CJDNS:
104 throw std::ios_base::failure(
105 strprintf(
"BIP155 CJDNS address with length %u (should be %u)", address_size,
125 switch (ipIn.
m_net) {
191 unsigned char hash[32] = {};
192 CSHA256().
Write((
const unsigned char*)name.data(), name.size()).Finalize(hash);
206 static const unsigned char prefix[] =
".onion checksum";
207 static constexpr
size_t prefix_len = 15;
211 hasher.Write(
MakeSpan(prefix).first(prefix_len));
212 hasher.Write(addr_pubkey);
213 hasher.Write(VERSION);
215 uint8_t checksum_full[SHA3_256::OUTPUT_SIZE];
217 hasher.Finalize(checksum_full);
219 memcpy(checksum, checksum_full,
sizeof(checksum));
233 static const char* suffix{
".onion"};
234 static constexpr
size_t suffix_len{6};
237 str.substr(str.size() - suffix_len) != suffix) {
242 const auto& input =
DecodeBase32(str.substr(0, str.size() - suffix_len).c_str(), &invalid);
248 switch (input.size()) {
251 m_addr.assign(input.begin(), input.end());
265 if (input_checksum != calculated_checksum) {
270 m_addr.assign(input_pubkey.begin(), input_pubkey.end());
281 const uint8_t* ptr =
reinterpret_cast<const uint8_t*
>(&ipv4Addr);
296 return std::all_of(
m_addr.begin(),
m_addr.end(), [](uint8_t b) {
return b == 0; });
346 HasPrefix(
m_addr, std::array<uint8_t, 12>{0x00, 0x64, 0xFF, 0x9B, 0x00, 0x00,
347 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
358 0x00, 0x00, 0x00, 0x00});
369 HasPrefix(
m_addr, std::array<uint8_t, 12>{0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
370 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00});
376 (
m_addr[3] & 0xF0) == 0x10;
382 (
m_addr[3] & 0xF0) == 0x20;
414 static const unsigned char pchLocal[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
415 if (
IsIPv6() && memcmp(
m_addr.data(), pchLocal,
sizeof(pchLocal)) == 0) {
435 unsigned char ipNone6[16] = {};
436 if (
IsIPv6() && memcmp(
m_addr.data(), ipNone6,
sizeof(ipNone6)) == 0) {
449 if (addr == INADDR_ANY || addr == INADDR_NONE) {
516 return strprintf(
"%x:%x:%x:%x:%x:%x:%x:%x",
534 struct sockaddr_storage sockaddr;
535 socklen_t socklen =
sizeof(sockaddr);
536 if (serv.
GetSockAddr((
struct sockaddr*)&sockaddr, &socklen)) {
537 char name[1025] =
"";
538 if (!getnameinfo((
const struct sockaddr*)&sockaddr, socklen, name,
539 sizeof(name),
nullptr, 0, NI_NUMERICHOST))
540 return std::string(name);
609 assert(
sizeof(*pipv4Addr) ==
m_addr.size());
629 assert(
sizeof(*pipv6Addr) ==
m_addr.size());
676 if (asmap.size() == 0 || (net_class !=
NET_IPV4 && net_class !=
NET_IPV6)) {
679 std::vector<bool> ip_bits(128);
682 for (int8_t byte_i = 0; byte_i < 12; ++byte_i) {
683 for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
688 for (
int i = 0; i < 32; ++i) {
689 ip_bits[96 + i] = (ipv4 >> (31 - i)) & 1;
694 for (int8_t byte_i = 0; byte_i < 16; ++byte_i) {
695 uint8_t cur_byte =
m_addr[byte_i];
696 for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
697 ip_bits[byte_i * 8 + bit_i] = (cur_byte >> (7 - bit_i)) & 1;
701 uint32_t mapped_as =
Interpret(asmap, ip_bits);
717 std::vector<unsigned char> vchRet;
724 for (
int i = 0; i < 4; i++) {
725 vchRet.push_back((asn >> (8 * i)) & 0xFF);
730 vchRet.push_back(net_class);
743 vchRet.push_back((ipv4 >> 24) & 0xFF);
744 vchRet.push_back((ipv4 >> 16) & 0xFF);
757 const size_t num_bytes = nBits / 8;
758 vchRet.insert(vchRet.end(),
m_addr.begin(),
m_addr.begin() + num_bytes);
762 assert(num_bytes <
m_addr.size());
763 vchRet.push_back(
m_addr[num_bytes] | ((1 << (8 - nBits)) - 1));
774 return {std::begin(serialized), std::end(serialized)};
776 return std::vector<unsigned char>(
m_addr.begin(),
m_addr.end());
783 memcpy(&nRet, &hash,
sizeof(nRet));
814 return REACH_UNREACHABLE;
823 default:
return REACH_DEFAULT;
828 default:
return REACH_DEFAULT;
829 case NET_TEREDO:
return REACH_TEREDO;
831 case NET_IPV6:
return fTunnel ? REACH_IPV6_WEAK : REACH_IPV6_STRONG;
835 default:
return REACH_DEFAULT;
841 default:
return REACH_DEFAULT;
842 case NET_TEREDO:
return REACH_TEREDO;
843 case NET_IPV6:
return REACH_IPV6_WEAK;
850 default:
return REACH_DEFAULT;
851 case NET_TEREDO:
return REACH_TEREDO;
852 case NET_IPV6:
return REACH_IPV6_WEAK;
877 assert(addr.sin_family == AF_INET);
882 assert(addr.sin6_family == AF_INET6);
887 switch (paddr->sa_family) {
889 *
this =
CService(*(
const struct sockaddr_in*)paddr);
892 *
this =
CService(*(
const struct sockaddr_in6*)paddr);
906 return static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.
port == b.
port;
911 return static_cast<CNetAddr>(a) < static_cast<CNetAddr>(b) || (
static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.
port < b.
port);
929 if (*addrlen < (socklen_t)
sizeof(
struct sockaddr_in))
931 *addrlen =
sizeof(
struct sockaddr_in);
932 struct sockaddr_in *paddrin = (
struct sockaddr_in*)paddr;
933 memset(paddrin, 0, *addrlen);
936 paddrin->sin_family = AF_INET;
937 paddrin->sin_port = htons(
port);
941 if (*addrlen < (socklen_t)
sizeof(
struct sockaddr_in6))
943 *addrlen =
sizeof(
struct sockaddr_in6);
944 struct sockaddr_in6 *paddrin6 = (
struct sockaddr_in6*)paddr;
945 memset(paddrin6, 0, *addrlen);
949 paddrin6->sin6_family = AF_INET6;
950 paddrin6->sin6_port = htons(
port);
962 key.push_back(
port / 0x100);
963 key.push_back(
port & 0x0FF);
1000 assert(mask <=
sizeof(
netmask) * 8);
1006 const uint8_t bits = n < 8 ? n : 8;
1007 netmask[i] = (uint8_t)((uint8_t)0xFF << (8 - bits));
1020 case 0x00:
return 0;
1021 case 0x80:
return 1;
1022 case 0xc0:
return 2;
1023 case 0xe0:
return 3;
1024 case 0xf0:
return 4;
1025 case 0xf8:
return 5;
1026 case 0xfc:
return 6;
1027 case 0xfe:
return 7;
1028 case 0xff:
return 8;
1040 bool zeros_found =
false;
1041 for (
auto b : mask.
m_addr) {
1043 if (num_bits == -1 || (zeros_found && num_bits != 0)) {
1059 for (
size_t x = 0; x < network.m_addr.size(); ++x) {
1066 switch (addr.
m_net) {
1111 for (
size_t x = 0; x < addr.
m_addr.size(); ++x) {
CSHA256 & Write(const unsigned char *data, size_t len)
bool SetNetFromBIP155Network(uint8_t possible_bip155_net, size_t address_size)
Set m_net from the provided BIP155 network id and size after validation.
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)
std::string ToStringIP() const
NODISCARD bool ValidAsCString(const std::string &str) noexcept
Check if a string does not contain any embedded NUL (\0) characters.
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 std::string IPv6ToString(Span< const uint8_t > a)
bool GetIn6Addr(struct in6_addr *pipv6Addr) const
Try to get our IPv6 address.
prevector< ADDR_IPV6_SIZE, uint8_t > m_addr
Raw representation of the network address.
static const int NET_UNKNOWN
std::string ToStringIPPort() const
uint32_t GetMappedAS(const std::vector< bool > &asmap) const
constexpr std::size_t size() const noexcept
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[]
BIP155Network GetBIP155Network() const
Get the BIP155 network id of this address.
std::string ToString() const
CNetAddr network
Network (base) address.
bool Match(const CNetAddr &addr) const
uint32_t m_scope_id
Scope id if scoped/link-local IPV6 address.
static constexpr size_t ADDR_CJDNS_SIZE
Size of CJDNS address (in bytes).
int GetReachabilityFrom(const CNetAddr *paddrPartner=nullptr) const
Calculates a metric for how reachable (*this) is from a given partner.
static constexpr size_t ADDR_IPV6_SIZE
Size of IPv6 address (in bytes).
static int GetExtNetwork(const CNetAddr *addr)
static constexpr size_t ADDR_I2P_SIZE
Size of I2P address (in bytes).
static constexpr size_t ADDR_TORV2_SIZE
Size of TORv2 address (in bytes).
static constexpr size_t CHECKSUM_LEN
BIP155Network
BIP155 network ids recognized by this software.
bool HasLinkedIPv4() const
Whether this address has a linked IPv4 address (see GetLinkedIPv4()).
A combination of a network address (CNetAddr) and a (TCP) port.
void SerializeV1Array(uint8_t(&arr)[V1_SERIALIZATION_SIZE]) const
Serialize in pre-ADDRv2/BIP155 format to an array.
static uint16_t ReadBE16(const unsigned char *ptr)
static uint32_t ReadBE32(const unsigned char *ptr)
std::string ToString() const
bool IsTor() const
Check whether this object represents a TOR address.
bool IsI2P() const
Check whether this object represents an I2P address.
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 which stores up to N elements directly (without h...
bool valid
Is this value valid? (only used to signal parse errors)
bool GetInAddr(struct in_addr *pipv4Addr) const
Try to get our IPv4 address.
bool IsAddrV1Compatible() const
Check if the current object can be serialized in pre-ADDRv2/BIP155 format.
std::string EncodeBase32(Span< const unsigned char > input, bool pad)
Base32 encode.
std::vector< unsigned char > GetGroup(const std::vector< bool > &asmap) const
Get the canonical identifier of our network group.
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
friend bool operator<(const CNetAddr &a, const CNetAddr &b)
friend bool operator==(const CNetAddr &a, const CNetAddr &b)
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
void * memcpy(void *a, const void *b, size_t c)
CSubNet()
Construct an invalid subnet (empty, Match() always returns false).
static const int NET_TEREDO
uint32_t GetLinkedIPv4() const
For IPv4, mapped IPv4, SIIT translated IPv4, Teredo, 6to4 tunneled addresses, return the relevant IPv...
Network GetNetClass() const
NODISCARD bool HasPrefix(const T1 &obj, const std::array< uint8_t, PREFIX_LEN > &prefix)
Check whether a container begins with the given prefix.
bool SetSpecial(const std::string &strName)
Parse a TOR address and set this object to it.
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).
std::string ToString() const
A Span is an object that can refer to a contiguous sequence of objects.
std::string ToStringPort() const
bool SanityCheckASMap(const std::vector< bool > &asmap)
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
std::vector< unsigned char > GetAddrBytes() const
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 IsCJDNS() const
Check whether this object represents a CJDNS address.
static constexpr size_t V1_SERIALIZATION_SIZE
Size of CNetAddr when serialized as ADDRv1 (pre-BIP155) (in bytes).
bool GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) const
Obtain the IPv4/6 socket address this represents.
void SetLegacyIPv6(Span< const uint8_t > ipv6)
Set from a legacy IPv6 address.
std::vector< unsigned char > GetKey() const
enum Network GetNetwork() 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.