Bitcoin ABC  0.22.12
P2P Digital Currency
netaddress.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2016 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef BITCOIN_NETADDRESS_H
6 #define BITCOIN_NETADDRESS_H
7 
8 #if defined(HAVE_CONFIG_H)
9 #include <config/bitcoin-config.h>
10 #endif
11 
12 #include <compat.h>
13 #include <serialize.h>
14 
15 #include <cstdint>
16 #include <string>
17 #include <vector>
18 
19 enum Network {
25 
27 };
28 
30 class CNetAddr {
31 protected:
32  // in network byte order
33  uint8_t ip[16];
34  // for scoped/link-local ipv6 addresses
35  uint32_t scopeId{0};
36 
37 public:
38  CNetAddr();
39  explicit CNetAddr(const struct in_addr &ipv4Addr);
40  void SetIP(const CNetAddr &ip);
41 
46  void SetRaw(Network network, const uint8_t *data);
47 
48  bool SetInternal(const std::string &name);
49 
50  // for Tor addresses
51  bool SetSpecial(const std::string &strName);
52  // INADDR_ANY equivalent
53  bool IsBindAny() const;
54  // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
55  bool IsIPv4() const;
56  // IPv6 address (not mapped IPv4, not Tor)
57  bool IsIPv6() const;
58  // IPv4 private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
59  bool IsRFC1918() const;
60  // IPv4 inter-network communications (198.18.0.0/15)
61  bool IsRFC2544() const;
62  // IPv4 ISP-level NAT (100.64.0.0/10)
63  bool IsRFC6598() const;
64  // IPv4 documentation addresses (192.0.2.0/24, 198.51.100.0/24,
65  // 203.0.113.0/24)
66  bool IsRFC5737() const;
67  // IPv6 documentation address (2001:0DB8::/32)
68  bool IsRFC3849() const;
69  // IPv4 autoconfig (169.254.0.0/16)
70  bool IsRFC3927() const;
71  // IPv6 6to4 tunnelling (2002::/16)
72  bool IsRFC3964() const;
73  // IPv6 unique local (FC00::/7)
74  bool IsRFC4193() const;
75  // IPv6 Teredo tunnelling (2001::/32)
76  bool IsRFC4380() const;
77  // IPv6 ORCHID (deprecated) (2001:10::/28)
78  bool IsRFC4843() const;
79  // IPv6 ORCHIDv2 (2001:20::/28)
80  bool IsRFC7343() const;
81  // IPv6 autoconfig (FE80::/64)
82  bool IsRFC4862() const;
83  // IPv6 well-known prefix for IPv4-embedded address (64:FF9B::/96)
84  bool IsRFC6052() const;
85  // IPv6 IPv4-translated address (::FFFF:0:0:0/96) (actually defined in
86  // RFC2765)
87  bool IsRFC6145() const;
88  // IPv6 Hurricane Electric - https://he.net (2001:0470::/36)
89  bool IsHeNet() const;
90  bool IsTor() const;
91  bool IsLocal() const;
92  bool IsRoutable() const;
93  bool IsInternal() const;
94  bool IsValid() const;
95  enum Network GetNetwork() const;
96  std::string ToString() const;
97  std::string ToStringIP() const;
98  unsigned int GetByte(int n) const;
99  uint64_t GetHash() const;
100  bool GetInAddr(struct in_addr *pipv4Addr) const;
101  uint32_t GetNetClass() const;
102 
105  uint32_t GetLinkedIPv4() const;
107  bool HasLinkedIPv4() const;
108 
109  // The AS on the BGP path to the node we use to diversify
110  // peers in AddrMan bucketing based on the AS infrastructure.
111  // The ip->AS mapping depends on how asmap is constructed.
112  uint32_t GetMappedAS(const std::vector<bool> &asmap) const;
113 
114  std::vector<uint8_t> GetGroup(const std::vector<bool> &asmap) const;
115  std::vector<uint8_t> GetAddrBytes() const {
116  return {std::begin(ip), std::end(ip)};
117  }
118  int GetReachabilityFrom(const CNetAddr *paddrPartner = nullptr) const;
119 
120  explicit CNetAddr(const struct in6_addr &pipv6Addr,
121  const uint32_t scope = 0);
122  bool GetIn6Addr(struct in6_addr *pipv6Addr) const;
123 
124  friend bool operator==(const CNetAddr &a, const CNetAddr &b);
125  friend bool operator!=(const CNetAddr &a, const CNetAddr &b) {
126  return !(a == b);
127  }
128  friend bool operator<(const CNetAddr &a, const CNetAddr &b);
129 
131 
132  template <typename Stream, typename Operation>
133  inline void SerializationOp(Stream &s, Operation ser_action) {
134  READWRITE(ip);
135  }
136 
137  friend class CSubNet;
138 };
139 
140 class CSubNet {
141 protected:
145  uint8_t netmask[16];
147  bool valid;
148 
149 public:
150  CSubNet();
151  CSubNet(const CNetAddr &addr, int32_t mask);
152  CSubNet(const CNetAddr &addr, const CNetAddr &mask);
153 
154  // constructor for single ip subnet (<ipv4>/32 or <ipv6>/128)
155  explicit CSubNet(const CNetAddr &addr);
156 
157  bool Match(const CNetAddr &addr) const;
158 
159  std::string ToString() const;
160  bool IsValid() const;
161 
162  friend bool operator==(const CSubNet &a, const CSubNet &b);
163  friend bool operator!=(const CSubNet &a, const CSubNet &b) {
164  return !(a == b);
165  }
166  friend bool operator<(const CSubNet &a, const CSubNet &b);
167 
169 
170  template <typename Stream, typename Operation>
171  inline void SerializationOp(Stream &s, Operation ser_action) {
172  READWRITE(network);
173  READWRITE(netmask);
174  READWRITE(valid);
175  }
176 };
177 
179 class CService : public CNetAddr {
180 protected:
181  // host order
182  uint16_t port;
183 
184 public:
185  CService();
186  CService(const CNetAddr &ip, unsigned short port);
187  CService(const struct in_addr &ipv4Addr, unsigned short port);
188  explicit CService(const struct sockaddr_in &addr);
189  unsigned short GetPort() const;
190  bool GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) const;
191  bool SetSockAddr(const struct sockaddr *paddr);
192  friend bool operator==(const CService &a, const CService &b);
193  friend bool operator!=(const CService &a, const CService &b) {
194  return !(a == b);
195  }
196  friend bool operator<(const CService &a, const CService &b);
197  std::vector<uint8_t> GetKey() const;
198  std::string ToString() const;
199  std::string ToStringPort() const;
200  std::string ToStringIPPort() const;
201 
202  CService(const struct in6_addr &ipv6Addr, unsigned short port);
203  explicit CService(const struct sockaddr_in6 &addr);
204 
206 
207  template <typename Stream, typename Operation>
208  inline void SerializationOp(Stream &s, Operation ser_action) {
209  READWRITE(ip);
210  READWRITE(WrapBigEndian(port));
211  }
212 };
213 
214 #endif // BITCOIN_NETADDRESS_H
int GetReachabilityFrom(const CNetAddr *paddrPartner=nullptr) const
Calculates a metric for how reachable (*this) is from a given partner.
Definition: netaddress.cpp:576
bool HasLinkedIPv4() const
Whether this address has a linked IPv4 address (see GetLinkedIPv4()).
Definition: netaddress.cpp:412
friend class CSubNet
Definition: netaddress.h:137
bool IsLocal() const
Definition: netaddress.cpp:218
uint8_t ip[16]
Definition: netaddress.h:33
bool IsRFC4380() const
Definition: netaddress.cpp:173
void SetIP(const CNetAddr &ip)
Definition: netaddress.cpp:28
bool IsIPv6() const
Definition: netaddress.cpp:127
std::vector< uint8_t > GetGroup(const std::vector< bool > &asmap) const
Get the canonical identifier of our network group.
Definition: netaddress.cpp:493
uint16_t port
Definition: netaddress.h:182
bool IsInternal() const
Definition: netaddress.cpp:308
CNetAddr network
Network (base) address.
Definition: netaddress.h:143
bool IsBindAny() const
Definition: netaddress.cpp:112
bool GetInAddr(struct in_addr *pipv4Addr) const
Try to get our IPv4 address.
Definition: netaddress.cpp:386
std::string ToString() const
Definition: netaddress.cpp:364
bool GetIn6Addr(struct in6_addr *pipv6Addr) const
Try to get our IPv6 address.
Definition: netaddress.cpp:404
BigEndian< I > WrapBigEndian(I &n)
Definition: serialize.h:729
enum Network GetNetwork() const
Definition: netaddress.cpp:312
bool IsRFC2544() const
Definition: netaddress.cpp:137
ADD_SERIALIZE_METHODS
Definition: netaddress.h:205
bool IsRFC4862() const
Definition: netaddress.cpp:178
uint32_t GetMappedAS(const std::vector< bool > &asmap) const
Definition: netaddress.cpp:450
std::vector< uint8_t > GetAddrBytes() const
Definition: netaddress.h:115
bool IsValid() const
Definition: netaddress.cpp:244
friend bool operator!=(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.h:125
friend bool operator!=(const CSubNet &a, const CSubNet &b)
Definition: netaddress.h:163
bool IsIPv4() const
Definition: netaddress.cpp:123
bool IsRFC5737() const
Definition: netaddress.cpp:151
uint32_t scopeId
Definition: netaddress.h:35
bool IsRFC6145() const
Definition: netaddress.cpp:187
bool IsRFC6052() const
Definition: netaddress.cpp:167
friend bool operator!=(const CService &a, const CService &b)
Definition: netaddress.h:193
std::string ToStringIP() const
Definition: netaddress.cpp:332
const char * name
Definition: rest.cpp:43
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:179
Network
Definition: netaddress.h:19
unsigned int GetByte(int n) const
Definition: netaddress.cpp:108
bool IsRFC3849() const
Definition: netaddress.cpp:158
bool IsRoutable() const
Definition: netaddress.cpp:296
uint64_t GetHash() const
Definition: netaddress.cpp:554
bool valid
Is this value valid? (only used to signal parse errors)
Definition: netaddress.h:147
void SerializationOp(Stream &s, Operation ser_action)
Definition: netaddress.h:208
IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96))
Definition: netaddress.h:30
friend bool operator<(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:372
bool IsRFC1918() const
Definition: netaddress.cpp:131
bool IsRFC6598() const
Definition: netaddress.cpp:146
bool IsHeNet() const
Definition: netaddress.cpp:203
bool IsRFC3927() const
Definition: netaddress.cpp:142
ADD_SERIALIZE_METHODS
Definition: netaddress.h:130
friend bool operator==(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:368
void SerializationOp(Stream &s, Operation ser_action)
Definition: netaddress.h:171
void SetRaw(Network network, const uint8_t *data)
Set raw IPv4 or IPv6 address (in network byte order)
Definition: netaddress.cpp:32
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:82
bool IsRFC7343() const
Definition: netaddress.cpp:198
bool IsRFC4843() const
Definition: netaddress.cpp:193
uint32_t GetLinkedIPv4() const
For IPv4, mapped IPv4, SIIT translated IPv4, Teredo, 6to4 tunneled addresses, return the relevant IPv...
Definition: netaddress.cpp:417
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:60
#define READWRITE(...)
Definition: serialize.h:191
bool IsRFC4193() const
Definition: netaddress.cpp:183
uint32_t GetNetClass() const
Definition: netaddress.cpp:433
bool IsTor() const
Definition: netaddress.cpp:214
void SerializationOp(Stream &s, Operation ser_action)
Definition: netaddress.h:133
ADD_SERIALIZE_METHODS
Definition: netaddress.h:168
bool IsRFC3964() const
Definition: netaddress.cpp:163
CNetAddr()
Construct an unspecified IPv6 network address (::/128).
Definition: netaddress.cpp:24