2021-03-09 22:24:35 +00:00
|
|
|
#include "net_int.hpp"
|
|
|
|
#include "ip.hpp"
|
2019-05-15 22:03:24 +00:00
|
|
|
#include <string>
|
2022-10-07 00:50:16 +00:00
|
|
|
#include <variant>
|
2019-05-15 22:03:24 +00:00
|
|
|
|
2022-04-28 15:09:51 +00:00
|
|
|
#include <oxenc/endian.h>
|
|
|
|
|
2019-05-01 13:40:10 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
2022-07-09 15:05:52 +00:00
|
|
|
namespace net
|
2021-03-09 16:02:41 +00:00
|
|
|
{
|
2022-07-09 15:05:52 +00:00
|
|
|
huint16_t
|
|
|
|
ToHost(port_t x)
|
|
|
|
{
|
|
|
|
return huint16_t{oxenc::big_to_host(x.n)};
|
|
|
|
}
|
2021-03-09 16:02:41 +00:00
|
|
|
|
2022-07-09 15:05:52 +00:00
|
|
|
huint32_t
|
|
|
|
ToHost(ipv4addr_t x)
|
|
|
|
{
|
|
|
|
return huint32_t{oxenc::big_to_host(x.n)};
|
|
|
|
}
|
2021-02-22 15:01:05 +00:00
|
|
|
|
2022-07-09 15:05:52 +00:00
|
|
|
huint128_t
|
|
|
|
ToHost(ipv6addr_t x)
|
|
|
|
{
|
|
|
|
return {ntoh128(x.n)};
|
|
|
|
}
|
2021-03-09 16:02:41 +00:00
|
|
|
|
2022-07-09 15:05:52 +00:00
|
|
|
port_t
|
|
|
|
ToNet(huint16_t x)
|
|
|
|
{
|
|
|
|
return port_t{oxenc::host_to_big(x.h)};
|
|
|
|
}
|
2021-03-02 18:18:22 +00:00
|
|
|
|
2022-07-09 15:05:52 +00:00
|
|
|
ipv4addr_t
|
|
|
|
ToNet(huint32_t x)
|
|
|
|
{
|
|
|
|
return ipv4addr_t{oxenc::host_to_big(x.h)};
|
|
|
|
}
|
2021-01-11 23:13:22 +00:00
|
|
|
|
2022-07-09 15:05:52 +00:00
|
|
|
ipv6addr_t
|
|
|
|
ToNet(huint128_t x)
|
|
|
|
{
|
|
|
|
return ipv6addr_t{hton128(x.h)};
|
|
|
|
}
|
|
|
|
} // namespace net
|
2021-03-09 16:02:41 +00:00
|
|
|
|
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);
|
2022-04-28 15:09:51 +00:00
|
|
|
oxenc::write_host_as_big(h, c.data() + 12);
|
2019-05-01 13:40:10 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-03-29 16:31:55 +00:00
|
|
|
template <>
|
|
|
|
bool
|
|
|
|
huint16_t::FromString(const std::string& str)
|
|
|
|
{
|
|
|
|
if (auto val = std::atoi(str.c_str()); val >= 0)
|
|
|
|
{
|
|
|
|
h = val;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-11 16:44:05 +00:00
|
|
|
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;
|
|
|
|
}
|
2022-07-09 15:05:52 +00:00
|
|
|
|
|
|
|
template <>
|
|
|
|
std::string
|
|
|
|
nuint128_t::ToString() const
|
|
|
|
{
|
|
|
|
char tmp[INET6_ADDRSTRLEN] = {0};
|
|
|
|
if (!inet_ntop(AF_INET6, (void*)&n, tmp, sizeof(tmp)))
|
|
|
|
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));
|
|
|
|
}
|
2022-10-07 00:50:16 +00:00
|
|
|
|
|
|
|
std::string
|
|
|
|
net::ToString(const ipaddr_t& ipaddr)
|
|
|
|
{
|
|
|
|
return std::visit([](const auto& ip) { return ip.ToString(); }, ipaddr);
|
|
|
|
}
|
|
|
|
|
2020-02-23 19:35:25 +00:00
|
|
|
} // namespace llarp
|