diff --git a/src/cloud.rs b/src/cloud.rs index ef2608d..64896d0 100644 --- a/src/cloud.rs +++ b/src/cloud.rs @@ -123,6 +123,7 @@ pub struct GenericCloud, M: Protocol, peers: PeerList, addresses: Vec, learning: bool, + broadcast: bool, reconnect_peers: Vec, table: T, socket: UdpSocket, @@ -137,7 +138,7 @@ pub struct GenericCloud, M: Protocol, impl, M: Protocol, I: VirtualInterface> GenericCloud { pub fn new(device: I, listen: String, network_id: Option, table: T, - peer_timeout: Duration, learning: bool, addresses: Vec) -> Self { + peer_timeout: Duration, learning: bool, broadcast: bool, addresses: Vec) -> Self { let socket = match UdpSocket::bind(&listen as &str) { Ok(socket) => socket, _ => panic!("Failed to open socket") @@ -146,6 +147,7 @@ impl, M: Protocol, I: VirtualInterfac peers: PeerList::new(peer_timeout), addresses: addresses, learning: learning, + broadcast: broadcast, reconnect_peers: Vec::new(), table: table, socket: socket, @@ -192,7 +194,6 @@ impl, M: Protocol, I: VirtualInterfac } fn housekeep(&mut self) -> Result<(), Error> { - debug!("Running housekeeping..."); self.peers.timeout(); self.table.housekeep(); if self.next_peerlist <= SteadyTime::now() { @@ -230,6 +231,10 @@ impl, M: Protocol, I: VirtualInterfac } }, 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() { @@ -273,8 +278,13 @@ impl, M: Protocol, I: VirtualInterfac } }, Message::Init(addrs) => { + if self.peers.contains(&peer) { + return Ok(()); + } self.peers.add(&peer); 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))); for addr in addrs { self.table.learn(addr, peer.clone()); @@ -344,7 +354,7 @@ impl TapCloud { }; info!("Opened tap device {}", device.ifname()); 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()); let table = RoutingTable::new(); 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]) } } diff --git a/src/ip.rs b/src/ip.rs index 6dc25ef..12e5e39 100644 --- a/src/ip.rs +++ b/src/ip.rs @@ -186,9 +186,15 @@ impl Table for RoutingTable { fn learn(&mut self, src: Self::Address, addr: SocketAddr) { match src { 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::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); + } } } diff --git a/src/udpmessage.rs b/src/udpmessage.rs index 1dfcc28..1723dfa 100644 --- a/src/udpmessage.rs +++ b/src/udpmessage.rs @@ -4,7 +4,7 @@ use std::u16; use super::cloud::{Error, NetworkId, Address}; use super::util::{as_obj, as_bytes}; -use super::ethernet; +#[cfg(test)] use super::ethernet; const MAGIC: [u8; 3] = [0x76, 0x70, 0x6e]; const VERSION: u8 = 0;