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; 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 std::string CNetAddr::ToStringIP() const
{ {
switch (m_net) { switch (m_net) {
case NET_IPV4: case NET_IPV4:
return IPv4ToString(m_addr); return IPv4ToString(m_addr);
case NET_IPV6: { case NET_IPV6:
return IPv6ToString(m_addr, m_scope_id); return IPv6ToString(m_addr, m_scope_id);
}
case NET_ONION: case NET_ONION:
uint8_t checksum[torv3::CHECKSUM_LEN]; return OnionToString(m_addr);
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";
case NET_I2P: case NET_I2P:
return EncodeBase32(m_addr, false /* don't pad with = */) + ".b32.i2p"; return EncodeBase32(m_addr, false /* don't pad with = */) + ".b32.i2p";
case NET_CJDNS: case NET_CJDNS: