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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#![recursion_limit = "1024"]
#[macro_use]
extern crate custom_derive;
extern crate conv;
#[macro_use]
extern crate log;
extern crate byteorder;
pub mod protocol;
use protocol::{ReadBytes, ConstPackedSizeBytes, WriteBytes};
use std::io;
use async_std::net::{ToSocketAddrs, UdpSocket};
use anyhow::Result;
use std::{self, time};
pub async fn request<A: ToSocketAddrs>(addr: A) -> io::Result<protocol::Packet> {
let mut packet = {
let leap_indicator = protocol::LeapIndicator::default();
let version = protocol::Version::V4;
let mode = protocol::Mode::Client;
let poll = 0;
let precision = 0;
let root_delay = protocol::ShortFormat::default();
let root_dispersion = protocol::ShortFormat::default();
let transmit_timestamp = Instant::now().into();
let stratum = protocol::Stratum::UNSPECIFIED;
let src = protocol::PrimarySource::Null;
let reference_id = protocol::ReferenceIdentifier::PrimarySource(src);
let reference_timestamp = protocol::TimestampFormat::default();
let receive_timestamp = protocol::TimestampFormat::default();
let origin_timestamp = protocol::TimestampFormat::default();
protocol::Packet {
leap_indicator,
version,
mode,
stratum,
poll,
precision,
root_delay,
root_dispersion,
reference_id,
reference_timestamp,
origin_timestamp,
receive_timestamp,
transmit_timestamp,
}
};
let mut bytes = [0u8; protocol::Packet::PACKED_SIZE_BYTES];
(&mut bytes[..]).write_bytes(&packet)?;
let sock = UdpSocket::bind("0.0.0.0:0").await?;
let sz = sock.send_to(&bytes, addr).await?;
debug!("{:?}", sock.local_addr());
debug!("sent: {}", sz);
let res = sock.recv(&mut bytes[..]).await?;
debug!("recv: {:?}", res);
debug!("{:?}", &bytes[..]);
packet = (&bytes[..]).read_bytes()?;
Ok(packet)
}
pub const EPOCH_DELTA: i64 = 2_208_988_800;
const NTP_SCALE: f64 = std::u32::MAX as f64;
#[derive(Copy, Clone, Debug)]
pub struct Instant {
secs: i64,
subsec_nanos: i32,
}
impl Instant {
pub fn new(secs: i64, subsec_nanos: i32) -> Instant {
if secs > 0 && subsec_nanos < 0 {
panic!("invalid instant: secs was positive but subsec_nanos was negative");
}
if secs < 0 && subsec_nanos > 0 {
panic!("invalid instant: secs was negative but subsec_nanos was positive");
}
Instant { secs, subsec_nanos }
}
pub fn now() -> Self {
match time::SystemTime::now().duration_since(time::UNIX_EPOCH) {
Ok(duration) => {
let secs = duration.as_secs() as i64;
let subsec_nanos = duration.subsec_nanos() as i32;
Instant::new(secs, subsec_nanos)
}
Err(sys_time_err) => {
let duration_pre_unix_epoch = sys_time_err.duration();
let secs = -(duration_pre_unix_epoch.as_secs() as i64);
let subsec_nanos = -(duration_pre_unix_epoch.subsec_nanos() as i32);
Instant::new(secs, subsec_nanos)
}
}
}
pub fn secs(&self) -> i64 {
self.secs
}
pub fn subsec_nanos(&self) -> i32 {
self.subsec_nanos
}
}
impl From<protocol::ShortFormat> for Instant {
fn from(t: protocol::ShortFormat) -> Self {
let secs = t.seconds as i64 - EPOCH_DELTA;
let subsec_nanos = (t.fraction as f64 / NTP_SCALE * 1e9) as i32;
Instant::new(secs, subsec_nanos)
}
}
impl From<protocol::TimestampFormat> for Instant {
fn from(t: protocol::TimestampFormat) -> Self {
let secs = t.seconds as i64 - EPOCH_DELTA;
let subsec_nanos = (t.fraction as f64 / NTP_SCALE * 1e9) as i32;
Instant::new(secs, subsec_nanos)
}
}
impl From<Instant> for protocol::ShortFormat {
fn from(t: Instant) -> Self {
let sec = t.secs() + EPOCH_DELTA;
let frac = t.subsec_nanos() as f64 * NTP_SCALE / 1e10;
protocol::ShortFormat {
seconds: sec as u16,
fraction: frac as u16,
}
}
}
impl From<Instant> for protocol::TimestampFormat {
fn from(t: Instant) -> Self {
let sec = t.secs() + EPOCH_DELTA;
let frac = t.subsec_nanos() as f64 * NTP_SCALE / 1e10;
protocol::TimestampFormat {
seconds: sec as u32,
fraction: frac as u32,
}
}
}
pub async fn get_unix_ntp_time() -> Result<i64> {
let pool_ntp = "pool.ntp.org:123";
let response = request(pool_ntp).await?;
let timestamp = response.transmit_timestamp;
Ok(Instant::from(timestamp).secs())
}