37 return BIP155Network::TORV2;
39 return BIP155Network::TORV3;
46 return BIP155Network::CJDNS;
60 size_t address_size) {
61 switch (possible_bip155_net) {
67 throw std::ios_base::failure(
68 strprintf(
"BIP155 IPv4 address with length %u (should be %u)",
75 throw std::ios_base::failure(
76 strprintf(
"BIP155 IPv6 address with length %u (should be %u)",
78 case BIP155Network::TORV2:
83 throw std::ios_base::failure(
84 strprintf(
"BIP155 TORv2 address with length %u (should be %u)",
86 case BIP155Network::TORV3:
91 throw std::ios_base::failure(
92 strprintf(
"BIP155 TORv3 address with length %u (should be %u)",
99 throw std::ios_base::failure(
100 strprintf(
"BIP155 I2P address with length %u (should be %u)",
102 case BIP155Network::CJDNS:
107 throw std::ios_base::failure(
108 strprintf(
"BIP155 CJDNS address with length %u (should be %u)",
127 switch (ipIn.
m_net) {
193 uint8_t hash[32] = {};
209 static const uint8_t
prefix[] =
".onion checksum";
210 static constexpr
size_t prefix_len = 15;
215 hasher.
Write(addr_pubkey);
222 memcpy(checksum, checksum_full,
sizeof(checksum));
244 static const char *suffix{
".onion"};
245 static constexpr
size_t suffix_len{6};
247 if (addr.size() <= suffix_len ||
248 addr.substr(addr.size() - suffix_len) != suffix) {
254 addr.substr(0, addr.size() - suffix_len).c_str(), &invalid);
260 switch (input.size()) {
280 if (input_checksum != calculated_checksum) {
285 m_addr.
assign(input_pubkey.begin(), input_pubkey.end());
296 static constexpr
size_t b32_len{52};
297 static const char *suffix{
".b32.i2p"};
298 static constexpr
size_t suffix_len{8};
300 if (addr.size() != b32_len + suffix_len ||
301 ToLower(addr.substr(b32_len)) != suffix) {
307 const std::string b32_padded = addr.substr(0, b32_len) +
"====";
310 const auto &address_bytes =
DecodeBase32(b32_padded.c_str(), &invalid);
317 m_addr.
assign(address_bytes.begin(), address_bytes.end());
324 const uint8_t *ptr =
reinterpret_cast<const uint8_t *
>(&ipv4Addr);
330 reinterpret_cast<const uint8_t *
>(&ipv6Addr),
sizeof(ipv6Addr)));
339 [](uint8_t b) { return b == 0; });
387 0x00, 0x00, 0x00, 0x00,
388 0x00, 0x00, 0x00, 0x00}});
399 0x00, 0x00, 0x00, 0x00}});
409 0x00, 0x00, 0x00, 0x00,
410 0xFF, 0xFF, 0x00, 0x00}});
416 (
m_addr[3] & 0xF0) == 0x10;
422 (
m_addr[3] & 0xF0) == 0x20;
460 static const uint8_t pchLocal[16] = {0, 0, 0, 0, 0, 0, 0, 0,
461 0, 0, 0, 0, 0, 0, 0, 1};
481 uint8_t ipNone6[16] = {};
497 if (addr == INADDR_ANY || addr == INADDR_NONE) {
564 return strprintf(
"%u.%u.%u.%u", a[0], a[1], a[2], a[3]);
572 const std::array<uint16_t, 8> groups{{
587 size_t start_index{0};
593 ZeroSpan longest, current;
594 for (
size_t i{0}; i < groups.size(); ++i) {
595 if (groups[i] != 0) {
596 current = {i + 1, 0};
600 if (current.len > longest.len) {
607 for (
size_t i{0}; i < groups.size(); ++i) {
610 if (longest.len >= 2 && i >= longest.start_index &&
611 i < longest.start_index + longest.len) {
612 if (i == longest.start_index) {
617 r +=
strprintf(
"%s%x", ((!r.empty() && r.back() !=
':') ?
":" :
""),
646 address.insert(address.end(), checksum,
766 if (asmap.size() == 0 || (net_class !=
NET_IPV4 && net_class !=
NET_IPV6)) {
770 std::vector<bool> ip_bits(128);
774 for (int8_t byte_i = 0; byte_i < 12; ++byte_i) {
775 for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
776 ip_bits[byte_i * 8 + bit_i] =
781 for (
int i = 0; i < 32; ++i) {
782 ip_bits[96 + i] = (ipv4 >> (31 - i)) & 1;
787 for (int8_t byte_i = 0; byte_i < 16; ++byte_i) {
788 uint8_t cur_byte =
m_addr[byte_i];
789 for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
790 ip_bits[byte_i * 8 + bit_i] = (cur_byte >> (7 - bit_i)) & 1;
794 uint32_t mapped_as =
Interpret(asmap, ip_bits);
809 std::vector<uint8_t> vchRet;
818 for (
int i = 0; i < 4; i++) {
819 vchRet.push_back((asn >> (8 * i)) & 0xFF);
824 vchRet.push_back(net_class);
837 vchRet.push_back((ipv4 >> 24) & 0xFF);
838 vchRet.push_back((ipv4 >> 16) & 0xFF);
851 const size_t num_bytes = nBits / 8;
857 vchRet.push_back(
m_addr[num_bytes] | ((1 << (8 - nBits)) - 1));
867 return {std::begin(serialized), std::end(serialized)};
877 if (addr ==
nullptr) {
899 return REACH_UNREACHABLE;
910 return REACH_DEFAULT;
917 return REACH_DEFAULT;
924 return fTunnel ? REACH_IPV6_WEAK : REACH_IPV6_STRONG;
929 return REACH_DEFAULT;
934 return REACH_PRIVATE;
939 return REACH_PRIVATE;
941 return REACH_DEFAULT;
946 return REACH_DEFAULT;
950 return REACH_IPV6_WEAK;
959 return REACH_DEFAULT;
963 return REACH_IPV6_WEAK;
968 return REACH_PRIVATE;
979 :
CNetAddr(ipv4Addr), port(portIn) {}
982 :
CNetAddr(ipv6Addr), port(portIn) {}
985 :
CNetAddr(addr.sin_addr), port(ntohs(addr.sin_port)) {
986 assert(addr.sin_family == AF_INET);
990 :
CNetAddr(addr.sin6_addr, addr.sin6_scope_id),
991 port(ntohs(addr.sin6_port)) {
992 assert(addr.sin6_family == AF_INET6);
996 switch (paddr->sa_family) {
999 CService(*
reinterpret_cast<const struct sockaddr_in *
>(paddr));
1003 CService(*
reinterpret_cast<const struct sockaddr_in6 *
>(paddr));
1039 if (*addrlen < (socklen_t)
sizeof(
struct sockaddr_in)) {
1042 *addrlen =
sizeof(
struct sockaddr_in);
1043 struct sockaddr_in *paddrin =
1044 reinterpret_cast<struct sockaddr_in *
>(paddr);
1045 memset(paddrin, 0, *addrlen);
1049 paddrin->sin_family = AF_INET;
1050 paddrin->sin_port = htons(
port);
1054 if (*addrlen < (socklen_t)
sizeof(
struct sockaddr_in6)) {
1057 *addrlen =
sizeof(
struct sockaddr_in6);
1058 struct sockaddr_in6 *paddrin6 =
1059 reinterpret_cast<struct sockaddr_in6 *
>(paddr);
1060 memset(paddrin6, 0, *addrlen);
1065 paddrin6->sin6_family = AF_INET6;
1066 paddrin6->sin6_port = htons(
port);
1078 key.push_back(
port / 0x100);
1080 key.push_back(
port & 0x0FF);
1117 const uint8_t bits = n < 8 ? n : 8;
1119 netmask[i] = (uint8_t)((uint8_t)0xFF << (8 - bits));
1162 bool zeros_found =
false;
1163 for (
auto b : mask.
m_addr) {
1165 if (num_bits == -1 || (zeros_found && num_bits != 0)) {
1208 for (
size_t x = 0; x < addr.
m_addr.
size(); ++x) {
uint32_t Interpret(const std::vector< bool > &asmap, const std::vector< bool > &ip)
Network GetNetClass() const
void SerializeV1Array(uint8_t(&arr)[V1_SERIALIZATION_SIZE]) const
Serialize in pre-ADDRv2/BIP155 format to an array.
std::string ToStringIP() const
prevector< ADDR_IPV6_SIZE, uint8_t > m_addr
Raw representation of the network address.
void SetIP(const CNetAddr &ip)
bool SetSpecial(const std::string &addr)
Parse a Tor or I2P address and set this object to it.
bool GetIn6Addr(struct in6_addr *pipv6Addr) const
Try to get our IPv6 address.
std::string ToString() const
bool IsCJDNS() const
Check whether this object represents a CJDNS address.
bool IsTor() const
Check whether this object represents a TOR address.
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()).
Network m_net
Network to which this address belongs.
void SetLegacyIPv6(Span< const uint8_t > ipv6)
Set from a legacy IPv6 address.
bool SetI2P(const std::string &addr)
Parse an I2P address and set this object to it.
BIP155Network GetBIP155Network() const
Get the BIP155 network id of this address.
uint32_t GetLinkedIPv4() const
For IPv4, mapped IPv4, SIIT translated IPv4, Teredo, 6to4 tunneled addresses, return the relevant IPv...
bool SetTor(const std::string &addr)
Parse a Tor address and set this object to it.
uint32_t m_scope_id
Scope id if scoped/link-local IPV6 address.
static constexpr size_t V1_SERIALIZATION_SIZE
Size of CNetAddr when serialized as ADDRv1 (pre-BIP155) (in bytes).
std::vector< uint8_t > GetGroup(const std::vector< bool > &asmap) const
Get the canonical identifier of our network group.
std::vector< uint8_t > GetAddrBytes() const
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.
uint32_t GetMappedAS(const std::vector< bool > &asmap) const
int GetReachabilityFrom(const CNetAddr *paddrPartner=nullptr) const
Calculates a metric for how reachable (*this) is from a given partner.
static constexpr size_t MAX_ADDRV2_SIZE
Maximum size of an address as defined in BIP155 (in bytes).
enum Network GetNetwork() const
CNetAddr()
Construct an unspecified IPv6 network address (::/128).
bool IsAddrV1Compatible() const
Check if the current object can be serialized in pre-ADDRv2/BIP155 format.
BIP155Network
BIP155 network ids recognized by this software.
bool IsI2P() const
Check whether this object represents an I2P address.
A hasher class for SHA-256.
CSHA256 & Write(const uint8_t *data, size_t len)
void Finalize(uint8_t hash[OUTPUT_SIZE])
A combination of a network address (CNetAddr) and a (TCP) port.
std::string ToStringIPPort() const
std::string ToString() const
std::vector< uint8_t > GetKey() const
bool SetSockAddr(const struct sockaddr *paddr)
std::string ToStringPort() const
bool GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) const
Obtain the IPv4/6 socket address this represents.
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
bool Match(const CNetAddr &addr) const
SHA3_256 & Finalize(Span< uint8_t > output)
SHA3_256 & Write(Span< const uint8_t > data)
static constexpr size_t OUTPUT_SIZE
A Span is an object that can refer to a contiguous sequence of objects.
constexpr std::size_t size() const noexcept
constexpr C * data() const noexcept
constexpr C * end() const noexcept
constexpr C * begin() const noexcept
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)
static uint16_t ReadBE16(const uint8_t *ptr)
static uint32_t ReadBE32(const uint8_t *ptr)
static const uint8_t VERSION[]
static constexpr size_t CHECKSUM_LEN
static void Checksum(Span< const uint8_t > addr_pubkey, uint8_t(&checksum)[CHECKSUM_LEN])
static constexpr size_t TOTAL_LEN
static const int NET_UNKNOWN
static int GetExtNetwork(const CNetAddr *addr)
static const int NET_TEREDO
static int NetmaskBits(uint8_t x)
bool operator==(const CNetAddr &a, const CNetAddr &b)
static std::string IPv6ToString(Span< const uint8_t > a, uint32_t scope_id)
Return an IPv6 address text representation with zero compression as described in RFC 5952 ("A Recomme...
static std::string IPv4ToString(Span< const uint8_t > a)
bool operator<(const CNetAddr &a, const CNetAddr &b)
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 constexpr size_t ADDR_TORV2_SIZE
Size of TORv2 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).
constexpr Span< A > MakeSpan(A(&a)[N])
MakeSpan for arrays:
std::vector< uint8_t > DecodeBase32(const char *p, bool *pf_invalid)
std::string ToLower(const std::string &str)
Returns the lowercase equivalent of the given string.
std::string EncodeBase32(Span< const uint8_t > input, bool pad)
Base32 encode.
bool ValidAsCString(const std::string &str) noexcept
Check if a string does not contain any embedded NUL (\0) characters.
bool HasPrefix(const T1 &obj, const std::array< uint8_t, PREFIX_LEN > &prefix)
Check whether a container begins with the given prefix.