2023-03-07 23:36:48 +00:00
|
|
|
// Rust Levin Library
|
|
|
|
// Written in 2023 by
|
|
|
|
// Cuprate Contributors
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
|
2023-03-07 22:37:55 +00:00
|
|
|
//! # Rust Levin
|
|
|
|
//!
|
|
|
|
//! A crate for working with the Levin protocol in Rust.
|
|
|
|
//!
|
|
|
|
//! The Levin protocol is a network protocol used in the Monero cryptocurrency. It is used for
|
|
|
|
//! peer-to-peer communication between nodes. This crate provides a Rust implementation of the Levin
|
2023-07-13 21:10:52 +00:00
|
|
|
//! header serialization and allows developers to define their own bucket bodies, for a complete
|
|
|
|
//! monero protocol crate see: monero-wire.
|
2023-03-07 22:37:55 +00:00
|
|
|
//!
|
|
|
|
//! ## License
|
|
|
|
//!
|
|
|
|
//! This project is licensed under the MIT License.
|
|
|
|
|
|
|
|
// Coding conventions
|
|
|
|
#![forbid(unsafe_code)]
|
|
|
|
#![deny(non_upper_case_globals)]
|
|
|
|
#![deny(non_camel_case_types)]
|
|
|
|
#![deny(unused_mut)]
|
2023-07-13 21:10:52 +00:00
|
|
|
//#![deny(missing_docs)]
|
2023-03-07 22:37:55 +00:00
|
|
|
|
2024-03-05 01:29:57 +00:00
|
|
|
use std::fmt::Debug;
|
|
|
|
|
|
|
|
use bytes::{Buf, Bytes};
|
|
|
|
use thiserror::Error;
|
|
|
|
|
2023-07-13 21:10:52 +00:00
|
|
|
pub mod codec;
|
2023-03-07 22:37:55 +00:00
|
|
|
pub mod header;
|
2024-03-05 01:29:57 +00:00
|
|
|
pub mod message;
|
2023-03-07 22:37:55 +00:00
|
|
|
|
2023-11-30 18:09:05 +00:00
|
|
|
pub use codec::*;
|
2023-03-07 22:37:55 +00:00
|
|
|
pub use header::BucketHead;
|
2024-03-05 01:29:57 +00:00
|
|
|
pub use message::LevinMessage;
|
2023-03-07 22:37:55 +00:00
|
|
|
|
2024-03-05 01:29:57 +00:00
|
|
|
use header::Flags;
|
2023-03-07 22:37:55 +00:00
|
|
|
|
2024-03-05 01:29:57 +00:00
|
|
|
/// The version field for bucket headers.
|
2023-11-30 18:09:05 +00:00
|
|
|
const MONERO_PROTOCOL_VERSION: u32 = 1;
|
2024-03-05 01:29:57 +00:00
|
|
|
/// The signature field for bucket headers, will be constant for all peers using the Monero levin
|
|
|
|
/// protocol.
|
2023-11-30 18:09:05 +00:00
|
|
|
const MONERO_LEVIN_SIGNATURE: u64 = 0x0101010101012101;
|
2024-03-05 01:29:57 +00:00
|
|
|
/// Maximum size a bucket can be before a handshake.
|
2023-11-30 18:09:05 +00:00
|
|
|
const MONERO_MAX_PACKET_SIZE_BEFORE_HANDSHAKE: u64 = 256 * 1000; // 256 KiB
|
2024-03-05 01:29:57 +00:00
|
|
|
/// Maximum size a bucket can be after a handshake.
|
2023-11-30 18:09:05 +00:00
|
|
|
const MONERO_MAX_PACKET_SIZE: u64 = 100_000_000; // 100MB
|
2023-07-13 21:10:52 +00:00
|
|
|
|
2023-03-07 22:37:55 +00:00
|
|
|
/// Possible Errors when working with levin buckets
|
|
|
|
#[derive(Error, Debug)]
|
|
|
|
pub enum BucketError {
|
2023-07-13 21:10:52 +00:00
|
|
|
/// Invalid header flags
|
|
|
|
#[error("Invalid header flags: {0}")]
|
|
|
|
InvalidHeaderFlags(&'static str),
|
|
|
|
/// Levin bucket exceeded max size
|
|
|
|
#[error("Levin bucket exceeded max size")]
|
|
|
|
BucketExceededMaxSize,
|
|
|
|
/// Invalid Fragmented Message
|
|
|
|
#[error("Levin fragmented message was invalid: {0}")]
|
|
|
|
InvalidFragmentedMessage(&'static str),
|
2023-11-30 18:09:05 +00:00
|
|
|
/// The Header did not have the correct signature
|
|
|
|
#[error("Levin header had incorrect signature")]
|
|
|
|
InvalidHeaderSignature,
|
2023-07-17 17:43:34 +00:00
|
|
|
/// Error decoding the body
|
2024-01-13 13:22:34 +00:00
|
|
|
#[error("Error decoding bucket body: {0}")]
|
2023-11-30 18:09:05 +00:00
|
|
|
BodyDecodingError(Box<dyn std::error::Error + Send + Sync>),
|
|
|
|
/// Unknown command ID
|
|
|
|
#[error("Unknown command ID")]
|
2023-10-09 20:09:14 +00:00
|
|
|
UnknownCommand,
|
2023-07-13 21:10:52 +00:00
|
|
|
/// I/O error
|
|
|
|
#[error("I/O error: {0}")]
|
2023-03-07 22:37:55 +00:00
|
|
|
IO(#[from] std::io::Error),
|
|
|
|
}
|
|
|
|
|
2023-11-30 18:09:05 +00:00
|
|
|
/// Levin protocol settings, allows setting custom parameters.
|
|
|
|
///
|
|
|
|
/// For Monero use [`Protocol::default()`]
|
|
|
|
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
|
|
|
|
pub struct Protocol {
|
|
|
|
pub version: u32,
|
|
|
|
pub signature: u64,
|
|
|
|
pub max_packet_size_before_handshake: u64,
|
|
|
|
pub max_packet_size: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Protocol {
|
|
|
|
fn default() -> Self {
|
|
|
|
Protocol {
|
|
|
|
version: MONERO_PROTOCOL_VERSION,
|
|
|
|
signature: MONERO_LEVIN_SIGNATURE,
|
|
|
|
max_packet_size_before_handshake: MONERO_MAX_PACKET_SIZE_BEFORE_HANDSHAKE,
|
|
|
|
max_packet_size: MONERO_MAX_PACKET_SIZE,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-07 22:37:55 +00:00
|
|
|
/// A levin Bucket
|
2024-03-05 01:29:57 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2023-11-30 18:09:05 +00:00
|
|
|
pub struct Bucket<C> {
|
2023-07-13 21:10:52 +00:00
|
|
|
/// The bucket header
|
2023-11-30 18:09:05 +00:00
|
|
|
pub header: BucketHead<C>,
|
2023-07-13 21:10:52 +00:00
|
|
|
/// The bucket body
|
2024-01-30 16:09:54 +00:00
|
|
|
pub body: Bytes,
|
2023-03-07 22:37:55 +00:00
|
|
|
}
|
|
|
|
|
2023-07-17 17:43:34 +00:00
|
|
|
/// An enum representing if the message is a request, response or notification.
|
2023-11-30 18:09:05 +00:00
|
|
|
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
|
2023-03-07 22:37:55 +00:00
|
|
|
pub enum MessageType {
|
|
|
|
/// Request
|
|
|
|
Request,
|
|
|
|
/// Response
|
|
|
|
Response,
|
2023-04-24 21:37:40 +00:00
|
|
|
/// Notification
|
|
|
|
Notification,
|
2023-03-07 22:37:55 +00:00
|
|
|
}
|
|
|
|
|
2023-04-24 21:37:40 +00:00
|
|
|
impl MessageType {
|
|
|
|
/// Returns if the message requires a response
|
|
|
|
pub fn have_to_return_data(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
MessageType::Request => true,
|
|
|
|
MessageType::Response | MessageType::Notification => false,
|
2023-03-07 22:37:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-24 21:37:40 +00:00
|
|
|
/// Returns the `MessageType` given the flags and have_to_return_data fields
|
|
|
|
pub fn from_flags_and_have_to_return(
|
2024-03-05 01:29:57 +00:00
|
|
|
flags: Flags,
|
2023-04-24 21:37:40 +00:00
|
|
|
have_to_return: bool,
|
|
|
|
) -> Result<Self, BucketError> {
|
2024-03-05 01:29:57 +00:00
|
|
|
Ok(match (flags, have_to_return) {
|
|
|
|
(Flags::REQUEST, true) => MessageType::Request,
|
|
|
|
(Flags::REQUEST, false) => MessageType::Notification,
|
|
|
|
(Flags::RESPONSE, false) => MessageType::Response,
|
|
|
|
_ => {
|
|
|
|
return Err(BucketError::InvalidHeaderFlags(
|
|
|
|
"Unable to assign a message type to this bucket",
|
|
|
|
))
|
|
|
|
}
|
|
|
|
})
|
2023-07-13 21:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_flags(&self) -> header::Flags {
|
|
|
|
match self {
|
2023-11-30 18:09:05 +00:00
|
|
|
MessageType::Request | MessageType::Notification => header::Flags::REQUEST,
|
|
|
|
MessageType::Response => header::Flags::RESPONSE,
|
2023-07-13 21:10:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-17 17:43:34 +00:00
|
|
|
#[derive(Debug)]
|
2023-11-30 18:09:05 +00:00
|
|
|
pub struct BucketBuilder<C> {
|
2023-07-13 21:10:52 +00:00
|
|
|
signature: Option<u64>,
|
|
|
|
ty: Option<MessageType>,
|
2023-11-30 18:09:05 +00:00
|
|
|
command: Option<C>,
|
2023-07-13 21:10:52 +00:00
|
|
|
return_code: Option<i32>,
|
|
|
|
protocol_version: Option<u32>,
|
2024-01-30 16:09:54 +00:00
|
|
|
body: Option<Bytes>,
|
2023-07-13 21:10:52 +00:00
|
|
|
}
|
|
|
|
|
2024-03-05 01:29:57 +00:00
|
|
|
impl<C: LevinCommand> BucketBuilder<C> {
|
|
|
|
pub fn new(protocol: &Protocol) -> Self {
|
2023-07-13 21:10:52 +00:00
|
|
|
Self {
|
2024-03-05 01:29:57 +00:00
|
|
|
signature: Some(protocol.signature),
|
2023-07-13 21:10:52 +00:00
|
|
|
ty: None,
|
|
|
|
command: None,
|
|
|
|
return_code: None,
|
2024-03-05 01:29:57 +00:00
|
|
|
protocol_version: Some(protocol.version),
|
2023-07-13 21:10:52 +00:00
|
|
|
body: None,
|
2023-03-07 22:37:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-13 21:10:52 +00:00
|
|
|
pub fn set_signature(&mut self, sig: u64) {
|
|
|
|
self.signature = Some(sig)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_message_type(&mut self, ty: MessageType) {
|
|
|
|
self.ty = Some(ty)
|
|
|
|
}
|
|
|
|
|
2023-11-30 18:09:05 +00:00
|
|
|
pub fn set_command(&mut self, command: C) {
|
2023-07-13 21:10:52 +00:00
|
|
|
self.command = Some(command)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_return_code(&mut self, code: i32) {
|
|
|
|
self.return_code = Some(code)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_protocol_version(&mut self, version: u32) {
|
|
|
|
self.protocol_version = Some(version)
|
|
|
|
}
|
|
|
|
|
2024-01-30 16:09:54 +00:00
|
|
|
pub fn set_body(&mut self, body: Bytes) {
|
2023-07-13 21:10:52 +00:00
|
|
|
self.body = Some(body)
|
|
|
|
}
|
|
|
|
|
2023-11-30 18:09:05 +00:00
|
|
|
pub fn finish(self) -> Bucket<C> {
|
2023-07-13 21:10:52 +00:00
|
|
|
let body = self.body.unwrap();
|
|
|
|
let ty = self.ty.unwrap();
|
|
|
|
Bucket {
|
|
|
|
header: BucketHead {
|
|
|
|
signature: self.signature.unwrap(),
|
|
|
|
size: body.len().try_into().unwrap(),
|
|
|
|
have_to_return_data: ty.have_to_return_data(),
|
|
|
|
command: self.command.unwrap(),
|
|
|
|
return_code: self.return_code.unwrap(),
|
|
|
|
flags: ty.as_flags(),
|
|
|
|
protocol_version: self.protocol_version.unwrap(),
|
|
|
|
},
|
|
|
|
body,
|
2023-04-24 21:37:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-07 22:37:55 +00:00
|
|
|
/// A levin body
|
|
|
|
pub trait LevinBody: Sized {
|
2024-03-05 01:29:57 +00:00
|
|
|
type Command: LevinCommand + Debug;
|
2023-11-30 18:09:05 +00:00
|
|
|
|
2023-03-07 22:37:55 +00:00
|
|
|
/// Decodes the message from the data in the header
|
2024-01-30 16:09:54 +00:00
|
|
|
fn decode_message<B: Buf>(
|
|
|
|
body: &mut B,
|
2023-11-30 18:09:05 +00:00
|
|
|
typ: MessageType,
|
|
|
|
command: Self::Command,
|
|
|
|
) -> Result<Self, BucketError>;
|
2023-03-07 22:37:55 +00:00
|
|
|
|
|
|
|
/// Encodes the message
|
2024-01-30 16:09:54 +00:00
|
|
|
fn encode(self, builder: &mut BucketBuilder<Self::Command>) -> Result<(), BucketError>;
|
2023-11-30 18:09:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The levin commands.
|
|
|
|
///
|
|
|
|
/// Implementers should account for all possible u32 values, this means
|
|
|
|
/// you will probably need some sort of `Unknown` variant.
|
|
|
|
pub trait LevinCommand: From<u32> + Into<u32> + PartialEq + Clone {
|
|
|
|
/// Returns the size limit for this command.
|
|
|
|
///
|
|
|
|
/// must be less than [`usize::MAX`]
|
|
|
|
fn bucket_size_limit(&self) -> u64;
|
|
|
|
/// Returns if this is a handshake
|
|
|
|
fn is_handshake(&self) -> bool;
|
2023-03-07 22:37:55 +00:00
|
|
|
}
|