5 #ifndef BITCOIN_NETADDRESS_H 6 #define BITCOIN_NETADDRESS_H 8 #if defined(HAVE_CONFIG_H) 76 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF
84 0xFD, 0x87, 0xD8, 0x7E, 0xEB, 0x43
93 0xFD, 0x6B, 0x88, 0xC0, 0x87, 0x24
143 explicit CNetAddr(
const struct in_addr& ipv4Addr);
201 bool GetInAddr(
struct in_addr* pipv4Addr)
const;
212 uint32_t
GetMappedAS(
const std::vector<bool> &asmap)
const;
214 std::vector<unsigned char>
GetGroup(
const std::vector<bool> &asmap)
const;
218 explicit CNetAddr(
const struct in6_addr& pipv6Addr,
const uint32_t scope = 0);
219 bool GetIn6Addr(
struct in6_addr* pipv6Addr)
const;
236 template <
typename Stream>
249 template <
typename Stream>
270 bool SetTor(
const std::string& addr);
279 bool SetI2P(
const std::string& addr);
361 template <
typename Stream>
374 template <
typename Stream>
403 template <
typename Stream>
416 template <
typename Stream>
463 s.ignore(address_size);
479 hasher.
Write(a.m_net);
480 hasher.
Write(a.m_addr.data(), a.m_addr.size());
481 return static_cast<size_t>(hasher.
Finalize());
543 if (obj.network.IsIPv4()) {
547 unsigned char dummy[12] = {0};
555 SER_READ(obj,
if (obj.valid) obj.valid = obj.SanityCheck());
568 CService(
const struct in_addr& ipv4Addr, uint16_t
port);
569 explicit CService(
const struct sockaddr_in& addr);
571 bool GetSockAddr(
struct sockaddr* paddr, socklen_t *addrlen)
const;
576 std::vector<unsigned char>
GetKey()
const;
581 CService(
const struct in6_addr& ipv6Addr, uint16_t
port);
582 explicit CService(
const struct sockaddr_in6& addr);
593 #endif // BITCOIN_NETADDRESS_H int GetReachabilityFrom(const CNetAddr *paddrPartner=nullptr) const
Calculates a metric for how reachable (*this) is from a given partner.
std::vector< unsigned char > GetGroup(const std::vector< bool > &asmap) const
Get the canonical identifier of our network group.
std::string ToStringPort() const
bool HasLinkedIPv4() const
Whether this address has a linked IPv4 address (see GetLinkedIPv4()).
void resize(size_type new_size)
bool SetNetFromBIP155Network(uint8_t possible_bip155_net, size_t address_size)
Set m_net from the provided BIP155 network id and size after validation.
uint64_t GetRand(uint64_t nMax) noexcept
Generate a uniform random integer in the range [0..range).
void assign(size_type n, const T &val)
void Serialize(Stream &s) const
Serialize to a stream.
A set of addresses that represent the hash of a string or FQDN.
CSipHasher & Write(uint64_t data)
Hash a 64-bit integer worth of data It is treated as if this was the little-endian interpretation of ...
Dummy value to indicate the number of NET_* constants.
void SetIP(const CNetAddr &ip)
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).
prevector< ADDR_IPV6_SIZE, uint8_t > m_addr
Raw representation of the network address.
void Unserialize(Stream &s)
Unserialize from a stream.
bool SetTor(const std::string &addr)
Parse a Tor address and set this object to it.
friend bool operator==(const CService &a, const CService &b)
static constexpr size_t ADDR_IPV4_SIZE
Size of IPv4 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.
bool SetI2P(const std::string &addr)
Parse an I2P address and set this object to it.
SERIALIZE_METHODS(CSubNet, obj)
CNetAddr network
Network (base) address.
size_t operator()(const CNetAddr &a) const noexcept
bool GetInAddr(struct in_addr *pipv4Addr) const
Try to get our IPv4 address.
bool SetSpecial(const std::string &addr)
Parse a Tor or I2P address and set this object to it.
std::string ToString() const
void UnserializeV1Array(uint8_t(&arr)[V1_SERIALIZATION_SIZE])
Unserialize from a pre-ADDRv2/BIP155 format from an array.
Network GetNetClass() const
void UnserializeV2Stream(Stream &s)
Unserialize from a ADDRv2 / BIP155 format.
#define READWRITEAS(type, obj)
bool SanityCheckASMap(const std::vector< bool > &asmap)
bool GetIn6Addr(struct in6_addr *pipv6Addr) const
Try to get our IPv6 address.
enum Network GetNetwork() const
static Wrapper< Formatter, T & > Using(T &&t)
Cause serialization/deserialization of an object to be done using a specified formatter class...
void UnserializeV1Stream(Stream &s)
Unserialize from a pre-ADDRv2/BIP155 format from a stream.
uint32_t m_scope_id
Scope id if scoped/link-local IPV6 address.
bool IsRelayable() const
Whether this address should be relayed to other peers even if we can't reach it ourselves.
uint32_t GetMappedAS(const std::vector< bool > &asmap) const
static constexpr size_t ADDR_CJDNS_SIZE
Size of CJDNS address (in bytes).
static constexpr int ADDRV2_FORMAT
A flag that is ORed into the protocol version to designate that addresses should be serialized in (un...
bool IsCJDNS() const
Check whether this object represents a CJDNS address.
friend bool operator!=(const CNetAddr &a, const CNetAddr &b)
friend bool operator!=(const CSubNet &a, const CSubNet &b)
bool IsI2P() const
Check whether this object represents an I2P address.
static constexpr size_t ADDR_IPV6_SIZE
Size of IPv6 address (in bytes).
static constexpr uint16_t I2P_SAM31_PORT
SAM 3.1 and earlier do not support specifying ports and force the port to 0.
void SerializeV1Array(uint8_t(&arr)[V1_SERIALIZATION_SIZE]) const
Serialize in pre-ADDRv2/BIP155 format to an array.
static constexpr size_t ADDR_I2P_SIZE
Size of I2P address (in bytes).
bool GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) const
Obtain the IPv4/6 socket address this represents.
friend bool operator!=(const CService &a, const CService &b)
std::string ToStringIP() const
BIP155Network GetBIP155Network() const
Get the BIP155 network id of this address.
BIP155Network
BIP155 network ids recognized by this software.
A combination of a network address (CNetAddr) and a (TCP) port.
bool HasPrefix(const T1 &obj, const std::array< uint8_t, PREFIX_LEN > &prefix)
Check whether a container begins with the given prefix.
void SerializeV1Stream(Stream &s) const
Serialize in pre-ADDRv2/BIP155 format to a stream.
friend bool operator<(const CService &a, const CService &b)
std::vector< unsigned char > GetKey() const
uint64_t Finalize() const
Compute the 64-bit SipHash-2-4 of the data written so far.
friend bool operator==(const CSubNet &a, const CSubNet &b)
bool valid
Is this value valid? (only used to signal parse errors)
bool Match(const CNetAddr &addr) const
uint8_t netmask[16]
Netmask, in network byte order.
friend bool operator<(const CNetAddr &a, const CNetAddr &b)
std::string ToString() const
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.
void SerializeV2Stream(Stream &s) const
Serialize as ADDRv2 / BIP155.
std::string ToStringIPPort() const
CSubNet()
Construct an invalid subnet (empty, Match() always returns false).
friend bool operator<(const CSubNet &a, const CSubNet &b)
std::string ToString() const
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).
#define SER_READ(obj, code)
A Span is an object that can refer to a contiguous sequence of objects.
uint32_t GetLinkedIPv4() const
For IPv4, mapped IPv4, SIIT translated IPv4, Teredo, 6to4 tunneled addresses, return the relevant IPv...
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.
bool IsAddrV1Compatible() const
Check if the current object can be serialized in pre-ADDRv2/BIP155 format.
SERIALIZE_METHODS(CService, obj)
bool IsTor() const
Check whether this object represents a TOR address.
static constexpr size_t V1_SERIALIZATION_SIZE
Size of CNetAddr when serialized as ADDRv1 (pre-BIP155) (in bytes).
void SetLegacyIPv6(Span< const uint8_t > ipv6)
Set from a legacy IPv6 address.
std::vector< unsigned char > GetAddrBytes() 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.