5#ifndef BITCOIN_NETADDRESS_H
6#define BITCOIN_NETADDRESS_H
62 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF};
69 0xFD, 0x87, 0xD8, 0x7E, 0xEB, 0x43};
77 0xFD, 0x6B, 0x88, 0xC0, 0x87, 0x24
133 explicit CNetAddr(
const struct in_addr& ipv4Addr);
197 bool GetInAddr(
struct in_addr* pipv4Addr)
const;
208 explicit CNetAddr(
const struct in6_addr& pipv6Addr,
const uint32_t scope = 0);
209 bool GetIn6Addr(
struct in6_addr* pipv6Addr)
const;
237 template <
typename Stream>
250 template <
typename Stream>
282 bool SetTor(
const std::string& addr);
291 bool SetI2P(
const std::string& addr);
361 template <
typename Stream>
374 template <
typename Stream>
409 template <
typename Stream>
422 template <
typename Stream>
469 s.ignore(address_size);
538 CService(
const struct in_addr& ipv4Addr, uint16_t
port);
539 explicit CService(
const struct sockaddr_in& addr);
541 bool GetSockAddr(
struct sockaddr* paddr, socklen_t* addrlen)
const;
551 std::vector<unsigned char>
GetKey()
const;
554 CService(
const struct in6_addr& ipv6Addr, uint16_t
port);
555 explicit CService(
const struct sockaddr_in6& addr);
580 hasher.
Write(a.m_net);
581 hasher.
Write(a.port);
582 hasher.
Write(a.m_addr);
583 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 ToStringAddr() 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 (or CJDNS) address.
std::vector< unsigned char > GetAddrBytes() const
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()).
bool HasCJDNSPrefix() const
Network m_net
Network to which this address belongs.
bool IsPrivacyNet() const
Whether this object is a privacy network.
void SetLegacyIPv6(Span< const uint8_t > ipv6)
Set from a legacy IPv6 address.
static constexpr SerParams V1
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).
static constexpr SerParams V2
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)
Create an "internal" address that represents a name or FQDN.
friend bool operator!=(const CNetAddr &a, const CNetAddr &b)
static constexpr size_t MAX_ADDRV2_SIZE
Maximum size of an address as defined in BIP155 (in bytes).
enum Network GetNetwork() const
int GetReachabilityFrom(const CNetAddr &paddrPartner) const
Calculates a metric for how reachable (*this) is from a given partner.
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.
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)
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)
sa_family_t GetSAFamily() const
Get the address family.
friend CService MaybeFlipIPv6toCJDNS(const CService &service)
If an IPv6 address belongs to the address range used by the CJDNS network and the CJDNS network is re...
bool GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) const
Obtain the IPv4/6 socket address this represents.
std::string ToStringAddrPort() const
std::vector< unsigned char > GetKey() const
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 It is treated as if this was the little-endian interpretation of ...
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
friend bool operator<(const CSubNet &a, const CSubNet &b)
CSubNet()
Construct an invalid subnet (empty, Match() always returns false).
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)
static CService ip(uint32_t i)
bool HasPrefix(const T1 &obj, const std::array< uint8_t, PREFIX_LEN > &prefix)
Check whether a container begins with the given prefix.
static constexpr uint8_t CJDNS_PREFIX
All CJDNS addresses start with 0xFC.
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).
std::string OnionToString(Span< const uint8_t > addr)
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).
#define SER_PARAMS_OPFUNC
Helper macro for SerParams structs.
static Wrapper< Formatter, T & > Using(T &&t)
Cause serialization/deserialization of an object to be done using a specified formatter class.