2019-01-11 01:42:02 +00:00
|
|
|
#include <net/net_int.hpp>
|
2019-06-11 16:44:05 +00:00
|
|
|
#include <net/ip.hpp>
|
2019-05-15 22:03:24 +00:00
|
|
|
#include <string>
|
|
|
|
|
2019-05-01 13:40:10 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
2021-01-11 23:13:22 +00:00
|
|
|
template <>
|
|
|
|
nuint32_t
|
|
|
|
ToNet(huint32_t h)
|
|
|
|
{
|
|
|
|
return xhtonl(h);
|
|
|
|
}
|
|
|
|
|
2019-05-01 13:40:10 +00:00
|
|
|
template <>
|
|
|
|
void
|
|
|
|
huint32_t::ToV6(V6Container& c)
|
|
|
|
{
|
|
|
|
c.resize(16);
|
|
|
|
std::fill(c.begin(), c.end(), 0);
|
|
|
|
htobe32buf(c.data() + 12, h);
|
|
|
|
c[11] = 0xff;
|
|
|
|
c[10] = 0xff;
|
|
|
|
}
|
|
|
|
|
2019-06-11 16:44:05 +00:00
|
|
|
template <>
|
|
|
|
void
|
|
|
|
huint128_t::ToV6(V6Container& c)
|
|
|
|
{
|
|
|
|
c.resize(16);
|
2020-05-20 19:14:05 +00:00
|
|
|
const in6_addr addr = net::HUIntToIn6(*this);
|
2019-06-11 16:44:05 +00:00
|
|
|
std::copy_n(addr.s6_addr, 16, c.begin());
|
|
|
|
}
|
|
|
|
|
2019-05-01 13:40:10 +00:00
|
|
|
template <>
|
|
|
|
std::string
|
|
|
|
huint32_t::ToString() const
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
uint32_t n = htonl(h);
|
2019-05-01 13:40:10 +00:00
|
|
|
char tmp[INET_ADDRSTRLEN] = {0};
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!inet_ntop(AF_INET, (void*)&n, tmp, sizeof(tmp)))
|
2019-05-01 13:40:10 +00:00
|
|
|
return "";
|
|
|
|
return tmp;
|
|
|
|
}
|
2019-06-11 16:44:05 +00:00
|
|
|
|
|
|
|
template <>
|
|
|
|
std::string
|
|
|
|
huint128_t::ToString() const
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
auto addr = ntoh128(h);
|
2019-06-11 16:44:05 +00:00
|
|
|
char tmp[INET6_ADDRSTRLEN] = {0};
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!inet_ntop(AF_INET6, (void*)&addr, tmp, sizeof(tmp)))
|
2019-06-11 16:44:05 +00:00
|
|
|
return "";
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
bool
|
|
|
|
huint32_t::FromString(const std::string& str)
|
|
|
|
{
|
|
|
|
uint32_t n;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!inet_pton(AF_INET, str.c_str(), &n))
|
2019-06-11 16:44:05 +00:00
|
|
|
return false;
|
|
|
|
h = ntohl(n);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
bool
|
|
|
|
huint128_t::FromString(const std::string& str)
|
|
|
|
{
|
2020-02-23 19:35:25 +00:00
|
|
|
llarp::uint128_t i;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!inet_pton(AF_INET6, str.c_str(), &i))
|
2019-06-11 16:44:05 +00:00
|
|
|
return false;
|
|
|
|
h = ntoh128(i);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-01 13:40:10 +00:00
|
|
|
template <>
|
|
|
|
std::string
|
|
|
|
nuint32_t::ToString() const
|
|
|
|
{
|
|
|
|
char tmp[INET_ADDRSTRLEN] = {0};
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!inet_ntop(AF_INET, (void*)&n, tmp, sizeof(tmp)))
|
2019-05-01 13:40:10 +00:00
|
|
|
return "";
|
|
|
|
return tmp;
|
|
|
|
}
|
2019-05-02 16:23:31 +00:00
|
|
|
template <>
|
|
|
|
std::string
|
2019-05-01 13:40:10 +00:00
|
|
|
huint16_t::ToString() const
|
|
|
|
{
|
|
|
|
return std::to_string(h);
|
|
|
|
}
|
|
|
|
|
2019-05-02 16:23:31 +00:00
|
|
|
template <>
|
|
|
|
std::string
|
2019-05-01 13:40:10 +00:00
|
|
|
nuint16_t::ToString() const
|
|
|
|
{
|
|
|
|
return std::to_string(ntohs(n));
|
|
|
|
}
|
2020-02-23 19:35:25 +00:00
|
|
|
} // namespace llarp
|