IpAddress::toString() support for IPv6

pull/1262/head
Stephen Shelton 4 years ago
parent 043f993e41
commit 4dd4327f36
No known key found for this signature in database
GPG Key ID: EE4BADACCE8B631C

@ -8,6 +8,8 @@
#include <charconv>
#include <stdexcept>
#include <arpa/inet.h>
namespace llarp
{
/// shared utility functions
@ -238,27 +240,38 @@ namespace llarp
return "";
uint8_t* ip6 = m_addr.sin6_addr.s6_addr;
std::string str;
// ensure SIIT
if (ip6[10] != 0xff or ip6[11] != 0xff)
throw std::runtime_error("Only SIIT address supported");
if (ip6[10] == 0xff and ip6[11] == 0xff)
{
// treat SIIT like IPv4
constexpr auto MaxIPv4PlusPortStringSize = 22;
str.reserve(MaxIPv4PlusPortStringSize);
// TODO: ensure these don't each incur a memory allocation
str.append(std::to_string(ip6[12]));
str.append(1, '.');
str.append(std::to_string(ip6[13]));
str.append(1, '.');
str.append(std::to_string(ip6[14]));
str.append(1, '.');
str.append(std::to_string(ip6[15]));
}
else
{
constexpr auto MaxIPv6PlusPortStringSize = 128;
str.reserve(MaxIPv6PlusPortStringSize);
constexpr auto MaxIPv4PlusPortStringSize = 22;
std::string str;
str.reserve(MaxIPv4PlusPortStringSize);
char buf[128] = {0x0};
inet_ntop(AF_INET6, &m_addr.sin6_addr.s6_addr, buf, sizeof(buf));
// TODO: ensure these don't each incur a memory allocation
str.append(std::to_string(ip6[12]));
str.append(1, '.');
str.append(std::to_string(ip6[13]));
str.append(1, '.');
str.append(std::to_string(ip6[14]));
str.append(1, '.');
str.append(std::to_string(ip6[15]));
str.append("[");
str.append(buf);
str.append("]");
}
str.append(1, ':');
str.append(std::to_string(getPort()));
return str;
}

Loading…
Cancel
Save