Bitcoin Core  21.99.0
P2P Digital Currency
Public Member Functions | Protected Attributes | Private Types | Private Member Functions | Static Private Attributes | Friends | List of all members
CNetAddr Class Reference

Network address. More...

#include <netaddress.h>

Inheritance diagram for CNetAddr:
[legend]
Collaboration diagram for CNetAddr:
[legend]

Public Member Functions

 CNetAddr ()
 Construct an unspecified IPv6 network address (::/128). More...
 
 CNetAddr (const struct in_addr &ipv4Addr)
 
void SetIP (const CNetAddr &ip)
 
void SetLegacyIPv6 (Span< const uint8_t > ipv6)
 Set from a legacy IPv6 address. More...
 
bool SetInternal (const std::string &name)
 Create an "internal" address that represents a name or FQDN. More...
 
bool SetSpecial (const std::string &addr)
 Parse a Tor or I2P address and set this object to it. More...
 
bool IsBindAny () const
 
bool IsIPv4 () const
 
bool IsIPv6 () const
 
bool IsRFC1918 () const
 
bool IsRFC2544 () const
 
bool IsRFC6598 () const
 
bool IsRFC5737 () const
 
bool IsRFC3849 () const
 
bool IsRFC3927 () const
 
bool IsRFC3964 () const
 
bool IsRFC4193 () const
 
bool IsRFC4380 () const
 
bool IsRFC4843 () const
 
bool IsRFC7343 () const
 
bool IsRFC4862 () const
 
bool IsRFC6052 () const
 
bool IsRFC6145 () const
 
bool IsHeNet () const
 
bool IsTor () const
 Check whether this object represents a TOR address. More...
 
bool IsI2P () const
 Check whether this object represents an I2P address. More...
 
bool IsCJDNS () const
 Check whether this object represents a CJDNS address. More...
 
bool IsLocal () const
 
bool IsRoutable () const
 
bool IsInternal () const
 
bool IsValid () const
 
bool IsAddrV1Compatible () const
 Check if the current object can be serialized in pre-ADDRv2/BIP155 format. More...
 
enum Network GetNetwork () const
 
std::string ToString () const
 
std::string ToStringIP () const
 
uint64_t GetHash () const
 
bool GetInAddr (struct in_addr *pipv4Addr) const
 Try to get our IPv4 address. More...
 
Network GetNetClass () const
 
uint32_t GetLinkedIPv4 () const
 For IPv4, mapped IPv4, SIIT translated IPv4, Teredo, 6to4 tunneled addresses, return the relevant IPv4 address as a uint32. More...
 
bool HasLinkedIPv4 () const
 Whether this address has a linked IPv4 address (see GetLinkedIPv4()). More...
 
uint32_t GetMappedAS (const std::vector< bool > &asmap) const
 
std::vector< unsigned char > GetGroup (const std::vector< bool > &asmap) const
 Get the canonical identifier of our network group. More...
 
std::vector< unsigned char > GetAddrBytes () const
 
int GetReachabilityFrom (const CNetAddr *paddrPartner=nullptr) const
 Calculates a metric for how reachable (*this) is from a given partner. More...
 
 CNetAddr (const struct in6_addr &pipv6Addr, const uint32_t scope=0)
 
bool GetIn6Addr (struct in6_addr *pipv6Addr) const
 Try to get our IPv6 address. More...
 
bool IsRelayable () const
 Whether this address should be relayed to other peers even if we can't reach it ourselves. More...
 
template<typename Stream >
void Serialize (Stream &s) const
 Serialize to a stream. More...
 
template<typename Stream >
void Unserialize (Stream &s)
 Unserialize from a stream. More...
 

Protected Attributes

prevector< ADDR_IPV6_SIZE, uint8_t > m_addr {ADDR_IPV6_SIZE, 0x0}
 Raw representation of the network address. More...
 
Network m_net {NET_IPV6}
 Network to which this address belongs. More...
 
uint32_t m_scope_id {0}
 Scope id if scoped/link-local IPV6 address. More...
 

Private Types

enum  BIP155Network : uint8_t {
  IPV4 = 1, IPV6 = 2, TORV2 = 3, TORV3 = 4,
  I2P = 5, CJDNS = 6
}
 BIP155 network ids recognized by this software. More...
 

Private Member Functions

bool SetTor (const std::string &addr)
 Parse a Tor address and set this object to it. More...
 
bool SetI2P (const std::string &addr)
 Parse an I2P address and set this object to it. More...
 
BIP155Network GetBIP155Network () const
 Get the BIP155 network id of this address. More...
 
bool SetNetFromBIP155Network (uint8_t possible_bip155_net, size_t address_size)
 Set m_net from the provided BIP155 network id and size after validation. More...
 
void SerializeV1Array (uint8_t(&arr)[V1_SERIALIZATION_SIZE]) const
 Serialize in pre-ADDRv2/BIP155 format to an array. More...
 
template<typename Stream >
void SerializeV1Stream (Stream &s) const
 Serialize in pre-ADDRv2/BIP155 format to a stream. More...
 
template<typename Stream >
void SerializeV2Stream (Stream &s) const
 Serialize as ADDRv2 / BIP155. More...
 
void UnserializeV1Array (uint8_t(&arr)[V1_SERIALIZATION_SIZE])
 Unserialize from a pre-ADDRv2/BIP155 format from an array. More...
 
template<typename Stream >
void UnserializeV1Stream (Stream &s)
 Unserialize from a pre-ADDRv2/BIP155 format from a stream. More...
 
template<typename Stream >
void UnserializeV2Stream (Stream &s)
 Unserialize from a ADDRv2 / BIP155 format. More...
 

Static Private Attributes

static constexpr size_t V1_SERIALIZATION_SIZE = ADDR_IPV6_SIZE
 Size of CNetAddr when serialized as ADDRv1 (pre-BIP155) (in bytes). More...
 
static constexpr size_t MAX_ADDRV2_SIZE = 512
 Maximum size of an address as defined in BIP155 (in bytes). More...
 

Friends

class CSubNet
 
bool operator== (const CNetAddr &a, const CNetAddr &b)
 
bool operator!= (const CNetAddr &a, const CNetAddr &b)
 
bool operator< (const CNetAddr &a, const CNetAddr &b)
 

Detailed Description

Network address.

Definition at line 119 of file netaddress.h.

Member Enumeration Documentation

◆ BIP155Network

enum CNetAddr::BIP155Network : uint8_t
private

BIP155 network ids recognized by this software.

Enumerator
IPV4 
IPV6 
TORV2 
TORV3 
I2P 
CJDNS 

Definition at line 282 of file netaddress.h.

Constructor & Destructor Documentation

◆ CNetAddr() [1/3]

CNetAddr::CNetAddr ( )

Construct an unspecified IPv6 network address (::/128).

Note
This address is considered invalid by CNetAddr::IsValid()

Definition at line 120 of file netaddress.cpp.

◆ CNetAddr() [2/3]

CNetAddr::CNetAddr ( const struct in_addr &  ipv4Addr)
explicit

Definition at line 315 of file netaddress.cpp.

Here is the call graph for this function:

◆ CNetAddr() [3/3]

CNetAddr::CNetAddr ( const struct in6_addr &  pipv6Addr,
const uint32_t  scope = 0 
)
explicit

Definition at line 322 of file netaddress.cpp.

Here is the call graph for this function:

Member Function Documentation

◆ GetAddrBytes()

std::vector< unsigned char > CNetAddr::GetAddrBytes ( ) const

Definition at line 811 of file netaddress.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetBIP155Network()

CNetAddr::BIP155Network CNetAddr::GetBIP155Network ( ) const
private

Get the BIP155 network id of this address.

Must not be called for IsInternal() objects.

Returns
BIP155 network id

Definition at line 27 of file netaddress.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetGroup()

std::vector< unsigned char > CNetAddr::GetGroup ( const std::vector< bool > &  asmap) const

Get the canonical identifier of our network group.

The groups are assigned in a way where it should be costly for an attacker to obtain addresses with many different group identifiers, even if it is cheap to obtain addresses with the same identifier.

Note
No two connections will be attempted to addresses with the same network group.

Definition at line 757 of file netaddress.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetHash()

uint64_t CNetAddr::GetHash ( ) const

Definition at line 821 of file netaddress.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetIn6Addr()

bool CNetAddr::GetIn6Addr ( struct in6_addr *  pipv6Addr) const

Try to get our IPv6 address.

Parameters
[out]pipv6AddrThe in6_addr struct to which to copy.
Returns
Whether or not the operation was successful, in particular, whether or not our address was an IPv6 address.
See also
CNetAddr::IsIPv6()

Definition at line 666 of file netaddress.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetInAddr()

bool CNetAddr::GetInAddr ( struct in_addr *  pipv4Addr) const

Try to get our IPv4 address.

Parameters
[out]pipv4AddrThe in_addr struct to which to copy.
Returns
Whether or not the operation was successful, in particular, whether or not our address was an IPv4 address.
See also
CNetAddr::IsIPv4()

Definition at line 647 of file netaddress.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetLinkedIPv4()

uint32_t CNetAddr::GetLinkedIPv4 ( ) const

For IPv4, mapped IPv4, SIIT translated IPv4, Teredo, 6to4 tunneled addresses, return the relevant IPv4 address as a uint32.

Definition at line 681 of file netaddress.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetMappedAS()

uint32_t CNetAddr::GetMappedAS ( const std::vector< bool > &  asmap) const

Definition at line 716 of file netaddress.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetNetClass()

Network CNetAddr::GetNetClass ( ) const

Definition at line 698 of file netaddress.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetNetwork()

enum Network CNetAddr::GetNetwork ( ) const

Definition at line 543 of file netaddress.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetReachabilityFrom()

int CNetAddr::GetReachabilityFrom ( const CNetAddr paddrPartner = nullptr) const

Calculates a metric for how reachable (*this) is from a given partner.

Definition at line 843 of file netaddress.cpp.

Here is the call graph for this function:

◆ HasLinkedIPv4()

bool CNetAddr::HasLinkedIPv4 ( ) const

Whether this address has a linked IPv4 address (see GetLinkedIPv4()).

Definition at line 676 of file netaddress.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsAddrV1Compatible()

bool CNetAddr::IsAddrV1Compatible ( ) const

Check if the current object can be serialized in pre-ADDRv2/BIP155 format.

Definition at line 523 of file netaddress.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsBindAny()

bool CNetAddr::IsBindAny ( ) const

Definition at line 328 of file netaddress.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsCJDNS()

bool CNetAddr::IsCJDNS ( ) const

Check whether this object represents a CJDNS address.

Definition at line 441 of file netaddress.cpp.

Here is the caller graph for this function:

◆ IsHeNet()

bool CNetAddr::IsHeNet ( ) const

Definition at line 422 of file netaddress.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsI2P()

bool CNetAddr::IsI2P ( ) const

Check whether this object represents an I2P address.

Definition at line 436 of file netaddress.cpp.

Here is the caller graph for this function:

◆ IsInternal()

bool CNetAddr::IsInternal ( ) const
Returns
Whether or not this is a dummy address that represents a name.
See also
CNetAddr::SetInternal(const std::string &)

Definition at line 518 of file netaddress.cpp.

Here is the caller graph for this function:

◆ IsIPv4()

bool CNetAddr::IsIPv4 ( ) const

Definition at line 336 of file netaddress.cpp.

Here is the caller graph for this function:

◆ IsIPv6()

bool CNetAddr::IsIPv6 ( ) const

Definition at line 338 of file netaddress.cpp.

Here is the caller graph for this function:

◆ IsLocal()

bool CNetAddr::IsLocal ( ) const

Definition at line 443 of file netaddress.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsRelayable()

bool CNetAddr::IsRelayable ( ) const
inline

Whether this address should be relayed to other peers even if we can't reach it ourselves.

Definition at line 226 of file netaddress.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsRFC1918()

bool CNetAddr::IsRFC1918 ( ) const

Definition at line 340 of file netaddress.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsRFC2544()

bool CNetAddr::IsRFC2544 ( ) const

Definition at line 348 of file netaddress.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsRFC3849()

bool CNetAddr::IsRFC3849 ( ) const

Definition at line 370 of file netaddress.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsRFC3927()

bool CNetAddr::IsRFC3927 ( ) const

Definition at line 353 of file netaddress.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsRFC3964()

bool CNetAddr::IsRFC3964 ( ) const

Definition at line 375 of file netaddress.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsRFC4193()

bool CNetAddr::IsRFC4193 ( ) const

Definition at line 398 of file netaddress.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsRFC4380()

bool CNetAddr::IsRFC4380 ( ) const

Definition at line 387 of file netaddress.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsRFC4843()

bool CNetAddr::IsRFC4843 ( ) const

Definition at line 410 of file netaddress.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsRFC4862()

bool CNetAddr::IsRFC4862 ( ) const

Definition at line 392 of file netaddress.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsRFC5737()

bool CNetAddr::IsRFC5737 ( ) const

Definition at line 363 of file netaddress.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsRFC6052()

bool CNetAddr::IsRFC6052 ( ) const

Definition at line 380 of file netaddress.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsRFC6145()

bool CNetAddr::IsRFC6145 ( ) const

Definition at line 403 of file netaddress.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsRFC6598()

bool CNetAddr::IsRFC6598 ( ) const

Definition at line 358 of file netaddress.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsRFC7343()

bool CNetAddr::IsRFC7343 ( ) const

Definition at line 416 of file netaddress.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsRoutable()

bool CNetAddr::IsRoutable ( ) const
Returns
Whether or not this network address is publicly routable on the global internet.
Note
A routable address is always valid. As in, the set of routable addresses is a subset of the set of valid addresses.
See also
CNetAddr::IsValid()

Definition at line 508 of file netaddress.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsTor()

bool CNetAddr::IsTor ( ) const

Check whether this object represents a TOR address.

See also
CNetAddr::SetSpecial(const std::string &)

Definition at line 431 of file netaddress.cpp.

Here is the caller graph for this function:

◆ IsValid()

bool CNetAddr::IsValid ( ) const
Returns
Whether or not this network address is a valid address that could be used to refer to an actual host.
Note
A valid address may or may not be publicly routable on the global internet. As in, the set of valid addresses is a superset of the set of publicly routable addresses.
See also
CNetAddr::IsRoutable()

Definition at line 469 of file netaddress.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Serialize()

template<typename Stream >
void CNetAddr::Serialize ( Stream &  s) const
inline

Serialize to a stream.

Definition at line 235 of file netaddress.h.

Here is the call graph for this function:

◆ SerializeV1Array()

void CNetAddr::SerializeV1Array ( uint8_t(&)  arr[V1_SERIALIZATION_SIZE]) const
inlineprivate

Serialize in pre-ADDRv2/BIP155 format to an array.

Definition at line 322 of file netaddress.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SerializeV1Stream()

template<typename Stream >
void CNetAddr::SerializeV1Stream ( Stream &  s) const
inlineprivate

Serialize in pre-ADDRv2/BIP155 format to a stream.

Definition at line 369 of file netaddress.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SerializeV2Stream()

template<typename Stream >
void CNetAddr::SerializeV2Stream ( Stream &  s) const
inlineprivate

Serialize as ADDRv2 / BIP155.

Definition at line 382 of file netaddress.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetI2P()

bool CNetAddr::SetI2P ( const std::string &  addr)
private

Parse an I2P address and set this object to it.

Parameters
[in]addrAddress to parse, must be a valid C string, for example ukeu3k5oycgaauneqgtnvselmt4yemvoilkln7jpvamvfx7dnkdq.b32.i2p.
Returns
Whether the operation was successful.
See also
CNetAddr::IsI2P()

Definition at line 287 of file netaddress.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetInternal()

bool CNetAddr::SetInternal ( const std::string &  name)

Create an "internal" address that represents a name or FQDN.

CAddrMan uses these fake addresses to keep track of which DNS seeds were used.

Returns
Whether or not the operation was successful.
See also
NET_INTERNAL, INTERNAL_IN_IPV6_PREFIX, CNetAddr::IsInternal(), CNetAddr::IsRFC4193()

Definition at line 185 of file netaddress.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetIP()

void CNetAddr::SetIP ( const CNetAddr ip)

Definition at line 122 of file netaddress.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetLegacyIPv6()

void CNetAddr::SetLegacyIPv6 ( Span< const uint8_t >  ipv6)

Set from a legacy IPv6 address.

Legacy IPv6 address may be a normal IPv6 address, or another address (e.g. IPv4) disguised as IPv6. This encoding is used in the legacy addr encoding.

Definition at line 153 of file netaddress.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetNetFromBIP155Network()

bool CNetAddr::SetNetFromBIP155Network ( uint8_t  possible_bip155_net,
size_t  address_size 
)
private

Set m_net from the provided BIP155 network id and size after validation.

Return values
truethe network was recognized, is valid and m_net was set
falsenot recognised (from future?) and should be silently ignored
Exceptions
std::ios_base::failureif the network is one of the BIP155 founding networks (id 1..6) with wrong address size.

Definition at line 56 of file netaddress.cpp.

Here is the caller graph for this function:

◆ SetSpecial()

bool CNetAddr::SetSpecial ( const std::string &  addr)

Parse a Tor or I2P address and set this object to it.

Parameters
[in]addrAddress to parse, for example pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion or ukeu3k5oycgaauneqgtnvselmt4yemvoilkln7jpvamvfx7dnkdq.b32.i2p.
Returns
Whether the operation was successful.
See also
CNetAddr::IsTor(), CNetAddr::IsI2P()

Definition at line 224 of file netaddress.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetTor()

bool CNetAddr::SetTor ( const std::string &  addr)
private

Parse a Tor address and set this object to it.

Parameters
[in]addrAddress to parse, must be a valid C string, for example pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion or 6hzph5hv6337r6p2.onion.
Returns
Whether the operation was successful.
See also
CNetAddr::IsTor()

Definition at line 241 of file netaddress.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ToString()

std::string CNetAddr::ToString ( ) const

Definition at line 622 of file netaddress.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ToStringIP()

std::string CNetAddr::ToStringIP ( ) const

Definition at line 570 of file netaddress.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Unserialize()

template<typename Stream >
void CNetAddr::Unserialize ( Stream &  s)
inline

Unserialize from a stream.

Definition at line 248 of file netaddress.h.

Here is the call graph for this function:

◆ UnserializeV1Array()

void CNetAddr::UnserializeV1Array ( uint8_t(&)  arr[V1_SERIALIZATION_SIZE])
inlineprivate

Unserialize from a pre-ADDRv2/BIP155 format from an array.

Definition at line 400 of file netaddress.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ UnserializeV1Stream()

template<typename Stream >
void CNetAddr::UnserializeV1Stream ( Stream &  s)
inlineprivate

Unserialize from a pre-ADDRv2/BIP155 format from a stream.

Definition at line 411 of file netaddress.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ UnserializeV2Stream()

template<typename Stream >
void CNetAddr::UnserializeV2Stream ( Stream &  s)
inlineprivate

Unserialize from a ADDRv2 / BIP155 format.

Definition at line 424 of file netaddress.h.

Here is the call graph for this function:
Here is the caller graph for this function:

Friends And Related Function Documentation

◆ CSubNet

friend class CSubNet
friend

Definition at line 257 of file netaddress.h.

◆ operator!=

bool operator!= ( const CNetAddr a,
const CNetAddr b 
)
friend

Definition at line 220 of file netaddress.h.

◆ operator<

bool operator< ( const CNetAddr a,
const CNetAddr b 
)
friend

Definition at line 632 of file netaddress.cpp.

◆ operator==

bool operator== ( const CNetAddr a,
const CNetAddr b 
)
friend

Definition at line 627 of file netaddress.cpp.

Member Data Documentation

◆ m_addr

prevector<ADDR_IPV6_SIZE, uint8_t> CNetAddr::m_addr {ADDR_IPV6_SIZE, 0x0}
protected

Raw representation of the network address.

In network byte order (big endian) for IPv4 and IPv6.

Definition at line 126 of file netaddress.h.

◆ m_net

Network CNetAddr::m_net {NET_IPV6}
protected

Network to which this address belongs.

Definition at line 131 of file netaddress.h.

◆ m_scope_id

uint32_t CNetAddr::m_scope_id {0}
protected

Scope id if scoped/link-local IPV6 address.

See https://tools.ietf.org/html/rfc4007

Definition at line 137 of file netaddress.h.

◆ MAX_ADDRV2_SIZE

constexpr size_t CNetAddr::MAX_ADDRV2_SIZE = 512
staticconstexprprivate

Maximum size of an address as defined in BIP155 (in bytes).

This is only the size of the address, not the entire CNetAddr object when serialized.

Definition at line 301 of file netaddress.h.

◆ V1_SERIALIZATION_SIZE

constexpr size_t CNetAddr::V1_SERIALIZATION_SIZE = ADDR_IPV6_SIZE
staticconstexprprivate

Size of CNetAddr when serialized as ADDRv1 (pre-BIP155) (in bytes).

Definition at line 294 of file netaddress.h.


The documentation for this class was generated from the following files: