diff --git a/src/main.rs b/src/main.rs index 55d8cf4..8c12353 100644 --- a/src/main.rs +++ b/src/main.rs @@ -148,7 +148,7 @@ struct ServerResponse { fn client > (mut args: I) -> Result <(), AppError> { use rand::RngCore; - let mut common_params = CommonParams::default (); + let common_params = CommonParams::default (); let mut bind_addr = "0.0.0.0".to_string (); while let Some (arg) = args.next () { @@ -208,7 +208,7 @@ fn client > (mut args: I) -> Result <(), AppError> { } let mut peers: Vec <_> = peers.into_iter ().collect (); - peers.sort_by_key (|(k, v)| v.mac); + peers.sort_by_key (|(_, v)| v.mac); println! ("Found {} peers:", peers.len ()); for (ip, resp) in peers.into_iter () { @@ -236,7 +236,7 @@ fn client > (mut args: I) -> Result <(), AppError> { fn server > (mut args: I) -> Result <(), AppError> { - let mut common_params = CommonParams::default (); + let common_params = CommonParams::default (); let mut bind_addr = "0.0.0.0".to_string (); let mut nickname = String::new (); diff --git a/src/message.rs b/src/message.rs index 3baf394..c1e5cda 100644 --- a/src/message.rs +++ b/src/message.rs @@ -138,28 +138,6 @@ impl Message { Ok (cursor.into_inner ()) } - fn read1 (r: &mut R) -> Result { - let t = tlv::Reader::u8 (r)?; - - Ok (match t { - 1 => { - let mut idem_id = [0u8; 8]; - r.read_exact (&mut idem_id)?; - - let mac = Self::read_mac_opt (r)?; - Self::Request1 { - idem_id, - mac, - } - }, - 2 => { - let mac = Self::read_mac_opt (r)?; - Self::Response1 (mac) - }, - _ => return Err (MessageError::UnknownType), - }) - } - fn read2 (r: &mut R) -> Result { let t = tlv::Reader::u8 (r)?; @@ -179,24 +157,12 @@ impl Message { Self::Response1 (mac) }, 3 => { - let mut len = [0; 4]; - r.read_exact (&mut len)?; - let _len = len; + tlv::Reader::<_>::length (r)?; let mut idem_id = [0; 8]; r.read_exact (&mut idem_id)?; - let mut nickname_len = [0; 4]; - r.read_exact (&mut nickname_len)?; - let nickname_len = u32::from_le_bytes (nickname_len); - let nickname_len = usize::try_from (nickname_len)?; - - if nickname_len > 64 { - return Err (MessageError::LengthPrefixTooLong ((64, nickname_len))); - } - - let mut nickname = vec! [0u8; nickname_len]; - r.read_exact (&mut nickname)?; + let nickname = tlv::Reader::<_>::lv_bytes_to_vec (r, 64)?; let nickname = String::from_utf8 (nickname)?; Self::Response2 (Response2 { @@ -221,12 +187,6 @@ impl Message { }) } - pub fn from_slice1 (buf: &[u8]) -> Result { - let mut cursor = Cursor::new (buf); - tlv::Reader::expect (&mut cursor, &MAGIC_NUMBER)?; - Self::read1 (&mut cursor) - } - pub fn from_slice2 (buf: &[u8]) -> Result , MessageError> { let mut cursor = Cursor::new (buf); tlv::Reader::expect (&mut cursor, &MAGIC_NUMBER)?; @@ -375,78 +335,4 @@ mod test { assert_eq! (input, decoded); } } - - #[test] - fn test_read_1 () { - for input in [ - Message::Request1 { - idem_id: [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08], - mac: None, - }, - Message::Response1 (Some ([0x11, 0x22, 0x33, 0x44, 0x55, 0x66])), - Message::Response1 (None), - ].into_iter () { - let encoded = input.to_vec ().unwrap (); - let decoded = Message::from_slice1 (&encoded).unwrap (); - assert_eq! (input, decoded); - } - - for (expected, input) in [ - ( - Message::Request1 { - idem_id: [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08], - mac: None, - }, - vec! [ - 154, 74, 67, 129, - // Request tag - 1, - // Idem ID - 1, 2, 3, 4, 5, 6, 7, 8, - // MAC is None - 0, - ], - ), - ( - Message::Response1 (Some ([0x11, 0x22, 0x33, 0x44, 0x55, 0x66])), - vec! [ - // Magic number for LookAround packets - 154, 74, 67, 129, - // Response tag - 2, - // MAC is Some - 1, - // MAC - 17, 34, 51, 68, 85, 102, - ], - ), - ( - Message::Response1 (None), - vec! [ - // Magic number for LookAround packets - 154, 74, 67, 129, - // Response tag - 2, - // MAC is None - 0, - ], - ), - ( - Message::Response1 (None), - vec! [ - // Magic number for LookAround packets - 154, 74, 67, 129, - // Response tag - 2, - // MAC is None - 0, - // New tag that older versions will just ignore - 255, - ], - ), - ].into_iter () { - let actual = Message::from_slice1 (&input).unwrap (); - assert_eq! (actual, expected, "{:?}", actual); - } - } } diff --git a/src/tlv.rs b/src/tlv.rs index fbb4121..21c1bff 100644 --- a/src/tlv.rs +++ b/src/tlv.rs @@ -6,8 +6,14 @@ type Result = std::result::Result ; pub enum TlvError { #[error ("Buffer too big")] BufferTooBig, - #[error ("Caller-provided buffer too small")] - CallerBufferTooSmall, + + // Violets are purple, + // To live is to suffer, + // The data is too big, + // For the gosh-darn buffer. + + #[error ("Data too big")] + DataTooBig, #[error (transparent)] Io (#[from] std::io::Error), #[error ("Actual bytes didn't match expected bytes")] @@ -54,22 +60,24 @@ impl Reader { Ok (()) } - fn length (r: &mut R) -> Result { + pub fn length (r: &mut R) -> Result { let mut buf = [0; 4]; r.read_exact (&mut buf)?; Ok (u32::from_le_bytes (buf)) } - fn lv_bytes (r: &mut R, buf: &mut [u8]) -> Result { + pub fn lv_bytes_to_vec (r: &mut R, limit: usize) -> Result > { let l = Self::length (r)?; - if usize::try_from (l)? > buf.len () { - return Err (TlvError::CallerBufferTooSmall); + let l = usize::try_from (l)?; + if l > limit { + return Err (TlvError::DataTooBig); } - r.read_exact (&mut buf [0..usize::try_from (l)?])?; + let mut v = vec! [0u8; l]; + r.read_exact (&mut v)?; - Ok (l) + Ok (v) } pub fn u8 (r: &mut R) -> std::io::Result { @@ -102,11 +110,9 @@ mod test { let mut r = Cursor::new (v); - let mut buf = vec! [0; 1024]; + let buf = super::Reader::lv_bytes_to_vec (&mut r, 1024).unwrap (); - let bytes_read = super::Reader::lv_bytes (&mut r, &mut buf).unwrap (); - - assert_eq! (usize::try_from (bytes_read).unwrap (), b.len ()); - assert_eq! (b, &buf [0..usize::try_from (bytes_read).unwrap ()]); + assert_eq! (buf.len (), b.len ()); + assert_eq! (b, &buf [0..usize::try_from (buf.len ()).unwrap ()]); } }