add WIP extra packet validation

This commit is contained in:
Mystikfluu 2023-05-30 22:12:23 +02:00
parent c1e4b3cd7a
commit bd118b3164
4 changed files with 56 additions and 8 deletions

View File

@ -63,6 +63,5 @@ macro_rules! big_array {
} }
big_array! { big_array! {
40, 48, 50, 56, 64, 72, 96, 100, 128, 160, 192, 200, 224, 256, 384, 488, 500, 512, 496, 500, 512,
768, 1024, 2048, 4096, 8192, 16384, 32768, 65536,
} }

View File

@ -10,7 +10,7 @@ use big_array::BigArray;
const MAX_FRAME_PAYLOAD:u16=508; const MAX_FRAME_PAYLOAD:u16=508;
const MAX_FRAME_PAYLOAD_U:usize=MAX_FRAME_PAYLOAD as usize; const MAX_FRAME_PAYLOAD_U:usize=MAX_FRAME_PAYLOAD as usize;
const HEADER_SIZE:u16 = 8; const HEADER_SIZE:u16 = 12;
const MAX_PAYLOAD:u16 = MAX_FRAME_PAYLOAD - HEADER_SIZE; const MAX_PAYLOAD:u16 = MAX_FRAME_PAYLOAD - HEADER_SIZE;
const MAX_PAYLOAD_U:usize = MAX_PAYLOAD as usize; const MAX_PAYLOAD_U:usize = MAX_PAYLOAD as usize;
@ -25,6 +25,7 @@ struct PacketInformation {
struct Packet { struct Packet {
packet_number: u32, //4 bytes packet_number: u32, //4 bytes
payload_hash: [u8; 4], //4 bytes payload_hash: [u8; 4], //4 bytes
payload_sum_hash: u32, //4 bytes
#[serde(with = "BigArray")] #[serde(with = "BigArray")]
payload: [u8; MAX_PAYLOAD_U], payload: [u8; MAX_PAYLOAD_U],
} //508 bytes } //508 bytes
@ -107,8 +108,10 @@ fn main() {
//create vector to store the packets //create vector to store the packets
let mut packets: Vec<Vec<u8>> = Vec::new(); let mut packets: Vec<Vec<u8>> = Vec::new();
let mut packet_hashes: Vec<u32> = Vec::new();
for _ in 0..packet_info.packet_numbers { for _ in 0..packet_info.packet_numbers {
packets.push(Vec::new()); packets.push(Vec::new());
packet_hashes.push(0);
} }
let mut received_packets = 0; let mut received_packets = 0;
@ -155,6 +158,7 @@ fn main() {
if p.payload_hash != hash.to_be_bytes() { if p.payload_hash != hash.to_be_bytes() {
continue; continue;
} }
packet_hashes[p.packet_number as usize] = p.payload_sum_hash;
packet = p; packet = p;
} else { } else {
@ -217,6 +221,15 @@ fn main() {
print!("Packet {}/{}\r", received_packets, packet_info.packet_numbers); print!("Packet {}/{}\r", received_packets, packet_info.packet_numbers);
} }
let mut crc32 = crc32fast::Hasher::new();
for i in 0..packets.len() {
crc32.update(&packets[i]);
if packet_hashes[i] != crc32.clone().finalize() {
println!("Packet {} hash does not match", i);
todo!("Request packets again")
}
}
//check hash via sha512 //check hash via sha512
let mut hasher = Sha512::new(); let mut hasher = Sha512::new();

View File

@ -63,6 +63,5 @@ macro_rules! big_array {
} }
big_array! { big_array! {
40, 48, 50, 56, 64, 72, 96, 100, 128, 160, 192, 200, 224, 256, 384, 488, 500, 512, 496, 500, 512,
768, 1024, 2048, 4096, 8192, 16384, 32768, 65536,
} }

View File

@ -10,7 +10,7 @@ use big_array::BigArray;
const MAX_FRAME_PAYLOAD:u16=508; const MAX_FRAME_PAYLOAD:u16=508;
const MAX_FRAME_PAYLOAD_U:usize=MAX_FRAME_PAYLOAD as usize; const MAX_FRAME_PAYLOAD_U:usize=MAX_FRAME_PAYLOAD as usize;
const HEADER_SIZE:u16 = 8; const HEADER_SIZE:u16 = 12;
const MAX_PAYLOAD:u16 = MAX_FRAME_PAYLOAD - HEADER_SIZE; const MAX_PAYLOAD:u16 = MAX_FRAME_PAYLOAD - HEADER_SIZE;
const MAX_PAYLOAD_U:usize = MAX_PAYLOAD as usize; const MAX_PAYLOAD_U:usize = MAX_PAYLOAD as usize;
@ -25,6 +25,7 @@ struct PacketInformation {
struct Packet { struct Packet {
packet_number: u32, //4 bytes packet_number: u32, //4 bytes
payload_crc32: [u8; 4], //4 bytes payload_crc32: [u8; 4], //4 bytes
payload_sum_crc32: u32, //4 bytes
#[serde(with = "BigArray")] #[serde(with = "BigArray")]
payload: [u8; MAX_PAYLOAD_U], payload: [u8; MAX_PAYLOAD_U],
} //508 bytes } //508 bytes
@ -65,8 +66,38 @@ fn main() {
let req = request.as_str(); let req = request.as_str();
if hash_request_regex.is_match(req) { if hash_request_regex.is_match(req) {
todo!("hash request"); println!("hash request");
//continue;
let mut sha512_hasher = Sha512::new();
let file_result = std::fs::File::open("./files/".to_string() + req);
if file_result.is_err() {
println!("File not found");
continue;
}
let mut file = file_result.unwrap();
let mut file_buffer = [0u8; MAX_PAYLOAD_U];
//read file chunk by chunk
while let Ok(bytes_read) = file.read(&mut file_buffer) {
if bytes_read == 0 {
break;
}
sha512_hasher.update(&file_buffer[..bytes_read]);
}
let result = sha512_hasher.finalize();
//add 1 byte of padding to result
let mut result_bytes = [0u8; 65];
result_bytes[..64].copy_from_slice(&result[..64]);
socket.send_to(&result_bytes, remote_addr).expect("Failed to send file hash");
continue;
} }
if missing_packet_request_regex.is_match(req) { if missing_packet_request_regex.is_match(req) {
@ -88,6 +119,7 @@ fn main() {
let mut packet = Packet { let mut packet = Packet {
packet_number: packet_number.parse::<u32>().unwrap(), packet_number: packet_number.parse::<u32>().unwrap(),
payload_crc32: crc32fast::hash(&file_buffer[..bytes_read]).to_be_bytes(), payload_crc32: crc32fast::hash(&file_buffer[..bytes_read]).to_be_bytes(),
payload_sum_crc32: 0,
payload: [0u8; MAX_PAYLOAD_U], payload: [0u8; MAX_PAYLOAD_U],
}; };
packet.payload[..bytes_read].copy_from_slice(&file_buffer[..bytes_read]); packet.payload[..bytes_read].copy_from_slice(&file_buffer[..bytes_read]);
@ -130,15 +162,20 @@ fn main() {
//read file chunk by chunk //read file chunk by chunk
let mut hasher = crc32fast::Hasher::new();
while let Ok(bytes_read) = file.read(&mut file_buffer) { while let Ok(bytes_read) = file.read(&mut file_buffer) {
if bytes_read == 0 { if bytes_read == 0 {
break; break;
} }
hasher.update(&file_buffer[..bytes_read]);
let mut packet = Packet { let mut packet = Packet {
packet_number, packet_number,
payload_crc32: crc32fast::hash(&file_buffer[..bytes_read]).to_be_bytes(), payload_crc32: crc32fast::hash(&file_buffer[..bytes_read]).to_be_bytes(),
payload: [0u8; MAX_PAYLOAD_U], payload: [0u8; MAX_PAYLOAD_U],
payload_sum_crc32: hasher.clone().finalize(),
}; };
packet.payload[..bytes_read].copy_from_slice(&file_buffer[..bytes_read]); packet.payload[..bytes_read].copy_from_slice(&file_buffer[..bytes_read]);
let packet_bytes = options.serialize(&packet).unwrap(); let packet_bytes = options.serialize(&packet).unwrap();