Some fixes

This commit is contained in:
Dennis Schwerdel 2020-11-02 20:44:05 +01:00
parent fc18245fda
commit cbc73d0c0e
4 changed files with 52 additions and 29 deletions

View File

@ -5,6 +5,12 @@ This project follows [semantic versioning](http://semver.org).
### UNRELEASED ### UNRELEASED
- [changed] Changed documentation - [changed] Changed documentation
- [changed] Updated dependencies
- [changed] Retrying connections for 120 secs
- [fixed] Fixed corner case with lost init message
- [fixed] Do not reconnect to timed out pending connections
- [fixed] Most specific claims beat less specific claims
- [fixed] Count all invalid protocol traffic
### v2.0.0 (2020-10-30) ### v2.0.0 (2020-10-30)

View File

@ -55,12 +55,12 @@ struct PeerData {
#[derive(Clone)] #[derive(Clone)]
pub struct ReconnectEntry { pub struct ReconnectEntry {
address: String, address: Option<(String, Time)>,
resolved: Vec<SocketAddr>, resolved: Vec<SocketAddr>,
next_resolve: Time,
tries: u16, tries: u16,
timeout: u16, timeout: u16,
next: Time next: Time,
final_timeout: Option<Time>
} }
@ -250,12 +250,12 @@ impl<D: Device, P: Protocol, S: Socket, TS: TimeSource> GenericCloud<D, P, S, TS
} }
}; };
self.reconnect_peers.push(ReconnectEntry { self.reconnect_peers.push(ReconnectEntry {
address: add, address: Some((add, now)),
tries: 0, tries: 0,
timeout: 1, timeout: 1,
resolved, resolved,
next_resolve: now, next: now,
next: now final_timeout: None
}) })
} }
@ -398,11 +398,13 @@ impl<D: Device, P: Protocol, S: Socket, TS: TimeSource> GenericCloud<D, P, S, TS
} }
} }
// Resolve entries anew // Resolve entries anew
if entry.next_resolve <= now { if let Some((ref address, ref mut next_resolve)) = entry.address {
if let Ok(addrs) = resolve(&entry.address as &str) { if *next_resolve <= now {
entry.resolved = addrs; if let Ok(addrs) = resolve(address as &str) {
entry.resolved = addrs;
}
*next_resolve = now + RESOLVE_INTERVAL;
} }
entry.next_resolve = now + RESOLVE_INTERVAL;
} }
// Ignore if next attempt is already in the future // Ignore if next attempt is already in the future
if entry.next > now { if entry.next > now {
@ -421,6 +423,7 @@ impl<D: Device, P: Protocol, S: Socket, TS: TimeSource> GenericCloud<D, P, S, TS
// Schedule next connection attempt // Schedule next connection attempt
entry.next = now + Time::from(entry.timeout); entry.next = now + Time::from(entry.timeout);
} }
self.reconnect_peers.retain(|e| e.final_timeout.unwrap_or(now) >= now);
if self.next_stats_out < now { if self.next_stats_out < now {
// Write out the statistics // Write out the statistics
self.write_out_stats().map_err(|err| Error::FileIo("Failed to write stats file", err))?; self.write_out_stats().map_err(|err| Error::FileIo("Failed to write stats file", err))?;
@ -697,7 +700,10 @@ impl<D: Device, P: Protocol, S: Socket, TS: TimeSource> GenericCloud<D, P, S, TS
} }
MESSAGE_TYPE_KEEPALIVE => self.update_peer_info(src, None)?, MESSAGE_TYPE_KEEPALIVE => self.update_peer_info(src, None)?,
MESSAGE_TYPE_CLOSE => self.remove_peer(src), MESSAGE_TYPE_CLOSE => self.remove_peer(src),
_ => return Err(Error::Message("Unknown message type")) _ => {
self.traffic.count_invalid_protocol(data.len());
return Err(Error::Message("Unknown message type"))
}
} }
} }
MessageResult::Initialized(info) => self.add_new_peer(src, info)?, MessageResult::Initialized(info) => self.add_new_peer(src, info)?,
@ -733,13 +739,17 @@ impl<D: Device, P: Protocol, S: Socket, TS: TimeSource> GenericCloud<D, P, S, TS
self.pending_inits.insert(src, init); self.pending_inits.insert(src, init);
Ok(res) Ok(res)
} }
Err(err) => return Err(err) Err(err) => {
self.traffic.count_invalid_protocol(data.len());
return Err(err)
}
} }
} }
} else if let Some(peer) = self.peers.get_mut(&src) { } else if let Some(peer) = self.peers.get_mut(&src) {
peer.crypto.handle_message(data) peer.crypto.handle_message(data)
} else { } else {
info!("Ignoring non-init message from unknown peer {}", addr_nice(src)); info!("Ignoring non-init message from unknown peer {}", addr_nice(src));
self.traffic.count_invalid_protocol(data.len());
return Ok(()) return Ok(())
}; };
match msg_result { match msg_result {

View File

@ -79,6 +79,8 @@ pub const STAGE_PENG: u8 = 3;
pub const WAITING_TO_CLOSE: u8 = 4; pub const WAITING_TO_CLOSE: u8 = 4;
pub const CLOSING: u8 = 5; pub const CLOSING: u8 = 5;
pub const MAX_FAILED_RETRIES: usize = 120;
pub const SALTED_NODE_ID_HASH_LEN: usize = 20; pub const SALTED_NODE_ID_HASH_LEN: usize = 20;
pub type SaltedNodeIdHash = [u8; SALTED_NODE_ID_HASH_LEN]; pub type SaltedNodeIdHash = [u8; SALTED_NODE_ID_HASH_LEN];
@ -447,7 +449,7 @@ impl<P: Payload> InitState<P> {
Ok(()) Ok(())
} else if self.next_stage == CLOSING { } else if self.next_stage == CLOSING {
Ok(()) Ok(())
} else if self.failed_retries < 5 { } else if self.failed_retries < MAX_FAILED_RETRIES {
self.failed_retries += 1; self.failed_retries += 1;
self.repeat_last_message(out); self.repeat_last_message(out);
Ok(()) Ok(())
@ -632,8 +634,9 @@ impl<P: Payload> InitState<P> {
} }
// decrypt the payload // decrypt the payload
let peer_payload = let peer_payload = self
self.decrypt(&mut encrypted_payload).map_err(|_| Error::CryptoInitFatal("Failed to decrypt payload"))?; .decrypt(&mut encrypted_payload)
.map_err(|_| Error::CryptoInitFatal("Failed to decrypt payload"))?;
// create and send stage 3 reply // create and send stage 3 reply
self.send_message(STAGE_PENG, None, out); self.send_message(STAGE_PENG, None, out);
@ -644,8 +647,9 @@ impl<P: Payload> InitState<P> {
} }
InitMsg::Peng { mut encrypted_payload, .. } => { InitMsg::Peng { mut encrypted_payload, .. } => {
// decrypt the payload // decrypt the payload
let peer_payload = let peer_payload = self
self.decrypt(&mut encrypted_payload).map_err(|_| Error::CryptoInitFatal("Failed to decrypt payload"))?; .decrypt(&mut encrypted_payload)
.map_err(|_| Error::CryptoInitFatal("Failed to decrypt payload"))?;
self.next_stage = CLOSING; // force resend when receiving any message self.next_stage = CLOSING; // force resend when receiving any message
Ok(InitResult::Success { peer_payload, is_initiator: false }) Ok(InitResult::Success { peer_payload, is_initiator: false })
@ -921,10 +925,7 @@ mod tests {
// Fail if no match // Fail if no match
test_algorithm_negotiation( test_algorithm_negotiation(
Algorithms { Algorithms { algorithm_speeds: smallvec![(&AES_128_GCM, 600.0)], allow_unencrypted: true },
algorithm_speeds: smallvec![(&AES_128_GCM, 600.0)],
allow_unencrypted: true
},
Algorithms { Algorithms {
algorithm_speeds: smallvec![(&AES_256_GCM, 500.0), (&CHACHA20_POLY1305, 400.0)], algorithm_speeds: smallvec![(&AES_256_GCM, 500.0), (&CHACHA20_POLY1305, 400.0)],
allow_unencrypted: false allow_unencrypted: false

View File

@ -88,15 +88,21 @@ impl<TS: TimeSource> ClaimTable<TS> {
if let Some(entry) = self.cache.get(&addr) { if let Some(entry) = self.cache.get(&addr) {
return Some(entry.peer) return Some(entry.peer)
} }
let mut found = None;
let mut prefix_len = -1;
for entry in &self.claims { for entry in &self.claims {
if entry.claim.matches(addr) { if entry.claim.prefix_len as isize > prefix_len && entry.claim.matches(addr) {
self.cache.insert(addr, CacheValue { found = Some(entry);
peer: entry.peer, prefix_len = entry.claim.prefix_len as isize;
timeout: min(TS::now() + self.cache_timeout as Time, entry.timeout)
});
return Some(entry.peer)
} }
} }
if let Some(entry) = found {
self.cache.insert(addr, CacheValue {
peer: entry.peer,
timeout: min(TS::now() + self.cache_timeout as Time, entry.timeout)
});
return Some(entry.peer)
}
None None
} }
@ -149,9 +155,9 @@ mod bench {
use super::*; use super::*;
use crate::util::MockTimeSource; use crate::util::MockTimeSource;
use test::Bencher;
use std::str::FromStr;
use smallvec::smallvec; use smallvec::smallvec;
use std::str::FromStr;
use test::Bencher;
#[bench] #[bench]
fn lookup_warm(b: &mut Bencher) { fn lookup_warm(b: &mut Bencher) {