diff options
| author | murilo ijanc | 2026-03-24 15:04:03 -0300 |
|---|---|---|
| committer | murilo ijanc | 2026-03-24 15:04:03 -0300 |
| commit | 9821aabf0b50d2487b07502d3d2cd89e7d62bdbe (patch) | |
| tree | 53da095ff90cc755bac3d4bf699172b5e8cd07d6 /src/error.rs | |
| download | tesseras-dht-0.1.0.tar.gz | |
Initial commitv0.1.0
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
Diffstat (limited to 'src/error.rs')
| -rw-r--r-- | src/error.rs | 80 |
1 files changed, 80 insertions, 0 deletions
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<io::Error> for Error { + fn from(e: io::Error) -> Self { + Error::Io(e) + } +} |