From 68d05e3a11ee8bf5236cdac87225e3518d918e5a Mon Sep 17 00:00:00 2001 From: altugbakan Date: Wed, 8 Mar 2023 21:12:57 +0300 Subject: [PATCH] Add work on worker and server --- src/config.rs | 4 +- src/main.rs | 6 +- src/message.rs | 27 ++++++-- src/packet.rs | 63 ++++++++++++++++-- src/server.rs | 177 ++++++++++++++++++++++++------------------------- src/worker.rs | 94 ++++++++++++++++++++++++-- 6 files changed, 259 insertions(+), 112 deletions(-) diff --git a/src/config.rs b/src/config.rs index 32ee90d..89332dc 100644 --- a/src/config.rs +++ b/src/config.rs @@ -41,7 +41,7 @@ impl Config { "-d" | "--directory" => { if let Some(dir_str) = args.next() { if !Path::new(&dir_str).exists() { - return Err(format!("{} does not exist", dir_str).into()); + return Err(format!("{dir_str} does not exist").into()); } config.directory = PathBuf::from(dir_str); } else { @@ -60,7 +60,7 @@ impl Config { println!(" -h, --help\t\t\tPrint help information"); process::exit(0); } - invalid => return Err(format!("invalid flag: {}", invalid).into()), + invalid => return Err(format!("invalid flag: {invalid}").into()), } } diff --git a/src/main.rs b/src/main.rs index ab31ba9..3b6827d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,8 +16,10 @@ fn main() { }); println!( - "Running TFTP Server on {}:{}", - config.ip_address, config.port + "Running TFTP Server on {}:{} in {}", + config.ip_address, + config.port, + config.directory.display() ); server.listen(); diff --git a/src/message.rs b/src/message.rs index 85ae55d..ed89460 100644 --- a/src/message.rs +++ b/src/message.rs @@ -3,7 +3,7 @@ use std::{ net::{SocketAddr, UdpSocket}, }; -use crate::packet::{ErrorCode, Opcode, Option}; +use crate::packet::{ErrorCode, Opcode, Packet, TransferOption}; pub struct Message; @@ -35,24 +35,37 @@ impl Message { } pub fn send_error_to(socket: &UdpSocket, to: &SocketAddr, code: ErrorCode, msg: &str) { + eprintln!("{msg}"); if socket.send_to(&get_error_buf(code, msg), to).is_err() { eprintln!("could not send an error message"); } } - pub fn send_oack_to( + pub fn send_oack( socket: &UdpSocket, - to: &SocketAddr, - options: Vec