Skip to content

Commit

Permalink
Applying rust fmt & add return_buf to ip defrag pool
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianSchmid committed Sep 12, 2024
1 parent e48e5ca commit 0a10f3e
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 48 deletions.
22 changes: 15 additions & 7 deletions etherparse/src/defrag/ip_defrag_buf.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{*, defrag::*};
use crate::{defrag::*, *};
use std::vec::Vec;

/// Buffer to reconstruct a single fragmented IP packet.
Expand All @@ -18,7 +18,11 @@ pub struct IpDefragBuf {
}

impl IpDefragBuf {
pub fn new(ip_number: IpNumber, mut data: Vec<u8>, mut sections: Vec<IpFragRange>) -> IpDefragBuf {
pub fn new(
ip_number: IpNumber,
mut data: Vec<u8>,
mut sections: Vec<IpFragRange>,
) -> IpDefragBuf {
IpDefragBuf {
ip_number,
data: {
Expand Down Expand Up @@ -57,16 +61,16 @@ impl IpDefragBuf {
self.end
}

/// Add a IPv4 slice
/// Add a IPv4 slice
#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
pub fn add(
&mut self,
offset: IpFragOffset,
more_fragments: bool,
payload: &[u8]
payload: &[u8],
) -> Result<(), IpDefragError> {
use IpDefragError::*;

// validate lengths
let Ok(len_u16) = u16::try_from(payload.len()) else {
return Err(SegmentTooBig {
Expand Down Expand Up @@ -103,7 +107,7 @@ impl IpDefragBuf {
}
}

// get enough memory to store the de-fragmented
// get enough memory to store the de-fragmented
let required_len = usize::from(end);
if self.data.len() < required_len {
if self.data.capacity() < required_len
Expand Down Expand Up @@ -195,7 +199,11 @@ mod test {

#[test]
fn new() {
let actual = IpDefragBuf::new(IpNumber::UDP, vec![1], vec![IpFragRange{start: 0, end: 1}]);
let actual = IpDefragBuf::new(
IpNumber::UDP,
vec![1],
vec![IpFragRange { start: 0, end: 1 }],
);
assert_eq!(actual.ip_number(), IpNumber::UDP);
assert!(actual.data().is_empty());
assert!(actual.sections().is_empty());
Expand Down
28 changes: 19 additions & 9 deletions etherparse/src/defrag/ip_defrag_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use crate::*;
pub enum IpDefragError {
/// Error if a payload lenght of a IP Fragment packet is not a multiple of 16
/// and the "more fragments" flag is set.
UnalignedFragmentPayloadLen { offset: IpFragOffset, payload_len: usize },
UnalignedFragmentPayloadLen {
offset: IpFragOffset,
payload_len: usize,
},

/// Error if a segment is bigger then the maximum allowed size.
SegmentTooBig {
Expand Down Expand Up @@ -43,13 +46,16 @@ impl std::error::Error for IpDefragError {}

#[cfg(test)]
mod tests {
use super::*;
use super::IpDefragError::*;
use super::*;
use std::format;

#[test]
fn debug() {
let err = UnalignedFragmentPayloadLen { offset: IpFragOffset::try_new(0).unwrap(), payload_len: 16 };
let err = UnalignedFragmentPayloadLen {
offset: IpFragOffset::try_new(0).unwrap(),
payload_len: 16,
};
let _ = format!("{err:?}");
}

Expand All @@ -59,7 +65,10 @@ mod tests {
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

let err = UnalignedFragmentPayloadLen { offset: IpFragOffset::try_new(0).unwrap(), payload_len: 16 };
let err = UnalignedFragmentPayloadLen {
offset: IpFragOffset::try_new(0).unwrap(),
payload_len: 16,
};
assert_eq!(err, err.clone());
let hash_a = {
let mut hasher = DefaultHasher::new();
Expand Down Expand Up @@ -92,10 +101,11 @@ mod tests {
#[test]
fn source() {
use std::error::Error;
assert!(
UnalignedFragmentPayloadLen { offset: IpFragOffset::try_new(0).unwrap(), payload_len: 16 }
.source()
.is_none()
);
assert!(UnalignedFragmentPayloadLen {
offset: IpFragOffset::try_new(0).unwrap(),
payload_len: 16
}
.source()
.is_none());
}
}
4 changes: 1 addition & 3 deletions etherparse/src/defrag/ip_defrag_payload_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ mod test {
assert_eq!(
format!(
"IpDefragPayloadVec {{ ip_number: {:?}, len_source: {:?}, payload: {:?} }}",
s.ip_number,
s.len_source,
s.payload
s.ip_number, s.len_source, s.payload
),
format!("{:?}", s)
);
Expand Down
55 changes: 32 additions & 23 deletions etherparse/src/defrag/ip_defrag_pool.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{*, defrag::*};
use crate::{defrag::*, *};
use std::collections::HashMap;
use std::vec::Vec;

Expand Down Expand Up @@ -35,7 +35,6 @@ where
Timestamp: Sized + core::fmt::Debug + Clone,
CustomChannelId: Sized + core::fmt::Debug + Clone + core::hash::Hash + Eq + PartialEq,
{

pub fn new() -> IpDefragPool {
IpDefragPool {
active: HashMap::new(),
Expand All @@ -45,8 +44,12 @@ where
}

Check warning on line 44 in etherparse/src/defrag/ip_defrag_pool.rs

View check run for this annotation

Codecov / codecov/patch

etherparse/src/defrag/ip_defrag_pool.rs#L38-L44

Added lines #L38 - L44 were not covered by tests

/// Add data from a sliced packet.
pub fn process_sliced_packet(&mut self, slice: &SlicedPacket, timestamp: Timestamp, channel_id: CustomChannelId) -> Result<Option<IpDefragPayloadVec>, IpDefragError> {

pub fn process_sliced_packet(
&mut self,
slice: &SlicedPacket,
timestamp: Timestamp,
channel_id: CustomChannelId,
) -> Result<Option<IpDefragPayloadVec>, IpDefragError> {

Check warning on line 52 in etherparse/src/defrag/ip_defrag_pool.rs

View check run for this annotation

Codecov / codecov/patch

etherparse/src/defrag/ip_defrag_pool.rs#L47-L52

Added lines #L47 - L52 were not covered by tests
// extract the fragment related data and skip non-fragmented packets
let (frag_id, offset, more_fragments, payload, is_ipv4) = match &slice.net {
Some(NetSlice::Ipv4(ipv4)) => {
Expand All @@ -58,15 +61,18 @@ where

let (outer_vlan_id, inner_vlan_id) = match &slice.vlan {
Some(VlanSlice::SingleVlan(s)) => (Some(s.vlan_identifier()), None),
Some(VlanSlice::DoubleVlan(d)) => (Some(d.outer().vlan_identifier()), Some(d.inner().vlan_identifier())),
None => (None, None)
Some(VlanSlice::DoubleVlan(d)) => (
Some(d.outer().vlan_identifier()),
Some(d.inner().vlan_identifier()),
),
None => (None, None),

Check warning on line 68 in etherparse/src/defrag/ip_defrag_pool.rs

View check run for this annotation

Codecov / codecov/patch

etherparse/src/defrag/ip_defrag_pool.rs#L62-L68

Added lines #L62 - L68 were not covered by tests
};

(
IpFragId {
outer_vlan_id,
inner_vlan_id,
ip: IpFragVersionSpecId::Ipv4{
ip: IpFragVersionSpecId::Ipv4 {
source: header.source(),
destination: header.destination(),
identification: header.identification(),
Expand All @@ -77,7 +83,7 @@ where
header.fragments_offset(),
header.more_fragments(),
ipv4.payload(),
true
true,
)

Check warning on line 87 in etherparse/src/defrag/ip_defrag_pool.rs

View check run for this annotation

Codecov / codecov/patch

etherparse/src/defrag/ip_defrag_pool.rs#L71-L87

Added lines #L71 - L87 were not covered by tests
}
Some(NetSlice::Ipv6(ipv6)) => {
Expand Down Expand Up @@ -107,16 +113,19 @@ where

let (outer_vlan_id, inner_vlan_id) = match &slice.vlan {
Some(VlanSlice::SingleVlan(s)) => (Some(s.vlan_identifier()), None),
Some(VlanSlice::DoubleVlan(d)) => (Some(d.outer().vlan_identifier()), Some(d.inner().vlan_identifier())),
None => (None, None)
Some(VlanSlice::DoubleVlan(d)) => (
Some(d.outer().vlan_identifier()),
Some(d.inner().vlan_identifier()),
),
None => (None, None),

Check warning on line 120 in etherparse/src/defrag/ip_defrag_pool.rs

View check run for this annotation

Codecov / codecov/patch

etherparse/src/defrag/ip_defrag_pool.rs#L114-L120

Added lines #L114 - L120 were not covered by tests
};

// calculate frag id
(
IpFragId {
outer_vlan_id,
inner_vlan_id,
ip: IpFragVersionSpecId::Ipv6{
ip: IpFragVersionSpecId::Ipv6 {
source: ipv6.header().source(),
destination: ipv6.header().destination(),
identification: frag.identification,
Expand All @@ -127,7 +136,7 @@ where
frag.fragment_offset,
frag.more_fragments,
ipv6.payload(),
false
false,
)

Check warning on line 140 in etherparse/src/defrag/ip_defrag_pool.rs

View check run for this annotation

Codecov / codecov/patch

etherparse/src/defrag/ip_defrag_pool.rs#L124-L140

Added lines #L124 - L140 were not covered by tests
}
None => {
Expand All @@ -146,7 +155,7 @@ where
if buf.0.is_complete() {
let (defraged_payload, sections) = entry.remove().0.take_bufs();
self.finished_section_bufs.push(sections);
Ok(Some(IpDefragPayloadVec{
Ok(Some(IpDefragPayloadVec {
ip_number: payload.ip_number,
len_source: if is_ipv4 {
LenSource::Ipv4HeaderTotalLen

Check warning on line 161 in etherparse/src/defrag/ip_defrag_pool.rs

View check run for this annotation

Codecov / codecov/patch

etherparse/src/defrag/ip_defrag_pool.rs#L150-L161

Added lines #L150 - L161 were not covered by tests
Expand All @@ -164,7 +173,7 @@ where
d.clear();
d

Check warning on line 174 in etherparse/src/defrag/ip_defrag_pool.rs

View check run for this annotation

Codecov / codecov/patch

etherparse/src/defrag/ip_defrag_pool.rs#L171-L174

Added lines #L171 - L174 were not covered by tests
} else {
Vec::with_capacity(payload.payload.len()*2)
Vec::with_capacity(payload.payload.len() * 2)

Check warning on line 176 in etherparse/src/defrag/ip_defrag_pool.rs

View check run for this annotation

Codecov / codecov/patch

etherparse/src/defrag/ip_defrag_pool.rs#L176

Added line #L176 was not covered by tests
};
let sections = if let Some(mut s) = self.finished_section_bufs.pop() {
s.clear();
Expand All @@ -179,7 +188,7 @@ where
if defrag_buf.is_complete() {
let (defraged_payload, sections) = defrag_buf.take_bufs();
self.finished_section_bufs.push(sections);
Ok(Some(IpDefragPayloadVec{
Ok(Some(IpDefragPayloadVec {
ip_number: payload.ip_number,
len_source: if is_ipv4 {
LenSource::Ipv4HeaderTotalLen

Check warning on line 194 in etherparse/src/defrag/ip_defrag_pool.rs

View check run for this annotation

Codecov / codecov/patch

etherparse/src/defrag/ip_defrag_pool.rs#L188-L194

Added lines #L188 - L194 were not covered by tests
Expand All @@ -192,24 +201,24 @@ where
entry.insert((defrag_buf, timestamp));
Ok(None)

Check warning on line 202 in etherparse/src/defrag/ip_defrag_pool.rs

View check run for this annotation

Codecov / codecov/patch

etherparse/src/defrag/ip_defrag_pool.rs#L201-L202

Added lines #L201 - L202 were not covered by tests
}
},
}
Err(err) => {
// return the buffers
let (data_buf, sections) = defrag_buf.take_bufs();
self.finished_data_bufs.push(data_buf);
self.finished_section_bufs.push(sections);
Err(err)

Check warning on line 210 in etherparse/src/defrag/ip_defrag_pool.rs

View check run for this annotation

Codecov / codecov/patch

etherparse/src/defrag/ip_defrag_pool.rs#L205-L210

Added lines #L205 - L210 were not covered by tests
},
}
}
}
}
}

Check warning on line 215 in etherparse/src/defrag/ip_defrag_pool.rs

View check run for this annotation

Codecov / codecov/patch

etherparse/src/defrag/ip_defrag_pool.rs#L215

Added line #L215 was not covered by tests
}

/// Returns a buffer to the pool so it can be re-used.
pub fn return_buf(&mut self, buf: IpDefragPayloadVec) {
self.finished_data_bufs.push(buf.payload);
}

Check warning on line 220 in etherparse/src/defrag/ip_defrag_pool.rs

View check run for this annotation

Codecov / codecov/patch

etherparse/src/defrag/ip_defrag_pool.rs#L218-L220

Added lines #L218 - L220 were not covered by tests
}

#[cfg(test)]
mod test {



}
mod test {}
2 changes: 1 addition & 1 deletion etherparse/src/defrag/ip_frag_id.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{*, defrag::*};
use crate::{defrag::*, *};

/// Values identifying a fragmented packet.
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
Expand Down
1 change: 0 additions & 1 deletion etherparse/src/defrag/ip_frag_range.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/// Describing the range of reconstructed data.
#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq, Ord, PartialOrd)]
pub struct IpFragRange {
Expand Down
8 changes: 4 additions & 4 deletions etherparse/src/defrag/ip_frag_version_spec_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub enum IpFragVersionSpecId {
/// IPv4 specific data.
Ipv4{
source: [u8; 4],
Ipv4 {
source: [u8; 4],
destination: [u8; 4],
identification: u16,
},
/// IPv6 specific data.
Ipv6{
source: [u8; 16],
Ipv6 {
source: [u8; 16],
destination: [u8; 16],
identification: u32,
},
Expand Down

0 comments on commit 0a10f3e

Please sign in to comment.