a
    kÄ#d¯1  ã                   @   sø   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
mZ ddlmZmZmZmZmZmZmZmZmZmZmZmZmZ dd	d
œZddd
œZi ZG dd„ de
ƒZG dd„ de
ƒZG dd„ de
ƒZG dd„ de
ƒZ eeedd� eee dd� dS )aG  
STAMP (Simple Two-Way Active Measurement Protocol) - RFC 8762.

References:
    * `Simple Two-Way Active Measurement Protocol [RFC 8762]
      <https://www.rfc-editor.org/rfc/rfc8762.html>`_
    * `Simple Two-Way Active Measurement Protocol Optional Extensions [RFC 8972]
      <https://www.rfc-editor.org/rfc/rfc8972.html>`_
é    )Úconfig)ÚPacket_metaclass)ÚUDP)ÚTimeStampField)ÚPacketÚbind_layers)ÚBitEnumFieldÚBitFieldÚByteEnumFieldÚ	ByteFieldÚ
FlagsFieldÚIntFieldÚMultipleTypeFieldÚNBytesFieldÚPacketFieldÚPacketListFieldÚ
ShortFieldÚStrLenFieldÚUTCTimeFieldz/No External Synchronization for the Time Sourcez2Clock Synchronized to UTC using an External Source)r   é   zNTP 64-bit Timestamp Formatz PTPv2 Truncated Timestamp Formatc                   @   sJ   e Zd ZdZdZedddeƒedddeƒedddƒe	d	dƒgZ
d
d„ ZdS )ÚErrorEstimatead  
    The Error Estimate specifies the estimate of the error and
    synchronization. The format of the Error Estimate field
    (defined in Section 4.1.2 of `RFC 4656
    <https://www.rfc-editor.org/rfc/rfc4656.html>`_) is reported below::

        0                   1
        0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
        |S|Z|   Scale   |   Multiplier  |
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

    ``S`` is interpreted as follows:

        +-------+-------------------------------------------------------+
        | Value | Description                                           |
        +-------+-------------------------------------------------------+
        |   0   | there is no notion of external synchronization for    |
        |       | the time source                                       |
        +-------+-------------------------------------------------------+
        |   1   | the party generating the timestamp has a clock that   |
        |       | is synchronized to UTC using an external source       |
        +-------+-------------------------------------------------------+

    ``Z`` is interpreted as follows (defined in Section 2.3 of `RFC 8186
    <https://www.rfc-editor.org/rfc/rfc8186.html>`_):

        +-------+---------------------------------------+
        | Value | Description                           |
        +-------+---------------------------------------+
        |   0   | NTP 64-bit format of a timestamp      |
        +-------+---------------------------------------+
        |   1   | PTPv2 truncated format of a timestamp |
        +-------+---------------------------------------+

    ``Scale`` and ``Multiplier`` are linked by the following relationship::

        ErrorEstimate = Multiplier*2^(-32)*2^Scale (in seconds)


    References:
        * `A One-way Active Measurement Protocol (OWAMP) [RFC 4656]
          <https://www.rfc-editor.org/rfc/rfc4656.html>`_
        * `Support of the IEEE 1588 Timestamp Format in a Two-Way Active
          Measurement Protocol (TWAMP) [RFC 8186]
          <https://www.rfc-editor.org/rfc/rfc8186.html>`_
    zError EstimateÚSr   r   ÚZÚscaleé   Ú
multiplierc                 C   s   t jjS ©N)r   ÚconfZpadding_layer)ÚselfÚpayload© r    úJ/var/www/html/gsl.tools/lib/python3.9/site-packages/scapy/contrib/stamp.pyÚguess_payload_classq   s    z!ErrorEstimate.guess_payload_classN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Únamer   Ú_sync_typesÚ_timestamp_typesr	   r   Úfields_descr"   r    r    r    r!   r   8   s   0
ür   c                   @   sl   e Zd ZdZdZeddddƒeddeƒed	dƒe	d
ddd„ d�gZ
dd„ Zi Zedd„ ƒZeddd„ƒZdS )ÚSTAMPTestTLVa�  
    The STAMP Test TLV defined in Section 4 of [RFC 8972] provides a flexible
    extension mechanism for optional informational elements.

    The TLV Format in a STAMP Test packet is reported below::

         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
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
        |STAMP TLV Flags|     Type      |           Length              |
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
        ~                            Value                              ~
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+


    +-------+---------+-------------------------------------------------+
    | Field           | Description                                     |
    +-----------------+-------------------------------------------------+
    | STAMP TLV Flags | 8-bit field; for the details about the STAMP    |
    |                 | TLV Flags Format, see RFC 8972                  |
    +-----------------+-------------------------------------------------+
    | Type            | characterizes the interpretation of the Value   |
    |                 | field                                           |
    +-----------------+-------------------------------------------------+
    | Length          | the length of the Value field in octets         |
    +-----------------+-------------------------------------------------+
    | Value           | interpreted according to the value of the Type  |
    |                 | field                                           |
    +-----------------+-------------------------------------------------+


    References:
        * `Simple Two-Way Active Measurement Protocol Optional Extensions
          [RFC 8972] <https://www.rfc-editor.org/rfc/rfc8972.html>`_
    zSTAMP Test Packet - Generic TLVÚflagsr   é   ZUMIRRRRRÚtypeNÚlenÚvalueÚ c                 C   s   | j S r   )r/   ©Úpktr    r    r!   Ú<lambda>£   ó    zSTAMPTestTLV.<lambda>©Zlength_fromc                 C   s   d|fS )Nr5   r    )r   Úpr    r    r!   Úextract_padding¦   s    zSTAMPTestTLV.extract_paddingc                 C   s   | | j | jj< d S r   )Úregistered_stamp_tlvr.   Údefault)Úclsr    r    r!   Úregister_variant«   s    zSTAMPTestTLV.register_variantc                 O   s&   |r"t |dd… ƒ}| j || ¡S | S )Nr   é   )Úordr9   Úget)r;   r3   ÚargsZkargsZtmp_typer    r    r!   Údispatch_hook¯   s    zSTAMPTestTLV.dispatch_hook)N)r#   r$   r%   r&   r'   r   r
   Ú_stamp_tlvsr   r   r*   r8   r9   Úclassmethodr<   rA   r    r    r    r!   r+   y   s   $
ü
r+   c                   @   sr   e Zd ZdZdZeddƒeeddƒdd„ fgedddd	�ƒe	d
e
ƒ e
ƒeddƒedddƒedg edd„ d�gZdS )Ú%STAMPSessionSenderTestUnauthenticateda*  
    Extended STAMP Session-Sender Test Packet in Unauthenticated Mode.

    The format (defined in Section 3 of `RFC 8972
    <https://www.rfc-editor.org/rfc/rfc8972.html>`_) is shown below::

         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
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
        |                        Sequence Number                        |
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
        |                          Timestamp                            |
        |                                                               |
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
        |         Error Estimate        |             SSID              |
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
        |                                                               |
        |                                                               |
        |                         MBZ (28 octets)                       |
        |                                                               |
        |                                                               |
        |                                                               |
        |                                                               |
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
        ~                            TLVs                               ~
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

    References:
        * `Simple Two-Way Active Measurement Protocol Optional Extensions
          [RFC 8972] <https://www.rfc-editor.org/rfc/rfc8972.html>`_
    zSTAMP Session-Sender TestÚseqr   Útsc                 C   s   | j jdkS ©Nr   ©Úerr_estimater   r2   r    r    r!   r4   Ý   r5   z.STAMPSessionSenderTestUnauthenticated.<lambda>ÚQ©ÚfmtrI   Ússidr   Zmbzé   Útlv_objectsc                 C   s   | j jd d S ©Nr-   é,   ©Úparentr/   r2   r    r    r!   r4   å   r5   r6   N)r#   r$   r%   r&   r'   r   r   r   r   r   r   r   r   r   r+   r*   r    r    r    r!   rD   ·   s"   ÿÿû
ÿôrD   c                   @   sÚ   e Zd ZdZdZeddƒeeddƒdd„ fgedddd	�ƒe	d
e
ƒ e
ƒeddƒeeddƒdd„ fgedddd	�ƒeddƒeeddƒdd„ fgedddd	�ƒe	de
ƒ e
ƒeddƒeddƒedddƒedg edd„ d�gZdS )Ú(STAMPSessionReflectorTestUnauthenticatedaU  
    Extended STAMP Session-Reflector Test Packet in Unauthenticated Mode.

    The format (defined in Section 3 of `RFC 8972
    <https://www.rfc-editor.org/rfc/rfc8972.html>`_) is shown below::

         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
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
        |                        Sequence Number                        |
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
        |                          Timestamp                            |
        |                                                               |
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
        |         Error Estimate        |           SSID                |
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
        |                          Receive Timestamp                    |
        |                                                               |
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
        |                 Session-Sender Sequence Number                |
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
        |                  Session-Sender Timestamp                     |
        |                                                               |
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
        | Session-Sender Error Estimate |           MBZ                 |
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
        |Ses-Sender TTL |                   MBZ                         |
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
        ~                            TLVs                               ~
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

    References:
        * `Simple Two-Way Active Measurement Protocol Optional Extensions
          [RFC 8972] <https://www.rfc-editor.org/rfc/rfc8972.html>`_
    zSTAMP Session-Reflector TestrE   r   rF   c                 C   s   | j jdkS rG   rH   r2   r    r    r!   r4     r5   z1STAMPSessionReflectorTestUnauthenticated.<lambda>rJ   rK   rI   rM   r   Zts_rxc                 C   s   | j jdkS rG   rH   r2   r    r    r!   r4     r5   Z
seq_senderZ	ts_senderc                 C   s   | j jdkS rG   )Úerr_estimate_senderr   r2   r    r    r!   r4   $  r5   rU   Zmbz1Z
ttl_senderéÿ   Zmbz2é   rO   c                 C   s   | j jd d S rP   rR   r2   r    r    r!   r4   -  r5   r6   N)r#   r$   r%   r&   r'   r   r   r   r   r   r   r   r   r   r   r+   r*   r    r    r    r!   rT   é   sF   #ÿÿûÿÿûÿÿû
ÿârT   i^  )Údport)ZsportN)!r&   Zscapyr   Zscapy.base_classesr   Zscapy.layers.inetr   Zscapy.layers.ntpr   Zscapy.packetr   r   Zscapy.fieldsr   r	   r
   r   r   r   r   r   r   r   r   r   r   r(   r)   rB   r   r+   rD   rT   r    r    r    r!   Ú<module>	   s&   
<þþA>2H