navid/veins / 9892cd4abd2cafaa2face537414206345988784d1f0f786b824d9e95b1ba1e7e / xveins/src/protocol.h
protocol.h
← xveins/src · 5252 bytes · raw · download
#pragma once
#include "crypto.h"
#include "net.h"
#include "wire.h"
#include <cstdint>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
namespace vn::proto {
constexpr std::uint16_t PROTOCOL_VERSION = 1;
struct Hello {
std::uint16_t min_version = PROTOCOL_VERSION;
std::uint16_t max_version = PROTOCOL_VERSION;
std::vector<std::string> capabilities;
std::string repo;
std::string client_nonce;
};
struct HelloOk {
std::uint16_t version = 0;
std::vector<std::string> capabilities;
std::string server_nonce;
std::uint8_t auth_required = 0;
};
struct Auth {
std::string public_key;
std::string signature;
};
struct AuthOk {
std::uint8_t role = 0;
std::string fingerprint;
};
struct RefInfo {
std::string name;
Id target{};
std::string change_id;
std::uint8_t kind = 0; // 0 bookmark, 1 tag
};
struct Negotiate {
std::vector<Id> wants;
std::vector<Id> haves;
};
struct UpdateRef {
std::string name;
std::optional<Id> expected;
std::optional<Id> target;
};
std::vector<std::uint8_t> encode_hello(const Hello &hello);
std::optional<Hello> decode_hello(Bytes data);
std::vector<std::uint8_t> encode_hello_ok(const HelloOk &hello);
std::optional<HelloOk> decode_hello_ok(Bytes data);
std::vector<std::uint8_t> encode_auth(const Auth &auth);
std::optional<Auth> decode_auth(Bytes data);
std::vector<std::uint8_t> encode_auth_ok(const AuthOk &auth);
std::optional<AuthOk> decode_auth_ok(Bytes data);
std::vector<std::uint8_t> encode_refs(const std::vector<RefInfo> &refs);
std::optional<std::vector<RefInfo>> decode_refs(Bytes data);
std::vector<std::uint8_t> encode_ids(const std::vector<Id> &ids);
std::optional<std::vector<Id>> decode_ids(Bytes data, std::size_t max);
std::vector<std::uint8_t> encode_negotiate(const std::vector<Id> &wants,
const std::vector<Id> &haves);
std::optional<Negotiate> decode_negotiate(Bytes data);
std::vector<std::uint8_t> encode_negotiate_ok(const std::vector<std::uint8_t> &known,
std::uint8_t enough);
std::optional<std::vector<std::uint8_t>> decode_negotiate_ok(Bytes data);
std::vector<std::uint8_t> encode_bitmap(const std::vector<std::uint8_t> &bits);
std::optional<std::vector<std::uint8_t>> decode_bitmap(Bytes data);
std::vector<std::uint8_t> encode_update_refs(const std::vector<UpdateRef> &updates);
std::optional<std::vector<UpdateRef>> decode_update_refs(Bytes data);
// The bytes signed in AUTH.
std::string auth_message(std::uint16_t version, std::string_view repo,
Bytes server_nonce, Bytes client_nonce, Bytes binding);
std::string_view role_name(std::uint8_t role);
// ---------------------------------------------------------------------------
// URLs
// ---------------------------------------------------------------------------
struct Url {
bool tls = false;
bool ssh = false;
std::string host;
int port = 0;
std::string path;
};
std::optional<Url> parse_url(std::string_view url);
std::string url_repo_name(std::string_view url);
// ---------------------------------------------------------------------------
// Client
// ---------------------------------------------------------------------------
class Client {
public:
Client() = default;
~Client() = default;
Client(Client &&) noexcept = default;
Client &operator=(Client &&) noexcept = default;
Client(const Client &) = delete;
Client &operator=(const Client &) = delete;
// Connect and authenticate. An empty key name means anonymous unless the
// server requires authentication, in which case "default" is used.
static std::optional<Client> connect(std::string_view url,
std::string_view key_name,
std::string_view repo_name,
std::string_view tls_pin = {});
bool list_refs(std::vector<RefInfo> &refs);
bool negotiate(const std::vector<Id> &wants, const std::vector<Id> &haves);
bool fetch_skeleton(const fs::path &repo, const std::vector<Id> &wants);
bool get_chunks(const fs::path &repo, const std::vector<Id> &ids);
bool have_query(const std::vector<Id> &ids, std::vector<std::uint8_t> &bits);
bool push_objects(const fs::path &repo, const std::vector<Id> &ids);
bool update_refs(const std::vector<UpdateRef> &updates);
bool whoami(std::uint8_t &role, std::string &fingerprint);
[[nodiscard]] const std::string &error() const { return last_error_; }
[[nodiscard]] std::uint8_t role() const { return role_; }
[[nodiscard]] bool authenticated() const { return authed_; }
[[nodiscard]] const std::string &fingerprint() const { return fingerprint_; }
[[nodiscard]] bool has_connection() const { return stream_.valid(); }
private:
std::optional<wire::Frame> request(std::uint8_t kind, Bytes payload);
bool receive_stream(const fs::path &repo);
void set_error(std::string text);
void set_error_frame(const wire::Frame &frame);
net::Stream stream_;
std::string binding_;
std::uint16_t version_ = PROTOCOL_VERSION;
std::uint8_t role_ = 0;
bool authed_ = false;
std::string fingerprint_;
std::string last_error_;
};
} // namespace vn::proto