1*0Sstevel@tonic-gate# Net::Time.pm 2*0Sstevel@tonic-gate# 3*0Sstevel@tonic-gate# Copyright (c) 1995-1998 Graham Barr <gbarr@pobox.com>. All rights reserved. 4*0Sstevel@tonic-gate# This program is free software; you can redistribute it and/or 5*0Sstevel@tonic-gate# modify it under the same terms as Perl itself. 6*0Sstevel@tonic-gate 7*0Sstevel@tonic-gatepackage Net::Time; 8*0Sstevel@tonic-gate 9*0Sstevel@tonic-gateuse strict; 10*0Sstevel@tonic-gateuse vars qw($VERSION @ISA @EXPORT_OK $TIMEOUT); 11*0Sstevel@tonic-gateuse Carp; 12*0Sstevel@tonic-gateuse IO::Socket; 13*0Sstevel@tonic-gaterequire Exporter; 14*0Sstevel@tonic-gateuse Net::Config; 15*0Sstevel@tonic-gateuse IO::Select; 16*0Sstevel@tonic-gate 17*0Sstevel@tonic-gate@ISA = qw(Exporter); 18*0Sstevel@tonic-gate@EXPORT_OK = qw(inet_time inet_daytime); 19*0Sstevel@tonic-gate 20*0Sstevel@tonic-gate$VERSION = "2.09"; # $Id: //depot/libnet/Net/Time.pm#9 $ 21*0Sstevel@tonic-gate 22*0Sstevel@tonic-gate$TIMEOUT = 120; 23*0Sstevel@tonic-gate 24*0Sstevel@tonic-gatesub _socket 25*0Sstevel@tonic-gate{ 26*0Sstevel@tonic-gate my($pname,$pnum,$host,$proto,$timeout) = @_; 27*0Sstevel@tonic-gate 28*0Sstevel@tonic-gate $proto ||= 'udp'; 29*0Sstevel@tonic-gate 30*0Sstevel@tonic-gate my $port = (getservbyname($pname, $proto))[2] || $pnum; 31*0Sstevel@tonic-gate 32*0Sstevel@tonic-gate my $hosts = defined $host ? [ $host ] : $NetConfig{$pname . '_hosts'}; 33*0Sstevel@tonic-gate 34*0Sstevel@tonic-gate my $me; 35*0Sstevel@tonic-gate 36*0Sstevel@tonic-gate foreach $host (@$hosts) 37*0Sstevel@tonic-gate { 38*0Sstevel@tonic-gate $me = IO::Socket::INET->new(PeerAddr => $host, 39*0Sstevel@tonic-gate PeerPort => $port, 40*0Sstevel@tonic-gate Proto => $proto 41*0Sstevel@tonic-gate ) and last; 42*0Sstevel@tonic-gate } 43*0Sstevel@tonic-gate 44*0Sstevel@tonic-gate return unless $me; 45*0Sstevel@tonic-gate 46*0Sstevel@tonic-gate $me->send("\n") 47*0Sstevel@tonic-gate if $proto eq 'udp'; 48*0Sstevel@tonic-gate 49*0Sstevel@tonic-gate $timeout = $TIMEOUT 50*0Sstevel@tonic-gate unless defined $timeout; 51*0Sstevel@tonic-gate 52*0Sstevel@tonic-gate IO::Select->new($me)->can_read($timeout) 53*0Sstevel@tonic-gate ? $me 54*0Sstevel@tonic-gate : undef; 55*0Sstevel@tonic-gate} 56*0Sstevel@tonic-gate 57*0Sstevel@tonic-gatesub inet_time 58*0Sstevel@tonic-gate{ 59*0Sstevel@tonic-gate my $s = _socket('time',37,@_) || return undef; 60*0Sstevel@tonic-gate my $buf = ''; 61*0Sstevel@tonic-gate my $offset = 0 | 0; 62*0Sstevel@tonic-gate 63*0Sstevel@tonic-gate return undef 64*0Sstevel@tonic-gate unless $s->recv($buf, length(pack("N",0))); 65*0Sstevel@tonic-gate 66*0Sstevel@tonic-gate # unpack, we | 0 to ensure we have an unsigned 67*0Sstevel@tonic-gate my $time = (unpack("N",$buf))[0] | 0; 68*0Sstevel@tonic-gate 69*0Sstevel@tonic-gate # the time protocol return time in seconds since 1900, convert 70*0Sstevel@tonic-gate # it to a the required format 71*0Sstevel@tonic-gate 72*0Sstevel@tonic-gate if($^O eq "MacOS") { 73*0Sstevel@tonic-gate # MacOS return seconds since 1904, 1900 was not a leap year. 74*0Sstevel@tonic-gate $offset = (4 * 31536000) | 0; 75*0Sstevel@tonic-gate } 76*0Sstevel@tonic-gate else { 77*0Sstevel@tonic-gate # otherwise return seconds since 1972, there were 17 leap years between 78*0Sstevel@tonic-gate # 1900 and 1972 79*0Sstevel@tonic-gate $offset = (70 * 31536000 + 17 * 86400) | 0; 80*0Sstevel@tonic-gate } 81*0Sstevel@tonic-gate 82*0Sstevel@tonic-gate $time - $offset; 83*0Sstevel@tonic-gate} 84*0Sstevel@tonic-gate 85*0Sstevel@tonic-gatesub inet_daytime 86*0Sstevel@tonic-gate{ 87*0Sstevel@tonic-gate my $s = _socket('daytime',13,@_) || return undef; 88*0Sstevel@tonic-gate my $buf = ''; 89*0Sstevel@tonic-gate 90*0Sstevel@tonic-gate $s->recv($buf, 1024) ? $buf 91*0Sstevel@tonic-gate : undef; 92*0Sstevel@tonic-gate} 93*0Sstevel@tonic-gate 94*0Sstevel@tonic-gate1; 95*0Sstevel@tonic-gate 96*0Sstevel@tonic-gate__END__ 97*0Sstevel@tonic-gate 98*0Sstevel@tonic-gate=head1 NAME 99*0Sstevel@tonic-gate 100*0Sstevel@tonic-gateNet::Time - time and daytime network client interface 101*0Sstevel@tonic-gate 102*0Sstevel@tonic-gate=head1 SYNOPSIS 103*0Sstevel@tonic-gate 104*0Sstevel@tonic-gate use Net::Time qw(inet_time inet_daytime); 105*0Sstevel@tonic-gate 106*0Sstevel@tonic-gate print inet_time(); # use default host from Net::Config 107*0Sstevel@tonic-gate print inet_time('localhost'); 108*0Sstevel@tonic-gate print inet_time('localhost', 'tcp'); 109*0Sstevel@tonic-gate 110*0Sstevel@tonic-gate print inet_daytime(); # use default host from Net::Config 111*0Sstevel@tonic-gate print inet_daytime('localhost'); 112*0Sstevel@tonic-gate print inet_daytime('localhost', 'tcp'); 113*0Sstevel@tonic-gate 114*0Sstevel@tonic-gate=head1 DESCRIPTION 115*0Sstevel@tonic-gate 116*0Sstevel@tonic-gateC<Net::Time> provides subroutines that obtain the time on a remote machine. 117*0Sstevel@tonic-gate 118*0Sstevel@tonic-gate=over 4 119*0Sstevel@tonic-gate 120*0Sstevel@tonic-gate=item inet_time ( [HOST [, PROTOCOL [, TIMEOUT]]]) 121*0Sstevel@tonic-gate 122*0Sstevel@tonic-gateObtain the time on C<HOST>, or some default host if C<HOST> is not given 123*0Sstevel@tonic-gateor not defined, using the protocol as defined in RFC868. The optional 124*0Sstevel@tonic-gateargument C<PROTOCOL> should define the protocol to use, either C<tcp> or 125*0Sstevel@tonic-gateC<udp>. The result will be a time value in the same units as returned 126*0Sstevel@tonic-gateby time() or I<undef> upon failure. 127*0Sstevel@tonic-gate 128*0Sstevel@tonic-gate=item inet_daytime ( [HOST [, PROTOCOL [, TIMEOUT]]]) 129*0Sstevel@tonic-gate 130*0Sstevel@tonic-gateObtain the time on C<HOST>, or some default host if C<HOST> is not given 131*0Sstevel@tonic-gateor not defined, using the protocol as defined in RFC867. The optional 132*0Sstevel@tonic-gateargument C<PROTOCOL> should define the protocol to use, either C<tcp> or 133*0Sstevel@tonic-gateC<udp>. The result will be an ASCII string or I<undef> upon failure. 134*0Sstevel@tonic-gate 135*0Sstevel@tonic-gate=back 136*0Sstevel@tonic-gate 137*0Sstevel@tonic-gate=head1 AUTHOR 138*0Sstevel@tonic-gate 139*0Sstevel@tonic-gateGraham Barr <gbarr@pobox.com> 140*0Sstevel@tonic-gate 141*0Sstevel@tonic-gate=head1 COPYRIGHT 142*0Sstevel@tonic-gate 143*0Sstevel@tonic-gateCopyright (c) 1995-1998 Graham Barr. All rights reserved. 144*0Sstevel@tonic-gateThis program is free software; you can redistribute it and/or modify 145*0Sstevel@tonic-gateit under the same terms as Perl itself. 146*0Sstevel@tonic-gate 147*0Sstevel@tonic-gate=for html <hr> 148*0Sstevel@tonic-gate 149*0Sstevel@tonic-gateI<$Id: //depot/libnet/Net/Time.pm#9 $> 150*0Sstevel@tonic-gate 151*0Sstevel@tonic-gate=cut 152