aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.toml3
-rw-r--r--src/cfb8.rs40
-rw-r--r--src/lib.rs2
-rw-r--r--src/reader.rs5
-rw-r--r--src/writer.rs14
5 files changed, 55 insertions, 9 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 1094109..45ae42d 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -22,4 +22,5 @@ default = [ "compression", "encryption" ]
futures-io = ["futures", "async-trait"]
tokio-io = ["tokio", "async-trait"]
encryption = ["aes"]
-compression = [ "flate2" ] \ No newline at end of file
+compression = [ "flate2" ]
+backtrace = [] \ No newline at end of file
diff --git a/src/cfb8.rs b/src/cfb8.rs
index 6fc3bb6..1348f73 100644
--- a/src/cfb8.rs
+++ b/src/cfb8.rs
@@ -3,6 +3,8 @@ use aes::{
Aes128,
};
use thiserror::Error;
+#[cfg(feature = "backtrace")]
+use std::backtrace::Backtrace;
pub type CraftCipherResult<T> = Result<T, CipherError>;
@@ -15,9 +17,35 @@ pub enum CipherComponent {
#[derive(Debug, Error)]
pub enum CipherError {
#[error("encryption is already enabled and cannot be enabled again")]
- AlreadyEnabled,
- #[error("bad size '{1}' for '{0:?}'")]
- BadSize(CipherComponent, usize),
+ AlreadyEnabled {
+ #[cfg(feature = "backtrace")]
+ backtrace: Backtrace,
+ },
+ #[error("bad size '{size}' for '{component:?}'")]
+ BadSize {
+ size: usize,
+ component: CipherComponent,
+ #[cfg(feature = "backtrace")]
+ backtrace: Backtrace
+ },
+}
+
+impl CipherError {
+ fn bad_size(component: CipherComponent, size: usize) -> Self {
+ CipherError::BadSize {
+ component,
+ size,
+ #[cfg(feature = "backtrace")]
+ backtrace: Backtrace::capture()
+ }
+ }
+
+ fn already_enabled() -> Self {
+ CipherError::AlreadyEnabled {
+ #[cfg(feature = "backtrace")]
+ backtrace: Backtrace::capture()
+ }
+ }
}
const BYTES_SIZE: usize = 16;
@@ -31,11 +59,11 @@ pub struct CraftCipher {
impl CraftCipher {
pub fn new(key: &[u8], iv: &[u8]) -> CraftCipherResult<Self> {
if iv.len() != BYTES_SIZE {
- return Err(CipherError::BadSize(CipherComponent::Iv, iv.len()));
+ return Err(CipherError::bad_size(CipherComponent::Iv, iv.len()));
}
if key.len() != BYTES_SIZE {
- return Err(CipherError::BadSize(CipherComponent::Key, iv.len()));
+ return Err(CipherError::bad_size(CipherComponent::Key, key.len()));
}
let mut iv_out = [0u8; BYTES_SIZE];
@@ -97,7 +125,7 @@ pub(crate) fn setup_craft_cipher(
iv: &[u8],
) -> Result<(), CipherError> {
if target.is_some() {
- Err(CipherError::AlreadyEnabled)
+ Err(CipherError::already_enabled())
} else {
*target = Some(CraftCipher::new(key, iv)?);
Ok(())
diff --git a/src/lib.rs b/src/lib.rs
index 5789dd5..09cb456 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,4 +1,4 @@
-#![feature(backtrace)]
+#![cfg_attr(feature = "backtrace", feature(backtrace))]
#[cfg(feature = "encryption")]
mod cfb8;
diff --git a/src/reader.rs b/src/reader.rs
index 886ecfe..55edaee 100644
--- a/src/reader.rs
+++ b/src/reader.rs
@@ -7,6 +7,7 @@ use flate2::{DecompressError, FlushDecompress, Status};
use mcproto_rs::protocol::{Id, PacketDirection, RawPacket, State};
use mcproto_rs::types::VarInt;
use mcproto_rs::{Deserialize, Deserialized};
+#[cfg(feature = "backtrace")]
use std::backtrace::Backtrace;
use std::io;
use thiserror::Error;
@@ -20,18 +21,21 @@ pub enum ReadError {
IoFailure {
#[from]
err: io::Error,
+ #[cfg(feature = "backtrace")]
backtrace: Backtrace,
},
#[error("failed to read header VarInt")]
PacketHeaderErr {
#[from]
err: mcproto_rs::DeserializeErr,
+ #[cfg(feature = "backtrace")]
backtrace: Backtrace,
},
#[error("failed to read packet")]
PacketErr {
#[from]
err: mcproto_rs::protocol::PacketErr,
+ #[cfg(feature = "backtrace")]
backtrace: Backtrace,
},
#[cfg(feature = "compression")]
@@ -39,6 +43,7 @@ pub enum ReadError {
DecompressFailed {
#[from]
err: DecompressErr,
+ #[cfg(feature = "backtrace")]
backtrace: Backtrace,
},
}
diff --git a/src/writer.rs b/src/writer.rs
index ef97043..acea3f3 100644
--- a/src/writer.rs
+++ b/src/writer.rs
@@ -7,6 +7,7 @@ use flate2::{CompressError, Compression, FlushCompress, Status};
use mcproto_rs::protocol::{Id, Packet, PacketDirection, RawPacket, State};
use mcproto_rs::types::VarInt;
use mcproto_rs::{Serialize, SerializeErr, SerializeResult, Serializer};
+#[cfg(feature = "backtrace")]
use std::backtrace::Backtrace;
use std::ops::{Deref, DerefMut};
use thiserror::Error;
@@ -20,6 +21,7 @@ pub enum WriteError {
Serialize {
#[from]
err: PacketSerializeFail,
+ #[cfg(feature = "backtrace")]
backtrace: Backtrace,
},
#[error("failed to compress packet")]
@@ -27,27 +29,34 @@ pub enum WriteError {
CompressFail {
#[from]
err: CompressError,
+ #[cfg(feature = "backtrace")]
backtrace: Backtrace,
},
#[error("compression gave buf error")]
#[cfg(feature = "compression")]
- CompressBufError { backtrace: Backtrace },
+ CompressBufError {
+ #[cfg(feature = "backtrace")]
+ backtrace: Backtrace
+ },
#[error("io error while writing data")]
IoFail {
#[from]
err: std::io::Error,
+ #[cfg(feature = "backtrace")]
backtrace: Backtrace,
},
#[error("bad direction")]
BadDirection {
attempted: PacketDirection,
expected: PacketDirection,
+ #[cfg(feature = "backtrace")]
backtrace: Backtrace,
},
#[error("bad state")]
BadState {
attempted: State,
expected: State,
+ #[cfg(feature = "backtrace")]
backtrace: Backtrace,
},
}
@@ -330,6 +339,7 @@ impl<W> CraftWriter<W> {
return Err(WriteError::BadDirection {
expected: self.direction,
attempted: id.direction,
+ #[cfg(feature = "backtrace")]
backtrace: Backtrace::capture(),
});
}
@@ -338,6 +348,7 @@ impl<W> CraftWriter<W> {
return Err(WriteError::BadState {
expected: self.state,
attempted: id.state,
+ #[cfg(feature = "backtrace")]
backtrace: Backtrace::capture(),
});
}
@@ -544,6 +555,7 @@ fn compress<'a, 'b>(
Status::Ok => {}
Status::BufError => {
return Err(WriteError::CompressBufError {
+ #[cfg(feature = "backtrace")]
backtrace: Backtrace::capture(),
})
}