2016-02-05 15:58:32 +00:00
|
|
|
// VpnCloud - Peer-to-Peer VPN
|
|
|
|
// Copyright (C) 2015-2016 Dennis Schwerdel
|
|
|
|
// This software is licensed under GPL-3 or newer (see LICENSE.md)
|
|
|
|
|
2015-11-23 00:04:30 +00:00
|
|
|
use std::net::{SocketAddr, ToSocketAddrs};
|
2015-11-20 09:59:01 +00:00
|
|
|
use std::collections::HashMap;
|
2015-11-19 15:34:20 +00:00
|
|
|
use std::hash::Hasher;
|
|
|
|
use std::net::UdpSocket;
|
2015-11-19 16:11:59 +00:00
|
|
|
use std::io::Read;
|
2016-02-08 19:37:06 +00:00
|
|
|
use std::io::Result as IoResult;
|
2015-11-23 00:04:30 +00:00
|
|
|
use std::fmt;
|
2015-11-20 08:11:54 +00:00
|
|
|
use std::os::unix::io::AsRawFd;
|
2015-11-21 17:09:13 +00:00
|
|
|
use std::marker::PhantomData;
|
2016-03-29 08:45:54 +00:00
|
|
|
use std::hash::BuildHasherDefault;
|
2015-11-19 15:34:20 +00:00
|
|
|
|
2016-03-29 08:45:54 +00:00
|
|
|
use fnv::FnvHasher;
|
2015-11-20 08:11:54 +00:00
|
|
|
use epoll;
|
2015-11-25 13:31:05 +00:00
|
|
|
use nix::sys::signal::{SIGTERM, SIGQUIT, SIGINT};
|
|
|
|
use signal::trap::Trap;
|
2015-11-25 20:55:30 +00:00
|
|
|
use time::SteadyTime;
|
2015-12-04 10:25:14 +00:00
|
|
|
use rand::{random, sample, thread_rng};
|
2015-11-19 15:34:20 +00:00
|
|
|
|
2015-12-03 08:38:14 +00:00
|
|
|
use super::types::{Table, Protocol, Range, Error, NetworkId, NodeId};
|
2015-11-23 00:40:47 +00:00
|
|
|
use super::device::Device;
|
2015-11-22 16:28:04 +00:00
|
|
|
use super::udpmessage::{encode, decode, Options, Message};
|
2015-11-24 19:55:14 +00:00
|
|
|
use super::crypto::Crypto;
|
2015-12-04 10:25:14 +00:00
|
|
|
use super::util::{now, Time, Duration};
|
2015-11-19 15:34:20 +00:00
|
|
|
|
2016-03-29 08:45:54 +00:00
|
|
|
type Hash = BuildHasherDefault<FnvHasher>;
|
|
|
|
|
2015-11-19 15:34:20 +00:00
|
|
|
struct PeerList {
|
|
|
|
timeout: Duration,
|
2016-03-29 08:45:54 +00:00
|
|
|
peers: HashMap<SocketAddr, Time, Hash>
|
2015-11-19 15:34:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PeerList {
|
|
|
|
fn new(timeout: Duration) -> PeerList {
|
2016-03-29 08:45:54 +00:00
|
|
|
PeerList{peers: HashMap::default(), timeout: timeout}
|
2015-11-19 15:34:20 +00:00
|
|
|
}
|
|
|
|
|
2015-11-20 11:09:07 +00:00
|
|
|
fn timeout(&mut self) -> Vec<SocketAddr> {
|
2015-11-25 20:55:30 +00:00
|
|
|
let now = now();
|
2015-11-19 15:34:20 +00:00
|
|
|
let mut del: Vec<SocketAddr> = Vec::new();
|
|
|
|
for (&addr, &timeout) in &self.peers {
|
|
|
|
if timeout < now {
|
|
|
|
del.push(addr);
|
|
|
|
}
|
|
|
|
}
|
2015-11-20 11:09:07 +00:00
|
|
|
for addr in &del {
|
2016-02-02 21:03:56 +00:00
|
|
|
debug!("Forgot peer: {}", addr);
|
2015-11-20 11:09:07 +00:00
|
|
|
self.peers.remove(addr);
|
2015-11-19 15:34:20 +00:00
|
|
|
}
|
2015-11-20 11:09:07 +00:00
|
|
|
del
|
2015-11-19 15:34:20 +00:00
|
|
|
}
|
|
|
|
|
2015-11-20 12:34:54 +00:00
|
|
|
#[inline(always)]
|
2015-11-19 15:34:20 +00:00
|
|
|
fn contains(&mut self, addr: &SocketAddr) -> bool {
|
|
|
|
self.peers.contains_key(addr)
|
|
|
|
}
|
|
|
|
|
2015-11-20 12:34:54 +00:00
|
|
|
#[inline]
|
2015-11-19 15:34:20 +00:00
|
|
|
fn add(&mut self, addr: &SocketAddr) {
|
2015-11-25 20:55:30 +00:00
|
|
|
if self.peers.insert(*addr, now()+self.timeout as Time).is_none() {
|
2016-02-02 21:03:56 +00:00
|
|
|
info!("New peer: {}", addr);
|
2015-11-19 15:34:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-20 12:34:54 +00:00
|
|
|
#[inline]
|
2015-11-19 15:34:20 +00:00
|
|
|
fn as_vec(&self) -> Vec<SocketAddr> {
|
|
|
|
self.peers.keys().map(|addr| *addr).collect()
|
|
|
|
}
|
|
|
|
|
2015-11-20 12:34:54 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn len(&self) -> usize {
|
|
|
|
self.peers.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-12-04 10:25:14 +00:00
|
|
|
fn subset(&self, size: usize) -> Vec<SocketAddr> {
|
|
|
|
sample(&mut thread_rng(), self.as_vec(), size)
|
2015-11-20 12:34:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-11-19 15:34:20 +00:00
|
|
|
fn remove(&mut self, addr: &SocketAddr) {
|
|
|
|
if self.peers.remove(&addr).is_some() {
|
2016-02-02 21:03:56 +00:00
|
|
|
info!("Removed peer: {}", addr);
|
2015-11-19 15:34:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-22 16:28:04 +00:00
|
|
|
|
2015-11-23 00:40:47 +00:00
|
|
|
pub struct GenericCloud<P: Protocol> {
|
2015-12-03 08:38:14 +00:00
|
|
|
node_id: NodeId,
|
2015-11-20 08:11:54 +00:00
|
|
|
peers: PeerList,
|
2015-11-22 23:49:58 +00:00
|
|
|
addresses: Vec<Range>,
|
2015-11-22 21:00:34 +00:00
|
|
|
learning: bool,
|
2015-11-22 21:45:04 +00:00
|
|
|
broadcast: bool,
|
2015-11-20 11:09:07 +00:00
|
|
|
reconnect_peers: Vec<SocketAddr>,
|
2015-12-03 08:38:14 +00:00
|
|
|
blacklist_peers: Vec<SocketAddr>,
|
2015-11-23 00:40:47 +00:00
|
|
|
table: Box<Table>,
|
2015-11-20 08:11:54 +00:00
|
|
|
socket: UdpSocket,
|
2015-11-23 00:40:47 +00:00
|
|
|
device: Device,
|
2015-11-23 14:40:04 +00:00
|
|
|
options: Options,
|
|
|
|
crypto: Crypto,
|
2015-11-25 20:55:30 +00:00
|
|
|
next_peerlist: Time,
|
2015-11-19 19:51:53 +00:00
|
|
|
update_freq: Duration,
|
2015-11-20 09:59:01 +00:00
|
|
|
buffer_out: [u8; 64*1024],
|
2015-11-25 20:55:30 +00:00
|
|
|
next_housekeep: Time,
|
2015-11-23 00:40:47 +00:00
|
|
|
_dummy_p: PhantomData<P>,
|
2015-11-19 16:11:59 +00:00
|
|
|
}
|
|
|
|
|
2015-11-23 00:40:47 +00:00
|
|
|
impl<P: Protocol> GenericCloud<P> {
|
2016-02-08 19:15:31 +00:00
|
|
|
pub fn new(device: Device, listen: &str, network_id: Option<NetworkId>, table: Box<Table>,
|
2015-11-23 14:40:04 +00:00
|
|
|
peer_timeout: Duration, learning: bool, broadcast: bool, addresses: Vec<Range>,
|
|
|
|
crypto: Crypto) -> Self {
|
2016-02-08 19:15:31 +00:00
|
|
|
let socket = match UdpSocket::bind(listen) {
|
2015-11-19 15:34:20 +00:00
|
|
|
Ok(socket) => socket,
|
2015-11-25 11:29:12 +00:00
|
|
|
_ => fail!("Failed to open socket {}", listen)
|
2015-11-19 15:34:20 +00:00
|
|
|
};
|
2015-11-23 14:40:04 +00:00
|
|
|
let mut options = Options::default();
|
|
|
|
options.network_id = network_id;
|
2015-11-22 16:28:04 +00:00
|
|
|
GenericCloud{
|
2015-12-03 08:38:14 +00:00
|
|
|
node_id: random(),
|
2015-11-20 08:11:54 +00:00
|
|
|
peers: PeerList::new(peer_timeout),
|
2015-11-22 21:00:34 +00:00
|
|
|
addresses: addresses,
|
|
|
|
learning: learning,
|
2015-11-22 21:45:04 +00:00
|
|
|
broadcast: broadcast,
|
2015-11-20 11:09:07 +00:00
|
|
|
reconnect_peers: Vec::new(),
|
2015-12-03 08:38:14 +00:00
|
|
|
blacklist_peers: Vec::new(),
|
2015-11-22 15:48:01 +00:00
|
|
|
table: table,
|
2015-11-20 08:11:54 +00:00
|
|
|
socket: socket,
|
2015-11-22 15:48:01 +00:00
|
|
|
device: device,
|
2015-11-23 14:40:04 +00:00
|
|
|
options: options,
|
|
|
|
crypto: crypto,
|
2015-11-25 20:55:30 +00:00
|
|
|
next_peerlist: now(),
|
2015-11-19 19:51:53 +00:00
|
|
|
update_freq: peer_timeout/2,
|
2015-11-20 09:59:01 +00:00
|
|
|
buffer_out: [0; 64*1024],
|
2015-11-25 20:55:30 +00:00
|
|
|
next_housekeep: now(),
|
2015-11-23 00:40:47 +00:00
|
|
|
_dummy_p: PhantomData,
|
2015-11-20 08:11:54 +00:00
|
|
|
}
|
2015-11-19 15:34:20 +00:00
|
|
|
}
|
|
|
|
|
2015-12-22 21:45:52 +00:00
|
|
|
#[inline]
|
2015-11-23 18:06:25 +00:00
|
|
|
pub fn ifname(&self) -> &str {
|
|
|
|
self.device.ifname()
|
|
|
|
}
|
|
|
|
|
2015-12-22 21:45:52 +00:00
|
|
|
#[inline]
|
|
|
|
fn broadcast_msg(&mut self, msg: &mut Message) -> Result<(), Error> {
|
|
|
|
debug!("Broadcasting {:?}", msg);
|
|
|
|
let msg_data = encode(&mut self.options, msg, &mut self.buffer_out, &mut self.crypto);
|
|
|
|
for addr in &self.peers.as_vec() {
|
|
|
|
try!(match self.socket.send_to(msg_data, addr) {
|
|
|
|
Ok(written) if written == msg_data.len() => Ok(()),
|
|
|
|
Ok(_) => Err(Error::SocketError("Sent out truncated packet")),
|
|
|
|
Err(e) => {
|
|
|
|
error!("Failed to send via network {:?}", e);
|
|
|
|
Err(Error::SocketError("IOError when sending"))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn send_msg(&mut self, addr: SocketAddr, msg: &mut Message) -> Result<(), Error> {
|
2015-11-19 15:34:20 +00:00
|
|
|
debug!("Sending {:?} to {}", msg, addr);
|
2015-12-13 21:03:06 +00:00
|
|
|
let msg_data = encode(&mut self.options, msg, &mut self.buffer_out, &mut self.crypto);
|
|
|
|
match self.socket.send_to(msg_data, addr) {
|
|
|
|
Ok(written) if written == msg_data.len() => Ok(()),
|
2015-11-19 15:34:20 +00:00
|
|
|
Ok(_) => Err(Error::SocketError("Sent out truncated packet")),
|
|
|
|
Err(e) => {
|
|
|
|
error!("Failed to send via network {:?}", e);
|
|
|
|
Err(Error::SocketError("IOError when sending"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-08 19:37:06 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn address(&self) -> IoResult<SocketAddr> {
|
|
|
|
self.socket.local_addr()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn peer_count(&self) -> usize {
|
|
|
|
self.peers.len()
|
|
|
|
}
|
|
|
|
|
2015-11-21 17:09:13 +00:00
|
|
|
pub fn connect<Addr: ToSocketAddrs+fmt::Display>(&mut self, addr: Addr, reconnect: bool) -> Result<(), Error> {
|
2015-11-20 11:09:07 +00:00
|
|
|
if let Ok(mut addrs) = addr.to_socket_addrs() {
|
2015-12-22 21:44:25 +00:00
|
|
|
while let Some(a) = addrs.next() {
|
|
|
|
if self.peers.contains(&a) || self.blacklist_peers.contains(&a) {
|
2015-11-20 11:09:07 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-23 10:55:37 +00:00
|
|
|
debug!("Connecting to {}", addr);
|
2015-11-20 11:09:07 +00:00
|
|
|
if reconnect {
|
2015-12-04 10:25:14 +00:00
|
|
|
if let Ok(mut addrs) = addr.to_socket_addrs() {
|
2015-12-22 21:44:25 +00:00
|
|
|
while let Some(a) = addrs.next() {
|
|
|
|
self.reconnect_peers.push(a);
|
2015-12-04 10:25:14 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-20 11:09:07 +00:00
|
|
|
}
|
2015-12-22 21:44:25 +00:00
|
|
|
let subnets = self.addresses.clone();
|
2015-12-03 08:38:14 +00:00
|
|
|
let node_id = self.node_id.clone();
|
2015-12-22 21:44:25 +00:00
|
|
|
let mut msg = Message::Init(0, node_id, subnets);
|
|
|
|
if let Ok(mut addrs) = addr.to_socket_addrs() {
|
|
|
|
while let Some(a) = addrs.next() {
|
|
|
|
//Ignore error this time
|
|
|
|
self.send_msg(a, &mut msg).ok();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
2015-11-19 15:34:20 +00:00
|
|
|
}
|
|
|
|
|
2015-11-20 08:11:54 +00:00
|
|
|
fn housekeep(&mut self) -> Result<(), Error> {
|
|
|
|
self.peers.timeout();
|
2015-11-21 17:09:13 +00:00
|
|
|
self.table.housekeep();
|
2015-11-25 20:55:30 +00:00
|
|
|
if self.next_peerlist <= now() {
|
2015-11-19 15:34:20 +00:00
|
|
|
debug!("Send peer list to all peers");
|
2015-11-20 12:34:54 +00:00
|
|
|
let mut peer_num = self.peers.len();
|
|
|
|
if peer_num > 10 {
|
|
|
|
peer_num = (peer_num as f32).sqrt().ceil() as usize;
|
|
|
|
if peer_num < 10 {
|
|
|
|
peer_num = 10;
|
|
|
|
}
|
2016-02-04 21:40:26 +00:00
|
|
|
if peer_num > 255 {
|
|
|
|
peer_num = 255
|
|
|
|
}
|
2015-11-20 12:34:54 +00:00
|
|
|
}
|
2015-12-04 10:25:14 +00:00
|
|
|
let peers = self.peers.subset(peer_num);
|
2015-12-13 21:03:06 +00:00
|
|
|
let mut msg = Message::Peers(peers);
|
2015-12-22 21:45:52 +00:00
|
|
|
try!(self.broadcast_msg(&mut msg));
|
2015-11-25 20:55:30 +00:00
|
|
|
self.next_peerlist = now() + self.update_freq as Time;
|
2015-11-19 15:34:20 +00:00
|
|
|
}
|
2015-11-20 11:09:07 +00:00
|
|
|
for addr in self.reconnect_peers.clone() {
|
|
|
|
try!(self.connect(addr, false));
|
|
|
|
}
|
2015-11-19 15:34:20 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-02-08 19:37:06 +00:00
|
|
|
pub fn handle_interface_data(&mut self, payload: &mut [u8], start: usize, end: usize) -> Result<(), Error> {
|
2015-12-22 21:47:41 +00:00
|
|
|
let (src, dst) = try!(P::parse(&payload[start..end]));
|
|
|
|
debug!("Read data from interface: src: {}, dst: {}, {} bytes", src, dst, end-start);
|
2015-11-22 17:05:15 +00:00
|
|
|
match self.table.lookup(&dst) {
|
2015-11-19 15:34:20 +00:00
|
|
|
Some(addr) => {
|
2015-11-26 21:16:51 +00:00
|
|
|
debug!("Found destination for {} => {}", dst, addr);
|
2015-11-22 21:00:34 +00:00
|
|
|
if self.peers.contains(&addr) {
|
2015-12-13 21:03:06 +00:00
|
|
|
try!(self.send_msg(addr, &mut Message::Data(payload, start, end)))
|
2015-11-22 21:00:34 +00:00
|
|
|
} else {
|
2015-11-26 21:16:51 +00:00
|
|
|
warn!("Destination for {} not found in peers: {}", dst, addr);
|
2016-03-29 11:54:28 +00:00
|
|
|
self.table.remove(&dst);
|
2015-11-22 21:00:34 +00:00
|
|
|
}
|
2015-11-19 15:34:20 +00:00
|
|
|
},
|
|
|
|
None => {
|
2015-11-22 21:45:04 +00:00
|
|
|
if !self.broadcast {
|
2015-11-26 21:16:51 +00:00
|
|
|
debug!("No destination for {} found, dropping", dst);
|
2015-11-22 21:45:04 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
2015-11-26 21:16:51 +00:00
|
|
|
debug!("No destination for {} found, broadcasting", dst);
|
2015-12-13 21:03:06 +00:00
|
|
|
let mut msg = Message::Data(payload, start, end);
|
2015-12-22 21:45:52 +00:00
|
|
|
try!(self.broadcast_msg(&mut msg));
|
2015-11-19 15:34:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-02-08 19:37:06 +00:00
|
|
|
pub fn handle_net_message(&mut self, peer: SocketAddr, options: Options, msg: Message) -> Result<(), Error> {
|
2015-12-02 13:16:13 +00:00
|
|
|
if self.options.network_id != options.network_id {
|
|
|
|
info!("Ignoring message from {} with wrong token {:?}", peer, options.network_id);
|
|
|
|
return Err(Error::WrongNetwork(options.network_id));
|
2015-11-19 15:34:20 +00:00
|
|
|
}
|
2015-11-25 20:05:11 +00:00
|
|
|
debug!("Received {:?} from {}", msg, peer);
|
2015-11-19 15:34:20 +00:00
|
|
|
match msg {
|
2015-12-13 21:03:06 +00:00
|
|
|
Message::Data(payload, start, end) => {
|
|
|
|
let (src, _dst) = try!(P::parse(&payload[start..end]));
|
|
|
|
debug!("Writing data to device: {} bytes", end-start);
|
|
|
|
match self.device.write(&payload[start..end]) {
|
2015-11-19 15:34:20 +00:00
|
|
|
Ok(()) => (),
|
|
|
|
Err(e) => {
|
2015-11-26 21:16:51 +00:00
|
|
|
error!("Failed to send via device: {}", e);
|
2015-11-22 21:00:34 +00:00
|
|
|
return Err(Error::TunTapDevError("Failed to write to device"));
|
2015-11-19 15:34:20 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-25 20:05:11 +00:00
|
|
|
// not adding peer to increase performance
|
2015-11-22 21:00:34 +00:00
|
|
|
if self.learning {
|
2015-11-22 23:49:58 +00:00
|
|
|
//learn single address
|
|
|
|
self.table.learn(src, None, peer);
|
2015-11-22 21:00:34 +00:00
|
|
|
}
|
2015-11-19 15:34:20 +00:00
|
|
|
},
|
2015-11-20 17:09:51 +00:00
|
|
|
Message::Peers(peers) => {
|
2015-11-19 15:34:20 +00:00
|
|
|
for p in &peers {
|
2015-12-03 08:38:14 +00:00
|
|
|
if ! self.peers.contains(p) && ! self.blacklist_peers.contains(p) {
|
2015-11-20 11:09:07 +00:00
|
|
|
try!(self.connect(p, false));
|
2015-11-19 15:34:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2015-12-03 08:38:14 +00:00
|
|
|
Message::Init(stage, node_id, ranges) => {
|
|
|
|
if node_id == self.node_id {
|
|
|
|
self.blacklist_peers.push(peer);
|
|
|
|
return Ok(())
|
|
|
|
}
|
2015-11-20 08:11:54 +00:00
|
|
|
self.peers.add(&peer);
|
2015-11-22 23:49:58 +00:00
|
|
|
for range in ranges {
|
|
|
|
self.table.learn(range.base, Some(range.prefix_len), peer.clone());
|
2015-11-22 21:00:34 +00:00
|
|
|
}
|
2015-11-26 09:52:58 +00:00
|
|
|
if stage == 0 {
|
2015-12-03 08:38:14 +00:00
|
|
|
let peers = self.peers.as_vec();
|
|
|
|
let own_addrs = self.addresses.clone();
|
|
|
|
let own_node_id = self.node_id.clone();
|
2015-12-13 21:03:06 +00:00
|
|
|
try!(self.send_msg(peer, &mut Message::Init(stage+1, own_node_id, own_addrs)));
|
|
|
|
try!(self.send_msg(peer, &mut Message::Peers(peers)));
|
2015-11-26 09:52:58 +00:00
|
|
|
}
|
2015-11-19 15:34:20 +00:00
|
|
|
},
|
2015-11-20 17:09:51 +00:00
|
|
|
Message::Close => {
|
2015-11-20 09:59:01 +00:00
|
|
|
self.peers.remove(&peer);
|
2016-03-29 11:54:28 +00:00
|
|
|
self.table.remove_all(&peer);
|
2015-11-20 09:59:01 +00:00
|
|
|
}
|
2015-11-19 15:34:20 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2015-11-20 08:11:54 +00:00
|
|
|
pub fn run(&mut self) {
|
2015-11-25 20:55:30 +00:00
|
|
|
let dummy_time = SteadyTime::now();
|
2015-11-25 13:31:05 +00:00
|
|
|
let trap = Trap::trap(&[SIGINT, SIGTERM, SIGQUIT]);
|
2015-11-25 11:29:12 +00:00
|
|
|
let epoll_handle = try_fail!(epoll::create1(0), "Failed to create epoll handle: {}");
|
2015-11-20 08:11:54 +00:00
|
|
|
let socket_fd = self.socket.as_raw_fd();
|
2015-11-22 15:48:01 +00:00
|
|
|
let device_fd = self.device.as_raw_fd();
|
2015-11-20 08:11:54 +00:00
|
|
|
let mut socket_event = epoll::EpollEvent{events: epoll::util::event_type::EPOLLIN, data: 0};
|
2015-11-22 15:48:01 +00:00
|
|
|
let mut device_event = epoll::EpollEvent{events: epoll::util::event_type::EPOLLIN, data: 1};
|
2015-11-25 11:29:12 +00:00
|
|
|
try_fail!(epoll::ctl(epoll_handle, epoll::util::ctl_op::ADD, socket_fd, &mut socket_event), "Failed to add socket to epoll handle: {}");
|
|
|
|
try_fail!(epoll::ctl(epoll_handle, epoll::util::ctl_op::ADD, device_fd, &mut device_event), "Failed to add device to epoll handle: {}");
|
2015-11-20 08:11:54 +00:00
|
|
|
let mut events = [epoll::EpollEvent{events: 0, data: 0}; 2];
|
|
|
|
let mut buffer = [0; 64*1024];
|
2015-11-19 15:34:20 +00:00
|
|
|
loop {
|
2016-03-29 11:54:28 +00:00
|
|
|
let count = try_fail!(epoll::wait(epoll_handle, &mut events, 1000), "Epoll wait failed: {}") as usize;
|
2015-11-20 09:59:01 +00:00
|
|
|
// Process events
|
2015-11-20 08:11:54 +00:00
|
|
|
for i in 0..count {
|
2016-03-29 11:54:28 +00:00
|
|
|
match &events[i].data {
|
2015-11-25 11:29:12 +00:00
|
|
|
&0 => {
|
|
|
|
let (size, src) = try_fail!(self.socket.recv_from(&mut buffer), "Failed to read from network socket: {}");
|
|
|
|
match decode(&mut buffer[..size], &mut self.crypto).and_then(|(options, msg)| self.handle_net_message(src, options, msg)) {
|
|
|
|
Ok(_) => (),
|
2015-11-30 22:04:24 +00:00
|
|
|
Err(e) => error!("Error: {}, from: {}", e, src)
|
2015-11-25 11:29:12 +00:00
|
|
|
}
|
2015-11-20 08:11:54 +00:00
|
|
|
},
|
2015-11-25 11:29:12 +00:00
|
|
|
&1 => {
|
2015-12-13 21:03:06 +00:00
|
|
|
let start = 64;
|
|
|
|
let size = try_fail!(self.device.read(&mut buffer[start..]), "Failed to read from tap device: {}");
|
|
|
|
match self.handle_interface_data(&mut buffer, start, start+size) {
|
2015-11-22 15:48:01 +00:00
|
|
|
Ok(_) => (),
|
2015-11-26 21:16:51 +00:00
|
|
|
Err(e) => error!("Error: {}", e)
|
2015-11-25 11:29:12 +00:00
|
|
|
}
|
2015-11-20 08:11:54 +00:00
|
|
|
},
|
|
|
|
_ => unreachable!()
|
|
|
|
}
|
2015-11-19 15:34:20 +00:00
|
|
|
}
|
2015-11-25 20:55:30 +00:00
|
|
|
if self.next_housekeep < now() {
|
|
|
|
// Check for signals
|
|
|
|
if trap.wait(dummy_time).is_some() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Do the housekeeping
|
2015-11-20 09:59:01 +00:00
|
|
|
match self.housekeep() {
|
|
|
|
Ok(_) => (),
|
2015-11-26 21:16:51 +00:00
|
|
|
Err(e) => error!("Error: {}", e)
|
2015-11-20 09:59:01 +00:00
|
|
|
}
|
2015-11-25 20:55:30 +00:00
|
|
|
self.next_housekeep = now() + 1
|
2015-11-20 09:59:01 +00:00
|
|
|
}
|
2015-11-19 19:51:53 +00:00
|
|
|
}
|
2015-11-25 13:31:05 +00:00
|
|
|
info!("Shutting down...");
|
2015-12-22 21:45:52 +00:00
|
|
|
self.broadcast_msg(&mut Message::Close).ok();
|
2015-11-19 19:51:53 +00:00
|
|
|
}
|
2015-11-19 15:34:20 +00:00
|
|
|
}
|