Updated dependencies, rustfmt

pull/46/head
Dennis Schwerdel 2019-12-04 09:32:35 +01:00
parent b279c43718
commit 509ab1504e
26 changed files with 1328 additions and 959 deletions

View File

@ -2,6 +2,11 @@
This project follows [semantic versioning](http://semver.org).
### Unreleased
- [changed] Rust version 1.41.0
- [changed] Updated dependencies
### v1.0.0 (2019-03-21)
- [added] Added ability to publish small beacons for rendezvous

733
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -18,16 +18,16 @@ serde = "1.0"
serde_derive = "1.0"
serde_yaml = "0.8"
log = { version = "0.4", features = ["std"] }
signal = "0.6"
signal = "0.7"
libc = "0.2"
rand = "0.6"
rand = "0.7"
fnv = "1"
net2 = "0.2"
yaml-rust = "0.4"
igd = "^0.8.2"
igd = "0.9"
siphasher = "0.3"
daemonize = "0.3"
ring = "0.14"
daemonize = "0.4"
ring = "0.16"
base-62 = "0.1"
[build-dependencies]

17
rustfmt.toml Normal file
View File

@ -0,0 +1,17 @@
use_small_heuristics = "Max"
comment_width = 120
fn_args_layout = "Compressed"
where_single_line = true
merge_imports = true
max_width = 120
force_multiline_blocks = true
normalize_comments = true
trailing_comma = "Never"
trailing_semicolon = false
use_field_init_shorthand = true
use_try_shorthand = true
wrap_comments = true
overflow_delimited_expr = true
blank_lines_upper_bound = 2
normalize_doc_attributes = true
inline_attribute_width = 50

View File

@ -5,20 +5,24 @@
use base_62;
use ring::digest;
use std::num::Wrapping;
use std::path::Path;
use std::io::{self, Write, Read};
use std::fs::{self, Permissions, File};
use std::os::unix::fs::PermissionsExt;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::marker::PhantomData;
use std::mem;
use std::thread;
use std::process::{Command, Stdio};
use std::{
fs::{self, File, Permissions},
io::{self, Read, Write},
marker::PhantomData,
mem,
num::Wrapping,
os::unix::fs::PermissionsExt,
path::Path,
process::{Command, Stdio},
sync::{
atomic::{AtomicBool, Ordering},
Arc, Mutex
},
thread
};
use super::util::{Encoder, TimeSource};
use std::net::{SocketAddr, SocketAddrV4, Ipv4Addr, SocketAddrV6, Ipv6Addr};
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
const TYPE_BEGIN: u8 = 0;
@ -52,10 +56,7 @@ impl<TS: TimeSource> BeaconSerializer<TS> {
Self {
magic: magic.to_owned(),
shared_key: shared_key.to_owned(),
future_peers: Arc::new(FutureResult {
has_result: AtomicBool::new(false),
result: Mutex::new(Vec::new())
}),
future_peers: Arc::new(FutureResult { has_result: AtomicBool::new(false), result: Mutex::new(Vec::new()) }),
_dummy_ts: PhantomData
}
}
@ -95,7 +96,6 @@ impl<TS: TimeSource> BeaconSerializer<TS> {
fn end(&self) -> String {
base_62::encode(&self.get_keystream(TYPE_END, 0, 0))[0..5].to_string()
}
fn encrypt_data(&self, data: &mut Vec<u8>) {
// Note: the 1 byte seed is only meant to protect from random changes,
// not malicious ones. For full protection, at least 8 bytes (~12
@ -196,8 +196,12 @@ impl<TS: TimeSource> BeaconSerializer<TS> {
ip[i] = Encoder::read_u16(&dat[i * 2..i * 2 + 2]);
}
let port = Encoder::read_u16(&dat[16..]);
let addr = SocketAddr::V6(SocketAddrV6::new(Ipv6Addr::new(ip[0], ip[1], ip[2],
ip[3], ip[4], ip[5], ip[6], ip[7]), port, 0, 0));
let addr = SocketAddr::V6(SocketAddrV6::new(
Ipv6Addr::new(ip[0], ip[1], ip[2], ip[3], ip[4], ip[5], ip[6], ip[7]),
port,
0,
0
));
peers.push(addr);
}
peers
@ -222,9 +226,15 @@ impl<TS: TimeSource> BeaconSerializer<TS> {
let end = self.end();
let beacon = format!("{}{}{}", begin, data, end);
debug!("Calling beacon command: {}", cmd);
let process = Command::new("sh").args(&["-c", cmd])
.env("begin", begin).env("data", data).env("end", end).env("beacon", beacon)
.stdout(Stdio::piped()).stderr(Stdio::piped()).spawn()?;
let process = Command::new("sh")
.args(&["-c", cmd])
.env("begin", begin)
.env("data", data)
.env("end", end)
.env("beacon", beacon)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()?;
thread::spawn(move || {
let output = process.wait_with_output().expect("Failed to wait on child");
if !output.status.success() {
@ -256,7 +266,9 @@ impl<TS: TimeSource> BeaconSerializer<TS> {
peers
}
pub fn read_from_file<P: AsRef<Path>>(&self, path: P, ttl_hours: Option<u16>) -> Result<Vec<SocketAddr>, io::Error> {
pub fn read_from_file<P: AsRef<Path>>(
&self, path: P, ttl_hours: Option<u16>
) -> Result<Vec<SocketAddr>, io::Error> {
let mut f = File::open(&path)?;
let mut contents = String::new();
f.read_to_string(&mut contents)?;
@ -267,9 +279,13 @@ impl<TS: TimeSource> BeaconSerializer<TS> {
let begin = self.begin();
let end = self.end();
debug!("Calling beacon command: {}", cmd);
let process = Command::new("sh").args(&["-c", cmd])
.env("begin", begin).env("end", end)
.stdout(Stdio::piped()).stderr(Stdio::piped()).spawn()?;
let process = Command::new("sh")
.args(&["-c", cmd])
.env("begin", begin)
.env("end", end)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()?;
let this = self.clone();
thread::spawn(move || {
let output = process.wait_with_output().expect("Failed to wait on child");
@ -299,10 +315,10 @@ impl<TS: TimeSource> BeaconSerializer<TS> {
}
#[cfg(test)] use crate::util::MockTimeSource;
#[cfg(test)] use std::str::FromStr;
#[cfg(test)] use std::time::Duration;
#[cfg(test)] use tempfile;
#[cfg(test)] use crate::util::MockTimeSource;
#[test]
fn encode() {
@ -329,7 +345,10 @@ fn decode() {
peers.push(SocketAddr::from_str("6.6.6.6:53").unwrap());
assert_eq!(format!("{:?}", peers), format!("{:?}", ser.decode("juWwKhjVTYjbwJjtYAZlMfEj7IDO55LN", None)));
peers.push(SocketAddr::from_str("[::1]:5678").unwrap());
assert_eq!(format!("{:?}", peers), format!("{:?}", ser.decode("juWwKjF5qZG7PE5imnpi5XARaXnP3UsMsGBLxM4FNFDzvjlKt1SO55LN", None)));
assert_eq!(
format!("{:?}", peers),
format!("{:?}", ser.decode("juWwKjF5qZG7PE5imnpi5XARaXnP3UsMsGBLxM4FNFDzvjlKt1SO55LN", None))
);
}
#[test]
@ -339,8 +358,14 @@ fn decode_split() {
let mut peers = Vec::new();
peers.push(SocketAddr::from_str("1.2.3.4:5678").unwrap());
peers.push(SocketAddr::from_str("6.6.6.6:53").unwrap());
assert_eq!(format!("{:?}", peers), format!("{:?}", ser.decode("juWwK-hj.VT:Yj bw\tJj\ntY(AZ)lM[fE]j7üIDäO55LN", None)));
assert_eq!(format!("{:?}", peers), format!("{:?}", ser.decode("j -, \nuW--wKhjVTYjbwJjtYAZlMfEj7IDO(5}5ÖÄÜ\nLN", None)));
assert_eq!(
format!("{:?}", peers),
format!("{:?}", ser.decode("juWwK-hj.VT:Yj bw\tJj\ntY(AZ)lM[fE]j7üIDäO55LN", None))
);
assert_eq!(
format!("{:?}", peers),
format!("{:?}", ser.decode("j -, \nuW--wKhjVTYjbwJjtYAZlMfEj7IDO(5}5ÖÄÜ\nLN", None))
);
}
#[test]
@ -350,7 +375,10 @@ fn decode_offset() {
let mut peers = Vec::new();
peers.push(SocketAddr::from_str("1.2.3.4:5678").unwrap());
peers.push(SocketAddr::from_str("6.6.6.6:53").unwrap());
assert_eq!(format!("{:?}", peers), format!("{:?}", ser.decode("Hello World: juWwKhjVTYjbwJjtYAZlMfEj7IDO55LN! End of the World", None)));
assert_eq!(
format!("{:?}", peers),
format!("{:?}", ser.decode("Hello World: juWwKhjVTYjbwJjtYAZlMfEj7IDO55LN! End of the World", None))
);
}
#[test]
@ -360,7 +388,10 @@ fn decode_multiple() {
let mut peers = Vec::new();
peers.push(SocketAddr::from_str("1.2.3.4:5678").unwrap());
peers.push(SocketAddr::from_str("6.6.6.6:53").unwrap());
assert_eq!(format!("{:?}", peers), format!("{:?}", ser.decode("juWwKkBEVBp9SsDiN3BO55LN juWwKtGGPQz1gXIBd68O55LN", None)));
assert_eq!(
format!("{:?}", peers),
format!("{:?}", ser.decode("juWwKkBEVBp9SsDiN3BO55LN juWwKtGGPQz1gXIBd68O55LN", None))
);
}
#[test]

View File

@ -4,22 +4,25 @@
use test::Bencher;
use std::str::FromStr;
use std::net::{UdpSocket, ToSocketAddrs, Ipv4Addr, SocketAddr, SocketAddrV4};
use std::{
net::{Ipv4Addr, SocketAddr, SocketAddrV4, ToSocketAddrs, UdpSocket},
str::FromStr
};
use super::MAGIC;
use super::config::Config;
use super::cloud::GenericCloud;
use super::device::Type;
use super::udpmessage::{Message, encode, decode};
use super::crypto::{Crypto, CryptoMethod};
use super::ethernet::{self, SwitchTable};
use super::types::{Address, Table, Protocol};
use super::ip::Packet;
use super::util::{TimeSource, SystemTimeSource, MockTimeSource};
use super::poll::WaitImpl;
use super::device::TunTapDevice;
use super::net::MockSocket;
use super::{
cloud::GenericCloud,
config::Config,
crypto::{Crypto, CryptoMethod},
device::{TunTapDevice, Type},
ethernet::{self, SwitchTable},
ip::Packet,
net::MockSocket,
poll::WaitImpl,
types::{Address, Protocol, Table},
udpmessage::{decode, encode, Message},
util::{MockTimeSource, SystemTimeSource, TimeSource},
MAGIC
};
#[bench]
fn crypto_chacha20(b: &mut Bencher) {
@ -99,9 +102,7 @@ fn switch_lookup(b: &mut Bencher) {
fn ethernet_parse(b: &mut Bencher) {
let mut data = [0; 1500];
data[5] = 45;
b.iter(|| {
ethernet::Frame::parse(&data).unwrap()
});
b.iter(|| ethernet::Frame::parse(&data).unwrap());
b.bytes = 1400;
}
@ -109,9 +110,7 @@ fn ethernet_parse(b: &mut Bencher) {
fn ipv4_parse(b: &mut Bencher) {
let mut data = [0; 1500];
data[0] = 4 * 16;
b.iter(|| {
Packet::parse(&data).unwrap()
});
b.iter(|| Packet::parse(&data).unwrap());
b.bytes = 1400;
}
@ -119,17 +118,13 @@ fn ipv4_parse(b: &mut Bencher) {
fn ipv6_parse(b: &mut Bencher) {
let mut data = [0; 1500];
data[0] = 6 * 16;
b.iter(|| {
Packet::parse(&data).unwrap()
});
b.iter(|| Packet::parse(&data).unwrap());
b.bytes = 1400;
}
#[bench]
fn now(b: &mut Bencher) {
b.iter(|| {
SystemTimeSource::now()
});
b.iter(|| SystemTimeSource::now());
b.bytes = 1400;
}
@ -139,9 +134,7 @@ fn epoll_wait(b: &mut Bencher) {
let socketv6 = UdpSocket::bind("[::]:0").unwrap();
let device = TunTapDevice::dummy("dummy", "/dev/zero", Type::Dummy).unwrap();
let mut waiter = WaitImpl::testing(&socketv4, &socketv6, &device, 1000).unwrap();
b.iter(|| {
assert!(waiter.next().is_some())
});
b.iter(|| assert!(waiter.next().is_some()));
b.bytes = 1400;
}
@ -152,7 +145,11 @@ fn create_test_node() -> TestNode {
&Config::default(),
TunTapDevice::dummy("dummy", "/dev/null", Type::Tap).unwrap(),
SwitchTable::new(1800, 10),
true, true, vec![], Crypto::None, None
true,
true,
vec![],
Crypto::None,
None
)
}
@ -161,9 +158,7 @@ fn handle_interface_data(b: &mut Bencher) {
let mut node = create_test_node();
let mut data = [0; 1500];
data[105] = 45;
b.iter(|| {
node.handle_interface_data(&mut data, 100, 1400).unwrap()
});
b.iter(|| node.handle_interface_data(&mut data, 100, 1400).unwrap());
b.bytes = 1400;
}
@ -173,9 +168,7 @@ fn handle_net_message(b: &mut Bencher) {
let addr = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 1));
let mut data = [0; 1500];
data[105] = 45;
b.iter(|| {
node.handle_net_message(addr.clone(), Message::Data(&mut data, 0, 1400)).unwrap()
});
b.iter(|| node.handle_net_message(addr.clone(), Message::Data(&mut data, 0, 1400)).unwrap());
b.bytes = 1400;
}
@ -184,8 +177,6 @@ fn udp_send(b: &mut Bencher) {
let sock = UdpSocket::bind("127.0.0.1:0").unwrap();
let data = [0; 1400];
let addr = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 1);
b.iter(|| {
sock.send_to(&data, &addr).unwrap()
});
b.iter(|| sock.send_to(&data, &addr).unwrap());
b.bytes = 1400;
}

View File

@ -2,30 +2,34 @@
// Copyright (C) 2015-2019 Dennis Schwerdel
// This software is licensed under GPL-3 or newer (see LICENSE.md)
use std::net::{SocketAddr, ToSocketAddrs};
use std::collections::HashMap;
use std::io::{self, Write};
use std::fmt;
use std::marker::PhantomData;
use std::hash::BuildHasherDefault;
use std::cmp::min;
use std::fs::{self, File, Permissions};
use std::os::unix::fs::PermissionsExt;
use std::{
cmp::min,
collections::HashMap,
fmt,
fs::{self, File, Permissions},
hash::BuildHasherDefault,
io::{self, Write},
marker::PhantomData,
net::{SocketAddr, ToSocketAddrs},
os::unix::fs::PermissionsExt
};
use fnv::FnvHasher;
use rand::{prelude::*, random, thread_rng};
use super::config::Config;
use super::types::{Table, Protocol, Range, Error, HeaderMagic, NodeId};
use super::device::Device;
use super::udpmessage::{encode, decode, Message};
use super::crypto::Crypto;
use super::port_forwarding::PortForwarding;
use super::util::{TimeSource, Time, Duration, resolve, CtrlC};
use super::poll::{WaitImpl, WaitResult};
use super::traffic::TrafficStats;
use super::beacon::BeaconSerializer;
use super::net::Socket;
use super::{
beacon::BeaconSerializer,
config::Config,
crypto::Crypto,
device::Device,
net::Socket,
poll::{WaitImpl, WaitResult},
port_forwarding::PortForwarding,
traffic::TrafficStats,
types::{Error, HeaderMagic, NodeId, Protocol, Range, Table},
udpmessage::{decode, encode, Message},
util::{resolve, CtrlC, Duration, Time, TimeSource}
};
pub type Hash = BuildHasherDefault<FnvHasher>;
@ -37,7 +41,7 @@ pub const STATS_INTERVAL: Time = 60;
struct PeerData {
timeout: Time,
node_id: NodeId,
alt_addrs: Vec<SocketAddr>,
alt_addrs: Vec<SocketAddr>
}
pub struct PeerList<TS: TimeSource> {
@ -89,7 +93,7 @@ impl<TS: TimeSource> PeerList<TS> {
pub fn is_connected<Addr: ToSocketAddrs + fmt::Debug>(&self, addr: Addr) -> Result<bool, Error> {
for addr in resolve(&addr)? {
if self.contains_addr(&addr) {
return Ok(true);
return Ok(true)
}
}
Ok(false)
@ -231,10 +235,11 @@ pub struct GenericCloud<D: Device, P: Protocol, T: Table, S: Socket, TS: TimeSou
impl<D: Device, P: Protocol, T: Table, S: Socket, TS: TimeSource> GenericCloud<D, P, T, S, TS> {
#[allow(clippy::too_many_arguments)]
pub fn new(config: &Config, device: D, table: T,
learning: bool, broadcast: bool, addresses: Vec<Range>,
crypto: Crypto, port_forwarding: Option<PortForwarding>
) -> Self {
pub fn new(
config: &Config, device: D, table: T, learning: bool, broadcast: bool, addresses: Vec<Range>, crypto: Crypto,
port_forwarding: Option<PortForwarding>
) -> Self
{
let socket4 = match S::listen_v4("0.0.0.0", config.port) {
Ok(socket) => socket,
Err(err) => fail!("Failed to open ipv4 address 0.0.0.0:{}: {}", config.port, err)
@ -299,7 +304,9 @@ impl<D: Device, P: Protocol, T: Table, S: Socket, TS: TimeSource> GenericCloud<D
};
match socket.send(msg_data, *addr) {
Ok(written) if written == msg_data.len() => Ok(()),
Ok(_) => Err(Error::Socket("Sent out truncated packet", io::Error::new(io::ErrorKind::Other, "truncated"))),
Ok(_) => {
Err(Error::Socket("Sent out truncated packet", io::Error::new(io::ErrorKind::Other, "truncated")))
}
Err(e) => Err(Error::Socket("IOError when sending", e))
}?
}
@ -363,7 +370,7 @@ impl<D: Device, P: Protocol, T: Table, S: Socket, TS: TimeSource> GenericCloud<D
address: add,
tries: 0,
timeout: 1,
resolved: resolved,
resolved,
next_resolve: now,
next: now
})
@ -377,7 +384,7 @@ impl<D: Device, P: Protocol, T: Table, S: Socket, TS: TimeSource> GenericCloud<D
fn is_own_address<Addr: ToSocketAddrs + fmt::Debug>(&self, addr: Addr) -> Result<bool, Error> {
for addr in resolve(&addr)? {
if self.own_addresses.contains(&addr) {
return Ok(true);
return Ok(true)
}
}
Ok(false)
@ -525,9 +532,13 @@ impl<D: Device, P: Protocol, T: Table, S: Socket, TS: TimeSource> GenericCloud<D
if let Some(ref path) = self.config.beacon_store {
let peers: Vec<_> = self.own_addresses.choose_multiple(&mut thread_rng(), 3).cloned().collect();
if path.starts_with('|') {
self.beacon_serializer.write_to_cmd(&peers, &path[1..]).map_err(|e| Error::Beacon("Failed to call beacon command", e))?;
self.beacon_serializer
.write_to_cmd(&peers, &path[1..])
.map_err(|e| Error::Beacon("Failed to call beacon command", e))?;
} else {
self.beacon_serializer.write_to_file(&peers, &path).map_err(|e| Error::Beacon("Failed to write beacon to file", e))?;
self.beacon_serializer
.write_to_file(&peers, &path)
.map_err(|e| Error::Beacon("Failed to write beacon to file", e))?;
}
}
Ok(())
@ -538,10 +549,15 @@ impl<D: Device, P: Protocol, T: Table, S: Socket, TS: TimeSource> GenericCloud<D
let peers;
if let Some(ref path) = self.config.beacon_load {
if path.starts_with('|') {
self.beacon_serializer.read_from_cmd(&path[1..], Some(50)).map_err(|e| Error::Beacon("Failed to call beacon command", e))?;
self.beacon_serializer
.read_from_cmd(&path[1..], Some(50))
.map_err(|e| Error::Beacon("Failed to call beacon command", e))?;
return Ok(())
} else {
peers = self.beacon_serializer.read_from_file(&path, Some(50)).map_err(|e| Error::Beacon("Failed to read beacon from file", e))?;
peers = self
.beacon_serializer
.read_from_file(&path, Some(50))
.map_err(|e| Error::Beacon("Failed to read beacon from file", e))?;
}
} else {
return Ok(())
@ -555,7 +571,9 @@ impl<D: Device, P: Protocol, T: Table, S: Socket, TS: TimeSource> GenericCloud<D
/// Calculates, resets and writes out the statistics to a file
fn write_out_stats(&mut self) -> Result<(), io::Error> {
if self.config.stats_file.is_none() { return Ok(()) }
if self.config.stats_file.is_none() {
return Ok(())
}
debug!("Writing out stats");
let mut f = File::create(self.config.stats_file.as_ref().unwrap())?;
self.peers.write_out(&mut f)?;
@ -589,7 +607,8 @@ impl<D: Device, P: Protocol, T: Table, S: Socket, TS: TimeSource> GenericCloud<D
debug!("Read data from interface: src: {}, dst: {}, {} bytes", src, dst, end - start);
self.traffic.count_out_payload(dst, src, end - start);
match self.table.lookup(&dst) {
Some(addr) => { // Peer found for destination
Some(addr) => {
// Peer found for destination
debug!("Found destination for {} => {}", dst, addr);
self.send_msg(addr, &mut Message::Data(payload, start, end))?;
if !self.peers.contains_addr(&addr) {
@ -599,7 +618,7 @@ impl<D: Device, P: Protocol, T: Table, S: Socket, TS: TimeSource> GenericCloud<D
self.table.remove(&dst);
self.connect_sock(addr)?;
}
},
}
None => {
if self.broadcast {
debug!("No destination for {} found, broadcasting", dst);
@ -655,14 +674,14 @@ impl<D: Device, P: Protocol, T: Table, S: Socket, TS: TimeSource> GenericCloud<D
self.traffic.count_in_payload(src, dst, end - start);
if let Err(e) = self.device.write(&mut payload[..end], start) {
error!("Failed to send via device: {}", e);
return Err(e);
return Err(e)
}
if self.learning {
// Learn single address
self.table.learn(src, None, peer);
}
// Not adding peer in this case to increase performance
},
}
Message::Peers(peers) => {
// Connect to sender if not connected
if !self.peers.contains_addr(&peer) {
@ -677,7 +696,7 @@ impl<D: Device, P: Protocol, T: Table, S: Socket, TS: TimeSource> GenericCloud<D
}
// Refresh peer
self.peers.refresh(&peer);
},
}
Message::Init(stage, node_id, ranges) => {
// Avoid connecting to self
if node_id == self.node_id {
@ -702,7 +721,7 @@ impl<D: Device, P: Protocol, T: Table, S: Socket, TS: TimeSource> GenericCloud<D
// Send peers in any case
let peers = self.peers.as_vec();
self.send_msg(peer, &mut Message::Peers(peers))?;
},
}
Message::Close => {
self.peers.remove(&peer);
self.table.remove_all(&peer);
@ -759,7 +778,8 @@ impl<D: Device, P: Protocol, T: Table, S: Socket, TS: TimeSource> GenericCloud<D
/// Also, this method will call `housekeep` every second.
pub fn run(&mut self) {
let ctrlc = CtrlC::new();
let waiter = try_fail!(WaitImpl::new(&self.socket4, &self.socket6, &self.device, 1000), "Failed to setup poll: {}");
let waiter =
try_fail!(WaitImpl::new(&self.socket4, &self.socket6, &self.device, 1000), "Failed to setup poll: {}");
let mut buffer = [0; 64 * 1024];
let mut poll_error = false;
for evt in waiter {
@ -770,8 +790,8 @@ impl<D: Device, P: Protocol, T: Table, S: Socket, TS: TimeSource> GenericCloud<D
}
error!("Poll wait failed: {}, retrying...", err);
poll_error = true;
},
WaitResult::Timeout => {},
}
WaitResult::Timeout => {}
WaitResult::SocketV4 => self.handle_socket_v4_event(&mut buffer),
WaitResult::SocketV6 => self.handle_socket_v6_event(&mut buffer),
WaitResult::Device => self.handle_device_event(&mut buffer)
@ -793,9 +813,9 @@ impl<D: Device, P: Protocol, T: Table, S: Socket, TS: TimeSource> GenericCloud<D
}
#[cfg(test)] use super::device::MockDevice;
#[cfg(test)] use super::net::MockSocket;
#[cfg(test)] use super::util::MockTimeSource;
#[cfg(test)] use super::device::MockDevice;
#[cfg(test)]
impl<P: Protocol, T: Table> GenericCloud<MockDevice, P, T, MockSocket, MockTimeSource> {

View File

@ -2,15 +2,17 @@
// Copyright (C) 2015-2019 Dennis Schwerdel
// This software is licensed under GPL-3 or newer (see LICENSE.md)
use super::{MAGIC, Args};
use super::{Args, MAGIC};
use super::device::Type;
use super::types::{Mode, HeaderMagic};
use super::crypto::CryptoMethod;
use super::util::{Encoder, Duration};
use super::{
crypto::CryptoMethod,
device::Type,
types::{HeaderMagic, Mode},
util::{Duration, Encoder}
};
use std::hash::{Hash, Hasher};
use siphasher::sip::SipHasher24;
use std::hash::{Hash, Hasher};
const HASH_PREFIX: &str = "hash:";
@ -47,13 +49,23 @@ pub struct Config {
impl Default for Config {
fn default() -> Self {
Config {
device_type: Type::Tap, device_name: "vpncloud%d".to_string(), device_path: None,
ifup: None, ifdown: None,
crypto: CryptoMethod::ChaCha20, shared_key: None,
device_type: Type::Tap,
device_name: "vpncloud%d".to_string(),
device_path: None,
ifup: None,
ifdown: None,
crypto: CryptoMethod::ChaCha20,
shared_key: None,
magic: None,
port: 3210, peers: vec![], peer_timeout: 1800, keepalive: None,
beacon_store: None, beacon_load: None, beacon_interval: 3600,
mode: Mode::Normal, dst_timeout: 300,
port: 3210,
peers: vec![],
peer_timeout: 1800,
keepalive: None,
beacon_store: None,
beacon_load: None,
beacon_interval: 3600,
mode: Mode::Normal,
dst_timeout: 300,
subnets: vec![],
port_forwarding: true,
daemonize: false,
@ -265,7 +277,7 @@ pub struct ConfigFile {
pub pid_file: Option<String>,
pub stats_file: Option<String>,
pub user: Option<String>,
pub group: Option<String>,
pub group: Option<String>
}
@ -417,7 +429,11 @@ fn config_merge() {
crypto: CryptoMethod::ChaCha20,
shared_key: Some("anothersecret".to_string()),
port: 3211,
peers: vec!["remote.machine.foo:3210".to_string(), "remote.machine.bar:3210".to_string(), "another:3210".to_string()],
peers: vec![
"remote.machine.foo:3210".to_string(),
"remote.machine.bar:3210".to_string(),
"another:3210".to_string()
],
peer_timeout: 1801,
keepalive: Some(850),
dst_timeout: 301,

View File

@ -4,14 +4,10 @@
use std::num::NonZeroU32;
use ring::aead::*;
use ring::pbkdf2;
use ring::rand::*;
use ring::digest::*;
use ring::{aead::*, pbkdf2, rand::*};
use super::types::Error;
const SALT: &[u8; 32] = b"vpncloudVPNCLOUDvpncl0udVpnCloud";
const HEX_PREFIX: &str = "hex:";
const HASH_PREFIX: &str = "hash:";
@ -25,8 +21,7 @@ pub enum CryptoMethod {
}
pub struct CryptoData {
sealing_key: SealingKey,
opening_key: OpeningKey,
crypto_key: LessSafeKey,
nonce: Vec<u8>,
key: Vec<u8>
}
@ -51,7 +46,6 @@ fn inc_nonce(nonce: &mut [u8]) {
warn!("Nonce overflowed");
}
impl Crypto {
#[inline]
pub fn method(&self) -> u8 {
@ -66,7 +60,7 @@ impl Crypto {
pub fn nonce_bytes(&self) -> usize {
match *self {
Crypto::None => 0,
Crypto::ChaCha20Poly1305(ref data) | Crypto::AES256GCM(ref data) => data.sealing_key.algorithm().nonce_len()
Crypto::ChaCha20Poly1305(ref data) | Crypto::AES256GCM(ref data) => data.crypto_key.algorithm().nonce_len()
}
}
@ -83,7 +77,7 @@ impl Crypto {
pub fn additional_bytes(&self) -> usize {
match *self {
Crypto::None => 0,
Crypto::ChaCha20Poly1305(ref data) | Crypto::AES256GCM(ref data) => data.sealing_key.algorithm().tag_len()
Crypto::ChaCha20Poly1305(ref data) | Crypto::AES256GCM(ref data) => data.crypto_key.algorithm().tag_len()
}
}
@ -102,18 +96,22 @@ impl Crypto {
fail!("Raw secret key must be exactly {} bytes long", algo.key_len());
}
for i in 0..algo.key_len() {
key[i] = try_fail!(u8::from_str_radix(&password[2*i..=2*i+1], 16), "Failed to parse raw secret key: {}");
key[i] = try_fail!(
u8::from_str_radix(&password[2 * i..=2 * i + 1], 16),
"Failed to parse raw secret key: {}"
);
}
} else {
let password = if password.starts_with(HASH_PREFIX) {
&password[HASH_PREFIX.len()..]
} else {
password
};
pbkdf2::derive(&SHA256, NonZeroU32::new(4096).unwrap(), SALT, password.as_bytes(), &mut key);
let password = if password.starts_with(HASH_PREFIX) { &password[HASH_PREFIX.len()..] } else { password };
pbkdf2::derive(
pbkdf2::PBKDF2_HMAC_SHA256,
NonZeroU32::new(4096).unwrap(),
SALT,
password.as_bytes(),
&mut key
);
}
let sealing_key = SealingKey::new(algo, &key[..algo.key_len()]).expect("Failed to create key");
let opening_key = OpeningKey::new(algo, &key[..algo.key_len()]).expect("Failed to create key");
let crypto_key = LessSafeKey::new(UnboundKey::new(algo, &key[..algo.key_len()]).expect("Failed to create key"));
let mut nonce: Vec<u8> = Vec::with_capacity(algo.nonce_len());
for _ in 0..algo.nonce_len() {
nonce.push(0);
@ -122,7 +120,7 @@ impl Crypto {
if SystemRandom::new().fill(&mut nonce[1..]).is_err() {
fail!("Randomizing nonce failed");
}
let data = CryptoData { sealing_key, opening_key, nonce, key };
let data = CryptoData { crypto_key, nonce, key };
match method {
CryptoMethod::ChaCha20 => Crypto::ChaCha20Poly1305(data),
CryptoMethod::AES256 => Crypto::AES256GCM(data)
@ -134,7 +132,7 @@ impl Crypto {
Crypto::None => Ok(buf.len()),
Crypto::ChaCha20Poly1305(ref data) | Crypto::AES256GCM(ref data) => {
let nonce = Nonce::try_assume_unique_for_key(nonce).unwrap();
match open_in_place(&data.opening_key, nonce, Aad::from(header), 0, buf) {
match data.crypto_key.open_in_place(nonce, Aad::from(header), buf) {
Ok(plaintext) => Ok(plaintext.len()),
Err(_) => Err(Error::Crypto("Failed to decrypt"))
}
@ -149,18 +147,19 @@ impl Crypto {
Crypto::ChaCha20Poly1305(ref mut data) | Crypto::AES256GCM(ref mut data) => {
inc_nonce(&mut data.nonce);
assert!(buf.len() - mlen >= tag_len);
let buf = &mut buf[.. mlen + tag_len];
let nonce = Nonce::try_assume_unique_for_key(&data.nonce).unwrap();
let new_len = seal_in_place(&data.sealing_key, nonce, Aad::from(header), buf, tag_len).expect("Failed to encrypt");
let tag = data
.crypto_key
.seal_in_place_separate_tag(nonce, Aad::from(header), &mut buf[..mlen])
.expect("Failed to encrypt");
buf[mlen..mlen + tag_len].copy_from_slice(tag.as_ref());
nonce_bytes.clone_from_slice(&data.nonce);
new_len
mlen + tag_len
}
}
}
}
#[test]
fn encrypt_decrypt_chacha20poly1305() {
let mut sender = Crypto::from_shared_key(CryptoMethod::ChaCha20, "test");

View File

@ -2,15 +2,16 @@
// Copyright (C) 2015-2019 Dennis Schwerdel
// This software is licensed under GPL-3 or newer (see LICENSE.md)
use std::os::unix::io::{AsRawFd, RawFd};
use std::io::{self, Error as IoError, ErrorKind, Read, Write};
use std::fs;
use std::fmt;
use std::collections::VecDeque;
use std::{
collections::VecDeque,
fmt, fs,
io::{self, Error as IoError, ErrorKind, Read, Write},
os::unix::io::{AsRawFd, RawFd}
};
use super::types::Error;
extern {
extern "C" {
fn setup_tap_device(fd: i32, ifname: *mut u8) -> i32;
fn setup_tun_device(fd: i32, ifname: *mut u8) -> i32;
}
@ -78,7 +79,7 @@ pub trait Device: AsRawFd {
pub struct TunTapDevice {
fd: fs::File,
ifname: String,
type_: Type,
type_: Type
}
@ -94,18 +95,16 @@ impl TunTapDevice {
///
/// # Errors
/// This method will return an error when the underlying system call fails. Common cases are:
/// - The special device file `/dev/net/tun` does not exist or is not accessible by the current
/// user.
/// - The special device file `/dev/net/tun` does not exist or is not accessible by the current user.
/// - The interface name is invalid or already in use.
/// - The current user does not have enough permissions to create tun/tap devices (this
/// requires root permissions).
/// - The current user does not have enough permissions to create tun/tap devices (this requires root permissions).
///
/// # Panics
/// This method panics if the interface name is longer than 31 bytes.
pub fn new(ifname: &str, type_: Type, path: Option<&str>) -> io::Result<Self> {
let path = path.unwrap_or_else(|| Self::default_path(type_));
if type_ == Type::Dummy {
return Self::dummy(ifname, path, type_);
return Self::dummy(ifname, path, type_)
}
let fd = fs::OpenOptions::new().read(true).write(true).open(path)?;
// Add trailing \0 to interface name
@ -126,7 +125,7 @@ impl TunTapDevice {
ifname_c.pop();
}
Ok(Self { fd, ifname: String::from_utf8(ifname_c).unwrap(), type_ })
},
}
_ => Err(IoError::last_os_error())
}
}
@ -168,9 +167,15 @@ impl TunTapDevice {
(start, read)
}
#[cfg(any(target_os = "bitrig", target_os = "dragonfly",
target_os = "freebsd", target_os = "ios", target_os = "macos",
target_os = "netbsd", target_os = "openbsd"))]
#[cfg(any(
target_os = "bitrig",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"
))]
#[inline]
fn correct_data_after_read(&mut self, buffer: &mut [u8], start: usize, read: usize) -> (usize, usize) {
if self.type_ == Type::Tun {
@ -188,15 +193,22 @@ impl TunTapDevice {
start
}
#[cfg(any(target_os = "bitrig", target_os = "dragonfly",
target_os = "freebsd", target_os = "ios", target_os = "macos",
target_os = "netbsd", target_os = "openbsd"))]
#[cfg(any(
target_os = "bitrig",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"
))]
#[inline]
fn correct_data_before_write(&mut self, buffer: &mut [u8], start: usize) -> usize {
if self.type_ == Type::Tun {
// BSD-based systems add a 4-byte header containing the Ethertype for TUN
assert!(start >= 4);
match buffer[start] >> 4 { // IP version
match buffer[start] >> 4 {
// IP version
4 => buffer[start - 4..start].copy_from_slice(&[0x00, 0x00, 0x08, 0x00]),
6 => buffer[start - 4..start].copy_from_slice(&[0x00, 0x00, 0x86, 0xdd]),
_ => unreachable!()
@ -230,7 +242,6 @@ impl Device for TunTapDevice {
Err(e) => Err(Error::TunTapDev("Write error", e))
}
}
}
impl AsRawFd for TunTapDevice {

View File

@ -2,17 +2,20 @@
// Copyright (C) 2015-2019 Dennis Schwerdel
// This software is licensed under GPL-3 or newer (see LICENSE.md)
use std::net::SocketAddr;
use std::collections::HashMap;
use std::collections::hash_map::Entry;
use std::hash::BuildHasherDefault;
use std::io::{self, Write};
use std::marker::PhantomData;
use std::{
collections::{hash_map::Entry, HashMap},
hash::BuildHasherDefault,
io::{self, Write},
marker::PhantomData,
net::SocketAddr
};
use fnv::FnvHasher;
use super::types::{Error, Table, Protocol, Address};
use super::util::{TimeSource, Time, Duration};
use super::{
types::{Address, Error, Protocol, Table},
util::{Duration, Time, TimeSource}
};
/// An ethernet frame dissector
///
@ -29,7 +32,7 @@ impl Protocol for Frame {
/// This method will fail when the given data is not a valid ethernet frame.
fn parse(data: &[u8]) -> Result<(Address, Address), Error> {
if data.len() < 14 {
return Err(Error::Parse("Frame is too short"));
return Err(Error::Parse("Frame is too short"))
}
let mut pos = 0;
let dst_data = &data[pos..pos + 6];
@ -39,12 +42,14 @@ impl Protocol for Frame {
if data[pos] == 0x81 && data[pos + 1] == 0x00 {
pos += 2;
if data.len() < pos + 2 {
return Err(Error::Parse("Vlan frame is too short"));
return Err(Error::Parse("Vlan frame is too short"))
}
let mut src = [0; 16];
let mut dst = [0; 16];
src[0] = data[pos]; src[1] = data[pos+1];
dst[0] = data[pos]; dst[1] = data[pos+1];
src[0] = data[pos];
src[1] = data[pos + 1];
dst[0] = data[pos];
dst[1] = data[pos + 1];
src[2..8].copy_from_slice(src_data);
dst[2..8].copy_from_slice(dst_data);
Ok((Address { data: src, len: 8 }, Address { data: dst, len: 8 }))
@ -120,7 +125,7 @@ impl<TS: TimeSource> Table for SwitchTable<TS> {
Entry::Vacant(entry) => {
entry.insert(SwitchTableValue { address: addr, timeout: deadline });
info!("Learned address {} => {}", key, addr);
},
}
Entry::Occupied(mut entry) => {
let mut entry = entry.get_mut();
if entry.timeout + Time::from(self.protection_period) > deadline {
@ -163,9 +168,9 @@ impl<TS: TimeSource> Table for SwitchTable<TS> {
}
#[cfg(test)] use std::str::FromStr;
#[cfg(test)] use std::net::ToSocketAddrs;
#[cfg(test)] use super::util::MockTimeSource;
#[cfg(test)] use std::net::ToSocketAddrs;
#[cfg(test)] use std::str::FromStr;
#[test]
fn decode_frame_without_vlan() {

View File

@ -2,14 +2,16 @@
// Copyright (C) 2015-2019 Dennis Schwerdel
// This software is licensed under GPL-3 or newer (see LICENSE.md)
use std::net::SocketAddr;
use std::collections::{hash_map, HashMap};
use std::hash::BuildHasherDefault;
use std::io::{self, Write};
use std::{
collections::{hash_map, HashMap},
hash::BuildHasherDefault,
io::{self, Write},
net::SocketAddr
};
use fnv::FnvHasher;
use super::types::{Protocol, Error, Table, Address};
use super::types::{Address, Error, Protocol, Table};
/// An IP packet dissector
@ -26,26 +28,26 @@ impl Protocol for Packet {
/// This method will fail when the given data is not a valid ipv4 and ipv6 packet.
fn parse(data: &[u8]) -> Result<(Address, Address), Error> {
if data.is_empty() {
return Err(Error::Parse("Empty header"));
return Err(Error::Parse("Empty header"))
}
let version = data[0] >> 4;
match version {
4 => {
if data.len() < 20 {
return Err(Error::Parse("Truncated IPv4 header"));
return Err(Error::Parse("Truncated IPv4 header"))
}
let src = Address::read_from_fixed(&data[12..], 4)?;
let dst = Address::read_from_fixed(&data[16..], 4)?;
Ok((src, dst))
},
}
6 => {
if data.len() < 40 {
return Err(Error::Parse("Truncated IPv6 header"));
return Err(Error::Parse("Truncated IPv6 header"))
}
let src = Address::read_from_fixed(&data[8..], 16)?;
let dst = Address::read_from_fixed(&data[24..], 16)?;
Ok((src, dst))
},
}
_ => Err(Error::Parse("Invalid version"))
}
}
@ -95,7 +97,9 @@ impl Table for RoutingTable {
// Add the entry to the routing table, creating a new list of the prefix group is empty.
match self.0.entry(group_bytes) {
hash_map::Entry::Occupied(mut entry) => entry.get_mut().push(routing_entry),
hash_map::Entry::Vacant(entry) => { entry.insert(vec![routing_entry]); }
hash_map::Entry::Vacant(entry) => {
entry.insert(vec![routing_entry]);
}
}
}
@ -126,7 +130,7 @@ impl Table for RoutingTable {
match_len += 8;
} else {
match_len += b.leading_zeros();
break;
break
}
}
// If the full prefix matches and the match is longer than the longest prefix
@ -174,8 +178,8 @@ impl Table for RoutingTable {
}
#[cfg(test)] use std::str::FromStr;
#[cfg(test)] use std::net::ToSocketAddrs;
#[cfg(test)] use std::str::FromStr;
#[test]
@ -188,7 +192,10 @@ fn decode_ipv4_packet() {
#[test]
fn decode_ipv6_packet() {
let data = [0x60,0,0,0,0,0,0,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,0,9,8,7,6,5,4,3,2,1,6,5,4,3,2,1];
let data = [
0x60, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1, 6, 5,
4, 3, 2, 1
];
let (src, dst) = Packet::parse(&data).unwrap();
assert_eq!(src, Address { data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6], len: 16 });
assert_eq!(dst, Address { data: [0, 9, 8, 7, 6, 5, 4, 3, 2, 1, 6, 5, 4, 3, 2, 1], len: 16 });
@ -197,7 +204,11 @@ fn decode_ipv6_packet() {
#[test]
fn decode_invalid_packet() {
assert!(Packet::parse(&[0x40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 192, 168, 1, 1, 192, 168, 1, 2]).is_ok());
assert!(Packet::parse(&[0x60,0,0,0,0,0,0,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,0,9,8,7,6,5,4,3,2,1,6,5,4,3,2,1]).is_ok());
assert!(Packet::parse(&[
0x60, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1, 6, 5,
4, 3, 2, 1
])
.is_ok());
// no data
assert!(Packet::parse(&[]).is_err());
// wrong version
@ -205,7 +216,11 @@ fn decode_invalid_packet() {
// truncated ipv4
assert!(Packet::parse(&[0x40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 192, 168, 1, 1, 192, 168, 1]).is_err());
// truncated ipv6
assert!(Packet::parse(&[0x60,0,0,0,0,0,0,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,0,9,8,7,6,5,4,3,2,1,6,5,4,3,2]).is_err());
assert!(Packet::parse(&[
0x60, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1, 6, 5,
4, 3, 2
])
.is_err());
}

View File

@ -10,42 +10,49 @@
#[cfg(test)] extern crate tempfile;
#[cfg(feature = "bench")] extern crate test;
#[macro_use] pub mod util;
#[cfg(test)] #[macro_use] mod tests;
pub mod types;
#[macro_use]
pub mod util;
#[cfg(test)]
#[macro_use]
mod tests;
pub mod beacon;
#[cfg(feature = "bench")] mod benches;
pub mod cloud;
pub mod config;
pub mod crypto;
pub mod udpmessage;
pub mod device;
pub mod ethernet;
pub mod ip;
pub mod cloud;
pub mod device;
pub mod net;
pub mod poll;
pub mod config;
pub mod port_forwarding;
pub mod traffic;
pub mod beacon;
pub mod net;
#[cfg(feature = "bench")] mod benches;
pub mod types;
pub mod udpmessage;
use docopt::Docopt;
use std::sync::Mutex;
use std::str::FromStr;
use std::process::Command;
use std::fs::File;
use std::path::Path;
use std::io::{self, Write};
use std::net::UdpSocket;
use std::{
fs::File,
io::{self, Write},
net::UdpSocket,
path::Path,
process::Command,
str::FromStr,
sync::Mutex
};
use crate::device::{TunTapDevice, Device, Type};
use crate::ethernet::SwitchTable;
use crate::ip::RoutingTable;
use crate::types::{Mode, Range, Protocol, HeaderMagic, Error};
use crate::cloud::GenericCloud;
use crate::crypto::{Crypto, CryptoMethod};
use crate::port_forwarding::PortForwarding;
use crate::util::{Duration, SystemTimeSource};
use crate::config::Config;
use crate::{
cloud::GenericCloud,
config::Config,
crypto::{Crypto, CryptoMethod},
device::{Device, TunTapDevice, Type},
ethernet::SwitchTable,
ip::RoutingTable,
port_forwarding::PortForwarding,
types::{Error, HeaderMagic, Mode, Protocol, Range},
util::{Duration, SystemTimeSource}
};
const VERSION: u8 = 1;
@ -116,7 +123,8 @@ impl log::Log for DualLogger {
let mut file = self.file.lock().expect("Lock poisoned");
if let Some(ref mut file) = *file {
let time = time::strftime("%F %T", &time::now()).expect("Failed to format timestamp");
writeln!(file, "{} - {} - {}", time, record.level(), record.args()).expect("Failed to write to logfile");
writeln!(file, "{} - {} - {}", time, record.level(), record.args())
.expect("Failed to write to logfile");
}
}
}
@ -135,9 +143,11 @@ fn run_script(script: &str, ifname: &str) {
cmd.arg("-c").arg(&script).env("IFNAME", ifname);
debug!("Running script: {:?}", cmd);
match cmd.status() {
Ok(status) => if !status.success() {
Ok(status) => {
if !status.success() {
error!("Script returned with error: {:?}", status.code())
},
}
}
Err(e) => error!("Failed to execute script {:?}: {}", script, e)
}
}
@ -154,17 +164,36 @@ enum AnyCloud<P: Protocol> {
impl<P: Protocol> AnyCloud<P> {
#[allow(unknown_lints, clippy::too_many_arguments)]
fn new(config: &Config, device: TunTapDevice, table: AnyTable,
learning: bool, broadcast: bool, addresses: Vec<Range>,
crypto: Crypto, port_forwarding: Option<PortForwarding>) -> Self {
fn new(
config: &Config, device: TunTapDevice, table: AnyTable, learning: bool, broadcast: bool, addresses: Vec<Range>,
crypto: Crypto, port_forwarding: Option<PortForwarding>
) -> Self
{
match table {
AnyTable::Switch(t) => AnyCloud::Switch(GenericCloud::<TunTapDevice, P, SwitchTable<SystemTimeSource>, UdpSocket, SystemTimeSource>::new(
config, device,t, learning, broadcast, addresses, crypto, port_forwarding
)),
AnyTable::Routing(t) => AnyCloud::Routing(GenericCloud::<TunTapDevice, P, RoutingTable, UdpSocket, SystemTimeSource>::new(
AnyTable::Switch(t) => {
AnyCloud::Switch(GenericCloud::<
TunTapDevice,
P,
SwitchTable<SystemTimeSource>,
UdpSocket,
SystemTimeSource
>::new(
config, device, t, learning, broadcast, addresses, crypto, port_forwarding
))
}
AnyTable::Routing(t) => {
AnyCloud::Routing(GenericCloud::<TunTapDevice, P, RoutingTable, UdpSocket, SystemTimeSource>::new(
config,
device,
t,
learning,
broadcast,
addresses,
crypto,
port_forwarding
))
}
}
}
fn ifname(&self) -> &str {
@ -198,8 +227,12 @@ impl<P: Protocol> AnyCloud<P> {
fn run<P: Protocol>(config: Config) {
let device = try_fail!(TunTapDevice::new(&config.device_name, config.device_type, config.device_path.as_ref().map(|s| s as &str)),
"Failed to open virtual {} interface {}: {}", config.device_type, config.device_name);
let device = try_fail!(
TunTapDevice::new(&config.device_name, config.device_type, config.device_path.as_ref().map(|s| s as &str)),
"Failed to open virtual {} interface {}: {}",
config.device_type,
config.device_name
);
info!("Opened device {}", device.ifname());
let mut ranges = Vec::with_capacity(config.subnets.len());
for s in &config.subnets {
@ -207,11 +240,13 @@ fn run<P: Protocol> (config: Config) {
}
let dst_timeout = config.dst_timeout;
let (learning, broadcasting, table) = match config.mode {
Mode::Normal => match config.device_type {
Mode::Normal => {
match config.device_type {
Type::Tap => (true, true, AnyTable::Switch(SwitchTable::new(dst_timeout, 10))),
Type::Tun => (false, false, AnyTable::Routing(RoutingTable::new())),
Type::Dummy => (false, false, AnyTable::Switch(SwitchTable::new(dst_timeout, 10)))
},
}
}
Mode::Router => (false, false, AnyTable::Routing(RoutingTable::new())),
Mode::Switch => (true, true, AnyTable::Switch(SwitchTable::new(dst_timeout, 10))),
Mode::Hub => (false, true, AnyTable::Switch(SwitchTable::new(dst_timeout, 10)))
@ -220,11 +255,7 @@ fn run<P: Protocol> (config: Config) {
Some(ref key) => Crypto::from_shared_key(config.crypto, key),
None => Crypto::None
};
let port_forwarding = if config.port_forwarding {
PortForwarding::new(config.port)
} else {
None
};
let port_forwarding = if config.port_forwarding { PortForwarding::new(config.port) } else { None };
let mut cloud = AnyCloud::<P>::new(&config, device, table, learning, broadcasting, ranges, crypto, port_forwarding);
if let Some(script) = config.ifup {
run_script(&script, cloud.ifname());
@ -256,24 +287,19 @@ fn run<P: Protocol> (config: Config) {
fn main() {
let args: Args = Docopt::new(USAGE).and_then(|d| d.deserialize()).unwrap_or_else(|e| e.exit());
if args.flag_version {
println!("VpnCloud v{}, protocol version {}",
env!("CARGO_PKG_VERSION"),
VERSION
);
return;
println!("VpnCloud v{}, protocol version {}", env!("CARGO_PKG_VERSION"), VERSION);
return
}
let logger = try_fail!(DualLogger::new(args.flag_log_file.as_ref()), "Failed to open logfile: {}");
log::set_boxed_logger(Box::new(logger)).unwrap();
assert!(!args.flag_verbose || !args.flag_quiet);
log::set_max_level(
if args.flag_verbose {
log::set_max_level(if args.flag_verbose {
log::LevelFilter::Debug
} else if args.flag_quiet {
log::LevelFilter::Error
} else {
log::LevelFilter::Info
}
);
});
let mut config = Config::default();
if let Some(ref file) = args.flag_config {
info!("Reading config file '{}'", file);

View File

@ -1,7 +1,9 @@
use std::os::unix::io::{RawFd, AsRawFd};
use std::net::{UdpSocket, SocketAddr, SocketAddrV4, SocketAddrV6};
use std::io::{self, ErrorKind};
use std::collections::VecDeque;
use std::{
collections::VecDeque,
io::{self, ErrorKind},
net::{SocketAddr, SocketAddrV4, SocketAddrV6, UdpSocket},
os::unix::io::{AsRawFd, RawFd}
};
use net2::UdpBuilder;
@ -16,13 +18,20 @@ pub trait Socket: AsRawFd + Sized {
impl Socket for UdpSocket {
fn listen_v4(host: &str, port: u16) -> Result<Self, io::Error> {
UdpBuilder::new_v4().expect("Failed to obtain ipv4 socket builder")
.reuse_address(true).expect("Failed to set so_reuseaddr").bind((host, port))
UdpBuilder::new_v4()
.expect("Failed to obtain ipv4 socket builder")
.reuse_address(true)
.expect("Failed to set so_reuseaddr")
.bind((host, port))
}
fn listen_v6(host: &str, port: u16) -> Result<Self, io::Error> {
UdpBuilder::new_v6().expect("Failed to obtain ipv4 socket builder")
.only_v6(true).expect("Failed to set only_v6")
.reuse_address(true).expect("Failed to set so_reuseaddr").bind((host, port))
UdpBuilder::new_v6()
.expect("Failed to obtain ipv4 socket builder")
.only_v6(true)
.expect("Failed to set only_v6")
.reuse_address(true)
.expect("Failed to set so_reuseaddr")
.bind((host, port))
}
fn receive(&mut self, buffer: &mut [u8]) -> Result<(usize, SocketAddr), io::Error> {
self.recv_from(buffer)

View File

@ -4,13 +4,11 @@
use libc;
use std::os::unix::io::RawFd;
use std::io;
use crate::device::Device;
use std::{io, os::unix::io::RawFd};
use super::WaitResult;
use crate::device::Type;
use crate::net::Socket;
use crate::{device::Type, net::Socket};
pub struct EpollWait {
poll_fd: RawFd,
@ -18,23 +16,25 @@ pub struct EpollWait {
socketv4: RawFd,
socketv6: RawFd,
device: RawFd,
timeout: u32,
timeout: u32
}
impl EpollWait {
pub fn new<S: Socket>(socketv4: &S, socketv6: &S, device: &Device, timeout: u32) -> io::Result<Self> {
pub fn new<S: Socket>(socketv4: &S, socketv6: &S, device: &dyn Device, timeout: u32) -> io::Result<Self> {
Self::create(socketv4, socketv6, device, timeout, libc::EPOLLIN as u32)
}
pub fn testing<S: Socket>(socketv4: &S, socketv6: &S, device: &Device, timeout: u32) -> io::Result<Self> {
pub fn testing<S: Socket>(socketv4: &S, socketv6: &S, device: &dyn Device, timeout: u32) -> io::Result<Self> {
Self::create(socketv4, socketv6, device, timeout, (libc::EPOLLIN | libc::EPOLLOUT) as u32)
}
fn create<S: Socket>(socketv4: &S, socketv6: &S, device: &Device, timeout: u32, flags: u32) -> io::Result<Self> {
fn create<S: Socket>(
socketv4: &S, socketv6: &S, device: &dyn Device, timeout: u32, flags: u32
) -> io::Result<Self> {
let mut event = libc::epoll_event { u64: 0, events: 0 };
let poll_fd = unsafe { libc::epoll_create(3) };
if poll_fd == -1 {
return Err(io::Error::last_os_error());
return Err(io::Error::last_os_error())
}
let raw_fds = if device.get_type() != Type::Dummy {
vec![socketv4.as_raw_fd(), socketv6.as_raw_fd(), device.as_raw_fd()]
@ -46,7 +46,7 @@ impl EpollWait {
event.events = flags;
let res = unsafe { libc::epoll_ctl(poll_fd, libc::EPOLL_CTL_ADD, fd, &mut event) };
if res == -1 {
return Err(io::Error::last_os_error());
return Err(io::Error::last_os_error())
}
}
Ok(Self {
@ -73,7 +73,8 @@ impl Iterator for EpollWait {
Some(match unsafe { libc::epoll_wait(self.poll_fd, &mut self.event, 1, self.timeout as i32) } {
-1 => WaitResult::Error(io::Error::last_os_error()),
0 => WaitResult::Timeout,
1 => if self.event.u64 == self.socketv4 as u64 {
1 => {
if self.event.u64 == self.socketv4 as u64 {
WaitResult::SocketV4
} else if self.event.u64 == self.socketv6 as u64 {
WaitResult::SocketV6
@ -81,9 +82,9 @@ impl Iterator for EpollWait {
WaitResult::Device
} else {
unreachable!()
},
}
}
_ => unreachable!()
})
}
}

View File

@ -2,8 +2,10 @@
// Copyright (C) 2015-2019 Dennis Schwerdel
// This software is licensed under GPL-3 or newer (see LICENSE.md)
use std::net::{SocketAddrV4, UdpSocket, SocketAddr};
use std::io;
use std::{
io,
net::{SocketAddr, SocketAddrV4, UdpSocket}
};
use igd::*;
@ -17,17 +19,18 @@ pub struct PortForwarding {
pub internal_addr: SocketAddrV4,
pub external_addr: SocketAddrV4,
pub gateway: Gateway,
pub next_extension: Option<Time>,
pub next_extension: Option<Time>
}
impl PortForwarding {
pub fn new(port: u16) -> Option<Self> {
// Get the gateway
let gateway = match search_gateway() {
let gateway = match search_gateway(Default::default()) {
Ok(gateway) => gateway,
Err(err) => {
if let SearchError::IoError(ref err) = err {
if err.kind() == io::ErrorKind::WouldBlock { // Why this code?
if err.kind() == io::ErrorKind::WouldBlock {
// Why this code?
warn!("Port-forwarding: no router found");
return None
}
@ -60,46 +63,49 @@ impl PortForwarding {
// - If the port is used, request any port
// - If timeout is denied, try permanent forwarding
info!("Port-forwarding: external IP is {}", external_ip);
let (external_addr, timeout) = match gateway.add_port(PortMappingProtocol::UDP, internal_addr.port(), internal_addr, LEASE_TIME, DESCRIPTION) {
let (external_addr, timeout) = match gateway.add_port(
PortMappingProtocol::UDP,
internal_addr.port(),
internal_addr,
LEASE_TIME,
DESCRIPTION
) {
Ok(()) => (SocketAddrV4::new(external_ip, internal_addr.port()), LEASE_TIME),
Err(AddPortError::PortInUse) => match gateway.add_any_port(PortMappingProtocol::UDP, internal_addr, LEASE_TIME, DESCRIPTION) {
Err(AddPortError::PortInUse) => {
match gateway.add_any_port(PortMappingProtocol::UDP, internal_addr, LEASE_TIME, DESCRIPTION) {
Ok(port) => (SocketAddrV4::new(external_ip, port), LEASE_TIME),
Err(AddAnyPortError::OnlyPermanentLeasesSupported) => match gateway.add_any_port(PortMappingProtocol::UDP, internal_addr, 0, DESCRIPTION) {
Err(AddAnyPortError::OnlyPermanentLeasesSupported) => {
match gateway.add_any_port(PortMappingProtocol::UDP, internal_addr, 0, DESCRIPTION) {
Ok(port) => (SocketAddrV4::new(external_ip, port), 0),
Err(err) => {
error!("Port-forwarding: failed to activate port forwarding: {}", err);
return None
}
},
}
}
Err(err) => {
error!("Port-forwarding: failed to activate port forwarding: {}", err);
return None
}
},
Err(AddPortError::OnlyPermanentLeasesSupported) => match gateway.add_port(PortMappingProtocol::UDP, internal_addr.port(), internal_addr, 0, DESCRIPTION) {
}
}
Err(AddPortError::OnlyPermanentLeasesSupported) => {
match gateway.add_port(PortMappingProtocol::UDP, internal_addr.port(), internal_addr, 0, DESCRIPTION) {
Ok(()) => (SocketAddrV4::new(external_ip, internal_addr.port()), 0),
Err(err) => {
error!("Port-forwarding: failed to activate port forwarding: {}", err);
return None
}
},
}
}
Err(err) => {
error!("Port-forwarding: failed to activate port forwarding: {}", err);
return None
}
};
info!("Port-forwarding: sucessfully activated port forward on {}, timeout: {}", external_addr, timeout);
let next_extension = if timeout > 0 {
Some(SystemTimeSource::now() + Time::from(timeout) - 60)
} else {
None
};
Some(PortForwarding {
internal_addr,
external_addr,
gateway,
next_extension
})
let next_extension = if timeout > 0 { Some(SystemTimeSource::now() + Time::from(timeout) - 60) } else { None };
Some(PortForwarding { internal_addr, external_addr, gateway, next_extension })
}
pub fn check_extend(&mut self) {
@ -110,7 +116,13 @@ impl PortForwarding {
} else {
return
}
match self.gateway.add_port(PortMappingProtocol::UDP, self.external_addr.port(), self.internal_addr, LEASE_TIME, DESCRIPTION) {
match self.gateway.add_port(
PortMappingProtocol::UDP,
self.external_addr.port(),
self.internal_addr,
LEASE_TIME,
DESCRIPTION
) {
Ok(()) => debug!("Port-forwarding: extended port forwarding"),
Err(err) => error!("Port-forwarding: failed to extend port forwarding: {}", err)
};

View File

@ -2,23 +2,26 @@
// Copyright (C) 2015-2019 Dennis Schwerdel
// This software is licensed under GPL-3 or newer (see LICENSE.md)
#[macro_use] mod helper;
mod peers;
#[macro_use]
mod helper;
mod payload;
mod peers;
pub use std::net::SocketAddr;
use std::sync::atomic::{Ordering, AtomicUsize};
use std::sync::atomic::{AtomicUsize, Ordering};
pub use super::ethernet::{self, SwitchTable};
pub use super::util::MockTimeSource;
pub use super::net::MockSocket;
pub use super::device::MockDevice;
pub use super::udpmessage::Message;
pub use super::config::Config;
pub use super::crypto::Crypto;
pub use super::cloud::GenericCloud;
pub use super::types::{Protocol, Table, Range};
pub use super::ip::{self, RoutingTable};
pub use super::{
cloud::GenericCloud,
config::Config,
crypto::Crypto,
device::MockDevice,
ethernet::{self, SwitchTable},
ip::{self, RoutingTable},
net::MockSocket,
types::{Protocol, Range, Table},
udpmessage::Message,
util::MockTimeSource
};
type TestNode<P, T> = GenericCloud<MockDevice, P, T, MockSocket, MockTimeSource>;
@ -38,12 +41,7 @@ fn create_tap_node() -> TapTestNode {
fn create_tap_node_with_config(mut config: Config) -> TapTestNode {
config.port = NEXT_PORT.with(|p| p.fetch_add(1, Ordering::Relaxed)) as u16;
TestNode::new(
&config,
MockDevice::new(),
SwitchTable::new(1800, 10),
true, true, vec![], Crypto::None, None
)
TestNode::new(&config, MockDevice::new(), SwitchTable::new(1800, 10), true, true, vec![], Crypto::None, None)
}
#[allow(dead_code)]
@ -52,7 +50,11 @@ fn create_tun_node(addresses: Vec<Range>) -> TunTestNode {
&Config { port: NEXT_PORT.with(|p| p.fetch_add(1, Ordering::Relaxed)) as u16, ..Config::default() },
MockDevice::new(),
RoutingTable::new(),
false, false, addresses, Crypto::None, None
false,
false,
addresses,
Crypto::None,
None
)
}

View File

@ -97,9 +97,11 @@ fn cross_connect() {
fn connect_via_beacons() {
MockTimeSource::set_time(0);
let beacon_path = "target/.vpncloud_test";
let mut node1 = create_tap_node_with_config(Config { beacon_store: Some(beacon_path.to_string()), ..Config::default()});
let mut node1 =
create_tap_node_with_config(Config { beacon_store: Some(beacon_path.to_string()), ..Config::default() });
let node1_addr = node1.address().unwrap().0;
let mut node2 = create_tap_node_with_config(Config { beacon_load: Some(beacon_path.to_string()), ..Config::default()});
let mut node2 =
create_tap_node_with_config(Config { beacon_load: Some(beacon_path.to_string()), ..Config::default() });
let node2_addr = addr!("2.2.2.2:2222");
assert!(!node1.peers().contains_node(&node2.node_id()));
@ -164,14 +166,14 @@ fn lost_init1() {
simulate!(node1 => node1_addr, node2 => node2_addr);
assert_connected!(node1, node2);
}
#[test]
fn wrong_magic() {
let mut node1 = create_tap_node();
let node1_addr = addr!("1.2.3.4:5678");
let mut node2 = create_tap_node_with_config(Config { magic: Some("hash:different".to_string()), ..Config::default()});
let mut node2 =
create_tap_node_with_config(Config { magic: Some("hash:different".to_string()), ..Config::default() });
let node2_addr = addr!("2.3.4.5:6789");
node1.connect("2.3.4.5:6789").unwrap();
@ -202,4 +204,3 @@ fn remove_dead_peers() {
fn update_primary_address() {
// TODO
}

View File

@ -2,13 +2,13 @@
// Copyright (C) 2018-2019 Dennis Schwerdel
// This software is licensed under GPL-3 or newer (see LICENSE.md)
use std::net::SocketAddr;
use std::collections::HashMap;
use std::io::{self, Write};
use std::{
collections::HashMap,
io::{self, Write},
net::SocketAddr
};
use super::types::Address;
use super::cloud::Hash;
use super::util::Bytes;
use super::{cloud::Hash, types::Address, util::Bytes};
#[derive(Default)]
@ -116,7 +116,14 @@ impl TrafficStats {
let mut payload: Vec<_> = self.get_payload_traffic().collect();
payload.sort_unstable_by_key(|(_, data)| (data.out_bytes + data.in_bytes));
for ((remote, local), data) in payload.iter().rev() {
writeln!(out, " - {} <-> {}: in={}/s, out={}/s", remote, local, Bytes(data.in_bytes/60), Bytes(data.out_bytes/60))?;
writeln!(
out,
" - {} <-> {}: in={}/s, out={}/s",
remote,
local,
Bytes(data.in_bytes / 60),
Bytes(data.out_bytes / 60)
)?;
}
Ok(())
}

View File

@ -2,11 +2,13 @@
// Copyright (C) 2015-2019 Dennis Schwerdel
// This software is licensed under GPL-3 or newer (see LICENSE.md)
use std::net::{SocketAddr, Ipv4Addr, Ipv6Addr};
use std::fmt;
use std::str::FromStr;
use std::hash::{Hash, Hasher};
use std::io::{self, Write};
use std::{
fmt,
hash::{Hash, Hasher},
io::{self, Write},
net::{Ipv4Addr, Ipv6Addr, SocketAddr},
str::FromStr
};
use super::util::{bytes_to_hex, Encoder};
@ -26,7 +28,7 @@ impl Address {
#[inline]
pub fn read_from(data: &[u8]) -> Result<(Address, usize), Error> {
if data.is_empty() {
return Err(Error::Parse("Address too short"));
return Err(Error::Parse("Address too short"))
}
let len = data[0] as usize;
let addr = Address::read_from_fixed(&data[1..], len)?;
@ -36,10 +38,10 @@ impl Address {
#[inline]
pub fn read_from_fixed(data: &[u8], len: usize) -> Result<Address, Error> {
if len > 16 {
return Err(Error::Parse("Invalid address, too long"));
return Err(Error::Parse("Invalid address, too long"))
}
if data.len() < len {
return Err(Error::Parse("Address too short"));
return Err(Error::Parse("Address too short"))
}
let mut bytes = [0; 16];
bytes[0..len].copy_from_slice(&data[0..len]);
@ -107,7 +109,7 @@ impl FromStr for Address {
let ip = addr.octets();
let mut res = [0; 16];
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) {
let segments = addr.segments();
@ -115,7 +117,7 @@ impl FromStr for Address {
for i in 0..8 {
Encoder::write_u16(segments[i], &mut res[2 * i..]);
}
return Ok(Address{data: res, len: 16});
return Ok(Address { data: res, len: 16 })
}
let parts: Vec<&str> = text.split(':').collect();
if parts.len() == 6 {
@ -123,7 +125,7 @@ impl FromStr for Address {
for i in 0..6 {
bytes[i] = u8::from_str_radix(parts[i], 16).map_err(|_| Error::Parse("Failed to parse mac"))?;
}
return Ok(Address{data: bytes, len: 6});
return Ok(Address { data: bytes, len: 6 })
}
Err(Error::Parse("Failed to parse address"))
}
@ -141,7 +143,7 @@ impl Range {
pub fn read_from(data: &[u8]) -> Result<(Range, usize), Error> {
let (address, read) = Address::read_from(data)?;
if data.len() < read + 1 {
return Err(Error::Parse("Range too short"));
return Err(Error::Parse("Range too short"))
}
let prefix_len = data[read];
Ok((Range { base: address, prefix_len }, read + 1))
@ -164,8 +166,7 @@ impl FromStr for Range {
Some(pos) => pos,
None => return Err(Error::Parse("Invalid range format"))
};
let prefix_len = u8::from_str(&text[pos+1..])
.map_err(|_| Error::Parse("Failed to parse prefix length"))?;
let prefix_len = u8::from_str(&text[pos + 1..]).map_err(|_| Error::Parse("Failed to parse prefix length"))?;
let base = Address::from_str(&text[..pos])?;
Ok(Range { base, prefix_len })
}
@ -201,7 +202,7 @@ impl fmt::Display for Mode {
Mode::Normal => write!(formatter, "normal"),
Mode::Hub => write!(formatter, "hub"),
Mode::Switch => write!(formatter, "switch"),
Mode::Router => write!(formatter, "router"),
Mode::Router => write!(formatter, "router")
}
}
}
@ -250,8 +251,14 @@ impl fmt::Display for Error {
fn address_parse_fmt() {
assert_eq!(format!("{}", Address::from_str("120.45.22.5").unwrap()), "120.45.22.5");
assert_eq!(format!("{}", Address::from_str("78:2d:16:05:01:02").unwrap()), "78:2d:16:05:01:02");
assert_eq!(format!("{}", Address{data: [3,56,120,45,22,5,1,2,0,0,0,0,0,0,0,0], len: 8}), "vlan824/78:2d:16:05:01:02");
assert_eq!(format!("{}", Address::from_str("0001:0203:0405:0607:0809:0a0b:0c0d:0e0f").unwrap()), "0001:0203:0405:0607:0809:0a0b:0c0d:0e0f");
assert_eq!(
format!("{}", Address { data: [3, 56, 120, 45, 22, 5, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0], len: 8 }),
"vlan824/78:2d:16:05:01:02"
);
assert_eq!(
format!("{}", Address::from_str("0001:0203:0405:0607:0809:0a0b:0c0d:0e0f").unwrap()),
"0001:0203:0405:0607:0809:0a0b:0c0d:0e0f"
);
assert_eq!(format!("{:?}", Address { data: [1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], len: 2 }), "0102");
assert!(Address::from_str("").is_err()); // Failed to parse address
}
@ -278,16 +285,29 @@ fn address_decode_encode() {
#[test]
fn address_eq() {
assert_eq!(Address::read_from_fixed(&[1,2,3,4], 4).unwrap(), Address::read_from_fixed(&[1,2,3,4], 4).unwrap());
assert_ne!(Address::read_from_fixed(&[1,2,3,4], 4).unwrap(), Address::read_from_fixed(&[1,2,3,5], 4).unwrap());
assert_eq!(Address::read_from_fixed(&[1,2,3,4], 3).unwrap(), Address::read_from_fixed(&[1,2,3,5], 3).unwrap());
assert_ne!(Address::read_from_fixed(&[1,2,3,4], 3).unwrap(), Address::read_from_fixed(&[1,2,3,4], 4).unwrap());
assert_eq!(
Address::read_from_fixed(&[1, 2, 3, 4], 4).unwrap(),
Address::read_from_fixed(&[1, 2, 3, 4], 4).unwrap()
);
assert_ne!(
Address::read_from_fixed(&[1, 2, 3, 4], 4).unwrap(),
Address::read_from_fixed(&[1, 2, 3, 5], 4).unwrap()
);
assert_eq!(
Address::read_from_fixed(&[1, 2, 3, 4], 3).unwrap(),
Address::read_from_fixed(&[1, 2, 3, 5], 3).unwrap()
);
assert_ne!(
Address::read_from_fixed(&[1, 2, 3, 4], 3).unwrap(),
Address::read_from_fixed(&[1, 2, 3, 4], 4).unwrap()
);
}
#[test]
fn address_range_decode_encode() {
let mut buf = [0; 32];
let range = Range{base: Address{data: [0,1,2,3,0,0,0,0,0,0,0,0,0,0,0,0], len: 4}, prefix_len: 24};
let range =
Range { base: Address { data: [0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], len: 4 }, prefix_len: 24 };
assert_eq!(range.write_to(&mut buf), 6);
assert_eq!(&buf[0..6], &[4, 0, 1, 2, 3, 24]);
assert_eq!((range, 6), Range::read_from(&buf).unwrap());

View File

@ -2,12 +2,16 @@
// Copyright (C) 2015-2019 Dennis Schwerdel
// This software is licensed under GPL-3 or newer (see LICENSE.md)
use std::fmt;
use std::net::{SocketAddr, SocketAddrV4, Ipv4Addr, SocketAddrV6, Ipv6Addr};
use std::{
fmt,
net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6}
};
use super::types::{NodeId, HeaderMagic, Error, Range, NODE_ID_BYTES};
use super::util::{bytes_to_hex, Encoder};
use super::crypto::Crypto;
use super::{
crypto::Crypto,
types::{Error, HeaderMagic, NodeId, Range, NODE_ID_BYTES},
util::{bytes_to_hex, Encoder}
};
#[derive(Clone, Copy, Default)]
#[repr(packed)]
@ -27,7 +31,7 @@ impl TopHeader {
pub fn read_from(data: &[u8]) -> Result<(TopHeader, usize), Error> {
if data.len() < TopHeader::size() {
return Err(Error::Parse("Empty message"));
return Err(Error::Parse("Empty message"))
}
let mut header = TopHeader::default();
header.magic.copy_from_slice(&data[0..4]);
@ -52,7 +56,7 @@ pub enum Message<'a> {
Data(&'a mut [u8], usize, usize), // data, start, end
Peers(Vec<SocketAddr>), // peers
Init(u8, NodeId, Vec<Range>), // step, node_id, ranges
Close,
Close
}
impl<'a> Message<'a> {
@ -81,9 +85,11 @@ impl<'a> fmt::Debug for Message<'a> {
write!(formatter, "{}", p)?;
}
write!(formatter, "]")
},
Message::Init(stage, ref node_id, ref peers) => write!(formatter, "Init(stage={}, node_id={}, {:?})", stage, bytes_to_hex(node_id), peers),
Message::Close => write!(formatter, "Close"),
}
Message::Init(stage, ref node_id, ref peers) => {
write!(formatter, "Init(stage={}, node_id={}, {:?})", stage, bytes_to_hex(node_id), peers)
}
Message::Close => write!(formatter, "Close")
}
}
}
@ -93,15 +99,15 @@ pub fn decode<'a>(data: &'a mut [u8], magic: HeaderMagic, crypto: &Crypto) -> Re
let mut end = data.len();
let (header, mut pos) = TopHeader::read_from(&data[..end])?;
if header.magic != magic {
return Err(Error::WrongHeaderMagic(header.magic));
return Err(Error::WrongHeaderMagic(header.magic))
}
if header.crypto_method != crypto.method() {
return Err(Error::Crypto("Wrong crypto method"));
return Err(Error::Crypto("Wrong crypto method"))
}
if crypto.method() > 0 {
let len = crypto.nonce_bytes();
if end < pos + len {
return Err(Error::Parse("Truncated crypto header"));
return Err(Error::Parse("Truncated crypto header"))
}
{
let (before, after) = data.split_at_mut(pos);
@ -115,14 +121,14 @@ pub fn decode<'a>(data: &'a mut [u8], magic: HeaderMagic, crypto: &Crypto) -> Re
0 => Message::Data(data, pos, end),
1 => {
if end < pos + 1 {
return Err(Error::Parse("Missing IPv4 count"));
return Err(Error::Parse("Missing IPv4 count"))
}
let mut peers = Vec::new();
let count = data[pos];
pos += 1;
let len = count as usize * 6;
if end < pos + len {
return Err(Error::Parse("IPv4 peer data too short"));
return Err(Error::Parse("IPv4 peer data too short"))
}
for _ in 0..count {
let ip = &data[pos..];
@ -134,13 +140,13 @@ pub fn decode<'a>(data: &'a mut [u8], magic: HeaderMagic, crypto: &Crypto) -> Re
peers.push(addr);
}
if end < pos + 1 {
return Err(Error::Parse("Missing IPv6 count"));
return Err(Error::Parse("Missing IPv6 count"))
}
let count = data[pos];
pos += 1;
let len = count as usize * 18;
if end < pos + len {
return Err(Error::Parse("IPv6 peer data too short"));
return Err(Error::Parse("IPv6 peer data too short"))
}
for _ in 0..count {
let mut ip = [0u16; 8];
@ -150,15 +156,19 @@ pub fn decode<'a>(data: &'a mut [u8], magic: HeaderMagic, crypto: &Crypto) -> Re
}
let port = Encoder::read_u16(&data[pos..]);
pos += 2;
let addr = SocketAddr::V6(SocketAddrV6::new(Ipv6Addr::new(ip[0], ip[1], ip[2],
ip[3], ip[4], ip[5], ip[6], ip[7]), port, 0, 0));
let addr = SocketAddr::V6(SocketAddrV6::new(
Ipv6Addr::new(ip[0], ip[1], ip[2], ip[3], ip[4], ip[5], ip[6], ip[7]),
port,
0,
0
));
peers.push(addr);
}
Message::Peers(peers)
},
}
2 => {
if end < pos + 2 + NODE_ID_BYTES {
return Err(Error::Parse("Init data too short"));
return Err(Error::Parse("Init data too short"))
}
let stage = data[pos];
pos += 1;
@ -174,7 +184,7 @@ pub fn decode<'a>(data: &'a mut [u8], magic: HeaderMagic, crypto: &Crypto) -> Re
addrs.push(range);
}
Message::Init(stage, node_id, addrs)
},
}
3 => Message::Close,
_ => return Err(Error::Parse("Unknown message type"))
};
@ -182,7 +192,9 @@ pub fn decode<'a>(data: &'a mut [u8], magic: HeaderMagic, crypto: &Crypto) -> Re
}
#[allow(unknown_lints, clippy::needless_range_loop)]
pub fn encode<'a>(msg: &'a mut Message, mut buf: &'a mut [u8], magic: HeaderMagic, crypto: &mut Crypto) -> &'a mut [u8] {
pub fn encode<'a>(
msg: &'a mut Message, mut buf: &'a mut [u8], magic: HeaderMagic, crypto: &mut Crypto
) -> &'a mut [u8] {
let header_type = match msg {
Message::Data(_, _, _) => 0,
Message::Peers(_) => 1,
@ -196,7 +208,7 @@ pub fn encode<'a>(msg: &'a mut Message, mut buf: &'a mut [u8], magic: HeaderMagi
buf = data;
start = data_start;
end = data_end;
},
}
Message::Peers(ref peers) => {
let mut v4addrs = Vec::new();
let mut v6addrs = Vec::new();
@ -218,7 +230,7 @@ pub fn encode<'a>(msg: &'a mut Message, mut buf: &'a mut [u8], magic: HeaderMagi
pos += 4;
Encoder::write_u16(addr.port(), &mut buf[pos..]);
pos += 2;
};
}
buf[pos] = v6addrs.len() as u8;
pos += 1;
for addr in v6addrs {
@ -229,9 +241,9 @@ pub fn encode<'a>(msg: &'a mut Message, mut buf: &'a mut [u8], magic: HeaderMagi
}
Encoder::write_u16(addr.port(), &mut buf[pos..]);
pos += 2;
};
}
end = pos;
},
}
Message::Init(stage, ref node_id, ref ranges) => {
let mut pos = start;
assert!(buf.len() >= pos + 2 + NODE_ID_BYTES);
@ -246,9 +258,8 @@ pub fn encode<'a>(msg: &'a mut Message, mut buf: &'a mut [u8], magic: HeaderMagi
pos += range.write_to(&mut buf[pos..]);
}
end = pos;
},
Message::Close => {
}
Message::Close => {}
}
assert!(start >= 64);
assert!(buf.len() >= end + 64);
@ -271,41 +282,56 @@ pub fn encode<'a>(msg: &'a mut Message, mut buf: &'a mut [u8], magic: HeaderMagi
&mut buf[start..end]
}
impl<'a> PartialEq for Message<'a> {
fn eq(&self, other: &Message) -> bool {
match *self {
Message::Data(ref data1, start1, end1) => if let Message::Data(ref data2, start2, end2) = *other {
Message::Data(ref data1, start1, end1) => {
if let Message::Data(ref data2, start2, end2) = *other {
data1[start1..end1] == data2[start2..end2]
} else { false },
Message::Peers(ref peers1) => if let Message::Peers(ref peers2) = *other {
} else {
false
}
}
Message::Peers(ref peers1) => {
if let Message::Peers(ref peers2) = *other {
peers1 == peers2
} else { false },
Message::Init(step1, node_id1, ref ranges1) => if let Message::Init(step2, node_id2, ref ranges2) = *other {
} else {
false
}
}
Message::Init(step1, node_id1, ref ranges1) => {
if let Message::Init(step2, node_id2, ref ranges2) = *other {
step1 == step2 && node_id1 == node_id2 && ranges1 == ranges2
} else { false },
Message::Close => if let Message::Close = *other {
} else {
false
}
}
Message::Close => {
if let Message::Close = *other {
true
} else { false }
} else {
false
}
}
}
}
}
#[cfg(test)] use std::str::FromStr;
#[cfg(test)] use super::MAGIC;
#[cfg(test)] use super::types::Address;
#[cfg(test)] use super::crypto::CryptoMethod;
#[cfg(test)] use super::types::Address;
#[cfg(test)] use super::MAGIC;
#[cfg(test)] use std::str::FromStr;
#[test]
#[allow(unused_assignments)]
fn udpmessage_packet() {
let mut crypto = Crypto::None;
let mut payload = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,2,3,4,5,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
let mut payload = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
];
let mut msg = Message::Data(&mut payload, 64, 69);
let mut buf = [0; 1024];
let mut len = 0;
@ -326,11 +352,12 @@ fn udpmessage_packet() {
#[allow(unused_assignments)]
fn udpmessage_encrypted() {
let mut crypto = Crypto::from_shared_key(CryptoMethod::ChaCha20, "test");
let mut payload = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,2,3,4,5,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
let mut payload = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
];
let mut orig_payload = [0; 133];
for i in 0..payload.len() {
orig_payload[i] = payload[i];
@ -356,9 +383,15 @@ fn udpmessage_encrypted() {
fn udpmessage_peers() {
use std::str::FromStr;
let mut crypto = Crypto::None;
let mut msg = Message::Peers(vec![SocketAddr::from_str("1.2.3.4:123").unwrap(), SocketAddr::from_str("5.6.7.8:12345").unwrap(), SocketAddr::from_str("[0001:0203:0405:0607:0809:0a0b:0c0d:0e0f]:6789").unwrap()]);
let mut should = [118,112,110,1,0,0,0,1,2,1,2,3,4,0,123,5,6,7,8,48,57,1,0,1,2,3,4,5,6,7,
8,9,10,11,12,13,14,15,26,133];
let mut msg = Message::Peers(vec![
SocketAddr::from_str("1.2.3.4:123").unwrap(),
SocketAddr::from_str("5.6.7.8:12345").unwrap(),
SocketAddr::from_str("[0001:0203:0405:0607:0809:0a0b:0c0d:0e0f]:6789").unwrap(),
]);
let mut should = [
118, 112, 110, 1, 0, 0, 0, 1, 2, 1, 2, 3, 4, 0, 123, 5, 6, 7, 8, 48, 57, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 26, 133
];
{
let mut buf = [0; 1024];
let res = encode(&mut msg, &mut buf[..], MAGIC, &mut crypto);
@ -383,11 +416,16 @@ fn udpmessage_peers() {
fn udpmessage_init() {
use super::types::Address;
let mut crypto = Crypto::None;
let addrs = vec![Range{base: Address{data: [0,1,2,3,0,0,0,0,0,0,0,0,0,0,0,0], len: 4}, prefix_len: 24},
Range{base: Address{data: [0,1,2,3,4,5,0,0,0,0,0,0,0,0,0,0], len: 6}, prefix_len: 16}];
let addrs = vec![
Range { base: Address { data: [0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], len: 4 }, prefix_len: 24 },
Range { base: Address { data: [0, 1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], len: 6 }, prefix_len: 16 },
];
let node_id = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
let mut msg = Message::Init(0, node_id, addrs);
let mut should = [118,112,110,1,0,0,0,2,0,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,2,4,0,1,2,3,24,6,0,1,2,3,4,5,16];
let mut should = [
118, 112, 110, 1, 0, 0, 0, 2, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 2, 4, 0, 1, 2, 3, 24, 6,
0, 1, 2, 3, 4, 5, 16
];
{
let mut buf = [0; 1024];
let res = encode(&mut msg, &mut buf[..], MAGIC, &mut crypto);
@ -438,17 +476,35 @@ fn udpmessage_invalid_crypto() {
assert!(decode(&mut [0x76, 0x70, 0x6e, 1, 1, 0, 0, 0], MAGIC, &mut crypto).is_err());
}
#[test]
fn message_fmt() {
assert_eq!(format!("{:?}", Message::Data(&mut [1, 2, 3, 4, 5], 0, 5)), "Data(5 bytes)");
assert_eq!(format!("{:?}", Message::Peers(vec![SocketAddr::from_str("1.2.3.4:123").unwrap(),
assert_eq!(
format!(
"{:?}",
Message::Peers(vec![
SocketAddr::from_str("1.2.3.4:123").unwrap(),
SocketAddr::from_str("5.6.7.8:12345").unwrap(),
SocketAddr::from_str("[0001:0203:0405:0607:0809:0a0b:0c0d:0e0f]:6789").unwrap()])),
"Peers [1.2.3.4:123, 5.6.7.8:12345, [1:203:405:607:809:a0b:c0d:e0f]:6789]");
assert_eq!(format!("{:?}", Message::Init(0, [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15], vec![
Range{base: Address{data: [0,1,2,3,0,0,0,0,0,0,0,0,0,0,0,0], len: 4}, prefix_len: 24},
Range{base: Address{data: [0,1,2,3,4,5,0,0,0,0,0,0,0,0,0,0], len: 6}, prefix_len: 16}
])), "Init(stage=0, node_id=000102030405060708090a0b0c0d0e0f, [0.1.2.3/24, 00:01:02:03:04:05/16])");
SocketAddr::from_str("[0001:0203:0405:0607:0809:0a0b:0c0d:0e0f]:6789").unwrap()
])
),
"Peers [1.2.3.4:123, 5.6.7.8:12345, [1:203:405:607:809:a0b:c0d:e0f]:6789]"
);
assert_eq!(
format!(
"{:?}",
Message::Init(0, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], vec![
Range {
base: Address { data: [0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], len: 4 },
prefix_len: 24
},
Range {
base: Address { data: [0, 1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], len: 6 },
prefix_len: 16
}
])
),
"Init(stage=0, node_id=000102030405060708090a0b0c0d0e0f, [0.1.2.3/24, 00:01:02:03:04:05/16])"
);
assert_eq!(format!("{:?}", Message::Close), "Close");
}

View File

@ -2,17 +2,17 @@
// Copyright (C) 2015-2019 Dennis Schwerdel
// This software is licensed under GPL-3 or newer (see LICENSE.md)
use std::net::{SocketAddr, ToSocketAddrs};
use std::fmt;
use std::sync::atomic::{AtomicIsize, Ordering};
use std::{
fmt,
net::{SocketAddr, ToSocketAddrs},
sync::atomic::{AtomicIsize, Ordering}
};
use super::types::Error;
#[cfg(target_os = "linux")]
use libc;
#[cfg(target_os = "linux")] use libc;
#[cfg(not(target_os = "linux"))]
use time;
#[cfg(not(target_os = "linux"))] use time;
use signal::{trap::Trap, Signal};
use std::time::Instant;
@ -30,9 +30,7 @@ pub fn bytes_to_hex(bytes: &[u8]) -> String {
v.push(HEX_CHARS[(byte >> 4) as usize]);
v.push(HEX_CHARS[(byte & 0xf) as usize]);
}
unsafe {
String::from_utf8_unchecked(v)
}
unsafe { String::from_utf8_unchecked(v) }
}
@ -52,8 +50,7 @@ impl Encoder {
#[inline]
pub fn read_u32(data: &[u8]) -> u32 {
(u32::from(data[0]) << 24) | (u32::from(data[1]) << 16) |
(u32::from(data[2]) << 8) | u32::from(data[3])
(u32::from(data[0]) << 24) | (u32::from(data[1]) << 16) | (u32::from(data[2]) << 8) | u32::from(data[3])
}
#[inline]
@ -66,10 +63,14 @@ impl Encoder {
#[inline]
pub fn read_u64(data: &[u8]) -> u64 {
(u64::from(data[0]) << 56) | (u64::from(data[1]) << 48) |
(u64::from(data[2]) << 40) | (u64::from(data[3]) << 32) |
(u64::from(data[4]) << 24) | (u64::from(data[5]) << 16) |
(u64::from(data[6]) << 8) | u64::from(data[7])
(u64::from(data[0]) << 56)
| (u64::from(data[1]) << 48)
| (u64::from(data[2]) << 40)
| (u64::from(data[3]) << 32)
| (u64::from(data[4]) << 24)
| (u64::from(data[5]) << 16)
| (u64::from(data[6]) << 8)
| u64::from(data[7])
}
#[inline]
@ -121,9 +122,11 @@ pub fn resolve<Addr: ToSocketAddrs+fmt::Debug>(addr: Addr) -> Result<Vec<SocketA
// Remove duplicates in addrs (why are there duplicates???)
let mut addrs = addrs.collect::<Vec<_>>();
// Try IPv4 first as it usually is faster
addrs.sort_by_key(|addr| match *addr {
addrs.sort_by_key(|addr| {
match *addr {
SocketAddr::V4(_) => 4,
SocketAddr::V6(_) => 6
}
});
addrs.dedup();
Ok(addrs)
@ -131,11 +134,9 @@ pub fn resolve<Addr: ToSocketAddrs+fmt::Debug>(addr: Addr) -> Result<Vec<SocketA
#[allow(unused_macros)]
macro_rules! addr {
($addr: expr) => {
{
($addr: expr) => {{
std::net::ToSocketAddrs::to_socket_addrs($addr).unwrap().next().unwrap()
}
};
}};
}
@ -147,29 +148,28 @@ impl fmt::Display for Bytes {
if size >= 512.0 {
size /= 1024.0;
} else {
return write!(formatter, "{:.0} B", size);
return write!(formatter, "{:.0} B", size)
}
if size >= 512.0 {
size /= 1024.0;
} else {
return write!(formatter, "{:.1} KiB", size);
return write!(formatter, "{:.1} KiB", size)
}
if size >= 512.0 {
size /= 1024.0;
} else {
return write!(formatter, "{:.1} MiB", size);
return write!(formatter, "{:.1} MiB", size)
}
if size >= 512.0 {
size /= 1024.0;
} else {
return write!(formatter, "{:.1} GiB", size);
return write!(formatter, "{:.1} GiB", size)
}
write!(formatter, "{:.1} TiB", size)
}
}
pub struct CtrlC {
dummy_time: Instant,
trap: Trap
@ -205,7 +205,9 @@ impl TimeSource for SystemTimeSource {
#[cfg(target_os = "linux")]
fn now() -> Time {
let mut tv = libc::timespec { tv_sec: 0, tv_nsec: 0 };
unsafe { libc::clock_gettime(6, &mut tv); }
unsafe {
libc::clock_gettime(6, &mut tv);
}
tv.tv_sec as Time
}

8
vpncloud.code-workspace Normal file
View File

@ -0,0 +1,8 @@
{
"folders": [
{
"path": "."
}
],
"settings": {}
}