Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: smoltcp-rs/smoltcp
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: d5147efb822e
Choose a base ref
...
head repository: smoltcp-rs/smoltcp
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 58c12b8f5158
Choose a head ref
  • 2 commits
  • 5 files changed
  • 1 contributor

Commits on Oct 2, 2017

  1. Formatting. NFC.

    whitequark committed Oct 2, 2017
    Copy the full SHA
    70248c5 View commit details
  2. Copy the full SHA
    58c12b8 View commit details
Showing with 32 additions and 40 deletions.
  1. +11 −9 src/phy/mod.rs
  2. +7 −7 src/wire/icmpv4.rs
  3. +2 −4 src/wire/ipv4.rs
  4. +9 −14 src/wire/tcp.rs
  5. +3 −6 src/wire/udp.rs
20 changes: 11 additions & 9 deletions src/phy/mod.rs
Original file line number Diff line number Diff line change
@@ -135,16 +135,16 @@ pub use self::tap_interface::TapInterface;
/// A tracer device for Ethernet frames.
pub type EthernetTracer<T> = Tracer<T, super::wire::EthernetFrame<&'static [u8]>>;

/// The checksum configuration for a device
/// A description of checksum behavior for a particular protocol.
#[derive(Debug, Clone, Copy)]
pub enum Checksum {
/// Validate checksum when receiving and supply checksum when sending
/// Verify checksum when receiving and compute checksum when sending.
Both,
/// Validate checksum when receiving
/// Verify checksum when receiving.
Rx,
/// Supply checksum before sending
/// Compute checksum before sending.
Tx,
/// Ignore checksum
/// Ignore checksum completely.
None,
}

@@ -155,22 +155,24 @@ impl Default for Checksum {
}

impl Checksum {
pub(crate) fn rx(&self) -> bool {
/// Returns whether checksum should be verified when receiving.
pub fn rx(&self) -> bool {
match *self {
Checksum::Both | Checksum::Rx => true,
_ => false
}
}

pub(crate) fn tx(&self) -> bool {
/// Returns whether checksum should be verified when sending.
pub fn tx(&self) -> bool {
match *self {
Checksum::Both | Checksum::Tx => true,
_ => false
}
}
}

/// Configuration of checksum capabilities for each applicable protocol
/// A description of checksum behavior for every supported protocol.
#[derive(Debug, Clone, Default)]
pub struct ChecksumCapabilities {
pub ipv4: Checksum,
@@ -204,7 +206,7 @@ pub struct DeviceCapabilities {
/// dynamically allocated.
pub max_burst_size: Option<usize>,

/// Checksum capabilities for the current device
/// The set of protocols for which checksum can be computed in hardware.
pub checksum: ChecksumCapabilities,

/// Only present to prevent people from trying to initialize every field of DeviceLimits,
14 changes: 7 additions & 7 deletions src/wire/icmpv4.rs
Original file line number Diff line number Diff line change
@@ -385,10 +385,11 @@ pub enum Repr<'a> {
impl<'a> Repr<'a> {
/// Parse an Internet Control Message Protocol version 4 packet and return
/// a high-level representation.
pub fn parse<T: AsRef<[u8]> + ?Sized>(packet: &Packet<&'a T>, checksum_caps: &ChecksumCapabilities) -> Result<Repr<'a>> {
if checksum_caps.icmpv4.rx() && !packet.verify_checksum() {
return Err(Error::Checksum)
}
pub fn parse<T>(packet: &Packet<&'a T>, checksum_caps: &ChecksumCapabilities)
-> Result<Repr<'a>>
where T: AsRef<[u8]> + ?Sized {
// Valid checksum is expected.
if checksum_caps.icmpv4.rx() && !packet.verify_checksum() { return Err(Error::Checksum) }

match (packet.msg_type(), packet.msg_code()) {
(Message::EchoRequest, 0) => {
@@ -446,9 +447,8 @@ impl<'a> Repr<'a> {

/// Emit a high-level representation into an Internet Control Message Protocol version 4
/// packet.
pub fn emit<T: AsRef<[u8]> + AsMut<[u8]> + ?Sized>(&self,
packet: &mut Packet<&mut T>,
checksum_caps: &ChecksumCapabilities) {
pub fn emit<T>(&self, packet: &mut Packet<&mut T>, checksum_caps: &ChecksumCapabilities)
where T: AsRef<[u8]> + AsMut<[u8]> + ?Sized {
packet.set_msg_code(0);
match self {
&Repr::EchoRequest { ident, seq_no, data } => {
6 changes: 2 additions & 4 deletions src/wire/ipv4.rs
Original file line number Diff line number Diff line change
@@ -402,14 +402,12 @@ pub struct Repr {

impl Repr {
/// Parse an Internet Protocol version 4 packet and return a high-level representation.
pub fn parse<T: AsRef<[u8]> + ?Sized>(packet: &Packet<&T>,
pub fn parse<T: AsRef<[u8]> + ?Sized>(packet: &Packet<&T>,
checksum_caps: &ChecksumCapabilities) -> Result<Repr> {
// Version 4 is expected.
if packet.version() != 4 { return Err(Error::Malformed) }
// Valid checksum is expected.
if checksum_caps.ipv4.rx() {
if !packet.verify_checksum() { return Err(Error::Checksum) }
}
if checksum_caps.ipv4.rx() && !packet.verify_checksum() { return Err(Error::Checksum) }
// We do not support fragmentation.
if packet.more_frags() || packet.frag_offset() != 0 { return Err(Error::Fragmented) }
// Total length may not be less than header length.
23 changes: 9 additions & 14 deletions src/wire/tcp.rs
Original file line number Diff line number Diff line change
@@ -643,18 +643,15 @@ pub struct Repr<'a> {

impl<'a> Repr<'a> {
/// Parse a Transmission Control Protocol packet and return a high-level representation.
pub fn parse<T: ?Sized>(packet: &Packet<&'a T>,
src_addr: &IpAddress,
dst_addr: &IpAddress,
checksum_caps: &ChecksumCapabilities) -> Result<Repr<'a>>
where T: AsRef<[u8]> {
pub fn parse<T>(packet: &Packet<&'a T>, src_addr: &IpAddress, dst_addr: &IpAddress,
checksum_caps: &ChecksumCapabilities) -> Result<Repr<'a>>
where T: AsRef<[u8]> + ?Sized {
// Source and destination ports must be present.
if packet.src_port() == 0 { return Err(Error::Malformed) }
if packet.dst_port() == 0 { return Err(Error::Malformed) }

// Valid checksum is expected...
if checksum_caps.tcpv4.rx() && !packet.verify_checksum(src_addr, dst_addr) {
return Err(Error::Checksum)
// Valid checksum is expected.
if checksum_caps.tcpv4.rx() && !packet.verify_checksum(src_addr, dst_addr) {
return Err(Error::Checksum)
}

let control =
@@ -717,10 +714,8 @@ impl<'a> Repr<'a> {
}

/// Emit a high-level representation into a Transmission Control Protocol packet.
pub fn emit<T>(&self, packet: &mut Packet<&mut T>,
src_addr: &IpAddress,
dst_addr: &IpAddress,
checksum_caps: &ChecksumCapabilities)
pub fn emit<T>(&self, packet: &mut Packet<&mut T>, src_addr: &IpAddress, dst_addr: &IpAddress,
checksum_caps: &ChecksumCapabilities)
where T: AsRef<[u8]> + AsMut<[u8]> + ?Sized {
packet.set_src_port(self.src_port);
packet.set_dst_port(self.dst_port);
@@ -748,7 +743,7 @@ impl<'a> Repr<'a> {
}
packet.set_urgent_at(0);
packet.payload_mut().copy_from_slice(self.payload);

if checksum_caps.tcpv4.tx() {
packet.fill_checksum(src_addr, dst_addr)
} else {
9 changes: 3 additions & 6 deletions src/wire/udp.rs
Original file line number Diff line number Diff line change
@@ -202,14 +202,11 @@ pub struct Repr<'a> {

impl<'a> Repr<'a> {
/// Parse an User Datagram Protocol packet and return a high-level representation.
pub fn parse<T: ?Sized>(packet: &Packet<&'a T>,
src_addr: &IpAddress,
dst_addr: &IpAddress,
checksum_caps: &ChecksumCapabilities) -> Result<Repr<'a>>
where T: AsRef<[u8]> {
pub fn parse<T>(packet: &Packet<&'a T>, src_addr: &IpAddress, dst_addr: &IpAddress,
checksum_caps: &ChecksumCapabilities) -> Result<Repr<'a>>
where T: AsRef<[u8]> + ?Sized {
// Destination port cannot be omitted (but source port can be).
if packet.dst_port() == 0 { return Err(Error::Malformed) }

// Valid checksum is expected...
if checksum_caps.udpv4.rx() && !packet.verify_checksum(src_addr, dst_addr) {
match (src_addr, dst_addr) {