Code improvements

This commit is contained in:
Dennis Schwerdel 2016-07-06 22:35:42 +02:00
parent 6d38130146
commit d37c61fd22
5 changed files with 23 additions and 23 deletions

View File

@ -237,10 +237,10 @@ impl<P: Protocol> GenericCloud<P> {
}; };
try!(match socket.send_to(msg_data, addr) { try!(match socket.send_to(msg_data, addr) {
Ok(written) if written == msg_data.len() => Ok(()), 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) => { Err(e) => {
error!("Failed to send via network {:?}", 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<P: Protocol> GenericCloud<P> {
}; };
match socket.send_to(msg_data, addr) { match socket.send_to(msg_data, addr) {
Ok(written) if written == msg_data.len() => Ok(()), 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) => { Err(e) => {
error!("Failed to send via network {:?}", 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<P: Protocol> GenericCloud<P> {
Message::Data(payload, start, end) => { Message::Data(payload, start, end) => {
let (src, _dst) = try!(P::parse(&payload[start..end])); let (src, _dst) = try!(P::parse(&payload[start..end]));
debug!("Writing data to device: {} bytes", end-start); debug!("Writing data to device: {} bytes", end-start);
match self.device.write(&mut payload[..end], start) { if let Err(e) = self.device.write(&mut payload[..end], start) {
Ok(()) => (), error!("Failed to send via device: {}", e);
Err(e) => { return Err(e);
error!("Failed to send via device: {}", e);
return Err(Error::TunTapDev("Failed to write to device"));
}
} }
if self.learning { if self.learning {
// Learn single address // Learn single address

View File

@ -133,7 +133,7 @@ impl Device {
/// This method will return an error if the underlying read call fails. /// This method will return an error if the underlying read call fails.
#[inline] #[inline]
pub fn read(&mut self, mut buffer: &mut [u8]) -> Result<(usize, usize), Error> { 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); let (start, read) = self.correct_data_after_read(&mut buffer, 0, read);
Ok((start, read)) Ok((start, read))
} }
@ -171,8 +171,8 @@ impl Device {
pub fn write(&mut self, mut data: &mut [u8], start: usize) -> Result<(), Error> { pub fn write(&mut self, mut data: &mut [u8], start: usize) -> Result<(), Error> {
let start = self.correct_data_before_write(&mut data, start); let start = self.correct_data_before_write(&mut data, start);
match self.fd.write_all(&data[start..]) { match self.fd.write_all(&data[start..]) {
Ok(_) => self.fd.flush().map_err(|_| Error::TunTapDev("Flush error")), Ok(_) => self.fd.flush().map_err(|e| Error::TunTapDev("Flush error", e)),
Err(_) => Err(Error::TunTapDev("Write error")) Err(e) => Err(Error::TunTapDev("Write error", e))
} }
} }

View File

@ -336,10 +336,10 @@ fn address_decode_encode() {
#[test] #[test]
fn address_eq() { 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).unwrap() == Address::read_from_fixed(&[1,2,3,4], 4).unwrap());
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], 4).unwrap() != Address::read_from_fixed(&[1,2,3,5], 4).unwrap());
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).unwrap() == Address::read_from_fixed(&[1,2,3,5], 3).unwrap());
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], 3).unwrap() != Address::read_from_fixed(&[1,2,3,4], 4).unwrap());
} }
#[test] #[test]

View File

@ -6,6 +6,7 @@ use std::net::{SocketAddr, Ipv4Addr, Ipv6Addr};
use std::fmt; use std::fmt;
use std::str::FromStr; use std::str::FromStr;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use std::io;
use super::util::{bytes_to_hex, Encoder}; use super::util::{bytes_to_hex, Encoder};
@ -211,20 +212,22 @@ pub trait Protocol: Sized {
fn parse(&[u8]) -> Result<(Address, Address), Error>; fn parse(&[u8]) -> Result<(Address, Address), Error>;
} }
#[derive(Debug, PartialEq)] #[derive(Debug)]
pub enum Error { pub enum Error {
Parse(&'static str), Parse(&'static str),
WrongNetwork(Option<NetworkId>), WrongNetwork(Option<NetworkId>),
Socket(&'static str), Socket(&'static str, io::Error),
Name(String), Name(String),
TunTapDev(&'static str), TunTapDev(&'static str, io::Error),
Crypto(&'static str) Crypto(&'static str)
} }
impl fmt::Display for Error { impl fmt::Display for Error {
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> { fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match *self { match *self {
Error::Parse(ref msg) | Error::Socket(ref msg) | Error::Parse(ref msg) => write!(formatter, "{}", msg),
Error::TunTapDev(ref msg) | Error::Crypto(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::Name(ref name) => write!(formatter, "failed to resolve name '{}'", name),
Error::WrongNetwork(Some(net)) => write!(formatter, "wrong network id: {}", net), Error::WrongNetwork(Some(net)) => write!(formatter, "wrong network id: {}", net),
Error::WrongNetwork(None) => write!(formatter, "wrong network id: none"), Error::WrongNetwork(None) => write!(formatter, "wrong network id: none"),

View File

@ -60,7 +60,7 @@ impl Default for TopHeader {
} }
} }
#[derive(Default, Debug, PartialEq, Eq)] #[derive(Default, Debug, PartialEq, Eq, Clone, Copy)]
pub struct Options { pub struct Options {
pub network_id: Option<NetworkId>, pub network_id: Option<NetworkId>,
} }