Changeset View
Changeset View
Standalone View
Standalone View
src/netaddress.h
Show All 10 Lines | |||||
#include <compat.h> | #include <compat.h> | ||||
#include <serialize.h> | #include <serialize.h> | ||||
#include <cstdint> | #include <cstdint> | ||||
#include <string> | #include <string> | ||||
#include <vector> | #include <vector> | ||||
/** | |||||
* A network type. | |||||
* @note An address may belong to more than one network, for example `10.0.0.1` | |||||
* belongs to both `NET_UNROUTABLE` and `NET_IPV4`. | |||||
* Keep these sequential starting from 0 and `NET_MAX` as the last entry. | |||||
* We have loops like `for (int i = 0; i < NET_MAX; i++)` that expect to iterate | |||||
* over all enum values and also `GetExtNetwork()` "extends" this enum by | |||||
* introducing standalone constants starting from `NET_MAX`. | |||||
*/ | |||||
enum Network { | enum Network { | ||||
/// Addresses from these networks are not publicly routable on the global | |||||
/// Internet. | |||||
NET_UNROUTABLE = 0, | NET_UNROUTABLE = 0, | ||||
/// IPv4 | |||||
NET_IPV4, | NET_IPV4, | ||||
/// IPv6 | |||||
NET_IPV6, | NET_IPV6, | ||||
/// TORv2 | |||||
NET_ONION, | NET_ONION, | ||||
/// A set of dummy addresses that map a name to an IPv6 address. These | |||||
/// addresses belong to RFC4193's fc00::/7 subnet (unique-local addresses). | |||||
/// We use them to map a string or FQDN to an IPv6 address in CAddrMan to | |||||
/// keep track of which DNS seeds were used. | |||||
NET_INTERNAL, | NET_INTERNAL, | ||||
/// Dummy value to indicate the number of NET_* constants. | |||||
NET_MAX, | NET_MAX, | ||||
}; | }; | ||||
/** IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96)) */ | /** | ||||
* Network address. | |||||
*/ | |||||
class CNetAddr { | class CNetAddr { | ||||
protected: | protected: | ||||
/** | |||||
* Network to which this address belongs. | |||||
*/ | |||||
Network m_net{NET_IPV6}; | |||||
// in network byte order | // in network byte order | ||||
uint8_t ip[16]; | uint8_t ip[16]; | ||||
// for scoped/link-local ipv6 addresses | // for scoped/link-local ipv6 addresses | ||||
uint32_t scopeId{0}; | uint32_t scopeId{0}; | ||||
public: | public: | ||||
CNetAddr(); | CNetAddr(); | ||||
explicit CNetAddr(const struct in_addr &ipv4Addr); | explicit CNetAddr(const struct in_addr &ipv4Addr); | ||||
void SetIP(const CNetAddr &ip); | void SetIP(const CNetAddr &ip); | ||||
/** | /** | ||||
* 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. | |||||
*/ | |||||
void SetLegacyIPv6(const uint8_t ipv6[16]); | |||||
/** | |||||
* Set raw IPv4 or IPv6 address (in network byte order) | * Set raw IPv4 or IPv6 address (in network byte order) | ||||
* @note Only NET_IPV4 and NET_IPV6 are allowed for network. | * @note Only NET_IPV4 and NET_IPV6 are allowed for network. | ||||
*/ | */ | ||||
void SetRaw(Network network, const uint8_t *data); | void SetRaw(Network network, const uint8_t *data); | ||||
bool SetInternal(const std::string &name); | bool SetInternal(const std::string &name); | ||||
// for Tor addresses | // for Tor addresses | ||||
▲ Show 20 Lines • Show All 71 Lines • ▼ Show 20 Lines | public: | ||||
bool GetIn6Addr(struct in6_addr *pipv6Addr) const; | bool GetIn6Addr(struct in6_addr *pipv6Addr) const; | ||||
friend bool operator==(const CNetAddr &a, const CNetAddr &b); | friend bool operator==(const CNetAddr &a, const CNetAddr &b); | ||||
friend bool operator!=(const CNetAddr &a, const CNetAddr &b) { | friend bool operator!=(const CNetAddr &a, const CNetAddr &b) { | ||||
return !(a == b); | return !(a == b); | ||||
} | } | ||||
friend bool operator<(const CNetAddr &a, const CNetAddr &b); | friend bool operator<(const CNetAddr &a, const CNetAddr &b); | ||||
SERIALIZE_METHODS(CNetAddr, obj) { READWRITE(obj.ip); } | /** | ||||
* Serialize to a stream. | |||||
*/ | |||||
template <typename Stream> void Serialize(Stream &s) const { s << ip; } | |||||
/** | |||||
* Unserialize from a stream. | |||||
*/ | |||||
template <typename Stream> void Unserialize(Stream &s) { | |||||
uint8_t ip_temp[sizeof(ip)]; | |||||
s >> ip_temp; | |||||
// Use SetLegacyIPv6() so that m_net is set correctly. For example | |||||
// ::FFFF:0102:0304 should be set as m_net=NET_IPV4 (1.2.3.4). | |||||
SetLegacyIPv6(ip_temp); | |||||
} | |||||
friend class CSubNet; | friend class CSubNet; | ||||
}; | }; | ||||
class CSubNet { | class CSubNet { | ||||
protected: | protected: | ||||
/// Network (base) address | /// Network (base) address | ||||
CNetAddr network; | CNetAddr network; | ||||
▲ Show 20 Lines • Show All 65 Lines • Show Last 20 Lines |