2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2022-2023 Mnemosyne LLC.
|
2022-10-19 16:42:08 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <iterator>
|
|
|
|
#include <limits>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <event2/buffer.h>
|
|
|
|
|
|
|
|
#include "error.h"
|
|
|
|
#include "net.h" // tr_socket_t
|
2022-11-06 21:11:30 +00:00
|
|
|
#include "utils-ev.h"
|
2023-01-25 18:20:10 +00:00
|
|
|
#include "utils.h" // for tr_htonll(), tr_ntohll()
|
2022-10-19 16:42:08 +00:00
|
|
|
|
|
|
|
namespace libtransmission
|
|
|
|
{
|
|
|
|
|
|
|
|
class Buffer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
class Iterator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using difference_type = long;
|
|
|
|
using value_type = std::byte;
|
|
|
|
using pointer = value_type*;
|
|
|
|
using reference = value_type&;
|
|
|
|
using iterator_category = std::random_access_iterator_tag;
|
|
|
|
|
|
|
|
Iterator(evbuffer* buf, size_t offset)
|
|
|
|
: buf_{ buf }
|
|
|
|
{
|
2023-01-28 02:12:09 +00:00
|
|
|
set_offset(offset);
|
2022-10-19 16:42:08 +00:00
|
|
|
}
|
|
|
|
|
2022-11-22 05:54:28 +00:00
|
|
|
[[nodiscard]] constexpr value_type& operator*() noexcept
|
2022-10-19 16:42:08 +00:00
|
|
|
{
|
2022-11-22 05:54:28 +00:00
|
|
|
return static_cast<value_type*>(iov_.iov_base)[iov_offset_];
|
2022-10-19 16:42:08 +00:00
|
|
|
}
|
|
|
|
|
2022-11-22 05:54:28 +00:00
|
|
|
[[nodiscard]] constexpr value_type operator*() const noexcept
|
2022-10-19 16:42:08 +00:00
|
|
|
{
|
2022-11-22 05:54:28 +00:00
|
|
|
return static_cast<value_type*>(iov_.iov_base)[iov_offset_];
|
2022-10-19 16:42:08 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 15:28:46 +00:00
|
|
|
[[nodiscard]] Iterator operator+(size_t n_bytes)
|
2022-10-19 16:42:08 +00:00
|
|
|
{
|
2022-11-21 15:28:46 +00:00
|
|
|
return Iterator{ buf_, offset() + n_bytes };
|
2022-10-19 16:42:08 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 15:28:46 +00:00
|
|
|
[[nodiscard]] Iterator operator-(size_t n_bytes)
|
2022-10-21 18:15:14 +00:00
|
|
|
{
|
2022-11-21 15:28:46 +00:00
|
|
|
return Iterator{ buf_, offset() - n_bytes };
|
2022-10-21 18:15:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto operator-(Iterator const& that) const noexcept
|
|
|
|
{
|
2022-11-21 15:28:46 +00:00
|
|
|
return offset() - that.offset();
|
2022-10-21 18:15:14 +00:00
|
|
|
}
|
|
|
|
|
2022-10-19 16:42:08 +00:00
|
|
|
Iterator& operator++() noexcept
|
|
|
|
{
|
2022-11-21 15:28:46 +00:00
|
|
|
*this += 1U;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Iterator& operator+=(size_t n_bytes)
|
|
|
|
{
|
|
|
|
if (iov_offset_ + n_bytes < iov_.iov_len)
|
2022-10-19 16:42:08 +00:00
|
|
|
{
|
2022-11-21 15:28:46 +00:00
|
|
|
iov_offset_ += n_bytes;
|
2022-10-19 16:42:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-01-28 02:12:09 +00:00
|
|
|
inc_offset(n_bytes);
|
2022-10-19 16:42:08 +00:00
|
|
|
}
|
|
|
|
|
2022-10-21 18:15:14 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Iterator& operator--() noexcept
|
|
|
|
{
|
2022-11-21 15:28:46 +00:00
|
|
|
if (iov_offset_ > 0)
|
|
|
|
{
|
|
|
|
--iov_offset_;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-01-28 02:12:09 +00:00
|
|
|
set_offset(offset() - 1);
|
2022-11-21 15:28:46 +00:00
|
|
|
}
|
2022-10-21 18:15:14 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-10-19 16:42:08 +00:00
|
|
|
[[nodiscard]] constexpr bool operator==(Iterator const& that) const noexcept
|
|
|
|
{
|
2022-11-21 15:28:46 +00:00
|
|
|
return offset() == that.offset();
|
2022-10-19 16:42:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr bool operator!=(Iterator const& that) const noexcept
|
|
|
|
{
|
2022-11-21 15:28:46 +00:00
|
|
|
return offset() != that.offset();
|
2022-10-19 16:42:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-11-21 15:28:46 +00:00
|
|
|
[[nodiscard]] constexpr size_t offset() const noexcept
|
|
|
|
{
|
|
|
|
return ptr_.pos + iov_offset_;
|
|
|
|
}
|
|
|
|
|
2023-01-28 02:12:09 +00:00
|
|
|
void inc_offset(size_t increment)
|
2022-11-21 15:28:46 +00:00
|
|
|
{
|
|
|
|
evbuffer_ptr_set(buf_, &ptr_, iov_offset_ + increment, EVBUFFER_PTR_ADD);
|
|
|
|
evbuffer_peek(buf_, std::numeric_limits<ev_ssize_t>::max(), &ptr_, &iov_, 1);
|
|
|
|
iov_offset_ = 0;
|
|
|
|
}
|
|
|
|
|
2023-01-28 02:12:09 +00:00
|
|
|
void set_offset(size_t offset)
|
2022-10-19 16:42:08 +00:00
|
|
|
{
|
2022-11-21 15:28:46 +00:00
|
|
|
evbuffer_ptr_set(buf_, &ptr_, offset, EVBUFFER_PTR_SET);
|
|
|
|
evbuffer_peek(buf_, std::numeric_limits<ev_ssize_t>::max(), &ptr_, &iov_, 1);
|
|
|
|
iov_offset_ = 0;
|
2022-10-19 16:42:08 +00:00
|
|
|
}
|
|
|
|
|
2022-10-21 18:15:14 +00:00
|
|
|
evbuffer* buf_;
|
2022-11-21 15:28:46 +00:00
|
|
|
evbuffer_ptr ptr_ = {};
|
2023-01-27 20:25:08 +00:00
|
|
|
evbuffer_iovec iov_ = {};
|
2022-11-21 15:28:46 +00:00
|
|
|
size_t iov_offset_ = 0;
|
2022-10-19 16:42:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Buffer() = default;
|
|
|
|
Buffer(Buffer&&) = default;
|
|
|
|
Buffer(Buffer const&) = delete;
|
|
|
|
Buffer& operator=(Buffer const&) = delete;
|
|
|
|
Buffer& operator=(Buffer&&) = default;
|
|
|
|
|
|
|
|
template<typename T>
|
2022-10-25 03:13:09 +00:00
|
|
|
explicit Buffer(T const& data)
|
2022-10-19 16:42:08 +00:00
|
|
|
{
|
|
|
|
add(std::data(data), std::size(data));
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] auto size() const noexcept
|
|
|
|
{
|
|
|
|
return evbuffer_get_length(buf_.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] auto empty() const noexcept
|
|
|
|
{
|
2022-10-25 03:13:09 +00:00
|
|
|
return evbuffer_get_length(buf_.get()) == 0;
|
2022-10-19 16:42:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] auto begin() noexcept
|
|
|
|
{
|
2022-11-22 05:54:28 +00:00
|
|
|
return Iterator{ buf_.get(), 0U };
|
2022-10-19 16:42:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] auto end() noexcept
|
|
|
|
{
|
2022-11-22 05:54:28 +00:00
|
|
|
return Iterator{ buf_.get(), size() };
|
2022-10-19 16:42:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] auto begin() const noexcept
|
|
|
|
{
|
2022-11-22 05:54:28 +00:00
|
|
|
return Iterator{ buf_.get(), 0U };
|
2022-10-19 16:42:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] auto end() const noexcept
|
|
|
|
{
|
2022-11-22 05:54:28 +00:00
|
|
|
return Iterator{ buf_.get(), size() };
|
2022-10-19 16:42:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2023-01-28 02:12:09 +00:00
|
|
|
[[nodiscard]] TR_CONSTEXPR20 bool starts_with(T const& needle) const
|
2022-10-19 16:42:08 +00:00
|
|
|
{
|
|
|
|
auto const n_bytes = std::size(needle);
|
|
|
|
auto const needle_begin = reinterpret_cast<std::byte const*>(std::data(needle));
|
|
|
|
auto const needle_end = needle_begin + n_bytes;
|
|
|
|
return n_bytes <= size() && std::equal(needle_begin, needle_end, cbegin());
|
|
|
|
}
|
|
|
|
|
2023-01-28 02:12:09 +00:00
|
|
|
[[nodiscard]] std::string to_string() const
|
|
|
|
{
|
|
|
|
auto str = std::string{};
|
|
|
|
str.resize(size());
|
|
|
|
evbuffer_copyout(buf_.get(), std::data(str), std::size(str));
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto to_buf(void* tgt, size_t n_bytes)
|
2022-10-19 16:42:08 +00:00
|
|
|
{
|
|
|
|
return evbuffer_remove(buf_.get(), tgt, n_bytes);
|
|
|
|
}
|
|
|
|
|
2023-01-28 02:12:09 +00:00
|
|
|
[[nodiscard]] uint16_t to_uint16()
|
2022-10-19 16:42:08 +00:00
|
|
|
{
|
|
|
|
auto tmp = uint16_t{};
|
2023-01-28 02:12:09 +00:00
|
|
|
to_buf(&tmp, sizeof(tmp));
|
2022-10-19 16:42:08 +00:00
|
|
|
return ntohs(tmp);
|
|
|
|
}
|
|
|
|
|
2023-01-28 02:12:09 +00:00
|
|
|
[[nodiscard]] uint32_t to_uint32()
|
2022-10-19 16:42:08 +00:00
|
|
|
{
|
|
|
|
auto tmp = uint32_t{};
|
2023-01-28 02:12:09 +00:00
|
|
|
to_buf(&tmp, sizeof(tmp));
|
2022-10-19 16:42:08 +00:00
|
|
|
return ntohl(tmp);
|
|
|
|
}
|
|
|
|
|
2023-01-28 02:12:09 +00:00
|
|
|
[[nodiscard]] uint64_t to_uint64()
|
2022-10-19 16:42:08 +00:00
|
|
|
{
|
|
|
|
auto tmp = uint64_t{};
|
2023-01-28 02:12:09 +00:00
|
|
|
to_buf(&tmp, sizeof(tmp));
|
2022-10-19 16:42:08 +00:00
|
|
|
return tr_ntohll(tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void drain(size_t n_bytes)
|
|
|
|
{
|
|
|
|
evbuffer_drain(buf_.get(), n_bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
drain(size());
|
|
|
|
}
|
|
|
|
|
2022-10-29 00:12:37 +00:00
|
|
|
// Returns the number of bytes written. Check `error` for error.
|
2023-01-28 02:12:09 +00:00
|
|
|
size_t to_socket(tr_socket_t sockfd, size_t n_bytes, tr_error** error = nullptr)
|
2022-10-19 16:42:08 +00:00
|
|
|
{
|
|
|
|
EVUTIL_SET_SOCKET_ERROR(0);
|
|
|
|
auto const res = evbuffer_write_atmost(buf_.get(), sockfd, n_bytes);
|
|
|
|
auto const err = EVUTIL_SOCKET_ERROR();
|
2022-10-29 00:12:37 +00:00
|
|
|
if (res >= 0)
|
2022-10-19 16:42:08 +00:00
|
|
|
{
|
2022-10-29 00:12:37 +00:00
|
|
|
return static_cast<size_t>(res);
|
2022-10-19 16:42:08 +00:00
|
|
|
}
|
2022-10-29 00:12:37 +00:00
|
|
|
tr_error_set(error, err, tr_net_strerror(err));
|
|
|
|
return 0;
|
2022-10-19 16:42:08 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 02:46:03 +00:00
|
|
|
[[nodiscard]] std::pair<std::byte*, size_t> pullup()
|
|
|
|
{
|
|
|
|
return { reinterpret_cast<std::byte*>(evbuffer_pullup(buf_.get(), -1)), size() };
|
|
|
|
}
|
|
|
|
|
2022-10-19 16:42:08 +00:00
|
|
|
void reserve(size_t n_bytes)
|
|
|
|
{
|
|
|
|
evbuffer_expand(buf_.get(), n_bytes - size());
|
|
|
|
}
|
|
|
|
|
2023-01-28 02:12:09 +00:00
|
|
|
size_t add_socket(tr_socket_t sockfd, size_t n_bytes, tr_error** error = nullptr)
|
2022-10-19 16:42:08 +00:00
|
|
|
{
|
|
|
|
EVUTIL_SET_SOCKET_ERROR(0);
|
|
|
|
auto const res = evbuffer_read(buf_.get(), sockfd, static_cast<int>(n_bytes));
|
|
|
|
auto const err = EVUTIL_SOCKET_ERROR();
|
2022-12-16 07:23:12 +00:00
|
|
|
|
|
|
|
if (res > 0)
|
|
|
|
{
|
|
|
|
return static_cast<size_t>(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res == 0)
|
|
|
|
{
|
2023-01-25 18:20:10 +00:00
|
|
|
tr_error_set_from_errno(error, ENOTCONN);
|
2022-12-16 07:23:12 +00:00
|
|
|
}
|
|
|
|
else
|
2022-10-19 16:42:08 +00:00
|
|
|
{
|
|
|
|
tr_error_set(error, err, tr_net_strerror(err));
|
|
|
|
}
|
2022-12-16 07:23:12 +00:00
|
|
|
|
|
|
|
return {};
|
2022-10-19 16:42:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Move all data from one buffer into another.
|
|
|
|
// This is a destructive add: the source buffer is empty after this call.
|
|
|
|
void add(Buffer& that)
|
|
|
|
{
|
|
|
|
evbuffer_add_buffer(buf_.get(), that.buf_.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
void add(Buffer&& that)
|
|
|
|
{
|
|
|
|
evbuffer_add_buffer(buf_.get(), that.buf_.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
void add(void const* bytes, size_t n_bytes)
|
|
|
|
{
|
|
|
|
evbuffer_add(buf_.get(), bytes, n_bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void add(T const& data)
|
|
|
|
{
|
|
|
|
add(std::data(data), std::size(data));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<
|
|
|
|
typename T,
|
|
|
|
typename std::enable_if_t<
|
|
|
|
std::is_same_v<T, char> || std::is_same_v<T, unsigned char> || std::is_same_v<T, std::byte>>* = nullptr>
|
|
|
|
void push_back(T ch)
|
|
|
|
{
|
|
|
|
add(&ch, 1);
|
|
|
|
}
|
2022-10-24 18:40:12 +00:00
|
|
|
|
2023-01-28 02:12:09 +00:00
|
|
|
void add_port(tr_port const& port)
|
2022-10-24 18:40:12 +00:00
|
|
|
{
|
|
|
|
auto nport = port.network();
|
|
|
|
add(&nport, sizeof(nport));
|
|
|
|
}
|
2022-10-19 16:42:08 +00:00
|
|
|
|
2023-01-28 02:12:09 +00:00
|
|
|
void add_uint8(uint8_t uch)
|
2022-10-19 16:42:08 +00:00
|
|
|
{
|
|
|
|
add(&uch, 1);
|
|
|
|
}
|
|
|
|
|
2023-01-28 02:12:09 +00:00
|
|
|
void add_uint16(uint16_t hs)
|
2022-10-19 16:42:08 +00:00
|
|
|
{
|
|
|
|
uint16_t const ns = htons(hs);
|
|
|
|
add(&ns, sizeof(ns));
|
|
|
|
}
|
|
|
|
|
2023-01-28 02:12:09 +00:00
|
|
|
void add_hton16(uint16_t hs)
|
2022-10-19 16:42:08 +00:00
|
|
|
{
|
2023-01-28 02:12:09 +00:00
|
|
|
add_uint16(hs);
|
2022-10-19 16:42:08 +00:00
|
|
|
}
|
|
|
|
|
2023-01-28 02:12:09 +00:00
|
|
|
void add_uint32(uint32_t hl)
|
2022-10-19 16:42:08 +00:00
|
|
|
{
|
|
|
|
uint32_t const nl = htonl(hl);
|
|
|
|
add(&nl, sizeof(nl));
|
|
|
|
}
|
|
|
|
|
2023-01-28 02:12:09 +00:00
|
|
|
void eadd_hton32(uint32_t hl)
|
2022-10-19 16:42:08 +00:00
|
|
|
{
|
2023-01-28 02:12:09 +00:00
|
|
|
add_uint32(hl);
|
2022-10-19 16:42:08 +00:00
|
|
|
}
|
|
|
|
|
2023-01-28 02:12:09 +00:00
|
|
|
void add_uint64(uint64_t hll)
|
2022-10-19 16:42:08 +00:00
|
|
|
{
|
|
|
|
uint64_t const nll = tr_htonll(hll);
|
|
|
|
add(&nll, sizeof(nll));
|
|
|
|
}
|
|
|
|
|
2023-01-28 02:12:09 +00:00
|
|
|
void add_hton64(uint64_t hll)
|
2022-10-19 16:42:08 +00:00
|
|
|
{
|
2023-01-28 02:12:09 +00:00
|
|
|
add_uint64(hll);
|
2022-10-19 16:42:08 +00:00
|
|
|
}
|
|
|
|
|
2023-01-28 02:12:09 +00:00
|
|
|
private:
|
|
|
|
evhelpers::evbuffer_unique_ptr buf_{ evbuffer_new() };
|
|
|
|
|
|
|
|
[[nodiscard]] Iterator cbegin() const noexcept
|
2022-10-19 16:42:08 +00:00
|
|
|
{
|
2023-01-28 02:12:09 +00:00
|
|
|
return Iterator{ buf_.get(), 0U };
|
2022-10-19 16:42:08 +00:00
|
|
|
}
|
|
|
|
|
2023-01-28 02:12:09 +00:00
|
|
|
[[nodiscard]] Iterator cend() const noexcept
|
|
|
|
{
|
|
|
|
return Iterator{ buf_.get(), size() };
|
|
|
|
}
|
2022-10-19 16:42:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace libtransmission
|