2015-11-19 15:34:20 +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;
|
2015-11-19 15:34:20 +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;
|
2015-11-19 15:34:20 +00:00
|
|
|
|
2015-11-20 12:34:54 +00:00
|
|
|
use time::{Duration, SteadyTime, precise_time_ns};
|
2015-11-20 08:11:54 +00:00
|
|
|
use epoll;
|
2015-11-19 15:34:20 +00:00
|
|
|
|
2015-11-22 18:00:56 +00:00
|
|
|
use super::device::{TunDevice, TapDevice};
|
2015-11-22 16:28:04 +00:00
|
|
|
use super::udpmessage::{encode, decode, Options, Message};
|
2015-11-22 18:00:56 +00:00
|
|
|
use super::ethernet::{Frame, EthAddr, MacTable};
|
|
|
|
use super::ip::{InternetProtocol, IpAddress, RoutingTable};
|
2015-11-22 16:28:04 +00:00
|
|
|
|
2015-11-19 15:34:20 +00:00
|
|
|
|
2015-11-20 17:09:51 +00:00
|
|
|
pub type NetworkId = u64;
|
2015-11-19 15:34:20 +00:00
|
|
|
|
2015-11-22 15:48:01 +00:00
|
|
|
pub trait Table {
|
|
|
|
type Address;
|
|
|
|
fn learn(&mut self, Self::Address, SocketAddr);
|
2015-11-22 17:05:15 +00:00
|
|
|
fn lookup(&self, &Self::Address) -> Option<SocketAddr>;
|
2015-11-22 15:48:01 +00:00
|
|
|
fn housekeep(&mut self);
|
|
|
|
}
|
|
|
|
|
2015-11-22 17:05:15 +00:00
|
|
|
pub trait Protocol: Sized {
|
2015-11-22 15:48:01 +00:00
|
|
|
type Address;
|
2015-11-22 17:05:15 +00:00
|
|
|
fn parse(&[u8]) -> Result<(Self::Address, Self::Address), Error>;
|
2015-11-22 15:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait VirtualInterface: AsRawFd {
|
2015-11-22 17:05:15 +00:00
|
|
|
fn read(&mut self, &mut [u8]) -> Result<usize, Error>;
|
|
|
|
fn write(&mut self, &[u8]) -> Result<(), Error>;
|
2015-11-22 15:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-19 15:34:20 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
ParseError(&'static str),
|
2015-11-20 17:09:51 +00:00
|
|
|
WrongNetwork(Option<NetworkId>),
|
2015-11-19 15:34:20 +00:00
|
|
|
SocketError(&'static str),
|
2015-11-21 17:09:13 +00:00
|
|
|
TunTapDevError(&'static str),
|
2015-11-19 15:34:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct PeerList {
|
|
|
|
timeout: Duration,
|
|
|
|
peers: HashMap<SocketAddr, SteadyTime>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PeerList {
|
|
|
|
fn new(timeout: Duration) -> PeerList {
|
|
|
|
PeerList{peers: HashMap::new(), timeout: timeout}
|
|
|
|
}
|
|
|
|
|
2015-11-20 11:09:07 +00:00
|
|
|
fn timeout(&mut self) -> Vec<SocketAddr> {
|
2015-11-19 15:34:20 +00:00
|
|
|
let now = SteadyTime::now();
|
|
|
|
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 {
|
2015-11-19 15:34:20 +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) {
|
|
|
|
if self.peers.insert(*addr, SteadyTime::now()+self.timeout).is_none() {
|
|
|
|
info!("New peer: {:?}", addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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]
|
|
|
|
fn subset(&self, size: usize, seed: u32) -> Vec<SocketAddr> {
|
|
|
|
let mut peers = self.as_vec();
|
|
|
|
let mut psrng = seed;
|
|
|
|
let len = peers.len();
|
|
|
|
for i in size..len {
|
|
|
|
peers.swap_remove(psrng as usize % (len - i));
|
|
|
|
psrng = ((1664525 as u64) * (psrng as u64) + (1013904223 as u64)) as u32;
|
|
|
|
}
|
|
|
|
peers
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-11-19 15:34:20 +00:00
|
|
|
fn remove(&mut self, addr: &SocketAddr) {
|
|
|
|
if self.peers.remove(&addr).is_some() {
|
|
|
|
info!("Removed peer: {:?}", addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-22 16:28:04 +00:00
|
|
|
|
2015-11-22 17:05:15 +00:00
|
|
|
pub struct GenericCloud<A, T: Table<Address=A>, M: Protocol<Address=A>, I: VirtualInterface> {
|
2015-11-20 08:11:54 +00:00
|
|
|
peers: PeerList,
|
2015-11-20 11:09:07 +00:00
|
|
|
reconnect_peers: Vec<SocketAddr>,
|
2015-11-22 15:48:01 +00:00
|
|
|
table: T,
|
2015-11-20 08:11:54 +00:00
|
|
|
socket: UdpSocket,
|
2015-11-22 15:48:01 +00:00
|
|
|
device: I,
|
2015-11-20 17:09:51 +00:00
|
|
|
network_id: Option<NetworkId>,
|
2015-11-20 08:11:54 +00:00
|
|
|
next_peerlist: SteadyTime,
|
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-20 11:09:07 +00:00
|
|
|
next_housekeep: SteadyTime,
|
2015-11-21 17:09:13 +00:00
|
|
|
_dummy_m: PhantomData<M>,
|
2015-11-19 16:11:59 +00:00
|
|
|
}
|
|
|
|
|
2015-11-22 17:05:15 +00:00
|
|
|
impl<A: fmt::Debug, T: Table<Address=A>, M: Protocol<Address=A>, I: VirtualInterface> GenericCloud<A, T, M, I> {
|
2015-11-22 15:48:01 +00:00
|
|
|
pub fn new(device: I, listen: String, network_id: Option<NetworkId>, table: T, peer_timeout: Duration) -> Self {
|
2015-11-19 15:34:20 +00:00
|
|
|
let socket = match UdpSocket::bind(&listen as &str) {
|
|
|
|
Ok(socket) => socket,
|
|
|
|
_ => panic!("Failed to open socket")
|
|
|
|
};
|
2015-11-22 16:28:04 +00:00
|
|
|
GenericCloud{
|
2015-11-20 08:11:54 +00:00
|
|
|
peers: PeerList::new(peer_timeout),
|
2015-11-20 11:09:07 +00:00
|
|
|
reconnect_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-20 17:09:51 +00:00
|
|
|
network_id: network_id,
|
2015-11-20 08:11:54 +00:00
|
|
|
next_peerlist: SteadyTime::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-21 17:09:13 +00:00
|
|
|
next_housekeep: SteadyTime::now(),
|
|
|
|
_dummy_m: PhantomData,
|
2015-11-20 08:11:54 +00:00
|
|
|
}
|
2015-11-19 15:34:20 +00:00
|
|
|
}
|
|
|
|
|
2015-11-22 17:05:15 +00:00
|
|
|
fn send_msg<Addr: ToSocketAddrs+fmt::Display>(&mut self, addr: Addr, msg: &Message) -> Result<(), Error> {
|
2015-11-19 15:34:20 +00:00
|
|
|
debug!("Sending {:?} to {}", msg, addr);
|
2015-11-20 17:09:51 +00:00
|
|
|
let mut options = Options::default();
|
|
|
|
options.network_id = self.network_id;
|
2015-11-22 16:28:04 +00:00
|
|
|
let size = encode(&options, msg, &mut self.buffer_out);
|
2015-11-20 08:11:54 +00:00
|
|
|
match self.socket.send_to(&self.buffer_out[..size], addr) {
|
2015-11-19 15:34:20 +00:00
|
|
|
Ok(written) if written == size => Ok(()),
|
|
|
|
Ok(_) => Err(Error::SocketError("Sent out truncated packet")),
|
|
|
|
Err(e) => {
|
|
|
|
error!("Failed to send via network {:?}", e);
|
|
|
|
Err(Error::SocketError("IOError when sending"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
while let Some(addr) = addrs.next() {
|
|
|
|
if self.peers.contains(&addr) {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-20 17:09:51 +00:00
|
|
|
info!("Connecting to {}", addr);
|
2015-11-20 11:09:07 +00:00
|
|
|
if reconnect {
|
|
|
|
let addr = addr.to_socket_addrs().unwrap().next().unwrap();
|
|
|
|
self.reconnect_peers.push(addr);
|
|
|
|
}
|
2015-11-20 17:09:51 +00:00
|
|
|
self.send_msg(addr, &Message::GetPeers)
|
2015-11-19 15:34:20 +00:00
|
|
|
}
|
|
|
|
|
2015-11-20 08:11:54 +00:00
|
|
|
fn housekeep(&mut self) -> Result<(), Error> {
|
2015-11-20 09:59:01 +00:00
|
|
|
debug!("Running housekeeping...");
|
2015-11-20 08:11:54 +00:00
|
|
|
self.peers.timeout();
|
2015-11-21 17:09:13 +00:00
|
|
|
self.table.housekeep();
|
2015-11-20 08:11:54 +00:00
|
|
|
if self.next_peerlist <= SteadyTime::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;
|
|
|
|
}
|
|
|
|
}
|
2015-11-20 12:39:57 +00:00
|
|
|
let peers = self.peers.subset(peer_num, precise_time_ns() as u32);
|
2015-11-20 17:09:51 +00:00
|
|
|
let msg = Message::Peers(peers);
|
2015-11-20 12:39:57 +00:00
|
|
|
for addr in &self.peers.as_vec() {
|
2015-11-19 15:34:20 +00:00
|
|
|
try!(self.send_msg(addr, &msg));
|
|
|
|
}
|
2015-11-20 08:11:54 +00:00
|
|
|
self.next_peerlist = SteadyTime::now() + self.update_freq;
|
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(())
|
|
|
|
}
|
|
|
|
|
2015-11-22 17:05:15 +00:00
|
|
|
fn handle_interface_data(&mut self, payload: &[u8]) -> Result<(), Error> {
|
|
|
|
let (src, dst) = try!(M::parse(payload));
|
|
|
|
debug!("Read data from interface: src: {:?}, dst: {:?}, {} bytes", src, dst, payload.len());
|
|
|
|
match self.table.lookup(&dst) {
|
2015-11-19 15:34:20 +00:00
|
|
|
Some(addr) => {
|
2015-11-22 17:05:15 +00:00
|
|
|
debug!("Found destination for {:?} => {}", dst, addr);
|
|
|
|
try!(self.send_msg(addr, &Message::Frame(payload)))
|
2015-11-19 15:34:20 +00:00
|
|
|
},
|
|
|
|
None => {
|
2015-11-22 17:05:15 +00:00
|
|
|
debug!("No destination for {:?} found, broadcasting", dst);
|
|
|
|
let msg = Message::Frame(payload);
|
2015-11-20 08:11:54 +00:00
|
|
|
for addr in &self.peers.as_vec() {
|
2015-11-19 15:34:20 +00:00
|
|
|
try!(self.send_msg(addr, &msg));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2015-11-22 17:05:15 +00:00
|
|
|
fn handle_net_message(&mut self, peer: SocketAddr, options: Options, msg: Message) -> Result<(), Error> {
|
2015-11-20 17:09:51 +00:00
|
|
|
if let Some(id) = self.network_id {
|
|
|
|
if options.network_id != Some(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
|
|
|
}
|
|
|
|
debug!("Recieved {:?} from {}", msg, peer);
|
|
|
|
match msg {
|
2015-11-22 17:05:15 +00:00
|
|
|
Message::Frame(payload) => {
|
|
|
|
let (src, _dst) = try!(M::parse(payload));
|
|
|
|
debug!("Writing data to device: {} bytes", payload.len());
|
|
|
|
match self.device.write(&payload) {
|
2015-11-19 15:34:20 +00:00
|
|
|
Ok(()) => (),
|
|
|
|
Err(e) => {
|
|
|
|
error!("Failed to send via tap device {:?}", e);
|
2015-11-21 17:09:13 +00:00
|
|
|
return Err(Error::TunTapDevError("Failed to write to tap device"));
|
2015-11-19 15:34:20 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-20 09:59:01 +00:00
|
|
|
self.peers.add(&peer);
|
2015-11-22 17:05:15 +00:00
|
|
|
self.table.learn(src, peer);
|
2015-11-19 15:34:20 +00:00
|
|
|
},
|
2015-11-20 17:09:51 +00:00
|
|
|
Message::Peers(peers) => {
|
2015-11-20 08:11:54 +00:00
|
|
|
self.peers.add(&peer);
|
2015-11-19 15:34:20 +00:00
|
|
|
for p in &peers {
|
2015-11-20 08:11:54 +00:00
|
|
|
if ! self.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-11-20 17:09:51 +00:00
|
|
|
Message::GetPeers => {
|
2015-11-20 08:11:54 +00:00
|
|
|
self.peers.add(&peer);
|
|
|
|
let peers = self.peers.as_vec();
|
2015-11-20 17:09:51 +00:00
|
|
|
try!(self.send_msg(peer, &Message::Peers(peers)));
|
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);
|
|
|
|
}
|
2015-11-19 15:34:20 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2015-11-20 08:11:54 +00:00
|
|
|
pub fn run(&mut self) {
|
|
|
|
let epoll_handle = epoll::create1(0).expect("Failed to create epoll handle");
|
|
|
|
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-20 08:11:54 +00:00
|
|
|
epoll::ctl(epoll_handle, epoll::util::ctl_op::ADD, socket_fd, &mut socket_event).expect("Failed to add socket to epoll handle");
|
2015-11-22 15:48:01 +00:00
|
|
|
epoll::ctl(epoll_handle, epoll::util::ctl_op::ADD, device_fd, &mut device_event).expect("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 {
|
2015-11-20 08:11:54 +00:00
|
|
|
let count = epoll::wait(epoll_handle, &mut events, 1000).expect("Epoll wait failed");
|
2015-11-20 09:59:01 +00:00
|
|
|
// Process events
|
2015-11-20 08:11:54 +00:00
|
|
|
for i in 0..count {
|
|
|
|
match &events[i as usize].data {
|
|
|
|
&0 => match self.socket.recv_from(&mut buffer) {
|
|
|
|
Ok((size, src)) => {
|
2015-11-22 16:28:04 +00:00
|
|
|
match decode(&buffer[..size]).and_then(|(options, msg)| self.handle_net_message(src, options, msg)) {
|
2015-11-20 08:11:54 +00:00
|
|
|
Ok(_) => (),
|
|
|
|
Err(e) => error!("Error: {:?}", e)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(_error) => panic!("Failed to read from network socket")
|
|
|
|
},
|
2015-11-22 15:48:01 +00:00
|
|
|
&1 => match self.device.read(&mut buffer) {
|
2015-11-22 17:05:15 +00:00
|
|
|
Ok(size) => match self.handle_interface_data(&buffer[..size]) {
|
2015-11-22 15:48:01 +00:00
|
|
|
Ok(_) => (),
|
|
|
|
Err(e) => error!("Error: {:?}", e)
|
2015-11-20 08:11:54 +00:00
|
|
|
},
|
|
|
|
Err(_error) => panic!("Failed to read from tap device")
|
|
|
|
},
|
|
|
|
_ => unreachable!()
|
|
|
|
}
|
2015-11-19 15:34:20 +00:00
|
|
|
}
|
2015-11-20 09:59:01 +00:00
|
|
|
// Do the housekeeping
|
2015-11-20 11:09:07 +00:00
|
|
|
if self.next_housekeep < SteadyTime::now() {
|
2015-11-20 09:59:01 +00:00
|
|
|
match self.housekeep() {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(e) => error!("Error: {:?}", e)
|
|
|
|
}
|
2015-11-20 11:09:07 +00:00
|
|
|
self.next_housekeep = SteadyTime::now() + Duration::seconds(1)
|
2015-11-20 09:59:01 +00:00
|
|
|
}
|
2015-11-19 19:51:53 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-19 15:34:20 +00:00
|
|
|
}
|
2015-11-22 15:48:01 +00:00
|
|
|
|
2015-11-22 16:28:04 +00:00
|
|
|
|
|
|
|
pub type TapCloud = GenericCloud<EthAddr, MacTable, Frame, TapDevice>;
|
2015-11-22 15:48:01 +00:00
|
|
|
|
|
|
|
impl TapCloud {
|
|
|
|
pub fn new_tap_cloud(device: &str, listen: String, network_id: Option<NetworkId>, mac_timeout: Duration, peer_timeout: Duration) -> Self {
|
|
|
|
let device = match TapDevice::new(device) {
|
|
|
|
Ok(device) => device,
|
|
|
|
_ => panic!("Failed to open tap device")
|
|
|
|
};
|
|
|
|
info!("Opened tap device {}", device.ifname());
|
|
|
|
let table = MacTable::new(mac_timeout);
|
|
|
|
Self::new(device, listen, network_id, table, peer_timeout)
|
|
|
|
}
|
|
|
|
}
|
2015-11-22 18:00:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
pub type TunCloud = GenericCloud<IpAddress, RoutingTable, InternetProtocol, TunDevice>;
|
|
|
|
|
|
|
|
impl TunCloud {
|
|
|
|
pub fn new_tun_cloud(device: &str, listen: String, network_id: Option<NetworkId>, table: RoutingTable, peer_timeout: Duration) -> Self {
|
|
|
|
let device = match TunDevice::new(device) {
|
|
|
|
Ok(device) => device,
|
|
|
|
_ => panic!("Failed to open tun device")
|
|
|
|
};
|
|
|
|
info!("Opened tun device {}", device.ifname());
|
|
|
|
Self::new(device, listen, network_id, table, peer_timeout)
|
|
|
|
}
|
|
|
|
}
|