[Git][NTPsec/ntpsec][master] Improved ntpdig instrumentation.

Eric S. Raymond gitlab at mg.gitlab.com
Tue Nov 29 12:01:59 UTC 2016


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


Commits:
68bc58be by Eric S. Raymond at 2016-11-29T07:01:09-05:00
Improved ntpdig instrumentation.

This is a tep twards landing the Python port, but something is still wrong with
the handling of dst in the Python version.

- - - - -


4 changed files:

- ntpdig/main.c
- ntpdig/pyntpdig
- ntpdig/utilities.c
- pylib/packet.py


Changes:

=====================================
ntpdig/main.c
=====================================
--- a/ntpdig/main.c
+++ b/ntpdig/main.c
@@ -1517,17 +1517,17 @@ offset_calculation(
 
 		printf("ntpdig offset_calculation: rpkt->reftime:\n");
 		l_fp_output(&p_ref, stdout);
-		printf("ntpdig offset_calculation: rpkt->org:\n");
+		printf("ntpdig offset_calculation: rpkt->org: ");
 		l_fp_output(&p_org, stdout);
-		printf("ntpdig offset_calculation: rpkt->rec:\n");
+		printf("ntpdig offset_calculation: rpkt->rec: ");
 		l_fp_output(&p_rec, stdout);
-		printf("ntpdig offset_calculation: rpkt->xmt:\n");
+		printf("ntpdig offset_calculation: rpkt->xmt: ");
 		l_fp_output(&p_xmt, stdout);
 	}
 #endif
 
-	TRACE(3, ("ntpdig offset_calculation:\trec - org t21: %.6f\n"
-		  "\txmt - dst t34: %.6f\tdelta: %.6f\toffset: %.6f\n",
+	TRACE(3, ("ntpdig offset_calculation:\nrec - org t21: %.6f\t"
+		  "\txmt - dst t34: %.6f\ndelta: %.6f\toffset: %.6f\n",
 		  t21, t34, delta, *offset));
 
 	return;


=====================================
ntpdig/pyntpdig
=====================================
--- a/ntpdig/pyntpdig
+++ b/ntpdig/pyntpdig
@@ -252,12 +252,13 @@ USAGE:  sntp [ -<flag> [<val>] | --<name>[{=| }<val>] ]...
 if __name__ == '__main__':
     try:
         (options, arguments) = getopt.getopt(sys.argv[1:],
-                                             "46a:c:dD:g:hjk:l:M:o:p:Sst:wWV",
+                                             "46a:c:dD:g:hjk:l:M:o:p:r:Sst:wWV",
                                              ["ipv4","ipv6",
                                               "authentication=",
                                               "concurrent=",
                                               "gap=", "help", "json",
                                               "keyfile=", "logfile=",
+                                              "replay=",
                                               "samples=", "steplimit=",
                                               "step", "slew",
                                               "timeout=",
@@ -283,6 +284,7 @@ if __name__ == '__main__':
     step = False
     slew = False
     timeout = 5
+    replay = None
     try:
         for (switch, val) in options:
             if switch in ("-4", "--ipv4"):
@@ -311,6 +313,8 @@ if __name__ == '__main__':
                 steplimit = int(val)
             elif switch in ("-p", "--samples"):
                 samples = int(val)
+            elif switch in ('-r', "--replay"):
+                replay = val
             elif switch in ("-S", "--step"):
                 step = True
             elif switch in ("-s", "--slew"):
@@ -354,6 +358,14 @@ if __name__ == '__main__':
     if not arguments:
         arguments = ["localhost"]
 
+    if replay:
+        pkt = ntp.packet.SyncPacket(replay.decode("hex"))
+        print(repr(pkt))
+        pkt.posixize()
+        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()))
+        raise SystemExit(0)
+
     returned = []
     for server in concurrent_hosts:
         try:


=====================================
ntpdig/utilities.c
=====================================
--- a/ntpdig/utilities.c
+++ b/ntpdig/utilities.c
@@ -17,6 +17,7 @@ pkt_output (
 
 	pkt = (uint8_t *)dpkg;
 
+#ifdef __UNUSED__
 	fprintf(output, HLINE);
 
 	for (a = 0; a < pkt_length; a++) {
@@ -28,6 +29,13 @@ pkt_output (
 
 	fprintf(output, "\n");
 	fprintf(output, HLINE);
+#endif /* __UNUSED__ */
+
+	for (a = 0; a < pkt_length; a++) {
+	    fprintf(output, "%02x", pkt[a]);
+	}
+	fprintf(output, "\n");
+
 }
 
 /* Output a long floating point value in hex in the style described above 


=====================================
pylib/packet.py
=====================================
--- a/pylib/packet.py
+++ b/pylib/packet.py
@@ -380,7 +380,7 @@ class SyncPacket(Packet):
         self.mac = ''
         self.hostname = None
         self.resolved = None
-        self.received = time.time()
+        self.received = SyncPacket.posix_to_ntp(time.time())
         self.trusted = True
         self.analyze()
         #self.posixize()
@@ -428,6 +428,11 @@ class SyncPacket(Packet):
         "Scale from NTP time to POSIX time"
         # Note: assumes we're in the same NTP era as the transmitter...
         return (t * 2**-32) - SyncPacket.UNIX_EPOCH 
+    @staticmethod
+    def posix_to_ntp(t):
+        "Scale from POSIX time to NTP time"
+        # Note: assumes we're in the same NTP era as the transmitter...
+        return (t + SyncPacket.UNIX_EPOCH) * 2**32
 
     def posixize(self):
         self.root_delay *= 2**-16
@@ -436,29 +441,30 @@ class SyncPacket(Packet):
         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.transmit_timestamp = SyncPacket.ntp_to_posix(self.received)
 
-    def __t1(self):
+    def t1(self):
         return self.origin_timestamp
-    def __t2(self):
+    def t2(self):
         return self.receive_timestamp
-    def __t3(self):
+    def t3(self):
         return self.transmit_timestamp
-    def __t4(self):
+    def t4(self):
         return self.received
     def delta(self):
         "Packet flight time"
-        return (self.__t4() - self.__t1()) - (self.__t3() - self.__t2())
+        return (self.t4() - self.t1()) - (self.t3() - self.t2())
     def epsilon(self):
         "Residual error due to clock imprecision."
         # FIXME: Include client imprecision.
-        return SyncPacket.PHI * (self.__t4() - self.__t1()) + 2**self.precision
+        return SyncPacket.PHI * (self.t4() - self.t1()) + 2**self.precision
     def synchd(self):
         "Synchronization distance, estimates worst-case error in seconds"
         # This is "lambda" in NTP-speak, but that's a Python keyword 
         return abs(self.delta()/2 + self.epsilon())
     def adjust(self):
         "Adjustment implied by this packet - 'theta' in NTP-speak."
-        return ((self.__t2()-self.__t1())+(self.__t3()-self.__t4()))/2
+        return ((self.t2()-self.t1())+(self.t3()-self.t4()))/2
 
     def leap(self):
         return ("no-leap", "add-leap", "del-leap", "unsync")[((self.li_vn_mode) >> 6) & 0x3]



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/68bc58be0060b5c055085187b2df3a70f22d6210
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ntpsec.org/pipermail/vc/attachments/20161129/51d95500/attachment.html>


More information about the vc mailing list