Bitcoin Core  0.20.99
P2P Digital Currency
Go to the documentation of this file.
1 // Copyright (c) 2009-2020 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or
8 #if defined(HAVE_CONFIG_H)
10 #endif
12 #include <compat.h>
13 #include <serialize.h>
15 #include <stdint.h>
16 #include <string>
17 #include <vector>
19 enum Network
20 {
28 };
31 class CNetAddr
32 {
33  protected:
34  unsigned char ip[16]; // in network byte order
35  uint32_t scopeId{0}; // for scoped/link-local ipv6 addresses
37  public:
38  CNetAddr();
39  explicit CNetAddr(const struct in_addr& ipv4Addr);
40  void SetIP(const CNetAddr& ip);
46  void SetRaw(Network network, const uint8_t *data);
48  bool SetInternal(const std::string& name);
50  bool SetSpecial(const std::string &strName); // for Tor addresses
51  bool IsBindAny() const; // INADDR_ANY equivalent
52  bool IsIPv4() const; // IPv4 mapped address (::FFFF:0:0/96,
53  bool IsIPv6() const; // IPv6 address (not mapped IPv4, not Tor)
54  bool IsRFC1918() const; // IPv4 private networks (,,
55  bool IsRFC2544() const; // IPv4 inter-network communications (
56  bool IsRFC6598() const; // IPv4 ISP-level NAT (
57  bool IsRFC5737() const; // IPv4 documentation addresses (,,
58  bool IsRFC3849() const; // IPv6 documentation address (2001:0DB8::/32)
59  bool IsRFC3927() const; // IPv4 autoconfig (
60  bool IsRFC3964() const; // IPv6 6to4 tunnelling (2002::/16)
61  bool IsRFC4193() const; // IPv6 unique local (FC00::/7)
62  bool IsRFC4380() const; // IPv6 Teredo tunnelling (2001::/32)
63  bool IsRFC4843() const; // IPv6 ORCHID (deprecated) (2001:10::/28)
64  bool IsRFC7343() const; // IPv6 ORCHIDv2 (2001:20::/28)
65  bool IsRFC4862() const; // IPv6 autoconfig (FE80::/64)
66  bool IsRFC6052() const; // IPv6 well-known prefix for IPv4-embedded address (64:FF9B::/96)
67  bool IsRFC6145() const; // IPv6 IPv4-translated address (::FFFF:0:0:0/96) (actually defined in RFC2765)
68  bool IsHeNet() const; // IPv6 Hurricane Electric - (2001:0470::/36)
69  bool IsTor() const;
70  bool IsLocal() const;
71  bool IsRoutable() const;
72  bool IsInternal() const;
73  bool IsValid() const;
74  enum Network GetNetwork() const;
75  std::string ToString() const;
76  std::string ToStringIP() const;
77  unsigned int GetByte(int n) const;
78  uint64_t GetHash() const;
79  bool GetInAddr(struct in_addr* pipv4Addr) const;
80  uint32_t GetNetClass() const;
83  uint32_t GetLinkedIPv4() const;
85  bool HasLinkedIPv4() const;
87  // The AS on the BGP path to the node we use to diversify
88  // peers in AddrMan bucketing based on the AS infrastructure.
89  // The ip->AS mapping depends on how asmap is constructed.
90  uint32_t GetMappedAS(const std::vector<bool> &asmap) const;
92  std::vector<unsigned char> GetGroup(const std::vector<bool> &asmap) const;
93  int GetReachabilityFrom(const CNetAddr *paddrPartner = nullptr) const;
95  explicit CNetAddr(const struct in6_addr& pipv6Addr, const uint32_t scope = 0);
96  bool GetIn6Addr(struct in6_addr* pipv6Addr) const;
98  friend bool operator==(const CNetAddr& a, const CNetAddr& b);
99  friend bool operator!=(const CNetAddr& a, const CNetAddr& b) { return !(a == b); }
100  friend bool operator<(const CNetAddr& a, const CNetAddr& b);
104  friend class CSubNet;
105 };
107 class CSubNet
108 {
109  protected:
113  uint8_t netmask[16];
115  bool valid;
117  public:
118  CSubNet();
119  CSubNet(const CNetAddr &addr, int32_t mask);
120  CSubNet(const CNetAddr &addr, const CNetAddr &mask);
122  //constructor for single ip subnet (<ipv4>/32 or <ipv6>/128)
123  explicit CSubNet(const CNetAddr &addr);
125  bool Match(const CNetAddr &addr) const;
127  std::string ToString() const;
128  bool IsValid() const;
130  friend bool operator==(const CSubNet& a, const CSubNet& b);
131  friend bool operator!=(const CSubNet& a, const CSubNet& b) { return !(a == b); }
132  friend bool operator<(const CSubNet& a, const CSubNet& b);
134  SERIALIZE_METHODS(CSubNet, obj) { READWRITE(, obj.netmask, obj.valid); }
135 };
138 class CService : public CNetAddr
139 {
140  protected:
141  uint16_t port; // host order
143  public:
144  CService();
145  CService(const CNetAddr& ip, unsigned short port);
146  CService(const struct in_addr& ipv4Addr, unsigned short port);
147  explicit CService(const struct sockaddr_in& addr);
148  unsigned short GetPort() const;
149  bool GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const;
150  bool SetSockAddr(const struct sockaddr* paddr);
151  friend bool operator==(const CService& a, const CService& b);
152  friend bool operator!=(const CService& a, const CService& b) { return !(a == b); }
153  friend bool operator<(const CService& a, const CService& b);
154  std::vector<unsigned char> GetKey() const;
155  std::string ToString() const;
156  std::string ToStringPort() const;
157  std::string ToStringIPPort() const;
159  CService(const struct in6_addr& ipv6Addr, unsigned short port);
160  explicit CService(const struct sockaddr_in6& addr);
163 };
165 bool SanityCheckASMap(const std::vector<bool>& asmap);
int GetReachabilityFrom(const CNetAddr *paddrPartner=nullptr) const
Calculates a metric for how reachable (*this) is from a given partner.
Definition: netaddress.cpp:568
std::vector< unsigned char > GetGroup(const std::vector< bool > &asmap) const
Get the canonical identifier of our network group.
Definition: netaddress.cpp:486
bool HasLinkedIPv4() const
Whether this address has a linked IPv4 address (see GetLinkedIPv4()).
Definition: netaddress.cpp:409
friend class CSubNet
Definition: netaddress.h:104
bool IsLocal() const
Definition: netaddress.cpp:229
bool IsRFC4380() const
Definition: netaddress.cpp:181
void SetIP(const CNetAddr &ip)
Definition: netaddress.cpp:28
bool IsIPv6() const
Definition: netaddress.cpp:130
uint16_t port
Definition: netaddress.h:141
bool IsInternal() const
Definition: netaddress.cpp:311
Definition: netaddress.h:134
CNetAddr network
Network (base) address.
Definition: netaddress.h:111
bool IsBindAny() const
Definition: netaddress.cpp:115
bool GetInAddr(struct in_addr *pipv4Addr) const
Try to get our IPv4 address.
Definition: netaddress.cpp:382
std::string ToString() const
Definition: netaddress.cpp:357
bool SanityCheckASMap(const std::vector< bool > &asmap)
Definition: netaddress.cpp:898
bool GetIn6Addr(struct in6_addr *pipv6Addr) const
Try to get our IPv6 address.
Definition: netaddress.cpp:400
enum Network GetNetwork() const
Definition: netaddress.cpp:316
static Wrapper< Formatter, T & > Using(T &&t)
Cause serialization/deserialization of an object to be done using a specified formatter class...
Definition: serialize.h:465
bool IsRFC2544() const
Definition: netaddress.cpp:143
bool IsRFC4862() const
Definition: netaddress.cpp:186
uint32_t GetMappedAS(const std::vector< bool > &asmap) const
Definition: netaddress.cpp:446
bool IsValid() const
Definition: netaddress.cpp:253
friend bool operator!=(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.h:99
friend bool operator!=(const CSubNet &a, const CSubNet &b)
Definition: netaddress.h:131
bool IsIPv4() const
Definition: netaddress.cpp:125
bool IsRFC5737() const
Definition: netaddress.cpp:158
uint32_t scopeId
Definition: netaddress.h:35
bool IsRFC6145() const
Definition: netaddress.cpp:197
bool IsRFC6052() const
Definition: netaddress.cpp:175
friend bool operator!=(const CService &a, const CService &b)
Definition: netaddress.h:152
std::string ToStringIP() const
Definition: netaddress.cpp:333
const char * name
Definition: rest.cpp:41
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:138
Definition: netaddress.h:19
unsigned int GetByte(int n) const
Definition: netaddress.cpp:110
Definition: netaddress.h:102
bool IsRFC3849() const
Definition: netaddress.cpp:165
bool IsRoutable() const
Definition: netaddress.cpp:301
uint64_t GetHash() const
Definition: netaddress.cpp:546
bool valid
Is this value valid? (only used to signal parse errors)
Definition: netaddress.h:115
IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96))
Definition: netaddress.h:31
friend bool operator<(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:367
bool IsRFC1918() const
Definition: netaddress.cpp:135
bool IsRFC6598() const
Definition: netaddress.cpp:153
bool IsHeNet() const
Definition: netaddress.cpp:213
bool IsRFC3927() const
Definition: netaddress.cpp:148
friend bool operator==(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:362
Serialization wrapper class for custom integers and enums.
Definition: serialize.h:497
unsigned char ip[16]
Definition: netaddress.h:34
void SetRaw(Network network, const uint8_t *data)
Set raw IPv4 or IPv6 address (in network byte order)
Definition: netaddress.cpp:33
bool SetSpecial(const std::string &strName)
Try to make this a dummy address that maps the specified onion address into IPv6 using OnionCat&#39;s ran...
Definition: netaddress.cpp:85
bool IsRFC7343() const
Definition: netaddress.cpp:208
bool IsRFC4843() const
Definition: netaddress.cpp:203
uint32_t GetLinkedIPv4() const
For IPv4, mapped IPv4, SIIT translated IPv4, Teredo, 6to4 tunneled addresses, return the relevant IPv...
Definition: netaddress.cpp:414
bool SetInternal(const std::string &name)
Try to make this a dummy address that maps the specified name into IPv6 like so: (0xFD + sha256("bitc...
Definition: netaddress.cpp:63
#define READWRITE(...)
Definition: serialize.h:171
bool IsRFC4193() const
Definition: netaddress.cpp:192
uint32_t GetNetClass() const
Definition: netaddress.cpp:429
Definition: netaddress.h:162
bool IsTor() const
Definition: netaddress.cpp:224
bool IsRFC3964() const
Definition: netaddress.cpp:170
Construct an unspecified IPv6 network address (::/128).
Definition: netaddress.cpp:23