[Git][NTPsec/ntpsec][master] 3 commits: Improved debugging in pyntpdig.
Eric S. Raymond
gitlab at mg.gitlab.com
Wed Dec 7 22:54:32 UTC 2016
Eric S. Raymond pushed to branch master at NTPsec / ntpsec
Commits:
a1c2d1b8 by Eric S. Raymond at 2016-12-07T15:31:12-05:00
Improved debugging in pyntpdig.
- - - - -
3eabba19 by Eric S. Raymond at 2016-12-07T17:24:16-05:00
Correct fatal errors in exception handling.
- - - - -
a3a9bfd1 by Eric S. Raymond at 2016-12-07T17:53:16-05:00
In pyntpdig, fill in the xmt field as the C version does.
- - - - -
2 changed files:
- ntpdig/pyntpdig
- pylib/packet.py
Changes:
=====================================
ntpdig/pyntpdig
=====================================
--- a/ntpdig/pyntpdig
+++ b/ntpdig/pyntpdig
@@ -58,11 +58,13 @@ def queryhost(server, concurrent, timeout=5, port=123):
return []
sockets = []
packets = []
+ request = ntp.packet.SyncPacket()
+ request.transmit_timestamp = ntp.packet.SyncPacket.posix_to_ntp(time.time())
+ packet = request.flatten()
for (family, socktype, proto, canonname, sockaddr) in iptuples:
if debug:
log("querying %s (%s)" % (sockaddr[0], server))
s = socket.socket(family, socktype)
- packet = b'\xe3' + b'\0' * 47
if keyid and keytype and passwd:
if debug:
log("authenticating with %s key %d" % (keytype, keyid))
@@ -153,17 +155,11 @@ def clock_select(packets):
if response.leap() == "unsync":
drop("leap not in sync")
continue
- if not response.origin_timestamp < response.reference_timestamp:
- drop("server is very broken")
- continue
- if response.origin_timestamp-response.reference_timestamp >= NTP_MAXAGE:
- drop("server has gone too long without sync")
- continue
if not response.trusted:
drop("request was authenticated but server is untrusted")
continue
# Bypass this test if we ever support broadcast-client mode again
- if response.origin_timestamp != 0:
+ if response.origin_timestamp == 0:
drop("unexpected response timestamp")
continue
filtered.append(response)
@@ -354,47 +350,47 @@ if __name__ == '__main__':
arguments = ["localhost"]
if replay:
- (packet, dst) = replay.split(":")
- pkt = ntp.packet.SyncPacket(packet.decode("hex"))
- pkt.received = ntp.packet.SyncPacket.posix_to_ntp(float(dst))
- #print(repr(pkt))
- def hexstamp(n):
- return "%08x.%08x" % (n >> 32, n & 0x00000000ffffffff)
- print("org t1: %s rec t2: %s" % (hexstamp(pkt.t1()), hexstamp(pkt.t2())))
- print("xmt t3: %s dst t4: %s" % (hexstamp(pkt.t3()), hexstamp(pkt.t4())))
- pkt.posixize()
- 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:
- try:
- returned += queryhost(server=server, concurrent=True, timeout=timeout)
- except SyncException:
- log(e.message)
- continue
- if len(returned) >= samples:
- break
- for server in arguments:
- try:
- returned += queryhost(server=server, concurrent=False, timeout=timeout)
- except SyncException:
- log(e.message)
- continue
- if len(returned) >= samples:
- break
+ (pkt, dst) = replay.split(":")
+ packet = ntp.packet.SyncPacket(pkt.decode("hex"))
+ packet.received = ntp.packet.SyncPacket.posix_to_ntp(float(dst))
+ returned = [packet]
+ else:
+ returned = []
+ for server in concurrent_hosts:
+ try:
+ returned += queryhost(server=server, concurrent=True, timeout=timeout)
+ except ntp.packet.SyncException as e:
+ log(e.message)
+ continue
+ if len(returned) >= samples:
+ break
+ for server in arguments:
+ try:
+ returned += queryhost(server=server, concurrent=False, timeout=timeout)
+ except ntp.packet.SyncException as e:
+ log(e.message)
+ continue
+ if len(returned) >= samples:
+ break
+ returned = clock_select(returned)
- returned = clock_select(returned)
if returned:
- syncpacket = returned[0]
- report(syncpacket, json)
+ pkt = returned[0]
+ if debug:
+ #print(repr(pkt))
+ def hexstamp(n):
+ return "%08x.%08x" % (n >> 32, n & 0x00000000ffffffff)
+ print("org t1: %s rec t2: %s" % (hexstamp(pkt.t1()), hexstamp(pkt.t2())))
+ print("xmt t3: %s dst t4: %s" % (hexstamp(pkt.t3()), hexstamp(pkt.t4())))
+ pkt.posixize()
+ 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)
# If we can step but we cannot slew, then step.
# If we can step or slew and and |offset| > steplimit, then step.
rc = True
- offset = syncpacket.adjust()
+ offset = pkt.adjust()
ntp.ntpc.setprogname("ntpdig")
if step and (not slew or (slew and (abs(offset) > steplimit))):
rc = ntp.ntpc.step_systime(offset)
=====================================
pylib/packet.py
=====================================
--- a/pylib/packet.py
+++ b/pylib/packet.py
@@ -329,7 +329,7 @@ class Packet:
self.li_vn_mode = 0 # leap, version, mode (uint8_t)
# Subclasses have variable fields here
self.extension = b'' # extension data
- self.li_vn_mode = Packet.PKT_LI_VN_MODE(0, version, mode)
+ self.li_vn_mode = Packet.PKT_LI_VN_MODE(3, version, mode)
# These decorators will allow us to assign the extension Python 3 strings
@property
@@ -382,7 +382,8 @@ class SyncPacket(Packet):
self.received = SyncPacket.posix_to_ntp(time.time())
self.trusted = True
self.rescaled = False
- self.analyze()
+ if self.data:
+ self.analyze()
def analyze(self):
if len(self.data) < SyncPacket.HEADER_LEN or (len(self.data) & 3) != 0:
@@ -472,7 +473,7 @@ class SyncPacket(Packet):
return ("no-leap", "add-leap", "del-leap", "unsync")[((self.li_vn_mode) >> 6) & 0x3]
def flatten(self):
"Flatten the packet into an octet sequence."
- body = struct.pack(ControlPacket.format,
+ body = struct.pack(SyncPacket.format,
self.li_vn_mode,
self.stratum,
self.poll,
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/9a9a756dff03442bd8e8f1daf68da0f38661bf81...a3a9bfd19d3f217439adbd8075f79bfb456b10a6
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ntpsec.org/pipermail/vc/attachments/20161207/5e80c296/attachment.html>
More information about the vc
mailing list