Package dpkt :: Module ntp
[hide private]
[frames] | no frames]

Source Code for Module dpkt.ntp

  1  # $Id: ntp.py 48 2008-05-27 17:31:15Z yardley $ 
  2  # -*- coding: utf-8 -*- 
  3  """Network Time Protocol.""" 
  4  from __future__ import print_function 
  5   
  6  from . import dpkt 
  7  from .decorators import deprecated 
  8   
  9  # NTP v4 
 10   
 11  # Leap Indicator (LI) Codes 
 12  NO_WARNING = 0 
 13  LAST_MINUTE_61_SECONDS = 1 
 14  LAST_MINUTE_59_SECONDS = 2 
 15  ALARM_CONDITION = 3 
 16   
 17  # Mode Codes 
 18  RESERVED = 0 
 19  SYMMETRIC_ACTIVE = 1 
 20  SYMMETRIC_PASSIVE = 2 
 21  CLIENT = 3 
 22  SERVER = 4 
 23  BROADCAST = 5 
 24  CONTROL_MESSAGE = 6 
 25  PRIVATE = 7 
26 27 28 -class NTP(dpkt.Packet):
29 """Network Time Protocol. 30 31 TODO: Longer class information.... 32 33 Attributes: 34 __hdr__: Header fields of NTP. 35 TODO. 36 """ 37 38 __hdr__ = ( 39 ('flags', 'B', 0), 40 ('stratum', 'B', 0), 41 ('interval', 'B', 0), 42 ('precision', 'B', 0), 43 ('delay', 'I', 0), 44 ('dispersion', 'I', 0), 45 ('id', '4s', 0), 46 ('update_time', '8s', 0), 47 ('originate_time', '8s', 0), 48 ('receive_time', '8s', 0), 49 ('transmit_time', '8s', 0) 50 ) 51 52 @property
53 - def v(self):
54 return (self.flags >> 3) & 0x7
55 56 @v.setter
57 - def v(self, v):
58 self.flags = (self.flags & ~0x38) | ((v & 0x7) << 3)
59 60 @property
61 - def li(self):
62 return (self.flags >> 6) & 0x3
63 64 @li.setter
65 - def li(self, li):
66 self.flags = (self.flags & ~0xc0) | ((li & 0x3) << 6)
67 68 @property
69 - def mode(self):
70 return self.flags & 0x7
71 72 @mode.setter
73 - def mode(self, mode):
74 self.flags = (self.flags & ~0x7) | (mode & 0x7)
75 76 77 __s = b'\x24\x02\x04\xef\x00\x00\x00\x84\x00\x00\x33\x27\xc1\x02\x04\x02\xc8\x90\xec\x11\x22\xae\x07\xe5\xc8\x90\xf9\xd9\xc0\x7e\x8c\xcd\xc8\x90\xf9\xd9\xda\xc5\xb0\x78\xc8\x90\xf9\xd9\xda\xc6\x8a\x93'
78 79 80 -def test_ntp_pack():
81 n = NTP(__s) 82 assert (__s == bytes(n))
83
84 85 -def test_ntp_unpack():
86 n = NTP(__s) 87 assert (n.li == NO_WARNING) 88 assert (n.v == 4) 89 assert (n.mode == SERVER) 90 assert (n.stratum == 2) 91 assert (n.id == b'\xc1\x02\x04\x02') 92 # test get/set functions 93 n.li = ALARM_CONDITION 94 n.v = 3 95 n.mode = CLIENT 96 assert (n.li == ALARM_CONDITION) 97 assert (n.v == 3) 98 assert (n.mode == CLIENT)
99 100 if __name__ == '__main__': 101 test_ntp_pack() 102 test_ntp_unpack() 103 print('Tests Successful...') 104