| 416 | */ |
| 417 | template <typename Stream> |
| 418 | void UnserializeV2Stream(Stream& s) |
| 419 | { |
| 420 | uint8_t bip155_net; |
| 421 | s >> bip155_net; |
| 422 | |
| 423 | size_t address_size; |
| 424 | s >> COMPACTSIZE(address_size); |
| 425 | |
| 426 | if (address_size > MAX_ADDRV2_SIZE) { |
| 427 | throw std::ios_base::failure(strprintf( |
| 428 | "Address too long: %u > %u", address_size, MAX_ADDRV2_SIZE)); |
| 429 | } |
| 430 | |
| 431 | m_scope_id = 0; |
| 432 | |
| 433 | if (SetNetFromBIP155Network(bip155_net, address_size)) { |
| 434 | m_addr.resize(address_size); |
| 435 | s >> Span{m_addr}; |
| 436 | |
| 437 | if (m_net != NET_IPV6) { |
| 438 | return; |
| 439 | } |
| 440 | |
| 441 | // Do some special checks on IPv6 addresses. |
| 442 | |
| 443 | // Recognize NET_INTERNAL embedded in IPv6, such addresses are not |
| 444 | // gossiped but could be coming from addrman, when unserializing from |
| 445 | // disk. |
| 446 | if (HasPrefix(m_addr, INTERNAL_IN_IPV6_PREFIX)) { |
| 447 | m_net = NET_INTERNAL; |
| 448 | memmove(m_addr.data(), m_addr.data() + INTERNAL_IN_IPV6_PREFIX.size(), |
| 449 | ADDR_INTERNAL_SIZE); |
| 450 | m_addr.resize(ADDR_INTERNAL_SIZE); |
| 451 | return; |
| 452 | } |
| 453 | |
| 454 | if (!HasPrefix(m_addr, IPV4_IN_IPV6_PREFIX) && |
| 455 | !HasPrefix(m_addr, TORV2_IN_IPV6_PREFIX)) { |
| 456 | return; |
| 457 | } |
| 458 | |
| 459 | // IPv4 and TORv2 are not supposed to be embedded in IPv6 (like in V1 |
| 460 | // encoding). Unserialize as !IsValid(), thus ignoring them. |
| 461 | } else { |
| 462 | // If we receive an unknown BIP155 network id (from the future?) then |
| 463 | // ignore the address - unserialize as !IsValid(). |
| 464 | s.ignore(address_size); |
| 465 | } |
| 466 | |
| 467 | // Mimic a default-constructed CNetAddr object which is !IsValid() and thus |
| 468 | // will not be gossiped, but continue reading next addresses from the stream. |
| 469 | m_net = NET_IPV6; |
| 470 | m_addr.assign(ADDR_IPV6_SIZE, 0x0); |
| 471 | } |
| 472 | }; |
| 473 | |
| 474 | class CSubNet |