1 /// A ICMPv4 timestamp or timestamp response message.
2 #[derive(Clone, Debug, PartialEq, Eq)]
3 pub struct TimestampMessage {
4     pub id: u16,
5     pub seq: u16,
6     pub originate_timestamp: u32,
7     pub receive_timestamp: u32,
8     pub transmit_timestamp: u32,
9 }
10 
11 impl TimestampMessage {
12     /// Deprecated use [`TimestampMessage::LEN`] instead.
13     #[deprecated(since = "0.14.0", note = "Use `TimestampMessage::LEN` instead")]
14     pub const SERIALIZED_SIZE: usize = 20;
15 
16     /// The size in bytes/octets of a timestamp request or timestamp response message.
17     pub const LEN: usize = 20;
18 
19     /// Decodes the timestamp message part of an ICMPv4 message.
from_bytes(bytes: [u8; 16]) -> TimestampMessage20     pub fn from_bytes(bytes: [u8; 16]) -> TimestampMessage {
21         TimestampMessage {
22             id: u16::from_be_bytes([bytes[0], bytes[1]]),
23             seq: u16::from_be_bytes([bytes[2], bytes[3]]),
24             originate_timestamp: u32::from_be_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]),
25             receive_timestamp: u32::from_be_bytes([bytes[8], bytes[9], bytes[10], bytes[11]]),
26             transmit_timestamp: u32::from_be_bytes([bytes[12], bytes[13], bytes[14], bytes[15]]),
27         }
28     }
29 }
30 
31 #[cfg(test)]
32 mod test {
33     use crate::icmpv4::*;
34     use alloc::format;
35     use proptest::prelude::*;
36 
37     #[test]
constants()38     fn constants() {
39         assert_eq!(20, TimestampMessage::LEN);
40     }
41 
42     proptest! {
43         #[test]
44         fn from_bytes(bytes in any::<[u8;16]>()) {
45             assert_eq!(
46                 TimestampMessage::from_bytes(bytes),
47                 TimestampMessage{
48                     id: u16::from_be_bytes([bytes[0], bytes[1]]),
49                     seq: u16::from_be_bytes([bytes[2], bytes[3]]),
50                     originate_timestamp: u32::from_be_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]),
51                     receive_timestamp: u32::from_be_bytes([bytes[8], bytes[9], bytes[10], bytes[11]]),
52                     transmit_timestamp: u32::from_be_bytes([bytes[12], bytes[13], bytes[14], bytes[15]]),
53                 }
54             );
55         }
56     }
57 
58     #[test]
clone_eq()59     fn clone_eq() {
60         let v = TimestampMessage {
61             id: 0,
62             seq: 0,
63             originate_timestamp: 0,
64             receive_timestamp: 0,
65             transmit_timestamp: 0,
66         };
67         assert_eq!(v.clone(), v);
68     }
69 
70     #[test]
debug()71     fn debug() {
72         let v = TimestampMessage {
73             id: 0,
74             seq: 0,
75             originate_timestamp: 0,
76             receive_timestamp: 0,
77             transmit_timestamp: 0,
78         };
79         assert_eq!(
80             format!("{:?}", v),
81             format!(
82                 "TimestampMessage {{ id: {:?}, seq: {:?}, originate_timestamp: {:?}, receive_timestamp: {:?}, transmit_timestamp: {:?} }}",
83                 v.id,
84                 v.seq,
85                 v.originate_timestamp,
86                 v.receive_timestamp,
87                 v.transmit_timestamp,
88             )
89         );
90     }
91 }
92