6 #ifndef PIVX_NETADDRESS_H
7 #define PIVX_NETADDRESS_H
9 #if defined(HAVE_CONFIG_H)
32 static constexpr
int ADDRV2_FORMAT = 0x20000000;
73 static const std::array<uint8_t, 12> IPV4_IN_IPV6_PREFIX{
74 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF
81 static const std::array<uint8_t, 6> TORV2_IN_IPV6_PREFIX{
82 0xFD, 0x87, 0xD8, 0x7E, 0xEB, 0x43
90 static const std::array<uint8_t, 6> INTERNAL_IN_IPV6_PREFIX{
91 0xFD, 0x6B, 0x88, 0xC0, 0x87, 0x24
95 static constexpr
size_t ADDR_IPV4_SIZE = 4;
98 static constexpr
size_t ADDR_IPV6_SIZE = 16;
101 static constexpr
size_t ADDR_TORV2_SIZE = 10;
105 static constexpr
size_t ADDR_TORV3_SIZE = 32;
108 static constexpr
size_t ADDR_I2P_SIZE = 32;
111 static constexpr
size_t ADDR_CJDNS_SIZE = 16;
114 static constexpr
size_t ADDR_INTERNAL_SIZE = 10;
137 explicit CNetAddr(
const struct in_addr& ipv4Addr);
190 bool GetInAddr(
struct in_addr* pipv4Addr)
const;
201 uint32_t
GetMappedAS(
const std::vector<bool> &asmap)
const;
203 std::vector<unsigned char>
GetGroup(
const std::vector<bool> &asmap)
const;
207 explicit CNetAddr(
const struct in6_addr& pipv6Addr,
const uint32_t scope = 0);
208 bool GetIn6Addr(
struct in6_addr* pipv6Addr)
const;
225 template <
typename Stream>
228 if (s.GetVersion() & ADDRV2_FORMAT) {
238 template <
typename Stream>
241 if (s.GetVersion() & ADDRV2_FORMAT) {
304 prefix_size =
sizeof(IPV4_IN_IPV6_PREFIX);
305 assert(prefix_size +
m_addr.
size() ==
sizeof(arr));
306 memcpy(arr, IPV4_IN_IPV6_PREFIX.data(), prefix_size);
313 prefix_size =
sizeof(TORV2_IN_IPV6_PREFIX);
314 assert(prefix_size +
m_addr.
size() ==
sizeof(arr));
315 memcpy(arr, TORV2_IN_IPV6_PREFIX.data(), prefix_size);
319 prefix_size =
sizeof(INTERNAL_IN_IPV6_PREFIX);
320 assert(prefix_size +
m_addr.
size() ==
sizeof(arr));
321 memcpy(arr, INTERNAL_IN_IPV6_PREFIX.data(), prefix_size);
340 template <
typename Stream>
353 template <
typename Stream>
382 template <
typename Stream>
395 template <
typename Stream>
442 s.ignore(address_size);
491 CService(
const struct in_addr& ipv4Addr, uint16_t
port);
492 explicit CService(
const struct sockaddr_in& addr);
494 bool GetSockAddr(
struct sockaddr* paddr, socklen_t* addrlen)
const;
499 std::vector<unsigned char>
GetKey()
const;
504 CService(
const struct in6_addr& ipv6Addr, uint16_t
port);
505 explicit CService(
const struct sockaddr_in6& addr);
Network GetNetClass() const
void SerializeV1Array(uint8_t(&arr)[V1_SERIALIZATION_SIZE]) const
Serialize in pre-ADDRv2/BIP155 format to an array.
bool IsRelayable() const
Whether this address should be relayed to other peers even if we can't reach it ourselves.
std::string ToStringIP() const
void SerializeV2Stream(Stream &s) const
Serialize as ADDRv2 / BIP155.
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.
void UnserializeV1Array(uint8_t(&arr)[V1_SERIALIZATION_SIZE])
Unserialize from a pre-ADDRv2/BIP155 format from an array.
friend bool operator==(const CNetAddr &a, const CNetAddr &b)
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...
void SerializeV1Stream(Stream &s) const
Serialize in pre-ADDRv2/BIP155 format to a stream.
void Serialize(Stream &s) const
Serialize to a stream.
void Unserialize(Stream &s)
Unserialize from a stream.
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.
void UnserializeV1Stream(Stream &s)
Unserialize from a pre-ADDRv2/BIP155 format from a stream.
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.
friend bool operator!=(const CNetAddr &a, const CNetAddr &b)
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).
void UnserializeV2Stream(Stream &s)
Unserialize from a ADDRv2 / BIP155 format.
friend bool operator<(const CNetAddr &a, const CNetAddr &b)
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 combination of a network address (CNetAddr) and a (TCP) port.
SERIALIZE_METHODS(CService, obj)
std::string ToStringIPPort() const
std::string ToString() const
friend bool operator<(const CService &a, const CService &b)
friend bool operator!=(const CService &a, const CService &b)
bool SetSockAddr(const struct sockaddr *paddr)
friend bool operator==(const CService &a, const CService &b)
std::string ToStringPort() const
bool GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) const
std::vector< unsigned char > GetKey() const
friend bool operator!=(const CSubNet &a, const CSubNet &b)
bool valid
Is this value valid? (only used to signal parse errors)
CNetAddr network
Network (base) address.
friend bool operator==(const CSubNet &a, const CSubNet &b)
uint8_t netmask[16]
Netmask, in network byte order.
std::string ToString() const
SERIALIZE_METHODS(CSubNet, obj)
friend bool operator<(const CSubNet &a, const CSubNet &b)
bool Match(const CNetAddr &addr) const
A Span is an object that can refer to a contiguous sequence of objects.
void resize(size_type new_size)
void assign(size_type n, const T &val)
void * memcpy(void *a, const void *b, size_t c)
void * memmove(void *a, const void *b, size_t c)
@ 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.
#define READWRITEAS(type, obj)
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.