p2p, refactor: extract OnionToString() from CNetAddr::ToStringIp()

This commit is contained in:
Jon Atack 2021-05-30 12:19:10 +02:00
parent 5f9d3c09b4
commit 8be56f0f8e
No known key found for this signature in database
GPG Key ID: 4F5721B3D0E3921D

View File

@ -594,22 +594,26 @@ static std::string IPv6ToString(Span<const uint8_t> a, uint32_t scope_id)
return r;
}
static std::string OnionToString(const Span<const uint8_t>& addr)
{
uint8_t checksum[torv3::CHECKSUM_LEN];
torv3::Checksum(addr, checksum);
// TORv3 onion_address = base32(PUBKEY | CHECKSUM | VERSION) + ".onion"
prevector<torv3::TOTAL_LEN, uint8_t> address{addr.begin(), addr.end()};
address.insert(address.end(), checksum, checksum + torv3::CHECKSUM_LEN);
address.insert(address.end(), torv3::VERSION, torv3::VERSION + sizeof(torv3::VERSION));
return EncodeBase32(address) + ".onion";
}
std::string CNetAddr::ToStringIP() const
{
switch (m_net) {
case NET_IPV4:
return IPv4ToString(m_addr);
case NET_IPV6: {
case NET_IPV6:
return IPv6ToString(m_addr, m_scope_id);
}
case NET_ONION:
uint8_t checksum[torv3::CHECKSUM_LEN];
torv3::Checksum(m_addr, checksum);
// TORv3 onion_address = base32(PUBKEY | CHECKSUM | VERSION) + ".onion"
prevector<torv3::TOTAL_LEN, uint8_t> address{m_addr.begin(), m_addr.end()};
address.insert(address.end(), checksum, checksum + torv3::CHECKSUM_LEN);
address.insert(address.end(), torv3::VERSION, torv3::VERSION + sizeof(torv3::VERSION));
return EncodeBase32(address) + ".onion";
return OnionToString(m_addr);
case NET_I2P:
return EncodeBase32(m_addr, false /* don't pad with = */) + ".b32.i2p";
case NET_CJDNS: