diff --git a/libsrt/src/packets/mod.rs b/libsrt/src/packets/mod.rs index 4569618..87f7d44 100644 --- a/libsrt/src/packets/mod.rs +++ b/libsrt/src/packets/mod.rs @@ -9,7 +9,7 @@ pub enum PacketParseError { IncorrectPacketLength } -pub trait SptprpPacket { +pub trait SRTPacket { const PACKET_ID: u8; fn to_bytes(&self) -> Vec; diff --git a/libsrt/src/packets/recvbound.rs b/libsrt/src/packets/recvbound.rs index e69de29..b1ec4c0 100644 --- a/libsrt/src/packets/recvbound.rs +++ b/libsrt/src/packets/recvbound.rs @@ -0,0 +1,75 @@ +use crate::packets::{PacketParseError, SRTPacket}; + +#[derive(Debug, PartialEq, Eq)] +pub struct HandshakeResponsePacket { + pub r_pub: [u8; 32], + pub check_encrypted: [u8; 48] +} + +impl SRTPacket for HandshakeResponsePacket { + const PACKET_ID: u8 = 0x07; + + fn to_bytes(&self) -> Vec { + let mut vec = vec![0x07]; + vec.extend_from_slice(&self.r_pub); + vec.extend_from_slice(&self.check_encrypted); + + vec + } + + fn from_bytes(bytes: &[u8]) -> Result where Self: Sized { + if bytes.len() != 81 { + return Err(PacketParseError::IncorrectPacketLength) + } + if bytes[0] != 0x07 { + return Err(PacketParseError::IncorrectPacketType) + } + + let r_pub = &bytes[1..33]; + let check = &bytes[33..81]; + + Ok(Self { + r_pub: r_pub.try_into().unwrap(), + check_encrypted: check.try_into().unwrap() + }) + } +} + +#[derive(Debug, PartialEq)] +pub struct EncryptedDataPacket { + pub data_len: u64, + pub enc_data: Vec +} +impl SRTPacket for EncryptedDataPacket { + const PACKET_ID: u8 = 0x08; + + fn to_bytes(&self) -> Vec { + let mut bytes = vec![8u8]; + bytes.extend_from_slice(&self.data_len.to_le_bytes()); + bytes.extend_from_slice(&self.enc_data[..]); + bytes + } + + fn from_bytes(bytes: &[u8]) -> Result where Self: Sized { + if bytes.len() < 9 { + return Err(PacketParseError::IncorrectPacketLength) + } + + if bytes[0] != 0x08 { + return Err(PacketParseError::IncorrectPacketType) + } + + let data_len = u64::from_le_bytes(bytes[1..9].try_into().unwrap()) as usize; + + if bytes.len() < 9 + data_len { + return Err(PacketParseError::IncorrectPacketLength) + } + + let data = &bytes[9..9 + data_len]; + + Ok(Self { + data_len: data_len as u64, + enc_data: data.to_vec(), + }) + } +} \ No newline at end of file