Fix all existing clippy warnings
This commit is contained in:
parent
9f4dc5a42f
commit
a9c4d499a7
@ -6,39 +6,36 @@ pub fn enable_logger(verbose: bool) {
|
|||||||
fern::Dispatch::new()
|
fern::Dispatch::new()
|
||||||
.format(move |out, message, record| {
|
.format(move |out, message, record| {
|
||||||
out.finish(format_args!(
|
out.finish(format_args!(
|
||||||
"{}: {}",
|
"{}{}: {}",
|
||||||
format!(
|
// Level messages are padded to keep the output looking somewhat sane
|
||||||
"{}{}",
|
match record.level() {
|
||||||
// Level messages are padded to keep the output looking somewhat sane
|
log::Level::Error => "ERROR"
|
||||||
match record.level() {
|
.if_supports_color(Stdout, |text| text.red())
|
||||||
log::Level::Error => "ERROR"
|
.if_supports_color(Stdout, |text| text.bold())
|
||||||
.if_supports_color(Stdout, |text| text.red())
|
.to_string(),
|
||||||
.if_supports_color(Stdout, |text| text.bold())
|
log::Level::Warn => "WARN "
|
||||||
.to_string(),
|
.if_supports_color(Stdout, |text| text.yellow())
|
||||||
log::Level::Warn => "WARN "
|
.if_supports_color(Stdout, |text| text.bold())
|
||||||
.if_supports_color(Stdout, |text| text.yellow())
|
.to_string(),
|
||||||
.if_supports_color(Stdout, |text| text.bold())
|
log::Level::Info => "INFO "
|
||||||
.to_string(),
|
.if_supports_color(Stdout, |text| text.green())
|
||||||
log::Level::Info => "INFO "
|
.if_supports_color(Stdout, |text| text.bold())
|
||||||
.if_supports_color(Stdout, |text| text.green())
|
.to_string(),
|
||||||
.if_supports_color(Stdout, |text| text.bold())
|
log::Level::Debug => "DEBUG"
|
||||||
.to_string(),
|
.if_supports_color(Stdout, |text| text.bright_blue())
|
||||||
log::Level::Debug => "DEBUG"
|
.if_supports_color(Stdout, |text| text.bold())
|
||||||
.if_supports_color(Stdout, |text| text.bright_blue())
|
.to_string(),
|
||||||
.if_supports_color(Stdout, |text| text.bold())
|
log::Level::Trace => "TRACE"
|
||||||
.to_string(),
|
.if_supports_color(Stdout, |text| text.bright_white())
|
||||||
log::Level::Trace => "TRACE"
|
.if_supports_color(Stdout, |text| text.bold())
|
||||||
.if_supports_color(Stdout, |text| text.bright_white())
|
.to_string(),
|
||||||
.if_supports_color(Stdout, |text| text.bold())
|
},
|
||||||
.to_string(),
|
// Only show the outer package name if verbose logging is enabled (otherwise nothing)
|
||||||
},
|
match verbose {
|
||||||
// Only show the outer package name if verbose logging is enabled (otherwise nothing)
|
true => format!(" [{}]", record.target().split("::").next().unwrap()),
|
||||||
match verbose {
|
false => String::new(),
|
||||||
true => format!(" [{}]", record.target().split("::").nth(0).unwrap()),
|
}
|
||||||
false => String::new(),
|
.if_supports_color(Stdout, |text| text.bright_black()),
|
||||||
}
|
|
||||||
.if_supports_color(Stdout, |text| text.bright_black())
|
|
||||||
),
|
|
||||||
message
|
message
|
||||||
))
|
))
|
||||||
})
|
})
|
||||||
|
@ -2,8 +2,6 @@
|
|||||||
//!
|
//!
|
||||||
//! *Note: There is a fair chance you are looking for `src/cli/main.rs` instead of this file.*
|
//! *Note: There is a fair chance you are looking for `src/cli/main.rs` instead of this file.*
|
||||||
|
|
||||||
#[warn(clippy::deprecated_cfg_attr)]
|
pub mod metrics;
|
||||||
|
|
||||||
pub mod nat;
|
pub mod nat;
|
||||||
mod packet;
|
mod packet;
|
||||||
pub mod metrics;
|
|
@ -1,5 +1,6 @@
|
|||||||
mod http;
|
mod http;
|
||||||
|
#[allow(clippy::module_inception)]
|
||||||
mod metrics;
|
mod metrics;
|
||||||
|
|
||||||
pub(crate) use metrics::*;
|
|
||||||
pub use http::serve_metrics;
|
pub use http::serve_metrics;
|
||||||
|
pub(crate) use metrics::*;
|
||||||
|
@ -1,15 +1,15 @@
|
|||||||
#[derive(Debug, thiserror::Error)]
|
#[derive(Debug, thiserror::Error)]
|
||||||
pub enum Nat64Error {
|
pub enum Nat64Error {
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
TableError(#[from] super::table::TableError),
|
Table(#[from] super::table::TableError),
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
TunError(#[from] protomask_tun::Error),
|
Tun(#[from] protomask_tun::Error),
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
IoError(#[from] std::io::Error),
|
Io(#[from] std::io::Error),
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
PacketHandlingError(#[from] crate::packet::error::PacketError),
|
PacketHandling(#[from] crate::packet::error::PacketError),
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
PacketReceiveError(#[from] tokio::sync::broadcast::error::RecvError),
|
PacketReceive(#[from] tokio::sync::broadcast::error::RecvError),
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
PacketSendError(#[from] tokio::sync::mpsc::error::SendError<Vec<u8>>),
|
PacketSend(#[from] tokio::sync::mpsc::error::SendError<Vec<u8>>),
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ use std::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use bimap::BiHashMap;
|
use bimap::BiHashMap;
|
||||||
use ipnet::{Ipv4Net, Ipv6Net};
|
use ipnet::Ipv4Net;
|
||||||
|
|
||||||
use crate::metrics::{IPV4_POOL_RESERVED, IPV4_POOL_SIZE};
|
use crate::metrics::{IPV4_POOL_RESERVED, IPV4_POOL_SIZE};
|
||||||
|
|
||||||
@ -136,61 +136,6 @@ impl Nat64Table {
|
|||||||
// Otherwise, there is no matching reservation
|
// Otherwise, there is no matching reservation
|
||||||
Err(TableError::NoIpv6Mapping(ipv4))
|
Err(TableError::NoIpv6Mapping(ipv4))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check if an address is within the IPv4 pool
|
|
||||||
pub fn is_address_within_pool(&self, address: &Ipv4Addr) -> bool {
|
|
||||||
self.ipv4_pool.iter().any(|net| net.contains(address))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Calculate the translated version of any address
|
|
||||||
pub fn calculate_xlat_addr(
|
|
||||||
&mut self,
|
|
||||||
input: &IpAddr,
|
|
||||||
ipv6_nat64_prefix: &Ipv6Net,
|
|
||||||
) -> Result<IpAddr, TableError> {
|
|
||||||
// Handle the incoming address type
|
|
||||||
match input {
|
|
||||||
// Handle IPv4
|
|
||||||
IpAddr::V4(ipv4_addr) => {
|
|
||||||
// If the address is in the IPv4 pool, it is a regular IPv4 address
|
|
||||||
if self.is_address_within_pool(ipv4_addr) {
|
|
||||||
// This means we need to pass through to `get_reverse`
|
|
||||||
return Ok(IpAddr::V6(self.get_reverse(*ipv4_addr)?));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Otherwise, it shall be embedded inside the ipv6 prefix
|
|
||||||
let prefix_octets = ipv6_nat64_prefix.addr().octets();
|
|
||||||
let address_octets = ipv4_addr.octets();
|
|
||||||
return Ok(IpAddr::V6(Ipv6Addr::new(
|
|
||||||
u16::from_be_bytes([prefix_octets[0], prefix_octets[1]]),
|
|
||||||
u16::from_be_bytes([prefix_octets[2], prefix_octets[3]]),
|
|
||||||
u16::from_be_bytes([prefix_octets[4], prefix_octets[5]]),
|
|
||||||
u16::from_be_bytes([prefix_octets[6], prefix_octets[7]]),
|
|
||||||
u16::from_be_bytes([prefix_octets[8], prefix_octets[9]]),
|
|
||||||
u16::from_be_bytes([prefix_octets[10], prefix_octets[11]]),
|
|
||||||
u16::from_be_bytes([address_octets[0], address_octets[1]]),
|
|
||||||
u16::from_be_bytes([address_octets[2], address_octets[3]]),
|
|
||||||
)));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle IPv6
|
|
||||||
IpAddr::V6(ipv6_addr) => {
|
|
||||||
// If the address is in the IPv6 prefix, it is an embedded IPv4 address
|
|
||||||
if ipv6_nat64_prefix.contains(ipv6_addr) {
|
|
||||||
let address_bytes = ipv6_addr.octets();
|
|
||||||
return Ok(IpAddr::V4(Ipv4Addr::new(
|
|
||||||
address_bytes[12],
|
|
||||||
address_bytes[13],
|
|
||||||
address_bytes[14],
|
|
||||||
address_bytes[15],
|
|
||||||
)));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Otherwise, it is a regular IPv6 address and we can pass through to `get_or_assign_ipv4`
|
|
||||||
return Ok(IpAddr::V4(self.get_or_assign_ipv4(*ipv6_addr)?));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Nat64Table {
|
impl Nat64Table {
|
||||||
@ -200,16 +145,12 @@ impl Nat64Table {
|
|||||||
|
|
||||||
// Prune from the reservation map
|
// Prune from the reservation map
|
||||||
self.reservations.retain(|v6, v4| {
|
self.reservations.retain(|v6, v4| {
|
||||||
if let Some(time) = self.reservation_times.get(&(*v6, *v4)) {
|
if let Some(Some(time)) = self.reservation_times.get(&(*v6, *v4)) {
|
||||||
if let Some(time) = time {
|
let keep = now - *time < self.reservation_timeout;
|
||||||
let keep = now - *time < self.reservation_timeout;
|
if !keep {
|
||||||
if !keep {
|
log::info!("Pruned reservation: {} -> {}", v6, v4);
|
||||||
log::info!("Pruned reservation: {} -> {}", v6, v4);
|
|
||||||
}
|
|
||||||
keep
|
|
||||||
} else {
|
|
||||||
true
|
|
||||||
}
|
}
|
||||||
|
keep
|
||||||
} else {
|
} else {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
@ -43,13 +43,13 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Into<Vec<u8>> for IcmpPacket<T>
|
impl<T> From<IcmpPacket<T>> for Vec<u8>
|
||||||
where
|
where
|
||||||
T: Into<Vec<u8>>,
|
T: Into<Vec<u8>>,
|
||||||
{
|
{
|
||||||
fn into(self) -> Vec<u8> {
|
fn from(packet: IcmpPacket<T>) -> Self {
|
||||||
// Convert the payload into raw bytes
|
// Convert the payload into raw bytes
|
||||||
let payload: Vec<u8> = self.payload.into();
|
let payload: Vec<u8> = packet.payload.into();
|
||||||
|
|
||||||
// Allocate a mutable packet to write into
|
// Allocate a mutable packet to write into
|
||||||
let total_length =
|
let total_length =
|
||||||
@ -58,8 +58,8 @@ where
|
|||||||
pnet_packet::icmp::MutableIcmpPacket::owned(vec![0u8; total_length]).unwrap();
|
pnet_packet::icmp::MutableIcmpPacket::owned(vec![0u8; total_length]).unwrap();
|
||||||
|
|
||||||
// Write the type and code
|
// Write the type and code
|
||||||
output.set_icmp_type(self.icmp_type);
|
output.set_icmp_type(packet.icmp_type);
|
||||||
output.set_icmp_code(self.icmp_code);
|
output.set_icmp_code(packet.icmp_code);
|
||||||
|
|
||||||
// Write the payload
|
// Write the payload
|
||||||
output.set_payload(&payload);
|
output.set_payload(&payload);
|
||||||
|
@ -85,13 +85,13 @@ impl Icmpv6Packet<RawBytes> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Into<Vec<u8>> for Icmpv6Packet<T>
|
impl<T> From<Icmpv6Packet<T>> for Vec<u8>
|
||||||
where
|
where
|
||||||
T: Into<Vec<u8>>,
|
T: Into<Vec<u8>>,
|
||||||
{
|
{
|
||||||
fn into(self) -> Vec<u8> {
|
fn from(packet: Icmpv6Packet<T>) -> Self {
|
||||||
// Convert the payload into raw bytes
|
// Convert the payload into raw bytes
|
||||||
let payload: Vec<u8> = self.payload.into();
|
let payload: Vec<u8> = packet.payload.into();
|
||||||
|
|
||||||
// Allocate a mutable packet to write into
|
// Allocate a mutable packet to write into
|
||||||
let total_length =
|
let total_length =
|
||||||
@ -100,8 +100,8 @@ where
|
|||||||
pnet_packet::icmpv6::MutableIcmpv6Packet::owned(vec![0u8; total_length]).unwrap();
|
pnet_packet::icmpv6::MutableIcmpv6Packet::owned(vec![0u8; total_length]).unwrap();
|
||||||
|
|
||||||
// Write the type and code
|
// Write the type and code
|
||||||
output.set_icmpv6_type(self.icmp_type);
|
output.set_icmpv6_type(packet.icmp_type);
|
||||||
output.set_icmpv6_code(self.icmp_code);
|
output.set_icmpv6_code(packet.icmp_code);
|
||||||
|
|
||||||
// Write the payload
|
// Write the payload
|
||||||
output.set_payload(&payload);
|
output.set_payload(&payload);
|
||||||
@ -110,8 +110,8 @@ where
|
|||||||
output.set_checksum(0);
|
output.set_checksum(0);
|
||||||
output.set_checksum(pnet_packet::icmpv6::checksum(
|
output.set_checksum(pnet_packet::icmpv6::checksum(
|
||||||
&output.to_immutable(),
|
&output.to_immutable(),
|
||||||
&self.source_address,
|
&packet.source_address,
|
||||||
&self.destination_address,
|
&packet.destination_address,
|
||||||
));
|
));
|
||||||
|
|
||||||
// Return the raw bytes
|
// Return the raw bytes
|
||||||
|
@ -25,6 +25,7 @@ pub struct Ipv4Packet<T> {
|
|||||||
|
|
||||||
impl<T> Ipv4Packet<T> {
|
impl<T> Ipv4Packet<T> {
|
||||||
/// Construct a new IPv4 packet
|
/// Construct a new IPv4 packet
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
pub fn new(
|
pub fn new(
|
||||||
dscp: u8,
|
dscp: u8,
|
||||||
ecn: u8,
|
ecn: u8,
|
||||||
@ -70,8 +71,8 @@ where
|
|||||||
|
|
||||||
fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
|
fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
|
||||||
// Parse the packet
|
// Parse the packet
|
||||||
let packet =
|
let packet = pnet_packet::ipv4::Ipv4Packet::new(&bytes)
|
||||||
pnet_packet::ipv4::Ipv4Packet::new(&bytes).ok_or(PacketError::TooShort(bytes.len(), bytes.to_vec()))?;
|
.ok_or(PacketError::TooShort(bytes.len(), bytes.to_vec()))?;
|
||||||
|
|
||||||
// Return the packet
|
// Return the packet
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
@ -90,42 +91,42 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Into<Vec<u8>> for Ipv4Packet<T>
|
impl<T> From<Ipv4Packet<T>> for Vec<u8>
|
||||||
where
|
where
|
||||||
T: Into<Vec<u8>> + Clone,
|
T: Into<Vec<u8>> + Clone,
|
||||||
{
|
{
|
||||||
fn into(self) -> Vec<u8> {
|
fn from(packet: Ipv4Packet<T>) -> Self {
|
||||||
// Convert the payload into raw bytes
|
// Convert the payload into raw bytes
|
||||||
let payload: Vec<u8> = self.payload.clone().into();
|
let payload: Vec<u8> = packet.payload.clone().into();
|
||||||
|
|
||||||
// Build the packet
|
// Build the packet
|
||||||
let total_length = 20 + (self.options_length_words() as usize * 4) + payload.len();
|
let total_length = 20 + (packet.options_length_words() as usize * 4) + payload.len();
|
||||||
let mut packet =
|
let mut output =
|
||||||
pnet_packet::ipv4::MutableIpv4Packet::owned(vec![0u8; total_length]).unwrap();
|
pnet_packet::ipv4::MutableIpv4Packet::owned(vec![0u8; total_length]).unwrap();
|
||||||
|
|
||||||
// Set the fields
|
// Set the fields
|
||||||
packet.set_version(4);
|
output.set_version(4);
|
||||||
packet.set_header_length(5 + self.options_length_words());
|
output.set_header_length(5 + packet.options_length_words());
|
||||||
packet.set_dscp(self.dscp);
|
output.set_dscp(packet.dscp);
|
||||||
packet.set_ecn(self.ecn);
|
output.set_ecn(packet.ecn);
|
||||||
packet.set_total_length(total_length.try_into().unwrap());
|
output.set_total_length(total_length.try_into().unwrap());
|
||||||
packet.set_identification(self.identification);
|
output.set_identification(packet.identification);
|
||||||
packet.set_flags(self.flags);
|
output.set_flags(packet.flags);
|
||||||
packet.set_fragment_offset(self.fragment_offset);
|
output.set_fragment_offset(packet.fragment_offset);
|
||||||
packet.set_ttl(self.ttl);
|
output.set_ttl(packet.ttl);
|
||||||
packet.set_next_level_protocol(self.protocol);
|
output.set_next_level_protocol(packet.protocol);
|
||||||
packet.set_source(self.source_address);
|
output.set_source(packet.source_address);
|
||||||
packet.set_destination(self.destination_address);
|
output.set_destination(packet.destination_address);
|
||||||
packet.set_options(&self.options);
|
output.set_options(&packet.options);
|
||||||
|
|
||||||
// Set the payload
|
// Set the payload
|
||||||
packet.set_payload(&payload);
|
output.set_payload(&payload);
|
||||||
|
|
||||||
// Calculate the checksum
|
// Calculate the checksum
|
||||||
packet.set_checksum(0);
|
output.set_checksum(0);
|
||||||
packet.set_checksum(pnet_packet::ipv4::checksum(&packet.to_immutable()));
|
output.set_checksum(pnet_packet::ipv4::checksum(&output.to_immutable()));
|
||||||
|
|
||||||
// Return the packet
|
// Return the packet
|
||||||
packet.to_immutable().packet().to_vec()
|
output.to_immutable().packet().to_vec()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -46,8 +46,8 @@ where
|
|||||||
|
|
||||||
fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
|
fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
|
||||||
// Parse the packet
|
// Parse the packet
|
||||||
let packet =
|
let packet = pnet_packet::ipv6::Ipv6Packet::new(&bytes)
|
||||||
pnet_packet::ipv6::Ipv6Packet::new(&bytes).ok_or(PacketError::TooShort(bytes.len(), bytes.to_vec()))?;
|
.ok_or(PacketError::TooShort(bytes.len(), bytes.to_vec()))?;
|
||||||
|
|
||||||
// Return the packet
|
// Return the packet
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
@ -62,13 +62,13 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Into<Vec<u8>> for Ipv6Packet<T>
|
impl<T> From<Ipv6Packet<T>> for Vec<u8>
|
||||||
where
|
where
|
||||||
T: Into<Vec<u8>>,
|
T: Into<Vec<u8>>,
|
||||||
{
|
{
|
||||||
fn into(self) -> Vec<u8> {
|
fn from(packet: Ipv6Packet<T>) -> Self {
|
||||||
// Convert the payload into raw bytes
|
// Convert the payload into raw bytes
|
||||||
let payload: Vec<u8> = self.payload.into();
|
let payload: Vec<u8> = packet.payload.into();
|
||||||
|
|
||||||
// Allocate a mutable packet to write into
|
// Allocate a mutable packet to write into
|
||||||
let total_length =
|
let total_length =
|
||||||
@ -78,13 +78,13 @@ where
|
|||||||
|
|
||||||
// Write the header
|
// Write the header
|
||||||
output.set_version(6);
|
output.set_version(6);
|
||||||
output.set_traffic_class(self.traffic_class);
|
output.set_traffic_class(packet.traffic_class);
|
||||||
output.set_flow_label(self.flow_label);
|
output.set_flow_label(packet.flow_label);
|
||||||
output.set_payload_length(payload.len() as u16);
|
output.set_payload_length(payload.len() as u16);
|
||||||
output.set_next_header(self.next_header);
|
output.set_next_header(packet.next_header);
|
||||||
output.set_hop_limit(self.hop_limit);
|
output.set_hop_limit(packet.hop_limit);
|
||||||
output.set_source(self.source_address);
|
output.set_source(packet.source_address);
|
||||||
output.set_destination(self.destination_address);
|
output.set_destination(packet.destination_address);
|
||||||
|
|
||||||
// Write the payload
|
// Write the payload
|
||||||
output.set_payload(&payload);
|
output.set_payload(&payload);
|
||||||
|
@ -11,8 +11,8 @@ impl TryFrom<Vec<u8>> for RawBytes {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Into<Vec<u8>> for RawBytes {
|
impl From<RawBytes> for Vec<u8> {
|
||||||
fn into(self) -> Vec<u8> {
|
fn from(val: RawBytes) -> Self {
|
||||||
self.0
|
val.0
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -24,6 +24,7 @@ pub struct TcpPacket<T> {
|
|||||||
|
|
||||||
impl<T> TcpPacket<T> {
|
impl<T> TcpPacket<T> {
|
||||||
/// Construct a new TCP packet
|
/// Construct a new TCP packet
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
pub fn new(
|
pub fn new(
|
||||||
source: SocketAddr,
|
source: SocketAddr,
|
||||||
destination: SocketAddr,
|
destination: SocketAddr,
|
||||||
@ -105,7 +106,7 @@ impl<T> TcpPacket<T> {
|
|||||||
fn options_length(&self) -> usize {
|
fn options_length(&self) -> usize {
|
||||||
self.options
|
self.options
|
||||||
.iter()
|
.iter()
|
||||||
.map(|option| TcpOptionPacket::packet_size(option))
|
.map(TcpOptionPacket::packet_size)
|
||||||
.sum::<usize>()
|
.sum::<usize>()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -139,7 +140,7 @@ where
|
|||||||
destination: SocketAddr::new(destination_address, parsed.get_destination()),
|
destination: SocketAddr::new(destination_address, parsed.get_destination()),
|
||||||
sequence: parsed.get_sequence(),
|
sequence: parsed.get_sequence(),
|
||||||
ack_number: parsed.get_acknowledgement(),
|
ack_number: parsed.get_acknowledgement(),
|
||||||
flags: parsed.get_flags() as u8,
|
flags: parsed.get_flags(),
|
||||||
window_size: parsed.get_window(),
|
window_size: parsed.get_window(),
|
||||||
urgent_pointer: parsed.get_urgent_ptr(),
|
urgent_pointer: parsed.get_urgent_ptr(),
|
||||||
options: parsed.get_options().to_vec(),
|
options: parsed.get_options().to_vec(),
|
||||||
@ -173,7 +174,7 @@ impl TcpPacket<RawBytes> {
|
|||||||
destination: SocketAddr::new(destination_address, parsed.get_destination()),
|
destination: SocketAddr::new(destination_address, parsed.get_destination()),
|
||||||
sequence: parsed.get_sequence(),
|
sequence: parsed.get_sequence(),
|
||||||
ack_number: parsed.get_acknowledgement(),
|
ack_number: parsed.get_acknowledgement(),
|
||||||
flags: parsed.get_flags() as u8,
|
flags: parsed.get_flags(),
|
||||||
window_size: parsed.get_window(),
|
window_size: parsed.get_window(),
|
||||||
urgent_pointer: parsed.get_urgent_ptr(),
|
urgent_pointer: parsed.get_urgent_ptr(),
|
||||||
options: parsed.get_options().to_vec(),
|
options: parsed.get_options().to_vec(),
|
||||||
@ -182,16 +183,16 @@ impl TcpPacket<RawBytes> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Into<Vec<u8>> for TcpPacket<T>
|
impl<T> From<TcpPacket<T>> for Vec<u8>
|
||||||
where
|
where
|
||||||
T: Into<Vec<u8>>,
|
T: Into<Vec<u8>>,
|
||||||
{
|
{
|
||||||
fn into(self) -> Vec<u8> {
|
fn from(packet: TcpPacket<T>) -> Self {
|
||||||
// Get the options length in words
|
// Get the options length in words
|
||||||
let options_length = self.options_length();
|
let options_length = packet.options_length();
|
||||||
|
|
||||||
// Convert the payload into raw bytes
|
// Convert the payload into raw bytes
|
||||||
let payload: Vec<u8> = self.payload.into();
|
let payload: Vec<u8> = packet.payload.into();
|
||||||
|
|
||||||
// Allocate a mutable packet to write into
|
// Allocate a mutable packet to write into
|
||||||
let total_length = pnet_packet::tcp::MutableTcpPacket::minimum_packet_size()
|
let total_length = pnet_packet::tcp::MutableTcpPacket::minimum_packet_size()
|
||||||
@ -201,34 +202,34 @@ where
|
|||||||
pnet_packet::tcp::MutableTcpPacket::owned(vec![0u8; total_length]).unwrap();
|
pnet_packet::tcp::MutableTcpPacket::owned(vec![0u8; total_length]).unwrap();
|
||||||
|
|
||||||
// Write the source and dest ports
|
// Write the source and dest ports
|
||||||
output.set_source(self.source.port());
|
output.set_source(packet.source.port());
|
||||||
output.set_destination(self.destination.port());
|
output.set_destination(packet.destination.port());
|
||||||
|
|
||||||
// Write the sequence and ack numbers
|
// Write the sequence and ack numbers
|
||||||
output.set_sequence(self.sequence);
|
output.set_sequence(packet.sequence);
|
||||||
output.set_acknowledgement(self.ack_number);
|
output.set_acknowledgement(packet.ack_number);
|
||||||
|
|
||||||
// Write the offset
|
// Write the offset
|
||||||
output.set_data_offset(5 + (options_length / 4) as u8);
|
output.set_data_offset(5 + (options_length / 4) as u8);
|
||||||
|
|
||||||
// Write the options
|
// Write the options
|
||||||
output.set_options(&self.options);
|
output.set_options(&packet.options);
|
||||||
|
|
||||||
// Write the flags
|
// Write the flags
|
||||||
output.set_flags(self.flags.into());
|
output.set_flags(packet.flags);
|
||||||
|
|
||||||
// Write the window size
|
// Write the window size
|
||||||
output.set_window(self.window_size);
|
output.set_window(packet.window_size);
|
||||||
|
|
||||||
// Write the urgent pointer
|
// Write the urgent pointer
|
||||||
output.set_urgent_ptr(self.urgent_pointer);
|
output.set_urgent_ptr(packet.urgent_pointer);
|
||||||
|
|
||||||
// Write the payload
|
// Write the payload
|
||||||
output.set_payload(&payload);
|
output.set_payload(&payload);
|
||||||
|
|
||||||
// Calculate the checksum
|
// Calculate the checksum
|
||||||
output.set_checksum(0);
|
output.set_checksum(0);
|
||||||
output.set_checksum(match (self.source.ip(), self.destination.ip()) {
|
output.set_checksum(match (packet.source.ip(), packet.destination.ip()) {
|
||||||
(IpAddr::V4(source_ip), IpAddr::V4(destination_ip)) => {
|
(IpAddr::V4(source_ip), IpAddr::V4(destination_ip)) => {
|
||||||
pnet_packet::tcp::ipv4_checksum(&output.to_immutable(), &source_ip, &destination_ip)
|
pnet_packet::tcp::ipv4_checksum(&output.to_immutable(), &source_ip, &destination_ip)
|
||||||
}
|
}
|
||||||
|
@ -141,13 +141,13 @@ impl UdpPacket<RawBytes> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Into<Vec<u8>> for UdpPacket<T>
|
impl<T> From<UdpPacket<T>> for Vec<u8>
|
||||||
where
|
where
|
||||||
T: Into<Vec<u8>>,
|
T: Into<Vec<u8>>,
|
||||||
{
|
{
|
||||||
fn into(self) -> Vec<u8> {
|
fn from(packet: UdpPacket<T>) -> Self {
|
||||||
// Convert the payload into raw bytes
|
// Convert the payload into raw bytes
|
||||||
let payload: Vec<u8> = self.payload.into();
|
let payload: Vec<u8> = packet.payload.into();
|
||||||
|
|
||||||
// Allocate a mutable packet to write into
|
// Allocate a mutable packet to write into
|
||||||
let total_length =
|
let total_length =
|
||||||
@ -156,8 +156,8 @@ where
|
|||||||
pnet_packet::udp::MutableUdpPacket::owned(vec![0u8; total_length]).unwrap();
|
pnet_packet::udp::MutableUdpPacket::owned(vec![0u8; total_length]).unwrap();
|
||||||
|
|
||||||
// Write the source and dest ports
|
// Write the source and dest ports
|
||||||
output.set_source(self.source.port());
|
output.set_source(packet.source.port());
|
||||||
output.set_destination(self.destination.port());
|
output.set_destination(packet.destination.port());
|
||||||
|
|
||||||
// Write the length
|
// Write the length
|
||||||
output.set_length(total_length as u16);
|
output.set_length(total_length as u16);
|
||||||
@ -167,7 +167,7 @@ where
|
|||||||
|
|
||||||
// Calculate the checksum
|
// Calculate the checksum
|
||||||
output.set_checksum(0);
|
output.set_checksum(0);
|
||||||
output.set_checksum(match (self.source.ip(), self.destination.ip()) {
|
output.set_checksum(match (packet.source.ip(), packet.destination.ip()) {
|
||||||
(IpAddr::V4(source_ip), IpAddr::V4(destination_ip)) => {
|
(IpAddr::V4(source_ip), IpAddr::V4(destination_ip)) => {
|
||||||
pnet_packet::udp::ipv4_checksum(&output.to_immutable(), &source_ip, &destination_ip)
|
pnet_packet::udp::ipv4_checksum(&output.to_immutable(), &source_ip, &destination_ip)
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ use pnet_packet::{
|
|||||||
use crate::packet::error::PacketError;
|
use crate::packet::error::PacketError;
|
||||||
|
|
||||||
/// Best effort translation from an ICMP type and code to an ICMPv6 type and code
|
/// Best effort translation from an ICMP type and code to an ICMPv6 type and code
|
||||||
|
#[allow(clippy::deprecated_cfg_attr)]
|
||||||
pub fn translate_type_and_code_4_to_6(
|
pub fn translate_type_and_code_4_to_6(
|
||||||
icmp_type: IcmpType,
|
icmp_type: IcmpType,
|
||||||
icmp_code: IcmpCode,
|
icmp_code: IcmpCode,
|
||||||
@ -55,6 +56,7 @@ pub fn translate_type_and_code_4_to_6(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Best effort translation from an ICMPv6 type and code to an ICMP type and code
|
/// Best effort translation from an ICMPv6 type and code to an ICMP type and code
|
||||||
|
#[allow(clippy::deprecated_cfg_attr)]
|
||||||
pub fn translate_type_and_code_6_to_4(
|
pub fn translate_type_and_code_6_to_4(
|
||||||
icmp_type: Icmpv6Type,
|
icmp_type: Icmpv6Type,
|
||||||
icmp_code: Icmpv6Code,
|
icmp_code: Icmpv6Code,
|
||||||
|
@ -121,7 +121,7 @@ pub fn translate_ipv6_to_ipv4(
|
|||||||
new_source,
|
new_source,
|
||||||
new_destination,
|
new_destination,
|
||||||
vec![],
|
vec![],
|
||||||
new_payload.unwrap_or_else(Vec::new),
|
new_payload.unwrap_or_default(),
|
||||||
);
|
);
|
||||||
|
|
||||||
// Return the output
|
// Return the output
|
||||||
|
@ -12,7 +12,7 @@ pub fn translate_tcp4_to_tcp6(
|
|||||||
new_destination_addr: Ipv6Addr,
|
new_destination_addr: Ipv6Addr,
|
||||||
) -> Result<TcpPacket<RawBytes>, PacketError> {
|
) -> Result<TcpPacket<RawBytes>, PacketError> {
|
||||||
// Build the packet
|
// Build the packet
|
||||||
Ok(TcpPacket::new(
|
TcpPacket::new(
|
||||||
SocketAddr::new(IpAddr::V6(new_source_addr), input.source().port()),
|
SocketAddr::new(IpAddr::V6(new_source_addr), input.source().port()),
|
||||||
SocketAddr::new(IpAddr::V6(new_destination_addr), input.destination().port()),
|
SocketAddr::new(IpAddr::V6(new_destination_addr), input.destination().port()),
|
||||||
input.sequence,
|
input.sequence,
|
||||||
@ -22,7 +22,7 @@ pub fn translate_tcp4_to_tcp6(
|
|||||||
input.urgent_pointer,
|
input.urgent_pointer,
|
||||||
input.options,
|
input.options,
|
||||||
input.payload,
|
input.payload,
|
||||||
)?)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Translates an IPv6 TCP packet to an IPv4 TCP packet
|
/// Translates an IPv6 TCP packet to an IPv4 TCP packet
|
||||||
@ -32,7 +32,7 @@ pub fn translate_tcp6_to_tcp4(
|
|||||||
new_destination_addr: Ipv4Addr,
|
new_destination_addr: Ipv4Addr,
|
||||||
) -> Result<TcpPacket<RawBytes>, PacketError> {
|
) -> Result<TcpPacket<RawBytes>, PacketError> {
|
||||||
// Build the packet
|
// Build the packet
|
||||||
Ok(TcpPacket::new(
|
TcpPacket::new(
|
||||||
SocketAddr::new(IpAddr::V4(new_source_addr), input.source().port()),
|
SocketAddr::new(IpAddr::V4(new_source_addr), input.source().port()),
|
||||||
SocketAddr::new(IpAddr::V4(new_destination_addr), input.destination().port()),
|
SocketAddr::new(IpAddr::V4(new_destination_addr), input.destination().port()),
|
||||||
input.sequence,
|
input.sequence,
|
||||||
@ -42,7 +42,7 @@ pub fn translate_tcp6_to_tcp4(
|
|||||||
input.urgent_pointer,
|
input.urgent_pointer,
|
||||||
input.options,
|
input.options,
|
||||||
input.payload,
|
input.payload,
|
||||||
)?)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -12,11 +12,11 @@ pub fn translate_udp4_to_udp6(
|
|||||||
new_destination_addr: Ipv6Addr,
|
new_destination_addr: Ipv6Addr,
|
||||||
) -> Result<UdpPacket<RawBytes>, PacketError> {
|
) -> Result<UdpPacket<RawBytes>, PacketError> {
|
||||||
// Build the packet
|
// Build the packet
|
||||||
Ok(UdpPacket::new(
|
UdpPacket::new(
|
||||||
SocketAddr::new(IpAddr::V6(new_source_addr), input.source().port()),
|
SocketAddr::new(IpAddr::V6(new_source_addr), input.source().port()),
|
||||||
SocketAddr::new(IpAddr::V6(new_destination_addr), input.destination().port()),
|
SocketAddr::new(IpAddr::V6(new_destination_addr), input.destination().port()),
|
||||||
input.payload,
|
input.payload,
|
||||||
)?)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Translates an IPv6 UDP packet to an IPv4 UDP packet
|
/// Translates an IPv6 UDP packet to an IPv4 UDP packet
|
||||||
@ -26,11 +26,11 @@ pub fn translate_udp6_to_udp4(
|
|||||||
new_destination_addr: Ipv4Addr,
|
new_destination_addr: Ipv4Addr,
|
||||||
) -> Result<UdpPacket<RawBytes>, PacketError> {
|
) -> Result<UdpPacket<RawBytes>, PacketError> {
|
||||||
// Build the packet
|
// Build the packet
|
||||||
Ok(UdpPacket::new(
|
UdpPacket::new(
|
||||||
SocketAddr::new(IpAddr::V4(new_source_addr), input.source().port()),
|
SocketAddr::new(IpAddr::V4(new_source_addr), input.source().port()),
|
||||||
SocketAddr::new(IpAddr::V4(new_destination_addr), input.destination().port()),
|
SocketAddr::new(IpAddr::V4(new_destination_addr), input.destination().port()),
|
||||||
input.payload,
|
input.payload,
|
||||||
)?)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user