From d37c61fd225066dd31267599fc6dc58fc6b790a4 Mon Sep 17 00:00:00 2001 From: Dennis Schwerdel Date: Wed, 6 Jul 2016 22:35:42 +0200 Subject: [PATCH] Code improvements --- src/cloud.rs | 17 +++++++---------- src/device.rs | 6 +++--- src/tests.rs | 8 ++++---- src/types.rs | 13 ++++++++----- src/udpmessage.rs | 2 +- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/cloud.rs b/src/cloud.rs index bfbb911..1d16996 100644 --- a/src/cloud.rs +++ b/src/cloud.rs @@ -237,10 +237,10 @@ impl GenericCloud

{ }; try!(match socket.send_to(msg_data, addr) { Ok(written) if written == msg_data.len() => Ok(()), - Ok(_) => Err(Error::Socket("Sent out truncated packet")), + Ok(_) => Err(Error::Socket("Sent out truncated packet", io::Error::new(io::ErrorKind::Other, "truncated"))), Err(e) => { error!("Failed to send via network {:?}", e); - Err(Error::Socket("IOError when sending")) + Err(Error::Socket("IOError when sending", e)) } }) } @@ -263,10 +263,10 @@ impl GenericCloud

{ }; match socket.send_to(msg_data, addr) { Ok(written) if written == msg_data.len() => Ok(()), - Ok(_) => Err(Error::Socket("Sent out truncated packet")), + Ok(_) => Err(Error::Socket("Sent out truncated packet", io::Error::new(io::ErrorKind::Other, "truncated"))), Err(e) => { error!("Failed to send via network {:?}", e); - Err(Error::Socket("IOError when sending")) + Err(Error::Socket("IOError when sending", e)) } } } @@ -500,12 +500,9 @@ impl GenericCloud

{ Message::Data(payload, start, end) => { let (src, _dst) = try!(P::parse(&payload[start..end])); debug!("Writing data to device: {} bytes", end-start); - match self.device.write(&mut payload[..end], start) { - Ok(()) => (), - Err(e) => { - error!("Failed to send via device: {}", e); - return Err(Error::TunTapDev("Failed to write to device")); - } + if let Err(e) = self.device.write(&mut payload[..end], start) { + error!("Failed to send via device: {}", e); + return Err(e); } if self.learning { // Learn single address diff --git a/src/device.rs b/src/device.rs index 758c947..e2f61a3 100644 --- a/src/device.rs +++ b/src/device.rs @@ -133,7 +133,7 @@ impl Device { /// This method will return an error if the underlying read call fails. #[inline] pub fn read(&mut self, mut buffer: &mut [u8]) -> Result<(usize, usize), Error> { - let read = try!(self.fd.read(&mut buffer).map_err(|_| Error::TunTapDev("Read error"))); + let read = try!(self.fd.read(&mut buffer).map_err(|e| Error::TunTapDev("Read error", e))); let (start, read) = self.correct_data_after_read(&mut buffer, 0, read); Ok((start, read)) } @@ -171,8 +171,8 @@ impl Device { pub fn write(&mut self, mut data: &mut [u8], start: usize) -> Result<(), Error> { let start = self.correct_data_before_write(&mut data, start); match self.fd.write_all(&data[start..]) { - Ok(_) => self.fd.flush().map_err(|_| Error::TunTapDev("Flush error")), - Err(_) => Err(Error::TunTapDev("Write error")) + Ok(_) => self.fd.flush().map_err(|e| Error::TunTapDev("Flush error", e)), + Err(e) => Err(Error::TunTapDev("Write error", e)) } } diff --git a/src/tests.rs b/src/tests.rs index 5fa2be9..2b15a85 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -336,10 +336,10 @@ fn address_decode_encode() { #[test] fn address_eq() { - assert!(Address::read_from_fixed(&[1,2,3,4], 4) == Address::read_from_fixed(&[1,2,3,4], 4)); - assert!(Address::read_from_fixed(&[1,2,3,4], 4) != Address::read_from_fixed(&[1,2,3,5], 4)); - assert!(Address::read_from_fixed(&[1,2,3,4], 3) == Address::read_from_fixed(&[1,2,3,5], 3)); - assert!(Address::read_from_fixed(&[1,2,3,4], 3) != Address::read_from_fixed(&[1,2,3,4], 4)); + assert!(Address::read_from_fixed(&[1,2,3,4], 4).unwrap() == Address::read_from_fixed(&[1,2,3,4], 4).unwrap()); + assert!(Address::read_from_fixed(&[1,2,3,4], 4).unwrap() != Address::read_from_fixed(&[1,2,3,5], 4).unwrap()); + assert!(Address::read_from_fixed(&[1,2,3,4], 3).unwrap() == Address::read_from_fixed(&[1,2,3,5], 3).unwrap()); + assert!(Address::read_from_fixed(&[1,2,3,4], 3).unwrap() != Address::read_from_fixed(&[1,2,3,4], 4).unwrap()); } #[test] diff --git a/src/types.rs b/src/types.rs index 2a69b08..3f1cb7c 100644 --- a/src/types.rs +++ b/src/types.rs @@ -6,6 +6,7 @@ use std::net::{SocketAddr, Ipv4Addr, Ipv6Addr}; use std::fmt; use std::str::FromStr; use std::hash::{Hash, Hasher}; +use std::io; use super::util::{bytes_to_hex, Encoder}; @@ -211,20 +212,22 @@ pub trait Protocol: Sized { fn parse(&[u8]) -> Result<(Address, Address), Error>; } -#[derive(Debug, PartialEq)] +#[derive(Debug)] pub enum Error { Parse(&'static str), WrongNetwork(Option), - Socket(&'static str), + Socket(&'static str, io::Error), Name(String), - TunTapDev(&'static str), + TunTapDev(&'static str, io::Error), Crypto(&'static str) } impl fmt::Display for Error { fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> { match *self { - Error::Parse(ref msg) | Error::Socket(ref msg) | - Error::TunTapDev(ref msg) | Error::Crypto(ref msg) => write!(formatter, "{}", msg), + Error::Parse(ref msg) => write!(formatter, "{}", msg), + Error::Socket(ref msg, ref err) => write!(formatter, "{}: {:?}", msg, err), + Error::TunTapDev(ref msg, ref err) => write!(formatter, "{}: {:?}", msg, err), + Error::Crypto(ref msg) => write!(formatter, "{}", msg), Error::Name(ref name) => write!(formatter, "failed to resolve name '{}'", name), Error::WrongNetwork(Some(net)) => write!(formatter, "wrong network id: {}", net), Error::WrongNetwork(None) => write!(formatter, "wrong network id: none"), diff --git a/src/udpmessage.rs b/src/udpmessage.rs index a1208bd..52282bf 100644 --- a/src/udpmessage.rs +++ b/src/udpmessage.rs @@ -60,7 +60,7 @@ impl Default for TopHeader { } } -#[derive(Default, Debug, PartialEq, Eq)] +#[derive(Default, Debug, PartialEq, Eq, Clone, Copy)] pub struct Options { pub network_id: Option, }