Skip to content

Commit

Permalink
Formatting. NFC.
Browse files Browse the repository at this point in the history
  • Loading branch information
whitequark committed Oct 2, 2017
1 parent d5147ef commit 70248c5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 31 deletions.
14 changes: 7 additions & 7 deletions src/wire/icmpv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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 } => {
Expand Down
6 changes: 2 additions & 4 deletions src/wire/ipv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
23 changes: 9 additions & 14 deletions src/wire/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 3 additions & 6 deletions src/wire/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 70248c5

Please sign in to comment.