Parcourir la source

Move protocol-related data structures to smoltcp::wire.

whitequark il y a 8 ans
Parent
commit
34d5e1bb6a
5 fichiers modifiés avec 90 ajouts et 65 suppressions
  1. 1 50
      src/lib.rs
  2. 16 14
      src/wire/arp.rs
  3. 1 1
      src/wire/ethernet.rs
  4. 0 0
      src/wire/ipv4.rs
  5. 72 0
      src/wire/mod.rs

+ 1 - 50
src/lib.rs

@@ -7,53 +7,4 @@ extern crate std;
 
 extern crate byteorder;
 
-macro_rules! enum_with_unknown {
-    (#[$( $attr:meta ),*]
-     pub enum $name:ident($ty:ty) { $( $variant:ident = $value:expr ),+ }) => {
-        #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
-        #[$( $attr ),*]
-        pub enum $name {
-            $( $variant ),*,
-            Unknown($ty)
-        }
-
-        impl ::core::convert::From<$ty> for $name {
-            fn from(value: $ty) -> Self {
-                match value {
-                    $( $value => $name::$variant ),*,
-                    other => $name::Unknown(other)
-                }
-            }
-        }
-
-        impl ::core::convert::From<$name> for $ty {
-            fn from(value: $name) -> Self {
-                match value {
-                    $( $name::$variant => $value ),*,
-                    $name::Unknown(other) => other
-                }
-            }
-        }
-    }
-}
-
-mod field {
-    pub type Field     = ::core::ops::Range<usize>;
-    pub type FieldFrom = ::core::ops::RangeFrom<usize>;
-}
-
-mod ethernet;
-mod arp;
-mod ipv4;
-
-pub use ethernet::ProtocolType as EthernetProtocolType;
-pub use ethernet::Address as EthernetAddress;
-pub use ethernet::Frame as EthernetFrame;
-
-pub use arp::HardwareType as ArpHardwareType;
-pub use arp::ProtocolType as ArpProtocolType;
-pub use arp::Operation as ArpOperation;
-pub use arp::Packet as ArpPacket;
-pub use arp::Repr as ArpRepr;
-
-pub use ipv4::Address as Ipv4Address;
+pub mod wire;

+ 16 - 14
src/arp.rs → src/wire/arp.rs

@@ -1,6 +1,6 @@
 use byteorder::{ByteOrder, NetworkEndian};
 
-pub use ::ethernet::ProtocolType as ProtocolType;
+pub use super::EthernetProtocolType as ProtocolType;
 
 enum_with_unknown! {
     /// ARP network protocol type.
@@ -24,7 +24,7 @@ pub struct Packet<T: AsRef<[u8]>>(T);
 mod field {
     #![allow(non_snake_case)]
 
-    use ::field::*;
+    use ::wire::field::*;
 
     pub const HTYPE: Field = 0..2;
     pub const PTYPE: Field = 2..4;
@@ -209,16 +209,18 @@ impl<T: AsRef<[u8]> + AsMut<[u8]>> Packet<T> {
     }
 }
 
+use super::{EthernetAddress, Ipv4Address};
+
 /// A high-level representation of an Address Resolution Protocol packet.
 #[derive(Debug, PartialEq, Eq, Clone, Copy)]
 pub enum Repr {
     /// An Ethernet and IPv4 Address Resolution Protocol packet.
     EthernetIpv4 {
         operation: Operation,
-        source_hardware_addr: ::EthernetAddress,
-        source_protocol_addr: ::Ipv4Address,
-        target_hardware_addr: ::EthernetAddress,
-        target_protocol_addr: ::Ipv4Address
+        source_hardware_addr: EthernetAddress,
+        source_protocol_addr: Ipv4Address,
+        target_hardware_addr: EthernetAddress,
+        target_protocol_addr: Ipv4Address
     },
     #[doc(hidden)]
     __Nonexhaustive
@@ -234,13 +236,13 @@ impl Repr {
                 Ok(Repr::EthernetIpv4 {
                     operation: packet.operation(),
                     source_hardware_addr:
-                        ::EthernetAddress::from_bytes(packet.source_hardware_addr()),
+                        EthernetAddress::from_bytes(packet.source_hardware_addr()),
                     source_protocol_addr:
-                        ::Ipv4Address::from_bytes(packet.source_protocol_addr()),
+                        Ipv4Address::from_bytes(packet.source_protocol_addr()),
                     target_hardware_addr:
-                        ::EthernetAddress::from_bytes(packet.target_hardware_addr()),
+                        EthernetAddress::from_bytes(packet.target_hardware_addr()),
                     target_protocol_addr:
-                        ::Ipv4Address::from_bytes(packet.target_protocol_addr())
+                        Ipv4Address::from_bytes(packet.target_protocol_addr())
                 })
             },
             _ => Err(())
@@ -321,13 +323,13 @@ mod test {
         Repr::EthernetIpv4 {
             operation: Operation::Request,
             source_hardware_addr:
-                ::EthernetAddress::from_bytes(&[0x11, 0x12, 0x13, 0x14, 0x15, 0x16]),
+                EthernetAddress::from_bytes(&[0x11, 0x12, 0x13, 0x14, 0x15, 0x16]),
             source_protocol_addr:
-                ::Ipv4Address::from_bytes(&[0x21, 0x22, 0x23, 0x24]),
+                Ipv4Address::from_bytes(&[0x21, 0x22, 0x23, 0x24]),
             target_hardware_addr:
-                ::EthernetAddress::from_bytes(&[0x31, 0x32, 0x33, 0x34, 0x35, 0x36]),
+                EthernetAddress::from_bytes(&[0x31, 0x32, 0x33, 0x34, 0x35, 0x36]),
             target_protocol_addr:
-                ::Ipv4Address::from_bytes(&[0x41, 0x42, 0x43, 0x44])
+                Ipv4Address::from_bytes(&[0x41, 0x42, 0x43, 0x44])
         }
     }
 

+ 1 - 1
src/ethernet.rs → src/wire/ethernet.rs

@@ -44,7 +44,7 @@ impl fmt::Display for Address {
 pub struct Frame<T: AsRef<[u8]>>(T);
 
 mod field {
-    use ::field::*;
+    use ::wire::field::*;
 
     pub const SOURCE:      Field     =  0..6;
     pub const DESTINATION: Field     =  6..12;

+ 0 - 0
src/ipv4.rs → src/wire/ipv4.rs


+ 72 - 0
src/wire/mod.rs

@@ -0,0 +1,72 @@
+//! Low-level packet access and construction.
+//!
+//! The `wire` module deals with the packet *representation*. It provides two levels
+//! of functionality.
+//!
+//!  * First, it provides functions to extract fields from sequences of octets,
+//!    and to insert fields into sequences of octets. This happens through the `Frame`
+//!    and `Packet` families of structures, e.g. [EthernetPacket](struct.EthernetPacket.html).
+//!
+//!  * Second, in cases where the space of valid field values is much smaller than the space
+//!    of possible field values, it provides a compact, high-level representation
+//!    of packet data that can be parsed from and emitted into a sequence of octets.
+//!    This happens through the `Repr` family of enums, e.g. [ArpRepr](enum.ArpRepr.html).
+//!
+//! The functions in the `wire` module are designed for robustness and use together with
+//! `-Cpanic=abort`. The accessor and parsing functions never panic. The setter and emission
+//! functions only panic if the underlying buffer is too small.
+//!
+//! The data structures in the `wire` module do not perform validation of received data;
+//! that is the job of an upper layer. This includes the `Repr` family, which only validate
+//! as much as is necessary to build the representation.
+
+macro_rules! enum_with_unknown {
+    (#[$( $attr:meta ),*]
+     pub enum $name:ident($ty:ty) { $( $variant:ident = $value:expr ),+ }) => {
+        #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
+        #[$( $attr ),*]
+        pub enum $name {
+            $( $variant ),*,
+            Unknown($ty)
+        }
+
+        impl ::core::convert::From<$ty> for $name {
+            fn from(value: $ty) -> Self {
+                match value {
+                    $( $value => $name::$variant ),*,
+                    other => $name::Unknown(other)
+                }
+            }
+        }
+
+        impl ::core::convert::From<$name> for $ty {
+            fn from(value: $name) -> Self {
+                match value {
+                    $( $name::$variant => $value ),*,
+                    $name::Unknown(other) => other
+                }
+            }
+        }
+    }
+}
+
+mod field {
+    pub type Field     = ::core::ops::Range<usize>;
+    pub type FieldFrom = ::core::ops::RangeFrom<usize>;
+}
+
+mod ethernet;
+mod arp;
+mod ipv4;
+
+pub use self::ethernet::ProtocolType as EthernetProtocolType;
+pub use self::ethernet::Address as EthernetAddress;
+pub use self::ethernet::Frame as EthernetFrame;
+
+pub use self::arp::HardwareType as ArpHardwareType;
+pub use self::arp::ProtocolType as ArpProtocolType;
+pub use self::arp::Operation as ArpOperation;
+pub use self::arp::Packet as ArpPacket;
+pub use self::arp::Repr as ArpRepr;
+
+pub use self::ipv4::Address as Ipv4Address;