mirror of https://github.com/dswd/vpncloud.git
Changes
This commit is contained in:
parent
19512ea264
commit
d2e3a2370b
22
Makefile
22
Makefile
|
@ -1,22 +0,0 @@
|
||||||
.PHONY: default
|
|
||||||
default: test build
|
|
||||||
|
|
||||||
.PHONY: build
|
|
||||||
build:
|
|
||||||
cargo build --release
|
|
||||||
|
|
||||||
.PHONY: test
|
|
||||||
test:
|
|
||||||
cargo test
|
|
||||||
|
|
||||||
.PHONY: bench
|
|
||||||
bench:
|
|
||||||
cargo bench --features bench
|
|
||||||
|
|
||||||
.PHONY: deb
|
|
||||||
deb:
|
|
||||||
make -C deb
|
|
||||||
|
|
||||||
.PHONY: clean
|
|
||||||
clean:
|
|
||||||
rm -rf target
|
|
|
@ -9,6 +9,7 @@ use std::net::{UdpSocket, ToSocketAddrs, Ipv4Addr, SocketAddr, SocketAddrV4};
|
||||||
use std::os::unix::io::AsRawFd;
|
use std::os::unix::io::AsRawFd;
|
||||||
|
|
||||||
use super::MAGIC;
|
use super::MAGIC;
|
||||||
|
use super::config::Config;
|
||||||
use super::cloud::GenericCloud;
|
use super::cloud::GenericCloud;
|
||||||
use super::device::{Device, Type};
|
use super::device::{Device, Type};
|
||||||
use super::udpmessage::{Message, encode, decode};
|
use super::udpmessage::{Message, encode, decode};
|
||||||
|
@ -150,9 +151,10 @@ fn epoll_wait(b: &mut Bencher) {
|
||||||
|
|
||||||
#[bench]
|
#[bench]
|
||||||
fn handle_interface_data(b: &mut Bencher) {
|
fn handle_interface_data(b: &mut Bencher) {
|
||||||
|
let config = Config::default();
|
||||||
let mut node = GenericCloud::<Frame, SwitchTable>::new(
|
let mut node = GenericCloud::<Frame, SwitchTable>::new(
|
||||||
MAGIC, Device::dummy("vpncloud0", "/dev/null", Type::Tap).unwrap(), 0,
|
&config, Device::dummy("vpncloud0", "/dev/null", Type::Tap).unwrap(),
|
||||||
SwitchTable::new(300, 10), 1800, true, true, vec![], Crypto::None, None
|
SwitchTable::new(300, 10), true, true, vec![], Crypto::None, None
|
||||||
);
|
);
|
||||||
let mut data = [0; 1500];
|
let mut data = [0; 1500];
|
||||||
data[105] = 45;
|
data[105] = 45;
|
||||||
|
@ -164,9 +166,10 @@ fn handle_interface_data(b: &mut Bencher) {
|
||||||
|
|
||||||
#[bench]
|
#[bench]
|
||||||
fn handle_net_message(b: &mut Bencher) {
|
fn handle_net_message(b: &mut Bencher) {
|
||||||
|
let config = Config::default();
|
||||||
let mut node = GenericCloud::<Frame, SwitchTable>::new(
|
let mut node = GenericCloud::<Frame, SwitchTable>::new(
|
||||||
MAGIC, Device::dummy("vpncloud0", "/dev/null", Type::Tap).unwrap(), 0,
|
&config, Device::dummy("vpncloud0", "/dev/null", Type::Tap).unwrap(),
|
||||||
SwitchTable::new(300, 10), 1800, true, true, vec![], Crypto::None, None
|
SwitchTable::new(300, 10), true, true, vec![], Crypto::None, None
|
||||||
);
|
);
|
||||||
let addr = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 1));
|
let addr = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 1));
|
||||||
let mut data = [0; 1500];
|
let mut data = [0; 1500];
|
||||||
|
|
48
src/cloud.rs
48
src/cloud.rs
|
@ -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::net::{SocketAddr, ToSocketAddrs};
|
use std::net::{SocketAddr, ToSocketAddrs};
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::HashMap;
|
||||||
use std::net::UdpSocket;
|
use std::net::UdpSocket;
|
||||||
use std::io::{self, Write};
|
use std::io::{self, Write};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
@ -12,7 +12,8 @@ use std::marker::PhantomData;
|
||||||
use std::hash::BuildHasherDefault;
|
use std::hash::BuildHasherDefault;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
use std::cmp::min;
|
use std::cmp::min;
|
||||||
use std::fs::File;
|
use std::fs::{self, File, Permissions};
|
||||||
|
use std::os::unix::fs::PermissionsExt;
|
||||||
|
|
||||||
use fnv::FnvHasher;
|
use fnv::FnvHasher;
|
||||||
use signal::{trap::Trap, Signal};
|
use signal::{trap::Trap, Signal};
|
||||||
|
@ -46,7 +47,7 @@ struct PeerList {
|
||||||
timeout: Duration,
|
timeout: Duration,
|
||||||
peers: HashMap<SocketAddr, PeerData, Hash>,
|
peers: HashMap<SocketAddr, PeerData, Hash>,
|
||||||
nodes: HashMap<NodeId, SocketAddr, Hash>,
|
nodes: HashMap<NodeId, SocketAddr, Hash>,
|
||||||
addresses: HashSet<SocketAddr, Hash>
|
addresses: HashMap<SocketAddr, NodeId, Hash>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PeerList {
|
impl PeerList {
|
||||||
|
@ -55,7 +56,7 @@ impl PeerList {
|
||||||
peers: HashMap::default(),
|
peers: HashMap::default(),
|
||||||
timeout,
|
timeout,
|
||||||
nodes: HashMap::default(),
|
nodes: HashMap::default(),
|
||||||
addresses: HashSet::default()
|
addresses: HashMap::default()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,7 +83,7 @@ impl PeerList {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn contains_addr(&self, addr: &SocketAddr) -> bool {
|
fn contains_addr(&self, addr: &SocketAddr) -> bool {
|
||||||
self.addresses.contains(addr)
|
self.addresses.contains_key(addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -110,7 +111,7 @@ impl PeerList {
|
||||||
node_id,
|
node_id,
|
||||||
alt_addrs: vec![]
|
alt_addrs: vec![]
|
||||||
});
|
});
|
||||||
self.addresses.insert(addr);
|
self.addresses.insert(addr, node_id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -122,22 +123,27 @@ impl PeerList {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn add_alt_addr(&mut self, node_id: NodeId, addr: SocketAddr) {
|
fn make_primary(&mut self, node_id: NodeId, addr: SocketAddr) {
|
||||||
if let Some(main_addr) = self.nodes.get(&node_id) {
|
if self.peers.contains_key(&addr) {
|
||||||
if let Some(ref mut data) = self.peers.get_mut(main_addr) {
|
return
|
||||||
data.alt_addrs.push(addr);
|
|
||||||
self.addresses.insert(addr);
|
|
||||||
} else {
|
|
||||||
error!("Main address for node is not connected");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
error!("Node not connected");
|
|
||||||
}
|
}
|
||||||
|
let old_addr = match self.nodes.remove(&node_id) {
|
||||||
|
Some(old_addr) => old_addr,
|
||||||
|
None => return error!("Node not connected")
|
||||||
|
};
|
||||||
|
self.nodes.insert(node_id, addr);
|
||||||
|
let mut peer = match self.peers.remove(&old_addr) {
|
||||||
|
Some(peer) => peer,
|
||||||
|
None => return error!("Main address for node is not connected")
|
||||||
|
};
|
||||||
|
peer.alt_addrs.retain(|i| i != &addr);
|
||||||
|
peer.alt_addrs.push(old_addr);
|
||||||
|
self.peers.insert(addr, peer);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn as_vec(&self) -> Vec<SocketAddr> {
|
fn as_vec(&self) -> Vec<SocketAddr> {
|
||||||
self.addresses.iter().cloned().collect()
|
self.addresses.keys().cloned().collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -153,7 +159,7 @@ impl PeerList {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn subset(&self, size: usize) -> Vec<SocketAddr> {
|
fn subset(&self, size: usize) -> Vec<SocketAddr> {
|
||||||
self.addresses.iter().choose_multiple(&mut thread_rng(), size).into_iter().cloned().collect()
|
self.peers.keys().choose_multiple(&mut thread_rng(), size).into_iter().cloned().collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -250,7 +256,7 @@ impl<P: Protocol, T: Table> GenericCloud<P, T> {
|
||||||
next_housekeep: now(),
|
next_housekeep: now(),
|
||||||
next_stats_out: now() + STATS_INTERVAL,
|
next_stats_out: now() + STATS_INTERVAL,
|
||||||
port_forwarding,
|
port_forwarding,
|
||||||
traffic: TrafficStats::new(),
|
traffic: TrafficStats::default(),
|
||||||
stats_file: config.stats_file.clone(),
|
stats_file: config.stats_file.clone(),
|
||||||
_dummy_p: PhantomData,
|
_dummy_p: PhantomData,
|
||||||
}
|
}
|
||||||
|
@ -474,6 +480,7 @@ impl<P: Protocol, T: Table> GenericCloud<P, T> {
|
||||||
try!(writeln!(&mut f));
|
try!(writeln!(&mut f));
|
||||||
try!(self.traffic.write_out(&mut f));
|
try!(self.traffic.write_out(&mut f));
|
||||||
try!(writeln!(&mut f));
|
try!(writeln!(&mut f));
|
||||||
|
try!(fs::set_permissions(self.stats_file.as_ref().unwrap(), Permissions::from_mode(0o644)));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -595,8 +602,7 @@ impl<P: Protocol, T: Table> GenericCloud<P, T> {
|
||||||
}
|
}
|
||||||
// Add sender as peer or as alternative address to existing peer
|
// Add sender as peer or as alternative address to existing peer
|
||||||
if self.peers.contains_node(&node_id) {
|
if self.peers.contains_node(&node_id) {
|
||||||
//TODO: make this address primary
|
self.peers.make_primary(node_id, peer);
|
||||||
self.peers.add_alt_addr(node_id, peer);
|
|
||||||
} else {
|
} else {
|
||||||
self.peers.add(node_id, peer);
|
self.peers.add(node_id, peer);
|
||||||
for range in ranges {
|
for range in ranges {
|
||||||
|
|
|
@ -7,6 +7,7 @@ use super::cloud::Hash;
|
||||||
use super::util::Bytes;
|
use super::util::Bytes;
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
pub struct TrafficEntry {
|
pub struct TrafficEntry {
|
||||||
pub out_bytes_total: u64,
|
pub out_bytes_total: u64,
|
||||||
pub out_packets_total: usize,
|
pub out_packets_total: usize,
|
||||||
|
@ -20,20 +21,6 @@ pub struct TrafficEntry {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TrafficEntry {
|
impl TrafficEntry {
|
||||||
pub fn new() -> Self {
|
|
||||||
TrafficEntry {
|
|
||||||
out_bytes_total: 0,
|
|
||||||
out_packets_total: 0,
|
|
||||||
out_bytes: 0,
|
|
||||||
out_packets: 0,
|
|
||||||
in_bytes_total: 0,
|
|
||||||
in_packets_total: 0,
|
|
||||||
in_bytes: 0,
|
|
||||||
in_packets: 0,
|
|
||||||
idle_periods: 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn count_out(&mut self, bytes: usize) {
|
fn count_out(&mut self, bytes: usize) {
|
||||||
self.out_packets += 1;
|
self.out_packets += 1;
|
||||||
|
@ -63,34 +50,32 @@ impl TrafficEntry {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
pub struct TrafficStats {
|
pub struct TrafficStats {
|
||||||
peers: HashMap<SocketAddr, TrafficEntry, Hash>,
|
peers: HashMap<SocketAddr, TrafficEntry, Hash>,
|
||||||
payload: HashMap<(Address, Address), TrafficEntry, Hash>
|
payload: HashMap<(Address, Address), TrafficEntry, Hash>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TrafficStats {
|
impl TrafficStats {
|
||||||
pub fn new() -> Self {
|
|
||||||
Self { peers: Default::default(), payload: Default::default() }
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn count_out_traffic(&mut self, peer: SocketAddr, bytes: usize) {
|
pub fn count_out_traffic(&mut self, peer: SocketAddr, bytes: usize) {
|
||||||
self.peers.entry(peer).or_insert_with(TrafficEntry::new).count_out(bytes);
|
self.peers.entry(peer).or_insert_with(TrafficEntry::default).count_out(bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn count_in_traffic(&mut self, peer: SocketAddr, bytes: usize) {
|
pub fn count_in_traffic(&mut self, peer: SocketAddr, bytes: usize) {
|
||||||
self.peers.entry(peer).or_insert_with(TrafficEntry::new).count_in(bytes);
|
self.peers.entry(peer).or_insert_with(TrafficEntry::default).count_in(bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn count_out_payload(&mut self, remote: Address, local: Address, bytes: usize) {
|
pub fn count_out_payload(&mut self, remote: Address, local: Address, bytes: usize) {
|
||||||
self.payload.entry((remote, local)).or_insert_with(TrafficEntry::new).count_out(bytes);
|
self.payload.entry((remote, local)).or_insert_with(TrafficEntry::default).count_out(bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn count_in_payload(&mut self, remote: Address, local: Address, bytes: usize) {
|
pub fn count_in_payload(&mut self, remote: Address, local: Address, bytes: usize) {
|
||||||
self.payload.entry((remote, local)).or_insert_with(TrafficEntry::new).count_in(bytes);
|
self.payload.entry((remote, local)).or_insert_with(TrafficEntry::default).count_in(bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn period(&mut self, cleanup_idle: Option<usize>) {
|
pub fn period(&mut self, cleanup_idle: Option<usize>) {
|
||||||
|
@ -131,4 +116,4 @@ impl TrafficStats {
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue