HOME


Mini Shell 1.0
Redirecting to https://devs.lapieza.net/iniciar-sesion Redirecting to https://devs.lapieza.net/iniciar-sesion.
DIR: /proc/self/root/usr/lib/python3/dist-packages/ntp/__pycache__/
Upload File :
Current File : //proc/self/root/usr/lib/python3/dist-packages/ntp/__pycache__/packet.cpython-311.pyc
�

|�dr��X�dZddlmZmZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddlZddlZddlZddlZdZdZdZdZdZd	Zd
ZdZdZGd
�d��ZGd�de��ZGd�de��Z Gd�de��Z!Gd�d��Z"dZ#dZ$dZ%dZ&dZ'dZ(dZ)dZ*dZ+d Z,d!Z-d"Z.d#Z/d$Z0d%Z1d&Z2d'Z3d(Z4d)Z5d*Z6d+Z7d,Z8d-Z9d.Z:d/Z;ej<fd0�Z=Gd1�d2��Z>Gd3�d4��Z?Gd5�d6e��Z@Gd7�d8��ZAd9�ZBd:�ZCd;�ZDd<�ZEd=�ZFGd>�d?��ZGdS)@a(
packet.py - definitions and classes for Python querying of NTP

Freely translated from the old C ntpq code by ESR, with comments
preserved.  The idea was to cleanly separate ntpq-that-was into a
thin front-end layer handling mainly command interpretation and a
back-end that presents the take from ntpd as objects that can be
re-used by other front ends. Other reusable pieces live in util.py.

This code should be Python2-vs-Python-3 agnostic.  Keep it that way!

Here are some pictures to help make sense of this code. First, from RFC 5905,
the general structure of an NTP packet (Figure 8):

       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                       |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                                                               |
      |                           digest (128)                        |
      |                                                               |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

The fixed header is 48 bytes long.  The simplest possible case of an
NTP packet is the minimal SNTP request, a mode 3 packet with the
Stratum and all following fields zeroed out to byte 47.

How to interpret these fields:

The modes are as follows:

+-------+--------------------------+
| Value | Meaning                  |
+-------+--------------------------+
| 0     | reserved                 |
| 1     | symmetric active         |
| 2     | symmetric passive        |
| 3     | client                   |
| 4     | server                   |
| 5     | broadcast                |
| 6     | NTP control message      |
| 7     | reserved for private use |
+-------+--------------------------+

While the Stratum field has 8 bytes, only values 0-16 (low 5 bits)
are legal. Value 16 means 'unsynchronized' Values 17-255 are reserved.

LI (Leap Indicator), Version, Poll, and Precision are not described
here; see RFC 5905.

t_1, the origin timestamp, is the time according to the client at
which the request was sent.

t_2, the receive timestamp, is the time according to the server at
which the request was received.

t_3, the transmit timestamp, is the time according to the server at
which the reply was sent.

You also need t_4, the destination timestamp, which is the time according to
the client at which the reply was received.  This is not in the reply packet,
it's the packet receipt time collected by the client.

The 'Reference timestamp' is an unused historical relic.  It's supposed to be
copied unchanged from upstream in the stratum hierarchy. Normal practice
has been for Stratum 1 servers to fill it in with the raw timestamp from the
most recent reference-clock.

Theta is the thing we want to estimate: the offset between the server
clock and the client clock. The sign convention is that theta is
positive if the server is ahead of the client.

Theta is estimated by [(t_2-t_1)+(t_3-t_4)]/2. The accuracy of this
estimate is predicated upon network latency being symmetrical.

Delta is the network round trip time, i.e. (t_4-t_1)-(t_3-t_2). Here's
how the terms work: (t_4-t_1) is the total time that the request was
in flight, and (t_3-t_2) is the time that the server spent processing it;
when you subtract that out you're left with just network delays.

Lambda nominally represents the maximum amount by which theta could be
off. It's computed as delta/2 + epsilon. The delta/2 term usually
dominates and represents the maximum amount by which network asymmetry
could be throwing off the calculation. Epsilon is the sum of three
other sources of error:

rho_r: the (im)precision field from response packet, representing the
server's inherent error in clock measurement.

rho_s: the client's own (im)precision.

PHI*(t_4-t_1): The amount by which the client's clock may plausibly
have drifted while the packet was in flight. PHI is taken to be a
constant of 15ppm.

rho_r and rho_s are estimated by making back-to-back calls to
clock_gettime() (or similar) and taking their difference. They're
encoded on the wire as an eight-bit two's complement integer
representing, to the nearest integer, log_2 of the value in seconds.

If you look at the raw data, there are 3 unknowns:
   * transit time client to server
   * transit time server to client
   * clock offset
but there are only two equations, so you can't solve it.

NTP gets the 3rd equation by assuming the transit times are equal.  That lets
it solve for the clock offset.

If you assume that both clocks are accurate which is reasonable if you have
GPS at both ends, then you can easily solve for the transit times in each
direction.

The RFC 5905 diagram is slightly out of date in that the digest header assumes
a 128-bit (16-octet) MD5 hash, but it is also possible for the field to be a
128-bit AES_CMAC hash or 160-bit (20-octet) SHA-1 hash.  NTPsec will
support any 128- or 160-bit MAC type in libcrypto.

An extension field consists of a 16-bit network-order type field
length, followed by a 16-bit network-order payload length in octets,
followed by the payload (which must be padded to a 4-octet boundary).

       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
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |         Type field             |      Payload length          |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                                                               |
      |                        Payload (variable)                     |
      |                                                               |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Here's what a Mode 6 packet looks like:

       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  | 6   |R|E|M|  Opcode  |          Sequence            |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |               Status           |       Association ID         |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |               Offset           |            Count             |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                                                               |
      .                                                               .
      .                        Payload (variable)                     .
      .                                                               .
      |                                                               |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                          Key Identifier                       |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                                                               |
      |                           digest (128)                        |
      |                                                               |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

In this case, the fixed header is 24 bytes long.

R = Response bit
E = Error bit
M = More bit.

A Mode 6 packet cannot have extension fields.

�)�print_function�divisionN� i�i�i�������c��eZdZdZed���Zed���Zejj	ejj
dfd�Zed���Z
e
jd���Z
d�Zd	�Zd
�ZdS)�PacketzEncapsulate an NTP fragmentc��|dzdz|dzzS)N���)�v�ms  �,/usr/lib/python3/dist-packages/ntp/packet.py�VN_MODEzPacket.VN_MODEs���a�%�A��!�c�'�*�+�c�J�|dzdzt�||��zS)Nr�)rr)�lrrs   r�PKT_LI_VN_MODEzPacket.PKT_LI_VN_MODEs#���a�%�A������1�!5�!5�5�6rNc��||_d|_d|_t�t
jj||��|_dS)Nrr)�session�
li_vn_mode�	extensionrr�ntp�magic�LEAP_NOTINSYNC)�self�mode�versionrs    r�__init__zPacket.__init__s>����������� �/�/��	�0H�07��?�?����rc��|jS�N)�_Packet__extension�r!s rrzPacket.extensions����rc�N�tj�|��|_dSr&)r�poly�	polybytesr')r!�xs  rrzPacket.extension"s ���8�-�-�a�0�0����rc�V�dtj�|j��S)N)zno-leapzadd-leapzdel-leap�unsync)rr�PKT_LEAPrr(s r�leapzPacket.leap&s&����)�,�,�T�_�=�=�?�	?rc��|jdz	dzS)Nrr�rr(s rr#zPacket.version*s����1�$��+�+rc��|jdzS)Nrr2r(s rr"zPacket.mode-s�����$�$r)�__name__�
__module__�__qualname__�__doc__�staticmethodrrrr�MODE_CLIENT�NTP_VERSIONr$�propertyr�setterr0r#r"rrrrr	s�������!�!��,�,��\�,��7�7��\�7� �I�1���.��?�?�?�?�� � ��X� ���1�1���1�?�?�?�,�,�,�%�%�%�%�%rrc��eZdZdd�Zd�ZdS)�
SyncExceptionrc�"�||_||_dSr&��message�	errorcode�r!rArBs   rr$zSyncException.__init__2������"����rc��|jSr&�rAr(s r�__str__zSyncException.__str__6�
���|�rN�r�r4r5r6r$rGrrrr>r>1s7������#�#�#�#�����rr>c���eZdZdZdZdZdZdZdd�Zd�Z	e
d	���Ze
d
���Zd�Z
d�Zd
�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�ZdS)�
SyncPacketz5Mode 1-5 time-synchronization packet, including SNTP.z!BBBbIIIQQQQ�0l�~TghUMu�>�c��t�|��d|_d|_d|_d|_d|_d|_d|_d|_	d|_
d|_d|_d|_
g|_d|_d|_d|_t$�t)j����|_d|_d|_|r4|�t2j�|����dSdS)NrrNTF)rr$�status�stratum�poll�	precision�
root_delay�root_dispersion�refid�reference_timestamp�origin_timestamp�receive_timestamp�transmit_timestampr�	extfields�mac�hostname�resolvedrL�posix_to_ntp�time�received�trusted�rescaled�analyzerr*r+)r!�datas  rr$zSyncPacket.__init__As������������������	������� �����
�#$�� � !���!"���"#��������������
���
�"�/�/��	���<�<��
������
��	3��L�L���+�+�D�1�1�2�2�2�2�2�	3�	3rc��t|��}|tjks	|dzdkrtd���t	jtj|dtj���\|_|_|_	|_
|_|_|_
|_|_|_|_|tjd�|_|j}t|��dkrgt	jd|dd���\}}|j�||dd|z�f��|d|zd�}t|��dk�gt|��dkr	||_dSt|��dkrtd	���t|��d
vrtd���t|��dvr	||_dSdS)
Nrrzimpossible packet length�z!IIr	rrzUnsupported DES authentication)r	rzPacket is a runt)r
rg)�lenrL�
HEADER_LENr>�struct�unpack�formatrrQrRrSrTrUrVrWrXrYrZrr[�appendr\)r!re�datalen�payload�ftype�flens      rrdzSyncPacket.analyzeYs����d�)�)���Z�*�*�*�w��{�q�.@�.@�� :�;�;�;�%+�M���t�$:�Z�%:�$:�;�%=�%=�
	"���	
��	
��	
��	
��	
�	�	
��	
�	!�	
�	�	
�	�	
�	 ��j�3�4�4�5����.���'�l�l�R���"�M�%���!���=�=�M�U�D��N�!�!�5�'�!�A�d�F�(�*;�"<�=�=�=��a��f�g�g�&�G��'�l�l�R����w�<�<�1����D�H�H�H�
��\�\�R�
�
�� @�A�A�A�
��\�\�W�
$�
$�� 2�3�3�3�
��\�\�X�
%�
%��D�H�H�H�&�
%rc�&�|dztjz
S)z!Scale from NTP time to POSIX time�)rL�
UNIX_EPOCH��ts r�ntp_to_posixzSyncPacket.ntp_to_posix�s���U��z�4�4�4rc�@�t|tjzdz��S)z!Scale from POSIX time to NTP timers)�intrLrtrus rr_zSyncPacket.posix_to_ntp�s��
�A�
�-�-��6�7�7�7rc���|js�d|_|xjdzc_|xjdzc_t�|j��|_t�|j��|_t�|j��|_t�|j��|_t�|j	��|_	dSdS)z%Rescale all timestamps to POSIX time.TrN)
rcrTrUrLrwrWrXrYrZrar(s r�posixizezSyncPacket.posixize�s����}�	C� �D�M��O�O��"�O�O�� � �R�'� � �'1�'>�'>��(�(*�(*�D�$�$.�$;�$;��%�%'�%'�D�!�%/�%<�%<��&�&(�&(�D�"�&0�&=�&=��'�')�')�D�#�&�3�3�D�M�B�B�D�M�M�M�	C�	Crc��|jSr&)rXr(s r�t1z
SyncPacket.t1�s���$�$rc��|jSr&)rYr(s r�t2z
SyncPacket.t2�s���%�%rc��|jSr&)rZr(s r�t3z
SyncPacket.t3�s���&�&rc��|jSr&)rar(s r�t4z
SyncPacket.t4�s
���}�rc��|���|���z
|���|���z
z
S)zPacket flight time)r�r}r�rr(s r�deltazSyncPacket.delta�s7�����	�	�D�G�G�I�I�%�$�'�'�)�)�d�g�g�i�i�*?�@�@rc��tj|���|���z
zd|jzzS)z(Residual error due to clock imprecision.�)rL�PHIr�r}rSr(s r�epsilonzSyncPacket.epsilon�s2���~������T�W�W�Y�Y�!6�7�!�T�^�:K�K�Krc�t�t|���dz|���z��S)z?Synchronization distance, estimates worst-case error in secondsr�)�absr�r�r(s r�synchdzSyncPacket.synchd�s+���4�:�:�<�<��>�D�L�L�N�N�2�3�3�3rc��|���|���z
|���|���z
zdzS)z9Adjustment implied by this packet - 'theta' in NTP-speak.r�)rr}r�r�r(s r�adjustzSyncPacket.adjust�s<�������4�7�7�9�9�$�t�w�w�y�y������':�;�Q�>�>rc���tjtj|j|j|j|j|j|j	|j
|j|j|j
|j��}||jzS�z*Flatten the packet into an octet sequence.)rj�packrLrlrrQrRrSrTrUrVrWrXrYrZr�r!�bodys  r�flattenzSyncPacket.flatten�sc���{�:�,��?��<��9��>��?��/��:��3��0��1��2�4�4���d�n�$�$rc�`�|jdz	dz|jdz	dz|jdz	dz|jdzfS)zAnalyze refid into octets.rg�rr	)rVr(s r�refid_octetszSyncPacket.refid_octets�sB����r�!�T�)���r�!�T�)���q��D�(��
�T�!�#�	#rc��tj�tjd|���z���S)z'Sometimes it's a clock name or KOD type)�BBBB)rr*�polystrrjr�r�r(s r�refid_as_stringzSyncPacket.refid_as_string�s0���x�����i�$�:K�:K�:M�:M�.M� O�P�P�Prc�j�tj�d|���z��S)zSometimes it's an IPV4 address.z%d.%d.%d.%d)rr*r�r�r(s r�refid_as_addresszSyncPacket.refid_as_address�s)���x���
��0A�0A�0C�0C� C�D�D�Drc�2�t|j��dkS)Nr�rhr\r(s r�
is_crypto_nakzSyncPacket.is_crypto_nak�s���4�8�}�}��!�!rc�2�t|j��dkS)Nr
r�r(s r�has_MD5zSyncPacket.has_MD5�����4�8�}�}��"�"rc�2�t|j��dkS)Nrgr�r(s r�has_SHA1zSyncPacket.has_SHA1�r�rc���d|���|���|���fz}|d|j|jfzz
}|���}t
d�|D����s|���}|d|zz
}|dtj	�
t�|j
����zz
}|dtj	�
t�|j����zz
}|dtj	�
t�|j����zz
}|dtj	�
t�|j����zz
}|jr|dt%|j��zz
}|jr"|dt%|j��dd�zz
}|dz
}|S)z@Represent a posixized sync packet in an eyeball-friendly format.z<NTP:%s:%d:%d:z%f:%fc3�2K�|]}|tjvV��dSr&)�string�	printable)�.0�cs  r�	<genexpr>z&SyncPacket.__repr__.<locals>.<genexpr>�s*����5�5�Q�1��(�(�5�5�5�5�5�5r�:�����>)r0r#r"rTrUr��allr�r�util�rfc3339rLrwrWrXrYrZr[�reprr\)r!�r�rss   r�__repr__zSyncPacket.__repr__�s�����	�	���T�\�\�^�^�T�Y�Y�[�[�I�I��	�W����)=�>�
>�>��
�
!�
!�
#�
#���5�5�"�5�5�5�5�5�	)��&�&�(�(�B�	�S�2�X�
��	�S�3�8�#�#��#�#�D�$<�=�=�?�?�?�	?��	�S�3�8�#�#��#�#�D�$9�:�:�<�<�<�	<��	�S�3�8�#�#��#�#�D�$:�;�;�=�=�=�	=��	�S�3�8�#�#��#�#�D�$;�<�<�>�>�>�	>���>�	,�
��t�D�N�+�+�+�+�A��8�	,�
��t�D�H�~�~�a��d�+�+�+�A�	�S����rN)rN)r4r5r6r7rlrirtr�r$rdr8rwr_r{r}rr�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrLrL:s�������;�;�
�F��J��J�
�C�3�3�3�3�0'�'�'�R�5�5��\�5�
�8�8��\�8�C�C�C� %�%�%�&�&�&�'�'�'����A�A�A�L�L�L�
4�4�4�
?�?�?�%�%�%� #�#�#�Q�Q�Q�E�E�E�"�"�"�#�#�#�#�#�#�����rrLc�^�eZdZdZdd�ZdZdZd�Zd�Zd	�Z	d
�Z
d�Zd�Zd
�Z
d�Zd�Zd�ZdS)�
ControlPacketzMode 6 request/response.rrNc���t�|tjj|j|���||_d|_d|_||_	d|_
||_t|��|_
dS)N)r"r#rr�r)rr$rr�MODE_CONTROL�
pktversion�r_e_m_op�sequencerP�associd�offsetrrh�count)r!r�opcoder��qdatas     rr$zControlPacket.__init__sl������3�9�#9� '� 2� '�	�	)�	)�	)���
���
���������������Z�Z��
�
�
rz!BBHHHHHrc��|jdzrdndS)N�TF�r�r(s r�is_responsezControlPacket.is_response����}�t�+�6�t�t��6rc��|jdzrdndS)N�@TFr�r(s r�is_errorzControlPacket.is_errorr�rc��|jdzrdndS)NrTFr�r(s r�morezControlPacket.morer�rc��|jdzS)N�r�r(s rr�zControlPacket.opcodes���}�t�#�#rc��|jdz	dzS)Nr	r�)rPr(s r�errcodezControlPacket.errcodes����q� �D�(�(rc� �|j|jzSr&)r�r�r(s r�endzControlPacket.ends���z�D�K�'�'rc�J�d|j|���|jfzS)z Return statistics on a fragment.z%5d %5d	%3d octets
)r�r�r�r(s r�statszControlPacket.stats s ��&�$�+�t�x�x�z�z�4�:�)N�N�Nrc�^�tj�|��}tjt
j|dt
j���\|_|_	|_
|_|_|_
|_|t
jd�|_|j
|j|j|j
fSr&)rr*r+rjrkr�rlrirr�r�rPr�r�r�r)r!�rawdatas  rrdzControlPacket.analyze$s����(�$�$�W�-�-���}�]�%9�%,�-F�m�.F�-F�%G�I�I�
	���	
��	
��	
��	
��	
��	
�� ��!9�!:�!:�;����
�t�{�D�L�$�+�F�Frc
��tjtj|j|j|j|j|j|j	|j
��}||jzSr�)rjr�r�rlrr�r�rPr�r�r�rr�s  rr�zControlPacket.flatten1sJ���{�=�/��?��=��=��;��<��;��:�'�'���d�n�$�$rc�^�|j�|�����dSr&)r�sendpktr�r(s r�sendzControlPacket.send=s&������T�\�\�^�^�,�,�,�,�,rN)rrrN)r4r5r6r7r$rlrir�r�r�r�r�r�r�rdr�r�rrrr�r��s���������
 �
 �
 �
 ��F��J�7�7�7�7�7�7�7�7�7�$�$�$�)�)�)�(�(�(�O�O�O�G�G�G�
%�
%�
%�-�-�-�-�-rr�c�(�eZdZdZd�Zd�Zd�ZeZdS)�Peerz*The information we have about an NTP peer.c�>�||_||_||_i|_dSr&)rr�rP�	variables)r!rr�rPs    rr$z
Peer.__init__Ds"���������������rc�B�|j���|_dSr&)r�readvarr�r(s r�readvarsz
Peer.readvarsJs����-�-�/�/����rc�$�d|j|jfzS)Nz<Peer: associd=%s status=%0x>)r�rPr(s rrGzPeer.__str__Ms��.�$�,���1L�L�LrN)r4r5r6r7r$r�rGr�rrrr�r�AsI������0�0����0�0�0�M�M�M��H�H�Hrr�z.***Server reports a bad format request packet
z/***Server disallowed request (authentication?)
z****Server reports a bad opcode in request
z(***Association ID {0} unknown to server
z,***A request variable unknown to the server
z/***Server indicates a request variable was bad
z(***Server returned an unspecified error
z.***Socket error; probably ntpd is not running
z***Request timed out
z'***Response from server was incomplete
z****Buffer size exceeded for returned data
z***Select call failed
z***No host open
z3***Response length should have been a multiple of 4z***Invalid key identifierz***Invalid passwordz***Key not foundz#***Unexpected nonce response formatz***Unknown parameter '%s'z***No credentialsz***Server error code %sz?***No response, probably high-traffic server with low MRU limitz***Bad MRU tag %sz#***Sort order %s is not implementedz%***No trusted keys have been declaredc�Z�d}|r�tj�||��\}}t|��}d|z}d�|D��}|t	|��z}||kr|d||z
zz
}d�|D��}|d�|��dzz
}|�|��|��dSdS)	z.Dump a packet in hex, in a familiar hex formatrz%02x c�L�g|]!}tj�|����"Sr)rr*�polyord�r�r,s  r�
<listcomp>z&dump_hex_printable.<locals>.<listcomp>ws(��:�:�:�A�C�H�$�$�Q�'�'�:�:�:rz   c�P�g|]#}d|cxkrdkrnnt|��nd��$S)r��.)�chrr�s  rr�z&dump_hex_printable.<locals>.<listcomp>|s:��I�I�I�1�r�Q�}�}�}�}��}�}�}�}�}�C��F�F�F�3�I�I�IrrN�
N)rr��	slicedatarh�tuple�join�write)�xdata�outfp�rowsize�linedata�linelen�lines      r�dump_hex_printabler�ms����G�
���(�,�,�U�G�<�<���%��h�-�-���� ��:�:��:�:�:����h������W����E�W�w�.�/�/�D�I�I��I�I�I�������!�!�D�(�(��
���D���������rc�*�eZdZdZd�Zd�Zd�Zd�ZdS)�MRUEntryz A traffic entry for an MRU list.c�v�d|_d|_d|_d|_d|_d|_d|_d|_dS)Nr)�addr�last�first�mvr��ct�sc�drr(s rr$zMRUEntry.__init__�s>����	���	���
�����������������rc��tj�|j��}tj�|j��}||z
|jzSr&)r�ntpc�
lfptofloatr�r�r)r!r�r�s   r�avgintzMRUEntry.avgint�sA���x�"�"�4�9�-�-����#�#�D�J�/�/���u����'�'rc�h�|j}|ddkra|d|�d���}|�d��}|dkr
|d|�}tjtj|��S|d|�d���}dtjtj|��zS)Nr�[r��]�%r�s)r��find�socket�	inet_pton�AF_INET6�AF_INET)r!r��pcts   r�sortaddrzMRUEntry.sortaddr�s����y����7�c�>�>���$�)�)�C�.�.�(�)�D��)�)�C�.�.�C��Q�w�w��D�S�D�z���#�F�O�T�:�:�:������3����(�D��f�.�v�~�t�D�D�D�Drc�F�dt|j��dd�zdzS)Nz<MRUEntry: r�r�r�)r��__dict__r(s rr�zMRUEntry.__repr__�s%���t�D�M�2�2�1�R�4�8�8�3�>�>rN)r4r5r6r7r$rrr�rrrr�r��sY������&�&����(�(�(�
E�E�E�$?�?�?�?�?rr�c�$�eZdZdZd�Zd�Zd�ZdS)�MRUListzFA sequence of address-timespan pairs returned by ntpd in one response.c�"�g|_d|_dSr&��entries�nowr(s rr$zMRUList.__init__�s���������rc��|jduS)z2Is the server done shipping entries for this span?N)rr(s r�is_completezMRUList.is_complete�s���x�t�#�#rc�(�d|j�d|j�d�S)Nz<MRUList: entries=z now=r�rr(s rr�zMRUList.__repr__�s���15����t�x�x�x�H�HrN)r4r5r6r7r$rr�rrrrr�sL������L�L����$�$�$�I�I�I�I�Irrc��eZdZdd�Zd�ZdS)�ControlExceptionrc�"�||_||_dSr&r@rCs   rr$zControlException.__init__�rDrc��|jSr&rFr(s rrGzControlException.__str__�rHrNrIrJrrrrr�s7������#�#�#�#�����rrc��eZdZdZdZdZejjdejj	dejj
dejjdejjdejj
d	ejjd
ejjdiZd�Zd
�Zd�Zd�Zd�Zejfd�Zd�Zd�Zd'd�Zd�Zd�Zd(d�Zd)d�Zd'd�Z ddejj!dfd�Z"d�Z#d �Z$d!�Z%d"�Z&d*d#�Z'd$�Z(d%�Z)d&�Z*dS)+�ControlSessionzA session to a host�T�UNSPEC�
PERMISSION�BADFMT�BADOP�BADASSOC�
UNKNOWNVAR�BADVALUE�RESTRICTc��d|_tj|_t|_t|_tj	j
dz|_d|_d|_
d|_d|_d|_d|_d|_d|_d|_d|_d|_d|_t0j|_t6j|_d|_d|_d|_ dS)Nrr�F�MD5rN)!�debugr�	AF_UNSPEC�	ai_family�
DEFTIMEOUT�primary_timeout�DEFSTIMEOUT�secondary_timeoutrr�NTP_OLDVERSIONr��always_auth�keytype�keyid�passwd�authr]�isnum�sock�portr��response�rstatusr"�
MRU_ROW_LIMIT�ntpd_row_limit�sys�stdout�logfp�
nonce_xmit�slots�flakeyr(s rr$zControlSession.__init__�s�����
��)���)���!,����)�2�Q�6��� ��������
������	���
���
���	���	���
���
����,�:����Z��
������
�����rc�^�tj�|j||j|��dSr&�rr��dologrDr.)r!�text�	thresholds   r�warndbgzControlSession.warndbg�s&������t�z�4���Y�?�?�?�?�?rc�X�|jr"|j���d|_dSdSr&)r<�closer(s rrOzControlSession.close�s3���9�	��I�O�O�����D�I�I�I�	�	rc��|jduS)z#Is the session connected to a host?N)r<r(s r�havehostzControlSession.havehost�s���y��$�$rc�������d��r
�dd����fd�}	|dtj���S#tj$rA}tj��jd��d|j���j	d	��Yd
}~nd
}~wwxYw	|dd���S#tj$�r3}�j�&�j�
d��d|j�d
���tj}	|tjz}n#t$rYnwxYw	ttd��rtjtjf}n
tjf}|j|vrX	|dd���cYd
}~S#tj$r3}�j�"�j�
d|jz��Yd
}~nd
}~wwxYwn1#t$r$�j��j�
d��YnwxYwYd
}~nd
}~wwxYwd
S)z5Try different ways to interpret an address and familyrr�r�c�h��tj�|�jtjtj|��Sr&)r�getaddrinfor0�
SOCK_DGRAM�IPPROTO_UDP)r=�hints�hnamer!s  ��r�
hinted_lookupz2ControlSession.__lookuphost.<locals>.hinted_lookups/����%�e�T�4�>�&,�&7�&,�&8�&+�-�-�
-rr)r=rWzntpq: numeric-mode lookup of z	 failed, rNrzntpq: standard-mode lookup of r��
EAI_NODATAzntpq: ndp lookup failed, %s
z+ntpq: API error, missing socket attributes
)�
startswithr�AI_NUMERICHOST�gaierrorrr�rJrD�strerrorr.r��AI_CANONNAME�
AI_ADDRCONFIG�AttributeError�hasattr�
EAI_NONAMErZ�errno)	r!rX�famrY�e�e1�fallback_hints�errlist�e2s	``       r�__lookuphostzControlSession.__lookuphost�s��������C� � �	 ��!�B�$�K�E�	-�	-�	-�	-�	-�	-�
	A� �=�e�6�3H�I�I�I�I����	A�	A�	A��H�N�N�4�:�:�#�e�e�Q�Z�Z�1�26�*�a�
A�
A�
A�
A�
A�
A�
A�
A�����	A���� 	H� �=�e�1�5�5�5�5����	H�	H�	H��z�%��
� � � �$)�E�E�2�;�;�;�"8�9�9�9�$�0�N�
��&�"6�6����!�
�
�
���
����
H��6�<�0�0�3�%�0�&�2C�D�G�G�%�0�2�G��8�w�&�&�<�,�}�%�q�A�A�A�A�A�A�A�A�A��!�?�<�<�<��:�1� �J�,�,�-L�/1�{�.;�<�<�<����������<������"�
H�
H�
H��:�)��J�$�$�F�H�H�H���
H�������������7	H����>�ts��A�B�7B�B�B!�!G$�19G�+C;�:G�;
D�G�D�G�AF)�E#�G$�#F%�2)F �F)� F%�%F)�(G�)+G�G�G�G�G$c��|�||��}|�dS|d\}}}}}|�(tj|d|��|_d|_n|p||_d|_t
j�|jd|jz|j	d��|d|_
	tj|||��|_n6#tj$r$}	td||	j|	jfz���d}	~	wwxYw	|j�|��n6#tj$r$}	td	||	j|	jfz���d}	~	wwxYwdS)
z"openhost - open a socket to a hostNFrTzOpening host %srr�zError opening %s: %s [%d]zError connecting to %s: %s [%d])�_ControlSession__lookuphostr�	inet_ntopr]r;rr�rJrDr.r=r<�errorrr^rd�connect)
r!rXre�res�family�socktype�protocol�	canonname�sockaddrrfs
          r�openhostzControlSession.openhost.s�������s�+�+���;��5�<?��F�9���8�Y����"�,�X�a�[�&�A�A�D�M��D�J�J�%�.��D�M��D�J�����t�z�#4�t�}�#D��z�1�	&�	&�	&��Q�K��	�	C��
�f�h��A�A�D�I�I���|�	C�	C�	C�"�#>�&+�Q�Z���%A�$B�C�C�
C�����	C����	C��I���h�'�'�'�'���|�	C�	C�	C�"�#D�&+�Q�Z���%A�$B�C�C�
C�����	C�����ts0�%C�C4�C/�/C4�8D�E�"E�Ec���|j��0|j�,	t��|_n#ttf$rYnwxYw|jr\|jdkrQ	|j���\|_|_|_dS#t$rtt���wxYw	tj
d��r-ttj�d����}nd}|dks|t$krtt&���n)#t(tf$rtt&���wxYw||_|j��	|j|j\|_}n{#t*t,f$rgt/jd|jz��}|�tt0���t3|��dkrtj�|��}YnwxYw||_dSdS)z2Get a keyid and the password if we don't have one.N�	localhostrzKeyid: z
%s Password: r
)r8r:�
Authenticator�OSError�IOErrorr]�controlr7r9�
ValueErrorr�SERR_NOTRUST�os�isattyryrr*�	polyinput�	MAX_KEYID�SERR_BADKEY�SyntaxError�
IndexError�	TypeError�getpass�SERR_INVPASSrhr��
hexstr2octets)r!�key_idr9s   r�passwordzControlSession.passwordJs����:���y� �� -���D�I�I����)�����D������y�
9�T�]�k�9�9�9��)�+�+�-�-�<�T�Z���t�{��F��!�9�9�9�*�<�8�8�8�9����
4��9�Q�<�<�� ���!3�!3�I�!>�!>�?�?�F�F��F��Q�;�;�&�9�"4�"4�*�;�7�7�7�#5����,�
4�
4�
4�&�{�3�3�3�
4�����D�J��;��

<�'+�y���'<�$���f�f���	�*�
<�
<�
<� ���4�<�)G�H�H���>�*�<�8�8�8��v�;�;��#�#� �X�3�3�F�;�;�F���
<����!�D�K�K�K��s8�%�9�9�,A=�=B� A(D	�	&D/�E�A5G�Gc�P�t|��dzr|dz
}t|��dz�tj�|jdt|��|jfz|jd��	|j�tj	�
|����n?#tj$r-|j�"|j�
d|jz��YdSwxYw|jdkr6|j�/|j�
d	��t||j��d
S)zSend a packet to the host.r�zSending %d octets.  seq=%drNzWrite to %s failed
r��zRequest packet:
r)rhrr�rJrDr�r.r<�sendallr*r+rror�r]r�)r!r�s  rr�zControlSession.sendpktss*���%�j�j�1�n�	��W��E��%�j�j�1�n�	�����t�z�3��e�*�*�d�m�4�5�6:�j�!�	E�	E�	E�	��I���c�h�0�0��7�7�8�8�8�8���|�	�	�	��z�%��
� � �!7�$�-�!G�H�H�H��2�2�		����

�J�!�O�O�$�*�"8��J���0�1�1�1��u�d�j�1�1�1��qs�.7B&�&8C"�!C"Fc�>�|jdkrL|j�E|jdkr|j�d��|j�d|||fz��t|��tjjkr3|j�*|j�dt|��z��dSt||||��}|xjdz
c_|xjdzc_|j|_|j	r�tj
�|j	��|_	tjt|j	��zdzr4|xj	d	z
c_	tjt|j	��zdz�4|s|j
s|���S|j�|j�t#t$���tjt|j	��zd
zr4|xj	d	z
c_	tjt|j	��zd
z�4t&�|���|j|j|j��}|�t#t.���|xj	tj
�|��z
c_	|���S)z(Ship an ntpq request packet to a server.r�Nrr�z-sendrequest: opcode=%d, associd=%d, qdata=%s
z'***Internal error! Data too large (%d)
r�ir�r)r.rDr�rhrr}�CTL_MAX_DATA_LENr�r�rr*r+rir6r�r8r9r�SERR_NOCREDrz�compute_macr�r7�
SERR_NOKEY)r!r�r�r�r:�pktr\s       r�sendrequestzControlSession.sendrequest�sf���J�!�O�O�$�*�"8��z�Q����
� � ��&�&�&��J���M� &���7�8�
9�
9�
9��u�:�:���4�4�4��z�%��
� � �!K�!$�U���",�-�-�-��2��D�&�'�5�9�9���
�
���
�
��
�
�� �
�
��}����=�	)��H�.�.�s�}�=�=�C�M�!�,�s�3�=�/A�/A�A�Q�F�
)��
�
��(�
�
�"�,�s�3�=�/A�/A�A�Q�F�
)�
�	�D�,�	��8�8�:�:���:����!4�"�;�/�/�/�
�(�3�s�}�+=�+=�=��B�	%��M�M�W�$�M�M��(�3�s�}�+=�+=�=��B�	%��'�'����
�
�(,�
�D�L�$�+�O�O���;�"�:�.�.�.��M�M�S�X�/�/��4�4�4�M�M��x�x�z�z�rc	����g}d�_d}d}�j�
�jj}nd�}�fd�}|dd��	|dz
}|d
tzkrt	t
���|s�jdz}	n
�jdz}	|dtj	��|	fzd
��	tj
�jggg|	��\}
}}n'#tj$rt	t���wxYw|dtj	��|	fzd
��|
s�|s|rt	t���|r��jdkr��j���j�d��t!|��D]9\}}
�j�d|dz|
���fz���:�j�dd|z��t	t$���|dtj	��zd��	t&j��j�d����}n'#t.j$rt	t0���wxYw�jr*�jt5j��kr|dd��d}|dt7|��zd��t9����	��|��n'#t<j$rt	t>���wxYw�� �|||��}|s��ˉj!r��j"r��j#tHz}||tJzz
}t7|��|tLztNzkrD�j�dt7|��|tLztNzfz��d�_!n$�j"�(|||���sd�_!�j)d�j#��_)�j#dkr!��*��r
|d�����|r!��*��r
|d�����fd�|D��}|r@|d}|j+�j+kr(|d�j#�j+|j#|j+fz����9|rG|d}|�,���j+kr"|d �j+|j#|j+fz�����|r?��,��|j+kr"|d!�j#�j+|j+fz�����|d"t7|��dz�j#�j+��,����*��fzd��|�-���|�.d#��$����*��sd	}�j/�_0|�r�|dj+dk�r�tcdt7|����D]O}||dz
�,��||j+kr!|d%|t7|��fzd���n"�Pd&�|D��}t&j�d�2|�����_|d't7�j��t7|��fzd���jd
kr&|d(��tg�j�j��n{�jdkr!|d)ti�j��z��nO�jd
krD�j�5d*��}�jd|�}|d+ti|��z��dSn���j!s
|d,��dSdS)-z<Get a response expected to match a given opcode and associd.rNFrNc��|Sr&r�r,s r�<lambda>z,ControlSession.getresponse.<locals>.<lambda>����a�rc�\��tj��j|�j|��Sr&rI��txt�thr!s  �rr�z,ControlSession.getresponse.<locals>.<lambda>��&���3�8�>�>�$�*�c�26�*�b�$B�$B�rzFragment collection beginsr�Tr�i�z$At %s, select with timeout %d beginsr�z"At %s, select with timeout %d endsz$ERR_INCOMPLETE: Received fragments:
z%d: %szlast fragment %sreceived
)znot rNzAt %s, socket read beginsriz'Flaky: I deliberately dropped a packet.zReceived %d octetsrz(AUTH - packet too short for MAC %d < %d
)�
packet_end�	mac_beginz*Received count of 0 in non-final fragment
zReceived second last fragment
c�4��g|]}|j�jk�|��Sr�r�)r��frag�rpkts  �rr�z.ControlSession.getresponse.<locals>.<listcomp>*s1���:�:�:�D�"�k�T�[�8�8� �8�8�8rz3duplicate %d octets at %d ignored, prior  %d at %d
r�z6received frag at %d overlaps with %d octet frag at %d
z6received %d octet frag at %d overlaps with frag at %d
z@Recording fragment %d, size = %d offset = %d,  end = %d, more=%sc��|jSr&r�)r�s rr�z,ControlSession.getresponse.<locals>.<lambda>Js��D�K�r��keyz#Hole in fragment sequence, %d of %dc�V�g|]&}tj�|j����'Sr)rr*r�r)r��fs  rr�z.ControlSession.getresponse.<locals>.<listcomp>[s:��$8�$8�$8�()�%(�H�$4�$4�Q�[�$A�$A�$8�$8�$8rz3Fragment collection ends. %d bytes  in %d fragmentszResponse packet:
zResponse packet:
%s
�
zFirst line:
%s
z7AUTH: Content untrusted due to authentication failure!
)6r>rDr��MAXFRAGSr�SERR_TOOMUCHr2r4r`�asctime�selectr<ro�SERR_SELECT�SERR_TIMEOUTr.�	enumerater��SERR_INCOMPLETErr*r+�recvr�SERR_SOCKETrG�randomrhr�rdrj�SERR_UNSPEC� _ControlSession__validate_packet�	_authpassr:r��MODE_SIX_HEADER_LENGTH�MODE_SIX_ALIGNMENT�KEYID_LENGTH�MINIMUM_MAC_LENGTH�
verify_macrr�r�r�rm�sortrPr?�ranger�r�r�r)r!r�r��timeo�	fragments�seenlastfrag�bail�warnrM�tvo�rd�_�ir�r��valid�_pend�not_earlierr�r��tempfraglist�eol�	firstliner�s`                      @r�getresponsezControlSession.getresponse�s������	���
������:�!��:�#�D�D��K�D�B�B�B�B��	��,�a�0�0�0�\	�
�A�I�D���(�
�#�#�&�|�4�4�4��
4��*�T�1����,�t�3���G�:��|�~�~�s�+�,�-.�
0�
0�
0�
4�#�]�D�I�;��B��D�D�
��Q�����<�
4�
4�
4�&�{�3�3�3�
4�����G�8��|�~�~�s�+�,�-.�
0�
0�
0��
8� �=��=�.�|�<�<�<��	G��
�a����
�.��
�(�(�C�E�E�E�)2�9�)=�)=�M�M�I�Q�� �J�,�,�X��1��d�j�j�l�l�8K�-K�L�L�L�L��
�(�(�)E�+7��+E�*F�G�G�G�&��7�7�7��G�/�$�,�.�.�@�!�D�D�D�
4��(�,�,�T�Y�^�^�D�-A�-A�B�B�����<�
4�
4�
4�&�{�3�3�3�
4�����{�
�t�{�f�m�o�o�=�=���A�1�E�E�E����G�(�3�w�<�<�7��;�;�;� ��&�&�D�
4����W�%�%�%�%���<�
4�
4�
4�&�{�3�3�3�
4�����*�*�4��&�'�J�J�E��
���~�	
+�$�)�	
+��
�%;�;���5�&�#5�5�6���w�<�<�5�<�#7�:L�#L�M�M��J�$�$�%P�&)�'�l�l�U�\�5I�L^�5^�%`�&a�b�b�b�%*�D�N�N���-�-�g�%�6;�.�=�=�+�%*�D�N�"�^�K�T�Z�K�8�D�N��z�Q���4�9�9�;�;����B�C�C�C���
��	�	���
���6�7�7�7��:�:�:�:�I�:�:�:�K��
�)�!�n���%���4�4��D�'� �J���'�-�{�/A�C�C�D�D�D���
� ��}���8�8�:�:���+�+��D�(� �K���T�[�A�B�C�C�C���
�t�x�x�z�z�K�,>�>�>���$��
�D�K��1C�D�E�F�F�F���G�)��9�~�~�a�'����{�D�H�H�J�J��	�	���=�=�?@�
A�
A�
A�
���T�"�"�"��N�N�7�7�N�8�8�8��9�9�;�;�
+�#��#�{����
�	�!�� 3�q� 8� 8��q�#�i�.�.�1�1� � �A� ��1��~�)�)�+�+�y��|�/B�B�B��� E�#$�c�)�n�n�"5�!6�78�:�:�:���C�
$8�$8�-6�$8�$8�$8�L�$'�H�$6�$6�r�w�w�|�7L�7L�$M�$M�D�M��G�/�"�4�=�1�1�3�y�>�>�B�C�DE�G�G�G��z�Q�����1�2�2�2�*�4�=�$�*�E�E�E�E���q�����5��T�]�8K�8K�K�L�L�L�L���q���"�m�0�0��7�7��$(�M�$�3�$�$7�	���0�4�	�?�?�B�C�C�C��4��y\	�z�~�	M��D�K�L�L�L�L�L�	M�	Ms$�!B<�<$C �.7H&�&$I
�*K�$K$c����j�
�jj}nd�}�fd�}|���tjjks'|���tjjkr#|d|���zd��dS|���tjjkr#|d|���zd��dS|�	��s|dd��dS|j
�j
kr|d|j
�j
fzd��dS|���|kr%|d	|���|fzd��dS|���r�|�
��r |d
|���z��dx�_�_t#t$t&j|���z|������|j|kr|d|j|fz��t-|��dzr|d
t-|��z��dSt.j|jzdzdz}t-|��|krC|d|jt-|��t.jz
fz��t#t4���dS)Nc��|Sr&rr�s rr�z2ControlSession.__validate_packet.<locals>.<lambda>wr�rc�\��tj��j|�j|��Sr&rIr�s  �rr�z2ControlSession.__validate_packet.<locals>.<lambda>xr�rz!Fragment received with version %dr�FzFragment received with mode %dz!Received request, wanted responsez&Received sequence number %d, wanted %dzReceived opcode %d, wanted %dz(Error %d received on non-final fragment
z,Association ID %d doesn't match expected %d
rz(Response fragment not padded, size = %d
���z>Response fragment claims %u octets payload, above %d received
T)rDr�r#rrr:r5r"r�r�r�r�r�r�r�r8r9r�SERR_SERVERr"�
server_errorsr�rhr�rir�r�)r!r�r�r�r�r�rM�shouldbesizes`       r�__validate_packetz ControlSession.__validate_packetrs����:�!��:�#�D�D��K�D�B�B�B�B���\�\�^�^�c�i�3�
3�
3��L�L�N�N�S�Y�5�5�5��G�7��l�l�n�n�%�&'�
)�
)�
)��5��9�9�;�;�#�)�0�0�0��G�4�t�y�y�{�{�B�A�F�F�F��5����!�!�	��G�7��;�;�;��5��=�D�M�)�)��G�<��]�D�M�2�3�45�
7�
7�
7��5��;�;�=�=�F�"�"��G�3��[�[�]�]�F�+�,�-.�
0�
0�
0��5��=�=�?�?�	 ��y�y�{�{�
'���@��|�|�~�~�&�'�'�'�'+�+�D�J���"�� �.�t�|�|�~�~�>�?������ � �
 ��<�7�"�"��D�@��L�'�*�+�
,�
,�
,�
�w�<�<�#��	��D�<��w�<�<� �
!�
!�
!��5�%�0�4�:�=��A�R�G���w�<�<�,�&�&��D�'��J��G���}�/G� G�H�I�
J�
J�
J�#�?�3�3�3��trrrNc�&�|���stt���d}	|�||||��	|�|||��}n2#t$r%}|r|jttfvrd}Yd}~�[|�d}~wwxYw	|S)z$send a request and save the responseTFN)rQr�SERR_NOHOSTr�r�rAr�r�)r!r�r�r�r:�retryrqrfs        r�doqueryzControlSession.doquery�s����}�}���	0�"�;�/�/�/���	����V�W�e�T�:�:�:�
��&�&�v�w�E�	�B�B����#�
�
�
���Q�Y�<��*I�I�I�!�E��H�H�H�H��G�����
����
��
s�A�
B
�(B�B�B
c���|�tjj|���t	|j��dzrt
t���g}|dkr{tt	|j��dz��D]V}|jd|zd|zdz�}tj
d|��\}}|�t|||�����W|�
d����|S)z(Read peer status, or throw an exception.)r�r�rrz!HHc��|jSr&)r�)�as rr�z)ControlSession.readstat.<locals>.<lambda>�s��!�)�rr�)r�rr}�CTL_OP_READSTATrhr>r�SERR_BADLENGTHr�rjrkrmr�r�)r!r��idlistr�rerPs      r�readstatzControlSession.readstat�s������C�K�7���I�I�I��t�}����!�	3�"�>�2�2�2����a�<�<��3�t�}�-�-�q�0�1�1�
;�
;���}�Q�q�S��1��Q��Y�/��$*�M�%��$>�$>�!��&��
�
�d�4��&�9�9�:�:�:�:����+�+��,�,�,��
rc�^�g}d}d}tj�|j��|_|jD]w}tj�|��}|dkr	||z
}|}�0|s0|dkr*|�|�����d}�bd|cxkrdkrn�r||z
}�x|r'|�|�����g}|D�]2}d|vrAtj�||�	d����\}	}
|
dd	�}
n|d}
}	|	���|
���}
}	|
r�	t|
d��}nv#t$rg	t|
��}|	d
kr|s|�d|
f��n4#t$r'|
ddkr|
ddkr
|
dd�}
|
}YnwxYwYnwxYw|
}|r|�|	||
ff����|�|	|f����4tj�
|��S)
z&Parse a response as a textual varlist.FrN�"�,rr��=r�N�delayzdelay-sr�)rr*r�r>r�rm�stripr�r��indexryr~�float�OrderedDict)r!�raw�kvpairs�instringr>r��cord�items�pairr��value�castedvalues            r�__parse_varlistzControlSession.__parse_varlist�sr��
��������(�(���7�7��
���	�	�A��8�#�#�A�&�&�D��C�x�x��A�
��'�<����
�!�s�(�(����x�~�~�/�/�0�0�0�����T�����C�������A�
����	-��N�N�8�>�>�+�+�,�,�,����	1�	1�D��d�{�{� �X�/�/��d�j�j��o�o�F�F�
��U��a�b�b�	���!�2�U�������e�k�k�m�m��C��
$�,�"%�e�Q�-�-�K�K��!�	,�	,�	,�,�&+�E�l�l���'�>�>�#�>�!�L�L�)�U�);�<�<�<���%�,�,�,�!�!�H��O�O�%��)�s�2B�2B�$)�!�B�$�K�E�&+����,������
	,����$���
1����c�K��#7�8�9�9�9�9����c�;�/�0�0�0�0��x�#�#�E�*�*�*s6�E$�$
G�/.F�G�.G�G�G�G�GNc��|�d}nd�|��}|�|||���|�|��S)z@Read system vars from the host as a dict, or throw an exception.NrNr�)r�r�)r�r��_ControlSession__parse_varlist)r!r��varlistr�r�r�s      rr�zControlSession.readvar
sM���?��E�E��H�H�W�%�%�E����V�W�E��:�:�:��#�#�C�(�(�(rc�|�|�tjj|d���|jstt���d|jvr,|jd|j�d���|_|j���|_|jtj	�
d��kS)z?Send configuration text to the daemon. Return True if accepted.T�r�r�r:r�NzConfig Succeeded)r�rr}�CTL_OP_CONFIGUREr>r�SERR_PERMISSIONr��rstripr*r+)r!�
configtexts  r�configzControlSession.configs������C�K�8�%�D�	�	2�	2�	2��}�	I�"�?�3�3�3�
��
�
%�
%� �M�*G�4�=�+>�+>�w�+G�+G�*G�H�D�M��
�,�,�.�.��
��}��� 2� 2�3E� F� F�F�Frc�>�td��D]�}|�tjj���tj��|_|j�tj	�
d����r8tj	�|j�����cS��ttur|j}n|j���}|j�d|z��t%t&���)zV
Ask for, and get, a nonce that can be replayed.
This combats source address spoofing
r)r�znonce=z## Nonce expected: %s)r�r�rr}�CTL_OP_REQ_NONCEr`rEr>r[r*r+r�r��str�bytes�decoderDr�r�
SERR_BADNONCE)r!r��resps   r�fetch_noncezControlSession.fetch_nonce!s���
�q���	?�	?�A��L�L��� <�L�=�=�=�"�i�k�k�D�O��}�'�'���(:�(:�8�(D�(D�E�E�
?��x�'�'��
�(;�(;�(=�(=�>�>�>�>�>�
?��%�<�<��=�D�D��=�'�'�)�)�D��
���0�4�7�8�8�8��}�-�-�-rc
�(�d}d}t|�����}g}i}|r|�t���|D�]\}	}
|�d|	�d|
��d��|	dkr|	�d|
��}n|	dkr�7|	d	kr�>|	d
kr%t
j�|
��|_�i|	dkr�pdD]�}|	�	|d
z��r�|	�
d
��\}}
	t|
��}
n%#t$rtt|	z���wxYw|
|vr'i|t|
��<|�|
��|
|t|
��|<���� |���|D]�}
t#��}|xjdz
c_dD]C}||t|
��vr*t'|||t|
��|���D|j�|����|�||j��|S)z=Extracts data from the key/value list into a more useful formNr�ztag=z, val=r�noncer�z
last.olderz
addr.olderrzlast.newest)r�r�r�rr�r�rrr�r�)�listr�r��
mru_kv_keyrMrrrrr[�splitryr~r�SERR_BADTAGrrmr�rF�setattrr)r!r��span�direct�mrurr��	fake_list�	fake_dict�tag�val�prefix�member�idxs              r�
__mru_analyzezControlSession.__mru_analyze7sl�������Y�_�_�&�&�'�'���	��	��	'��J�J�:�J�&�&�&��	6�	6�J�S�#��L�L�L�S�S�S�#�#�6��:�:�:��g�~�~�#&�3�3���,�����$�$����$�$���e�|�|��8�.�.�s�3�3�����
�%�%��Q�
6�
6���>�>�&�3�,�/�/�
6�$'�I�I�c�N�N�M�V�S�B�!�#�h�h����%�B�B�B�.�{�S�/@�A�A�A�B�����)�+�+�.0�	�#�c�(�(�+�!�(�(��-�-�-�25�I�c�#�h�h�'��/��
6�	�������		%�		%�C��*�*�C��J�J�!�O�J�J�R�
F�
F���Y�s�3�x�x�0�0�0��C���3�s�8�8�)<�V�)D�E�E�E���L����$�$�$�$����F�4�<� � � ��s�.C>�>"D c���|j�|�|jtjjkrO|�dd��|dz
}|dkrtt���|�d|zd��n�|jtjjkrZ|rd}|�dd��n�|xjdzc_t||j��}|�d|zd��nz|jttfvr\|r-td|dz��}|�d	|zd��n6td|dz��}|�d
|zd��n	|jr|�||||fS)Nz4no overlap between prior entries and server MRU listr�r	z/--->   Restarting from the beginning, retry #%uFz+Reverted to row limit from fragments limit.z0Row limit reduced to %d following CERR_BADVALUE.r�z7Frag limit reduced to %d following incomplete response.z7Row limit reduced to %d following  incomplete response.)
rBrr}�CERR_UNKNOWNVARrMr�
SERR_STALL�
CERR_BADVALUErA�minr�r��max)r!rf�restarted_count�	cap_frags�limit�fragss      r�__mru_query_errorz ControlSession.__mru_query_errorjs����;���G�
�[�C�K�7�
7�
7�
�L�L�+�,-�
/�
/�
/��q� �O���"�"�&�z�2�2�2��L�L�%�'6�7�89�
;�
;�
;�
;�
�[�C�K�5�
5�
5��	
:�!�	����0�12�4�4�4�4��#�#�q�(�#�#��E�4�#6�7�7�����.�05�6�78�:�:�:�:�
�[�_�l�;�
;�
;��
A��A�u�q�y�)�)�����4�6;�<�=>�@�@�@�@��A�u�q�y�)�)�����5�7<�=�>?�A�A�A�A�
�[�	��G��	�5�%�7�7rc	���d}d}d}d}t}|�i}|rt|��\}}}|���}	t��}
	t	dtz|j��}d|	|fz}|r8d|vrt
|d��|d<d|vrt
|d��|d<t|��\}
}||z
}		|�tj
j|���d	}n9#t$r,}d}|�
|||||��}|\}}}}Yd}~nd}~wwxYw|���}|r||��|�||
|��}|r|}	|
���rn�|sP|rt	t|d
z��}n5t	dtz|jt#|d
z|dzdz����}t%j��|jz
tj
jkr|���}	d
|	|rdnd|r|n||
fz}|t+|
t-|����z
}|�g|
_���n#t0$rYnwxYwt3|
||��|
S)zRetrieve MRU list datarTNrz%s, frags=%d�resall�resany)r�r�Fr��!rz%s, %s=%d%sr%r$)r��parse_mru_variablesr	rr rA�hex�generate_mru_parmsr�rr}�CTL_OP_READ_MRUr� _ControlSession__mru_query_errorr��_ControlSession__mru_analyzerr!r`rE�
NONCE_TIMEOUT�generate_mru_lastseenrhr�KeyboardInterrupt�
stitch_mru)r!r��rawhookrr"r#�sorter�sortkeyr%rrr$�req_buf�parms�
firstParms�recoverable_read_errorsrfrq�newNonces                   r�mrulistzControlSession.mrulist�s)�����	����������I��	D�%8��%C�%C�"�F�G�U�� � �"�"���y�y��U	���H��d�&9�:�:�E�$��u�~�5�G��
C��y�(�(�*-�i��.A�*B�*B�I�h�'��y�(�(�*-�i��.A�*B�*B�I�h�'� 2�9� =� =��E�:��z�!�G�G
&�C��L�L���(C�'.�!�0�0�0�.3�+�+��'�C�C�C�.2�+��0�0��O�1:�E�5�J�J�C�?B�<�O�Y��u�u�u�u�u�u�����	C����!�0�0�2�2�	��'��G�I�&�&�&� �-�-�i��v�F�F���%�$�E��#�#�%�%���"/�:� �:� #�H�e�a�i� 8� 8��� #�A��L�$(�$7�$'���	�(-��
�R��%9�%9�!:�!:���9�;�;���0�C�K�4M�M�M� �,�,�.�.�E�'� �&/�<�7�7�W�$-�8�5�5�5� �"�"��
�0��s�7�|�|�D�D�D���%�#%�D�L�OG
&�B��N!�	�	�	��D�	����	�4���)�)�)��s>�A6I�(C,�+I�,
D"�6"D�I�D"�"D$I�
I�Ic�$�|�tjj|d���g}|������D]�\}}|d���r�d|vr�|�d��\}}t|��}|t|��dz
krTtt|��|dz��D]3}|�tj�
�����4||||<��|S)zRetrieve ordered-list data.Tr�r�r�r�)r�rr}�CTL_OP_READ_ORDLIST_Ar�r��isdigitrryrhr�rmr�r�)r!�listtype�stanzasr�r��stem�stanzar�s        r�	__ordlistzControlSession.__ordlist�s�����C�K�=�#�$�	�	0�	0�	0��� �0�0�2�2�8�8�:�:�	.�	.�L�S�%��2�w��� � �
.�S�C�Z�Z�!$���3�����v��V�����C��L�L�1�,�,�,�"�3�w�<�<��!��<�<�?�?�����s�x�';�';�'=�'=�>�>�>�>�(-�����%���rc�,�|�d��S)zRetrieve reslist data.�addr_restrictions��_ControlSession__ordlistr(s r�reslistzControlSession.reslist
s���~�~�1�2�2�2rc�,�|�d��S)zRetrieve ifstats data.�ifstatsrHr(s rrLzControlSession.ifstatss���~�~�i�(�(�(r)F)rrNFrI)NNN)+r4r5r6r7r@r�rr}�CERR_UNSPEC�CERR_PERMISSION�CERR_BADFMT�
CERR_BADOP�
CERR_BADASSOCrr�
CERR_RESTRICTr�r$rMrOrQrmrr/rwr�r�r�r�r�r�r�r��CTL_OP_READVARr�rr	r0r/r=rIrJrLrrrr"r"�s)���������M��I�������#�\�����������!�:���#�\���!�:���!�:�	
�M����2@�@�@����
%�%�%�6�6�6�p#)�"2�����8'!�'!�'!�R���(3�3�3�3�jtM�tM�tM�l>�>�>�@����*����3+�3+�3+�3+�j ���{�1�u�)�)�)�)�G�G�G�.�.�.�,1�1�1�f%8�%8�%8�Nh�h�h�h�T
�
�
�3�3�3�)�)�)�)�)rr"c
��d}d}t}d|vr^|d}|d=d�d�d�d�d�d�d�d	�d
�d�d�d
�d�}|dkrd}|�.|�|��}|�tt|z���t	|�����D]�}|dvr�|�d��s|�d��rw|�d��}t|��dks7|dttt	td������vrtt|z�����tt|z���d|vr%t|�d����}|d=d|vr.|�dd��tjjz|d<|d=d|vr.|�dd��tjjz|d<|d=|||fS)Nr�c�J�tj�|j��Sr&�rrrr��rfs rr�z%parse_mru_variables.<locals>.<lambda>"s���� 3� 3�A�F� ;� ;�rc�L�tj�|j��Sr&rVrWs rr�z%parse_mru_variables.<locals>.<lambda>$s��#�(�"5�"5�a�f�"=�"=�!=�rc�,�|���Sr&�rrWs rr�z%parse_mru_variables.<locals>.<lambda>&s��������rc�*�|���Sr&rZrWs rr�z%parse_mru_variables.<locals>.<lambda>(s�������rc�*�|���Sr&�rrWs rr�z%parse_mru_variables.<locals>.<lambda>*s��a�j�j�l�l�rc�*�|���Sr&r]rWs rr�z%parse_mru_variables.<locals>.<lambda>,s��q�z�z�|�|�rc��|jSr&�rrWs rr�z%parse_mru_variables.<locals>.<lambda>.�
����u�rc��|jSr&r`rWs rr�z%parse_mru_variables.<locals>.<lambda>0�����rc��|jSr&�rrWs rr�z%parse_mru_variables.<locals>.<lambda>2rarc��|jSr&rerWs rr�z%parse_mru_variables.<locals>.<lambda>4rcrc��|jSr&�rrWs rr�z%parse_mru_variables.<locals>.<lambda>6s
��q�t�e�rc��|jSr&rhrWs rr�z%parse_mru_variables.<locals>.<lambda>8s��q�t�r)�lstintz-lstintrz-avgintr�z-addrr�z-count�scorez-score�dropz-droprj)�mincount�mindrop�minscorer(r)�kod�limited�	maxlstint�	minlstint�laddr�recentr�r%r$zaddr.zlast.r�r�r�rr%rpr)rrq)r��getr�SERR_BADSORTr�keysr[rrh�maprr��
SERR_BADPARAMryrr�RES_KOD�RES_LIMITED)r�r6r7r%�sortdict�k�kns       rr+r+s2��
�F��G��E�
�����F�#���f��<�;�=�=�+�+�+�+�*�*�+�+�$�_�$�n�$�_�$�n�#�O�#�^�1
�
��4�h����G����\�\�'�*�*�F��~�&�|�g�'=�>�>�>�
�)�.�.�"�"�
#�
#�6�6���+�+�+�
�
�\�\�'�
"�
"�	6�a�l�l�7�&;�&;�	6�������B��2�w�w�!�|�|�r�!�u�C��T�%��)�)�_�_�,E�,E�E�E�&�}�q�'8�9�9�9��"�=�1�#4�5�5�5��)����I�M�M�'�*�*�+�+���g���	���'�m�m�H�a�8�8� #�	� 1�2�	�(���e���I���'�m�m�H�a�8�8� #�	� 5�6�	�(���i� ��7�E�!�!rc��i}g}t|j��D]?\}}|j|vr
g||j<||j�||jf���@|D]%}|t||d����dd�z
}�&d�|D��}|�d���|D]}|j�|���|r<|j�|���|dkr|j���dSdSdS)Nc��|dS)Nr�rr�s rr�zstitch_mru.<locals>.<lambda>is
���!��rr�r�c��g|]
}|d��SrIrr�s  rr�zstitch_mru.<locals>.<listcomp>js��%�%�%��q��t�%�%�%rT)�reverser�)	r�rr�rmr��sortedr��popr�)rr6r7�addrdict�deletiar��entryr�s        rr4r4[sH���H��G����-�-�5�5�
��E��:�X�%�%�#%�H�U�Z� �����#�#�Q��
�O�4�4�4�4��C�C���6�(�4�.�n�n�=�=�=�c�r�c�B�B���%�%�W�%�%�%�G��L�L��L����
��������������#�����f��%�%�%��f����L� � �"�"�"�"�"�	#�#��rc���|sdSd�t|�����D��}dd�|��z}d|vrd|dz}||z
}n|}||fS)N)rNrNc�0�g|]}|ddk�d|z��S)rruz%s=%sr)r��its  rr�z&generate_mru_parms.<locals>.<listcomp>{s7��J�J�J��b��e�x�6G�6G��2��6G�6G�6Grz, ruz, recent=%s)rr�r�)r��parmStrsr9r:s    rr-r-ws������v�J�J��y���0�0�1�1�J�J�J�H��4�9�9�X�&�&�&�E��9���"�Y�x�%8�8�
��e��
�
��
��*��rc�:�d}tt|j����D]v}|jt|j��|z
dz
}d||j||jfz}|t|��zt|��zt
jjkrn||z
}�w|S)NrNr�z, addr.%d=%s, last.%d=%s)r�rhrr�r�rr}r�)r�existingBufferSize�bufr�rf�incrs      rr2r2�s���
�C�
�3�t�|�$�$�
%�
%�����L��T�\�*�*�Q�.��2�3��)�Q����1�6�,B�B����S���)�C��I�I�5���,�
-�
-��E��4�K�C�C��Jrc��|d�d��}t|��dkrdS	t|d��S#t$rYdSwxYw)Nrr�r����r�)rrhryr~)�token�bitss  rr
r
�sb����8�>�>�#���D�
�4�y�y�A�~�~��r���4��7�|�|��������r�r����s�A�
A�Ac�b�eZdZdZdd�Zd�Zd�Zdd�Zed���Z	ed���Z
dd
�ZdS)
rzz+MAC authentication manager for NTP packets.Nc��i|_|��t|��D]�}d|vr|d|�d���}|���}|s�:|���\}}}|���dvrd}t
|��dkr'tj�	|dd���}||f|jt|��<��dSdS)N�#)�AES�
AES128CMACzAES-128r
r�)�	passwords�openr�r�r�upperrhrr�r�ry)r!�keyfiler�r8r7r9s      rr$zAuthenticator.__init__�s���������W�
�
�

?�

?���$�;�;�� 0����C��� 0�1�D��z�z�|�|�����+/�:�:�<�<�(�����=�=�?�?�&;�;�;�'�G��v�;�;��#�#�!�X�3�3�F�3�B�3�K�@�@�F�.5�v�->���s�5�z�z�*�*���

?�

?rc�*�t|j��S)z1return the number of keytype/passwd tuples stored)rhr�r(s r�__len__zAuthenticator.__len__�s���4�>�"�"�"rc�6�|j�|��S)z#get a keytype/passwd tuple by keyid)r�rv)r!r8s  r�__getitem__zAuthenticator.__getitem__�s���~�!�!�%�(�(�(rc��|�||jvr|f|j|zS|ddfStd��D]�}|�d��ryt|���d��}|j|\}}|�t
�t
|��dkrtj�	|��}|||fcS��t
�)z?Get the keytype/passwd tuple that controls localhost and its idNz/etc/ntpsec/ntp.confr}r�r
)
r�r�r[ryrr~rhrr�r�)r!r8r�r7r9s     rr}zAuthenticator.control�s��������&�&��x�$�.��"7�7�7��t�T�*�*��/�0�0�		0�		0�D����y�)�)�
0��D�J�J�L�L��O�,�,��$(�N�5�$9�!��&��>�$�$��v�;�;��#�#� �X�3�3�F�;�;�F��w��/�/�/�/�
0��rc�X�tj�|��sdStj�tj�|��tj�|��|��}|rt
|��dkrdStjd|��|zS)z)Create the authentication payload to sendFrr�!I)	rr�	checknamer\r*r+rhrjr�)ror8r7r9�mac2s     rr�zAuthenticator.compute_mac�s����x�!�!�'�*�*�	��5��x�|�|�C�H�.�.�w�7�7��H�.�.�v�6�6��A�A���	�s�4�y�y�A�~�~��3��{�4��'�'�$�.�.rc�F�t|��tjjkS)zDoes this packet have a MAC?)rhrr�
LEN_PKT_NOMAC)�packets r�have_maczAuthenticator.have_mac�s���6�{�{�S�Y�4�4�4rrMc��|d|�}|||tz�}||tzd�}tjd|��\}||jvrdS|j|\}}tj�|��sdStj�tj�	|��tj�	|��|��}	|	sdS	tj||	��S#t$r	||	kcYSwxYw)zDDoes the MAC on this packet verify according to credentials we have?Nr�F)
r�rjrkr�rrr�r\r*r+�hmac�compare_digestra)
r!r�r�r�ror8r\r7r9r�s
          rr�zAuthenticator.verify_mac�s����*��%���y��<�!7�7�8���Y�|�+�,�,�-���=��u�-�-������&�&��5� �N�5�1���&��x�!�!�'�*�*�	��5��x�|�|�C�H�.�.�w�7�7��H�.�.�v�6�6��A�A���	��5�	��&�s�D�1�1�1���	�	�	��$�;����	���s�"C7�7D
�	D
r&)rMrM)r4r5r6r7r$r�r�r}r8r�r�r�rrrrzrz�s�������1�1�?�?�?�?�&#�#�#�)�)�)�����(�/�/��\�/��5�5��\�5������rrz)Hr7�
__future__rrr�r�r�r�r�rr�rjrBr`�ntp.controlr�	ntp.magic�ntp.ntpc�ntp.util�ntp.polyr�r1r3r�r�r�r�r��MAX_BARE_MAC_LENGTHr�
BaseExceptionr>rLr�r��SERR_BADFMTr��
SERR_BADOP�
SERR_BADASSOC�SERR_UNKNOWNVAR�
SERR_BADVALUEr�r�r�r�r�r�r�r�r�r�r�rrzr�r�rrrwrrCr�r�rrr"r+r4r-r2r
rzrrr�<module>r�s���I�I�V0�/�/�/�/�/�/�/���������	�	�	�	�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�������������������������(���
���
�	�����������%%�%%�%%�%%�%%�%%�%%�%%�P�����M����@�@�@�@�@��@�@�@�FA-�A-�A-�A-�A-�F�A-�A-�A-�H��������"@��D��
:�
�;�
�A��B�
�9��?��'��<��<��'��!��F��)��$��
�
�5�
�+�
�!��'��
N�
�!��4��6��%(�J�����(%?�%?�%?�%?�%?�%?�%?�%?�PI�I�I�I�I�I�I�I������}����N
)�N
)�N
)�N
)�N
)�N
)�N
)�N
)�bE"�E"�E"�P#�#�#�8
�
�
� 
�
�
����Y�Y�Y�Y�Y�Y�Y�Y�Y�Yr