use std::net::{SocketAddr, ToSocketAddrs}; use std::collections::HashMap; use std::hash::Hasher; use std::net::UdpSocket; use std::io::Read; use std::fmt; use std::os::unix::io::AsRawFd; use std::marker::PhantomData; use epoll; use nix::sys::signal::{SIGTERM, SIGQUIT, SIGINT}; use signal::trap::Trap; use time::SteadyTime; use super::types::{Table, Protocol, Range, Error, NetworkId}; use super::device::Device; use super::udpmessage::{encode, decode, Options, Message}; use super::crypto::Crypto; use super::util::{now, Time, Duration, time_rand}; struct PeerList { timeout: Duration, peers: HashMap } impl PeerList { fn new(timeout: Duration) -> PeerList { PeerList{peers: HashMap::new(), timeout: timeout} } fn timeout(&mut self) -> Vec { let now = now(); let mut del: Vec = Vec::new(); for (&addr, &timeout) in &self.peers { if timeout < now { del.push(addr); } } for addr in &del { debug!("Forgot peer: {:?}", addr); self.peers.remove(addr); } del } #[inline(always)] fn contains(&mut self, addr: &SocketAddr) -> bool { self.peers.contains_key(addr) } #[inline] fn add(&mut self, addr: &SocketAddr) { if self.peers.insert(*addr, now()+self.timeout as Time).is_none() { info!("New peer: {:?}", addr); } } #[inline] fn as_vec(&self) -> Vec { self.peers.keys().map(|addr| *addr).collect() } #[inline(always)] fn len(&self) -> usize { self.peers.len() } #[inline] fn subset(&self, size: usize, seed: u32) -> Vec { 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] fn remove(&mut self, addr: &SocketAddr) { if self.peers.remove(&addr).is_some() { info!("Removed peer: {:?}", addr); } } } pub struct GenericCloud { peers: PeerList, addresses: Vec, learning: bool, broadcast: bool, reconnect_peers: Vec, table: Box, socket: UdpSocket, device: Device, options: Options, crypto: Crypto, next_peerlist: Time, update_freq: Duration, buffer_out: [u8; 64*1024], next_housekeep: Time, _dummy_p: PhantomData

, } impl GenericCloud

{ pub fn new(device: Device, listen: String, network_id: Option, table: Box

, peer_timeout: Duration, learning: bool, broadcast: bool, addresses: Vec, crypto: Crypto) -> Self { let socket = match UdpSocket::bind(&listen as &str) { Ok(socket) => socket, _ => fail!("Failed to open socket {}", listen) }; let mut options = Options::default(); options.network_id = network_id; GenericCloud{ peers: PeerList::new(peer_timeout), addresses: addresses, learning: learning, broadcast: broadcast, reconnect_peers: Vec::new(), table: table, socket: socket, device: device, options: options, crypto: crypto, next_peerlist: now(), update_freq: peer_timeout/2, buffer_out: [0; 64*1024], next_housekeep: now(), _dummy_p: PhantomData, } } pub fn ifname(&self) -> &str { self.device.ifname() } fn send_msg(&mut self, addr: Addr, msg: &Message) -> Result<(), Error> { debug!("Sending {:?} to {}", msg, addr); let size = encode(&mut self.options, msg, &mut self.buffer_out, &mut self.crypto); match self.socket.send_to(&self.buffer_out[..size], addr) { 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")) } } } pub fn connect(&mut self, addr: Addr, reconnect: bool) -> Result<(), Error> { if let Ok(mut addrs) = addr.to_socket_addrs() { while let Some(addr) = addrs.next() { if self.peers.contains(&addr) { return Ok(()); } } } debug!("Connecting to {}", addr); if reconnect { let addr = addr.to_socket_addrs().unwrap().next().unwrap(); self.reconnect_peers.push(addr); } let addrs = self.addresses.clone(); self.send_msg(addr, &Message::Init(0, addrs)) } fn housekeep(&mut self) -> Result<(), Error> { self.peers.timeout(); self.table.housekeep(); if self.next_peerlist <= now() { debug!("Send peer list to all peers"); 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; } } let peers = self.peers.subset(peer_num, time_rand() as u32); let msg = Message::Peers(peers); for addr in &self.peers.as_vec() { try!(self.send_msg(addr, &msg)); } self.next_peerlist = now() + self.update_freq as Time; } for addr in self.reconnect_peers.clone() { try!(self.connect(addr, false)); } Ok(()) } fn handle_interface_data(&mut self, payload: &[u8]) -> Result<(), Error> { let (src, dst) = try!(P::parse(payload)); debug!("Read data from interface: src: {}, dst: {}, {} bytes", src, dst, payload.len()); match self.table.lookup(&dst) { Some(addr) => { debug!("Found destination for {} => {}", dst, addr); if self.peers.contains(&addr) { try!(self.send_msg(addr, &Message::Data(payload))) } else { warn!("Destination for {} not found in peers: {}", dst, addr); } }, None => { if !self.broadcast { debug!("No destination for {} found, dropping", dst); return Ok(()); } debug!("No destination for {} found, broadcasting", dst); let msg = Message::Data(payload); for addr in &self.peers.as_vec() { try!(self.send_msg(addr, &msg)); } } } Ok(()) } fn handle_net_message(&mut self, peer: SocketAddr, options: Options, msg: Message) -> Result<(), Error> { if let Some(id) = self.options.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)); } } debug!("Received {:?} from {}", msg, peer); match msg { Message::Data(payload) => { let (src, _dst) = try!(P::parse(payload)); debug!("Writing data to device: {} bytes", payload.len()); match self.device.write(&payload) { Ok(()) => (), Err(e) => { error!("Failed to send via device: {}", e); return Err(Error::TunTapDevError("Failed to write to device")); } } // not adding peer to increase performance if self.learning { //learn single address self.table.learn(src, None, peer); } }, Message::Peers(peers) => { self.peers.add(&peer); for p in &peers { if ! self.peers.contains(p) { try!(self.connect(p, false)); } } }, Message::Init(stage, ranges) => { self.peers.add(&peer); let peers = self.peers.as_vec(); let own_addrs = self.addresses.clone(); for range in ranges { self.table.learn(range.base, Some(range.prefix_len), peer.clone()); } if stage == 0 { try!(self.send_msg(peer, &Message::Init(stage+1, own_addrs))); try!(self.send_msg(peer, &Message::Peers(peers))); } }, Message::Close => { self.peers.remove(&peer); } } Ok(()) } pub fn run(&mut self) { let dummy_time = SteadyTime::now(); let trap = Trap::trap(&[SIGINT, SIGTERM, SIGQUIT]); let epoll_handle = try_fail!(epoll::create1(0), "Failed to create epoll handle: {}"); let socket_fd = self.socket.as_raw_fd(); let device_fd = self.device.as_raw_fd(); let mut socket_event = epoll::EpollEvent{events: epoll::util::event_type::EPOLLIN, data: 0}; let mut device_event = epoll::EpollEvent{events: epoll::util::event_type::EPOLLIN, data: 1}; 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: {}"); let mut events = [epoll::EpollEvent{events: 0, data: 0}; 2]; let mut buffer = [0; 64*1024]; loop { let count = try_fail!(epoll::wait(epoll_handle, &mut events, 1000), "Epoll wait failed: {}"); // Process events for i in 0..count { match &events[i as usize].data { &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(_) => (), Err(e) => error!("Error: {}, from: {}", e, src) } }, &1 => { let size = try_fail!(self.device.read(&mut buffer), "Failed to read from tap device: {}"); match self.handle_interface_data(&buffer[..size]) { Ok(_) => (), Err(e) => error!("Error: {}", e) } }, _ => unreachable!() } } if self.next_housekeep < now() { // Check for signals if trap.wait(dummy_time).is_some() { break; } // Do the housekeeping match self.housekeep() { Ok(_) => (), Err(e) => error!("Error: {}", e) } self.next_housekeep = now() + 1 } } info!("Shutting down..."); for p in &self.peers.as_vec() { let _ = self.send_msg(p, &Message::Close); } } }