Net::NTP(3pm) | User Contributed Perl Documentation | Net::NTP(3pm) |
Given a NTP Packet (from B), return the offset to local (A) according to its xmttime(T1) and rectime(T4)
theta = T(B) - T(A) = 1/2 * [(T2-T1) + (T3-T4)]
Return the delay from the sender (B) of $packet given known local xmttime(T1) and rectime(T4)
delta = T(ABA) = (T4-T1) - (T3-T2).
Net::NTP - Perl extension for decoding NTP server responses
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"
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).
This only supports Association Mode 3 (Client).
This module exports a single method (get_ntp_response) and returns an associative array based upon RFC1305 and RFC2030. The response from the server is "humanized" to a point that further processing of the information received from the server can be manipulated. For example: timestamps are in epoch, so one could use the localtime function to produce an even more "human" representation of the timestamp.
get_ntp_response(<server>, <port>);
This module exports a single method - get_ntp_response. It takes the server as the first argument (localhost is the default) and port to send/recieve the packets (ntp or 123 by default). It returns an associative array of the various parts of the packet as outlined in RFC1305. It "normalizes" or "humanizes" various parts of the packet. For example: all the timestamps are in epoch, NOT hexidecimal.
Two special fields ("Delay" and "Offset") are calculated and added to the response.
If there's a timeout or other communications error get_ntp_response will die (so call get_ntp_response in an eval block).
perl, IO::Socket, RFC5905, RFC1305, RFC2030
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.
Copyright 2009 by Ask Bjørn Hansen; 2004 by James G. Willmore
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Net::NTP::Packet
Representation of a NTP Packet with serialization primitives.
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) | | | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
create a new Net::NTP::Packet instance.
Parameters are the field names, gotten from "7.3. Packet Header Variables"
Make a packet in association mode 'Client' to be sent to a server.
Encode a packet to its wire format. NOTE: It only encodes server packets at the moment.
decode the NTP packet from its wire format.
2021-01-03 | perl v5.32.0 |