From 9821aabf0b50d2487b07502d3d2cd89e7d62bdbe Mon Sep 17 00:00:00 2001 From: murilo ijanc Date: Tue, 24 Mar 2026 15:04:03 -0300 Subject: Initial commit NAT-aware Kademlia DHT library for peer-to-peer networks. Features: - Distributed key-value storage (iterative FIND_NODE, FIND_VALUE, STORE) - NAT traversal via DTUN hole-punching and proxy relay - Reliable Datagram Protocol (RDP) with 7-state connection machine - Datagram transport with automatic fragmentation/reassembly - Ed25519 packet authentication - 256-bit node IDs (Ed25519 public keys) - Rate limiting, ban list, and eclipse attack mitigation - Persistence and metrics - OpenBSD and Linux support --- src/error.rs | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/error.rs (limited to 'src/error.rs') diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..d5d6f56 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,80 @@ +use std::fmt; +use std::io; + +#[derive(Debug)] +pub enum Error { + Io(io::Error), + + /// Bad magic number in header. + BadMagic(u16), + + /// Protocol version not supported. + UnsupportedVersion(u8), + + /// Unknown message type byte. + UnknownMessageType(u8), + + /// Packet or buffer too small for the operation. + BufferTooSmall, + + /// Payload length doesn't match declared length. + PayloadMismatch, + + /// Generic invalid message (malformed content). + InvalidMessage, + + /// Not connected to the network. + NotConnected, + + /// Operation timed out. + Timeout, + + /// Signature verification failed. + BadSignature, +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Error::Io(e) => write!(f, "I/O error: {e}"), + Error::BadMagic(m) => { + write!(f, "bad magic: 0x{m:04x}") + } + Error::UnsupportedVersion(v) => { + write!(f, "unsupported version: {v}") + } + Error::UnknownMessageType(t) => { + write!(f, "unknown message type: 0x{t:02x}") + } + Error::BufferTooSmall => { + write!(f, "buffer too small") + } + Error::PayloadMismatch => { + write!(f, "payload length mismatch") + } + Error::InvalidMessage => { + write!(f, "invalid message") + } + Error::NotConnected => write!(f, "not connected"), + Error::Timeout => write!(f, "timeout"), + Error::BadSignature => { + write!(f, "signature verification failed") + } + } + } +} + +impl std::error::Error for Error { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + match self { + Error::Io(e) => Some(e), + _ => None, + } + } +} + +impl From for Error { + fn from(e: io::Error) -> Self { + Error::Io(e) + } +} -- cgit v1.2.3