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