implement rest of packets [changes by tm85]

This commit is contained in:
c0repwn3r 2023-01-28 14:21:51 -05:00
parent bbc8c0991f
commit b769fa69ea
Signed by: core
GPG Key ID: FDBF740DADDCEECF
2 changed files with 76 additions and 1 deletions

View File

@ -9,7 +9,7 @@ pub enum PacketParseError {
IncorrectPacketLength
}
pub trait SptprpPacket {
pub trait SRTPacket {
const PACKET_ID: u8;
fn to_bytes(&self) -> Vec<u8>;

View File

@ -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<u8> {
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<Self, PacketParseError> 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<u8>
}
impl SRTPacket for EncryptedDataPacket {
const PACKET_ID: u8 = 0x08;
fn to_bytes(&self) -> Vec<u8> {
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<Self, PacketParseError> 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(),
})
}
}