1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
/// Echo Request & Response common parts between ICMPv4 and ICMPv6.
///
/// # RFC 4443 Description (ICMPv6)
///
/// Every node MUST implement an ICMPv6 Echo responder function that
/// receives Echo Requests and originates corresponding Echo Replies. A
/// node SHOULD also implement an application-layer interface for
/// originating Echo Requests and receiving Echo Replies, for diagnostic
/// purposes.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct IcmpEchoHeader {
/// An identifier to aid in matching Echo Replies to Echo Requests. May be zero.
pub id: u16,
/// A sequence number to aid in matching Echo Replies to Echo Requests. May be zero.
pub seq: u16,
}
impl IcmpEchoHeader {
/// Serialized size of an IcmpEchoHeader header in bytes/octets.
pub const LEN: usize = 4;
/// Return the seq + id encoded to the on the wire format.
#[inline]
pub fn to_bytes(&self) -> [u8; 4] {
let id_be = self.id.to_be_bytes();
let seq_be = self.seq.to_be_bytes();
[id_be[0], id_be[1], seq_be[0], seq_be[1]]
}
/// Decodes the seq + id from the on the wire format.
#[inline]
pub fn from_bytes(bytes5to8: [u8; 4]) -> IcmpEchoHeader {
IcmpEchoHeader {
id: u16::from_be_bytes([bytes5to8[0], bytes5to8[1]]),
seq: u16::from_be_bytes([bytes5to8[2], bytes5to8[3]]),
}
}
}
#[cfg(test)]
mod test {
use crate::*;
use alloc::format;
use proptest::prelude::*;
proptest! {
#[test]
fn to_bytes(
id in any::<u16>(),
seq in any::<u16>()
) {
let id_bytes = id.to_be_bytes();
let seq_bytes = seq.to_be_bytes();
assert_eq!(
IcmpEchoHeader{ id, seq }.to_bytes(),
[
id_bytes[0], id_bytes[1],
seq_bytes[0], seq_bytes[1]
]
);
}
#[test]
fn from_bytes(
bytes in any::<[u8;4]>()
) {
assert_eq!(
IcmpEchoHeader::from_bytes(bytes),
IcmpEchoHeader {
id: u16::from_be_bytes([bytes[0], bytes[1]]),
seq: u16::from_be_bytes([bytes[2], bytes[3]])
}
);
}
#[test]
fn clone_eq(
id in any::<u16>(),
seq in any::<u16>()
) {
let value = IcmpEchoHeader{ id, seq };
assert_eq!(value.clone(), value);
}
#[test]
fn debug(
id in any::<u16>(),
seq in any::<u16>()
) {
assert_eq!(
format!("{:?}", IcmpEchoHeader{ id, seq }),
format!("IcmpEchoHeader {{ id: {:?}, seq: {:?} }}", id, seq)
);
}
}
}