Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
3.4 KiB

  1. # Net::Time.pm
  2. #
  3. # Copyright (c) 1995-1998 Graham Barr <[email protected]>. All rights reserved.
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the same terms as Perl itself.
  6. package Net::Time;
  7. use strict;
  8. use vars qw($VERSION @ISA @EXPORT_OK $TIMEOUT);
  9. use Carp;
  10. use IO::Socket;
  11. require Exporter;
  12. use Net::Config;
  13. use IO::Select;
  14. @ISA = qw(Exporter);
  15. @EXPORT_OK = qw(inet_time inet_daytime);
  16. $VERSION = "2.08";
  17. $TIMEOUT = 120;
  18. sub _socket
  19. {
  20. my($pname,$pnum,$host,$proto,$timeout) = @_;
  21. $proto ||= 'udp';
  22. my $port = (getservbyname($pname, $proto))[2] || $pnum;
  23. my $hosts = defined $host ? [ $host ] : $NetConfig{$pname . '_hosts'};
  24. my $me;
  25. foreach $host (@$hosts)
  26. {
  27. $me = IO::Socket::INET->new(PeerAddr => $host,
  28. PeerPort => $port,
  29. Proto => $proto
  30. ) and last;
  31. }
  32. return unless $me;
  33. $me->send("\n")
  34. if $proto eq 'udp';
  35. $timeout = $TIMEOUT
  36. unless defined $timeout;
  37. IO::Select->new($me)->can_read($timeout)
  38. ? $me
  39. : undef;
  40. }
  41. sub inet_time
  42. {
  43. my $s = _socket('time',37,@_) || return undef;
  44. my $buf = '';
  45. my $offset = 0 | 0;
  46. return undef
  47. unless $s->recv($buf, length(pack("N",0)));
  48. # unpack, we | 0 to ensure we have an unsigned
  49. my $time = (unpack("N",$buf))[0] | 0;
  50. # the time protocol return time in seconds since 1900, convert
  51. # it to a the required format
  52. if($^O eq "MacOS") {
  53. # MacOS return seconds since 1904, 1900 was not a leap year.
  54. $offset = (4 * 31536000) | 0;
  55. }
  56. else {
  57. # otherwise return seconds since 1972, there were 17 leap years between
  58. # 1900 and 1972
  59. $offset = (70 * 31536000 + 17 * 86400) | 0;
  60. }
  61. $time - $offset;
  62. }
  63. sub inet_daytime
  64. {
  65. my $s = _socket('daytime',13,@_) || return undef;
  66. my $buf = '';
  67. $s->recv($buf, 1024) ? $buf
  68. : undef;
  69. }
  70. 1;
  71. __END__
  72. =head1 NAME
  73. Net::Time - time and daytime network client interface
  74. =head1 SYNOPSIS
  75. use Net::Time qw(inet_time inet_daytime);
  76. print inet_time(); # use default host from Net::Config
  77. print inet_time('localhost');
  78. print inet_time('localhost', 'tcp');
  79. print inet_daytime(); # use default host from Net::Config
  80. print inet_daytime('localhost');
  81. print inet_daytime('localhost', 'tcp');
  82. =head1 DESCRIPTION
  83. C<Net::Time> provides subroutines that obtain the time on a remote machine.
  84. =over 4
  85. =item inet_time ( [HOST [, PROTOCOL [, TIMEOUT]]])
  86. Obtain the time on C<HOST>, or some default host if C<HOST> is not given
  87. or not defined, using the protocol as defined in RFC868. The optional
  88. argument C<PROTOCOL> should define the protocol to use, either C<tcp> or
  89. C<udp>. The result will be a time value in the same units as returned
  90. by time() or I<undef> upon failure.
  91. =item inet_daytime ( [HOST [, PROTOCOL [, TIMEOUT]]])
  92. Obtain the time on C<HOST>, or some default host if C<HOST> is not given
  93. or not defined, using the protocol as defined in RFC867. The optional
  94. argument C<PROTOCOL> should define the protocol to use, either C<tcp> or
  95. C<udp>. The result will be an ASCII string or I<undef> upon failure.
  96. =back
  97. =head1 AUTHOR
  98. Graham Barr <[email protected]>
  99. =head1 COPYRIGHT
  100. Copyright (c) 1995-1998 Graham Barr. All rights reserved.
  101. This program is free software; you can redistribute it and/or modify
  102. it under the same terms as Perl itself.
  103. =cut