mirror of https://github.com/dswd/vpncloud.git
restructure
This commit is contained in:
parent
b4fdf89b32
commit
8f747e3448
|
@ -11,9 +11,8 @@ use std::time::Duration as StdDuration;
|
||||||
|
|
||||||
use time::{Duration, SteadyTime};
|
use time::{Duration, SteadyTime};
|
||||||
|
|
||||||
pub use ethernet::{encode as eth_encode, decode as eth_decode, Frame as EthernetFrame};
|
use super::{ethernet, udpmessage};
|
||||||
pub use tapdev::TapDevice;
|
use super::tapdev::TapDevice;
|
||||||
pub use udpmessage::{encode as udp_encode, decode as udp_decode, Message as UdpMessage};
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
|
#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
|
@ -180,10 +179,10 @@ impl EthCloud {
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn send_msg<A: ToSocketAddrs + fmt::Display>(&self, addr: A, msg: &UdpMessage) -> Result<(), Error> {
|
fn send_msg<A: ToSocketAddrs + fmt::Display>(&self, addr: A, msg: &udpmessage::Message) -> Result<(), Error> {
|
||||||
debug!("Sending {:?} to {}", msg, addr);
|
debug!("Sending {:?} to {}", msg, addr);
|
||||||
let mut buffer = [0u8; 64*1024];
|
let mut buffer = [0u8; 64*1024];
|
||||||
let size = udp_encode(self.token, msg, &mut buffer);
|
let size = udpmessage::encode(self.token, msg, &mut buffer);
|
||||||
match self.socket.lock().expect("Lock poisoned").send_to(&buffer[..size], addr) {
|
match self.socket.lock().expect("Lock poisoned").send_to(&buffer[..size], addr) {
|
||||||
Ok(written) if written == size => Ok(()),
|
Ok(written) if written == size => Ok(()),
|
||||||
Ok(_) => Err(Error::SocketError("Sent out truncated packet")),
|
Ok(_) => Err(Error::SocketError("Sent out truncated packet")),
|
||||||
|
@ -196,7 +195,7 @@ impl EthCloud {
|
||||||
|
|
||||||
pub fn connect<A: ToSocketAddrs + fmt::Display>(&self, addr: A) -> Result<(), Error> {
|
pub fn connect<A: ToSocketAddrs + fmt::Display>(&self, addr: A) -> Result<(), Error> {
|
||||||
info!("Connecting to {}", addr);
|
info!("Connecting to {}", addr);
|
||||||
self.send_msg(addr, &UdpMessage::GetPeers)
|
self.send_msg(addr, &udpmessage::Message::GetPeers)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn housekeep(&self) -> Result<(), Error> {
|
fn housekeep(&self) -> Result<(), Error> {
|
||||||
|
@ -206,7 +205,7 @@ impl EthCloud {
|
||||||
if *next_peerlist <= SteadyTime::now() {
|
if *next_peerlist <= SteadyTime::now() {
|
||||||
debug!("Send peer list to all peers");
|
debug!("Send peer list to all peers");
|
||||||
let peers = self.peers.lock().expect("Lock poisoned").as_vec();
|
let peers = self.peers.lock().expect("Lock poisoned").as_vec();
|
||||||
let msg = UdpMessage::Peers(peers.clone());
|
let msg = udpmessage::Message::Peers(peers.clone());
|
||||||
for addr in &peers {
|
for addr in &peers {
|
||||||
try!(self.send_msg(addr, &msg));
|
try!(self.send_msg(addr, &msg));
|
||||||
}
|
}
|
||||||
|
@ -215,16 +214,16 @@ impl EthCloud {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_ethernet_frame(&self, frame: EthernetFrame) -> Result<(), Error> {
|
fn handle_ethernet_frame(&self, frame: ethernet::Frame) -> Result<(), Error> {
|
||||||
debug!("Read ethernet frame from tap {:?}", frame);
|
debug!("Read ethernet frame from tap {:?}", frame);
|
||||||
match self.mactable.lock().expect("Lock poisoned").lookup(frame.dst, frame.vlan) {
|
match self.mactable.lock().expect("Lock poisoned").lookup(frame.dst, frame.vlan) {
|
||||||
Some(addr) => {
|
Some(addr) => {
|
||||||
debug!("Found destination for {:?} (vlan {}) => {}", frame.dst, frame.vlan, addr);
|
debug!("Found destination for {:?} (vlan {}) => {}", frame.dst, frame.vlan, addr);
|
||||||
try!(self.send_msg(addr, &UdpMessage::Frame(frame)))
|
try!(self.send_msg(addr, &udpmessage::Message::Frame(frame)))
|
||||||
},
|
},
|
||||||
None => {
|
None => {
|
||||||
debug!("No destination for {:?} (vlan {}) found, broadcasting", frame.dst, frame.vlan);
|
debug!("No destination for {:?} (vlan {}) found, broadcasting", frame.dst, frame.vlan);
|
||||||
let msg = UdpMessage::Frame(frame);
|
let msg = udpmessage::Message::Frame(frame);
|
||||||
for addr in &self.peers.lock().expect("Lock poisoned").as_vec() {
|
for addr in &self.peers.lock().expect("Lock poisoned").as_vec() {
|
||||||
try!(self.send_msg(addr, &msg));
|
try!(self.send_msg(addr, &msg));
|
||||||
}
|
}
|
||||||
|
@ -233,16 +232,16 @@ impl EthCloud {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_net_message(&self, peer: SocketAddr, token: Token, msg: UdpMessage) -> Result<(), Error> {
|
fn handle_net_message(&self, peer: SocketAddr, token: Token, msg: udpmessage::Message) -> Result<(), Error> {
|
||||||
if token != self.token {
|
if token != self.token {
|
||||||
info!("Ignoring message from {} with wrong token {}", peer, token);
|
info!("Ignoring message from {} with wrong token {}", peer, token);
|
||||||
return Err(Error::WrongToken(token));
|
return Err(Error::WrongToken(token));
|
||||||
}
|
}
|
||||||
debug!("Recieved {:?} from {}", msg, peer);
|
debug!("Recieved {:?} from {}", msg, peer);
|
||||||
match msg {
|
match msg {
|
||||||
UdpMessage::Frame(frame) => {
|
udpmessage::Message::Frame(frame) => {
|
||||||
let mut buffer = [0u8; 64*1024];
|
let mut buffer = [0u8; 64*1024];
|
||||||
let size = eth_encode(&frame, &mut buffer);
|
let size = ethernet::encode(&frame, &mut buffer);
|
||||||
debug!("Writing ethernet frame to tap: {:?}", frame);
|
debug!("Writing ethernet frame to tap: {:?}", frame);
|
||||||
match self.tapdev.lock().expect("Lock poisoned").write(&buffer[..size]) {
|
match self.tapdev.lock().expect("Lock poisoned").write(&buffer[..size]) {
|
||||||
Ok(()) => (),
|
Ok(()) => (),
|
||||||
|
@ -254,7 +253,7 @@ impl EthCloud {
|
||||||
self.peers.lock().expect("Lock poisoned").add(&peer);
|
self.peers.lock().expect("Lock poisoned").add(&peer);
|
||||||
self.mactable.lock().expect("Lock poisoned").learn(frame.src, frame.vlan, &peer);
|
self.mactable.lock().expect("Lock poisoned").learn(frame.src, frame.vlan, &peer);
|
||||||
},
|
},
|
||||||
UdpMessage::Peers(peers) => {
|
udpmessage::Message::Peers(peers) => {
|
||||||
self.peers.lock().expect("Lock poisoned").add(&peer);
|
self.peers.lock().expect("Lock poisoned").add(&peer);
|
||||||
for p in &peers {
|
for p in &peers {
|
||||||
if ! self.peers.lock().expect("Lock poisoned").contains(p) {
|
if ! self.peers.lock().expect("Lock poisoned").contains(p) {
|
||||||
|
@ -262,12 +261,12 @@ impl EthCloud {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
UdpMessage::GetPeers => {
|
udpmessage::Message::GetPeers => {
|
||||||
self.peers.lock().expect("Lock poisoned").add(&peer);
|
self.peers.lock().expect("Lock poisoned").add(&peer);
|
||||||
let peers = self.peers.lock().expect("Lock poisoned").as_vec();
|
let peers = self.peers.lock().expect("Lock poisoned").as_vec();
|
||||||
try!(self.send_msg(peer, &UdpMessage::Peers(peers)));
|
try!(self.send_msg(peer, &udpmessage::Message::Peers(peers)));
|
||||||
},
|
},
|
||||||
UdpMessage::Close => self.peers.lock().expect("Lock poisoned").remove(&peer)
|
udpmessage::Message::Close => self.peers.lock().expect("Lock poisoned").remove(&peer)
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -278,7 +277,7 @@ impl EthCloud {
|
||||||
loop {
|
loop {
|
||||||
match tapdev.read(&mut buffer) {
|
match tapdev.read(&mut buffer) {
|
||||||
Ok(size) => {
|
Ok(size) => {
|
||||||
match eth_decode(&buffer[..size]).and_then(|frame| self.handle_ethernet_frame(frame)) {
|
match ethernet::decode(&mut buffer[..size]).and_then(|frame| self.handle_ethernet_frame(frame)) {
|
||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
Err(e) => error!("Error: {:?}", e)
|
Err(e) => error!("Error: {:?}", e)
|
||||||
}
|
}
|
||||||
|
@ -294,7 +293,7 @@ impl EthCloud {
|
||||||
loop {
|
loop {
|
||||||
match socket.recv_from(&mut buffer) {
|
match socket.recv_from(&mut buffer) {
|
||||||
Ok((size, src)) => {
|
Ok((size, src)) => {
|
||||||
match udp_decode(&buffer[..size]).and_then(|(token, msg)| self.handle_net_message(src, token, msg)) {
|
match udpmessage::decode(&buffer[..size]).and_then(|(token, msg)| self.handle_net_message(src, token, msg)) {
|
||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
Err(e) => error!("Error: {:?}", e)
|
Err(e) => error!("Error: {:?}", e)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,15 +1,7 @@
|
||||||
use std::{mem, slice, ptr, fmt};
|
use std::{mem, ptr, fmt};
|
||||||
|
|
||||||
pub use super::{Mac, Error};
|
use super::ethcloud::{Mac, Error};
|
||||||
|
use super::util::{as_bytes, as_obj};
|
||||||
unsafe fn as_bytes<T>(obj: &T) -> &[u8] {
|
|
||||||
slice::from_raw_parts(mem::transmute::<&T, *const u8>(obj), mem::size_of::<T>())
|
|
||||||
}
|
|
||||||
|
|
||||||
unsafe fn as_obj<T>(data: &[u8]) -> &T {
|
|
||||||
assert!(data.len() >= mem::size_of::<T>());
|
|
||||||
mem::transmute(data.as_ptr())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(PartialEq)]
|
#[derive(PartialEq)]
|
||||||
pub struct Frame<'a> {
|
pub struct Frame<'a> {
|
||||||
|
|
10
src/main.rs
10
src/main.rs
|
@ -3,6 +3,7 @@ extern crate time;
|
||||||
extern crate docopt;
|
extern crate docopt;
|
||||||
extern crate rustc_serialize;
|
extern crate rustc_serialize;
|
||||||
|
|
||||||
|
mod util;
|
||||||
mod udpmessage;
|
mod udpmessage;
|
||||||
mod tapdev;
|
mod tapdev;
|
||||||
mod ethernet;
|
mod ethernet;
|
||||||
|
@ -11,12 +12,13 @@ mod ethcloud;
|
||||||
use time::Duration;
|
use time::Duration;
|
||||||
use docopt::Docopt;
|
use docopt::Docopt;
|
||||||
|
|
||||||
pub use ethcloud::{Error, Mac, Token, EthCloud};
|
use ethcloud::{Error, Token, EthCloud};
|
||||||
pub use ethernet::{encode as eth_encode, decode as eth_decode, Frame as EthernetFrame};
|
|
||||||
pub use tapdev::TapDevice;
|
|
||||||
pub use udpmessage::{encode as udp_encode, decode as udp_decode, Message as UdpMessage};
|
|
||||||
|
|
||||||
|
|
||||||
|
//FIXME: Send peer list in several packets when too large. The current behaviour panics at about
|
||||||
|
// 10000 peers.
|
||||||
|
//TODO: Implement IPv6
|
||||||
|
|
||||||
|
|
||||||
struct SimpleLogger;
|
struct SimpleLogger;
|
||||||
|
|
||||||
|
|
|
@ -2,16 +2,14 @@ use std::{mem, ptr, fmt};
|
||||||
use std::net::{SocketAddr, SocketAddrV4, Ipv4Addr};
|
use std::net::{SocketAddr, SocketAddrV4, Ipv4Addr};
|
||||||
use std::u16;
|
use std::u16;
|
||||||
|
|
||||||
pub use super::{Mac, Error, Token, EthernetFrame, eth_decode, eth_encode};
|
use super::ethcloud::{Mac, Error, Token};
|
||||||
|
use super::ethernet;
|
||||||
|
use super::util::as_obj;
|
||||||
|
|
||||||
unsafe fn as_obj<T>(data: &[u8]) -> &T {
|
|
||||||
assert!(data.len() >= mem::size_of::<T>());
|
|
||||||
mem::transmute(data.as_ptr())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(PartialEq)]
|
#[derive(PartialEq)]
|
||||||
pub enum Message<'a> {
|
pub enum Message<'a> {
|
||||||
Frame(EthernetFrame<'a>),
|
Frame(ethernet::Frame<'a>),
|
||||||
Peers(Vec<SocketAddr>),
|
Peers(Vec<SocketAddr>),
|
||||||
GetPeers,
|
GetPeers,
|
||||||
Close,
|
Close,
|
||||||
|
@ -49,7 +47,7 @@ pub fn decode(data: &[u8]) -> Result<(Token, Message), Error> {
|
||||||
match data[pos] {
|
match data[pos] {
|
||||||
0 => {
|
0 => {
|
||||||
pos += 1;
|
pos += 1;
|
||||||
Ok((token, Message::Frame(try!(eth_decode(&data[pos..])))))
|
Ok((token, Message::Frame(try!(ethernet::decode(&data[pos..])))))
|
||||||
},
|
},
|
||||||
1 => {
|
1 => {
|
||||||
pos += 1;
|
pos += 1;
|
||||||
|
@ -93,7 +91,7 @@ pub fn encode(token: Token, msg: &Message, buf: &mut [u8]) -> usize {
|
||||||
&Message::Frame(ref frame) => {
|
&Message::Frame(ref frame) => {
|
||||||
buf[pos] = 0;
|
buf[pos] = 0;
|
||||||
pos += 1;
|
pos += 1;
|
||||||
pos += eth_encode(&frame, &mut buf[pos..])
|
pos += ethernet::encode(&frame, &mut buf[pos..])
|
||||||
},
|
},
|
||||||
&Message::Peers(ref peers) => {
|
&Message::Peers(ref peers) => {
|
||||||
buf[pos] = 1;
|
buf[pos] = 1;
|
||||||
|
@ -140,7 +138,7 @@ fn encode_message_packet() {
|
||||||
let src = Mac([1,2,3,4,5,6]);
|
let src = Mac([1,2,3,4,5,6]);
|
||||||
let dst = Mac([7,8,9,10,11,12]);
|
let dst = Mac([7,8,9,10,11,12]);
|
||||||
let payload = [1,2,3,4,5];
|
let payload = [1,2,3,4,5];
|
||||||
let msg = Message::Frame(EthernetFrame{src: &src, dst: &dst, vlan: 0, payload: &payload});
|
let msg = Message::Frame(ethernet::Frame{src: &src, dst: &dst, vlan: 0, payload: &payload});
|
||||||
let mut buf = [0; 1024];
|
let mut buf = [0; 1024];
|
||||||
let size = encode(token, &msg, &mut buf[..]);
|
let size = encode(token, &msg, &mut buf[..]);
|
||||||
assert_eq!(size, 26);
|
assert_eq!(size, 26);
|
||||||
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
use std::{mem, slice};
|
||||||
|
|
||||||
|
pub unsafe fn as_bytes<T>(obj: &T) -> &[u8] {
|
||||||
|
slice::from_raw_parts(mem::transmute::<&T, *const u8>(obj), mem::size_of::<T>())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub unsafe fn as_obj<T>(data: &[u8]) -> &T {
|
||||||
|
assert!(data.len() >= mem::size_of::<T>());
|
||||||
|
mem::transmute(data.as_ptr())
|
||||||
|
}
|
Loading…
Reference in New Issue