logo
Free, unlimited AI code reviews that run on commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

Net::NTP - Perl extension for decoding NTP server responses

Abstract

       All this module does is send a packet to an NTP server and then decode the packet received into it's
       respective parts - as outlined in RFC5905 (superseding RFC1305 and RFC2030).

Author

       Now maintained by Ask Bjørn Hansen, <ask@develooper.com<gt>

       Originally by James G. Willmore, <jwillmore (at) adelphia.net<gt> or <owner (at) ljcomputing.net<gt>

       Special thanks to Ralf D. Kloth <ralf (at) qrq.de<gt> for the code to decode NTP packets.

Description

       Representation of a NTP Packet with serialization primitives.

   PROTOCOL-RFC5905-Section7.
              0                   1                   2                   3
              0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
             +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
             |LI | VN  |Mode |    Stratum     |     Poll      |  Precision   |
             +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
             |                         Root Delay                            |
             +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
             |                         Root Dispersion                       |
             +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
             |                          Reference ID                         |
             +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
             |                                                               |
             +                     Reference Timestamp (64)                  +
             |                                                               |
             +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
             |                                                               |
             +                      Origin Timestamp (64)                    +
             |                                                               |
             +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
             |                                                               |
             +                      Receive Timestamp (64)                   +
             |                                                               |
             +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
             |                                                               |
             +                      Transmit Timestamp (64)                  +
             |                                                               |
             +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
             |                                                               |
             .                                                               .
             .                    Extension Field 1 (variable)               .
             .                                                               .
             |                                                               |
             +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
             |                                                               |
             .                                                               .
             .                    Extension Field 2 (variable)               .
             .                                                               .
             |                                                               |
             +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
             |                          Key Identifier                       |
             +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
             |                                                               |
             |                            dgst (128)                         |
             |                                                               |
             +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

   new
       create a new Net::NTP::Packet instance.

       Parameters are the field names, gotten from "7.3.  Packet Header Variables"

   new_client_packet($xmttime)
       Make a packet in association mode 'Client' to be sent to a server.

   encode()
       Encode a packet to its wire format.  NOTE: It only encodes server packets at the moment.

   $packet=Net::NTP::Packet->decode($data,$xmttime,$rectime)
       decode the NTP packet from its wire format.

perl v5.36.0                                       2022-10-13                                      Net::NTP(3pm)

Limitations

       This only supports Association Mode 3 (Client).

Name

       Net::NTP::Packet

See Also

       perl, IO::Socket, RFC5905, RFC1305, RFC2030

Synopsis

         use Net::NTP qw(get_ntp_response);
         use Time::HiRes qw(time);
         my %response = get_ntp_response();

         my $xmttime = time();
         my $spkt = Net::NTP::Packet->new_client_packet($xmttime);
         $socket->send($pkt->encode());
         $socket->recv(my $data, 1024);
         my $rectime = time();
         my $cpkt = Net::NTP::Packet->decode($data, $xmttime, $rectime);
         print "Stratum: ", $cpkt->{stratum}, "\n";
         print "Offset: ", Net::NTP->offset($pkt, $xmttime, $rectime), "\n"

See Also