Address some clippy comments

Signed-off-by: Lee Smet <lee.smet@hotmail.com>
This commit is contained in:
Lee Smet
2023-07-26 13:15:57 +02:00
parent 2c5eea29ae
commit 974f4ac007
6 changed files with 22 additions and 33 deletions
+2 -3
View File
@@ -113,8 +113,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
};
let dest_addr = if let Some(IpHeader::Version6(header, _)) = packet.ip {
let dest_addr = Ipv6Addr::from(header.destination);
dest_addr
Ipv6Addr::from(header.destination)
} else {
continue;
};
@@ -123,7 +122,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
// Check if destination address is in 200::/7 range
let first_byte = dest_addr.segments()[0] >> 8; // get the first byte
if first_byte < 0x02 || first_byte > 0x3F {
if !(0x02..=0x3F).contains(&first_byte) {
continue;
}
+2
View File
@@ -1,6 +1,8 @@
//! Dedicated logic for
//! [metrics](https://datatracker.ietf.org/doc/html/rfc8966#metric-computation).
#![allow(clippy::suspicious_arithmetic_impl)]
use core::fmt;
use std::ops::Add;
+9 -10
View File
@@ -7,7 +7,7 @@ use crate::{
source_table::{FeasibilityDistance, SourceKey, SourceTable},
x25519::{self, shared_secret_from_keypair},
};
use log::{debug, error, info, trace, warn};
use log::{error, info, trace, warn};
use std::{
collections::HashMap,
error::Error,
@@ -228,7 +228,7 @@ impl Router {
// create retraction update for each dead peer
let retraction_update = ControlPacket::new_update(
32,
UPDATE_INTERVAL as u16,
UPDATE_INTERVAL,
inner.router_seqno,
Metric::infinite(),
dead_peer.overlay_ip(), // todo: fix to use actual prefix, not IP
@@ -338,7 +338,6 @@ impl Router {
// if no entry exists (based on prefix, plen AND neighbor field)
if !route_entry_exists {
if metric.is_infinite() || !inner.source_table.is_update_feasible(&update) {
return;
} else {
// this means that the update is feasible and the metric is not infinite
// create a new route entry and add it to the routing table (which requires a new source entry to be created as well)
@@ -643,7 +642,7 @@ impl Router {
return true;
}
}
return false;
false
}
async fn handle_incoming_data_packet(self, mut router_data_rx: UnboundedReceiver<DataPacket>) {
@@ -717,7 +716,7 @@ impl Router {
// println!("\n\n best route towards {}: {:?}", dest_ip, best_route);
return best_route;
best_route
}
pub async fn propagate_static_route(self) {
@@ -785,13 +784,13 @@ impl RouterInner {
peer_interfaces: Vec::new(),
router_control_tx,
router_data_tx,
node_tun: node_tun,
node_tun,
selected_routing_table: RoutingTable::new(),
fallback_routing_table: RoutingTable::new(),
source_table: SourceTable::new(),
router_seqno: SeqNo::default(),
static_routes: static_routes,
node_keypair: node_keypair,
static_routes,
node_keypair,
node_tun_addr,
dest_pubkey_map: HashMap::new(),
};
@@ -861,7 +860,7 @@ impl RouterInner {
for peer in self.peer_interfaces.iter() {
let update = ControlPacket::new_update(
sr.plen, // static routes have plen 32
UPDATE_INTERVAL as u16,
UPDATE_INTERVAL,
self.router_seqno, // updates receive the seqno of the router
peer.link_cost().into(), // direct connection to other peer, so the only cost is the cost towards the peer
sr.prefix, // the prefix of a static route corresponds to the TUN addr of the node
@@ -892,7 +891,7 @@ impl RouterInner {
let update = ControlPacket::new_update(
sr.0.plen(),
UPDATE_INTERVAL as u16,
UPDATE_INTERVAL,
self.router_seqno, // updates receive the seqno of the router
sr.1.metric() + Metric::from(peer_link_cost),
// the cost of the route is the cost of the route + the cost of the link to the peer
+1 -7
View File
@@ -9,7 +9,7 @@ use core::ops::{Add, AddAssign};
const SEQNO_COMPARE_TRESHOLD: u16 = 32_768;
/// A sequence number on a route.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
pub struct SeqNo(u16);
impl SeqNo {
@@ -40,12 +40,6 @@ impl fmt::Display for SeqNo {
}
}
impl Default for SeqNo {
fn default() -> Self {
SeqNo(0)
}
}
impl From<u16> for SeqNo {
fn from(value: u16) -> Self {
SeqNo(value)
+4 -4
View File
@@ -111,16 +111,16 @@ impl SourceTable {
let source_key = SourceKey::new(prefix, plen, router_id);
match self.get(&source_key) {
Some(&entry) => {
return (!seqno.lt(&entry.seqno()))
(!seqno.lt(&entry.seqno()))
|| (seqno == entry.seqno() && metric < entry.metric())
|| metric.is_infinite();
|| metric.is_infinite()
}
None => return true,
None => true,
}
}
_ => {
error!("Error accepting update, control struct did not match update packet");
return false;
false
}
}
}
+4 -9
View File
@@ -19,9 +19,9 @@ pub fn get_keypair() -> Result<(StaticSecret, PublicKey), Box<dyn std::error::Er
let path = Path::new("keys.txt");
let (secret_key, public_key) = if path.exists() {
let mut file = File::open(&path).expect("Failed to open file");
let mut file = File::open(path).expect("Failed to open file");
let mut secret_bytes = [0u8; 32];
file.read(&mut secret_bytes).expect("Failed to read file");
file.read_exact(&mut secret_bytes)?;
let secret_key = StaticSecret::from(secret_bytes);
let public_key = PublicKey::from(&secret_key);
@@ -31,14 +31,9 @@ pub fn get_keypair() -> Result<(StaticSecret, PublicKey), Box<dyn std::error::Er
let secret_key = StaticSecret::new(OsRng);
let public_key = PublicKey::from(&secret_key);
let mut file = OpenOptions::new()
.write(true)
.create_new(true)
.open(&path)
.expect("Failed to open file");
let mut file = OpenOptions::new().write(true).create_new(true).open(path)?;
file.write_all(secret_key.to_bytes().as_ref())
.expect("Failed to write to file");
file.write_all(secret_key.to_bytes().as_ref())?;
(secret_key, public_key)
};