| 1 | # Net::Time.pm | ||||||||||||||||||||
| 2 | # | ||||||||||||||||||||
| 3 | # Copyright (C) 1995-2004 Graham Barr. All rights reserved. | ||||||||||||||||||||
| 4 | # Copyright (C) 2014, 2020 Steve Hay. All rights reserved. | ||||||||||||||||||||
| 5 | # This module is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||
| 6 | # the same terms as Perl itself, i.e. under the terms of either the GNU General | ||||||||||||||||||||
| 7 | # Public License or the Artistic License, as specified in the F<LICENCE> file. | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | package Net::Time; | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 2 | ▶ | use 5.008001; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 2 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 2 | ▶ | use Exporter; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | 2 | ▶ | use IO::Select; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 2 | ▶ | use IO::Socket; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | 2 | ▶ | use Net::Config; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 2 | our @ISA = qw(Exporter); | |||||||||||||||||||
| 23 | 2 | our @EXPORT_OK = qw(inet_time inet_daytime); | |||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 2 | our $VERSION = "3.16"; | |||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 2 | our $TIMEOUT = 120; | |||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 5 | ▶ | sub _socket { | ||||||||||||||||||
|
Subroutine
_socket: called CC=10 SCAR=23.3
| |||||||||||||||||||||
| 30 | 5 | my ($pname, $pnum, $host, $proto, $timeout) = @_; | |||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 5 | ▶ | $proto ||= 'udp'; | ||||||||||||||||||
|
Condition100%2/2
$proto ||= "udp"
MC/DC100%1/1
$proto ||= "udp"
$proto
Truth table100%2/2
A
A= $proto
| |||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 5 | ▶ | my $port = (getservbyname($pname, $proto))[2] || $pnum; | ||||||||||||||||||
|
Condition33%1/3
(getservbyname $pname, $proto)[2] || $pnum
MC/DC0%0/2
(getservbyname $pname, $proto)[2] || $pnum
(getservbyname $pname, $proto)[2]
$pnum
Truth table33%1/3
A || B
A= (getservbyname $pname, $proto)[2]B= $pnum
| |||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 5 | ▶ | my $hosts = defined $host ? [$host] : $NetConfig{$pname . '_hosts'}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 5 | my $me; | |||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 5 | foreach my $addr (@$hosts) { | |||||||||||||||||||
| 41 | 5 | ▶ | $me = IO::Socket::INET->new( | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 42 | PeerAddr => $addr, | ||||||||||||||||||||
| 43 | PeerPort => $port, | ||||||||||||||||||||
| 44 | Proto => $proto | ||||||||||||||||||||
| 45 | ) | ||||||||||||||||||||
| 46 | and last; | ||||||||||||||||||||
| 47 | } | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 5 | ▶ | return unless $me; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 4 | ▶ | $me->send("\n") | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 52 | if $proto eq 'udp'; | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 4 | ▶ | $timeout = $TIMEOUT | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 55 | unless defined $timeout; | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 4 | ▶ | IO::Select->new($me)->can_read($timeout) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 58 | ? $me | ||||||||||||||||||||
| 59 | : undef; | ||||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 1 | ▶ | sub inet_time { | ||||||||||||||||||
|
Subroutine
inet_time: called CC=4 SCAR=14.6
Pod
documented
| |||||||||||||||||||||
| 64 | 1 | ▶ | my $s = _socket('time', 37, @_) || return; | ||||||||||||||||||
|
Condition50%1/2
_socket('time', 37, @_) || (return)
MC/DC0%0/1
_socket('time', 37, @_) || (return)
_socket('time', 37, @_)
Truth table50%1/2
A
A= _socket('time', 37, @_)
| |||||||||||||||||||||
| 65 | 1 | my $buf = ''; | |||||||||||||||||||
| 66 | 1 | my $offset = 0 | 0; | |||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | return | ||||||||||||||||||||
| 69 | 1 | ▶ | unless defined $s->recv($buf, length(pack("N", 0))); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | # unpack, we | 0 to ensure we have an unsigned | ||||||||||||||||||||
| 72 | 1 | my $time = (unpack("N", $buf))[0] | 0; | |||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | # the time protocol return time in seconds since 1900, convert | ||||||||||||||||||||
| 75 | # it to a the required format | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 1 | ▶ | if ($^O eq "MacOS") { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | # MacOS return seconds since 1904, 1900 was not a leap year. | ||||||||||||||||||||
| 80 | 0 | $offset = (4 * 31536000) | 0; | |||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | else { | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | # otherwise return seconds since 1972, there were 17 leap years between | ||||||||||||||||||||
| 85 | # 1900 and 1972 | ||||||||||||||||||||
| 86 | 1 | $offset = (70 * 31536000 + 17 * 86400) | 0; | |||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 1 | $time - $offset; | |||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 1 | ▶ | sub inet_daytime { | ||||||||||||||||||
|
Subroutine
inet_daytime: called CC=3 SCAR=11.7
Pod
documented
| |||||||||||||||||||||
| 94 | 1 | ▶ | my $s = _socket('daytime', 13, @_) || return; | ||||||||||||||||||
|
Condition50%1/2
_socket('daytime', 13, @_) || (return)
MC/DC0%0/1
_socket('daytime', 13, @_) || (return)
_socket('daytime', 13, @_)
Truth table50%1/2
A
A= _socket('daytime', 13, @_)
| |||||||||||||||||||||
| 95 | 1 | my $buf = ''; | |||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 1 | ▶ | defined($s->recv($buf, 1024)) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 98 | ? $buf | ||||||||||||||||||||
| 99 | : undef; | ||||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 2 | 1; | |||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | __END__ | ||||||||||||||||||||