Skip to content

Commit

Permalink
Reorder use/mod statements
Browse files Browse the repository at this point in the history
  • Loading branch information
arctic-alpaca committed Mar 4, 2024
1 parent 4f98f49 commit 828dc1f
Show file tree
Hide file tree
Showing 46 changed files with 142 additions and 114 deletions.
3 changes: 2 additions & 1 deletion benches/arp.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use criterion::{criterion_group, criterion_main, BatchSize, Criterion, Throughput};
use rand::{thread_rng, Rng};

use mutnet::addresses::ipv4::Ipv4Address;
use mutnet::addresses::mac::MacAddress;
use mutnet::arp::{Arp, ArpMethods, ArpMethodsMut, ParseArpError};
use mutnet::data_buffer::{BufferIntoInner, DataBuffer};
use mutnet::no_previous_header::NoPreviousHeader;
use mutnet::typed_protocol_headers::OperationCode;
use rand::{thread_rng, Rng};

// ARP for IPv4
#[rustfmt::skip]
Expand Down
1 change: 1 addition & 0 deletions benches/checksum.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use criterion::{criterion_group, criterion_main, BatchSize, Criterion, Throughput};

use mutnet::checksum::{internet_checksum, internet_checksum_up_to_64_bytes};

fn random_bytes<const SIZE: usize>() -> [u8; SIZE] {
Expand Down
1 change: 1 addition & 0 deletions benches/ethernet.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use criterion::{criterion_group, criterion_main, BatchSize, Criterion, Throughput};

use mutnet::data_buffer::{BufferIntoInner, DataBuffer, PayloadMut};
use mutnet::error::UnexpectedBufferEndError;
use mutnet::ethernet::{Eth, EthernetMethods, EthernetMethodsMut};
Expand Down
3 changes: 2 additions & 1 deletion benches/ipv4.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use criterion::{criterion_group, criterion_main, BatchSize, Criterion, Throughput};
use rand::{thread_rng, Rng};

use mutnet::data_buffer::{BufferIntoInner, DataBuffer, PayloadMut};
use mutnet::ipv4::{Ipv4, Ipv4Methods, Ipv4MethodsMut, ParseIpv4Error};
use mutnet::no_previous_header::NoPreviousHeader;
use mutnet::typed_protocol_headers::{Dscp, Ecn};
use rand::{thread_rng, Rng};

#[allow(clippy::unusual_byte_groupings)]
#[rustfmt::skip]
Expand Down
1 change: 1 addition & 0 deletions benches/ipv6.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use criterion::{criterion_group, criterion_main, BatchSize, Criterion, Throughput};

use mutnet::addresses::ipv6::Ipv6Addr;
use mutnet::data_buffer::{BufferIntoInner, DataBuffer, PayloadMut};
use mutnet::ipv6::{Ipv6, Ipv6Methods, Ipv6MethodsMut, ParseIpv6Error};
Expand Down
3 changes: 2 additions & 1 deletion benches/ipv6_extensions.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use criterion::{criterion_group, criterion_main, BatchSize, Criterion, Throughput};
use rand::{thread_rng, Rng};

use mutnet::data_buffer::DataBuffer;
use mutnet::ipv6_extensions::{Ipv6Extensions, ParseIpv6ExtensionsError};
use mutnet::no_previous_header::NoPreviousHeader;
use mutnet::typed_protocol_headers::Ipv6ExtensionType;
use rand::{thread_rng, Rng};

const MEM_TO_FILL: usize = 64;
const MAX_EXTENSIONS: usize = 16;
Expand Down
1 change: 1 addition & 0 deletions benches/tcp.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use criterion::{criterion_group, criterion_main, BatchSize, Criterion, Throughput};

use mutnet::data_buffer::{BufferIntoInner, DataBuffer, PayloadMut};
use mutnet::no_previous_header::NoPreviousHeader;
use mutnet::tcp::{ParseTcpError, Tcp, TcpMethods, TcpMethodsMut};
Expand Down
3 changes: 2 additions & 1 deletion benches/udp.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use criterion::{criterion_group, criterion_main, BatchSize, Criterion, Throughput};
use rand::{thread_rng, Rng};

use mutnet::data_buffer::{BufferIntoInner, DataBuffer, PayloadMut};
use mutnet::no_previous_header::NoPreviousHeader;
use mutnet::udp::{ParseUdpError, Udp, UdpMethods, UdpMethodsMut};
use rand::{thread_rng, Rng};

#[rustfmt::skip]
const UDP: [u8; 14] = [
Expand Down
9 changes: 5 additions & 4 deletions examples/parse_from_iface.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
// Formatting and printing data to the default output may lead to dropped packets as this can be slow.
// This example should not be used to judge performance but rather to see how mutnet can be used.

use std::error::Error;
use std::fmt;
use std::fmt::Display;
use std::io::{stdout, Write};

use mutnet::addresses::ipv4::Ipv4Address;
use mutnet::addresses::ipv6::Ipv6Addr;
use mutnet::arp::ArpMethods;
Expand All @@ -13,10 +18,6 @@ use mutnet::typed_protocol_headers::EtherType;
use mutnet::typed_protocol_headers::InternetProtocolNumber;
use mutnet::typed_protocol_headers::OperationCode;
use mutnet::udp::UdpMethods;
use std::error::Error;
use std::fmt;
use std::fmt::Display;
use std::io::{stdout, Write};

#[derive(Debug, Default)]
struct Data {
Expand Down
12 changes: 6 additions & 6 deletions src/arp.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
//! ARP implementation and ARP specific errors.
mod error;
mod method_traits;

pub use error::*;
pub use method_traits::*;

#[cfg(all(feature = "remove_checksum", feature = "verify_arp", kani))]
mod verification;

use crate::data_buffer::traits::{HeaderMetadata, HeaderMetadataMut, Layer};
use crate::data_buffer::traits::{HeaderMetadataExtraction, Payload};
use crate::data_buffer::{
Expand All @@ -18,6 +12,12 @@ use crate::error::LengthExceedsAvailableSpaceError;
use crate::internal_utils::{check_and_calculate_data_length, header_start_offset_from_phi};
use crate::no_previous_header::NoPreviousHeader;

mod error;
mod method_traits;

#[cfg(all(feature = "remove_checksum", feature = "verify_arp", kani))]
mod verification;

/// ARP metadata.
///
/// Contains meta data about the ARP header in the parsed data buffer.
Expand Down
8 changes: 3 additions & 5 deletions src/arp/error.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
//! ARP specific errors.
use crate::error::UnexpectedBufferEndError;

use core::fmt::{Debug, Display, Formatter};

#[cfg(all(feature = "error_trait", not(feature = "std")))]
use core::error;

use core::fmt::{Debug, Display, Formatter};
#[cfg(feature = "std")]
use std::error;

use crate::error::UnexpectedBufferEndError;

/// Error returned when parsing an ARP header.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub enum ParseArpError {
Expand Down
3 changes: 2 additions & 1 deletion src/arp/method_traits.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
//! ARP access and manipulation methods.
use core::ops::Range;

use crate::addresses::ipv4::Ipv4Address;
use crate::addresses::mac::MacAddress;
use crate::data_buffer::traits::{BufferAccess, BufferAccessMut, HeaderManipulation, Layer};
use crate::typed_protocol_headers::{EtherType, UnrecognizedEtherTypeError};
use crate::typed_protocol_headers::{OperationCode, UnrecognizedOperationCodeError};
use core::ops::Range;

pub(crate) const HARDWARE_TYPE: Range<usize> = 0..2;
pub(crate) const PROTOCOL_TYPE: Range<usize> = 2..4;
Expand Down
3 changes: 2 additions & 1 deletion src/arp/verification.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use super::*;
use crate::data_buffer::traits::BufferIntoInner;
use crate::ethernet::Eth;

use super::*;

const SLICE_LENGTH: usize = 100;
const HEADROOM: usize = SLICE_LENGTH + 10;

Expand Down
1 change: 0 additions & 1 deletion src/checksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,6 @@ pub(crate) fn checksum_match_64_bytes(buf: &[u8]) -> u64 {

#[cfg(test)]
mod tests {

use crate::checksum::internet_checksum;

#[rustfmt::skip]
Expand Down
5 changes: 3 additions & 2 deletions src/data_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
//! Wrapper for network data buffer and parsed protocols metadata.
pub(crate) mod traits;
use core::ops::Range;

pub(crate) use crate::data_buffer::traits::*;
pub use crate::data_buffer::traits::{BufferIntoInner, Payload, PayloadMut};
use crate::error::LengthExceedsAvailableSpaceError;
use core::ops::Range;

pub(crate) mod traits;

/// Wraps the data buffer and contains metadata about the parsed protocols.
#[allow(private_bounds)]
Expand Down
3 changes: 2 additions & 1 deletion src/data_buffer/traits.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! Traits used to access header metadata or network data from the [`DataBuffer`].
use core::ops::Range;

use crate::error::{LengthExceedsAvailableSpaceError, NotEnoughHeadroomError};
use crate::ipv6_extensions::{Ipv6ExtMetaData, Ipv6ExtMetaDataMut};
use core::ops::Range;

/// Provides access to the underlying data buffer.
pub trait BufferIntoInner<B>
Expand Down
11 changes: 6 additions & 5 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
//! Non-protocol specific errors.
#[cfg(all(feature = "error_trait", not(feature = "std")))]
use core::error;
use core::fmt::{Debug, Display, Formatter};
#[cfg(feature = "std")]
use std::error;

use crate::arp::ParseArpError;
use crate::ieee802_1q_vlan::ParseIeee802_1QError;
use crate::ipv4::ParseIpv4Error;
Expand All @@ -8,11 +14,6 @@ use crate::tcp::ParseTcpError;
use crate::typed_protocol_headers::UnrecognizedEtherTypeError;
use crate::typed_protocol_headers::UnrecognizedInternetProtocolNumberError;
use crate::udp::ParseUdpError;
#[cfg(all(feature = "error_trait", not(feature = "std")))]
use core::error;
use core::fmt::{Debug, Display, Formatter};
#[cfg(feature = "std")]
use std::error;

/// Error returned if the data buffer is expected to be longer than it actually is.
///
Expand Down
13 changes: 6 additions & 7 deletions src/ethernet.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
//! Ethernet II implementation and Ethernet II specific errors.
mod method_traits;

pub use method_traits::*;

#[cfg(all(feature = "remove_checksum", feature = "verify_ethernet", kani))]
mod verification;

use crate::data_buffer::traits::{
BufferAccess, BufferAccessMut, HeaderMetadata, HeaderMetadataMut, Layer,
};
Expand All @@ -16,9 +11,14 @@ use crate::ieee802_1q_vlan::UpdateEtherTypeBelowIeee802_1q;
use crate::internal_utils::check_and_calculate_data_length;
use crate::typed_protocol_headers::EtherType;

mod method_traits;

#[cfg(all(feature = "remove_checksum", feature = "verify_ethernet", kani))]
mod verification;

/// Ethernet II metadata.
///
/// Contains meta data about the Ethernet II header in the parsed data buffer.
/// Contains metadata about the Ethernet II header in the parsed data buffer.
#[derive(Eq, PartialEq, Hash, Copy, Clone, Debug)]
pub struct Eth {
/// Amount of headroom.
Expand Down Expand Up @@ -192,7 +192,6 @@ where

#[cfg(test)]
mod tests {

use crate::data_buffer::traits::HeaderMetadataMut;
use crate::data_buffer::{DataBuffer, Payload, PayloadMut};
use crate::error::{LengthExceedsAvailableSpaceError, UnexpectedBufferEndError};
Expand Down
3 changes: 2 additions & 1 deletion src/ethernet/method_traits.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
//! Ethernet II access and manipulation methods.
use core::ops::Range;

use crate::addresses::mac::MacAddress;
use crate::data_buffer::traits::{
BufferAccess, BufferAccessMut, HeaderManipulation, HeaderMetadata, Layer,
};
use crate::typed_protocol_headers::{EtherType, UnrecognizedEtherTypeError};
use core::ops::Range;

pub(crate) const DESTINATION_MAC: Range<usize> = 0..6;
pub(crate) const SOURCE_MAC: Range<usize> = 6..12;
Expand Down
3 changes: 2 additions & 1 deletion src/ethernet/verification.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::*;
use crate::data_buffer::traits::BufferIntoInner;

use super::*;

const SLICE_LENGTH: usize = 20 + 40;
const HEADROOM: usize = SLICE_LENGTH + 10;

Expand Down
12 changes: 6 additions & 6 deletions src/ieee802_1q_vlan.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
//! IEEE 802.1Q implementation and IEEE 802.1Q specific errors.
mod error;
mod method_traits;

pub use error::*;
pub use method_traits::*;

#[cfg(all(feature = "remove_checksum", feature = "verify_vlan", kani))]
mod verification;

use crate::data_buffer::traits::HeaderMetadataExtraction;
use crate::data_buffer::traits::{
BufferAccess, BufferAccessMut, HeaderMetadata, HeaderMetadataMut, Layer,
Expand All @@ -20,6 +14,12 @@ use crate::error::LengthExceedsAvailableSpaceError;
use crate::internal_utils::{check_and_calculate_data_length, header_start_offset_from_phi};
use crate::no_previous_header::NoPreviousHeader;

mod error;
mod method_traits;

#[cfg(all(feature = "remove_checksum", feature = "verify_vlan", kani))]
mod verification;

/// Supported VLAN tags.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[cfg_attr(kani, derive(kani::Arbitrary))]
Expand Down
2 changes: 0 additions & 2 deletions src/ieee802_1q_vlan/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
#[cfg(all(feature = "error_trait", not(feature = "std")))]
use core::error;

use core::fmt::{Debug, Display, Formatter};

#[cfg(feature = "std")]
use std::error;

Expand Down
3 changes: 2 additions & 1 deletion src/ieee802_1q_vlan/method_traits.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//! IEEE 802.1Q access and manipulation methods.
use core::ops::Range;

use crate::data_buffer::traits::{
BufferAccess, BufferAccessMut, HeaderManipulation, HeaderMetadata, Layer,
};
use crate::error::NotEnoughHeadroomError;
use crate::ieee802_1q_vlan::{NotDoubleTaggedError, Vlan};
use crate::typed_protocol_headers::{EtherType, UnrecognizedEtherTypeError};
use core::ops::Range;

pub(crate) const SINGLE_TAGGED_VLAN_C_TAG_CONTROL_INFORMATION: Range<usize> = 0..2;
pub(crate) const SINGLE_TAGGED_ETHER_TYPE: Range<usize> = 2..4;
Expand Down
3 changes: 2 additions & 1 deletion src/ieee802_1q_vlan/verification.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use super::*;
use crate::data_buffer::traits::BufferIntoInner;
use crate::ethernet::{Eth, EthernetMethods};
use crate::ipv6::Ipv6;
use crate::tcp::Tcp;
use crate::typed_protocol_headers::constants;
use crate::typed_protocol_headers::EtherType;

use super::*;

const SLICE_LENGTH: usize = 60;
const HEADROOM: usize = SLICE_LENGTH + 10;

Expand Down
3 changes: 2 additions & 1 deletion src/internal_utils.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use core::cmp::Ordering;

use crate::checksum::internet_checksum_intermediary;
use crate::data_buffer::traits::{HeaderManipulation, HeaderMetadata, HeaderMetadataMut, Layer};
use crate::error::{NotEnoughHeadroomError, UnexpectedBufferEndError};
use crate::ipv4::Ipv4Methods;
use crate::ipv6::Ipv6Methods;
use core::cmp::Ordering;

#[inline]
pub(crate) fn check_and_calculate_data_length<E>(
Expand Down
12 changes: 6 additions & 6 deletions src/ipv4.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
//! IPV4 implementation and IPV4 specific errors.
mod error;
mod method_traits;

pub use error::*;
pub use method_traits::*;

#[cfg(all(feature = "remove_checksum", feature = "verify_ipv4", kani))]
mod verification;

use crate::data_buffer::traits::HeaderMetadataExtraction;
use crate::data_buffer::traits::{
BufferAccess, BufferAccessMut, HeaderMetadata, HeaderMetadataMut, Layer,
Expand All @@ -21,6 +15,12 @@ use crate::error::{InvalidChecksumError, LengthExceedsAvailableSpaceError};
use crate::internal_utils::{check_and_calculate_data_length, header_start_offset_from_phi};
use crate::no_previous_header::NoPreviousHeader;

mod error;
mod method_traits;

#[cfg(all(feature = "remove_checksum", feature = "verify_ipv4", kani))]
mod verification;

/// IPv4 metadata.
///
/// Contains meta data about the IPv4 header in the parsed data buffer.
Expand Down
Loading

0 comments on commit 828dc1f

Please sign in to comment.