Formatting. NFC.

This commit is contained in:
whitequark 2017-10-02 21:51:43 +00:00
parent 61cb64406d
commit 30754efc4f
4 changed files with 21 additions and 31 deletions

View File

@ -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 } => {

View File

@ -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.

View File

@ -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 {

View File

@ -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) {