vpncloud/src/ethcloud.rs

337 lines
11 KiB
Rust
Raw Normal View History

2015-11-19 15:34:20 +00:00
use std::net::{SocketAddr, ToSocketAddrs};
use std::collections::HashMap;
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-19 16:11:59 +00:00
use std::sync::{Arc, Mutex};
use std::thread;
use std::ops::Deref;
use std::time::Duration as StdDuration;
2015-11-19 15:34:20 +00:00
use time::{Duration, SteadyTime};
2015-11-19 18:29:42 +00:00
use super::{ethernet, udpmessage};
use super::tapdev::TapDevice;
2015-11-19 15:34:20 +00:00
#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Mac(pub [u8; 6]);
impl fmt::Debug for Mac {
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(formatter, "{:x}:{:x}:{:x}:{:x}:{:x}:{:x}",
self.0[0], self.0[1], self.0[2], self.0[3], self.0[4], self.0[5])
}
}
pub type Token = u64;
#[derive(Debug)]
pub enum Error {
ParseError(&'static str),
WrongToken(Token),
SocketError(&'static str),
TapdevError(&'static str),
}
struct PeerList {
timeout: Duration,
peers: HashMap<SocketAddr, SteadyTime>
}
impl PeerList {
fn new(timeout: Duration) -> PeerList {
PeerList{peers: HashMap::new(), timeout: timeout}
}
fn timeout(&mut self) {
let now = SteadyTime::now();
let mut del: Vec<SocketAddr> = 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);
}
}
fn contains(&mut self, addr: &SocketAddr) -> bool {
self.peers.contains_key(addr)
}
fn add(&mut self, addr: &SocketAddr) {
if self.peers.insert(*addr, SteadyTime::now()+self.timeout).is_none() {
info!("New peer: {:?}", addr);
}
}
fn as_vec(&self) -> Vec<SocketAddr> {
self.peers.keys().map(|addr| *addr).collect()
}
fn remove(&mut self, addr: &SocketAddr) {
if self.peers.remove(&addr).is_some() {
info!("Removed peer: {:?}", addr);
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
struct MacTableKey {
mac: Mac,
vlan: u16
}
struct MacTableValue {
address: SocketAddr,
timeout: SteadyTime
}
struct MacTable {
table: HashMap<MacTableKey, MacTableValue>,
timeout: Duration
}
impl MacTable {
fn new(timeout: Duration) -> MacTable {
MacTable{table: HashMap::new(), timeout: timeout}
}
fn timeout(&mut self) {
let now = SteadyTime::now();
let mut del: Vec<MacTableKey> = Vec::new();
for (&key, val) in &self.table {
if val.timeout < now {
del.push(key);
}
}
for key in del {
info!("Forgot mac: {:?} (vlan {})", key.mac, key.vlan);
self.table.remove(&key);
}
}
fn learn(&mut self, mac: &Mac, vlan: u16, addr: &SocketAddr) {
let key = MacTableKey{mac: *mac, vlan: vlan};
let value = MacTableValue{address: *addr, timeout: SteadyTime::now()+self.timeout};
if self.table.insert(key, value).is_none() {
info!("Learned mac: {:?} (vlan {}) => {}", mac, vlan, addr);
}
}
fn lookup(&self, mac: &Mac, vlan: u16) -> Option<SocketAddr> {
let key = MacTableKey{mac: *mac, vlan: vlan};
match self.table.get(&key) {
Some(value) => Some(value.address),
None => None
}
}
}
2015-11-19 16:11:59 +00:00
pub struct EthCloudInner {
peers: Mutex<PeerList>,
mactable: Mutex<MacTable>,
socket: Mutex<UdpSocket>,
tapdev: Mutex<TapDevice>,
2015-11-19 15:34:20 +00:00
token: Token,
2015-11-19 16:11:59 +00:00
next_peerlist: Mutex<SteadyTime>,
2015-11-19 19:51:53 +00:00
update_freq: Duration,
running: Mutex<bool>
2015-11-19 15:34:20 +00:00
}
2015-11-19 16:11:59 +00:00
#[derive(Clone)]
pub struct EthCloud(Arc<EthCloudInner>);
impl Deref for EthCloud {
type Target = EthCloudInner;
#[inline(always)]
fn deref(&self) -> &Self::Target {
&self.0
}
}
2015-11-19 15:34:20 +00:00
impl EthCloud {
pub fn new(device: &str, listen: String, token: Token, mac_timeout: Duration, peer_timeout: Duration) -> Self {
let socket = match UdpSocket::bind(&listen as &str) {
Ok(socket) => socket,
_ => panic!("Failed to open socket")
};
let tapdev = match TapDevice::new(device) {
Ok(tapdev) => tapdev,
_ => panic!("Failed to open tap device")
};
info!("Opened tap device {}", tapdev.ifname());
2015-11-19 16:11:59 +00:00
EthCloud(Arc::new(EthCloudInner{
peers: Mutex::new(PeerList::new(peer_timeout)),
mactable: Mutex::new(MacTable::new(mac_timeout)),
socket: Mutex::new(socket),
tapdev: Mutex::new(tapdev),
2015-11-19 15:34:20 +00:00
token: token,
2015-11-19 16:11:59 +00:00
next_peerlist: Mutex::new(SteadyTime::now()),
2015-11-19 19:51:53 +00:00
update_freq: peer_timeout/2,
running: Mutex::new(true)
2015-11-19 16:11:59 +00:00
}))
2015-11-19 15:34:20 +00:00
}
2015-11-19 18:29:42 +00:00
fn send_msg<A: ToSocketAddrs + fmt::Display>(&self, addr: A, msg: &udpmessage::Message) -> Result<(), Error> {
2015-11-19 15:34:20 +00:00
debug!("Sending {:?} to {}", msg, addr);
let mut buffer = [0u8; 64*1024];
2015-11-19 18:29:42 +00:00
let size = udpmessage::encode(self.token, msg, &mut buffer);
2015-11-19 16:11:59 +00:00
match self.socket.lock().expect("Lock poisoned").send_to(&buffer[..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-19 16:11:59 +00:00
pub fn connect<A: ToSocketAddrs + fmt::Display>(&self, addr: A) -> Result<(), Error> {
2015-11-19 15:34:20 +00:00
info!("Connecting to {}", addr);
2015-11-19 18:29:42 +00:00
self.send_msg(addr, &udpmessage::Message::GetPeers)
2015-11-19 15:34:20 +00:00
}
2015-11-19 16:11:59 +00:00
fn housekeep(&self) -> Result<(), Error> {
self.peers.lock().expect("Lock poisoned").timeout();
self.mactable.lock().expect("Lock poisoned").timeout();
let mut next_peerlist = self.next_peerlist.lock().expect("Lock poisoned");
if *next_peerlist <= SteadyTime::now() {
2015-11-19 15:34:20 +00:00
debug!("Send peer list to all peers");
2015-11-19 16:11:59 +00:00
let peers = self.peers.lock().expect("Lock poisoned").as_vec();
2015-11-19 18:29:42 +00:00
let msg = udpmessage::Message::Peers(peers.clone());
2015-11-19 16:11:59 +00:00
for addr in &peers {
2015-11-19 15:34:20 +00:00
try!(self.send_msg(addr, &msg));
}
2015-11-19 16:11:59 +00:00
*next_peerlist = SteadyTime::now() + self.update_freq;
2015-11-19 15:34:20 +00:00
}
Ok(())
}
2015-11-19 18:29:42 +00:00
fn handle_ethernet_frame(&self, frame: ethernet::Frame) -> Result<(), Error> {
2015-11-19 15:34:20 +00:00
debug!("Read ethernet frame from tap {:?}", frame);
2015-11-19 16:11:59 +00:00
match self.mactable.lock().expect("Lock poisoned").lookup(frame.dst, frame.vlan) {
2015-11-19 15:34:20 +00:00
Some(addr) => {
debug!("Found destination for {:?} (vlan {}) => {}", frame.dst, frame.vlan, addr);
2015-11-19 18:29:42 +00:00
try!(self.send_msg(addr, &udpmessage::Message::Frame(frame)))
2015-11-19 15:34:20 +00:00
},
None => {
debug!("No destination for {:?} (vlan {}) found, broadcasting", frame.dst, frame.vlan);
2015-11-19 18:29:42 +00:00
let msg = udpmessage::Message::Frame(frame);
2015-11-19 16:11:59 +00:00
for addr in &self.peers.lock().expect("Lock poisoned").as_vec() {
2015-11-19 15:34:20 +00:00
try!(self.send_msg(addr, &msg));
}
}
}
Ok(())
}
2015-11-19 18:29:42 +00:00
fn handle_net_message(&self, peer: SocketAddr, token: Token, msg: udpmessage::Message) -> Result<(), Error> {
2015-11-19 15:34:20 +00:00
if token != self.token {
info!("Ignoring message from {} with wrong token {}", peer, token);
return Err(Error::WrongToken(token));
}
debug!("Recieved {:?} from {}", msg, peer);
match msg {
2015-11-19 18:29:42 +00:00
udpmessage::Message::Frame(frame) => {
2015-11-19 15:34:20 +00:00
let mut buffer = [0u8; 64*1024];
2015-11-19 18:29:42 +00:00
let size = ethernet::encode(&frame, &mut buffer);
2015-11-19 15:34:20 +00:00
debug!("Writing ethernet frame to tap: {:?}", frame);
2015-11-19 16:11:59 +00:00
match self.tapdev.lock().expect("Lock poisoned").write(&buffer[..size]) {
2015-11-19 15:34:20 +00:00
Ok(()) => (),
Err(e) => {
error!("Failed to send via tap device {:?}", e);
return Err(Error::TapdevError("Failed to write to tap device"));
}
}
2015-11-19 16:20:34 +00:00
self.peers.lock().expect("Lock poisoned").add(&peer);
self.mactable.lock().expect("Lock poisoned").learn(frame.src, frame.vlan, &peer);
2015-11-19 15:34:20 +00:00
},
2015-11-19 18:29:42 +00:00
udpmessage::Message::Peers(peers) => {
2015-11-19 16:11:59 +00:00
self.peers.lock().expect("Lock poisoned").add(&peer);
2015-11-19 15:34:20 +00:00
for p in &peers {
2015-11-19 16:11:59 +00:00
if ! self.peers.lock().expect("Lock poisoned").contains(p) {
2015-11-19 15:34:20 +00:00
try!(self.connect(p));
}
}
},
2015-11-19 18:29:42 +00:00
udpmessage::Message::GetPeers => {
2015-11-19 16:11:59 +00:00
self.peers.lock().expect("Lock poisoned").add(&peer);
let peers = self.peers.lock().expect("Lock poisoned").as_vec();
2015-11-19 18:29:42 +00:00
try!(self.send_msg(peer, &udpmessage::Message::Peers(peers)));
2015-11-19 15:34:20 +00:00
},
2015-11-19 18:29:42 +00:00
udpmessage::Message::Close => self.peers.lock().expect("Lock poisoned").remove(&peer)
2015-11-19 15:34:20 +00:00
}
Ok(())
}
2015-11-19 16:11:59 +00:00
fn run_tapdev(&self) {
2015-11-19 15:34:20 +00:00
let mut buffer = [0u8; 64*1024];
2015-11-19 16:11:59 +00:00
let mut tapdev = self.tapdev.lock().expect("Lock poisoned").clone();
2015-11-19 15:34:20 +00:00
loop {
2015-11-19 16:11:59 +00:00
match tapdev.read(&mut buffer) {
Ok(size) => {
2015-11-19 18:29:42 +00:00
match ethernet::decode(&mut buffer[..size]).and_then(|frame| self.handle_ethernet_frame(frame)) {
2015-11-19 15:34:20 +00:00
Ok(_) => (),
Err(e) => error!("Error: {:?}", e)
}
},
2015-11-19 16:11:59 +00:00
Err(_error) => panic!("Failed to read from tap device")
2015-11-19 15:34:20 +00:00
}
2015-11-19 16:11:59 +00:00
}
}
fn run_socket(&self) {
let mut buffer = [0u8; 64*1024];
let socket = self.socket.lock().expect("Lock poisoned").try_clone().expect("Failed to clone socket");
loop {
match socket.recv_from(&mut buffer) {
Ok((size, src)) => {
2015-11-19 18:29:42 +00:00
match udpmessage::decode(&buffer[..size]).and_then(|(token, msg)| self.handle_net_message(src, token, msg)) {
2015-11-19 15:34:20 +00:00
Ok(_) => (),
Err(e) => error!("Error: {:?}", e)
}
},
2015-11-19 16:11:59 +00:00
Err(_error) => panic!("Failed to read from network socket")
2015-11-19 15:34:20 +00:00
}
2015-11-19 16:11:59 +00:00
}
}
pub fn run(&self) {
let clone = self.clone();
thread::spawn(move || {
clone.run_socket()
});
let clone = self.clone();
thread::spawn(move || {
clone.run_tapdev()
});
2015-11-19 19:51:53 +00:00
while *self.running.lock().expect("Lock poisoned") {
2015-11-19 15:34:20 +00:00
match self.housekeep() {
Ok(_) => (),
Err(e) => error!("Error: {:?}", e)
}
2015-11-19 16:11:59 +00:00
thread::sleep(StdDuration::new(1, 0));
2015-11-19 15:34:20 +00:00
}
}
2015-11-19 19:51:53 +00:00
pub fn close(&self) {
info!("Shutting down...");
for p in self.peers.lock().expect("Lock poisoned").as_vec() {
match self.send_msg(p, &udpmessage::Message::Close) {
Ok(()) => (),
Err(e) => error!("Failed to send close message to {}: {:?}", p, e)
}
}
*self.running.lock().expect("Lock poisoned") = false;
}
2015-11-19 15:34:20 +00:00
}