37 return BIP155Network::TORV2;
39 return BIP155Network::TORV3;
44 return BIP155Network::I2P;
46 return BIP155Network::CJDNS;
58 switch (possible_bip155_net) {
60 if (address_size == ADDR_IPV4_SIZE) {
64 throw std::ios_base::failure(
65 strprintf(
"BIP155 IPv4 address with length %u (should be %u)", address_size,
68 if (address_size == ADDR_IPV6_SIZE) {
72 throw std::ios_base::failure(
73 strprintf(
"BIP155 IPv6 address with length %u (should be %u)", address_size,
75 case BIP155Network::TORV2:
76 if (address_size == ADDR_TORV2_SIZE) {
80 throw std::ios_base::failure(
81 strprintf(
"BIP155 TORv2 address with length %u (should be %u)", address_size,
83 case BIP155Network::TORV3:
84 if (address_size == ADDR_TORV3_SIZE) {
88 throw std::ios_base::failure(
89 strprintf(
"BIP155 TORv3 address with length %u (should be %u)", address_size,
91 case BIP155Network::I2P:
92 if (address_size == ADDR_I2P_SIZE) {
96 throw std::ios_base::failure(
97 strprintf(
"BIP155 I2P address with length %u (should be %u)", address_size,
99 case BIP155Network::CJDNS:
100 if (address_size == ADDR_CJDNS_SIZE) {
104 throw std::ios_base::failure(
105 strprintf(
"BIP155 CJDNS address with length %u (should be %u)", address_size,
125 switch (ipIn.
m_net) {
139 assert(ipIn.
m_addr.
size() == ADDR_CJDNS_SIZE);
142 assert(ipIn.
m_addr.
size() == ADDR_INTERNAL_SIZE);
155 assert(ipv6.
size() == ADDR_IPV6_SIZE);
159 if (
HasPrefix(ipv6, IPV4_IN_IPV6_PREFIX)) {
162 skip =
sizeof(IPV4_IN_IPV6_PREFIX);
163 }
else if (
HasPrefix(ipv6, TORV2_IN_IPV6_PREFIX)) {
166 skip =
sizeof(TORV2_IN_IPV6_PREFIX);
167 }
else if (
HasPrefix(ipv6, INTERNAL_IN_IPV6_PREFIX)) {
170 skip =
sizeof(INTERNAL_IN_IPV6_PREFIX);
191 unsigned char hash[32] = {};
199 static constexpr
size_t CHECKSUM_LEN = 2;
200 static const unsigned char VERSION[] = {3};
201 static constexpr
size_t TOTAL_LEN = ADDR_TORV3_SIZE + CHECKSUM_LEN +
sizeof(
VERSION);
206 static const unsigned char prefix[] =
".onion checksum";
207 static constexpr
size_t prefix_len = 15;
212 hasher.
Write(addr_pubkey);
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()) {
249 case ADDR_TORV2_SIZE:
253 case torv3::TOTAL_LEN: {
256 Span<const uint8_t> input_version{input.
data() + ADDR_TORV3_SIZE + torv3::CHECKSUM_LEN,
sizeof(torv3::VERSION)};
258 uint8_t calculated_checksum[torv3::CHECKSUM_LEN];
259 torv3::Checksum(input_pubkey, calculated_checksum);
261 if (input_checksum != calculated_checksum || input_version != torv3::VERSION) {
266 m_addr.
assign(input_pubkey.begin(), input_pubkey.end());
277 const uint8_t* ptr =
reinterpret_cast<const uint8_t*
>(&ipv4Addr);
342 HasPrefix(
m_addr, std::array<uint8_t, 12>{0x00, 0x64, 0xFF, 0x9B, 0x00, 0x00,
343 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
354 0x00, 0x00, 0x00, 0x00});
365 HasPrefix(
m_addr, std::array<uint8_t, 12>{0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
366 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00});
372 (
m_addr[3] & 0xF0) == 0x10;
378 (
m_addr[3] & 0xF0) == 0x20;
410 static const unsigned char pchLocal[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
427 sizeof(IPV4_IN_IPV6_PREFIX) - 3) == 0) {
432 unsigned char ipNone6[16] = {};
445 const uint32_t addr = ReadBE32(
m_addr.
data());
446 if (addr == INADDR_ANY || addr == INADDR_NONE) {
502 assert(a.
size() == ADDR_IPV6_SIZE);
504 return strprintf(
"%x:%x:%x:%x:%x:%x:%x:%x",
522 struct sockaddr_storage sockaddr;
523 socklen_t socklen =
sizeof(sockaddr);
524 if (serv.
GetSockAddr((
struct sockaddr*)&sockaddr, &socklen)) {
525 char name[1025] =
"";
526 if (!getnameinfo((
const struct sockaddr*)&sockaddr, socklen,
name,
527 sizeof(
name),
nullptr, 0, NI_NUMERICHOST))
528 return std::string(
name);
533 return IPv6ToString(
m_addr);
537 case ADDR_TORV2_SIZE:
539 case ADDR_TORV3_SIZE: {
541 uint8_t checksum[torv3::CHECKSUM_LEN];
542 torv3::Checksum(
m_addr, checksum);
546 address.insert(address.end(), checksum, checksum + torv3::CHECKSUM_LEN);
547 address.insert(address.end(), torv3::VERSION, torv3::VERSION +
sizeof(torv3::VERSION));
557 return IPv6ToString(
m_addr);
631 return ReadBE32(
MakeSpan(
m_addr).subspan(2, ADDR_IPV4_SIZE).data());
659 if (asmap.size() == 0 || (net_class !=
NET_IPV4 && net_class !=
NET_IPV6)) {
662 std::vector<bool> ip_bits(128);
665 for (int8_t byte_i = 0; byte_i < 12; ++byte_i) {
666 for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
667 ip_bits[byte_i * 8 + bit_i] = (IPV4_IN_IPV6_PREFIX[byte_i] >> (7 - bit_i)) & 1;
671 for (
int i = 0; i < 32; ++i) {
672 ip_bits[96 + i] = (ipv4 >> (31 - i)) & 1;
677 for (int8_t byte_i = 0; byte_i < 16; ++byte_i) {
678 uint8_t cur_byte =
m_addr[byte_i];
679 for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
680 ip_bits[byte_i * 8 + bit_i] = (cur_byte >> (7 - bit_i)) & 1;
684 uint32_t mapped_as =
Interpret(asmap, ip_bits);
700 std::vector<unsigned char> vchRet;
707 for (
int i = 0; i < 4; i++) {
708 vchRet.push_back((asn >> (8 * i)) & 0xFF);
713 vchRet.push_back(net_class);
720 nBits = ADDR_INTERNAL_SIZE * 8;
726 vchRet.push_back((ipv4 >> 24) & 0xFF);
727 vchRet.push_back((ipv4 >> 16) & 0xFF);
740 const size_t num_bytes = nBits / 8;
746 vchRet.push_back(
m_addr[num_bytes] | ((1 << (8 - nBits)) - 1));
757 return {std::begin(serialized), std::end(serialized)};
766 memcpy(&nRet, &hash,
sizeof(nRet));
772 static const int NET_UNKNOWN =
NET_MAX + 0;
773 static const int NET_TEREDO =
NET_MAX + 1;
774 int static GetExtNetwork(
const CNetAddr *addr)
797 return REACH_UNREACHABLE;
799 int ourNet = GetExtNetwork(
this);
800 int theirNet = GetExtNetwork(paddrPartner);
806 default:
return REACH_DEFAULT;
811 default:
return REACH_DEFAULT;
812 case NET_TEREDO:
return REACH_TEREDO;
814 case NET_IPV6:
return fTunnel ? REACH_IPV6_WEAK : REACH_IPV6_STRONG;
818 default:
return REACH_DEFAULT;
824 default:
return REACH_DEFAULT;
825 case NET_TEREDO:
return REACH_TEREDO;
826 case NET_IPV6:
return REACH_IPV6_WEAK;
833 default:
return REACH_DEFAULT;
834 case NET_TEREDO:
return REACH_TEREDO;
835 case NET_IPV6:
return REACH_IPV6_WEAK;
860 assert(addr.sin_family == AF_INET);
865 assert(addr.sin6_family == AF_INET6);
870 switch (paddr->sa_family) {
872 *
this =
CService(*(
const struct sockaddr_in*)paddr);
875 *
this =
CService(*(
const struct sockaddr_in6*)paddr);
905 if (*addrlen < (socklen_t)
sizeof(
struct sockaddr_in))
907 *addrlen =
sizeof(
struct sockaddr_in);
908 struct sockaddr_in *paddrin = (
struct sockaddr_in*)paddr;
909 memset(paddrin, 0, *addrlen);
912 paddrin->sin_family = AF_INET;
913 paddrin->sin_port = htons(
port);
917 if (*addrlen < (socklen_t)
sizeof(
struct sockaddr_in6))
919 *addrlen =
sizeof(
struct sockaddr_in6);
920 struct sockaddr_in6 *paddrin6 = (
struct sockaddr_in6*)paddr;
921 memset(paddrin6, 0, *addrlen);
924 paddrin6->sin6_scope_id =
scopeId;
925 paddrin6->sin6_family = AF_INET6;
926 paddrin6->sin6_port = htons(
port);
935 key.push_back(
port / 0x100);
936 key.push_back(
port & 0x0FF);
967 valid = (addr.
IsIPv4() && mask <= ADDR_IPV4_SIZE * 8) ||
968 (addr.
IsIPv6() && mask <= ADDR_IPV6_SIZE * 8);
973 assert(mask <=
sizeof(
netmask) * 8);
979 const uint8_t bits = n < 8 ? n : 8;
980 netmask[i] = (uint8_t)((uint8_t)0xFF << (8 - bits));
990 static inline int NetmaskBits(uint8_t x)
1000 case 0xfe:
return 7;
1001 case 0xff:
return 8;
1013 bool zeros_found =
false;
1014 for (
auto b : mask.
m_addr) {
1015 const int num_bits = NetmaskBits(b);
1016 if (num_bits == -1 || (zeros_found && num_bits != 0)) {
1056 for (
size_t x = 0; x < addr.
m_addr.
size(); ++x) {
1074 cidr += NetmaskBits(
netmask[i]);
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 GetIn6Addr(struct in6_addr *pipv6Addr) const
Try to get our IPv6 address.
std::vector< unsigned char > GetAddrBytes() const
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
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.
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...
static constexpr size_t V1_SERIALIZATION_SIZE
Size of CNetAddr when serialized as ADDRv1 (pre-BIP155) (in bytes).
bool SetSpecial(const std::string &strName)
Parse a TOR address and set this object to it.
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)
Transform an arbitrary string into a non-routable ipv6 address.
std::vector< unsigned char > GetGroup(const std::vector< bool > &asmap) const
Get the canonical identifier of our network group.
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.
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.
std::string ToStringIPPort() const
std::string ToString() const
bool SetSockAddr(const struct sockaddr *paddr)
std::string ToStringPort() const
bool GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) 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
bool Match(const CNetAddr &addr) const
SHA3_256 & Write(Span< const unsigned char > data)
SHA3_256 & Finalize(Span< unsigned char > output)
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)
void * memcpy(void *a, const void *b, size_t c)
uint256 Hash(const T1 pbegin, const T1 pend)
Compute the 256-bit hash of an object.
bool operator==(const CNetAddr &a, const CNetAddr &b)
bool operator!=(const CNetAddr &a, const CNetAddr &b)
bool operator<(const CNetAddr &a, const CNetAddr &b)
@ 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.
constexpr Span< A > MakeSpan(A(&a)[N])
MakeSpan for arrays:
NODISCARD bool HasPrefix(const T1 &obj, const std::array< uint8_t, PREFIX_LEN > &prefix)
Check whether a container begins with the given prefix.
bool ValidAsCString(const std::string &str) noexcept
Check if a string does not contain any embedded NUL (\0) characters.
std::string EncodeBase32(Span< const unsigned char > input, bool pad)
Base32 encode.
std::vector< unsigned char > DecodeBase32(const char *p, bool *pfInvalid)