diff --git a/src/lib.rs b/src/lib.rs index f222a2f..14f189f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,11 @@ //! //! *Note: There is a fair chance you are looking for `src/cli/main.rs` instead of this file.* +#![deny(clippy::pedantic)] +#![allow(clippy::module_name_repetitions)] +#![allow(clippy::missing_errors_doc)] +#![allow(clippy::missing_panics_doc)] + pub mod metrics; pub mod nat; mod packet; diff --git a/src/metrics/http.rs b/src/metrics/http.rs index c0eddc6..5f88e1c 100644 --- a/src/metrics/http.rs +++ b/src/metrics/http.rs @@ -7,6 +7,7 @@ use hyper::{ use prometheus::{Encoder, TextEncoder}; /// Handle an HTTP request +#[allow(clippy::unused_async)] async fn handle_request(request: Request) -> Result, Infallible> { // If the request is targeting the metrics endpoint if request.method() == Method::GET && request.uri().path() == "/metrics" { @@ -39,6 +40,6 @@ pub async fn serve_metrics(bind_addr: SocketAddr) { // Run the server if let Err(e) = server.await { - eprintln!("Metrics server error: {}", e); + eprintln!("Metrics server error: {e}"); } } diff --git a/src/nat/mod.rs b/src/nat/mod.rs index f79fb1b..930b363 100644 --- a/src/nat/mod.rs +++ b/src/nat/mod.rs @@ -44,7 +44,7 @@ impl Nat64 { interface.add_route(ipv6_nat_prefix.into()).await?; // Add the IPv4 pool prefixes as routes - for ipv4_prefix in ipv4_pool.iter() { + for ipv4_prefix in &ipv4_pool { interface.add_route((*ipv4_prefix).into()).await?; } @@ -149,7 +149,7 @@ impl Nat64 { // Spawn a task to process the packet tokio::spawn(async move { if let Some(output) = unwrap_log(translate_ipv6_to_ipv4( - packet, + &packet, new_source, new_destination, )) { @@ -169,7 +169,7 @@ impl Nat64 { log::warn!("Translator running behind! Dropping {} packets", count); Ok(()) } - error => Err(error), + error @ broadcast::error::RecvError::Closed => Err(error), }, }?; } diff --git a/src/nat/table.rs b/src/nat/table.rs index 6770178..e7e3693 100644 --- a/src/nat/table.rs +++ b/src/nat/table.rs @@ -176,10 +176,10 @@ impl Nat64Table { // Track the values IPV4_POOL_RESERVED .with_label_values(&["dynamic"]) - .set(total_dynamic_reservations as i64); + .set(i64::from(total_dynamic_reservations)); IPV4_POOL_RESERVED .with_label_values(&["static"]) - .set(total_static_reservations as i64); + .set(i64::from(total_static_reservations)); } } diff --git a/src/packet/protocols/icmp.rs b/src/packet/protocols/icmp.rs index 6def84e..53e4a2a 100644 --- a/src/packet/protocols/icmp.rs +++ b/src/packet/protocols/icmp.rs @@ -13,7 +13,7 @@ pub struct IcmpPacket { } impl IcmpPacket { - /// Construct a new ICMPv6 packet + /// Construct a new `ICMP` packet pub fn new(icmp_type: IcmpType, icmp_code: IcmpCode, payload: T) -> Self { Self { icmp_type, @@ -32,7 +32,7 @@ where fn try_from(bytes: Vec) -> Result { // Parse the packet let packet = pnet_packet::icmp::IcmpPacket::new(&bytes) - .ok_or(PacketError::TooShort(bytes.len(), bytes.to_vec()))?; + .ok_or(PacketError::TooShort(bytes.len(), bytes.clone()))?; // Return the packet Ok(Self { diff --git a/src/packet/protocols/icmpv6.rs b/src/packet/protocols/icmpv6.rs index 4d48539..d320e93 100644 --- a/src/packet/protocols/icmpv6.rs +++ b/src/packet/protocols/icmpv6.rs @@ -19,7 +19,7 @@ pub struct Icmpv6Packet { } impl Icmpv6Packet { - /// Construct a new ICMPv6 packet + /// Construct a new `ICMPv6` packet pub fn new( source_address: Ipv6Addr, destination_address: Ipv6Addr, @@ -41,7 +41,7 @@ impl Icmpv6Packet where T: From>, { - /// Construct a new ICMPv6 packet from raw bytes + /// Construct a new `ICMPv6` packet from raw bytes #[allow(dead_code)] pub fn new_from_bytes( bytes: &[u8], @@ -64,7 +64,7 @@ where } impl Icmpv6Packet { - /// Construct a new ICMPv6 packet with a raw payload from raw bytes + /// Construct a new `ICMPv6` packet with a raw payload from raw bytes pub fn new_from_bytes_raw_payload( bytes: &[u8], source_address: Ipv6Addr, diff --git a/src/packet/protocols/ipv4.rs b/src/packet/protocols/ipv4.rs index aa23327..6b17f6b 100644 --- a/src/packet/protocols/ipv4.rs +++ b/src/packet/protocols/ipv4.rs @@ -54,6 +54,7 @@ impl Ipv4Packet { } } + #[allow(clippy::cast_possible_truncation)] fn options_length_words(&self) -> u8 { self.options .iter() @@ -72,7 +73,7 @@ where fn try_from(bytes: Vec) -> Result { // Parse the packet let packet = pnet_packet::ipv4::Ipv4Packet::new(&bytes) - .ok_or(PacketError::TooShort(bytes.len(), bytes.to_vec()))?; + .ok_or(PacketError::TooShort(bytes.len(), bytes.clone()))?; // Return the packet Ok(Self { diff --git a/src/packet/protocols/ipv6.rs b/src/packet/protocols/ipv6.rs index bc50ff9..bd89377 100644 --- a/src/packet/protocols/ipv6.rs +++ b/src/packet/protocols/ipv6.rs @@ -47,7 +47,7 @@ where fn try_from(bytes: Vec) -> Result { // Parse the packet let packet = pnet_packet::ipv6::Ipv6Packet::new(&bytes) - .ok_or(PacketError::TooShort(bytes.len(), bytes.to_vec()))?; + .ok_or(PacketError::TooShort(bytes.len(), bytes.clone()))?; // Return the packet Ok(Self { @@ -80,7 +80,7 @@ where output.set_version(6); output.set_traffic_class(packet.traffic_class); output.set_flow_label(packet.flow_label); - output.set_payload_length(payload.len() as u16); + output.set_payload_length(u16::try_from(payload.len()).unwrap()); output.set_next_header(packet.next_header); output.set_hop_limit(packet.hop_limit); output.set_source(packet.source_address); diff --git a/src/packet/protocols/tcp.rs b/src/packet/protocols/tcp.rs index ced9c2e..d1623e5 100644 --- a/src/packet/protocols/tcp.rs +++ b/src/packet/protocols/tcp.rs @@ -103,11 +103,12 @@ impl TcpPacket { } /// Get the length of the options in words - fn options_length(&self) -> usize { + #[allow(clippy::cast_possible_truncation)] + fn options_length(&self) -> u8 { self.options .iter() - .map(TcpOptionPacket::packet_size) - .sum::() + .map(|option| TcpOptionPacket::packet_size(option) as u8) + .sum::() } } @@ -143,7 +144,7 @@ where flags: parsed.get_flags(), window_size: parsed.get_window(), urgent_pointer: parsed.get_urgent_ptr(), - options: parsed.get_options().to_vec(), + options: parsed.get_options().clone(), payload: parsed.payload().to_vec().into(), }) } @@ -177,7 +178,7 @@ impl TcpPacket { flags: parsed.get_flags(), window_size: parsed.get_window(), urgent_pointer: parsed.get_urgent_ptr(), - options: parsed.get_options().to_vec(), + options: parsed.get_options().clone(), payload: RawBytes(parsed.payload().to_vec()), }) } @@ -196,7 +197,7 @@ where // Allocate a mutable packet to write into let total_length = pnet_packet::tcp::MutableTcpPacket::minimum_packet_size() - + options_length + + options_length as usize + payload.len(); let mut output = pnet_packet::tcp::MutableTcpPacket::owned(vec![0u8; total_length]).unwrap(); @@ -210,7 +211,7 @@ where output.set_acknowledgement(packet.ack_number); // Write the offset - output.set_data_offset(5 + (options_length / 4) as u8); + output.set_data_offset(5 + (options_length / 4)); // Write the options output.set_options(&packet.options); diff --git a/src/packet/protocols/udp.rs b/src/packet/protocols/udp.rs index cfc8572..98b73dd 100644 --- a/src/packet/protocols/udp.rs +++ b/src/packet/protocols/udp.rs @@ -160,7 +160,7 @@ where output.set_destination(packet.destination.port()); // Write the length - output.set_length(total_length as u16); + output.set_length(u16::try_from(total_length).unwrap()); // Write the payload output.set_payload(&payload); diff --git a/src/packet/xlat/icmp/mod.rs b/src/packet/xlat/icmp/mod.rs index d40563f..f0479b0 100644 --- a/src/packet/xlat/icmp/mod.rs +++ b/src/packet/xlat/icmp/mod.rs @@ -1,3 +1,5 @@ +#![allow(clippy::doc_markdown)] + use std::net::{Ipv4Addr, Ipv6Addr}; use pnet_packet::{icmp::IcmpTypes, icmpv6::Icmpv6Types}; @@ -92,7 +94,7 @@ pub fn translate_icmpv6_to_icmp( // Translate let inner_payload = - translate_ipv6_to_ipv4(inner_payload.try_into()?, new_source, new_destination)?; + translate_ipv6_to_ipv4(&inner_payload.try_into()?, new_source, new_destination)?; let inner_payload: Vec = inner_payload.into(); // Build the new payload diff --git a/src/packet/xlat/icmp/type_code.rs b/src/packet/xlat/icmp/type_code.rs index 01d9e7d..8aead6c 100644 --- a/src/packet/xlat/icmp/type_code.rs +++ b/src/packet/xlat/icmp/type_code.rs @@ -1,5 +1,7 @@ //! Functions to map between ICMP and ICMPv6 types/codes +#![allow(clippy::doc_markdown)] + use pnet_packet::{ icmp::{destination_unreachable, IcmpCode, IcmpType, IcmpTypes}, icmpv6::{Icmpv6Code, Icmpv6Type, Icmpv6Types}, @@ -30,6 +32,7 @@ pub fn translate_type_and_code_4_to_6( (IcmpTypes::DestinationUnreachable, icmp_code) => Ok(( Icmpv6Types::DestinationUnreachable, #[cfg_attr(rustfmt, rustfmt_skip)] + #[allow(clippy::match_same_arms)] Icmpv6Code(match icmp_code { destination_unreachable::IcmpCodes::DestinationHostUnreachable => 3, destination_unreachable::IcmpCodes::DestinationProtocolUnreachable => 4, @@ -78,6 +81,7 @@ pub fn translate_type_and_code_6_to_4( (Icmpv6Types::DestinationUnreachable, icmp_code) => Ok(( IcmpTypes::DestinationUnreachable, #[cfg_attr(rustfmt, rustfmt_skip)] + #[allow(clippy::match_same_arms)] match icmp_code.0 { 1 => destination_unreachable::IcmpCodes::CommunicationAdministrativelyProhibited, 2 => destination_unreachable::IcmpCodes::SourceHostIsolated, diff --git a/src/packet/xlat/ip.rs b/src/packet/xlat/ip.rs index 793f282..ae9804f 100644 --- a/src/packet/xlat/ip.rs +++ b/src/packet/xlat/ip.rs @@ -70,7 +70,7 @@ pub fn translate_ipv4_to_ipv6( /// Translates an IPv6 packet to an IPv4 packet pub fn translate_ipv6_to_ipv4( - input: Ipv6Packet>, + input: &Ipv6Packet>, new_source: Ipv4Addr, new_destination: Ipv4Addr, ) -> Result>, PacketError> {