blib/lib/Net/Frame/Layer/NTP.pm

filter: Statementstmt 96.3% 238 / 247 Branchbran 50.0% 11 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 97.0% 66 / 68 Podpod 100% 8 / 8 totaltotal 93.6% 323 / 345 CC 12 SCAR 5.0
CC
12
Cov
93%
_frac2bin
6.0
_getTime
5.9
encapsulate
3.3
CRAP 1.6
↑ Summary
1 #
2 # $Id: NTP.pm 49 2012-11-19 13:15:34Z VinsWorldcom $
3 #
4 package Net::Frame::Layer::NTP;
5 4 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 our $VERSION = '1.02';
8  
9 4 ▶use Net::Frame::Layer qw(:consts :subs);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 our @ISA = qw(Net::Frame::Layer Exporter);
12  
13 4 our %EXPORT_TAGS = (
14 consts => [qw(
15 NF_NTP_ADJ
16 NF_NTP_LI_NOWARN
17 NF_NTP_LI_61
18 NF_NTP_LI_59
19 NF_NTP_LI_ALARM
20 NF_NTP_MODE_RSVD
21 NF_NTP_MODE_SYMACTIVE
22 NF_NTP_MODE_SYMPASSIVE
23 NF_NTP_MODE_CLIENT
24 NF_NTP_MODE_SERVER
25 NF_NTP_MODE_BROADCAST
26 NF_NTP_MODE_NTPCONTROL
27 NF_NTP_MODE_PRIVATE
28 NF_NTP_STRATUM_UNSPEC
29 NF_NTP_STRATUM_PRIMARY
30 NF_NTP_STRATUM_UNSYNC
31 NF_NTP_REFID_GOES
32 NF_NTP_REFID_GPS
33 NF_NTP_REFID_GAL
34 NF_NTP_REFID_PPS
35 NF_NTP_REFID_IRIG
36 NF_NTP_REFID_WWVB
37 NF_NTP_REFID_DCF
38 NF_NTP_REFID_HBG
39 NF_NTP_REFID_MSF
40 NF_NTP_REFID_JJY
41 NF_NTP_REFID_LORC
42 NF_NTP_REFID_TDF
43 NF_NTP_REFID_CHU
44 NF_NTP_REFID_WWV
45 NF_NTP_REFID_WWVH
46 NF_NTP_REFID_NIST
47 NF_NTP_REFID_ACTS
48 NF_NTP_REFID_USNO
49 NF_NTP_REFID_PTB
50 NF_NTP_KoD_ACST
51 NF_NTP_KoD_AUTH
52 NF_NTP_KoD_AUTO
53 NF_NTP_KoD_BCST
54 NF_NTP_KoD_CRYP
55 NF_NTP_KoD_DENY
56 NF_NTP_KoD_DROP
57 NF_NTP_KoD_RSTR
58 NF_NTP_KoD_INIT
59 NF_NTP_KoD_MCST
60 NF_NTP_KoD_NKEY
61 NF_NTP_KoD_RATE
62 NF_NTP_KoD_RMOT
63 NF_NTP_KoD_STEP
64 )],
65 subs => [qw(
66 ntpTimestamp
67 ntp2date
68 )],
69 );
70 our @EXPORT_OK = (
71 4 @{$EXPORT_TAGS{consts}},
72 4 @{$EXPORT_TAGS{subs}},
73 );
74  
75 4 ▶use constant NF_NTP_ADJ => 2208988800;
Subroutine BEGIN: called CC=1 SCAR=0.0
76 4 ▶use constant NF_NTP_LI_NOWARN => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 4 ▶use constant NF_NTP_LI_61 => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 4 ▶use constant NF_NTP_LI_59 => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 4 ▶use constant NF_NTP_LI_ALARM => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 4 ▶use constant NF_NTP_MODE_RSVD => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 4 ▶use constant NF_NTP_MODE_SYMACTIVE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 4 ▶use constant NF_NTP_MODE_SYMPASSIVE => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 4 ▶use constant NF_NTP_MODE_CLIENT => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 4 ▶use constant NF_NTP_MODE_SERVER => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
85 4 ▶use constant NF_NTP_MODE_BROADCAST => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 4 ▶use constant NF_NTP_MODE_NTPCONTROL => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
87 4 ▶use constant NF_NTP_MODE_PRIVATE => 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 4 ▶use constant NF_NTP_STRATUM_UNSPEC => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
89 4 ▶use constant NF_NTP_STRATUM_PRIMARY => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
90 4 ▶use constant NF_NTP_STRATUM_UNSYNC => 16;
Subroutine BEGIN: called CC=1 SCAR=0.0
91 4 ▶use constant NF_NTP_REFID_GOES => 0x474f4553;
Subroutine BEGIN: called CC=1 SCAR=0.0
92 4 ▶use constant NF_NTP_REFID_GPS => 0x47505300;
Subroutine BEGIN: called CC=1 SCAR=0.0
93 4 ▶use constant NF_NTP_REFID_GAL => 0x47414c00;
Subroutine BEGIN: called CC=1 SCAR=0.0
94 4 ▶use constant NF_NTP_REFID_PPS => 0x50505300;
Subroutine BEGIN: called CC=1 SCAR=0.0
95 4 ▶use constant NF_NTP_REFID_IRIG => 0x49524947;
Subroutine BEGIN: called CC=1 SCAR=0.0
96 4 ▶use constant NF_NTP_REFID_WWVB => 0x57575642;
Subroutine BEGIN: called CC=1 SCAR=0.0
97 4 ▶use constant NF_NTP_REFID_DCF => 0x44434600;
Subroutine BEGIN: called CC=1 SCAR=0.0
98 4 ▶use constant NF_NTP_REFID_HBG => 0x48424700;
Subroutine BEGIN: called CC=1 SCAR=0.0
99 4 ▶use constant NF_NTP_REFID_MSF => 0x4d534600;
Subroutine BEGIN: called CC=1 SCAR=0.0
100 4 ▶use constant NF_NTP_REFID_JJY => 0x4a4a5900;
Subroutine BEGIN: called CC=1 SCAR=0.0
101 4 ▶use constant NF_NTP_REFID_LORC => 0x4c4f5243;
Subroutine BEGIN: called CC=1 SCAR=0.0
102 4 ▶use constant NF_NTP_REFID_TDF => 0x54444600;
Subroutine BEGIN: called CC=1 SCAR=0.0
103 4 ▶use constant NF_NTP_REFID_CHU => 0x43485500;
Subroutine BEGIN: called CC=1 SCAR=0.0
104 4 ▶use constant NF_NTP_REFID_WWV => 0x57575600;
Subroutine BEGIN: called CC=1 SCAR=0.0
105 4 ▶use constant NF_NTP_REFID_WWVH => 0x57575648;
Subroutine BEGIN: called CC=1 SCAR=0.0
106 4 ▶use constant NF_NTP_REFID_NIST => 0x4e495354;
Subroutine BEGIN: called CC=1 SCAR=0.0
107 4 ▶use constant NF_NTP_REFID_ACTS => 0x41435453;
Subroutine BEGIN: called CC=1 SCAR=0.0
108 4 ▶use constant NF_NTP_REFID_USNO => 0x55534e4f;
Subroutine BEGIN: called CC=1 SCAR=0.0
109 4 ▶use constant NF_NTP_REFID_PTB => 0x50544200;
Subroutine BEGIN: called CC=1 SCAR=0.0
110 4 ▶use constant NF_NTP_KoD_ACST => 0x41435354;
Subroutine BEGIN: called CC=1 SCAR=0.0
111 4 ▶use constant NF_NTP_KoD_AUTH => 0x41555448;
Subroutine BEGIN: called CC=1 SCAR=0.0
112 4 ▶use constant NF_NTP_KoD_AUTO => 0x4155544f;
Subroutine BEGIN: called CC=1 SCAR=0.0
113 4 ▶use constant NF_NTP_KoD_BCST => 0x42435354;
Subroutine BEGIN: called CC=1 SCAR=0.0
114 4 ▶use constant NF_NTP_KoD_CRYP => 0x43525950;
Subroutine BEGIN: called CC=1 SCAR=0.0
115 4 ▶use constant NF_NTP_KoD_DENY => 0x44454e59;
Subroutine BEGIN: called CC=1 SCAR=0.0
116 4 ▶use constant NF_NTP_KoD_DROP => 0x44524f50;
Subroutine BEGIN: called CC=1 SCAR=0.0
117 4 ▶use constant NF_NTP_KoD_RSTR => 0x52535452;
Subroutine BEGIN: called CC=1 SCAR=0.0
118 4 ▶use constant NF_NTP_KoD_INIT => 0x494e4954;
Subroutine BEGIN: called CC=1 SCAR=0.0
119 4 ▶use constant NF_NTP_KoD_MCST => 0x4d435354;
Subroutine BEGIN: called CC=1 SCAR=0.0
120 4 ▶use constant NF_NTP_KoD_NKEY => 0x4e4b4559;
Subroutine BEGIN: called CC=1 SCAR=0.0
121 4 ▶use constant NF_NTP_KoD_RATE => 0x52415445;
Subroutine BEGIN: called CC=1 SCAR=0.0
122 4 ▶use constant NF_NTP_KoD_RMOT => 0x524d4f54;
Subroutine BEGIN: called CC=1 SCAR=0.0
123 4 ▶use constant NF_NTP_KoD_STEP => 0x53544550;
Subroutine BEGIN: called CC=1 SCAR=0.0
124  
125 4 our @AS = qw(
126 leap
127 version
128 mode
129 stratum
130 poll
131 precision
132 rootDelay
133 rootDisp
134 refId
135 refTime
136 refTime_frac
137 org
138 org_frac
139 rec
140 rec_frac
141 xmt
142 xmt_frac
143 );
144 4 __PACKAGE__->cgBuildIndices;
145 4 __PACKAGE__->cgBuildAccessorsScalar(\@AS);
146  
147 #no strict 'vars';
148  
149 4 ▶use Bit::Vector;
Subroutine BEGIN: called CC=1 SCAR=0.0
150 4 ▶use Time::HiRes qw (time);
Subroutine BEGIN: called CC=1 SCAR=0.0
151  
152 4 $Net::Frame::Layer::UDP::Next->{123} = "NTP";
153  
154 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
155  
156 shift->SUPER::new(
157 1 leap => NF_NTP_LI_NOWARN,
158 version => 3,
159 mode => NF_NTP_MODE_CLIENT,
160 stratum => NF_NTP_STRATUM_UNSPEC,
161 poll => 0,
162 precision => 0,
163 rootDelay => 0,
164 rootDisp => 0,
165 refId => 0,
166 refTime => 0,
167 refTime_frac => 0,
168 org => 0,
169 org_frac => 0,
170 rec => 0,
171 rec_frac => 0,
172 xmt => ntpTimestamp(time),
173 xmt_frac => 0,
174 @_,
175 );
176 }
177  
178 0 ▶sub getLength { 48 }
Subroutine getLength: not called CC=1 SCAR=6.9
Pod documented
179  
180 1 ▶sub pack {
Subroutine pack: called CC=2 SCAR=7.0
Pod documented
181 1 my $self = shift;
182  
183 1 my $leap = Bit::Vector->new_Dec(2, $self->leap);
184 1 my $version = Bit::Vector->new_Dec(3, $self->version);
185 1 my $mode = Bit::Vector->new_Dec(3, $self->mode);
186 1 my $bvlist = $leap->Concat_List($version, $mode);
187  
188 1 ▶ my $raw = $self->SUPER::pack('CCCC N11',
Branch: 1 branch
BranchTrueFalseTotal
my $raw = $self->SUPER::pack("CCCC N11", $bvlist->to_Dec, $self->stratum, $self->poll, $self->precision, $self->rootDelay, $self->rootDisp, $self->refId, $self->refTime, $self->refTime_frac, $self->org, $self->org_frac, $self->rec, $self->rec_frac, $self->xmt, $self->xmt_frac) or return 1 0 1
189 $bvlist->to_Dec,
190 $self->stratum,
191 $self->poll,
192 $self->precision,
193 $self->rootDelay,
194 $self->rootDisp,
195 $self->refId,
196 $self->refTime,
197 $self->refTime_frac,
198 $self->org,
199 $self->org_frac,
200 $self->rec,
201 $self->rec_frac,
202 $self->xmt,
203 $self->xmt_frac,
204 ) or return;
205  
206 1 return $self->raw($raw);
207 }
208  
209 1 ▶sub unpack {
Subroutine unpack: called CC=2 SCAR=6.9
Pod documented
210 1 my $self = shift;
211  
212 1 ▶ my ($bv, $stratum, $poll, $precision,
Branch: 1 branch
BranchTrueFalseTotal
() = $self->SUPER::unpack("CCCC N N H8 N8 a*", $self->raw) or return 1 0 1
213 $rootDelay, $rootDisp,
214 $refId,
215 $refTime, $refTime_frac,
216 $org, $org_frac,
217 $rec, $rec_frac,
218 $xmt, $xmt_frac,
219 $payload) =
220 $self->SUPER::unpack('CCCC N N H8 N8 a*', $self->raw)
221 or return;
222  
223 1 my $bvlist = Bit::Vector->new_Dec(8, $bv);
224 1 $self->leap ($bvlist->Chunk_Read(2,6));
225 1 $self->version($bvlist->Chunk_Read(3,3));
226 1 $self->mode ($bvlist->Chunk_Read(3,0));
227  
228 1 $self->stratum($stratum);
229 1 $self->poll($poll);
230 1 $self->precision($precision);
231 1 $self->rootDelay($rootDelay);
232 1 $self->rootDisp($rootDisp);
233 1 $self->refId(_unpack_refid($stratum, $refId));
234 1 $self->refTime($refTime);
235 1 $self->refTime_frac($refTime_frac);
236 1 $self->org($org);
237 1 $self->org_frac($org_frac);
238 1 $self->rec($rec);
239 1 $self->rec_frac($rec_frac);
240 1 $self->xmt($xmt);
241 1 $self->xmt_frac($xmt_frac);
242  
243 1 $self->payload($payload);
244  
245 1 return $self;
246 }
247  
248 1 ▶sub encapsulate {
Subroutine encapsulate: called CC=3 SCAR=12.0
Pod documented
249 1 my $self = shift;
250  
251 1 ▶ return $self->nextLayer if $self->nextLayer;
Branch: 1 branch
BranchTrueFalseTotal
if $self->nextLayer 0 1 1
252  
253 # Needed?
254 1 ▶ if ($self->payload) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->payload) 0 1 1
255 0 return 'NTP';
256 }
257  
258 1 NF_LAYER_NONE;
259 }
260  
261 1 ▶sub print {
Subroutine print: called CC=1 SCAR=0.0
Pod documented
262 1 my $self = shift;
263  
264 1 my $refTime_frac = _bin2frac(_dec2bin($self->refTime_frac));
265 1 my $org_frac = _bin2frac(_dec2bin($self->org_frac));
266 1 my $rec_frac = _bin2frac(_dec2bin($self->rec_frac));
267 1 my $xmt_frac = _bin2frac(_dec2bin($self->xmt_frac));
268  
269 1 my $l = $self->layer;
270 1 my $buf = sprintf
271 "$l: leap:%d version:%d mode:%d stratum:%d\n".
272 "$l: poll:%d precision:%d\n".
273 "$l: rootDelay:%d rootDisp:%d refId:%s\n".
274 "$l: refTime:%d refTime_frac:%s\n".
275 # "$l: [%s%s]\n".
276 "$l: org:%d org_frac:%s\n".
277 # "$l: [%s%s]\n".
278 "$l: rec:%d rec_frac:%s\n".
279 # "$l: [%s%s]\n".
280 "$l: xmt:%d xmt_frac:%s\n",
281 # "$l: [%s%s]",
282 $self->leap, $self->version, $self->mode, $self->stratum,
283 $self->poll, $self->precision,
284 $self->rootDelay, $self->rootDisp, $self->refId,
285 $self->refTime, $self->refTime_frac,
286 # _getTime($self->refTime + $refTime_frac - NF_NTP_ADJ), substr($refTime_frac, 1),
287 $self->org, $self->org_frac,
288 # _getTime($self->org + $org_frac - NF_NTP_ADJ), substr($org_frac, 1),
289 $self->rec, $self->rec_frac,
290 # _getTime($self->rec + $rec_frac - NF_NTP_ADJ), substr($rec_frac, 1),
291 $self->xmt, $self->xmt_frac;
292 # _getTime($self->xmt + $xmt_frac - NF_NTP_ADJ), substr($xmt_frac, 1);
293  
294 1 return $buf;
295 }
296  
297 ####
298  
299 1 ▶sub ntp2date {
Subroutine ntp2date: called CC=1 SCAR=0.0
Pod documented
300 1 my ($time, $frac) = @_;
301 1 my $adj_frac = _bin2frac(_dec2bin($frac));
302 1 my $ts = _getTime($time + $adj_frac - NF_NTP_ADJ) . substr($adj_frac, 1) . " UTC";
303 1 return $ts
304 }
305  
306 2 ▶sub ntpTimestamp {
Subroutine ntpTimestamp: called CC=1 SCAR=0.0
Pod documented
307 2 return int(shift() + NF_NTP_ADJ);
308 }
309  
310 1 ▶sub _unpack_refid {
Subroutine _unpack_refid: called CC=2 SCAR=7.4
311 1 my $stratum = shift;
312 1 my $raw_id = shift;
313 1 ▶ if ($stratum < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stratum < 2) 1 0 1
314 1 return CORE::unpack("A4", CORE::pack("H8", $raw_id));
315 }
316 0 return sprintf("%d.%d.%d.%d", CORE::unpack("C4", CORE::pack("H8", $raw_id)));
317 }
318  
319 5 ▶sub _dec2bin {
Subroutine _dec2bin: called CC=1 SCAR=0.0
320 5 my $str = CORE::unpack("B32", CORE::pack("N", shift));
321 5 return $str;
322 }
323  
324 0 ▶sub _frac2bin {
Subroutine _frac2bin: not called CC=2 SCAR=17.9
325 0 my $bin = '';
326 0 my $frac = shift;
327 0 ▶ while (length($bin) < 32) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $bin < 32) 0 0 0
328 0 $bin = $bin . int($frac * 2);
329 0 $frac = ($frac * 2) - (int($frac * 2));
330 }
331 0 return $bin;
332 }
333  
334 5 ▶sub _bin2frac {
Subroutine _bin2frac: called CC=2 SCAR=6.9
335 5 my @bin = split '', shift;
336 5 my $frac = 0;
337 5 ▶ while (@bin) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bin) 160 5 165
338 160 $frac = ($frac + pop @bin) / 2;
339 }
340 5 return $frac;
341 }
342  
343 1 ▶sub _getTime {
Subroutine _getTime: called CC=5 SCAR=17.8
344 1 my @time = gmtime(shift);
345 1 my @month = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
346 1 ▶ my $ts =
Branch: 4 branches
BranchTrueFalseTotal
$time[3] < 10 ? : 0 1 1
$time[2] < 10 ? : 0 1 1
$time[1] < 10 ? : 0 1 1
$time[0] < 10 ? : 0 1 1
347 $month[ $time[4] ] . " "
348 . ( ( $time[3] < 10 ) ? ( " " . $time[3] ) : $time[3] ) . " "
349 . (1900 + $time[5]) . " "
350 . ( ( $time[2] < 10 ) ? ( "0" . $time[2] ) : $time[2] ) . ":"
351 . ( ( $time[1] < 10 ) ? ( "0" . $time[1] ) : $time[1] ) . ":"
352 . ( ( $time[0] < 10 ) ? ( "0" . $time[0] ) : $time[0] );
353  
354 1 return $ts
355 }
356  
357 4 1;
358  
359 __END__
↑ Summary