aboutsummaryrefslogtreecommitdiff
path: root/src/netaddress.cpp
diff options
context:
space:
mode:
authorVasil Dimov <vd@FreeBSD.org>2020-05-19 15:01:50 +0200
committerVasil Dimov <vd@FreeBSD.org>2020-09-17 22:17:17 +0200
commite0d73573a37bf4b519f6f61e5678572d48a64517 (patch)
tree62d7661d4afc81dc8b3547b4d57a31f49fd9cc8f /src/netaddress.cpp
parentfe42411b4b07b99c591855f5f00ad45dfeec8e30 (diff)
downloadbitcoin-e0d73573a37bf4b519f6f61e5678572d48a64517.tar.xz
net: CNetAddr: add support to (un)serialize as ADDRv2
Co-authored-by: Carl Dong <contact@carldong.me>
Diffstat (limited to 'src/netaddress.cpp')
-rw-r--r--src/netaddress.cpp55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/netaddress.cpp b/src/netaddress.cpp
index 86fca451f0..e60c2f1558 100644
--- a/src/netaddress.cpp
+++ b/src/netaddress.cpp
@@ -14,10 +14,65 @@
#include <algorithm>
#include <array>
#include <cstdint>
+#include <ios>
#include <iterator>
#include <tuple>
constexpr size_t CNetAddr::V1_SERIALIZATION_SIZE;
+constexpr size_t CNetAddr::MAX_ADDRV2_SIZE;
+
+CNetAddr::BIP155Network CNetAddr::GetBIP155Network() const
+{
+ switch (m_net) {
+ case NET_IPV4:
+ return BIP155Network::IPV4;
+ case NET_IPV6:
+ return BIP155Network::IPV6;
+ case NET_ONION:
+ return BIP155Network::TORV2;
+ case NET_INTERNAL: // should have been handled before calling this function
+ case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
+ case NET_MAX: // m_net is never and should not be set to NET_MAX
+ assert(false);
+ } // no default case, so the compiler can warn about missing cases
+
+ assert(false);
+}
+
+bool CNetAddr::SetNetFromBIP155Network(uint8_t possible_bip155_net, size_t address_size)
+{
+ switch (possible_bip155_net) {
+ case BIP155Network::IPV4:
+ if (address_size == ADDR_IPV4_SIZE) {
+ m_net = NET_IPV4;
+ return true;
+ }
+ throw std::ios_base::failure(
+ strprintf("BIP155 IPv4 address with length %u (should be %u)", address_size,
+ ADDR_IPV4_SIZE));
+ case BIP155Network::IPV6:
+ if (address_size == ADDR_IPV6_SIZE) {
+ m_net = NET_IPV6;
+ return true;
+ }
+ throw std::ios_base::failure(
+ strprintf("BIP155 IPv6 address with length %u (should be %u)", address_size,
+ ADDR_IPV6_SIZE));
+ case BIP155Network::TORV2:
+ if (address_size == ADDR_TORV2_SIZE) {
+ m_net = NET_ONION;
+ return true;
+ }
+ throw std::ios_base::failure(
+ strprintf("BIP155 TORv2 address with length %u (should be %u)", address_size,
+ ADDR_TORV2_SIZE));
+ }
+
+ // Don't throw on addresses with unknown network ids (maybe from the future).
+ // Instead silently drop them and have the unserialization code consume
+ // subsequent ones which may be known to us.
+ return false;
+}
/**
* Construct an unspecified IPv6 network address (::/128).