5#ifndef BITCOIN_NETADDRESS_H
6#define BITCOIN_NETADDRESS_H
8#if defined(HAVE_CONFIG_H)
9#include <config/bitcoin-config.h>
75 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF}};
82 {0xFD, 0x87, 0xD8, 0x7E, 0xEB, 0x43}};
91 {0xFD, 0x6B, 0x88, 0xC0, 0x87, 0x24}};
142 explicit CNetAddr(
const struct in_addr &ipv4Addr);
220 bool GetInAddr(
struct in_addr *pipv4Addr)
const;
232 uint32_t
GetMappedAS(
const std::vector<bool> &asmap)
const;
234 std::vector<uint8_t>
GetGroup(
const std::vector<bool> &asmap)
const;
238 explicit CNetAddr(
const struct in6_addr &pipv6Addr,
239 const uint32_t scope = 0);
240 bool GetIn6Addr(
struct in6_addr *pipv6Addr)
const;
257 template <
typename Stream>
void Serialize(Stream &s)
const {
287 bool SetTor(
const std::string &addr);
296 bool SetI2P(
const std::string &addr);
340 size_t address_size);
484 s.ignore(address_size);
527 if (obj.network.IsIPv4()) {
531 uint8_t dummy[12] = {0};
532 auto netmask_span =
Span{obj.netmask};
540 SER_READ(obj,
if (obj.valid) obj.valid = obj.SanityCheck());
553 CService(
const struct in_addr &ipv4Addr, uint16_t
port);
554 explicit CService(
const struct sockaddr_in &addr);
556 bool GetSockAddr(
struct sockaddr *paddr, socklen_t *addrlen)
const;
563 std::vector<uint8_t>
GetKey()
const;
568 CService(
const struct in6_addr &ipv6Addr, uint16_t
port);
569 explicit CService(
const struct sockaddr_in6 &addr);
589 hasher.
Write(a.m_net);
590 hasher.
Write(a.port);
591 hasher.
Write(a.m_addr.data(), a.m_addr.size());
592 return static_cast<size_t>(hasher.
Finalize());
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 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.
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...
bool SetTor(const std::string &addr)
Parse a Tor address and set this object to it.
void SerializeV1Stream(Stream &s) const
Serialize in pre-ADDRv2/BIP155 format to a stream.
uint32_t m_scope_id
Scope id if scoped/link-local IPV6 address.
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).
void UnserializeV1Stream(Stream &s)
Unserialize from a pre-ADDRv2/BIP155 format from a stream.
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.
friend bool operator!=(const CNetAddr &a, const CNetAddr &b)
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.
size_t operator()(const CService &a) const noexcept
CServiceHash(uint64_t salt_k0, uint64_t salt_k1)
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)
std::vector< uint8_t > GetKey() const
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
Obtain the IPv4/6 socket address this represents.
uint64_t Finalize() const
Compute the 64-bit SipHash-2-4 of the data written so far.
CSipHasher & Write(uint64_t data)
Hash a 64-bit integer worth of data.
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
void resize(size_type new_size)
void assign(size_type n, const T &val)
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...
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.
static constexpr uint16_t I2P_SAM31_PORT
SAM 3.1 and earlier do not support specifying ports and force the port to 0.
@ 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).
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
#define READWRITEAS(type, obj)
#define SER_READ(obj, code)
static Wrapper< Formatter, T & > Using(T &&t)
Cause serialization/deserialization of an object to be done using a specified formatter class.
bool HasPrefix(const T1 &obj, const std::array< uint8_t, PREFIX_LEN > &prefix)
Check whether a container begins with the given prefix.