2017-09-03 15:29:10 +02:00
|
|
|
// Copyright (c) 2009-2015 The Bitcoin Core developers
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#ifndef BITCOIN_NETADDRESS_H
|
|
|
|
#define BITCOIN_NETADDRESS_H
|
|
|
|
|
|
|
|
#if defined(HAVE_CONFIG_H)
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <config/dash-config.h>
|
2017-09-03 15:29:10 +02:00
|
|
|
#endif
|
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <compat.h>
|
|
|
|
#include <serialize.h>
|
2018-04-08 17:57:22 +02:00
|
|
|
#include <span.h>
|
2017-09-03 15:29:10 +02:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2017-12-20 12:45:01 +01:00
|
|
|
extern bool fAllowPrivateNet;
|
|
|
|
|
2017-09-03 15:29:10 +02:00
|
|
|
enum Network
|
|
|
|
{
|
|
|
|
NET_UNROUTABLE = 0,
|
|
|
|
NET_IPV4,
|
|
|
|
NET_IPV6,
|
2020-04-16 07:21:49 +02:00
|
|
|
NET_ONION,
|
2017-06-24 12:16:41 +02:00
|
|
|
NET_INTERNAL,
|
2017-09-03 15:29:10 +02:00
|
|
|
|
|
|
|
NET_MAX,
|
|
|
|
};
|
|
|
|
|
|
|
|
/** IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96)) */
|
|
|
|
class CNetAddr
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
unsigned char ip[16]; // in network byte order
|
2020-06-10 02:06:54 +02:00
|
|
|
uint32_t scopeId{0}; // for scoped/link-local ipv6 addresses
|
2017-09-03 15:29:10 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
CNetAddr();
|
2017-08-17 22:59:56 +02:00
|
|
|
explicit CNetAddr(const struct in_addr& ipv4Addr);
|
2017-09-03 15:29:10 +02:00
|
|
|
void SetIP(const CNetAddr& ip);
|
|
|
|
|
2018-04-19 14:24:26 +02:00
|
|
|
private:
|
2017-09-03 15:29:10 +02:00
|
|
|
/**
|
|
|
|
* Set raw IPv4 or IPv6 address (in network byte order)
|
|
|
|
* @note Only NET_IPV4 and NET_IPV6 are allowed for network.
|
|
|
|
*/
|
|
|
|
void SetRaw(Network network, const uint8_t *data);
|
|
|
|
|
2018-04-19 14:24:26 +02:00
|
|
|
public:
|
2017-06-24 12:16:41 +02:00
|
|
|
/**
|
|
|
|
* Transform an arbitrary string into a non-routable ipv6 address.
|
|
|
|
* Useful for mapping resolved addresses back to their source.
|
|
|
|
*/
|
|
|
|
bool SetInternal(const std::string& name);
|
|
|
|
|
2017-09-03 15:29:10 +02:00
|
|
|
bool SetSpecial(const std::string &strName); // for Tor addresses
|
2018-11-22 10:51:39 +01:00
|
|
|
bool IsBindAny() const; // INADDR_ANY equivalent
|
2017-09-03 15:29:10 +02:00
|
|
|
bool IsIPv4() const; // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
|
|
|
|
bool IsIPv6() const; // IPv6 address (not mapped IPv4, not Tor)
|
|
|
|
bool IsRFC1918() const; // IPv4 private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
|
2017-01-29 16:05:06 +01:00
|
|
|
bool IsRFC2544() const; // IPv4 inter-network communications (192.18.0.0/15)
|
2017-09-03 15:29:10 +02:00
|
|
|
bool IsRFC6598() const; // IPv4 ISP-level NAT (100.64.0.0/10)
|
|
|
|
bool IsRFC5737() const; // IPv4 documentation addresses (192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24)
|
|
|
|
bool IsRFC3849() const; // IPv6 documentation address (2001:0DB8::/32)
|
|
|
|
bool IsRFC3927() const; // IPv4 autoconfig (169.254.0.0/16)
|
|
|
|
bool IsRFC3964() const; // IPv6 6to4 tunnelling (2002::/16)
|
|
|
|
bool IsRFC4193() const; // IPv6 unique local (FC00::/7)
|
|
|
|
bool IsRFC4380() const; // IPv6 Teredo tunnelling (2001::/32)
|
|
|
|
bool IsRFC4843() const; // IPv6 ORCHID (2001:10::/28)
|
|
|
|
bool IsRFC4862() const; // IPv6 autoconfig (FE80::/64)
|
|
|
|
bool IsRFC6052() const; // IPv6 well-known prefix (64:FF9B::/96)
|
|
|
|
bool IsRFC6145() const; // IPv6 IPv4-translated address (::FFFF:0:0:0/96)
|
|
|
|
bool IsTor() const;
|
|
|
|
bool IsLocal() const;
|
|
|
|
bool IsRoutable() const;
|
2017-06-24 12:16:41 +02:00
|
|
|
bool IsInternal() const;
|
2017-09-03 15:29:10 +02:00
|
|
|
bool IsValid() const;
|
|
|
|
enum Network GetNetwork() const;
|
|
|
|
std::string ToString() const;
|
|
|
|
std::string ToStringIP(bool fUseGetnameinfo = true) const;
|
|
|
|
unsigned int GetByte(int n) const;
|
|
|
|
uint64_t GetHash() const;
|
|
|
|
bool GetInAddr(struct in_addr* pipv4Addr) const;
|
|
|
|
std::vector<unsigned char> GetGroup() const;
|
2019-08-06 05:08:33 +02:00
|
|
|
int GetReachabilityFrom(const CNetAddr *paddrPartner = nullptr) const;
|
2017-09-03 15:29:10 +02:00
|
|
|
|
2017-08-17 22:59:56 +02:00
|
|
|
explicit CNetAddr(const struct in6_addr& pipv6Addr, const uint32_t scope = 0);
|
2017-09-03 15:29:10 +02:00
|
|
|
bool GetIn6Addr(struct in6_addr* pipv6Addr) const;
|
|
|
|
|
|
|
|
friend bool operator==(const CNetAddr& a, const CNetAddr& b);
|
2018-04-19 14:24:26 +02:00
|
|
|
friend bool operator!=(const CNetAddr& a, const CNetAddr& b) { return !(a == b); }
|
2017-09-03 15:29:10 +02:00
|
|
|
friend bool operator<(const CNetAddr& a, const CNetAddr& b);
|
|
|
|
|
|
|
|
ADD_SERIALIZE_METHODS;
|
|
|
|
|
|
|
|
template <typename Stream, typename Operation>
|
2016-11-09 12:32:57 +01:00
|
|
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
2018-03-30 13:10:00 +02:00
|
|
|
READWRITE(ip);
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
friend class CSubNet;
|
|
|
|
};
|
|
|
|
|
|
|
|
class CSubNet
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
/// Network (base) address
|
|
|
|
CNetAddr network;
|
|
|
|
/// Netmask, in network byte order
|
|
|
|
uint8_t netmask[16];
|
|
|
|
/// Is this value valid? (only used to signal parse errors)
|
|
|
|
bool valid;
|
|
|
|
|
|
|
|
public:
|
|
|
|
CSubNet();
|
|
|
|
CSubNet(const CNetAddr &addr, int32_t mask);
|
|
|
|
CSubNet(const CNetAddr &addr, const CNetAddr &mask);
|
|
|
|
|
|
|
|
//constructor for single ip subnet (<ipv4>/32 or <ipv6>/128)
|
|
|
|
explicit CSubNet(const CNetAddr &addr);
|
|
|
|
|
|
|
|
bool Match(const CNetAddr &addr) const;
|
|
|
|
|
|
|
|
std::string ToString() const;
|
|
|
|
bool IsValid() const;
|
|
|
|
|
|
|
|
friend bool operator==(const CSubNet& a, const CSubNet& b);
|
2018-04-19 14:24:26 +02:00
|
|
|
friend bool operator!=(const CSubNet& a, const CSubNet& b) { return !(a == b); }
|
2017-09-03 15:29:10 +02:00
|
|
|
friend bool operator<(const CSubNet& a, const CSubNet& b);
|
|
|
|
|
|
|
|
ADD_SERIALIZE_METHODS;
|
|
|
|
|
|
|
|
template <typename Stream, typename Operation>
|
2016-11-09 12:32:57 +01:00
|
|
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
2017-09-03 15:29:10 +02:00
|
|
|
READWRITE(network);
|
2018-03-30 13:10:00 +02:00
|
|
|
READWRITE(netmask);
|
|
|
|
READWRITE(valid);
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/** A combination of a network address (CNetAddr) and a (TCP) port */
|
|
|
|
class CService : public CNetAddr
|
|
|
|
{
|
|
|
|
protected:
|
2020-12-17 10:02:44 +01:00
|
|
|
uint16_t port; // host order
|
2017-09-03 15:29:10 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
CService();
|
|
|
|
CService(const CNetAddr& ip, unsigned short port);
|
|
|
|
CService(const struct in_addr& ipv4Addr, unsigned short port);
|
2017-08-17 22:59:56 +02:00
|
|
|
explicit CService(const struct sockaddr_in& addr);
|
2017-09-03 15:29:10 +02:00
|
|
|
void SetPort(unsigned short portIn);
|
|
|
|
unsigned short GetPort() const;
|
|
|
|
bool GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const;
|
|
|
|
bool SetSockAddr(const struct sockaddr* paddr);
|
|
|
|
friend bool operator==(const CService& a, const CService& b);
|
2018-04-19 14:24:26 +02:00
|
|
|
friend bool operator!=(const CService& a, const CService& b) { return !(a == b); }
|
2017-09-03 15:29:10 +02:00
|
|
|
friend bool operator<(const CService& a, const CService& b);
|
|
|
|
std::vector<unsigned char> GetKey() const;
|
|
|
|
std::string ToString(bool fUseGetnameinfo = true) const;
|
|
|
|
std::string ToStringPort() const;
|
|
|
|
std::string ToStringIPPort(bool fUseGetnameinfo = true) const;
|
|
|
|
|
|
|
|
CService(const struct in6_addr& ipv6Addr, unsigned short port);
|
2017-08-17 22:59:56 +02:00
|
|
|
explicit CService(const struct sockaddr_in6& addr);
|
2017-09-03 15:29:10 +02:00
|
|
|
|
2021-01-18 21:54:46 +01:00
|
|
|
/**
|
|
|
|
* Serialize to a stream.
|
|
|
|
*/
|
|
|
|
template <typename Stream>
|
|
|
|
void Serialize(Stream& s) const
|
|
|
|
{
|
|
|
|
s << ip;
|
|
|
|
s << WrapBigEndian(port);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unserialize from a stream.
|
|
|
|
*/
|
|
|
|
template <typename Stream>
|
|
|
|
void Unserialize(Stream& s)
|
2020-07-15 20:59:00 +02:00
|
|
|
{
|
2021-01-18 21:54:46 +01:00
|
|
|
unsigned char 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);
|
|
|
|
s >> WrapBigEndian(port);
|
2017-09-03 15:29:10 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // BITCOIN_NETADDRESS_H
|