Add better error handling and messages

This commit is contained in:
altugbakan 2023-03-13 20:05:24 +03:00
parent 81fc120235
commit 66f479e8bc
5 changed files with 23 additions and 11 deletions

2
Cargo.lock generated
View file

@ -4,4 +4,4 @@ version = 3
[[package]] [[package]]
name = "tftpd" name = "tftpd"
version = "0.1.3" version = "0.1.4"

View file

@ -1,6 +1,6 @@
[package] [package]
name = "tftpd" name = "tftpd"
version = "0.1.3" version = "0.1.4"
authors = ["Altuğ Bakan <mail@alt.ug>"] authors = ["Altuğ Bakan <mail@alt.ug>"]
edition = "2021" edition = "2021"
description = "Multithreaded TFTP server daemon" description = "Multithreaded TFTP server daemon"

View file

@ -333,8 +333,14 @@ fn parse_ack(buf: &[u8]) -> Result<Packet, Box<dyn Error>> {
fn parse_error(buf: &[u8]) -> Result<Packet, Box<dyn Error>> { fn parse_error(buf: &[u8]) -> Result<Packet, Box<dyn Error>> {
let code = ErrorCode::from_u16(Convert::to_u16(&buf[2..])?)?; let code = ErrorCode::from_u16(Convert::to_u16(&buf[2..])?)?;
let (msg, _) = Convert::to_string(buf, 4)?; if let Ok((msg, _)) = Convert::to_string(buf, 4) {
Ok(Packet::Error { code, msg }) Ok(Packet::Error { code, msg })
} else {
Ok(Packet::Error {
code,
msg: "(no message)".to_string(),
})
}
} }
fn serialize_data(block_num: &u16, data: &Vec<u8>) -> Vec<u8> { fn serialize_data(block_num: &u16, data: &Vec<u8>) -> Vec<u8> {

View file

@ -49,7 +49,7 @@ impl Server {
} => { } => {
println!("Sending {filename} to {from}"); println!("Sending {filename} to {from}");
if let Err(err) = self.handle_rrq(filename.clone(), &mut options, &from) { if let Err(err) = self.handle_rrq(filename.clone(), &mut options, &from) {
eprintln!("{err}") eprintln!("Error while sending file: {err}")
} }
} }
Packet::Wrq { Packet::Wrq {
@ -59,7 +59,7 @@ impl Server {
} => { } => {
println!("Receiving {filename} from {from}"); println!("Receiving {filename} from {from}");
if let Err(err) = self.handle_wrq(filename.clone(), &mut options, &from) { if let Err(err) = self.handle_wrq(filename.clone(), &mut options, &from) {
eprintln!("{err}") eprintln!("Error while receiving file: {err}")
} }
} }
_ => { _ => {
@ -69,7 +69,7 @@ impl Server {
ErrorCode::IllegalOperation, ErrorCode::IllegalOperation,
"invalid request".to_string(), "invalid request".to_string(),
) )
.unwrap_or_else(|err| eprintln!("{err}")); .unwrap_or_else(|_| eprintln!("Received invalid request"));
} }
}; };
} }

View file

@ -1,6 +1,6 @@
use std::{ use std::{
error::Error, error::Error,
fs::File, fs::{self, File},
io::{Read, Write}, io::{Read, Write},
net::{SocketAddr, UdpSocket}, net::{SocketAddr, UdpSocket},
path::{Path, PathBuf}, path::{Path, PathBuf},
@ -95,6 +95,12 @@ impl Worker {
if let Err(err) = handle_receive() { if let Err(err) = handle_receive() {
eprintln!("{err}"); eprintln!("{err}");
if fs::remove_file(&file_path).is_err() {
eprintln!(
"Error while cleaning {}",
file_path.file_name().unwrap().to_str().unwrap()
);
}
} }
}); });
} }
@ -143,7 +149,7 @@ fn send_file(
println!( println!(
"Sent {} to {}", "Sent {} to {}",
file_path.display(), file_path.file_name().unwrap().to_str().unwrap(),
socket.peer_addr().unwrap() socket.peer_addr().unwrap()
); );
Ok(()) Ok(())
@ -198,7 +204,7 @@ fn receive_file(
println!( println!(
"Received {} from {}", "Received {} from {}",
file_path.display(), file_path.file_name().unwrap().to_str().unwrap(),
socket.peer_addr().unwrap() socket.peer_addr().unwrap()
); );
Ok(()) Ok(())