add WIP extra packet validation
This commit is contained in:
@@ -63,6 +63,5 @@ macro_rules! big_array {
|
||||
}
|
||||
|
||||
big_array! {
|
||||
40, 48, 50, 56, 64, 72, 96, 100, 128, 160, 192, 200, 224, 256, 384, 488, 500, 512,
|
||||
768, 1024, 2048, 4096, 8192, 16384, 32768, 65536,
|
||||
496, 500, 512,
|
||||
}
|
||||
+40
-3
@@ -10,7 +10,7 @@ use big_array::BigArray;
|
||||
|
||||
const MAX_FRAME_PAYLOAD:u16=508;
|
||||
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_U:usize = MAX_PAYLOAD as usize;
|
||||
|
||||
@@ -25,6 +25,7 @@ struct PacketInformation {
|
||||
struct Packet {
|
||||
packet_number: u32, //4 bytes
|
||||
payload_crc32: [u8; 4], //4 bytes
|
||||
payload_sum_crc32: u32, //4 bytes
|
||||
#[serde(with = "BigArray")]
|
||||
payload: [u8; MAX_PAYLOAD_U],
|
||||
} //508 bytes
|
||||
@@ -65,8 +66,38 @@ fn main() {
|
||||
let req = request.as_str();
|
||||
|
||||
if hash_request_regex.is_match(req) {
|
||||
todo!("hash request");
|
||||
//continue;
|
||||
println!("hash request");
|
||||
|
||||
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) {
|
||||
@@ -88,6 +119,7 @@ fn main() {
|
||||
let mut packet = Packet {
|
||||
packet_number: packet_number.parse::<u32>().unwrap(),
|
||||
payload_crc32: crc32fast::hash(&file_buffer[..bytes_read]).to_be_bytes(),
|
||||
payload_sum_crc32: 0,
|
||||
payload: [0u8; MAX_PAYLOAD_U],
|
||||
};
|
||||
packet.payload[..bytes_read].copy_from_slice(&file_buffer[..bytes_read]);
|
||||
@@ -130,15 +162,20 @@ fn main() {
|
||||
|
||||
//read file chunk by chunk
|
||||
|
||||
let mut hasher = crc32fast::Hasher::new();
|
||||
|
||||
while let Ok(bytes_read) = file.read(&mut file_buffer) {
|
||||
if bytes_read == 0 {
|
||||
break;
|
||||
}
|
||||
|
||||
hasher.update(&file_buffer[..bytes_read]);
|
||||
|
||||
let mut packet = Packet {
|
||||
packet_number,
|
||||
payload_crc32: crc32fast::hash(&file_buffer[..bytes_read]).to_be_bytes(),
|
||||
payload: [0u8; MAX_PAYLOAD_U],
|
||||
payload_sum_crc32: hasher.clone().finalize(),
|
||||
};
|
||||
packet.payload[..bytes_read].copy_from_slice(&file_buffer[..bytes_read]);
|
||||
let packet_bytes = options.serialize(&packet).unwrap();
|
||||
|
||||
Reference in New Issue
Block a user