2018-12-03 22:22:59 +00:00
|
|
|
#ifndef LLARP_NET_INT_HPP
|
|
|
|
#define LLARP_NET_INT_HPP
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2019-01-11 01:42:02 +00:00
|
|
|
#include <net/net.h>
|
2018-12-12 02:52:51 +00:00
|
|
|
|
2019-07-30 23:42:13 +00:00
|
|
|
#include <cstdlib> // for itoa
|
2018-12-03 22:22:59 +00:00
|
|
|
#include <iostream>
|
2019-03-27 21:20:04 +00:00
|
|
|
#include <util/endian.hpp>
|
2019-05-01 13:40:10 +00:00
|
|
|
#include <vector>
|
2018-12-03 22:22:59 +00:00
|
|
|
|
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
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename UInt_t>
|
2019-05-01 13:40:10 +00:00
|
|
|
struct huint_t
|
2018-12-03 22:22:59 +00:00
|
|
|
{
|
2019-05-01 13:40:10 +00:00
|
|
|
UInt_t h;
|
2018-12-03 22:22:59 +00:00
|
|
|
|
2019-05-01 13:40:10 +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}};
|
2019-05-01 13:40:10 +00:00
|
|
|
}
|
2018-12-03 22:22:59 +00:00
|
|
|
|
2019-05-01 13:40:10 +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}};
|
2019-05-01 13:40:10 +00:00
|
|
|
}
|
2018-12-03 22:22:59 +00:00
|
|
|
|
2019-05-01 13:40:10 +00:00
|
|
|
constexpr huint_t
|
2019-09-03 15:56:56 +00:00
|
|
|
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
|
2019-05-01 13:40:10 +00:00
|
|
|
operator^(huint_t x) const
|
|
|
|
{
|
2019-06-11 16:44:05 +00:00
|
|
|
return huint_t{UInt_t{h ^ x.h}};
|
2019-05-01 13:40:10 +00:00
|
|
|
}
|
2018-12-03 22:22:59 +00:00
|
|
|
|
2019-05-01 13:40:10 +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
|
|
|
}
|
|
|
|
|
2019-05-01 13:40:10 +00:00
|
|
|
inline huint_t
|
|
|
|
operator++()
|
2019-03-27 13:36:11 +00:00
|
|
|
{
|
2019-05-01 13:40:10 +00:00
|
|
|
++h;
|
|
|
|
return *this;
|
2019-03-27 13:36:11 +00:00
|
|
|
}
|
2019-05-02 15:19:21 +00:00
|
|
|
|
2019-05-01 13:40:10 +00:00
|
|
|
inline huint_t
|
|
|
|
operator--()
|
2019-02-08 19:43:25 +00:00
|
|
|
{
|
2019-05-01 13:40:10 +00:00
|
|
|
--h;
|
|
|
|
return *this;
|
2019-02-08 19:43:25 +00:00
|
|
|
}
|
2019-05-01 13:40:10 +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;
|
|
|
|
}
|
|
|
|
|
2019-05-01 13:40:10 +00:00
|
|
|
constexpr bool
|
|
|
|
operator==(huint_t x) const
|
|
|
|
{
|
|
|
|
return h == x.h;
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
using V6Container = std::vector<uint8_t>;
|
2019-05-01 13:40:10 +00:00
|
|
|
void
|
|
|
|
ToV6(V6Container& c);
|
2018-12-03 22:22:59 +00:00
|
|
|
|
2019-05-01 13:40:10 +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&);
|
|
|
|
|
2019-05-01 13:40:10 +00:00
|
|
|
friend std::ostream&
|
|
|
|
operator<<(std::ostream& out, const huint_t& i)
|
|
|
|
{
|
|
|
|
return out << i.ToString();
|
|
|
|
}
|
|
|
|
};
|
2018-12-03 22:22:59 +00:00
|
|
|
|
2020-04-07 18:38:56 +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
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename UInt_t>
|
2019-05-01 13:40:10 +00:00
|
|
|
struct nuint_t
|
|
|
|
{
|
|
|
|
UInt_t n;
|
2018-12-03 22:22:59 +00:00
|
|
|
|
2019-05-01 13:40:10 +00:00
|
|
|
constexpr nuint_t operator&(nuint_t x) const
|
2018-12-03 22:22:59 +00:00
|
|
|
{
|
2019-05-01 13:40:10 +00:00
|
|
|
return nuint_t{UInt_t(n & x.n)};
|
2018-12-03 22:22:59 +00:00
|
|
|
}
|
|
|
|
|
2019-05-01 13:40:10 +00:00
|
|
|
constexpr nuint_t
|
|
|
|
operator|(nuint_t x) const
|
2018-12-03 22:22:59 +00:00
|
|
|
{
|
2019-05-01 13:40:10 +00:00
|
|
|
return nuint_t{UInt_t(n | x.n)};
|
|
|
|
}
|
2018-12-03 22:22:59 +00:00
|
|
|
|
2019-05-01 13:40:10 +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
|
|
|
|
2019-05-01 13:40:10 +00:00
|
|
|
constexpr nuint_t
|
|
|
|
operator~() const
|
|
|
|
{
|
|
|
|
return nuint_t{UInt_t(~n)};
|
|
|
|
}
|
2018-12-03 22:22:59 +00:00
|
|
|
|
2019-05-01 13:40:10 +00:00
|
|
|
inline nuint_t
|
|
|
|
operator++()
|
|
|
|
{
|
|
|
|
++n;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
inline nuint_t
|
|
|
|
operator--()
|
|
|
|
{
|
|
|
|
--n;
|
|
|
|
return *this;
|
|
|
|
}
|
2018-12-03 22:22:59 +00:00
|
|
|
|
2019-05-01 13:40:10 +00:00
|
|
|
constexpr bool
|
|
|
|
operator<(nuint_t x) const
|
|
|
|
{
|
|
|
|
return n < x.n;
|
|
|
|
}
|
2018-12-03 22:22:59 +00:00
|
|
|
|
2019-05-01 13:40:10 +00:00
|
|
|
constexpr bool
|
|
|
|
operator==(nuint_t x) const
|
2018-12-03 22:22:59 +00:00
|
|
|
{
|
2019-05-01 13:40:10 +00:00
|
|
|
return n == x.n;
|
2018-12-03 22:22:59 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
using V6Container = std::vector<uint8_t>;
|
2019-05-01 13:40:10 +00:00
|
|
|
void
|
|
|
|
ToV6(V6Container& c);
|
2018-12-03 22:22:59 +00:00
|
|
|
|
2019-05-01 13:40:10 +00:00
|
|
|
std::string
|
|
|
|
ToString() const;
|
2018-12-03 22:22:59 +00:00
|
|
|
|
|
|
|
friend std::ostream&
|
2019-05-01 13:40:10 +00:00
|
|
|
operator<<(std::ostream& out, const nuint_t& i)
|
2018-12-03 22:22:59 +00:00
|
|
|
{
|
2019-05-01 13:40:10 +00:00
|
|
|
return out << i.ToString();
|
2018-12-03 22:22:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
using nuint32_t = nuint_t<uint32_t>;
|
|
|
|
using nuint16_t = nuint_t<uint16_t>;
|
|
|
|
using nuint128_t = nuint_t<llarp::uint128_t>;
|
2018-12-03 22:22:59 +00:00
|
|
|
|
|
|
|
static inline nuint32_t
|
|
|
|
xhtonl(huint32_t x)
|
|
|
|
{
|
|
|
|
return nuint32_t{htonl(x.h)};
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline huint32_t
|
|
|
|
xntohl(nuint32_t x)
|
|
|
|
{
|
|
|
|
return huint32_t{ntohl(x.n)};
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline nuint16_t
|
|
|
|
xhtons(huint16_t x)
|
|
|
|
{
|
|
|
|
return nuint16_t{htons(x.h)};
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline huint16_t
|
|
|
|
xntohs(nuint16_t x)
|
|
|
|
{
|
|
|
|
return huint16_t{ntohs(x.n)};
|
|
|
|
}
|
|
|
|
} // namespace llarp
|
|
|
|
|
2020-02-23 19:35:25 +00:00
|
|
|
namespace std
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename UInt_t>
|
|
|
|
struct hash<llarp::nuint_t<UInt_t>>
|
2020-02-23 19:35:25 +00:00
|
|
|
{
|
|
|
|
size_t
|
2020-04-07 18:38:56 +00:00
|
|
|
operator()(const llarp::nuint_t<UInt_t>& x) const
|
2020-02-23 19:35:25 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
return std::hash<UInt_t>{}(x.n);
|
2020-02-23 19:35:25 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename UInt_t>
|
|
|
|
struct hash<llarp::huint_t<UInt_t>>
|
2020-02-23 19:35:25 +00:00
|
|
|
{
|
|
|
|
size_t
|
2020-04-07 18:38:56 +00:00
|
|
|
operator()(const llarp::huint_t<UInt_t>& x) const
|
2020-02-23 19:35:25 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
return std::hash<UInt_t>{}(x.h);
|
2020-02-23 19:35:25 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace std
|
|
|
|
|
2018-12-03 22:22:59 +00:00
|
|
|
#endif
|