lokinet/llarp/util/aligned.hpp

344 lines
6.3 KiB
C++
Raw Permalink Normal View History

#pragma once
2018-06-01 14:08:54 +00:00
#include "bencode.h"
#include <llarp/util/logging.hpp>
#include <llarp/util/formattable.hpp>
2018-12-12 01:02:36 +00:00
#include <oxenc/hex.h>
#include <array>
#include <cstddef>
2018-06-01 14:24:58 +00:00
#include <iomanip>
2018-06-01 14:08:54 +00:00
#include <iostream>
#include <memory>
#include <numeric>
#include <type_traits>
2018-12-20 17:25:11 +00:00
#include <algorithm>
2018-06-01 14:08:54 +00:00
2018-10-23 12:40:34 +00:00
extern "C"
{
extern void
randombytes(unsigned char* const ptr, unsigned long long sz);
2019-12-10 18:50:52 +00:00
extern int
sodium_is_zero(const unsigned char* n, const size_t nlen);
2018-10-23 12:40:34 +00:00
}
2018-06-01 14:08:54 +00:00
namespace llarp
{
/// aligned buffer that is sz bytes long and aligns to the nearest Alignment
template <size_t sz>
2020-06-12 23:00:39 +00:00
// Microsoft C malloc(3C) cannot return pointers aligned wider than 8 ffs
#ifdef _WIN32
struct alignas(uint64_t) AlignedBuffer
#else
2020-05-23 21:18:00 +00:00
struct alignas(std::max_align_t) AlignedBuffer
2020-06-12 23:00:39 +00:00
#endif
2018-06-01 14:08:54 +00:00
{
2020-05-23 21:18:00 +00:00
static_assert(alignof(std::max_align_t) <= 16, "insane alignment");
static_assert(
sz >= 8,
"AlignedBuffer cannot be used with buffers smaller than 8 "
"bytes");
static constexpr size_t SIZE = sz;
using Data = std::array<byte_t, SIZE>;
virtual ~AlignedBuffer() = default;
2018-07-26 21:08:56 +00:00
AlignedBuffer()
{
Zero();
2018-06-22 00:25:30 +00:00
}
explicit AlignedBuffer(const byte_t* data)
2018-06-01 14:08:54 +00:00
{
*this = data;
}
explicit AlignedBuffer(const Data& buf)
{
m_data = buf;
2018-06-01 14:08:54 +00:00
}
AlignedBuffer&
operator=(const byte_t* data)
2018-06-01 14:08:54 +00:00
{
std::memcpy(m_data.data(), data, sz);
2018-06-01 14:08:54 +00:00
return *this;
}
2018-09-24 14:31:58 +00:00
/// bitwise NOT
AlignedBuffer<sz>
2018-09-24 14:31:58 +00:00
operator~() const
{
AlignedBuffer<sz> ret;
std::transform(begin(), end(), ret.begin(), [](byte_t a) { return ~a; });
2018-09-24 14:31:58 +00:00
return ret;
}
2018-06-01 14:08:54 +00:00
bool
operator==(const AlignedBuffer& other) const
{
return m_data == other.m_data;
2018-06-01 14:08:54 +00:00
}
bool
operator!=(const AlignedBuffer& other) const
{
return m_data != other.m_data;
2018-06-01 14:08:54 +00:00
}
bool
operator<(const AlignedBuffer& other) const
{
return m_data < other.m_data;
}
2018-08-10 21:34:11 +00:00
bool
operator>(const AlignedBuffer& other) const
{
return m_data > other.m_data;
2018-08-10 21:34:11 +00:00
}
bool
operator<=(const AlignedBuffer& other) const
{
return m_data <= other.m_data;
2018-08-10 21:34:11 +00:00
}
bool
operator>=(const AlignedBuffer& other) const
{
return m_data >= other.m_data;
2018-08-10 21:34:11 +00:00
}
AlignedBuffer
operator^(const AlignedBuffer& other) const
{
AlignedBuffer<sz> ret;
Replace libuv with uvw & related refactoring - removes all the llarp_ev_* functions, replacing with methods/classes/functions in the llarp namespace. - banish ev/ev.h to the void - Passes various things by const lvalue ref, especially shared_ptr's that don't need to be copied (to avoid an atomic refcount increment/decrement). - Add a llarp::UDPHandle abstract class for UDP handling - Removes the UDP tick handler; code that needs tick can just do a separate handler on the event loop outside the UDP socket. - Adds an "OwnedBuffer" which owns its own memory but is implicitly convertible to a llarp_buffer_t. This is mostly needed to take over ownership of buffers from uvw without copying them as, currently, uvw does its own allocation (pending some open upstream issues/PRs). - Logic: - add `make_caller`/`call_forever`/`call_every` utility functions to abstract Call wrapping and dependent timed tasks. - Add inLogicThread() so that code can tell its inside the logic thread (typically for debugging assertions). - get rid of janky integer returns and dealing with cancellations on call_later: the other methods added here and the event loop code remove the need for them. - Event loop: - redo everything with uvw instead of libuv - rename EventLoopWakeup::Wakeup to EventLoopWakeup::Trigger to better reflect what it does. - add EventLoopRepeater for repeated events, and replace the code that reschedules itself every time it is called with a repeater. - Split up `EventLoop::run()` into a non-virtual base method and abstract `run_loop()` methods; the base method does a couple extra setup/teardown things that don't need to be in the derived class. - udp_listen is replaced with ev->udp(...) which returns a new UDPHandle object rather that needing gross C-style-but-not-actually-C-compatible structs. - Remove unused register_poll_fd_(un)readable - Use shared_ptr for EventLoopWakeup rather than returning a raw pointer; uvw lets us not have to worry about having the event loop class maintain ownership of it. - Add factory EventLoop::create() function to create a default (uvw-based) event loop (previously this was one of the llarp_ev_blahblah unnamespaced functions). - ev_libuv: this is mostly rewritten; all of the glue code/structs, in particular, are gone as they are no longer needed with uvw. - DNS: - Rename DnsHandler to DnsInterceptor to better describe what it does (this is the code that intercepts all DNS to the tun IP range for Android). - endpoint: - remove unused "isolated network" code - remove distinct (but actually always the same) variables for router/endpoint logic objects - llarp_buffer_t - make constructors type-safe against being called with points to non-size-1 values - tun packet reading: - read all available packets off the device/file descriptor; previously we were reading one packet at a time then returning to the event loop to poll again. - ReadNextPacket() now returns a 0-size packet if the read would block (so that we can implement the previous point). - ReadNextPacket() now throws on I/O error - Miscellaneous code cleanups/simplifications
2021-03-02 02:06:20 +00:00
std::transform(begin(), end(), other.begin(), ret.begin(), std::bit_xor<>());
return ret;
}
AlignedBuffer&
operator^=(const AlignedBuffer& other)
{
// Mutate in place instead.
for (size_t i = 0; i < sz; ++i)
{
m_data[i] ^= other.m_data[i];
}
return *this;
2018-06-01 14:08:54 +00:00
}
2021-03-05 17:31:52 +00:00
byte_t&
operator[](size_t idx)
{
assert(idx < SIZE);
return m_data[idx];
}
2021-03-05 17:31:52 +00:00
const byte_t&
operator[](size_t idx) const
{
assert(idx < SIZE);
return m_data[idx];
}
static constexpr size_t
size()
{
return sz;
}
2018-06-14 14:04:42 +00:00
void
Fill(byte_t f)
{
m_data.fill(f);
2018-06-14 14:04:42 +00:00
}
2018-12-20 16:03:55 +00:00
Data&
as_array()
{
return m_data;
2018-12-20 16:03:55 +00:00
}
const Data&
as_array() const
{
return m_data;
2018-12-20 16:03:55 +00:00
}
byte_t*
data()
{
return m_data.data();
}
const byte_t*
data() const
{
return m_data.data();
}
bool
IsZero() const
{
const uint64_t* ptr = reinterpret_cast<const uint64_t*>(data());
for (size_t idx = 0; idx < SIZE / sizeof(uint64_t); idx++)
{
if (ptr[idx])
return false;
}
return true;
}
2018-06-14 14:04:42 +00:00
2018-06-01 14:08:54 +00:00
void
Zero()
{
m_data.fill(0);
2018-06-01 14:08:54 +00:00
}
virtual void
2018-06-01 14:08:54 +00:00
Randomize()
{
randombytes(data(), SIZE);
2018-06-01 14:08:54 +00:00
}
typename Data::iterator
begin()
{
return m_data.begin();
}
typename Data::iterator
end()
{
return m_data.end();
}
typename Data::const_iterator
begin() const
{
return m_data.cbegin();
}
typename Data::const_iterator
end() const
{
return m_data.cend();
}
bool
FromBytestring(llarp_buffer_t* buf)
{
if (buf->sz != sz)
{
llarp::LogError("bdecode buffer size mismatch ", buf->sz, "!=", sz);
return false;
}
memcpy(data(), buf->base, sz);
return true;
}
bool
BEncode(llarp_buffer_t* buf) const
{
return bencode_write_bytestring(buf, data(), sz);
}
bool
BDecode(llarp_buffer_t* buf)
{
llarp_buffer_t strbuf;
if (!bencode_read_string(buf, &strbuf))
{
return false;
}
return FromBytestring(&strbuf);
}
std::string_view
ToView() const
{
return {reinterpret_cast<const char*>(data()), sz};
}
std::string
ToHex() const
{
return oxenc::to_hex(begin(), end());
}
2020-03-01 02:15:25 +00:00
std::string
ShortHex() const
{
return oxenc::to_hex(begin(), begin() + 4);
2020-03-01 02:15:25 +00:00
}
2020-05-19 18:53:03 +00:00
bool
FromHex(std::string_view str)
{
if (str.size() != 2 * size() || !oxenc::is_hex(str))
return false;
oxenc::from_hex(str.begin(), str.end(), begin());
return true;
2020-05-19 18:53:03 +00:00
}
private:
Data m_data;
2018-06-01 14:08:54 +00:00
};
namespace detail
{
template <size_t Sz>
static std::true_type
is_aligned_buffer_impl(AlignedBuffer<Sz>*);
static std::false_type
is_aligned_buffer_impl(...);
} // namespace detail
// True if T is or is derived from AlignedBuffer<N> for any N
template <typename T>
constexpr inline bool is_aligned_buffer =
decltype(detail::is_aligned_buffer_impl(static_cast<T*>(nullptr)))::value;
2018-06-18 22:03:50 +00:00
} // namespace llarp
namespace fmt
{
// Any AlignedBuffer<N> (or subclass) gets hex formatted when output:
template <typename T>
struct formatter<
T,
char,
std::enable_if_t<llarp::is_aligned_buffer<T> && !llarp::IsToStringFormattable<T>>>
: formatter<std::string_view>
{
template <typename FormatContext>
auto
format(const T& val, FormatContext& ctx)
{
auto it = oxenc::hex_encoder{val.begin(), val.end()};
return std::copy(it, it.end(), ctx.out());
}
};
} // namespace fmt
namespace std
{
template <size_t sz>
struct hash<llarp::AlignedBuffer<sz>>
{
std::size_t
operator()(const llarp::AlignedBuffer<sz>& buf) const noexcept
{
std::size_t h = 0;
std::memcpy(&h, buf.data(), sizeof(std::size_t));
return h;
}
};
} // namespace std