Some code cleanup

This commit is contained in:
Dennis Schwerdel 2016-07-06 18:48:58 +02:00
parent 4e047b6065
commit 3488d2d2a3
5 changed files with 18 additions and 18 deletions

View File

@ -5,7 +5,7 @@
use std::net::{SocketAddr, ToSocketAddrs}; use std::net::{SocketAddr, ToSocketAddrs};
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::net::UdpSocket; use std::net::UdpSocket;
use std::io::Result as IoResult; use std::io;
use std::fmt; use std::fmt;
use std::os::unix::io::AsRawFd; use std::os::unix::io::AsRawFd;
use std::marker::PhantomData; use std::marker::PhantomData;
@ -279,7 +279,7 @@ impl<P: Protocol> GenericCloud<P> {
/// # Errors /// # Errors
/// Returns an IOError if the underlying system call fails /// Returns an IOError if the underlying system call fails
#[allow(dead_code)] #[allow(dead_code)]
pub fn address(&self) -> IoResult<(SocketAddr, SocketAddr)> { pub fn address(&self) -> io::Result<(SocketAddr, SocketAddr)> {
Ok((try!(self.socket4.local_addr()), try!(self.socket6.local_addr()))) Ok((try!(self.socket4.local_addr()), try!(self.socket6.local_addr())))
} }

View File

@ -3,7 +3,7 @@
// This software is licensed under GPL-3 or newer (see LICENSE.md) // This software is licensed under GPL-3 or newer (see LICENSE.md)
use std::os::unix::io::{AsRawFd, RawFd}; use std::os::unix::io::{AsRawFd, RawFd};
use std::io::{Result as IoResult, Error as IoError, Read, Write}; use std::io::{self, Error as IoError, Read, Write};
use std::fs; use std::fs;
use std::fmt; use std::fmt;
@ -61,7 +61,7 @@ impl Device {
/// ///
/// # Panics /// # Panics
/// This method panics if the interface name is longer than 31 bytes. /// This method panics if the interface name is longer than 31 bytes.
pub fn new(ifname: &str, type_: Type) -> IoResult<Self> { pub fn new(ifname: &str, type_: Type) -> io::Result<Self> {
let fd = try!(fs::OpenOptions::new().read(true).write(true).open("/dev/net/tun")); let fd = try!(fs::OpenOptions::new().read(true).write(true).open("/dev/net/tun"));
// Add trailing \0 to interface name // Add trailing \0 to interface name
let mut ifname_string = String::with_capacity(32); let mut ifname_string = String::with_capacity(32);
@ -112,7 +112,7 @@ impl Device {
/// # Errors /// # Errors
/// This method will return an error if the file can not be opened for reading and writing. /// This method will return an error if the file can not be opened for reading and writing.
#[allow(dead_code)] #[allow(dead_code)]
pub fn dummy(ifname: &str, path: &str, type_: Type) -> IoResult<Self> { pub fn dummy(ifname: &str, path: &str, type_: Type) -> io::Result<Self> {
Ok(Device{ Ok(Device{
fd: try!(fs::OpenOptions::new().create(true).read(true).write(true).open(path)), fd: try!(fs::OpenOptions::new().create(true).read(true).write(true).open(path)),
ifname: ifname.to_string(), ifname: ifname.to_string(),
@ -191,8 +191,8 @@ impl Device {
// BSD-based systems add a 4-byte header containing the Ethertype for TUN // BSD-based systems add a 4-byte header containing the Ethertype for TUN
assert!(start>=4); assert!(start>=4);
match buffer[start] >> 4 { // IP version match buffer[start] >> 4 { // IP version
4 => buffer[start-4..start].clone_from_slice(&[0x00, 0x00, 0x08, 0x00]), 4 => buffer[start-4..start].copy_from_slice(&[0x00, 0x00, 0x08, 0x00]),
6 => buffer[start-4..start].clone_from_slice(&[0x00, 0x00, 0x86, 0xdd]), 6 => buffer[start-4..start].copy_from_slice(&[0x00, 0x00, 0x86, 0xdd]),
_ => unreachable!() _ => unreachable!()
} }
start-4 start-4

View File

@ -42,8 +42,8 @@ impl Protocol for Frame {
let mut dst = [0; 16]; let mut dst = [0; 16];
src[0] = data[pos]; src[1] = data[pos+1]; src[0] = data[pos]; src[1] = data[pos+1];
dst[0] = data[pos]; dst[1] = data[pos+1]; dst[0] = data[pos]; dst[1] = data[pos+1];
src[2..8].clone_from_slice(src_data); src[2..8].copy_from_slice(src_data);
dst[2..8].clone_from_slice(dst_data); dst[2..8].copy_from_slice(dst_data);
Ok((Address{data: src, len: 8}, Address{data: dst, len: 8})) Ok((Address{data: src, len: 8}, Address{data: dst, len: 8}))
} else { } else {
let src = try!(Address::read_from_fixed(&src_data, 6)); let src = try!(Address::read_from_fixed(&src_data, 6));

View File

@ -41,7 +41,7 @@ impl Address {
return Err(Error::ParseError("Address too short")); return Err(Error::ParseError("Address too short"));
} }
let mut bytes = [0; 16]; let mut bytes = [0; 16];
bytes[0..len].clone_from_slice(&data[0..len]); bytes[0..len].copy_from_slice(&data[0..len]);
Ok(Address{data: bytes, len: len as u8}) Ok(Address{data: bytes, len: len as u8})
} }
@ -50,7 +50,7 @@ impl Address {
assert!(data.len() >= self.len as usize + 1); assert!(data.len() >= self.len as usize + 1);
data[0] = self.len; data[0] = self.len;
let len = self.len as usize; let len = self.len as usize;
data[1..len+1].clone_from_slice(&self.data[0..len]); data[1..len+1].copy_from_slice(&self.data[0..len]);
self.len as usize + 1 self.len as usize + 1
} }
} }
@ -114,7 +114,7 @@ impl FromStr for Address {
if let Ok(addr) = Ipv4Addr::from_str(text) { if let Ok(addr) = Ipv4Addr::from_str(text) {
let ip = addr.octets(); let ip = addr.octets();
let mut res = [0; 16]; let mut res = [0; 16];
res[0..4].clone_from_slice(&ip); res[0..4].copy_from_slice(&ip);
return Ok(Address{data: res, len: 4}); return Ok(Address{data: res, len: 4});
} }
if let Ok(addr) = Ipv6Addr::from_str(text) { if let Ok(addr) = Ipv6Addr::from_str(text) {

View File

@ -9,7 +9,7 @@ use super::types::{NodeId, Error, NetworkId, Range, NODE_ID_BYTES};
use super::util::{bytes_to_hex, Encoder}; use super::util::{bytes_to_hex, Encoder};
use super::crypto::Crypto; use super::crypto::Crypto;
const MAGIC: [u8; 3] = [0x76, 0x70, 0x6e]; const MAGIC: [u8; 3] = *b"vpn";
pub const VERSION: u8 = 1; pub const VERSION: u8 = 1;
const NETWORK_ID_BYTES: usize = 8; const NETWORK_ID_BYTES: usize = 8;
@ -36,7 +36,7 @@ impl TopHeader {
return Err(Error::ParseError("Empty message")); return Err(Error::ParseError("Empty message"));
} }
let mut header = TopHeader::default(); let mut header = TopHeader::default();
header.magic.clone_from_slice(&data[0..3]); header.magic.copy_from_slice(&data[0..3]);
header.version = data[3]; header.version = data[3];
header.crypto_method = data[4]; header.crypto_method = data[4];
header.flags = data[6]; header.flags = data[6];
@ -45,7 +45,7 @@ impl TopHeader {
} }
pub fn write_to(&self, data: &mut [u8]) -> usize { pub fn write_to(&self, data: &mut [u8]) -> usize {
data[0..3].clone_from_slice(&self.magic); data[0..3].copy_from_slice(&self.magic);
data[3] = self.version; data[3] = self.version;
data[4] = self.crypto_method; data[4] = self.crypto_method;
data[6] = self.flags; data[6] = self.flags;
@ -177,7 +177,7 @@ pub fn decode<'a>(data: &'a mut [u8], crypto: &mut Crypto) -> Result<(Options, M
let stage = data[pos]; let stage = data[pos];
pos += 1; pos += 1;
let mut node_id = [0; NODE_ID_BYTES]; let mut node_id = [0; NODE_ID_BYTES];
node_id.clone_from_slice(&data[pos..pos+NODE_ID_BYTES]); node_id.copy_from_slice(&data[pos..pos+NODE_ID_BYTES]);
pos += NODE_ID_BYTES; pos += NODE_ID_BYTES;
let count = data[pos] as usize; let count = data[pos] as usize;
pos += 1; pos += 1;
@ -223,7 +223,7 @@ pub fn encode<'a>(options: &Options, msg: &'a mut Message, mut buf: &'a mut [u8]
pos += 1; pos += 1;
for addr in v4addrs { for addr in v4addrs {
let ip = addr.ip().octets(); let ip = addr.ip().octets();
buf[pos..pos+4].clone_from_slice(&ip); buf[pos..pos+4].copy_from_slice(&ip);
pos += 4; pos += 4;
Encoder::write_u16(addr.port(), &mut buf[pos..]); Encoder::write_u16(addr.port(), &mut buf[pos..]);
pos += 2; pos += 2;
@ -246,7 +246,7 @@ pub fn encode<'a>(options: &Options, msg: &'a mut Message, mut buf: &'a mut [u8]
assert!(buf.len() >= pos + 2 + NODE_ID_BYTES); assert!(buf.len() >= pos + 2 + NODE_ID_BYTES);
buf[pos] = stage; buf[pos] = stage;
pos += 1; pos += 1;
buf[pos..pos+NODE_ID_BYTES].clone_from_slice(node_id); buf[pos..pos+NODE_ID_BYTES].copy_from_slice(node_id);
pos += NODE_ID_BYTES; pos += NODE_ID_BYTES;
assert!(ranges.len() <= 255); assert!(ranges.len() <= 255);
buf[pos] = ranges.len() as u8; buf[pos] = ranges.len() as u8;