Branch data Line data Source code
# 1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto
# 2 : : // Copyright (c) 2009-2020 The Bitcoin Core developers
# 3 : : // Distributed under the MIT software license, see the accompanying
# 4 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
# 5 : :
# 6 : : #include <cstdint>
# 7 : : #include <netaddress.h>
# 8 : : #include <hash.h>
# 9 : : #include <util/strencodings.h>
# 10 : : #include <util/asmap.h>
# 11 : : #include <tinyformat.h>
# 12 : :
# 13 : : static const unsigned char pchIPv4[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff };
# 14 : : static const unsigned char pchOnionCat[] = {0xFD,0x87,0xD8,0x7E,0xEB,0x43};
# 15 : :
# 16 : : // 0xFD + sha256("bitcoin")[0:5]
# 17 : : static const unsigned char g_internal_prefix[] = { 0xFD, 0x6B, 0x88, 0xC0, 0x87, 0x24 };
# 18 : :
# 19 : : /**
# 20 : : * Construct an unspecified IPv6 network address (::/128).
# 21 : : *
# 22 : : * @note This address is considered invalid by CNetAddr::IsValid()
# 23 : : */
# 24 : : CNetAddr::CNetAddr() : m_net(NET_IPV6)
# 25 : 1195150 : {
# 26 : 1195150 : memset(ip, 0, sizeof(ip));
# 27 : 1195150 : }
# 28 : :
# 29 : : void CNetAddr::SetIP(const CNetAddr& ipIn)
# 30 : 2 : {
# 31 : 2 : m_net = ipIn.m_net;
# 32 : 2 : memcpy(ip, ipIn.ip, sizeof(ip));
# 33 : 2 : }
# 34 : :
# 35 : : void CNetAddr::SetRaw(Network network, const uint8_t *ip_in)
# 36 : 226236 : {
# 37 : 226236 : switch(network)
# 38 : 226236 : {
# 39 : 222374 : case NET_IPV4:
# 40 : 222374 : m_net = NET_IPV4;
# 41 : 222374 : memcpy(ip, pchIPv4, 12);
# 42 : 222374 : memcpy(ip+12, ip_in, 4);
# 43 : 222374 : break;
# 44 : 3862 : case NET_IPV6:
# 45 : 3862 : if (memcmp(ip_in, pchIPv4, sizeof(pchIPv4)) == 0) {
# 46 : 1755 : m_net = NET_IPV4;
# 47 : 2107 : } else if (memcmp(ip_in, pchOnionCat, sizeof(pchOnionCat)) == 0) {
# 48 : 8 : m_net = NET_ONION;
# 49 : 2099 : } else if (memcmp(ip_in, g_internal_prefix, sizeof(g_internal_prefix)) == 0) {
# 50 : 2 : m_net = NET_INTERNAL;
# 51 : 2097 : } else {
# 52 : 2097 : m_net = NET_IPV6;
# 53 : 2097 : }
# 54 : 3862 : memcpy(ip, ip_in, 16);
# 55 : 3862 : break;
# 56 : 0 : default:
# 57 : 0 : assert(!"invalid network");
# 58 : 226236 : }
# 59 : 226236 : }
# 60 : :
# 61 : : /**
# 62 : : * Try to make this a dummy address that maps the specified name into IPv6 like
# 63 : : * so: (0xFD + %sha256("bitcoin")[0:5]) + %sha256(name)[0:10]. Such dummy
# 64 : : * addresses have a prefix of fd6b:88c0:8724::/48 and are guaranteed to not be
# 65 : : * publicly routable as it falls under RFC4193's fc00::/7 subnet allocated to
# 66 : : * unique-local addresses.
# 67 : : *
# 68 : : * CAddrMan uses these fake addresses to keep track of which DNS seeds were
# 69 : : * used.
# 70 : : *
# 71 : : * @returns Whether or not the operation was successful.
# 72 : : *
# 73 : : * @see CNetAddr::IsInternal(), CNetAddr::IsRFC4193()
# 74 : : */
# 75 : : bool CNetAddr::SetInternal(const std::string &name)
# 76 : 436 : {
# 77 : 436 : if (name.empty()) {
# 78 : 0 : return false;
# 79 : 0 : }
# 80 : 436 : m_net = NET_INTERNAL;
# 81 : 436 : unsigned char hash[32] = {};
# 82 : 436 : CSHA256().Write((const unsigned char*)name.data(), name.size()).Finalize(hash);
# 83 : 436 : memcpy(ip, g_internal_prefix, sizeof(g_internal_prefix));
# 84 : 436 : memcpy(ip + sizeof(g_internal_prefix), hash, sizeof(ip) - sizeof(g_internal_prefix));
# 85 : 436 : return true;
# 86 : 436 : }
# 87 : :
# 88 : : /**
# 89 : : * Try to make this a dummy address that maps the specified onion address into
# 90 : : * IPv6 using OnionCat's range and encoding. Such dummy addresses have a prefix
# 91 : : * of fd87:d87e:eb43::/48 and are guaranteed to not be publicly routable as they
# 92 : : * fall under RFC4193's fc00::/7 subnet allocated to unique-local addresses.
# 93 : : *
# 94 : : * @returns Whether or not the operation was successful.
# 95 : : *
# 96 : : * @see CNetAddr::IsTor(), CNetAddr::IsRFC4193()
# 97 : : */
# 98 : : bool CNetAddr::SetSpecial(const std::string &strName)
# 99 : 222385 : {
# 100 : 222385 : if (strName.size()>6 && strName.substr(strName.size() - 6, 6) == ".onion") {
# 101 : 7 : std::vector<unsigned char> vchAddr = DecodeBase32(strName.substr(0, strName.size() - 6).c_str());
# 102 : 7 : if (vchAddr.size() != 16-sizeof(pchOnionCat))
# 103 : 0 : return false;
# 104 : 7 : m_net = NET_ONION;
# 105 : 7 : memcpy(ip, pchOnionCat, sizeof(pchOnionCat));
# 106 : 77 : for (unsigned int i=0; i<16-sizeof(pchOnionCat); i++)
# 107 : 70 : ip[i + sizeof(pchOnionCat)] = vchAddr[i];
# 108 : 7 : return true;
# 109 : 7 : }
# 110 : 222378 : return false;
# 111 : 222378 : }
# 112 : :
# 113 : : CNetAddr::CNetAddr(const struct in_addr& ipv4Addr)
# 114 : 222374 : {
# 115 : 222374 : SetRaw(NET_IPV4, (const uint8_t*)&ipv4Addr);
# 116 : 222374 : }
# 117 : :
# 118 : : CNetAddr::CNetAddr(const struct in6_addr& ipv6Addr, const uint32_t scope)
# 119 : 1168 : {
# 120 : 1168 : SetRaw(NET_IPV6, (const uint8_t*)&ipv6Addr);
# 121 : 1168 : scopeId = scope;
# 122 : 1168 : }
# 123 : :
# 124 : : unsigned int CNetAddr::GetByte(int n) const
# 125 : 2390190 : {
# 126 : 2390190 : return ip[15-n];
# 127 : 2390190 : }
# 128 : :
# 129 : : bool CNetAddr::IsBindAny() const
# 130 : 1030 : {
# 131 : 1030 : const int cmplen = IsIPv4() ? 4 : 16;
# 132 : 1030 : for (int i = 0; i < cmplen; ++i) {
# 133 : 1030 : if (GetByte(i)) return false;
# 134 : 1030 : }
# 135 : 1030 :
# 136 : 1030 : return true;
# 137 : 1030 : }
# 138 : :
# 139 : 2282927 : bool CNetAddr::IsIPv4() const { return m_net == NET_IPV4; }
# 140 : :
# 141 : 2256912 : bool CNetAddr::IsIPv6() const { return m_net == NET_IPV6; }
# 142 : :
# 143 : : bool CNetAddr::IsRFC1918() const
# 144 : 201276 : {
# 145 : 201276 : return IsIPv4() && (
# 146 : 201152 : GetByte(3) == 10 ||
# 147 : 201152 : (GetByte(3) == 192 && GetByte(2) == 168) ||
# 148 : 201152 : (GetByte(3) == 172 && (GetByte(2) >= 16 && GetByte(2) <= 31)));
# 149 : 201276 : }
# 150 : :
# 151 : : bool CNetAddr::IsRFC2544() const
# 152 : 201252 : {
# 153 : 201252 : return IsIPv4() && GetByte(3) == 198 && (GetByte(2) == 18 || GetByte(2) == 19);
# 154 : 201252 : }
# 155 : :
# 156 : : bool CNetAddr::IsRFC3927() const
# 157 : 201250 : {
# 158 : 201250 : return IsIPv4() && (GetByte(3) == 169 && GetByte(2) == 254);
# 159 : 201250 : }
# 160 : :
# 161 : : bool CNetAddr::IsRFC6598() const
# 162 : 201242 : {
# 163 : 201242 : return IsIPv4() && GetByte(3) == 100 && GetByte(2) >= 64 && GetByte(2) <= 127;
# 164 : 201242 : }
# 165 : :
# 166 : : bool CNetAddr::IsRFC5737() const
# 167 : 201242 : {
# 168 : 201242 : return IsIPv4() && ((GetByte(3) == 192 && GetByte(2) == 0 && GetByte(1) == 2) ||
# 169 : 201118 : (GetByte(3) == 198 && GetByte(2) == 51 && GetByte(1) == 100) ||
# 170 : 201118 : (GetByte(3) == 203 && GetByte(2) == 0 && GetByte(1) == 113));
# 171 : 201242 : }
# 172 : :
# 173 : : bool CNetAddr::IsRFC3849() const
# 174 : 341846 : {
# 175 : 341846 : return IsIPv6() && GetByte(15) == 0x20 && GetByte(14) == 0x01 &&
# 176 : 341846 : GetByte(13) == 0x0D && GetByte(12) == 0xB8;
# 177 : 341846 : }
# 178 : :
# 179 : : bool CNetAddr::IsRFC3964() const
# 180 : 45 : {
# 181 : 45 : return IsIPv6() && GetByte(15) == 0x20 && GetByte(14) == 0x02;
# 182 : 45 : }
# 183 : :
# 184 : : bool CNetAddr::IsRFC6052() const
# 185 : 53 : {
# 186 : 53 : static const unsigned char pchRFC6052[] = {0,0x64,0xFF,0x9B,0,0,0,0,0,0,0,0};
# 187 : 53 : return IsIPv6() && memcmp(ip, pchRFC6052, sizeof(pchRFC6052)) == 0;
# 188 : 53 : }
# 189 : :
# 190 : : bool CNetAddr::IsRFC4380() const
# 191 : 37 : {
# 192 : 37 : return IsIPv6() && GetByte(15) == 0x20 && GetByte(14) == 0x01 && GetByte(13) == 0 &&
# 193 : 37 : GetByte(12) == 0;
# 194 : 37 : }
# 195 : :
# 196 : : bool CNetAddr::IsRFC4862() const
# 197 : 201244 : {
# 198 : 201244 : static const unsigned char pchRFC4862[] = {0xFE,0x80,0,0,0,0,0,0};
# 199 : 201244 : return IsIPv6() && memcmp(ip, pchRFC4862, sizeof(pchRFC4862)) == 0;
# 200 : 201244 : }
# 201 : :
# 202 : : bool CNetAddr::IsRFC4193() const
# 203 : 201244 : {
# 204 : 201244 : return IsIPv6() && (GetByte(15) & 0xFE) == 0xFC;
# 205 : 201244 : }
# 206 : :
# 207 : : bool CNetAddr::IsRFC6145() const
# 208 : 59 : {
# 209 : 59 : static const unsigned char pchRFC6145[] = {0,0,0,0,0,0,0,0,0xFF,0xFF,0,0};
# 210 : 59 : return IsIPv6() && memcmp(ip, pchRFC6145, sizeof(pchRFC6145)) == 0;
# 211 : 59 : }
# 212 : :
# 213 : : bool CNetAddr::IsRFC4843() const
# 214 : 201244 : {
# 215 : 201244 : return IsIPv6() && GetByte(15) == 0x20 && GetByte(14) == 0x01 &&
# 216 : 201244 : GetByte(13) == 0x00 && (GetByte(12) & 0xF0) == 0x10;
# 217 : 201244 : }
# 218 : :
# 219 : : bool CNetAddr::IsRFC7343() const
# 220 : 201244 : {
# 221 : 201244 : return IsIPv6() && GetByte(15) == 0x20 && GetByte(14) == 0x01 &&
# 222 : 201244 : GetByte(13) == 0x00 && (GetByte(12) & 0xF0) == 0x20;
# 223 : 201244 : }
# 224 : :
# 225 : : bool CNetAddr::IsHeNet() const
# 226 : 4 : {
# 227 : 4 : return (GetByte(15) == 0x20 && GetByte(14) == 0x01 && GetByte(13) == 0x04 && GetByte(12) == 0x70);
# 228 : 4 : }
# 229 : :
# 230 : : /**
# 231 : : * @returns Whether or not this is a dummy address that maps an onion address
# 232 : : * into IPv6.
# 233 : : *
# 234 : : * @see CNetAddr::SetSpecial(const std::string &)
# 235 : : */
# 236 : 166059 : bool CNetAddr::IsTor() const { return m_net == NET_ONION; }
# 237 : :
# 238 : : bool CNetAddr::IsLocal() const
# 239 : 326575 : {
# 240 : 326575 : // IPv4 loopback (127.0.0.0/8 or 0.0.0.0/8)
# 241 : 326575 : if (IsIPv4() && (GetByte(3) == 127 || GetByte(3) == 0))
# 242 : 19451 : return true;
# 243 : 307124 :
# 244 : 307124 : // IPv6 loopback (::1/128)
# 245 : 307124 : static const unsigned char pchLocal[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
# 246 : 307124 : if (IsIPv6() && memcmp(ip, pchLocal, 16) == 0)
# 247 : 4 : return true;
# 248 : 307120 :
# 249 : 307120 : return false;
# 250 : 307120 : }
# 251 : :
# 252 : : /**
# 253 : : * @returns Whether or not this network address is a valid address that @a could
# 254 : : * be used to refer to an actual host.
# 255 : : *
# 256 : : * @note A valid address may or may not be publicly routable on the global
# 257 : : * internet. As in, the set of valid addresses is a superset of the set of
# 258 : : * publicly routable addresses.
# 259 : : *
# 260 : : * @see CNetAddr::IsRoutable()
# 261 : : */
# 262 : : bool CNetAddr::IsValid() const
# 263 : 399194 : {
# 264 : 399194 : // Cleanup 3-byte shifted addresses caused by garbage in size field
# 265 : 399194 : // of addr messages from versions before 0.2.9 checksum.
# 266 : 399194 : // Two consecutive addr messages look like this:
# 267 : 399194 : // header20 vectorlen3 addr26 addr26 addr26 header20 vectorlen3 addr26 addr26 addr26...
# 268 : 399194 : // so if the first length field is garbled, it reads the second batch
# 269 : 399194 : // of addr misaligned by 3 bytes.
# 270 : 399194 : if (IsIPv6() && memcmp(ip, pchIPv4+3, sizeof(pchIPv4)-3) == 0)
# 271 : 0 : return false;
# 272 : 399194 :
# 273 : 399194 : // unspecified IPv6 address (::/128)
# 274 : 399194 : unsigned char ipNone6[16] = {};
# 275 : 399194 : if (IsIPv6() && memcmp(ip, ipNone6, 16) == 0)
# 276 : 57350 : return false;
# 277 : 341844 :
# 278 : 341844 : // documentation IPv6 address
# 279 : 341844 : if (IsRFC3849())
# 280 : 0 : return false;
# 281 : 341844 :
# 282 : 341844 : if (IsInternal())
# 283 : 0 : return false;
# 284 : 341844 :
# 285 : 341844 : if (IsIPv4())
# 286 : 341163 : {
# 287 : 341163 : // INADDR_NONE
# 288 : 341163 : uint32_t ipNone = INADDR_NONE;
# 289 : 341163 : if (memcmp(ip+12, &ipNone, 4) == 0)
# 290 : 0 : return false;
# 291 : 341163 :
# 292 : 341163 : // 0
# 293 : 341163 : ipNone = 0;
# 294 : 341163 : if (memcmp(ip+12, &ipNone, 4) == 0)
# 295 : 7 : return false;
# 296 : 341837 : }
# 297 : 341837 :
# 298 : 341837 : return true;
# 299 : 341837 : }
# 300 : :
# 301 : : /**
# 302 : : * @returns Whether or not this network address is publicly routable on the
# 303 : : * global internet.
# 304 : : *
# 305 : : * @note A routable address is always valid. As in, the set of routable addresses
# 306 : : * is a subset of the set of valid addresses.
# 307 : : *
# 308 : : * @see CNetAddr::IsValid()
# 309 : : */
# 310 : : bool CNetAddr::IsRoutable() const
# 311 : 231551 : {
# 312 : 231551 : return IsValid() && !(IsRFC1918() || IsRFC2544() || IsRFC3927() || IsRFC4862() || IsRFC6598() || IsRFC5737() || (IsRFC4193() && !IsTor()) || IsRFC4843() || IsRFC7343() || IsLocal() || IsInternal());
# 313 : 231551 : }
# 314 : :
# 315 : : /**
# 316 : : * @returns Whether or not this is a dummy address that maps a name into IPv6.
# 317 : : *
# 318 : : * @see CNetAddr::SetInternal(const std::string &)
# 319 : : */
# 320 : : bool CNetAddr::IsInternal() const
# 321 : 1046311 : {
# 322 : 1046311 : return m_net == NET_INTERNAL;
# 323 : 1046311 : }
# 324 : :
# 325 : : enum Network CNetAddr::GetNetwork() const
# 326 : 592 : {
# 327 : 592 : if (IsInternal())
# 328 : 2 : return NET_INTERNAL;
# 329 : 590 :
# 330 : 590 : if (!IsRoutable())
# 331 : 244 : return NET_UNROUTABLE;
# 332 : 346 :
# 333 : 346 : return m_net;
# 334 : 346 : }
# 335 : :
# 336 : : std::string CNetAddr::ToStringIP() const
# 337 : 164622 : {
# 338 : 164622 : if (IsTor())
# 339 : 5 : return EncodeBase32(&ip[6], 10) + ".onion";
# 340 : 164617 : if (IsInternal())
# 341 : 0 : return EncodeBase32(ip + sizeof(g_internal_prefix), sizeof(ip) - sizeof(g_internal_prefix)) + ".internal";
# 342 : 164617 : CService serv(*this, 0);
# 343 : 164617 : struct sockaddr_storage sockaddr;
# 344 : 164617 : socklen_t socklen = sizeof(sockaddr);
# 345 : 164617 : if (serv.GetSockAddr((struct sockaddr*)&sockaddr, &socklen)) {
# 346 : 164617 : char name[1025] = "";
# 347 : 164617 : if (!getnameinfo((const struct sockaddr*)&sockaddr, socklen, name, sizeof(name), nullptr, 0, NI_NUMERICHOST))
# 348 : 164617 : return std::string(name);
# 349 : 0 : }
# 350 : 0 : if (IsIPv4())
# 351 : 0 : return strprintf("%u.%u.%u.%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0));
# 352 : 0 : else
# 353 : 0 : return strprintf("%x:%x:%x:%x:%x:%x:%x:%x",
# 354 : 0 : GetByte(15) << 8 | GetByte(14), GetByte(13) << 8 | GetByte(12),
# 355 : 0 : GetByte(11) << 8 | GetByte(10), GetByte(9) << 8 | GetByte(8),
# 356 : 0 : GetByte(7) << 8 | GetByte(6), GetByte(5) << 8 | GetByte(4),
# 357 : 0 : GetByte(3) << 8 | GetByte(2), GetByte(1) << 8 | GetByte(0));
# 358 : 0 : }
# 359 : :
# 360 : : std::string CNetAddr::ToString() const
# 361 : 5617 : {
# 362 : 5617 : return ToStringIP();
# 363 : 5617 : }
# 364 : :
# 365 : : bool operator==(const CNetAddr& a, const CNetAddr& b)
# 366 : 10731 : {
# 367 : 10731 : return a.m_net == b.m_net && memcmp(a.ip, b.ip, 16) == 0;
# 368 : 10731 : }
# 369 : :
# 370 : : bool operator<(const CNetAddr& a, const CNetAddr& b)
# 371 : 151797 : {
# 372 : 151797 : return a.m_net < b.m_net || (a.m_net == b.m_net && memcmp(a.ip, b.ip, 16) < 0);
# 373 : 151797 : }
# 374 : :
# 375 : : /**
# 376 : : * Try to get our IPv4 address.
# 377 : : *
# 378 : : * @param[out] pipv4Addr The in_addr struct to which to copy.
# 379 : : *
# 380 : : * @returns Whether or not the operation was successful, in particular, whether
# 381 : : * or not our address was an IPv4 address.
# 382 : : *
# 383 : : * @see CNetAddr::IsIPv4()
# 384 : : */
# 385 : : bool CNetAddr::GetInAddr(struct in_addr* pipv4Addr) const
# 386 : 164123 : {
# 387 : 164123 : if (!IsIPv4())
# 388 : 0 : return false;
# 389 : 164123 : memcpy(pipv4Addr, ip+12, 4);
# 390 : 164123 : return true;
# 391 : 164123 : }
# 392 : :
# 393 : : /**
# 394 : : * Try to get our IPv6 address.
# 395 : : *
# 396 : : * @param[out] pipv6Addr The in6_addr struct to which to copy.
# 397 : : *
# 398 : : * @returns Whether or not the operation was successful, in particular, whether
# 399 : : * or not our address was an IPv6 address.
# 400 : : *
# 401 : : * @see CNetAddr::IsIPv6()
# 402 : : */
# 403 : : bool CNetAddr::GetIn6Addr(struct in6_addr* pipv6Addr) const
# 404 : 1950 : {
# 405 : 1950 : if (!IsIPv6()) {
# 406 : 0 : return false;
# 407 : 0 : }
# 408 : 1950 : memcpy(pipv6Addr, ip, 16);
# 409 : 1950 : return true;
# 410 : 1950 : }
# 411 : :
# 412 : : bool CNetAddr::HasLinkedIPv4() const
# 413 : 99451 : {
# 414 : 99451 : return IsRoutable() && (IsIPv4() || IsRFC6145() || IsRFC6052() || IsRFC3964() || IsRFC4380());
# 415 : 99451 : }
# 416 : :
# 417 : : uint32_t CNetAddr::GetLinkedIPv4() const
# 418 : 31832 : {
# 419 : 31832 : if (IsIPv4() || IsRFC6145() || IsRFC6052()) {
# 420 : 31828 : // IPv4, mapped IPv4, SIIT translated IPv4: the IPv4 address is the last 4 bytes of the address
# 421 : 31828 : return ReadBE32(ip + 12);
# 422 : 31828 : } else if (IsRFC3964()) {
# 423 : 2 : // 6to4 tunneled IPv4: the IPv4 address is in bytes 2-6
# 424 : 2 : return ReadBE32(ip + 2);
# 425 : 2 : } else if (IsRFC4380()) {
# 426 : 2 : // Teredo tunneled IPv4: the IPv4 address is in the last 4 bytes of the address, but bitflipped
# 427 : 2 : return ~ReadBE32(ip + 12);
# 428 : 2 : }
# 429 : 0 : assert(false);
# 430 : 0 : }
# 431 : :
# 432 : 95507 : uint32_t CNetAddr::GetNetClass() const {
# 433 : 95507 : uint32_t net_class = NET_IPV6;
# 434 : 95507 : if (IsLocal()) {
# 435 : 8379 : net_class = 255;
# 436 : 8379 : }
# 437 : 95507 : if (IsInternal()) {
# 438 : 4 : net_class = NET_INTERNAL;
# 439 : 95503 : } else if (!IsRoutable()) {
# 440 : 27893 : net_class = NET_UNROUTABLE;
# 441 : 67610 : } else if (HasLinkedIPv4()) {
# 442 : 67592 : net_class = NET_IPV4;
# 443 : 67592 : } else if (IsTor()) {
# 444 : 10 : net_class = NET_ONION;
# 445 : 10 : }
# 446 : 95507 : return net_class;
# 447 : 95507 : }
# 448 : :
# 449 : 58530 : uint32_t CNetAddr::GetMappedAS(const std::vector<bool> &asmap) const {
# 450 : 58530 : uint32_t net_class = GetNetClass();
# 451 : 58530 : if (asmap.size() == 0 || (net_class != NET_IPV4 && net_class != NET_IPV6)) {
# 452 : 45708 : return 0; // Indicates not found, safe because AS0 is reserved per RFC7607.
# 453 : 45708 : }
# 454 : 12822 : std::vector<bool> ip_bits(128);
# 455 : 12822 : if (HasLinkedIPv4()) {
# 456 : 12822 : // For lookup, treat as if it was just an IPv4 address (pchIPv4 prefix + IPv4 bits)
# 457 : 166686 : for (int8_t byte_i = 0; byte_i < 12; ++byte_i) {
# 458 : 1384776 : for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
# 459 : 1230912 : ip_bits[byte_i * 8 + bit_i] = (pchIPv4[byte_i] >> (7 - bit_i)) & 1;
# 460 : 1230912 : }
# 461 : 153864 : }
# 462 : 12822 : uint32_t ipv4 = GetLinkedIPv4();
# 463 : 423126 : for (int i = 0; i < 32; ++i) {
# 464 : 410304 : ip_bits[96 + i] = (ipv4 >> (31 - i)) & 1;
# 465 : 410304 : }
# 466 : 12822 : } else {
# 467 : 0 : // Use all 128 bits of the IPv6 address otherwise
# 468 : 0 : for (int8_t byte_i = 0; byte_i < 16; ++byte_i) {
# 469 : 0 : uint8_t cur_byte = GetByte(15 - byte_i);
# 470 : 0 : for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
# 471 : 0 : ip_bits[byte_i * 8 + bit_i] = (cur_byte >> (7 - bit_i)) & 1;
# 472 : 0 : }
# 473 : 0 : }
# 474 : 0 : }
# 475 : 12822 : uint32_t mapped_as = Interpret(asmap, ip_bits);
# 476 : 12822 : return mapped_as;
# 477 : 12822 : }
# 478 : :
# 479 : : /**
# 480 : : * Get the canonical identifier of our network group
# 481 : : *
# 482 : : * The groups are assigned in a way where it should be costly for an attacker to
# 483 : : * obtain addresses with many different group identifiers, even if it is cheap
# 484 : : * to obtain addresses with the same identifier.
# 485 : : *
# 486 : : * @note No two connections will be attempted to addresses with the same network
# 487 : : * group.
# 488 : : */
# 489 : : std::vector<unsigned char> CNetAddr::GetGroup(const std::vector<bool> &asmap) const
# 490 : 36977 : {
# 491 : 36977 : std::vector<unsigned char> vchRet;
# 492 : 36977 : uint32_t net_class = GetNetClass();
# 493 : 36977 : // If non-empty asmap is supplied and the address is IPv4/IPv6,
# 494 : 36977 : // return ASN to be used for bucketing.
# 495 : 36977 : uint32_t asn = GetMappedAS(asmap);
# 496 : 36977 : if (asn != 0) { // Either asmap was empty, or address has non-asmappable net class (e.g. TOR).
# 497 : 7246 : vchRet.push_back(NET_IPV6); // IPv4 and IPv6 with same ASN should be in the same bucket
# 498 : 36230 : for (int i = 0; i < 4; i++) {
# 499 : 28984 : vchRet.push_back((asn >> (8 * i)) & 0xFF);
# 500 : 28984 : }
# 501 : 7246 : return vchRet;
# 502 : 7246 : }
# 503 : 29731 :
# 504 : 29731 : vchRet.push_back(net_class);
# 505 : 29731 : int nStartByte = 0;
# 506 : 29731 : int nBits = 16;
# 507 : 29731 :
# 508 : 29731 : if (IsLocal()) {
# 509 : 953 : // all local addresses belong to the same group
# 510 : 953 : nBits = 0;
# 511 : 28778 : } else if (IsInternal()) {
# 512 : 2 : // all internal-usage addresses get their own group
# 513 : 2 : nStartByte = sizeof(g_internal_prefix);
# 514 : 2 : nBits = (sizeof(ip) - sizeof(g_internal_prefix)) * 8;
# 515 : 28776 : } else if (!IsRoutable()) {
# 516 : 9757 : // all other unroutable addresses belong to the same group
# 517 : 9757 : nBits = 0;
# 518 : 19019 : } else if (HasLinkedIPv4()) {
# 519 : 19010 : // IPv4 addresses (and mapped IPv4 addresses) use /16 groups
# 520 : 19010 : uint32_t ipv4 = GetLinkedIPv4();
# 521 : 19010 : vchRet.push_back((ipv4 >> 24) & 0xFF);
# 522 : 19010 : vchRet.push_back((ipv4 >> 16) & 0xFF);
# 523 : 19010 : return vchRet;
# 524 : 19010 : } else if (IsTor()) {
# 525 : 5 : nStartByte = 6;
# 526 : 5 : nBits = 4;
# 527 : 5 : } else if (IsHeNet()) {
# 528 : 2 : // for he.net, use /36 groups
# 529 : 2 : nBits = 36;
# 530 : 2 : } else {
# 531 : 2 : // for the rest of the IPv6 network, use /32 groups
# 532 : 2 : nBits = 32;
# 533 : 2 : }
# 534 : 29731 :
# 535 : 29731 : // push our ip onto vchRet byte by byte...
# 536 : 29731 : while (nBits >= 8)
# 537 : 36 : {
# 538 : 36 : vchRet.push_back(GetByte(15 - nStartByte));
# 539 : 36 : nStartByte++;
# 540 : 36 : nBits -= 8;
# 541 : 36 : }
# 542 : 10721 : // ...for the last byte, push nBits and for the rest of the byte push 1's
# 543 : 10721 : if (nBits > 0)
# 544 : 7 : vchRet.push_back(GetByte(15 - nStartByte) | ((1 << (8 - nBits)) - 1));
# 545 : 10721 :
# 546 : 10721 : return vchRet;
# 547 : 29731 : }
# 548 : :
# 549 : : uint64_t CNetAddr::GetHash() const
# 550 : 10 : {
# 551 : 10 : uint256 hash = Hash(&ip[0], &ip[16]);
# 552 : 10 : uint64_t nRet;
# 553 : 10 : memcpy(&nRet, &hash, sizeof(nRet));
# 554 : 10 : return nRet;
# 555 : 10 : }
# 556 : :
# 557 : : // private extensions to enum Network, only returned by GetExtNetwork,
# 558 : : // and only used in GetReachabilityFrom
# 559 : : static const int NET_UNKNOWN = NET_MAX + 0;
# 560 : : static const int NET_TEREDO = NET_MAX + 1;
# 561 : : int static GetExtNetwork(const CNetAddr *addr)
# 562 : 0 : {
# 563 : 0 : if (addr == nullptr)
# 564 : 0 : return NET_UNKNOWN;
# 565 : 0 : if (addr->IsRFC4380())
# 566 : 0 : return NET_TEREDO;
# 567 : 0 : return addr->GetNetwork();
# 568 : 0 : }
# 569 : :
# 570 : : /** Calculates a metric for how reachable (*this) is from a given partner */
# 571 : : int CNetAddr::GetReachabilityFrom(const CNetAddr *paddrPartner) const
# 572 : 2 : {
# 573 : 2 : enum Reachability {
# 574 : 2 : REACH_UNREACHABLE,
# 575 : 2 : REACH_DEFAULT,
# 576 : 2 : REACH_TEREDO,
# 577 : 2 : REACH_IPV6_WEAK,
# 578 : 2 : REACH_IPV4,
# 579 : 2 : REACH_IPV6_STRONG,
# 580 : 2 : REACH_PRIVATE
# 581 : 2 : };
# 582 : 2 :
# 583 : 2 : if (!IsRoutable() || IsInternal())
# 584 : 2 : return REACH_UNREACHABLE;
# 585 : 0 :
# 586 : 0 : int ourNet = GetExtNetwork(this);
# 587 : 0 : int theirNet = GetExtNetwork(paddrPartner);
# 588 : 0 : bool fTunnel = IsRFC3964() || IsRFC6052() || IsRFC6145();
# 589 : 0 :
# 590 : 0 : switch(theirNet) {
# 591 : 0 : case NET_IPV4:
# 592 : 0 : switch(ourNet) {
# 593 : 0 : default: return REACH_DEFAULT;
# 594 : 0 : case NET_IPV4: return REACH_IPV4;
# 595 : 0 : }
# 596 : 0 : case NET_IPV6:
# 597 : 0 : switch(ourNet) {
# 598 : 0 : default: return REACH_DEFAULT;
# 599 : 0 : case NET_TEREDO: return REACH_TEREDO;
# 600 : 0 : case NET_IPV4: return REACH_IPV4;
# 601 : 0 : case NET_IPV6: return fTunnel ? REACH_IPV6_WEAK : REACH_IPV6_STRONG; // only prefer giving our IPv6 address if it's not tunnelled
# 602 : 0 : }
# 603 : 0 : case NET_ONION:
# 604 : 0 : switch(ourNet) {
# 605 : 0 : default: return REACH_DEFAULT;
# 606 : 0 : case NET_IPV4: return REACH_IPV4; // Tor users can connect to IPv4 as well
# 607 : 0 : case NET_ONION: return REACH_PRIVATE;
# 608 : 0 : }
# 609 : 0 : case NET_TEREDO:
# 610 : 0 : switch(ourNet) {
# 611 : 0 : default: return REACH_DEFAULT;
# 612 : 0 : case NET_TEREDO: return REACH_TEREDO;
# 613 : 0 : case NET_IPV6: return REACH_IPV6_WEAK;
# 614 : 0 : case NET_IPV4: return REACH_IPV4;
# 615 : 0 : }
# 616 : 0 : case NET_UNKNOWN:
# 617 : 0 : case NET_UNROUTABLE:
# 618 : 0 : default:
# 619 : 0 : switch(ourNet) {
# 620 : 0 : default: return REACH_DEFAULT;
# 621 : 0 : case NET_TEREDO: return REACH_TEREDO;
# 622 : 0 : case NET_IPV6: return REACH_IPV6_WEAK;
# 623 : 0 : case NET_IPV4: return REACH_IPV4;
# 624 : 0 : case NET_ONION: return REACH_PRIVATE; // either from Tor, or don't care about our address
# 625 : 0 : }
# 626 : 0 : }
# 627 : 0 : }
# 628 : :
# 629 : : CService::CService() : port(0)
# 630 : 688117 : {
# 631 : 688117 : }
# 632 : :
# 633 : : CService::CService(const CNetAddr& cip, uint16_t portIn) : CNetAddr(cip), port(portIn)
# 634 : 372432 : {
# 635 : 372432 : }
# 636 : :
# 637 : : CService::CService(const struct in_addr& ipv4Addr, uint16_t portIn) : CNetAddr(ipv4Addr), port(portIn)
# 638 : 5 : {
# 639 : 5 : }
# 640 : :
# 641 : : CService::CService(const struct in6_addr& ipv6Addr, uint16_t portIn) : CNetAddr(ipv6Addr), port(portIn)
# 642 : 3 : {
# 643 : 3 : }
# 644 : :
# 645 : : CService::CService(const struct sockaddr_in& addr) : CNetAddr(addr.sin_addr), port(ntohs(addr.sin_port))
# 646 : 1145 : {
# 647 : 1145 : assert(addr.sin_family == AF_INET);
# 648 : 1145 : }
# 649 : :
# 650 : : CService::CService(const struct sockaddr_in6 &addr) : CNetAddr(addr.sin6_addr, addr.sin6_scope_id), port(ntohs(addr.sin6_port))
# 651 : 0 : {
# 652 : 0 : assert(addr.sin6_family == AF_INET6);
# 653 : 0 : }
# 654 : :
# 655 : : bool CService::SetSockAddr(const struct sockaddr *paddr)
# 656 : 1145 : {
# 657 : 1145 : switch (paddr->sa_family) {
# 658 : 1145 : case AF_INET:
# 659 : 1145 : *this = CService(*(const struct sockaddr_in*)paddr);
# 660 : 1145 : return true;
# 661 : 0 : case AF_INET6:
# 662 : 0 : *this = CService(*(const struct sockaddr_in6*)paddr);
# 663 : 0 : return true;
# 664 : 0 : default:
# 665 : 0 : return false;
# 666 : 1145 : }
# 667 : 1145 : }
# 668 : :
# 669 : : uint16_t CService::GetPort() const
# 670 : 156 : {
# 671 : 156 : return port;
# 672 : 156 : }
# 673 : :
# 674 : : bool operator==(const CService& a, const CService& b)
# 675 : 990 : {
# 676 : 990 : return static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port == b.port;
# 677 : 990 : }
# 678 : :
# 679 : : bool operator<(const CService& a, const CService& b)
# 680 : 4786 : {
# 681 : 4786 : return static_cast<CNetAddr>(a) < static_cast<CNetAddr>(b) || (static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port < b.port);
# 682 : 4786 : }
# 683 : :
# 684 : : /**
# 685 : : * Obtain the IPv4/6 socket address this represents.
# 686 : : *
# 687 : : * @param[out] paddr The obtained socket address.
# 688 : : * @param[in,out] addrlen The size, in bytes, of the address structure pointed
# 689 : : * to by paddr. The value that's pointed to by this
# 690 : : * parameter might change after calling this function if
# 691 : : * the size of the corresponding address structure
# 692 : : * changed.
# 693 : : *
# 694 : : * @returns Whether or not the operation was successful.
# 695 : : */
# 696 : : bool CService::GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const
# 697 : 166073 : {
# 698 : 166073 : if (IsIPv4()) {
# 699 : 164123 : if (*addrlen < (socklen_t)sizeof(struct sockaddr_in))
# 700 : 0 : return false;
# 701 : 164123 : *addrlen = sizeof(struct sockaddr_in);
# 702 : 164123 : struct sockaddr_in *paddrin = (struct sockaddr_in*)paddr;
# 703 : 164123 : memset(paddrin, 0, *addrlen);
# 704 : 164123 : if (!GetInAddr(&paddrin->sin_addr))
# 705 : 0 : return false;
# 706 : 164123 : paddrin->sin_family = AF_INET;
# 707 : 164123 : paddrin->sin_port = htons(port);
# 708 : 164123 : return true;
# 709 : 164123 : }
# 710 : 1950 : if (IsIPv6()) {
# 711 : 1950 : if (*addrlen < (socklen_t)sizeof(struct sockaddr_in6))
# 712 : 0 : return false;
# 713 : 1950 : *addrlen = sizeof(struct sockaddr_in6);
# 714 : 1950 : struct sockaddr_in6 *paddrin6 = (struct sockaddr_in6*)paddr;
# 715 : 1950 : memset(paddrin6, 0, *addrlen);
# 716 : 1950 : if (!GetIn6Addr(&paddrin6->sin6_addr))
# 717 : 0 : return false;
# 718 : 1950 : paddrin6->sin6_scope_id = scopeId;
# 719 : 1950 : paddrin6->sin6_family = AF_INET6;
# 720 : 1950 : paddrin6->sin6_port = htons(port);
# 721 : 1950 : return true;
# 722 : 1950 : }
# 723 : 0 : return false;
# 724 : 0 : }
# 725 : :
# 726 : : /**
# 727 : : * @returns An identifier unique to this service's address and port number.
# 728 : : */
# 729 : : std::vector<unsigned char> CService::GetKey() const
# 730 : 1255701 : {
# 731 : 1255701 : auto key = GetAddrBytes();
# 732 : 1255701 : key.push_back(port / 0x100); // most significant byte of our port
# 733 : 1255701 : key.push_back(port & 0x0FF); // least significant byte of our port
# 734 : 1255701 : return key;
# 735 : 1255701 : }
# 736 : :
# 737 : : std::string CService::ToStringPort() const
# 738 : 143893 : {
# 739 : 143893 : return strprintf("%u", port);
# 740 : 143893 : }
# 741 : :
# 742 : : std::string CService::ToStringIPPort() const
# 743 : 143893 : {
# 744 : 143893 : if (IsIPv4() || IsTor() || IsInternal()) {
# 745 : 142487 : return ToStringIP() + ":" + ToStringPort();
# 746 : 142487 : } else {
# 747 : 1406 : return "[" + ToStringIP() + "]:" + ToStringPort();
# 748 : 1406 : }
# 749 : 143893 : }
# 750 : :
# 751 : : std::string CService::ToString() const
# 752 : 138949 : {
# 753 : 138949 : return ToStringIPPort();
# 754 : 138949 : }
# 755 : :
# 756 : : CSubNet::CSubNet():
# 757 : : valid(false)
# 758 : 316 : {
# 759 : 316 : memset(netmask, 0, sizeof(netmask));
# 760 : 316 : }
# 761 : :
# 762 : : CSubNet::CSubNet(const CNetAddr &addr, int32_t mask)
# 763 : 593 : {
# 764 : 593 : valid = true;
# 765 : 593 : network = addr;
# 766 : 593 : // Default to /32 (IPv4) or /128 (IPv6), i.e. match single address
# 767 : 593 : memset(netmask, 255, sizeof(netmask));
# 768 : 593 :
# 769 : 593 : // IPv4 addresses start at offset 12, and first 12 bytes must match, so just offset n
# 770 : 593 : const int astartofs = network.IsIPv4() ? 12 : 0;
# 771 : 593 :
# 772 : 593 : int32_t n = mask;
# 773 : 593 : if(n >= 0 && n <= (128 - astartofs*8)) // Only valid if in range of bits of address
# 774 : 584 : {
# 775 : 584 : n += astartofs*8;
# 776 : 584 : // Clear bits [n..127]
# 777 : 14999 : for (; n < 128; ++n)
# 778 : 14415 : netmask[n>>3] &= ~(1<<(7-(n&7)));
# 779 : 584 : } else
# 780 : 9 : valid = false;
# 781 : 593 :
# 782 : 593 : // Normalize network according to netmask
# 783 : 10081 : for(int x=0; x<16; ++x)
# 784 : 9488 : network.ip[x] &= netmask[x];
# 785 : 593 : }
# 786 : :
# 787 : : CSubNet::CSubNet(const CNetAddr &addr, const CNetAddr &mask)
# 788 : 90 : {
# 789 : 90 : valid = true;
# 790 : 90 : network = addr;
# 791 : 90 : // Default to /32 (IPv4) or /128 (IPv6), i.e. match single address
# 792 : 90 : memset(netmask, 255, sizeof(netmask));
# 793 : 90 :
# 794 : 90 : // IPv4 addresses start at offset 12, and first 12 bytes must match, so just offset n
# 795 : 90 : const int astartofs = network.IsIPv4() ? 12 : 0;
# 796 : 90 :
# 797 : 570 : for(int x=astartofs; x<16; ++x)
# 798 : 480 : netmask[x] = mask.ip[x];
# 799 : 90 :
# 800 : 90 : // Normalize network according to netmask
# 801 : 1530 : for(int x=0; x<16; ++x)
# 802 : 1440 : network.ip[x] &= netmask[x];
# 803 : 90 : }
# 804 : :
# 805 : : CSubNet::CSubNet(const CNetAddr &addr):
# 806 : : valid(addr.IsValid())
# 807 : 623 : {
# 808 : 623 : memset(netmask, 255, sizeof(netmask));
# 809 : 623 : network = addr;
# 810 : 623 : }
# 811 : :
# 812 : : /**
# 813 : : * @returns True if this subnet is valid, the specified address is valid, and
# 814 : : * the specified address belongs in this subnet.
# 815 : : */
# 816 : : bool CSubNet::Match(const CNetAddr &addr) const
# 817 : 64468 : {
# 818 : 64468 : if (!valid || !addr.IsValid() || network.m_net != addr.m_net)
# 819 : 16 : return false;
# 820 : 1095650 : for(int x=0; x<16; ++x)
# 821 : 1031212 : if ((addr.ip[x] & netmask[x]) != network.ip[x])
# 822 : 14 : return false;
# 823 : 64452 : return true;
# 824 : 64452 : }
# 825 : :
# 826 : : /**
# 827 : : * @returns The number of 1-bits in the prefix of the specified subnet mask. If
# 828 : : * the specified subnet mask is not a valid one, -1.
# 829 : : */
# 830 : : static inline int NetmaskBits(uint8_t x)
# 831 : 610 : {
# 832 : 610 : switch(x) {
# 833 : 545 : case 0x00: return 0;
# 834 : 8 : case 0x80: return 1;
# 835 : 8 : case 0xc0: return 2;
# 836 : 11 : case 0xe0: return 3;
# 837 : 8 : case 0xf0: return 4;
# 838 : 8 : case 0xf8: return 5;
# 839 : 10 : case 0xfc: return 6;
# 840 : 10 : case 0xfe: return 7;
# 841 : 0 : case 0xff: return 8;
# 842 : 2 : default: return -1;
# 843 : 610 : }
# 844 : 610 : }
# 845 : :
# 846 : : std::string CSubNet::ToString() const
# 847 : 1151 : {
# 848 : 1151 : /* Parse binary 1{n}0{N-n} to see if mask can be represented as /n */
# 849 : 1151 : int cidr = 0;
# 850 : 1151 : bool valid_cidr = true;
# 851 : 1151 : int n = network.IsIPv4() ? 12 : 0;
# 852 : 10270 : for (; n < 16 && netmask[n] == 0xff; ++n)
# 853 : 9119 : cidr += 8;
# 854 : 1151 : if (n < 16) {
# 855 : 610 : int bits = NetmaskBits(netmask[n]);
# 856 : 610 : if (bits < 0)
# 857 : 2 : valid_cidr = false;
# 858 : 608 : else
# 859 : 608 : cidr += bits;
# 860 : 610 : ++n;
# 861 : 610 : }
# 862 : 2418 : for (; n < 16 && valid_cidr; ++n)
# 863 : 1267 : if (netmask[n] != 0x00)
# 864 : 2 : valid_cidr = false;
# 865 : 1151 :
# 866 : 1151 : /* Format output */
# 867 : 1151 : std::string strNetmask;
# 868 : 1151 : if (valid_cidr) {
# 869 : 1147 : strNetmask = strprintf("%u", cidr);
# 870 : 1147 : } else {
# 871 : 4 : if (network.IsIPv4())
# 872 : 2 : strNetmask = strprintf("%u.%u.%u.%u", netmask[12], netmask[13], netmask[14], netmask[15]);
# 873 : 2 : else
# 874 : 2 : strNetmask = strprintf("%x:%x:%x:%x:%x:%x:%x:%x",
# 875 : 2 : netmask[0] << 8 | netmask[1], netmask[2] << 8 | netmask[3],
# 876 : 2 : netmask[4] << 8 | netmask[5], netmask[6] << 8 | netmask[7],
# 877 : 2 : netmask[8] << 8 | netmask[9], netmask[10] << 8 | netmask[11],
# 878 : 2 : netmask[12] << 8 | netmask[13], netmask[14] << 8 | netmask[15]);
# 879 : 4 : }
# 880 : 1151 :
# 881 : 1151 : return network.ToString() + "/" + strNetmask;
# 882 : 1151 : }
# 883 : :
# 884 : : bool CSubNet::IsValid() const
# 885 : 329 : {
# 886 : 329 : return valid;
# 887 : 329 : }
# 888 : :
# 889 : : bool operator==(const CSubNet& a, const CSubNet& b)
# 890 : 4 : {
# 891 : 4 : return a.valid == b.valid && a.network == b.network && !memcmp(a.netmask, b.netmask, 16);
# 892 : 4 : }
# 893 : :
# 894 : : bool operator<(const CSubNet& a, const CSubNet& b)
# 895 : 103 : {
# 896 : 103 : return (a.network < b.network || (a.network == b.network && memcmp(a.netmask, b.netmask, 16) < 0));
# 897 : 103 : }
# 898 : :
# 899 : : bool SanityCheckASMap(const std::vector<bool>& asmap)
# 900 : 5 : {
# 901 : 5 : return SanityCheckASMap(asmap, 128); // For IP address lookups, the input is 128 bits
# 902 : 5 : }
|