use checksum in server acknowledgement

add WIP rust client
This commit is contained in:
Mystikfluu
2023-05-21 20:23:27 +02:00
parent 105ef005be
commit b7916ed4c8
7 changed files with 225 additions and 19 deletions
+101
View File
@@ -0,0 +1,101 @@
use bincode::{self, Error, options, Options};
use std::net::{UdpSocket, SocketAddr};
use serde::{Serialize, Deserialize};
const MAX_FRAME_PAYLOAD:u16=508;
const MAX_FRAME_PAYLOAD_U:usize=MAX_FRAME_PAYLOAD as usize;
const HEADER_SIZE:u16 = 20;
const MAX_PAYLOAD:u16 = MAX_FRAME_PAYLOAD - HEADER_SIZE;
const MAX_PAYLOAD_U:usize = MAX_PAYLOAD as usize;
#[derive(Serialize, Deserialize)]
struct PacketInformation {
packet_numbers: u32, //4 bytes
last_packet_size: u16, //2 bytes
response_filename_checksum: u64, //8 bytes
} //14 bytes
struct packet {
packet_number: u32, //4 bytes
payload_hash: u128, //16 bytes
payload: [u8; MAX_PAYLOAD_U], //508 bytes
} //512 bytes
fn main() {
let timeout = 1;
let server_ip = "213.47.107.152:1337";
let filename = "data.bin";
let local_addr: SocketAddr = "0.0.0.0:26000".parse().expect("Failed to parse address");
let socket = UdpSocket::bind(local_addr).expect("Failed to bind socket");
socket.set_read_timeout(Some(std::time::Duration::new(timeout, 0))).expect("set_read_timeout call failed");
//socket.set_nonblocking(true).expect("set_nonblocking call failed");
let server_addr: SocketAddr = server_ip.parse().expect("Failed to parse address");
let message = filename;
let sum: u64 = filename.bytes().map(|b| b as u64).sum();
socket.send_to(message.as_bytes(), server_addr).expect("Failed to send data");
// Receive data
let mut buffer = [0u8; MAX_FRAME_PAYLOAD_U];
let (received_bytes, remote_addr) = socket.recv_from(&mut buffer).expect("Failed to receive data");
let filled_buffer = &buffer[..received_bytes];
//print the filled buffer
println!("Received data: {:?} {}", filled_buffer, filled_buffer.len());
if remote_addr != server_addr {
panic!("Received data from unknown address");
}
let options = options().with_big_endian().allow_trailing_bytes().with_fixint_encoding();
let packet_info_result: Result<PacketInformation, Error> = options.deserialize(filled_buffer);
let packet_info: PacketInformation;
match packet_info_result {
Ok(packet) => {
println!("Packets to receive: {}", packet.packet_numbers);
println!("Last packet size: {}", packet.last_packet_size);
println!("Response filename checksum: {}", packet.response_filename_checksum);
//check checksum with sum
if packet.response_filename_checksum != sum {
panic!("Filename Checksums do not match, correct checksum: {}", sum);
}
packet_info = packet;
}
Err(err) => {
panic!("Failed to deserialize data: {}", err);
}
}
//create vector to store the packets
let mut packets: Vec<u8> = Vec::new();
for _ in 0..packet_info.packet_numbers {
packets.push(0);
}
let received_packets = 0;
//receive the packets
while received_packets < packet_info.packet_numbers {
let mut buffer = [0u8; MAX_PAYLOAD_U];
let (received_bytes, remote_addr) = socket.recv_from(&mut buffer).expect("Failed to receive data");
let filled_buffer = &buffer[..received_bytes];
//print the filled buffer
println!("Received data: {:?} {}", filled_buffer, filled_buffer.len());
if remote_addr != server_addr {
panic!("Received data from unknown address");
}
}
}