lokinet/llarp/net/net_int.hpp

306 lines
5.9 KiB
C++
Raw Normal View History

#pragma once
2018-12-03 22:22:59 +00:00
// for addrinfo
#ifndef _WIN32
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#else
#include <winsock2.h>
#include <ws2tcpip.h>
#define inet_aton(x, y) inet_pton(AF_INET, x, y)
#endif
#include "net.h"
2019-07-30 23:42:13 +00:00
#include <cstdlib> // for itoa
2018-12-03 22:22:59 +00:00
#include <iostream>
#include <vector>
2018-12-03 22:22:59 +00:00
#include <llarp/util/formattable.hpp>
#include <oxenc/variant.h>
2020-02-24 17:22:24 +00:00
#include "uint128.hpp"
2019-06-11 16:44:05 +00:00
2018-12-03 22:22:59 +00:00
namespace llarp
{
template <typename UInt_t>
struct huint_t
2018-12-03 22:22:59 +00:00
{
UInt_t h;
2018-12-03 22:22:59 +00:00
2021-03-05 17:31:52 +00:00
constexpr huint_t
operator&(huint_t x) const
{
2019-06-11 16:44:05 +00:00
return huint_t{UInt_t{h & x.h}};
}
2018-12-03 22:22:59 +00:00
constexpr huint_t
operator|(huint_t x) const
{
2019-06-11 16:44:05 +00:00
return huint_t{UInt_t{h | x.h}};
}
2018-12-03 22:22:59 +00:00
constexpr huint_t
operator-(huint_t x) const
{
return huint_t{UInt_t{h - x.h}};
}
constexpr huint_t
operator+(huint_t x) const
{
return huint_t{UInt_t{h + x.h}};
}
constexpr huint_t
operator^(huint_t x) const
{
2019-06-11 16:44:05 +00:00
return huint_t{UInt_t{h ^ x.h}};
}
2018-12-03 22:22:59 +00:00
constexpr huint_t
operator~() const
2018-12-03 22:22:59 +00:00
{
2019-06-11 16:44:05 +00:00
return huint_t{UInt_t{~h}};
}
constexpr huint_t
operator<<(int n) const
{
UInt_t v{h};
v <<= n;
return huint_t{v};
2018-12-03 22:22:59 +00:00
}
inline huint_t
operator++()
{
++h;
return *this;
}
2019-05-02 15:19:21 +00:00
inline huint_t
operator--()
2019-02-08 19:43:25 +00:00
{
--h;
return *this;
2019-02-08 19:43:25 +00:00
}
constexpr bool
operator<(huint_t x) const
{
return h < x.h;
}
2019-06-11 16:44:05 +00:00
constexpr bool
operator!=(huint_t x) const
{
return h != x.h;
}
constexpr bool
operator==(huint_t x) const
{
return h == x.h;
}
using V6Container = std::vector<uint8_t>;
[[deprecated]] void
ToV6(V6Container& c);
2018-12-03 22:22:59 +00:00
std::string
ToString() const;
2018-12-03 22:22:59 +00:00
2019-06-11 16:44:05 +00:00
bool
FromString(const std::string&);
};
2018-12-03 22:22:59 +00:00
using huint32_t = huint_t<uint32_t>;
using huint16_t = huint_t<uint16_t>;
using huint128_t = huint_t<llarp::uint128_t>;
2018-12-03 22:22:59 +00:00
template <typename UInt_t>
struct nuint_t
{
UInt_t n = 0;
2018-12-03 22:22:59 +00:00
2021-03-05 17:31:52 +00:00
constexpr nuint_t
operator&(nuint_t x) const
2018-12-03 22:22:59 +00:00
{
return nuint_t{UInt_t(n & x.n)};
2018-12-03 22:22:59 +00:00
}
constexpr nuint_t
operator|(nuint_t x) const
2018-12-03 22:22:59 +00:00
{
return nuint_t{UInt_t(n | x.n)};
}
2018-12-03 22:22:59 +00:00
constexpr nuint_t
operator^(nuint_t x) const
{
return nuint_t{UInt_t(n ^ x.n)};
}
2018-12-03 22:22:59 +00:00
constexpr nuint_t
operator~() const
{
return nuint_t{UInt_t(~n)};
}
2018-12-03 22:22:59 +00:00
inline nuint_t
operator++()
{
++n;
return *this;
}
inline nuint_t
operator--()
{
--n;
return *this;
}
2018-12-03 22:22:59 +00:00
constexpr bool
operator<(nuint_t x) const
{
return n < x.n;
}
2018-12-03 22:22:59 +00:00
2022-09-14 22:40:54 +00:00
constexpr bool
operator!=(nuint_t x) const
{
return n != x.n;
}
constexpr bool
operator==(nuint_t x) const
2018-12-03 22:22:59 +00:00
{
return n == x.n;
2018-12-03 22:22:59 +00:00
}
using V6Container = std::vector<uint8_t>;
[[deprecated]] void
ToV6(V6Container& c);
2018-12-03 22:22:59 +00:00
std::string
ToString() const;
2018-12-03 22:22:59 +00:00
bool
FromString(const std::string& data)
{
huint_t<UInt_t> x;
if (not x.FromString(data))
return false;
*this = ToNet(x);
return true;
}
inline static nuint_t<UInt_t>
from_string(const std::string& str)
{
nuint_t<UInt_t> x{};
if (not x.FromString(str))
throw std::invalid_argument{fmt::format("{} is not a valid value")};
return x;
}
template <typename... Args_t>
inline static nuint_t<UInt_t>
from_host(Args_t&&... args)
{
return ToNet(huint_t<UInt_t>{std::forward<Args_t>(args)...});
}
};
2018-12-03 22:22:59 +00:00
namespace net
2018-12-03 22:22:59 +00:00
{
/// hides the nuint types used with net_port_t / net_ipv4addr_t / net_ipv6addr_t
namespace
{
using n_uint16_t = llarp::nuint_t<uint16_t>;
using n_uint32_t = llarp::nuint_t<uint32_t>;
using n_uint128_t = llarp::nuint_t<llarp::uint128_t>;
} // namespace
using port_t = n_uint16_t;
using ipv4addr_t = n_uint32_t;
using flowlabel_t = n_uint32_t;
using ipv6addr_t = n_uint128_t;
using ipaddr_t = std::variant<ipv4addr_t, ipv6addr_t>;
std::string
ToString(const ipaddr_t& ip);
huint16_t ToHost(port_t);
huint32_t ToHost(ipv4addr_t);
huint128_t ToHost(ipv6addr_t);
port_t ToNet(huint16_t);
ipv4addr_t ToNet(huint32_t);
ipv6addr_t ToNet(huint128_t);
} // namespace net
template <>
inline constexpr bool IsToStringFormattable<huint128_t> = true;
template <>
inline constexpr bool IsToStringFormattable<huint32_t> = true;
template <>
inline constexpr bool IsToStringFormattable<huint16_t> = true;
template <>
inline constexpr bool IsToStringFormattable<net::ipv6addr_t> = true;
template <>
inline constexpr bool IsToStringFormattable<net::ipv4addr_t> = true;
template <>
inline constexpr bool IsToStringFormattable<net::port_t> = true;
using nuint16_t [[deprecated("use llarp::net::port_t instead")]] = llarp::net::port_t;
using nuint32_t [[deprecated("use llarp::net::ipv4addr_t instead")]] = llarp::net::ipv4addr_t;
using nuint128_t [[deprecated("use llarp::net::ipv6addr_t instead")]] = llarp::net::ipv6addr_t;
2018-12-03 22:22:59 +00:00
template <typename UInt_t>
[[deprecated("use llarp::net::ToNet instead")]] inline llarp::nuint_t<UInt_t>
ToNet(llarp::huint_t<UInt_t> x)
2018-12-03 22:22:59 +00:00
{
return llarp::net::ToNet(x);
2018-12-03 22:22:59 +00:00
}
template <typename UInt_t>
[[deprecated("use llarp::net::ToHost instead")]] inline llarp::huint_t<UInt_t>
ToHost(llarp::nuint_t<UInt_t> x)
2018-12-03 22:22:59 +00:00
{
return llarp::net::ToHost(x);
2018-12-03 22:22:59 +00:00
}
[[deprecated("use llarp::net::ToHost instead")]] inline net::ipv4addr_t
xhtonl(huint32_t x)
2018-12-03 22:22:59 +00:00
{
return ToNet(x);
2018-12-03 22:22:59 +00:00
}
} // namespace llarp
namespace std
{
template <typename UInt_t>
struct hash<llarp::nuint_t<UInt_t>>
{
size_t
operator()(const llarp::nuint_t<UInt_t>& x) const
{
return std::hash<UInt_t>{}(x.n);
}
};
template <typename UInt_t>
struct hash<llarp::huint_t<UInt_t>>
{
size_t
operator()(const llarp::huint_t<UInt_t>& x) const
{
return std::hash<UInt_t>{}(x.h);
}
};
} // namespace std