xref: /openbsd-src/gnu/usr.bin/perl/cpan/libnet/lib/Net/Time.pm (revision 99fd087599a8791921855f21bd7e36130f39aadc)
1# Net::Time.pm
2#
3# Copyright (C) 1995-2004 Graham Barr.  All rights reserved.
4# Copyright (C) 2014 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
9package Net::Time;
10
11use 5.008001;
12
13use strict;
14use warnings;
15
16use Carp;
17use Exporter;
18use IO::Select;
19use IO::Socket;
20use Net::Config;
21
22our @ISA       = qw(Exporter);
23our @EXPORT_OK = qw(inet_time inet_daytime);
24
25our $VERSION = "3.11";
26
27our $TIMEOUT = 120;
28
29sub _socket {
30  my ($pname, $pnum, $host, $proto, $timeout) = @_;
31
32  $proto ||= 'udp';
33
34  my $port = (getservbyname($pname, $proto))[2] || $pnum;
35
36  my $hosts = defined $host ? [$host] : $NetConfig{$pname . '_hosts'};
37
38  my $me;
39
40  foreach my $addr (@$hosts) {
41    $me = IO::Socket::INET->new(
42      PeerAddr => $addr,
43      PeerPort => $port,
44      Proto    => $proto
45      )
46      and last;
47  }
48
49  return unless $me;
50
51  $me->send("\n")
52    if $proto eq 'udp';
53
54  $timeout = $TIMEOUT
55    unless defined $timeout;
56
57  IO::Select->new($me)->can_read($timeout)
58    ? $me
59    : undef;
60}
61
62
63sub inet_time {
64  my $s      = _socket('time', 37, @_) || return;
65  my $buf    = '';
66  my $offset = 0 | 0;
67
68  return
69    unless defined $s->recv($buf, length(pack("N", 0)));
70
71  # unpack, we | 0 to ensure we have an unsigned
72  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  if ($^O eq "MacOS") {
78
79    # MacOS return seconds since 1904, 1900 was not a leap year.
80    $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    $offset = (70 * 31536000 + 17 * 86400) | 0;
87  }
88
89  $time - $offset;
90}
91
92
93sub inet_daytime {
94  my $s   = _socket('daytime', 13, @_) || return;
95  my $buf = '';
96
97  defined($s->recv($buf, 1024))
98    ? $buf
99    : undef;
100}
101
1021;
103
104__END__
105
106=head1 NAME
107
108Net::Time - time and daytime network client interface
109
110=head1 SYNOPSIS
111
112    use Net::Time qw(inet_time inet_daytime);
113
114    print inet_time();          # use default host from Net::Config
115    print inet_time('localhost');
116    print inet_time('localhost', 'tcp');
117
118    print inet_daytime();       # use default host from Net::Config
119    print inet_daytime('localhost');
120    print inet_daytime('localhost', 'tcp');
121
122=head1 DESCRIPTION
123
124C<Net::Time> provides subroutines that obtain the time on a remote machine.
125
126=over 4
127
128=item inet_time ( [HOST [, PROTOCOL [, TIMEOUT]]])
129
130Obtain the time on C<HOST>, or some default host if C<HOST> is not given
131or not defined, using the protocol as defined in RFC868. The optional
132argument C<PROTOCOL> should define the protocol to use, either C<tcp> or
133C<udp>. The result will be a time value in the same units as returned
134by time() or I<undef> upon failure.
135
136=item inet_daytime ( [HOST [, PROTOCOL [, TIMEOUT]]])
137
138Obtain the time on C<HOST>, or some default host if C<HOST> is not given
139or not defined, using the protocol as defined in RFC867. The optional
140argument C<PROTOCOL> should define the protocol to use, either C<tcp> or
141C<udp>. The result will be an ASCII string or I<undef> upon failure.
142
143=back
144
145=head1 AUTHOR
146
147Graham Barr E<lt>F<gbarr@pobox.com>E<gt>.
148
149Steve Hay E<lt>F<shay@cpan.org>E<gt> is now maintaining libnet as of version
1501.22_02.
151
152=head1 COPYRIGHT
153
154Copyright (C) 1995-2004 Graham Barr.  All rights reserved.
155
156Copyright (C) 2014 Steve Hay.  All rights reserved.
157
158=head1 LICENCE
159
160This module is free software; you can redistribute it and/or modify it under the
161same terms as Perl itself, i.e. under the terms of either the GNU General Public
162License or the Artistic License, as specified in the F<LICENCE> file.
163
164=cut
165