Line data Source code
1 : // Copyright (c) 2009-2020 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 <attributes.h>
13 : #include <compat.h>
14 : #include <prevector.h>
15 : #include <serialize.h>
16 :
17 : #include <array>
18 : #include <cstdint>
19 : #include <string>
20 : #include <vector>
21 :
22 : /**
23 : * A network type.
24 : * @note An address may belong to more than one network, for example `10.0.0.1`
25 : * belongs to both `NET_UNROUTABLE` and `NET_IPV4`.
26 : * Keep these sequential starting from 0 and `NET_MAX` as the last entry.
27 : * We have loops like `for (int i = 0; i < NET_MAX; i++)` that expect to iterate
28 : * over all enum values and also `GetExtNetwork()` "extends" this enum by
29 : * introducing standalone constants starting from `NET_MAX`.
30 : */
31 : enum Network
32 : {
33 : /// Addresses from these networks are not publicly routable on the global Internet.
34 : NET_UNROUTABLE = 0,
35 :
36 : /// IPv4
37 : NET_IPV4,
38 :
39 : /// IPv6
40 : NET_IPV6,
41 :
42 : /// TORv2
43 : NET_ONION,
44 :
45 : /// A set of addresses that represent the hash of a string or FQDN. We use
46 : /// them in CAddrMan to keep track of which DNS seeds were used.
47 : NET_INTERNAL,
48 :
49 : /// Dummy value to indicate the number of NET_* constants.
50 : NET_MAX,
51 : };
52 :
53 : /// Prefix of an IPv6 address when it contains an embedded IPv4 address.
54 : /// Used when (un)serializing addresses in ADDRv1 format (pre-BIP155).
55 : static const std::array<uint8_t, 12> IPV4_IN_IPV6_PREFIX{
56 : 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF
57 : };
58 :
59 : /// Prefix of an IPv6 address when it contains an embedded TORv2 address.
60 : /// Used when (un)serializing addresses in ADDRv1 format (pre-BIP155).
61 : /// Such dummy IPv6 addresses are guaranteed to not be publicly routable as they
62 : /// fall under RFC4193's fc00::/7 subnet allocated to unique-local addresses.
63 : static const std::array<uint8_t, 6> TORV2_IN_IPV6_PREFIX{
64 : 0xFD, 0x87, 0xD8, 0x7E, 0xEB, 0x43
65 : };
66 :
67 : /// Prefix of an IPv6 address when it contains an embedded "internal" address.
68 : /// Used when (un)serializing addresses in ADDRv1 format (pre-BIP155).
69 : /// The prefix comes from 0xFD + SHA256("bitcoin")[0:5].
70 : /// Such dummy IPv6 addresses are guaranteed to not be publicly routable as they
71 : /// fall under RFC4193's fc00::/7 subnet allocated to unique-local addresses.
72 : static const std::array<uint8_t, 6> INTERNAL_IN_IPV6_PREFIX{
73 : 0xFD, 0x6B, 0x88, 0xC0, 0x87, 0x24 // 0xFD + sha256("bitcoin")[0:5].
74 : };
75 :
76 : /// Size of IPv4 address (in bytes).
77 : static constexpr size_t ADDR_IPV4_SIZE = 4;
78 :
79 : /// Size of IPv6 address (in bytes).
80 : static constexpr size_t ADDR_IPV6_SIZE = 16;
81 :
82 : /// Size of TORv2 address (in bytes).
83 : static constexpr size_t ADDR_TORV2_SIZE = 10;
84 :
85 : /// Size of "internal" (NET_INTERNAL) address (in bytes).
86 : static constexpr size_t ADDR_INTERNAL_SIZE = 10;
87 :
88 : /**
89 : * Network address.
90 : */
91 11983464 : class CNetAddr
92 : {
93 : protected:
94 : /**
95 : * Raw representation of the network address.
96 : * In network byte order (big endian) for IPv4 and IPv6.
97 : */
98 : prevector<ADDR_IPV6_SIZE, uint8_t> m_addr{ADDR_IPV6_SIZE, 0x0};
99 :
100 : /**
101 : * Network to which this address belongs.
102 : */
103 : Network m_net{NET_IPV6};
104 :
105 : uint32_t scopeId{0}; // for scoped/link-local ipv6 addresses
106 :
107 : public:
108 : CNetAddr();
109 : explicit CNetAddr(const struct in_addr& ipv4Addr);
110 : void SetIP(const CNetAddr& ip);
111 :
112 : /**
113 : * Set from a legacy IPv6 address.
114 : * Legacy IPv6 address may be a normal IPv6 address, or another address
115 : * (e.g. IPv4) disguised as IPv6. This encoding is used in the legacy
116 : * `addr` encoding.
117 : */
118 : void SetLegacyIPv6(Span<const uint8_t> ipv6);
119 :
120 : bool SetInternal(const std::string& name);
121 :
122 : bool SetSpecial(const std::string &strName); // for Tor addresses
123 : bool IsBindAny() const; // INADDR_ANY equivalent
124 : bool IsIPv4() const; // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
125 : bool IsIPv6() const; // IPv6 address (not mapped IPv4, not Tor)
126 : bool IsRFC1918() const; // IPv4 private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
127 : bool IsRFC2544() const; // IPv4 inter-network communications (198.18.0.0/15)
128 : bool IsRFC6598() const; // IPv4 ISP-level NAT (100.64.0.0/10)
129 : bool IsRFC5737() const; // IPv4 documentation addresses (192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24)
130 : bool IsRFC3849() const; // IPv6 documentation address (2001:0DB8::/32)
131 : bool IsRFC3927() const; // IPv4 autoconfig (169.254.0.0/16)
132 : bool IsRFC3964() const; // IPv6 6to4 tunnelling (2002::/16)
133 : bool IsRFC4193() const; // IPv6 unique local (FC00::/7)
134 : bool IsRFC4380() const; // IPv6 Teredo tunnelling (2001::/32)
135 : bool IsRFC4843() const; // IPv6 ORCHID (deprecated) (2001:10::/28)
136 : bool IsRFC7343() const; // IPv6 ORCHIDv2 (2001:20::/28)
137 : bool IsRFC4862() const; // IPv6 autoconfig (FE80::/64)
138 : bool IsRFC6052() const; // IPv6 well-known prefix for IPv4-embedded address (64:FF9B::/96)
139 : bool IsRFC6145() const; // IPv6 IPv4-translated address (::FFFF:0:0:0/96) (actually defined in RFC2765)
140 : bool IsHeNet() const; // IPv6 Hurricane Electric - https://he.net (2001:0470::/36)
141 : bool IsTor() const;
142 : bool IsLocal() const;
143 : bool IsRoutable() const;
144 : bool IsInternal() const;
145 : bool IsValid() const;
146 : enum Network GetNetwork() const;
147 : std::string ToString() const;
148 : std::string ToStringIP() const;
149 : uint64_t GetHash() const;
150 : bool GetInAddr(struct in_addr* pipv4Addr) const;
151 : uint32_t GetNetClass() const;
152 :
153 : //! For IPv4, mapped IPv4, SIIT translated IPv4, Teredo, 6to4 tunneled addresses, return the relevant IPv4 address as a uint32.
154 : uint32_t GetLinkedIPv4() const;
155 : //! Whether this address has a linked IPv4 address (see GetLinkedIPv4()).
156 : bool HasLinkedIPv4() const;
157 :
158 : // The AS on the BGP path to the node we use to diversify
159 : // peers in AddrMan bucketing based on the AS infrastructure.
160 : // The ip->AS mapping depends on how asmap is constructed.
161 : uint32_t GetMappedAS(const std::vector<bool> &asmap) const;
162 :
163 : std::vector<unsigned char> GetGroup(const std::vector<bool> &asmap) const;
164 : std::vector<unsigned char> GetAddrBytes() const;
165 : int GetReachabilityFrom(const CNetAddr *paddrPartner = nullptr) const;
166 :
167 : explicit CNetAddr(const struct in6_addr& pipv6Addr, const uint32_t scope = 0);
168 : bool GetIn6Addr(struct in6_addr* pipv6Addr) const;
169 :
170 : friend bool operator==(const CNetAddr& a, const CNetAddr& b);
171 : friend bool operator!=(const CNetAddr& a, const CNetAddr& b) { return !(a == b); }
172 : friend bool operator<(const CNetAddr& a, const CNetAddr& b);
173 :
174 : /**
175 : * Serialize to a stream.
176 : */
177 : template <typename Stream>
178 56249 : void Serialize(Stream& s) const
179 : {
180 56249 : SerializeV1Stream(s);
181 56249 : }
182 :
183 : /**
184 : * Unserialize from a stream.
185 : */
186 : template <typename Stream>
187 12451 : void Unserialize(Stream& s)
188 : {
189 12451 : UnserializeV1Stream(s);
190 12451 : }
191 :
192 : friend class CSubNet;
193 :
194 : private:
195 : /**
196 : * Size of CNetAddr when serialized as ADDRv1 (pre-BIP155) (in bytes).
197 : */
198 : static constexpr size_t V1_SERIALIZATION_SIZE = ADDR_IPV6_SIZE;
199 :
200 : /**
201 : * Serialize in pre-ADDRv2/BIP155 format to an array.
202 : * Some addresses (e.g. TORv3) cannot be serialized in pre-BIP155 format.
203 : */
204 4435770 : void SerializeV1Array(uint8_t (&arr)[V1_SERIALIZATION_SIZE]) const
205 : {
206 : size_t prefix_size;
207 :
208 4435770 : switch (m_net) {
209 : case NET_IPV6:
210 3682713 : assert(m_addr.size() == sizeof(arr));
211 3682713 : memcpy(arr, m_addr.data(), m_addr.size());
212 3682713 : return;
213 : case NET_IPV4:
214 : prefix_size = sizeof(IPV4_IN_IPV6_PREFIX);
215 753053 : assert(prefix_size + m_addr.size() == sizeof(arr));
216 753053 : memcpy(arr, IPV4_IN_IPV6_PREFIX.data(), prefix_size);
217 753053 : memcpy(arr + prefix_size, m_addr.data(), m_addr.size());
218 753053 : return;
219 : case NET_ONION:
220 : prefix_size = sizeof(TORV2_IN_IPV6_PREFIX);
221 3 : assert(prefix_size + m_addr.size() == sizeof(arr));
222 3 : memcpy(arr, TORV2_IN_IPV6_PREFIX.data(), prefix_size);
223 3 : memcpy(arr + prefix_size, m_addr.data(), m_addr.size());
224 3 : return;
225 : case NET_INTERNAL:
226 : prefix_size = sizeof(INTERNAL_IN_IPV6_PREFIX);
227 1 : assert(prefix_size + m_addr.size() == sizeof(arr));
228 1 : memcpy(arr, INTERNAL_IN_IPV6_PREFIX.data(), prefix_size);
229 1 : memcpy(arr + prefix_size, m_addr.data(), m_addr.size());
230 1 : return;
231 : case NET_UNROUTABLE:
232 : case NET_MAX:
233 0 : assert(false);
234 : } // no default case, so the compiler can warn about missing cases
235 :
236 0 : assert(false);
237 4435770 : }
238 :
239 : /**
240 : * Serialize in pre-ADDRv2/BIP155 format to a stream.
241 : * Some addresses (e.g. TORv3) cannot be serialized in pre-BIP155 format.
242 : */
243 : template <typename Stream>
244 56249 : void SerializeV1Stream(Stream& s) const
245 : {
246 56249 : uint8_t serialized[V1_SERIALIZATION_SIZE];
247 :
248 56249 : SerializeV1Array(serialized);
249 :
250 56249 : s << serialized;
251 56249 : }
252 :
253 : /**
254 : * Unserialize from a pre-ADDRv2/BIP155 format from an array.
255 : */
256 12451 : void UnserializeV1Array(uint8_t (&arr)[V1_SERIALIZATION_SIZE])
257 : {
258 : // Use SetLegacyIPv6() so that m_net is set correctly. For example
259 : // ::FFFF:0102:0304 should be set as m_net=NET_IPV4 (1.2.3.4).
260 12451 : SetLegacyIPv6(arr);
261 12451 : }
262 :
263 : /**
264 : * Unserialize from a pre-ADDRv2/BIP155 format from a stream.
265 : */
266 : template <typename Stream>
267 12451 : void UnserializeV1Stream(Stream& s)
268 : {
269 12451 : uint8_t serialized[V1_SERIALIZATION_SIZE];
270 :
271 12451 : s >> serialized;
272 :
273 12451 : UnserializeV1Array(serialized);
274 12451 : }
275 : };
276 :
277 10565 : class CSubNet
278 : {
279 : protected:
280 : /// Network (base) address
281 : CNetAddr network;
282 : /// Netmask, in network byte order
283 : uint8_t netmask[16];
284 : /// Is this value valid? (only used to signal parse errors)
285 : bool valid;
286 :
287 : public:
288 : CSubNet();
289 : CSubNet(const CNetAddr& addr, uint8_t mask);
290 : CSubNet(const CNetAddr& addr, const CNetAddr& mask);
291 :
292 : //constructor for single ip subnet (<ipv4>/32 or <ipv6>/128)
293 : explicit CSubNet(const CNetAddr& addr);
294 :
295 : bool Match(const CNetAddr &addr) const;
296 :
297 : std::string ToString() const;
298 : bool IsValid() const;
299 :
300 : friend bool operator==(const CSubNet& a, const CSubNet& b);
301 1 : friend bool operator!=(const CSubNet& a, const CSubNet& b) { return !(a == b); }
302 : friend bool operator<(const CSubNet& a, const CSubNet& b);
303 :
304 153 : SERIALIZE_METHODS(CSubNet, obj) { READWRITE(obj.network, obj.netmask, obj.valid); }
305 : };
306 :
307 : /** A combination of a network address (CNetAddr) and a (TCP) port */
308 6339683 : class CService : public CNetAddr
309 : {
310 : protected:
311 : uint16_t port; // host order
312 :
313 : public:
314 : CService();
315 : CService(const CNetAddr& ip, uint16_t port);
316 : CService(const struct in_addr& ipv4Addr, uint16_t port);
317 : explicit CService(const struct sockaddr_in& addr);
318 : uint16_t GetPort() const;
319 : bool GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const;
320 : bool SetSockAddr(const struct sockaddr* paddr);
321 : friend bool operator==(const CService& a, const CService& b);
322 2656 : friend bool operator!=(const CService& a, const CService& b) { return !(a == b); }
323 : friend bool operator<(const CService& a, const CService& b);
324 : std::vector<unsigned char> GetKey() const;
325 : std::string ToString() const;
326 : std::string ToStringPort() const;
327 : std::string ToStringIPPort() const;
328 :
329 : CService(const struct in6_addr& ipv6Addr, uint16_t port);
330 : explicit CService(const struct sockaddr_in6& addr);
331 :
332 131238 : SERIALIZE_METHODS(CService, obj)
333 : {
334 43746 : READWRITEAS(CNetAddr, obj);
335 43746 : READWRITE(Using<BigEndianFormatter<2>>(obj.port));
336 43746 : }
337 : };
338 :
339 : bool SanityCheckASMap(const std::vector<bool>& asmap);
340 :
341 : #endif // BITCOIN_NETADDRESS_H
|