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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
extern crate time;
use std::convert;
use byteorder::{NetworkEndian, ReadBytesExt, WriteBytesExt};
use conv::TryFrom;
use errors::*;
use formats::{
LeapIndicator,
Version,
Mode,
Stratum,
ReferenceIdentifier,
PrimarySource
};
use formats::timestamp::{ShortFormat, TimestampFormat};
#[derive(Debug, PartialEq, Default)]
pub struct Packet {
pub li: LeapIndicator,
pub vn: Version,
pub mode: Mode,
pub stratum: Stratum,
pub poll: i8,
pub precision: i8,
pub delay: ShortFormat,
pub dispersion: ShortFormat,
pub ref_id: ReferenceIdentifier,
pub ref_time: TimestampFormat,
pub orig_time: TimestampFormat,
pub recv_time: TimestampFormat,
pub transmit_time: TimestampFormat,
}
impl Packet {
pub fn new_client() -> Packet {
println!("{}", TimestampFormat::from(time::now().to_timespec()));
Packet {
mode: Mode::Client,
vn: Version::Ver2,
transmit_time: time::now().to_timespec().into(),
..Default::default()
}
}
}
impl From<Packet> for Vec<u8> {
fn from(p: Packet) -> Vec<u8> {
let mut buf = Vec::with_capacity(48);
let mut li_vn_mode = 0;
li_vn_mode |= (p.li as u8) << 6;
li_vn_mode |= (p.vn as u8) << 3;
li_vn_mode |= p.mode as u8;
buf.push(li_vn_mode);
buf.push(p.stratum.get_value());
buf.push(p.poll as u8);
buf.push(p.precision as u8);
buf.write_u32::<NetworkEndian>(p.delay.into()).expect("can't fail");
buf.write_u32::<NetworkEndian>(p.dispersion.into()).expect("can't fail");
buf.write_u32::<NetworkEndian>(p.ref_id.into()).expect("can't fail");
buf.write_u64::<NetworkEndian>(p.ref_time.into()).expect("can't fail");
buf.write_u64::<NetworkEndian>(p.orig_time.into()).expect("can't fail");
buf.write_u64::<NetworkEndian>(p.recv_time.into()).expect("can't fail");
buf.write_u64::<NetworkEndian>(p.transmit_time.into()).expect("can't fail");
buf
}
}
impl<T: ReadBytesExt> convert::TryFrom<T> for Packet {
type Err = Error;
fn try_from(mut rdr: T) -> Result<Packet> {
let li_vn_mode = rdr.read_u8()?;
let li = LeapIndicator::try_from(li_vn_mode >> 6)?;
let vn = Version::try_from((li_vn_mode >> 3) & 0b111)?;
let mode = Mode::try_from(li_vn_mode & 0b111)?;
let stratum = Stratum::new(rdr.read_u8()?);
let poll = rdr.read_i8()?;
let precision = rdr.read_i8()?;
let delay = rdr.read_u32::<NetworkEndian>()?.into();
let dispersion = rdr.read_u32::<NetworkEndian>()?.into();
let ref_id_raw = rdr.read_u32::<NetworkEndian>().unwrap();
let ref_id = if stratum.primary() {
let source = PrimarySource::try_from(ref_id_raw)?;
ReferenceIdentifier::Primary(source)
} else if stratum.secondary() {
ReferenceIdentifier::Secondary(ref_id_raw)
} else {
return Err(format!("Unsupported stratum: {}", stratum.get_value()).into());
};
let ref_time = rdr.read_u64::<NetworkEndian>()?.into();
let orig_time = rdr.read_u64::<NetworkEndian>()?.into();
let recv_time = rdr.read_u64::<NetworkEndian>()?.into();
let transmit_time = rdr.read_u64::<NetworkEndian>()?.into();
Ok(Packet {
li: li,
vn: vn,
mode: mode,
stratum: stratum,
poll: poll,
precision: precision,
delay: delay,
dispersion: dispersion,
ref_id: ref_id,
ref_time: ref_time,
orig_time: orig_time,
recv_time: recv_time,
transmit_time: transmit_time,
})
}
}
#[cfg(test)]
mod tests {
use std::io::Cursor;
use super::Packet;
use std::convert::TryFrom;
use formats::{
LeapIndicator,
Version,
Mode,
Stratum,
ReferenceIdentifier,
PrimarySource
};
use formats::timestamp::{ShortFormat, TimestampFormat};
#[test]
fn packet_from_bytes() {
let input = vec![20u8, 1, 3, 240, 0, 0, 0, 0, 0, 0, 0, 24, 67, 68, 77, 65, 215, 188, 128, 105, 198, 169, 46, 99,
215, 187, 177, 194, 159, 47, 120, 0, 215, 188, 128, 113, 45, 236, 230, 45, 215, 188, 128, 113,
46, 35, 158, 108];
let expected_output = Packet { li: LeapIndicator::NoWarning, vn: Version::Ver2,
mode: Mode::Server, stratum: Stratum::new(1),
poll: 3, precision: -16, delay: ShortFormat { sec: 0, frac: 0 },
dispersion: ShortFormat { sec: 0, frac: 24 },
ref_id: ReferenceIdentifier::Primary(PrimarySource::CDMA),
ref_time: TimestampFormat { sec: 3619455081, frac: 3332976227 },
orig_time: TimestampFormat { sec: 3619402178, frac: 2670688256 },
recv_time: TimestampFormat { sec: 3619455089, frac: 770500141 },
transmit_time: TimestampFormat { sec: 3619455089, frac: 774086252 }
};
let rdr = Cursor::new(input);
assert_eq!(expected_output, Packet::try_from(rdr).unwrap());
}
#[test]
fn packet_to_bytes() {
let expected_output = vec![
20, 1, 3, 240, 0, 0, 0,
0, 0, 0, 0, 24, 67, 68,
77, 65, 215, 188, 128, 105, 198,
169, 46, 99, 215, 187, 177, 194,
159, 47, 120, 0, 215, 188, 128,
113, 45, 236, 230, 45, 215, 188,
128, 113, 46, 35, 158, 108
];
let input = Packet { li: LeapIndicator::NoWarning, vn: Version::Ver2,
mode: Mode::Server, stratum: Stratum::new(1),
poll: 3, precision: -16, delay: ShortFormat { sec: 0, frac: 0 },
dispersion: ShortFormat { sec: 0, frac: 24 },
ref_id: ReferenceIdentifier::Primary(PrimarySource::CDMA),
ref_time: TimestampFormat { sec: 3619455081, frac: 3332976227 },
orig_time: TimestampFormat { sec: 3619402178, frac: 2670688256 },
recv_time: TimestampFormat { sec: 3619455089, frac: 770500141 },
transmit_time: TimestampFormat { sec: 3619455089, frac: 774086252 }
};
let output: Vec<u8> = input.into();
assert_eq!(output, expected_output);
}
#[test]
fn packet_conversion_roundtrip() {
let input = vec![
20, 1, 3, 240, 0, 0, 0,
0, 0, 0, 0, 24, 67, 68,
77, 65, 215, 188, 128, 105, 198,
169, 46, 99, 215, 187, 177, 194,
159, 47, 120, 0, 215, 188, 128,
113, 45, 236, 230, 45, 215, 188,
128, 113, 46, 35, 158, 108
];
let rdr = Cursor::new(&input);
let output: Vec<u8> = Packet::try_from(rdr).unwrap().into();
assert_eq!(input.as_slice(), output.as_slice());
}
}