blib/lib/Net/NTP.pm

filter: Statementstmt 38.1% 40 / 105 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 53.8% 7 / 13 Podpod 0.0% 0 / 1 totaltotal 32.4% 47 / 145 SCAR 42.3
CC
13
Cov
31%
get_ntp_response
90.0
__ANON__
6.0
__ANON__
6.0
CRAP 68.5
↑ Summary
1 package Net::NTP;
2 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use constant HAVE_SOCKET_INET6 => eval { require IO::Socket::INET6 };
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 require Exporter;
11  
12 1 our @ISA = qw(Exporter);
13  
14 1 our @EXPORT = qw(
15 get_ntp_response
16 );
17  
18 1 our $VERSION = '1.4';
19  
20 1 our $TIMEOUT = 5;
21  
22 1 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 1 our %STRATUM = (
34 '0' => 'unspecified or unavailable',
35 '1' => 'primary reference (e.g., radio clock)',
36 );
37  
38 1 for (2 .. 15) {
39 14 $STRATUM{$_} = 'secondary reference (via NTP or SNTP)';
40 }
41  
42 1 for (16 .. 255) {
43 240 $STRATUM{$_} = 'reserved';
44 }
45  
46 1 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 1 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 ▶use constant NTP_ADJ => 2208988800;
Subroutine BEGIN: called CC=1 SCAR=0.0
75  
76 1 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 0 ▶ my $bin = '';
Subroutine __ANON__: not called CC=2 SCAR=17.9
94 0 my $frac = shift;
95 0 ▶ while (length($bin) < 32) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $bin < 32) 0 0 0
96 0 $bin = $bin . int($frac * 2);
97 0 $frac = ($frac * 2) - (int($frac * 2));
98 }
99 0 return $bin;
100 1 };
101  
102 my $bin2frac = sub {
103 0 ▶ my @bin = split '', shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
104 0 my $frac = 0;
105 0 ▶ while (@bin) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bin) 0 0 0
106 0 $frac = ($frac + pop @bin) / 2;
107 }
108 0 return $frac;
109 1 };
110  
111 my $percision = sub {
112 0 ▶ my $number = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
113 0 ▶ if ($number > 127) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number > 127) 0 0 0
114 0 $number -= 255;
115 }
116 0 return sprintf("%1.4e", 2**$number);
117 1 };
118  
119 my $unpack_ip = sub {
120 0 ▶ my $ip;
Subroutine __ANON__: not called CC=2 SCAR=17.9
121 0 my $stratum = shift;
122 0 my $tmp_ip = shift;
123 0 ▶ if ($stratum < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stratum < 2) { } 0 0 0
124 0 $ip = unpack("A4", pack("H8", $tmp_ip));
125 }
126 else {
127 0 $ip = sprintf("%d.%d.%d.%d", unpack("C4", pack("H8", $tmp_ip)));
128 }
129 0 return $ip;
130 1 };
131  
132  
133 sub get_ntp_response {
134  
135 0 ▶ my $host = shift || 'localhost';
Subroutine get_ntp_response: not called CC=9 SCAR=45.0
Pod undocumented
Condition0%0/2
shift() || 'localhost'
l!l
00
MC/DC0%0/1
shift() || 'localhost'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
136 0 ▶ my $port = shift || 'ntp';
Condition0%0/2
shift() || 'ntp'
l!l
00
MC/DC0%0/1
shift() || 'ntp'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
137  
138 0 my %args = (
139 Proto => 'udp',
140 PeerHost => $host,
141 PeerPort => $port
142 );
143 0 my $sock;
144 0 ▶ if (HAVE_SOCKET_INET6) {
Branch: 1 branch
BranchTrueFalseTotal
if (&HAVE_SOCKET_INET6()) { } 0 0 0
145 0 $sock = IO::Socket::INET6->new(%args);
146 }
147 else {
148 0 $sock = IO::Socket::INET->new(%args);
149 }
150 0 ▶ die $@ unless $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless $sock 0 0 0
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 ▶ $sock->send($ntp_msg)
Branch: 1 branch
BranchTrueFalseTotal
$sock->send($ntp_msg) or die "send() failed: $!\n" 0 0 0
164 or die "send() failed: $!\n";
165  
166 0 eval {
167 0 ▶ local $SIG{ALRM} = sub { die "Net::NTP timed out getting NTP packet\n"; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
168 0 alarm($TIMEOUT);
169 0 ▶ $sock->recv($data, 960)
Branch: 1 branch
BranchTrueFalseTotal
$sock->recv($data, 960) or die "recv() failed: $!\n" 0 0 0
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 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 0 0
178 0 ▶ return if $err =~ m/^Net::NTP timed out/;
Branch: 1 branch
BranchTrueFalseTotal
if $err =~ /^Net::NTP timed out/ 0 0 0
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 @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 0 (($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 1;
224 __END__
↑ Summary