From 7f32af672926bd73994ad71760ec0b7add9ff9b1 Mon Sep 17 00:00:00 2001 From: jake Date: Wed, 18 Dec 2019 21:05:58 -0800 Subject: [PATCH] packet errors for unknown packets --- psopacket/src/lib.rs | 4 ++-- src/lib.rs | 7 +++++-- src/packet/messages.rs | 8 +++++++- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/psopacket/src/lib.rs b/psopacket/src/lib.rs index 600edba..08b3b6c 100644 --- a/psopacket/src/lib.rs +++ b/psopacket/src/lib.rs @@ -160,7 +160,7 @@ fn generate_psopacket_impl(pkt_cmd: u16, name: syn::Ident, attrs: &Vec else { 0 }; if cmd != #pkt_cmd { - return Err(PacketParseError::WrongPacketCommand); + return Err(PacketParseError::WrongPacketCommand {expected: #pkt_cmd, got: cmd}); } if len as usize != data.len() { @@ -422,7 +422,7 @@ fn generate_psomessage_impl(msg_cmd: u8, name: syn::Ident, attrs: &Vec let len = cur.read(&mut subbuf).unwrap(); if cmd != #msg_cmd { - return Err(PacketParseError::WrongPacketCommand); + return Err(PacketParseError::WrongMessageCommand {expected: #msg_cmd, got: cmd}); } if len != size as usize * 4 - 2 { diff --git a/src/lib.rs b/src/lib.rs index 8b48f4e..0b5e266 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,9 +12,12 @@ use std::io::{Read, Seek}; #[derive(Debug, PartialEq)] pub enum PacketParseError { NotEnoughBytes, - WrongPacketCommand, + WrongPacketCommand {expected: u16, got: u16}, WrongPacketForServerType(u16, Vec), + UnknownPacket(u16, Vec), WrongPacketSize(u16, usize), + WrongMessageCommand {expected: u8, got: u8}, + UnknownMessage(u8, Vec), DataStructNotLargeEnough(u64, usize), InvalidValue, ReadError, @@ -336,7 +339,7 @@ mod test { let mut bytes = test.as_bytes(); bytes[2] = 17; let test2 = Test::from_bytes(&bytes); - assert!(test2 == Err(PacketParseError::WrongPacketCommand)); + assert!(test2 == Err(PacketParseError::WrongPacketCommand { expected: 0x23, got: 17})); } #[test] diff --git a/src/packet/messages.rs b/src/packet/messages.rs index fb23b03..fdbfa77 100644 --- a/src/packet/messages.rs +++ b/src/packet/messages.rs @@ -110,7 +110,13 @@ impl PSOPacketData for GameMessage { PlayerDoneChangingMap::CMD => Ok(GameMessage::PlayerDoneChangingMap(PlayerDoneChangingMap::from_bytes(&mut cur)?)), TellOtherPlayerMyLocation::CMD => Ok(GameMessage::TellOtherPlayerMyLocation(TellOtherPlayerMyLocation::from_bytes(&mut cur)?)), UnknownAE::CMD => Ok(GameMessage::UnknownAE(UnknownAE::from_bytes(&mut cur)?)), - _ => Err(PacketParseError::WrongPacketCommand), + _ => Err(PacketParseError::UnknownMessage(byte[0], + { + let mut b = vec![0; len[0] as usize * 4]; + cur.read(&mut b).unwrap(); + b.to_vec() + } + )), } } fn as_bytes(&self) -> Vec {