diff --git a/Cargo.lock b/Cargo.lock index 13c9879..0f1b88a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1027,7 +1027,7 @@ dependencies = [ [[package]] name = "neolink" -version = "0.5.0" +version = "0.5.1" dependencies = [ "aes", "anyhow", @@ -1065,7 +1065,7 @@ dependencies = [ [[package]] name = "neolink_core" -version = "0.5.0" +version = "0.5.1" dependencies = [ "aes", "assert_matches", diff --git a/crates/core/src/bc/de.rs b/crates/core/src/bc/de.rs index b87a567..8372122 100644 --- a/crates/core/src/bc/de.rs +++ b/crates/core/src/bc/de.rs @@ -3,22 +3,26 @@ use super::xml::{BcPayloads, BcXml}; use super::xml_crypto; use crate::RX_TIMEOUT; use err_derive::Error; -use nom::IResult; -use nom::{bytes::streaming::take, combinator::*, number::streaming::*, sequence::*}; +use nom::{ + bytes::streaming::take, combinator::*, error::context as error_context, number::streaming::*, + sequence::*, +}; use std::io::Read; use time::OffsetDateTime; +type IResult> = Result<(I, O), nom::Err>; + /// The error types used during deserialisation #[derive(Debug, Error)] pub enum Error { /// A Nom parsing error usually a malformed packet - #[error(display = "Parsing error")] + #[error(display = "Parsing error: {}", _0)] NomError(String), /// An IO error such as the stream being dropped #[error(display = "I/O error")] IoError(#[error(source)] std::io::Error), } -type NomErrorType<'a> = nom::error::Error<&'a [u8]>; +type NomErrorType<'a> = nom::error::VerboseError<&'a [u8]>; impl<'a> From>> for Error { fn from(k: nom::Err>) -> Self { @@ -35,7 +39,7 @@ fn read_from_reader(mut parser: P, mut rdr: R) -> Result where R: Read, E: for<'a> From>> + From, - P: FnMut(&[u8]) -> nom::IResult<&[u8], O>, + P: FnMut(&[u8]) -> IResult<&[u8], O>, { let mut input: Vec = Vec::new(); loop { @@ -133,10 +137,18 @@ fn bc_modern_msg<'a>( buf: &'a [u8], ) -> IResult<&'a [u8], ModernMsg> { use nom::{ - error::{make_error, ErrorKind}, + error::{ContextError, ErrorKind, ParseError}, Err, }; + fn make_error>(input: I, ctx: &'static str, kind: ErrorKind) -> E + where + I: std::marker::Copy, + E: ContextError, + { + E::add_context(input, ctx, E::from_error_kind(input, kind)) + } + let ext_len = match header.payload_offset { Some(off) => off, _ => 0, // If missing payload_offset treat all as payload @@ -150,7 +162,13 @@ fn bc_modern_msg<'a>( 0x00 => context.set_encrypted(EncryptionProtocol::Unencrypted), 0x01 => context.set_encrypted(EncryptionProtocol::BCEncrypt), 0x02 => context.set_encrypted(EncryptionProtocol::Aes(None)), - _ => return Err(Err::Error(make_error(buf, ErrorKind::MapRes))), + _ => { + return Err(Err::Error(make_error( + buf, + "Encryption Protocol is Unknown", + ErrorKind::MapRes, + ))); + } } } @@ -172,8 +190,13 @@ fn bc_modern_msg<'a>( let extension = if ext_len > 0 { // Apply the XML parse function, but throw away the reference to decrypted in the Ok and // Err case. This error-error-error thing is the same idiom Nom uses internally. - let parsed = Extension::try_parse(processed_ext_buf) - .map_err(|_| Err::Error(make_error(buf, ErrorKind::MapRes)))?; + let parsed = Extension::try_parse(processed_ext_buf).map_err(|_| { + Err::Error(make_error( + buf, + "Unable to parse Extension XML", + ErrorKind::MapRes, + )) + })?; if let Extension { binary_data: Some(1), .. @@ -199,8 +222,13 @@ fn bc_modern_msg<'a>( if context.in_bin_mode.contains(&(header.msg_num)) { payload = Some(BcPayloads::Binary(payload_buf.to_vec())); } else { - let xml = BcXml::try_parse(processed_payload_buf.as_slice()) - .map_err(|_| Err::Error(make_error(buf, ErrorKind::MapRes)))?; + let xml = BcXml::try_parse(processed_payload_buf.as_slice()).map_err(|_| { + Err::Error(make_error( + buf, + "Unable to parse Payload XML", + ErrorKind::MapRes, + )) + })?; payload = Some(BcPayloads::BcXml(xml)); } } else { @@ -211,15 +239,20 @@ fn bc_modern_msg<'a>( } fn bc_header(buf: &[u8]) -> IResult<&[u8], BcHeader> { - let (buf, _magic) = verify(le_u32, |x| *x == MAGIC_HEADER)(buf)?; - let (buf, msg_id) = le_u32(buf)?; - let (buf, body_len) = le_u32(buf)?; - let (buf, channel_id) = le_u8(buf)?; - let (buf, stream_type) = le_u8(buf)?; - let (buf, msg_num) = le_u16(buf)?; - let (buf, (response_code, class)) = tuple((le_u16, le_u16))(buf)?; + let (buf, _magic) = + error_context("Magic invalid", verify(le_u32, |x| *x == MAGIC_HEADER))(buf)?; + let (buf, msg_id) = error_context("MsgID missing", le_u32)(buf)?; + let (buf, body_len) = error_context("BodyLen missing", le_u32)(buf)?; + let (buf, channel_id) = error_context("ChannelID missing", le_u8)(buf)?; + let (buf, stream_type) = error_context("StreamType missing", le_u8)(buf)?; + let (buf, msg_num) = error_context("MsgNum missing", le_u16)(buf)?; + let (buf, (response_code, class)) = + error_context("ResponseCode missing", tuple((le_u16, le_u16)))(buf)?; - let (buf, payload_offset) = cond(has_payload_offset(class), le_u32)(buf)?; + let (buf, payload_offset) = error_context( + "Payload Offset is missing", + cond(has_payload_offset(class), le_u32), + )(buf)?; Ok(( buf, diff --git a/crates/core/src/bcmedia/de.rs b/crates/core/src/bcmedia/de.rs index d563872..72c60a1 100644 --- a/crates/core/src/bcmedia/de.rs +++ b/crates/core/src/bcmedia/de.rs @@ -15,7 +15,7 @@ const PAD_SIZE: u32 = 8; #[derive(Debug, Error)] pub enum Error { /// A Nom parsing error usually a malformed packet - #[error(display = "Parsing error")] + #[error(display = "Parsing error: {}", _0)] NomError(String), /// An IO error such as the stream being dropped #[error(display = "I/O error")] diff --git a/crates/core/src/bcudp/de.rs b/crates/core/src/bcudp/de.rs index a691f32..d05dda8 100644 --- a/crates/core/src/bcudp/de.rs +++ b/crates/core/src/bcudp/de.rs @@ -1,27 +1,36 @@ use super::{crc::calc_crc, model::*, xml::*, xml_crypto::decrypt}; use crate::RX_TIMEOUT; use err_derive::Error; -use nom::IResult; use nom::{ combinator::*, - error::{make_error, ErrorKind}, + error::{context as error_context, ContextError, ErrorKind, ParseError}, number::streaming::*, take, Err, }; use std::io::Read; use time::OffsetDateTime; +fn make_error>(input: I, ctx: &'static str, kind: ErrorKind) -> E +where + I: std::marker::Copy, + E: ContextError, +{ + E::add_context(input, ctx, E::from_error_kind(input, kind)) +} + +type IResult> = Result<(I, O), nom::Err>; + /// The error types used during deserialisation #[derive(Debug, Error)] pub enum Error { /// A Nom parsing error usually a malformed packet - #[error(display = "Parsing error")] + #[error(display = "Parsing error: {}", _0)] NomError(String), /// An IO error such as the stream being dropped #[error(display = "I/O error")] IoError(#[error(source)] std::io::Error), } -type NomErrorType<'a> = nom::error::Error<&'a [u8]>; +type NomErrorType<'a> = nom::error::VerboseError<&'a [u8]>; impl<'a> From>> for Error { fn from(k: nom::Err>) -> Self { @@ -38,7 +47,7 @@ fn read_from_reader(mut parser: P, mut rdr: R) -> Result where R: Read, E: for<'a> From>> + From, - P: FnMut(&[u8]) -> nom::IResult<&[u8], O>, + P: FnMut(&[u8]) -> IResult<&[u8], O>, { let mut input: Vec = Vec::new(); loop { @@ -89,12 +98,15 @@ impl BcUdp { } fn bcudp(buf: &[u8]) -> IResult<&[u8], BcUdp> { - let (buf, magic) = verify(le_u32, |x| { - matches!( - *x, - MAGIC_HEADER_UDP_NEGO | MAGIC_HEADER_UDP_ACK | MAGIC_HEADER_UDP_DATA - ) - })(buf)?; + let (buf, magic) = error_context( + "Magic is invalid", + verify(le_u32, |x| { + matches!( + *x, + MAGIC_HEADER_UDP_NEGO | MAGIC_HEADER_UDP_ACK | MAGIC_HEADER_UDP_DATA + ) + }), + )(buf)?; match magic { MAGIC_HEADER_UDP_NEGO => { @@ -114,31 +126,41 @@ fn bcudp(buf: &[u8]) -> IResult<&[u8], BcUdp> { } fn udp_disc(buf: &[u8]) -> IResult<&[u8], UdpDiscovery> { - let (buf, payload_size) = le_u32(buf)?; - let (buf, _unknown_a) = verify(le_u32, |&x| x == 1)(buf)?; - let (buf, tid) = le_u32(buf)?; - let (buf, checksum) = le_u32(buf)?; + let (buf, payload_size) = error_context("DISC: Missing payload size", le_u32)(buf)?; + let (buf, _unknown_a) = error_context( + "DISC: Unable to verify UnknowA", + verify(le_u32, |&x| x == 1), + )(buf)?; + let (buf, tid) = error_context("DISC: Missing TID", le_u32)(buf)?; + let (buf, checksum) = error_context("DISC: Missing checksum", le_u32)(buf)?; let (buf, enc_data_slice) = take!(buf, payload_size)?; let actual_checksum = calc_crc(enc_data_slice); assert_eq!(checksum, actual_checksum); let decrypted_payload = decrypt(tid, enc_data_slice); - let payload = UdpXml::try_parse(decrypted_payload.as_slice()) - .map_err(|_| Err::Error(make_error(buf, ErrorKind::MapRes)))?; + let payload = UdpXml::try_parse(decrypted_payload.as_slice()).map_err(|_| { + Err::Error(make_error( + buf, + "DISC: Unable to decode UDPXml", + ErrorKind::MapRes, + )) + })?; let data = UdpDiscovery { tid, payload }; Ok((buf, data)) } fn udp_ack(buf: &[u8]) -> IResult<&[u8], UdpAck> { - let (buf, connection_id) = le_i32(buf)?; - let (buf, _unknown_a) = verify(le_u32, |&x| x == 0)(buf)?; - let (buf, _unknown_b) = verify(le_u32, |&x| x == 0)(buf)?; - let (buf, packet_id) = le_u32(buf)?; // This is the point at which the camera has contigious - // packets to - let (buf, _unknown) = le_u32(buf)?; - let (buf, payload_size) = le_u32(buf)?; + let (buf, connection_id) = error_context("ACK: Missing connect ID", le_i32)(buf)?; + let (buf, _unknown_a) = + error_context("ACK: Unable to verify UnknowA", verify(le_u32, |&x| x == 0))(buf)?; + let (buf, _unknown_b) = + error_context("ACK: Unable to verify UnknowB", verify(le_u32, |&x| x == 0))(buf)?; + let (buf, packet_id) = error_context("Missing packet_id", le_u32)(buf)?; // This is the point at which the camera has contigious + // packets to + let (buf, _unknown) = error_context("ACK: Missing unknown", le_u32)(buf)?; + let (buf, payload_size) = error_context("ACK: Missing payload_size", le_u32)(buf)?; let (buf, payload) = if payload_size > 0 { let (buf, t_payload) = take!(buf, payload_size)?; // It is a binary payload of // `00 01 01 01 01 00 01` @@ -159,10 +181,11 @@ fn udp_ack(buf: &[u8]) -> IResult<&[u8], UdpAck> { } fn udp_data(buf: &[u8]) -> IResult<&[u8], UdpData> { - let (buf, connection_id) = le_i32(buf)?; - let (buf, _unknown_a) = verify(le_u32, |&x| x == 0)(buf)?; - let (buf, packet_id) = le_u32(buf)?; - let (buf, payload_size) = le_u32(buf)?; + let (buf, connection_id) = error_context("DATA: Missing connection_id", le_i32)(buf)?; + let (buf, _unknown_a) = + error_context("DATA: Unable to verify UnownA", verify(le_u32, |&x| x == 0))(buf)?; + let (buf, packet_id) = error_context("DATA: Missing packet_id", le_u32)(buf)?; + let (buf, payload_size) = error_context("DATA: Missing payload_size", le_u32)(buf)?; let (buf, payload) = take!(buf, payload_size)?; let data = UdpData {