5 #ifndef BITCOIN_NETADDRESS_H
6 #define BITCOIN_NETADDRESS_H
8 #if defined(HAVE_CONFIG_H)
74 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
141 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>
238 template <
typename Stream>
300 assert(
m_addr.size() ==
sizeof(arr));
305 assert(prefix_size +
m_addr.size() ==
sizeof(arr));
314 assert(prefix_size +
m_addr.size() ==
sizeof(arr));
320 assert(prefix_size +
m_addr.size() ==
sizeof(arr));
334 memset(arr, 0x0, V1_SERIALIZATION_SIZE);
340 template <
typename Stream>
353 template <
typename Stream>
382 template <
typename Stream>
395 template <
typename Stream>
404 if (address_size > MAX_ADDRV2_SIZE) {
406 "Address too long: %u > %u", address_size, MAX_ADDRV2_SIZE));
412 m_addr.resize(address_size);
442 s.ignore(address_size);
506 if (obj.network.IsIPv4()) {
510 unsigned char dummy[12] = {0};
518 SER_READ(obj,
if (obj.valid) obj.valid = obj.SanityCheck());
531 CService(
const struct in_addr& ipv4Addr, uint16_t port);
532 explicit CService(
const struct sockaddr_in& addr);
534 bool GetSockAddr(
struct sockaddr* paddr, socklen_t *addrlen)
const;
539 std::vector<unsigned char>
GetKey()
const;
544 CService(
const struct in6_addr& ipv6Addr, uint16_t port);
545 explicit CService(
const struct sockaddr_in6& addr);
556 #endif // BITCOIN_NETADDRESS_H
bool SetNetFromBIP155Network(uint8_t possible_bip155_net, size_t address_size)
Set m_net from the provided BIP155 network id and size after validation.
A set of addresses that represent the hash of a string or FQDN.
Dummy value to indicate the number of NET_* constants.
void SetIP(const CNetAddr &ip)
std::string ToStringIP() const
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).
bool GetIn6Addr(struct in6_addr *pipv6Addr) const
Try to get our IPv6 address.
prevector< ADDR_IPV6_SIZE, uint8_t > m_addr
Raw representation of the network address.
void Unserialize(Stream &s)
Unserialize from a stream.
std::string ToStringIPPort() const
uint32_t GetMappedAS(const std::vector< bool > &asmap) const
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.
BIP155Network GetBIP155Network() const
Get the BIP155 network id of this address.
void SerializeV2Stream(Stream &s) const
Serialize as ADDRv2 / BIP155.
std::string ToString() const
SERIALIZE_METHODS(CSubNet, obj)
CNetAddr network
Network (base) address.
void UnserializeV1Array(uint8_t(&arr)[V1_SERIALIZATION_SIZE])
Unserialize from a pre-ADDRv2/BIP155 format from an array.
bool Match(const CNetAddr &addr) const
void UnserializeV2Stream(Stream &s)
Unserialize from a ADDRv2 / BIP155 format.
#define READWRITEAS(type, obj)
bool SanityCheckASMap(const std::vector< bool > &asmap)
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.
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...
int GetReachabilityFrom(const CNetAddr *paddrPartner=nullptr) const
Calculates a metric for how reachable (*this) is from a given partner.
friend bool operator!=(const CNetAddr &a, const CNetAddr &b)
friend bool operator!=(const CSubNet &a, const CSubNet &b)
static constexpr size_t ADDR_IPV6_SIZE
Size of IPv6 address (in bytes).
static constexpr size_t ADDR_I2P_SIZE
Size of I2P address (in bytes).
static constexpr size_t ADDR_TORV2_SIZE
Size of TORv2 address (in bytes).
friend bool operator!=(const CService &a, const CService &b)
BIP155Network
BIP155 network ids recognized by this software.
bool HasLinkedIPv4() const
Whether this address has a linked IPv4 address (see GetLinkedIPv4()).
A combination of a network address (CNetAddr) and a (TCP) port.
void SerializeV1Array(uint8_t(&arr)[V1_SERIALIZATION_SIZE]) const
Serialize in pre-ADDRv2/BIP155 format to an array.
friend bool operator<(const CService &a, const CService &b)
std::string ToString() const
bool IsTor() const
Check whether this object represents a TOR address.
bool IsI2P() const
Check whether this object represents an I2P address.
friend bool operator==(const CSubNet &a, const CSubNet &b)
bool valid
Is this value valid? (only used to signal parse errors)
bool GetInAddr(struct in_addr *pipv4Addr) const
Try to get our IPv4 address.
bool IsAddrV1Compatible() const
Check if the current object can be serialized in pre-ADDRv2/BIP155 format.
std::vector< unsigned char > GetGroup(const std::vector< bool > &asmap) const
Get the canonical identifier of our network group.
uint8_t netmask[16]
Netmask, in network byte order.
friend bool operator<(const CNetAddr &a, const CNetAddr &b)
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 * memcpy(void *a, const void *b, size_t c)
CSubNet()
Construct an invalid subnet (empty, Match() always returns false).
void Serialize(Stream &s) const
Serialize to a stream.
uint32_t GetLinkedIPv4() const
For IPv4, mapped IPv4, SIIT translated IPv4, Teredo, 6to4 tunneled addresses, return the relevant IPv...
friend bool operator<(const CSubNet &a, const CSubNet &b)
Network GetNetClass() const
NODISCARD bool HasPrefix(const T1 &obj, const std::array< uint8_t, PREFIX_LEN > &prefix)
Check whether a container begins with the given prefix.
bool SetSpecial(const std::string &strName)
Parse a TOR address and set this object to it.
void SerializeV1Stream(Stream &s) const
Serialize in pre-ADDRv2/BIP155 format to a stream.
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)
std::string ToString() const
A Span is an object that can refer to a contiguous sequence of objects.
std::string ToStringPort() const
std::vector< unsigned char > GetAddrBytes() const
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.
void * memmove(void *a, const void *b, size_t c)
bool IsCJDNS() const
Check whether this object represents a CJDNS address.
SERIALIZE_METHODS(CService, obj)
static constexpr size_t V1_SERIALIZATION_SIZE
Size of CNetAddr when serialized as ADDRv1 (pre-BIP155) (in bytes).
bool GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) const
Obtain the IPv4/6 socket address this represents.
void SetLegacyIPv6(Span< const uint8_t > ipv6)
Set from a legacy IPv6 address.
bool IsRelayable() const
Whether this address should be relayed to other peers even if we can't reach it ourselves.
std::vector< unsigned char > GetKey() const
enum Network GetNetwork() 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.