#pragma once // for addrinfo #ifndef _WIN32 #include #include #include #else #include #include #define inet_aton(x, y) inet_pton(AF_INET, x, y) #endif #include "net.h" #include // for itoa #include #include #include #include #include "uint128.hpp" namespace llarp { template struct huint_t { UInt_t 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 { 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 { 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~() const { return huint_t{UInt_t{~h}}; } constexpr huint_t operator<<(int n) const { UInt_t v{h}; v <<= n; return huint_t{v}; } inline huint_t operator++() { ++h; return *this; } inline huint_t operator--() { --h; return *this; } constexpr bool operator<(huint_t x) const { return h < x.h; } 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; [[deprecated]] void ToV6(V6Container& c); std::string ToString() const; bool FromString(const std::string&); }; using huint32_t = huint_t; using huint16_t = huint_t; using huint128_t = huint_t; template struct nuint_t { UInt_t n = 0; constexpr nuint_t operator&(nuint_t x) const { return nuint_t{UInt_t(n & x.n)}; } constexpr nuint_t operator|(nuint_t x) const { return nuint_t{UInt_t(n | x.n)}; } constexpr nuint_t operator^(nuint_t x) const { return nuint_t{UInt_t(n ^ x.n)}; } constexpr nuint_t operator~() const { return nuint_t{UInt_t(~n)}; } inline nuint_t operator++() { ++n; return *this; } inline nuint_t operator--() { --n; return *this; } constexpr bool operator<(nuint_t x) const { return n < x.n; } constexpr bool operator!=(nuint_t x) const { return n != x.n; } constexpr bool operator==(nuint_t x) const { return n == x.n; } using V6Container = std::vector; [[deprecated]] void ToV6(V6Container& c); std::string ToString() const; bool FromString(const std::string& data) { huint_t x; if (not x.FromString(data)) return false; *this = ToNet(x); return true; } inline static nuint_t from_string(const std::string& str) { nuint_t x{}; if (not x.FromString(str)) throw std::invalid_argument{fmt::format("{} is not a valid value")}; return x; } template inline static nuint_t from_host(Args_t&&... args) { return ToNet(huint_t{std::forward(args)...}); } }; namespace net { /// hides the nuint types used with net_port_t / net_ipv4addr_t / net_ipv6addr_t namespace { using n_uint16_t = llarp::nuint_t; using n_uint32_t = llarp::nuint_t; using n_uint128_t = llarp::nuint_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; 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 = true; template <> inline constexpr bool IsToStringFormattable = true; template <> inline constexpr bool IsToStringFormattable = true; template <> inline constexpr bool IsToStringFormattable = true; template <> inline constexpr bool IsToStringFormattable = true; template <> inline constexpr bool IsToStringFormattable = 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; template /* [[deprecated("use llarp::net::ToNet instead")]] */ inline llarp::nuint_t ToNet(llarp::huint_t x) { return llarp::net::ToNet(x); } template /* [[deprecated("use llarp::net::ToHost instead")]] */ inline llarp::huint_t ToHost(llarp::nuint_t x) { return llarp::net::ToHost(x); } /* [[deprecated("use llarp::net::ToHost instead")]] */ inline net::ipv4addr_t xhtonl(huint32_t x) { return ToNet(x); } } // namespace llarp namespace std { template struct hash> { size_t operator()(const llarp::nuint_t& x) const { return std::hash{}(x.n); } }; template struct hash> { size_t operator()(const llarp::huint_t& x) const { return std::hash{}(x.h); } }; } // namespace std