line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::NTP; |
2
|
1
|
|
|
1
|
|
22435
|
use 5.008; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
41
|
|
3
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
38
|
|
4
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
43
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
699
|
use IO::Socket; |
|
1
|
|
|
|
|
424961
|
|
|
1
|
|
|
|
|
4
|
|
7
|
1
|
|
|
1
|
|
564
|
use constant HAVE_SOCKET_INET6 => eval { require IO::Socket::INET6 }; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
242
|
|
8
|
1
|
|
|
1
|
|
578
|
use Time::HiRes qw(time); |
|
1
|
|
|
|
|
1397
|
|
|
1
|
|
|
|
|
4
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
require Exporter; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our @EXPORT = qw( |
15
|
|
|
|
|
|
|
get_ntp_response |
16
|
|
|
|
|
|
|
); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
our $VERSION = '1.4'; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
our $TIMEOUT = 5; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
our %MODE = ( |
23
|
|
|
|
|
|
|
'0' => 'reserved', |
24
|
|
|
|
|
|
|
'1' => 'symmetric active', |
25
|
|
|
|
|
|
|
'2' => 'symmetric passive', |
26
|
|
|
|
|
|
|
'3' => 'client', |
27
|
|
|
|
|
|
|
'4' => 'server', |
28
|
|
|
|
|
|
|
'5' => 'broadcast', |
29
|
|
|
|
|
|
|
'6' => 'reserved for NTP control message', |
30
|
|
|
|
|
|
|
'7' => 'reserved for private use' |
31
|
|
|
|
|
|
|
); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
our %STRATUM = ( |
34
|
|
|
|
|
|
|
'0' => 'unspecified or unavailable', |
35
|
|
|
|
|
|
|
'1' => 'primary reference (e.g., radio clock)', |
36
|
|
|
|
|
|
|
); |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
for (2 .. 15) { |
39
|
|
|
|
|
|
|
$STRATUM{$_} = 'secondary reference (via NTP or SNTP)'; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
for (16 .. 255) { |
43
|
|
|
|
|
|
|
$STRATUM{$_} = 'reserved'; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
our %STRATUM_ONE_TEXT = ( |
47
|
|
|
|
|
|
|
'LOCL' => |
48
|
|
|
|
|
|
|
'uncalibrated local clock used as a primary reference for a subnet without external means of synchronization', |
49
|
|
|
|
|
|
|
'PPS' => |
50
|
|
|
|
|
|
|
'atomic clock or other pulse-per-second source individually calibrated to national standards', |
51
|
|
|
|
|
|
|
'ACTS' => 'NIST dialup modem service', |
52
|
|
|
|
|
|
|
'USNO' => 'USNO modem service', |
53
|
|
|
|
|
|
|
'PTB' => 'PTB (Germany) modem service', |
54
|
|
|
|
|
|
|
'TDF' => 'Allouis (France) Radio 164 kHz', |
55
|
|
|
|
|
|
|
'DCF' => 'Mainflingen (Germany) Radio 77.5 kHz', |
56
|
|
|
|
|
|
|
'MSF' => 'Rugby (UK) Radio 60 kHz', |
57
|
|
|
|
|
|
|
'WWV' => 'Ft. Collins (US) Radio 2.5, 5, 10, 15, 20 MHz', |
58
|
|
|
|
|
|
|
'WWVB' => 'Boulder (US) Radio 60 kHz', |
59
|
|
|
|
|
|
|
'WWVH' => 'Kaui Hawaii (US) Radio 2.5, 5, 10, 15 MHz', |
60
|
|
|
|
|
|
|
'CHU' => 'Ottawa (Canada) Radio 3330, 7335, 14670 kHz', |
61
|
|
|
|
|
|
|
'LORC' => 'LORAN-C radionavigation system', |
62
|
|
|
|
|
|
|
'OMEG' => 'OMEGA radionavigation system', |
63
|
|
|
|
|
|
|
'GPS' => 'Global Positioning Service', |
64
|
|
|
|
|
|
|
'GOES' => 'Geostationary Orbit Environment Satellite', |
65
|
|
|
|
|
|
|
); |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
our %LEAP_INDICATOR = ( |
68
|
|
|
|
|
|
|
'0' => 'no warning', |
69
|
|
|
|
|
|
|
'1' => 'last minute has 61 seconds', |
70
|
|
|
|
|
|
|
'2' => 'last minute has 59 seconds)', |
71
|
|
|
|
|
|
|
'3' => 'alarm condition (clock not synchronized)' |
72
|
|
|
|
|
|
|
); |
73
|
|
|
|
|
|
|
|
74
|
1
|
|
|
1
|
|
341
|
use constant NTP_ADJ => 2208988800; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
794
|
|
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
my @ntp_packet_fields = ( |
77
|
|
|
|
|
|
|
'Leap Indicator', |
78
|
|
|
|
|
|
|
'Version Number', |
79
|
|
|
|
|
|
|
'Mode', |
80
|
|
|
|
|
|
|
'Stratum', |
81
|
|
|
|
|
|
|
'Poll Interval', |
82
|
|
|
|
|
|
|
'Precision', |
83
|
|
|
|
|
|
|
'Root Delay', |
84
|
|
|
|
|
|
|
'Root Dispersion', |
85
|
|
|
|
|
|
|
'Reference Clock Identifier', |
86
|
|
|
|
|
|
|
'Reference Timestamp', |
87
|
|
|
|
|
|
|
'Originate Timestamp', |
88
|
|
|
|
|
|
|
'Receive Timestamp', |
89
|
|
|
|
|
|
|
'Transmit Timestamp', |
90
|
|
|
|
|
|
|
); |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
my $frac2bin = sub { |
93
|
|
|
|
|
|
|
my $bin = ''; |
94
|
|
|
|
|
|
|
my $frac = shift; |
95
|
|
|
|
|
|
|
while (length($bin) < 32) { |
96
|
|
|
|
|
|
|
$bin = $bin . int($frac * 2); |
97
|
|
|
|
|
|
|
$frac = ($frac * 2) - (int($frac * 2)); |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
return $bin; |
100
|
|
|
|
|
|
|
}; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
my $bin2frac = sub { |
103
|
|
|
|
|
|
|
my @bin = split '', shift; |
104
|
|
|
|
|
|
|
my $frac = 0; |
105
|
|
|
|
|
|
|
while (@bin) { |
106
|
|
|
|
|
|
|
$frac = ($frac + pop @bin) / 2; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
return $frac; |
109
|
|
|
|
|
|
|
}; |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
my $percision = sub { |
112
|
|
|
|
|
|
|
my $number = shift; |
113
|
|
|
|
|
|
|
if ($number > 127) { |
114
|
|
|
|
|
|
|
$number -= 255; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
return sprintf("%1.4e", 2**$number); |
117
|
|
|
|
|
|
|
}; |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
my $unpack_ip = sub { |
120
|
|
|
|
|
|
|
my $ip; |
121
|
|
|
|
|
|
|
my $stratum = shift; |
122
|
|
|
|
|
|
|
my $tmp_ip = shift; |
123
|
|
|
|
|
|
|
if ($stratum < 2) { |
124
|
|
|
|
|
|
|
$ip = unpack("A4", pack("H8", $tmp_ip)); |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
else { |
127
|
|
|
|
|
|
|
$ip = sprintf("%d.%d.%d.%d", unpack("C4", pack("H8", $tmp_ip))); |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
return $ip; |
130
|
|
|
|
|
|
|
}; |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
sub get_ntp_response { |
134
|
|
|
|
|
|
|
|
135
|
0
|
|
0
|
0
|
0
|
|
my $host = shift || 'localhost'; |
136
|
0
|
|
0
|
|
|
|
my $port = shift || 'ntp'; |
137
|
|
|
|
|
|
|
|
138
|
0
|
|
|
|
|
|
my %args = ( |
139
|
|
|
|
|
|
|
Proto => 'udp', |
140
|
|
|
|
|
|
|
PeerHost => $host, |
141
|
|
|
|
|
|
|
PeerPort => $port |
142
|
|
|
|
|
|
|
); |
143
|
0
|
|
|
|
|
|
my $sock; |
144
|
0
|
0
|
|
|
|
|
if (HAVE_SOCKET_INET6) { |
145
|
0
|
|
|
|
|
|
$sock = IO::Socket::INET6->new(%args); |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
else { |
148
|
0
|
|
|
|
|
|
$sock = IO::Socket::INET->new(%args); |
149
|
|
|
|
|
|
|
} |
150
|
0
|
0
|
|
|
|
|
die $@ unless $sock; |
151
|
|
|
|
|
|
|
|
152
|
0
|
|
|
|
|
|
my %tmp_pkt; |
153
|
|
|
|
|
|
|
my %packet; |
154
|
0
|
|
|
|
|
|
my $data; |
155
|
|
|
|
|
|
|
|
156
|
0
|
|
|
|
|
|
my $client_localtime = time; |
157
|
0
|
|
|
|
|
|
my $client_adj_localtime = $client_localtime + NTP_ADJ; |
158
|
0
|
|
|
|
|
|
my $client_frac_localtime = $frac2bin->($client_adj_localtime); |
159
|
|
|
|
|
|
|
|
160
|
0
|
|
|
|
|
|
my $ntp_msg = |
161
|
|
|
|
|
|
|
pack("B8 C3 N10 B32", '00011011', (0) x 12, int($client_adj_localtime), $client_frac_localtime); |
162
|
|
|
|
|
|
|
|
163
|
0
|
0
|
|
|
|
|
$sock->send($ntp_msg) |
164
|
|
|
|
|
|
|
or die "send() failed: $!\n"; |
165
|
|
|
|
|
|
|
|
166
|
0
|
|
|
|
|
|
eval { |
167
|
0
|
|
|
0
|
|
|
local $SIG{ALRM} = sub { die "Net::NTP timed out getting NTP packet\n"; }; |
|
0
|
|
|
|
|
|
|
168
|
0
|
|
|
|
|
|
alarm($TIMEOUT); |
169
|
0
|
0
|
|
|
|
|
$sock->recv($data, 960) |
170
|
|
|
|
|
|
|
or die "recv() failed: $!\n"; |
171
|
0
|
|
|
|
|
|
alarm(0); |
172
|
|
|
|
|
|
|
}; |
173
|
0
|
|
|
|
|
|
alarm 0; |
174
|
|
|
|
|
|
|
|
175
|
0
|
|
|
|
|
|
my $client_recvtime = time; |
176
|
|
|
|
|
|
|
|
177
|
0
|
0
|
|
|
|
|
if (my $err = $@) { |
178
|
0
|
0
|
|
|
|
|
return if $err =~ m/^Net::NTP timed out/; |
179
|
0
|
|
|
|
|
|
die $err; |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
0
|
|
|
|
|
|
my @ntp_fields = qw/byte1 stratum poll precision/; |
183
|
0
|
|
|
|
|
|
push @ntp_fields, qw/delay delay_fb disp disp_fb ident/; |
184
|
0
|
|
|
|
|
|
push @ntp_fields, qw/ref_time ref_time_fb/; |
185
|
0
|
|
|
|
|
|
push @ntp_fields, qw/org_time org_time_fb/; |
186
|
0
|
|
|
|
|
|
push @ntp_fields, qw/recv_time recv_time_fb/; |
187
|
0
|
|
|
|
|
|
push @ntp_fields, qw/trans_time trans_time_fb/; |
188
|
|
|
|
|
|
|
|
189
|
0
|
|
|
|
|
|
@tmp_pkt{@ntp_fields} = unpack("a C3 n B16 n B16 H8 N B32 N B32 N B32 N B32", $data); |
190
|
|
|
|
|
|
|
|
191
|
0
|
|
|
|
|
|
@packet{@ntp_packet_fields} = ( |
192
|
|
|
|
|
|
|
(unpack("C", $tmp_pkt{byte1} & "\xC0") >> 6), |
193
|
|
|
|
|
|
|
(unpack("C", $tmp_pkt{byte1} & "\x38") >> 3), |
194
|
|
|
|
|
|
|
(unpack("C", $tmp_pkt{byte1} & "\x07")), |
195
|
|
|
|
|
|
|
$tmp_pkt{stratum}, |
196
|
|
|
|
|
|
|
(sprintf("%0.4f", $tmp_pkt{poll})), |
197
|
|
|
|
|
|
|
$tmp_pkt{precision} - 255, |
198
|
|
|
|
|
|
|
($bin2frac->($tmp_pkt{delay_fb})), |
199
|
|
|
|
|
|
|
(sprintf("%0.4f", $tmp_pkt{disp})), |
200
|
|
|
|
|
|
|
$unpack_ip->($tmp_pkt{stratum}, $tmp_pkt{ident}), |
201
|
|
|
|
|
|
|
(($tmp_pkt{ref_time} += $bin2frac->($tmp_pkt{ref_time_fb})) -= NTP_ADJ), |
202
|
|
|
|
|
|
|
(($tmp_pkt{org_time} += $bin2frac->($tmp_pkt{org_time_fb})) -= NTP_ADJ), |
203
|
|
|
|
|
|
|
(($tmp_pkt{recv_time} += $bin2frac->($tmp_pkt{recv_time_fb})) -= NTP_ADJ), |
204
|
|
|
|
|
|
|
(($tmp_pkt{trans_time} += $bin2frac->($tmp_pkt{trans_time_fb})) -= NTP_ADJ) |
205
|
|
|
|
|
|
|
); |
206
|
|
|
|
|
|
|
|
207
|
0
|
|
|
|
|
|
my $dest_org = sprintf "%0.5f", (($client_recvtime - $client_localtime)); |
208
|
0
|
|
|
|
|
|
my $recv_trans = sprintf "%0.5f", ($packet{'Receive Timestamp'} - $packet{'Transmit Timestamp'}); |
209
|
0
|
|
|
|
|
|
my $delay = sprintf "%0.5f", ($dest_org + $recv_trans); |
210
|
|
|
|
|
|
|
|
211
|
0
|
|
|
|
|
|
my $recv_org = $packet{'Receive Timestamp'} - $client_recvtime; |
212
|
0
|
|
|
|
|
|
my $trans_dest = $packet{'Transmit Timestamp'} - $client_localtime; |
213
|
0
|
|
|
|
|
|
my $offset = ($recv_org + $trans_dest) / 2; |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
# Calculated offset / delay |
216
|
0
|
|
|
|
|
|
$packet{Offset} = $offset; |
217
|
0
|
|
|
|
|
|
$packet{Delay} = $delay; |
218
|
|
|
|
|
|
|
|
219
|
0
|
|
|
|
|
|
return %packet; |
220
|
|
|
|
|
|
|
} |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
1; |
224
|
|
|
|
|
|
|
__END__ |