navid/veins / 343097cb4e32be11164082207ca436c707a89ddea70be26cdcdd0158c5f98bf4 / xveins/src/wire.cpp

wire.cpp

← xveins/src · 5812 bytes · raw · download

#include "wire.h"

#include <zstd.h>

#include <array>
#include <cstring>

namespace vn::wire {

namespace {

bool read_exact(net::Stream &stream, std::uint8_t *out, std::size_t count) {
  std::size_t got = 0;
  while (got < count) {
    const std::int64_t n = stream.read(out + got, count - got);
    if (n <= 0)
      return false;
    got += static_cast<std::size_t>(n);
  }
  return true;
}

} // namespace

std::optional<Frame> read_frame(net::Stream &stream) {
  std::array<std::uint8_t, 4> head{};
  if (!read_exact(stream, head.data(), head.size()))
    return std::nullopt;
  const std::uint32_t length = (static_cast<std::uint32_t>(head[0]) << 24) |
                               (static_cast<std::uint32_t>(head[1]) << 16) |
                               (static_cast<std::uint32_t>(head[2]) << 8) |
                               static_cast<std::uint32_t>(head[3]);
  if (length < 5 || length > MAX_FRAME)
    return std::nullopt;
  std::vector<std::uint8_t> body(length);
  if (!read_exact(stream, body.data(), body.size()))
    return std::nullopt;
  Frame frame;
  frame.kind = body[0];
  frame.request_id = (static_cast<std::uint32_t>(body[1]) << 24) |
                     (static_cast<std::uint32_t>(body[2]) << 16) |
                     (static_cast<std::uint32_t>(body[3]) << 8) |
                     static_cast<std::uint32_t>(body[4]);
  frame.payload.assign(body.begin() + 5, body.end());
  return frame;
}

bool write_frame(net::Stream &stream, std::uint8_t kind,
                 std::uint32_t request_id, Bytes payload) {
  if (payload.size() > MAX_FRAME - 5)
    return false;
  const std::uint32_t length = static_cast<std::uint32_t>(5 + payload.size());
  std::array<std::uint8_t, 9> head{};
  head[0] = static_cast<std::uint8_t>((length >> 24) & 255u);
  head[1] = static_cast<std::uint8_t>((length >> 16) & 255u);
  head[2] = static_cast<std::uint8_t>((length >> 8) & 255u);
  head[3] = static_cast<std::uint8_t>(length & 255u);
  head[4] = kind;
  head[5] = static_cast<std::uint8_t>((request_id >> 24) & 255u);
  head[6] = static_cast<std::uint8_t>((request_id >> 16) & 255u);
  head[7] = static_cast<std::uint8_t>((request_id >> 8) & 255u);
  head[8] = static_cast<std::uint8_t>(request_id & 255u);
  if (!stream.write_all(Bytes(head.data(), head.size())))
    return false;
  return payload.empty() || stream.write_all(payload);
}

bool write_simple(net::Stream &stream, std::uint8_t kind,
                  std::uint32_t request_id) {
  return write_frame(stream, kind, request_id, Bytes{});
}

bool write_error(net::Stream &stream, std::uint32_t request_id,
                 std::uint8_t code, std::string_view message) {
  ByteWriter writer(64);
  writer.u8(code);
  writer.length_prefixed(message);
  return write_frame(stream, ERROR, request_id, writer.view());
}

std::uint8_t read_error(Bytes payload) {
  if (payload.empty())
    return ERR_INTERNAL;
  return payload[0];
}

std::string error_message(Bytes payload) {
  if (payload.size() < 2)
    return {};
  Reader reader(payload.subspan(1));
  auto value = reader.bytes(4096);
  if (!value)
    return {};
  return std::string(str_of(*value));
}

std::string_view error_code_name(std::uint8_t code) {
  switch (code) {
  case ERR_UNSUPPORTED_VERSION:
    return "unsupported protocol version";
  case ERR_BAD_FRAME:
    return "bad frame";
  case ERR_NOT_FOUND:
    return "object not found";
  case ERR_HASH_MISMATCH:
    return "hash mismatch";
  case ERR_NON_CANONICAL:
    return "non-canonical object";
  case ERR_MISSING_CLOSURE:
    return "missing object closure";
  case ERR_REF_CONFLICT:
    return "ref conflict";
  case ERR_REJECTED:
    return "request rejected";
  case ERR_INTERNAL:
    return "server internal error";
  case ERR_UNAUTHENTICATED:
    return "authentication required";
  case ERR_FORBIDDEN:
    return "permission denied";
  case ERR_AUTH_FAILED:
    return "authentication failed";
  case ERR_QUOTA:
    return "storage quota exceeded";
  case ERR_TOO_LARGE:
    return "push too large";
  default:
    return "unknown server error";
  }
}

std::optional<IncomingRecord> parse_record(Bytes data) {
  if (data.size() < 35)
    return std::nullopt;
  Reader reader(data);
  const auto id = reader.id();
  if (!id)
    return std::nullopt;
  const auto kind = reader.u8();
  if (!kind)
    return std::nullopt;
  const auto compression = reader.u8();
  if (!compression)
    return std::nullopt;
  const auto raw_len = reader.varint();
  if (!raw_len)
    return std::nullopt;
  const auto stored_len = reader.varint();
  if (!stored_len)
    return std::nullopt;
  if (*raw_len == 0 || *raw_len > 64u * 1024u * 1024u ||
      *stored_len > MAX_FRAME)
    return std::nullopt;
  const auto stored = reader.raw(static_cast<std::size_t>(*stored_len));
  if (!stored)
    return std::nullopt;
  if (!reader.at_end())
    return std::nullopt;
  IncomingRecord record;
  record.id = *id;
  record.kind = *kind;
  if (*compression == 0) {
    if (stored->size() != static_cast<std::size_t>(*raw_len))
      return std::nullopt;
    record.body.assign(stored->begin(), stored->end());
    return record;
  }
  record.body.resize(static_cast<std::size_t>(*raw_len));
  const std::size_t result =
      ZSTD_decompress(record.body.data(), record.body.size(), stored->data(),
                      stored->size());
  if (ZSTD_isError(result) || result != record.body.size())
    return std::nullopt;
  return record;
}

std::vector<std::uint8_t> make_record(const Id &id, std::uint8_t kind,
                                      Bytes body) {
  ByteWriter writer(body.size() + 64);
  writer.raw(Bytes(id.data(), id.size()));
  writer.u8(kind);
  writer.u8(0);
  writer.varint(body.size());
  writer.varint(body.size());
  writer.raw(body);
  return writer.take();
}

} // namespace vn::wire