Fixed tun mode

This commit is contained in:
Dennis Schwerdel 2015-11-22 22:45:04 +01:00
parent d9dedcdd0d
commit 64342d1288
3 changed files with 23 additions and 7 deletions

View File

@ -123,6 +123,7 @@ pub struct GenericCloud<A: Address, T: Table<Address=A>, M: Protocol<Address=A>,
peers: PeerList, peers: PeerList,
addresses: Vec<A>, addresses: Vec<A>,
learning: bool, learning: bool,
broadcast: bool,
reconnect_peers: Vec<SocketAddr>, reconnect_peers: Vec<SocketAddr>,
table: T, table: T,
socket: UdpSocket, socket: UdpSocket,
@ -137,7 +138,7 @@ pub struct GenericCloud<A: Address, T: Table<Address=A>, M: Protocol<Address=A>,
impl<A: Address, T: Table<Address=A>, M: Protocol<Address=A>, I: VirtualInterface> GenericCloud<A, T, M, I> { impl<A: Address, T: Table<Address=A>, M: Protocol<Address=A>, I: VirtualInterface> GenericCloud<A, T, M, I> {
pub fn new(device: I, listen: String, network_id: Option<NetworkId>, table: T, pub fn new(device: I, listen: String, network_id: Option<NetworkId>, table: T,
peer_timeout: Duration, learning: bool, addresses: Vec<A>) -> Self { peer_timeout: Duration, learning: bool, broadcast: bool, addresses: Vec<A>) -> Self {
let socket = match UdpSocket::bind(&listen as &str) { let socket = match UdpSocket::bind(&listen as &str) {
Ok(socket) => socket, Ok(socket) => socket,
_ => panic!("Failed to open socket") _ => panic!("Failed to open socket")
@ -146,6 +147,7 @@ impl<A: Address, T: Table<Address=A>, M: Protocol<Address=A>, I: VirtualInterfac
peers: PeerList::new(peer_timeout), peers: PeerList::new(peer_timeout),
addresses: addresses, addresses: addresses,
learning: learning, learning: learning,
broadcast: broadcast,
reconnect_peers: Vec::new(), reconnect_peers: Vec::new(),
table: table, table: table,
socket: socket, socket: socket,
@ -192,7 +194,6 @@ impl<A: Address, T: Table<Address=A>, M: Protocol<Address=A>, I: VirtualInterfac
} }
fn housekeep(&mut self) -> Result<(), Error> { fn housekeep(&mut self) -> Result<(), Error> {
debug!("Running housekeeping...");
self.peers.timeout(); self.peers.timeout();
self.table.housekeep(); self.table.housekeep();
if self.next_peerlist <= SteadyTime::now() { if self.next_peerlist <= SteadyTime::now() {
@ -230,6 +231,10 @@ impl<A: Address, T: Table<Address=A>, M: Protocol<Address=A>, I: VirtualInterfac
} }
}, },
None => { None => {
if !self.broadcast {
debug!("No destination for {:?} found, dropping", dst);
return Ok(());
}
debug!("No destination for {:?} found, broadcasting", dst); debug!("No destination for {:?} found, broadcasting", dst);
let msg = Message::Data(payload); let msg = Message::Data(payload);
for addr in &self.peers.as_vec() { for addr in &self.peers.as_vec() {
@ -273,8 +278,13 @@ impl<A: Address, T: Table<Address=A>, M: Protocol<Address=A>, I: VirtualInterfac
} }
}, },
Message::Init(addrs) => { Message::Init(addrs) => {
if self.peers.contains(&peer) {
return Ok(());
}
self.peers.add(&peer); self.peers.add(&peer);
let peers = self.peers.as_vec(); let peers = self.peers.as_vec();
let own_addrs = self.addresses.clone();
try!(self.send_msg(peer, &Message::Init(own_addrs)));
try!(self.send_msg(peer, &Message::Peers(peers))); try!(self.send_msg(peer, &Message::Peers(peers)));
for addr in addrs { for addr in addrs {
self.table.learn(addr, peer.clone()); self.table.learn(addr, peer.clone());
@ -344,7 +354,7 @@ impl TapCloud {
}; };
info!("Opened tap device {}", device.ifname()); info!("Opened tap device {}", device.ifname());
let table = MacTable::new(mac_timeout); let table = MacTable::new(mac_timeout);
Self::new(device, listen, network_id, table, peer_timeout, true, vec![]) Self::new(device, listen, network_id, table, peer_timeout, true, true, vec![])
} }
} }
@ -360,6 +370,6 @@ impl TunCloud {
info!("Opened tun device {}", device.ifname()); info!("Opened tun device {}", device.ifname());
let table = RoutingTable::new(); let table = RoutingTable::new();
let subnet = IpAddress::from_str(&subnet).expect("Invalid subnet"); let subnet = IpAddress::from_str(&subnet).expect("Invalid subnet");
Self::new(device, listen, network_id, table, peer_timeout, false, vec![subnet]) Self::new(device, listen, network_id, table, peer_timeout, false, false, vec![subnet])
} }
} }

View File

@ -186,9 +186,15 @@ impl Table for RoutingTable {
fn learn(&mut self, src: Self::Address, addr: SocketAddr) { fn learn(&mut self, src: Self::Address, addr: SocketAddr) {
match src { match src {
IpAddress::V4(_) => (), IpAddress::V4(_) => (),
IpAddress::V4Net(base, prefix_len) => self.add(IpAddress::V4(base).to_bytes(), prefix_len, addr), IpAddress::V4Net(base, prefix_len) => {
info!("Adding to routing table: {:?} => {}", src, addr);
self.add(IpAddress::V4(base).to_bytes(), prefix_len, addr);
},
IpAddress::V6(_) => (), IpAddress::V6(_) => (),
IpAddress::V6Net(base, prefix_len) => self.add(IpAddress::V6(base).to_bytes(), prefix_len, addr) IpAddress::V6Net(base, prefix_len) => {
info!("Adding to routing table: {:?} => {}", src, addr);
self.add(IpAddress::V6(base).to_bytes(), prefix_len, addr);
}
} }
} }

View File

@ -4,7 +4,7 @@ use std::u16;
use super::cloud::{Error, NetworkId, Address}; use super::cloud::{Error, NetworkId, Address};
use super::util::{as_obj, as_bytes}; use super::util::{as_obj, as_bytes};
use super::ethernet; #[cfg(test)] use super::ethernet;
const MAGIC: [u8; 3] = [0x76, 0x70, 0x6e]; const MAGIC: [u8; 3] = [0x76, 0x70, 0x6e];
const VERSION: u8 = 0; const VERSION: u8 = 0;