Removing learned addresses of lost peers

This commit is contained in:
Dennis Schwerdel 2016-03-29 13:54:28 +02:00
parent c11d5b4d6e
commit fe7b0f0007
6 changed files with 72 additions and 14 deletions

View File

@ -18,6 +18,7 @@ This project follows [semantic versioning](http://semver.org).
- [fixed] Fixed wrong hex address formatting - [fixed] Fixed wrong hex address formatting
- [fixed] Fixed peer exchange for more than 65000 peers - [fixed] Fixed peer exchange for more than 65000 peers
- [fixed] Initializing crypto for benchmarks - [fixed] Initializing crypto for benchmarks
- [fixed] Removing learned addresses of lost peers
### v0.4.3 (2016-02-02) ### v0.4.3 (2016-02-02)

View File

@ -60,6 +60,7 @@ fn message_encode(b: &mut Bencher) {
b.iter(|| { b.iter(|| {
encode(&mut options, &mut msg, &mut buf[..], &mut crypto); encode(&mut options, &mut msg, &mut buf[..], &mut crypto);
}); });
b.bytes = 1400;
} }
#[bench] #[bench]
@ -73,6 +74,7 @@ fn message_decode(b: &mut Bencher) {
b.iter(|| { b.iter(|| {
decode(&mut res, &mut crypto).unwrap(); decode(&mut res, &mut crypto).unwrap();
}); });
b.bytes = 1400;
} }
#[bench] #[bench]
@ -82,7 +84,8 @@ fn switch_learn(b: &mut Bencher) {
let peer = "1.2.3.4:5678".to_socket_addrs().unwrap().next().unwrap(); let peer = "1.2.3.4:5678".to_socket_addrs().unwrap().next().unwrap();
b.iter(|| { b.iter(|| {
table.learn(addr.clone(), None, peer); table.learn(addr.clone(), None, peer);
}) });
b.bytes = 1400;
} }
#[bench] #[bench]
@ -93,8 +96,8 @@ fn switch_lookup(b: &mut Bencher) {
table.learn(addr.clone(), None, peer); table.learn(addr.clone(), None, peer);
b.iter(|| { b.iter(|| {
table.lookup(&addr); table.lookup(&addr);
}) });
b.bytes = 1400;
} }
#[bench] #[bench]
@ -103,7 +106,8 @@ fn ethernet_parse(b: &mut Bencher) {
data[5] = 45; data[5] = 45;
b.iter(|| { b.iter(|| {
Frame::parse(&data).unwrap() Frame::parse(&data).unwrap()
}) });
b.bytes = 1400;
} }
#[bench] #[bench]
@ -112,7 +116,8 @@ fn ipv4_parse(b: &mut Bencher) {
data[0] = 4*16; data[0] = 4*16;
b.iter(|| { b.iter(|| {
Packet::parse(&data).unwrap() Packet::parse(&data).unwrap()
}) });
b.bytes = 1400;
} }
#[bench] #[bench]
@ -121,14 +126,16 @@ fn ipv6_parse(b: &mut Bencher) {
data[0] = 6*16; data[0] = 6*16;
b.iter(|| { b.iter(|| {
Packet::parse(&data).unwrap() Packet::parse(&data).unwrap()
}) });
b.bytes = 1400;
} }
#[bench] #[bench]
fn now(b: &mut Bencher) { fn now(b: &mut Bencher) {
b.iter(|| { b.iter(|| {
util_now() util_now()
}) });
b.bytes = 1400;
} }
#[bench] #[bench]
@ -142,6 +149,7 @@ fn epoll_wait(b: &mut Bencher) {
b.iter(|| { b.iter(|| {
epoll::wait(epoll_handle, &mut events, 1000).unwrap() epoll::wait(epoll_handle, &mut events, 1000).unwrap()
}); });
b.bytes = 1400;
} }
#[bench] #[bench]

View File

@ -250,6 +250,7 @@ impl<P: Protocol> GenericCloud<P> {
try!(self.send_msg(addr, &mut Message::Data(payload, start, end))) try!(self.send_msg(addr, &mut Message::Data(payload, start, end)))
} else { } else {
warn!("Destination for {} not found in peers: {}", dst, addr); warn!("Destination for {} not found in peers: {}", dst, addr);
self.table.remove(&dst);
} }
}, },
None => { None => {
@ -314,6 +315,7 @@ impl<P: Protocol> GenericCloud<P> {
}, },
Message::Close => { Message::Close => {
self.peers.remove(&peer); self.peers.remove(&peer);
self.table.remove_all(&peer);
} }
} }
Ok(()) Ok(())
@ -332,10 +334,10 @@ impl<P: Protocol> GenericCloud<P> {
let mut events = [epoll::EpollEvent{events: 0, data: 0}; 2]; let mut events = [epoll::EpollEvent{events: 0, data: 0}; 2];
let mut buffer = [0; 64*1024]; let mut buffer = [0; 64*1024];
loop { loop {
let count = try_fail!(epoll::wait(epoll_handle, &mut events, 1000), "Epoll wait failed: {}"); let count = try_fail!(epoll::wait(epoll_handle, &mut events, 1000), "Epoll wait failed: {}") as usize;
// Process events // Process events
for i in 0..count { for i in 0..count {
match &events[i as usize].data { match &events[i].data {
&0 => { &0 => {
let (size, src) = try_fail!(self.socket.recv_from(&mut buffer), "Failed to read from network socket: {}"); 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)) { match decode(&mut buffer[..size], &mut self.crypto).and_then(|(options, msg)| self.handle_net_message(src, options, msg)) {

View File

@ -97,7 +97,20 @@ impl Table for SwitchTable {
} }
} }
fn remove_all(&mut self, _addr: SocketAddr) { #[inline]
unimplemented!() fn remove(&mut self, key: &Address) -> bool {
self.table.remove(key).is_some()
}
fn remove_all(&mut self, addr: &SocketAddr) {
let mut remove = Vec::new();
for (key, val) in self.table.iter() {
if &val.address == addr {
remove.push(key.clone());
}
}
for key in remove {
self.table.remove(&key);
}
} }
} }

View File

@ -111,7 +111,40 @@ impl Table for RoutingTable {
//nothing to do //nothing to do
} }
fn remove_all(&mut self, _addr: SocketAddr) { #[inline]
unimplemented!() fn remove(&mut self, addr: &Address) -> bool {
let len = addr.len as usize;
let mut found = false;
let mut found_len: isize = -1;
for i in 0..len+1 {
if let Some(group) = self.0.get(&addr.data[0..len-i]) {
for entry in group {
let mut match_len = 0;
for j in 0..addr.len as usize {
let b = addr.data[j] ^ entry.bytes[j];
if b == 0 {
match_len += 8;
} else {
match_len += b.leading_zeros();
break;
}
}
if match_len as u8 >= entry.prefix_len && match_len as isize > found_len {
found = true;
found_len = match_len as isize;
}
}
}
}
if found {
self.0.remove(&addr.data[0..found_len as usize]);
}
found
}
fn remove_all(&mut self, addr: &SocketAddr) {
for (_key, entry) in self.0.iter_mut() {
entry.retain(|entr| &entr.address != addr);
}
} }
} }

View File

@ -219,7 +219,8 @@ pub trait Table {
fn learn(&mut self, Address, Option<u8>, SocketAddr); fn learn(&mut self, Address, Option<u8>, SocketAddr);
fn lookup(&mut self, &Address) -> Option<SocketAddr>; fn lookup(&mut self, &Address) -> Option<SocketAddr>;
fn housekeep(&mut self); fn housekeep(&mut self);
fn remove_all(&mut self, SocketAddr); fn remove(&mut self, &Address) -> bool;
fn remove_all(&mut self, &SocketAddr);
} }
pub trait Protocol: Sized { pub trait Protocol: Sized {