[Git][NTPsec/ntpsec][master] pyntpdig is probably now correct up to precision clipping.

Eric S. Raymond gitlab at mg.gitlab.com
Wed Dec 7 13:17:05 UTC 2016


Eric S. Raymond pushed to branch master at NTPsec / ntpsec


Commits:
1edfa59c by Eric S. Raymond at 2016-12-07T08:16:07-05:00
pyntpdig is probably now correct up to precision clipping.

- - - - -


2 changed files:

- ntpdig/pyntpdig
- pylib/packet.py


Changes:

=====================================
ntpdig/pyntpdig
=====================================
--- a/ntpdig/pyntpdig
+++ b/ntpdig/pyntpdig
@@ -177,28 +177,30 @@ def clock_select(packets):
     # Return the best
     return filtered[:1]
 
-def report(packet, json, adjusted):
+def report(packet, json):
     "Report on the SNTP packet selected for display, and its adjustment."
     say = sys.stdout.write
 
+    packet.posixize()
+
     # Cheesy way to get local timezone offset
     gmt_time = int(time.time())
     local_time = int(time.mktime(time.gmtime(gmt_time)))
     tmoffset = (local_time - gmt_time) // 60	# In minutes
 
     # The server's idea of the time
-    t = time.localtime(int(ntp.packet.SyncPacket.ntp_to_posix(packet.transmit_timestamp)))
+    t = time.localtime(int(packet.transmit_timestamp))
     ms = int(packet.transmit_timestamp * 1000000) % 1000000
 
-    digits = 0
-    precision = packet.precision
-    while True:
-        precision *= 10.0
-        if precision >= 1:
-            break
-        digits += 1
-    if digits > 6:
-        digits = 6;
+    digits = 6
+    #precision = packet.precision
+    #while True:
+    #    precision *= 10.0
+    #    if precision >= 1:
+    #        break
+    #    digits += 1
+    #if digits > 6:
+    #    digits = 6;
 
     date = time.strftime("%Y-%m-%d", t)
     tod = time.strftime("%T", t) + (".%*d" % (digits, ms))
@@ -371,6 +373,8 @@ if __name__ == '__main__':
         print("org t1: %f rec t2: %f" % (pkt.t1(), pkt.t2()))
         print("xmt t3: %f dst t4: %f" % (pkt.t3(), pkt.t4()))
         print("rec-org t21: %f  xmt-dst t34: %f" % (pkt.t2() - pkt.t1(), pkt.t3() - pkt.t4()))
+        report(pkt, json)
+        raise SystemExit(0)
 
     returned = []
     for server in concurrent_hosts:
@@ -393,7 +397,7 @@ if __name__ == '__main__':
     returned = clock_select(returned)
     if returned:
         syncpacket = returned[0]
-        report(syncpacket, json, False)
+        report(syncpacket, json)
 	# If we can step but we cannot slew, then step.
 	# If we can step or slew and and |offset| > steplimit, then step.
         rc = True


=====================================
pylib/packet.py
=====================================
--- a/pylib/packet.py
+++ b/pylib/packet.py
@@ -355,7 +355,7 @@ class SyncException(BaseException):
 
 class SyncPacket(Packet):
     "Mode 1-5 time-synchronization packet, including SNTP."
-    format = "!BBBBIIIQQQQ"
+    format = "!BBBbIIIQQQQ"
     HEADER_LEN = 48
     UNIX_EPOCH = 2208988800	# Midnight 1 Jan 1970 in secs since NTP epoch
     PHI = 15 * 1e-6		# 15ppm
@@ -381,8 +381,8 @@ class SyncPacket(Packet):
         self.resolved = None
         self.received = SyncPacket.posix_to_ntp(time.time())
         self.trusted = True
+        self.rescaled = False
         self.analyze()
-        #self.posixize()
 
     def analyze(self):
         if len(self.data) < SyncPacket.HEADER_LEN or (len(self.data) & 3) != 0:
@@ -434,13 +434,16 @@ class SyncPacket(Packet):
         return int((t + SyncPacket.UNIX_EPOCH) * 2**32)
 
     def posixize(self):
-        self.root_delay *= 2**-16
-        self.root_dispersion *= 2**-16
-        self.reference_timestamp = SyncPacket.ntp_to_posix(self.reference_timestamp)
-        self.origin_timestamp = SyncPacket.ntp_to_posix(self.origin_timestamp)
-        self.receive_timestamp = SyncPacket.ntp_to_posix(self.receive_timestamp)
-        self.transmit_timestamp = SyncPacket.ntp_to_posix(self.transmit_timestamp)
-        self.received = SyncPacket.ntp_to_posix(self.received)
+        "Rescale all timestamps to POSIX time."
+        if not self.rescaled:
+            self.rescaled = True
+            self.root_delay *= 2**-16
+            self.root_dispersion *= 2**-16
+            self.reference_timestamp = SyncPacket.ntp_to_posix(self.reference_timestamp)
+            self.origin_timestamp = SyncPacket.ntp_to_posix(self.origin_timestamp)
+            self.receive_timestamp = SyncPacket.ntp_to_posix(self.receive_timestamp)
+            self.transmit_timestamp = SyncPacket.ntp_to_posix(self.transmit_timestamp)
+            self.received = SyncPacket.ntp_to_posix(self.received)
 
     def t1(self):
         return self.origin_timestamp



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/1edfa59c286ee900bda1adb45918c2326eb50036
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ntpsec.org/pipermail/vc/attachments/20161207/f95020ce/attachment.html>


More information about the vc mailing list