Net::NTP(3pm)
π offset & delay (read these two first, bestie)
offset($packet, $xmttime, $rectime)
So you hand it an NTP Packet (from B, yeah?) and it gives you back the offset to local (A), using its xmttime (T1) and rectime (T4). The math is literally:
theta = T(B) - T(A) = 1/2 * [(T2-T1) + (T3-T4)]
delay($packet, $xmttime, $rectime)
This one gives you the delay from the sender (B) of $packet, assuming you already know your local xmttime (T1) and rectime (T4). And the formula is giving:
delta = T(ABA) = (T4-T1) - (T3-T2).
β¨ NAME β¨
Net::NTP β a Perl extension for decoding NTP server responses. That's it, that's the girl. ππ
π 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"
π ABSTRACT π
Okay so all this module does β and I mean all β is send a packet to an NTP server and then decode the packet it gets back into its respective parts. As outlined in RFC5905 (which superseded RFC1305 and RFC2030 btw, no cap). Simple. Iconic. π
π§ LIMITATIONS π§
This only supports Association Mode 3 (Client). That's the whole vibe, don't come for her. π
π DESCRIPTION π
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 where further processing of the information you got from the server can be manipulated. For example: timestamps are in epoch, so you could literally use the localtime function to produce an even more "human" representation of the timestamp. Slay. πΈ
π«Ά EXPORT π«Ά
get_ntp_response(<server>, <port>);
This module exports a single method β get_ntp_response. It takes the server as the first argument (and bestie, localhost is the default) and the port to send/recieve the packets on (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. Extra, but make it useful. π
π¨ If there's a timeout or other communications error,get_ntp_responsewill die β so callget_ntp_responsein anevalblock. Not me getting caught lacking without an eval block π
π SEE ALSO π
perl, IO::Socket, RFC5905, RFC1305, 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. π«Ά
Β©οΈ COPYRIGHT AND LICENSE Β©οΈ
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.
π¦ NAME π¦
Net::NTP::Packet
π¦ DESCRIPTION π¦
Representation of a NTP Packet with serialization primitives. She's structured, she's organized, she ate. π
π₯ PROTOCOL - RFC 5905 - Section 7. π₯
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. β¨
The parameters are the field names, gotten from "7.3. Packet Header Variables". iykyk π
new_client_packet($xmttime)
Make a packet in association mode 'Client' to be sent to a server. Go little packet gooo ποΈ
encode()
Encode a packet to its wire format. NOTE: It only encodes server packets at the moment. She's doing her best, okay? π
$packet = Net::NTP::Packet->decode($data, $xmttime, $rectime)
decode the NTP packet from its wire format. Reverse uno card π
and that's the whole page bestie, you're so welcome π
This one's from perl v5.36.0, dated 2022-10-13 β yeah she's been around, and she's still serving. Stan a legacy queen. ππ«Ά
π¦ where this page comes from
- ubuntu noble: libnet-ntp-perl 1.5-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu questing: libnet-ntp-perl 1.5-3
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms