Bitcoin Core  0.19.99
P2P Digital Currency
Go to the documentation of this file.
1 // Copyright (c) 2009-2019 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  template <typename Stream, typename Operation>
105  inline void SerializationOp(Stream& s, Operation ser_action) {
106  READWRITE(ip);
107  }
109  friend class CSubNet;
110 };
112 class CSubNet
113 {
114  protected:
118  uint8_t netmask[16];
120  bool valid;
122  public:
123  CSubNet();
124  CSubNet(const CNetAddr &addr, int32_t mask);
125  CSubNet(const CNetAddr &addr, const CNetAddr &mask);
127  //constructor for single ip subnet (<ipv4>/32 or <ipv6>/128)
128  explicit CSubNet(const CNetAddr &addr);
130  bool Match(const CNetAddr &addr) const;
132  std::string ToString() const;
133  bool IsValid() const;
135  friend bool operator==(const CSubNet& a, const CSubNet& b);
136  friend bool operator!=(const CSubNet& a, const CSubNet& b) { return !(a == b); }
137  friend bool operator<(const CSubNet& a, const CSubNet& b);
141  template <typename Stream, typename Operation>
142  inline void SerializationOp(Stream& s, Operation ser_action) {
143  READWRITE(network);
144  READWRITE(netmask);
145  READWRITE(valid);
146  }
147 };
150 class CService : public CNetAddr
151 {
152  protected:
153  uint16_t port; // host order
155  public:
156  CService();
157  CService(const CNetAddr& ip, unsigned short port);
158  CService(const struct in_addr& ipv4Addr, unsigned short port);
159  explicit CService(const struct sockaddr_in& addr);
160  unsigned short GetPort() const;
161  bool GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const;
162  bool SetSockAddr(const struct sockaddr* paddr);
163  friend bool operator==(const CService& a, const CService& b);
164  friend bool operator!=(const CService& a, const CService& b) { return !(a == b); }
165  friend bool operator<(const CService& a, const CService& b);
166  std::vector<unsigned char> GetKey() const;
167  std::string ToString() const;
168  std::string ToStringPort() const;
169  std::string ToStringIPPort() const;
171  CService(const struct in6_addr& ipv6Addr, unsigned short port);
172  explicit CService(const struct sockaddr_in6& addr);
176  template <typename Stream, typename Operation>
177  inline void SerializationOp(Stream& s, Operation ser_action) {
178  READWRITE(ip);
179  READWRITE(WrapBigEndian(port));
180  }
181 };
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:109
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:153
bool IsInternal() const
Definition: netaddress.cpp:311
CNetAddr network
Network (base) address.
Definition: netaddress.h:116
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 GetIn6Addr(struct in6_addr *pipv6Addr) const
Try to get our IPv6 address.
Definition: netaddress.cpp:400
BigEndian< I > WrapBigEndian(I &n)
Definition: serialize.h:630
enum Network GetNetwork() const
Definition: netaddress.cpp:316
bool IsRFC2544() const
Definition: netaddress.cpp:143
Definition: netaddress.h:174
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:136
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:164
std::string ToStringIP() const
Definition: netaddress.cpp:333
const char * name
Definition: rest.cpp:40
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:150
Definition: netaddress.h:19
unsigned int GetByte(int n) const
Definition: netaddress.cpp:110
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:120
void SerializationOp(Stream &s, Operation ser_action)
Definition: netaddress.h:177
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
Definition: netaddress.h:102
friend bool operator==(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:362
unsigned char ip[16]
Definition: netaddress.h:34
void SerializationOp(Stream &s, Operation ser_action)
Definition: netaddress.h:142
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:191
bool IsRFC4193() const
Definition: netaddress.cpp:192
uint32_t GetNetClass() const
Definition: netaddress.cpp:429
bool IsTor() const
Definition: netaddress.cpp:224
void SerializationOp(Stream &s, Operation ser_action)
Definition: netaddress.h:105
Definition: netaddress.h:139
bool IsRFC3964() const
Definition: netaddress.cpp:170
Construct an unspecified IPv6 network address (::/128).
Definition: netaddress.cpp:23