sha3 -> crc32
serve missing packet requests in rust server
This commit is contained in:
parent
85386d11fb
commit
c1e4b3cd7a
30
rust/client/Cargo.lock
generated
30
rust/client/Cargo.lock
generated
@ -41,6 +41,15 @@ dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crc32fast"
|
||||
version = "1.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crypto-common"
|
||||
version = "0.1.6"
|
||||
@ -77,18 +86,9 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"bincode",
|
||||
"byteorder",
|
||||
"crc32fast",
|
||||
"serde",
|
||||
"sha2",
|
||||
"sha3",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "keccak"
|
||||
version = "0.1.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8f6d5ed8676d904364de097082f4e7d240b571b67989ced0240f08b7f966f940"
|
||||
dependencies = [
|
||||
"cpufeatures",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -146,16 +146,6 @@ dependencies = [
|
||||
"digest",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sha3"
|
||||
version = "0.10.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "75872d278a8f37ef87fa0ddbda7802605cb18344497949862c0d4dcb291eba60"
|
||||
dependencies = [
|
||||
"digest",
|
||||
"keccak",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.16"
|
||||
|
@ -9,8 +9,8 @@ edition = "2021"
|
||||
bincode = "1.3.3"
|
||||
serde = { version = "1.0.163", features = ["serde_derive"], default-features = false }
|
||||
byteorder = { default-features = false, version = "1.4.3" }
|
||||
sha3 = { default-features = false, version = "0.10.8" }
|
||||
sha2 = { default-features = false, version = "0.10.6" }
|
||||
crc32fast = { default-features = false, version = "1.3.2" }
|
||||
|
||||
[profile.release]
|
||||
lto = true # Enable link-time optimization
|
||||
|
@ -63,6 +63,6 @@ macro_rules! big_array {
|
||||
}
|
||||
|
||||
big_array! {
|
||||
40, 48, 50, 56, 64, 72, 96, 100, 128, 160, 192, 200, 224, 256, 384, 488, 512,
|
||||
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,
|
||||
}
|
@ -1,16 +1,16 @@
|
||||
use bincode::{self, Error, options, Options};
|
||||
use std::{net::{UdpSocket, SocketAddr}, io::Write};
|
||||
use serde::{Serialize, Deserialize};
|
||||
use sha3::{Digest, Sha3_512};
|
||||
use sha2::Sha512;
|
||||
use sha2::{Digest, Sha512};
|
||||
use std::io::{self, BufRead};
|
||||
use crc32fast;
|
||||
|
||||
mod big_array;
|
||||
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 = 20;
|
||||
const HEADER_SIZE:u16 = 8;
|
||||
const MAX_PAYLOAD:u16 = MAX_FRAME_PAYLOAD - HEADER_SIZE;
|
||||
const MAX_PAYLOAD_U:usize = MAX_PAYLOAD as usize;
|
||||
|
||||
@ -24,10 +24,10 @@ struct PacketInformation {
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct Packet {
|
||||
packet_number: u32, //4 bytes
|
||||
payload_hash: [u8; 16], //16 bytes
|
||||
payload_hash: [u8; 4], //4 bytes
|
||||
#[serde(with = "BigArray")]
|
||||
payload: [u8; MAX_PAYLOAD_U], //488 bytes
|
||||
} //512 bytes
|
||||
payload: [u8; MAX_PAYLOAD_U],
|
||||
} //508 bytes
|
||||
|
||||
fn read_stdin(message: String) -> String {
|
||||
print!("{}", message);
|
||||
@ -150,11 +150,9 @@ fn main() {
|
||||
//println!("Packet {}", p.packet_number);
|
||||
//check checksum with sum
|
||||
if p.packet_number != packet_info.packet_numbers - 1 {
|
||||
let mut hasher = Sha3_512::new();
|
||||
hasher.update(&p.payload);
|
||||
let hash = hasher.finalize();
|
||||
let hash = crc32fast::hash(&p.payload);
|
||||
|
||||
if p.payload_hash != hash[..16] {
|
||||
if p.payload_hash != hash.to_be_bytes() {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
30
rust/server/Cargo.lock
generated
30
rust/server/Cargo.lock
generated
@ -50,6 +50,15 @@ dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crc32fast"
|
||||
version = "1.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crypto-common"
|
||||
version = "0.1.6"
|
||||
@ -86,19 +95,10 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"bincode",
|
||||
"byteorder",
|
||||
"crc32fast",
|
||||
"regex",
|
||||
"serde",
|
||||
"sha2",
|
||||
"sha3",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "keccak"
|
||||
version = "0.1.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8f6d5ed8676d904364de097082f4e7d240b571b67989ced0240f08b7f966f940"
|
||||
dependencies = [
|
||||
"cpufeatures",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -179,16 +179,6 @@ dependencies = [
|
||||
"digest",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sha3"
|
||||
version = "0.10.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "75872d278a8f37ef87fa0ddbda7802605cb18344497949862c0d4dcb291eba60"
|
||||
dependencies = [
|
||||
"digest",
|
||||
"keccak",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.18"
|
||||
|
@ -9,9 +9,9 @@ edition = "2021"
|
||||
bincode = "1.3.3"
|
||||
serde = { version = "1.0.163", features = ["serde_derive"], default-features = false }
|
||||
byteorder = { default-features = false, version = "1.4.3" }
|
||||
sha3 = { default-features = false, version = "0.10.8" }
|
||||
sha2 = { default-features = false, version = "0.10.6" }
|
||||
regex = { default-features = true, version = "1.8.3" }
|
||||
crc32fast = { default-features = false, version = "1.3.2" }
|
||||
|
||||
[profile.release]
|
||||
lto = true # Enable link-time optimization
|
||||
|
@ -63,6 +63,6 @@ macro_rules! big_array {
|
||||
}
|
||||
|
||||
big_array! {
|
||||
40, 48, 50, 56, 64, 72, 96, 100, 128, 160, 192, 200, 224, 256, 384, 488, 512,
|
||||
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,
|
||||
}
|
@ -1,16 +1,16 @@
|
||||
use bincode::{self, options, Options};
|
||||
use std::{net::{UdpSocket, SocketAddr}, io::Read};
|
||||
use std::{net::{UdpSocket, SocketAddr}, io::{Read, Seek}};
|
||||
use serde::{Serialize, Deserialize};
|
||||
use sha3::{Digest, Sha3_512};
|
||||
use sha2::Sha512;
|
||||
use sha2::{Digest,Sha512};
|
||||
use regex::Regex;
|
||||
use crc32fast;
|
||||
|
||||
mod big_array;
|
||||
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 = 20;
|
||||
const HEADER_SIZE:u16 = 8;
|
||||
const MAX_PAYLOAD:u16 = MAX_FRAME_PAYLOAD - HEADER_SIZE;
|
||||
const MAX_PAYLOAD_U:usize = MAX_PAYLOAD as usize;
|
||||
|
||||
@ -24,10 +24,10 @@ struct PacketInformation {
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct Packet {
|
||||
packet_number: u32, //4 bytes
|
||||
payload_hash: [u8; 16], //16 bytes
|
||||
payload_crc32: [u8; 4], //4 bytes
|
||||
#[serde(with = "BigArray")]
|
||||
payload: [u8; MAX_PAYLOAD_U], //488 bytes
|
||||
} //512 bytes
|
||||
payload: [u8; MAX_PAYLOAD_U],
|
||||
} //508 bytes
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct StrPacket {
|
||||
@ -37,7 +37,7 @@ struct StrPacket {
|
||||
|
||||
fn main() {
|
||||
let port = "1337";
|
||||
let timeout = 100;
|
||||
let timeout = 100; //ms
|
||||
let local_addr: SocketAddr = ("0.0.0.0:".to_string()+port).parse().expect("Failed to parse address");
|
||||
let socket = UdpSocket::bind(local_addr).expect("Failed to bind socket");
|
||||
|
||||
@ -47,9 +47,9 @@ fn main() {
|
||||
|
||||
let options = options().with_big_endian().allow_trailing_bytes().with_fixint_encoding();
|
||||
|
||||
let hash_request_regex = Regex::new(r"[a-zA-Z0-9.-_ ]+:").unwrap();
|
||||
let hash_request_regex = Regex::new(r"[\w.-_ ]+:").unwrap();
|
||||
|
||||
let missing_packet_request_regex = Regex::new(r"([a-zA-Z0-9.-_ ]+)(/[0-9]+)+").unwrap();
|
||||
let missing_packet_request_regex = Regex::new(r"([\w.-_ ]+)(/\d{1,10})+").unwrap();
|
||||
|
||||
loop {
|
||||
let mut buffer = [0u8; MAX_FRAME_PAYLOAD_U];
|
||||
@ -59,7 +59,7 @@ fn main() {
|
||||
|
||||
let request_packet = bincode::deserialize::<StrPacket>(filled_buffer).unwrap();
|
||||
let mut request: String = request_packet.payload.iter().map(|&c| c as char).collect();
|
||||
request.retain(|c| c != '\0');
|
||||
request.retain(|c| c != '\0'); //remove NUL bytes
|
||||
|
||||
println!("Received request: {}", request);
|
||||
let req = request.as_str();
|
||||
@ -70,8 +70,32 @@ fn main() {
|
||||
}
|
||||
|
||||
if missing_packet_request_regex.is_match(req) {
|
||||
todo!("missing packet request");
|
||||
//continue;
|
||||
println!("missing packet request");
|
||||
let mut split = req.split("/");
|
||||
let filename = split.next().unwrap();
|
||||
|
||||
let possible_file = std::fs::File::open("./files/".to_string() + filename);
|
||||
if possible_file.is_err() {
|
||||
//println!("File not found");
|
||||
continue;
|
||||
}
|
||||
let mut file = possible_file.unwrap();
|
||||
|
||||
for packet_number in split {
|
||||
file.seek(std::io::SeekFrom::Start((packet_number.parse::<u64>().unwrap() * MAX_PAYLOAD as u64) as u64)).unwrap();
|
||||
let mut file_buffer = [0u8; MAX_PAYLOAD_U];
|
||||
let bytes_read = file.read(&mut file_buffer).unwrap();
|
||||
let mut packet = Packet {
|
||||
packet_number: packet_number.parse::<u32>().unwrap(),
|
||||
payload_crc32: crc32fast::hash(&file_buffer[..bytes_read]).to_be_bytes(),
|
||||
payload: [0u8; MAX_PAYLOAD_U],
|
||||
};
|
||||
packet.payload[..bytes_read].copy_from_slice(&file_buffer[..bytes_read]);
|
||||
let packet_bytes = options.serialize(&packet).unwrap();
|
||||
socket.send_to(&packet_bytes, remote_addr).expect("Failed to send packet");
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
println!("Received file request");
|
||||
@ -111,15 +135,11 @@ fn main() {
|
||||
break;
|
||||
}
|
||||
|
||||
let mut hasher = Sha3_512::new();
|
||||
hasher.update(&file_buffer[..bytes_read]);
|
||||
let result = hasher.finalize();
|
||||
let mut packet = Packet {
|
||||
packet_number,
|
||||
payload_hash: [0u8; 16],
|
||||
payload_crc32: crc32fast::hash(&file_buffer[..bytes_read]).to_be_bytes(),
|
||||
payload: [0u8; MAX_PAYLOAD_U],
|
||||
};
|
||||
packet.payload_hash.copy_from_slice(&result[..16]);
|
||||
packet.payload[..bytes_read].copy_from_slice(&file_buffer[..bytes_read]);
|
||||
let packet_bytes = options.serialize(&packet).unwrap();
|
||||
socket.send_to(&packet_bytes, remote_addr).expect("Failed to send packet");
|
||||
|
Loading…
x
Reference in New Issue
Block a user