o
    ]i                     @   s   d dl mZ d dlmZ ddlmZmZ g dZdZg dZ	dZ
d	ZG d
d deZdd ZG dd dZG dd dZe  dS )    )htonl)Optional   )ffilib)ErrorPolicySession__version__z1.0.0)znothing to reportzunspecified failurezunsupported parameterzcouldn't allocate memoryzcouldn't deallocate properlyzcouldn't initializez'can't process as much data as requestedzauthentication failurezcipher failurezreplay check failed (bad index)z#replay check failed (index too old)zalgorithm failed test routinezunsupported operationzno appropriate context foundz$unable to perform desired validationzcan't use key any morezerror in use of socketzerror in use POSIX signalsznonce check failedzcouldn't read datazcouldn't write datazerror parsing datazerror encoding datazerror while using semaphoreszerror while using pfkeyz&error MKI present in packet is invalidz#packet index is too old to considerz#packet index advanced, reset needed      c                   @   s   e Zd ZdZdS )r   z:
    Error that occurred making a `libsrtp` API call.
    N)__name__
__module____qualname____doc__ r   r   F/home/ubuntu/.local/lib/python3.10/site-packages/pylibsrtp/__init__.pyr   1   s    r   c                 C   s   | t jkrtt|  d S N)r   srtp_err_status_okr   ERRORS)rcr   r   r   _srtp_assert9   s   
r   c                   @   sX  e Zd ZdZejZejZej	Z
ejZejZejZejZejZdedefdee dedededdf
d	d
ZedefddZejdeddfddZedee fddZejdee ddfddZedefddZedefddZejdeddfddZedefddZ e jdeddfddZ edefddZ!e!jdeddfddZ!dS )r   z(
    Policy for single SRTP stream.
    Nr   key	ssrc_type
ssrc_valuesrtp_profilereturnc                 C   s\   t d| _|| _ttt | jj| tt	t | jj
| || _|| _|| _d S )Nzsrtp_policy_t *)r   new_policy_srtp_profiler   r   +srtp_crypto_policy_set_from_profile_for_rtp	addressofrtp,srtp_crypto_policy_set_from_profile_for_rtcprtcpr   r   r   )selfr   r   r   r   r   r   r   __init__U   s   
zPolicy.__init__c                 C   s   | j jdkS )z_
        Whether retransmissions of packets with the same sequence number are allowed.
        r   r   allow_repeat_txr%   r   r   r   r(   n   s   zPolicy.allow_repeat_txr(   c                 C   s   |rdnd| j _d S )Nr   r   r'   )r%   r(   r   r   r   r(   u   s   c                 C   s   | j du rdS t| j S )z4
        The SRTP master key + master salt.
        N)_Policy__cdatar   bufferr)   r   r   r   r   y   s   
z
Policy.keyc                 C   s   |d u rd | _ tj| j_d S t| jt| j }t	|t
s#tdt||k r/td| tdt|| _ || j dt|< | j | j_d S )Nzkey must be bytesz"key must contain at least %d byteszunsigned char[]r   )r*   r   NULLr   r   r   "srtp_profile_get_master_key_lengthr   #srtp_profile_get_master_salt_length
isinstancebytes	TypeErrorlen
ValueErrorr   )r%   r   expected_lengthr   r   r   r      s    


c                 C   s   | j S )z#
        The SRTP profile.
        )r   r)   r   r   r   r      s   zPolicy.srtp_profilec                 C   
   | j jjS )z 
        The SSRC type.
        r   ssrctyper)   r   r   r   r         
zPolicy.ssrc_typec                 C      || j j_d S r   r6   )r%   r   r   r   r   r         c                 C   r5   )z:
        The SSRC value, if it is not a wildcard.
        r   r7   valuer)   r   r   r   r      r9   zPolicy.ssrc_valuec                 C   r:   r   r<   )r%   r   r   r   r   r      r;   c                 C   s   | j jS )z?
        The window size to use for replay protection.
        r   window_sizer)   r   r   r   r?      s   zPolicy.window_sizer?   c                 C   s   || j _d S r   r>   )r%   r?   r   r   r   r?      s   )"r   r   r   r   r   srtp_profile_aes128_cm_sha1_80SRTP_PROFILE_AES128_CM_SHA1_80srtp_profile_aes128_cm_sha1_32SRTP_PROFILE_AES128_CM_SHA1_32srtp_profile_aead_aes_128_gcmSRTP_PROFILE_AEAD_AES_128_GCMsrtp_profile_aead_aes_256_gcmSRTP_PROFILE_AEAD_AES_256_GCMssrc_undefinedSSRC_UNDEFINEDssrc_specificSSRC_SPECIFICssrc_any_inboundSSRC_ANY_INBOUNDssrc_any_outboundSSRC_ANY_OUTBOUNDr   r0   intr&   propertyboolr(   setterr   r   r   r   r?   r   r   r   r   r   >   s^    
r   c                   @   s   e Zd ZdZddee ddfddZdeddfddZd	eddfd
dZ	de
de
fddZde
de
fddZde
de
fddZde
de
fddZdddZdS )r	   z
    SRTP session, which may comprise several streams.

    If `policy` is not specified, streams should be added later using the
    :func:`add_stream` method.
    Npolicyr   c                 C   sb   t d}|d u rt j}n|j}tt|| t dd| _t | j| _	t 
|dd | _d S )Nzsrtp_t *zchar[]i  c                 S   s   t | d S )Nr   )r   srtp_dealloc)xr   r   r   <lambda>   s    z"Session.__init__.<locals>.<lambda>)r   r   r,   r   r   r   srtp_create_cdatar+   _buffergc_srtp)r%   rT   srtpr   r   r   r   r&      s   
zSession.__init__c                 C   s   t t| jd |j dS )z
        Add a stream to the SRTP session, applying the given `policy`
        to the stream.

        :param policy: :class:`Policy`
        r   N)r   r   srtp_add_streamr\   r   )r%   rT   r   r   r   
add_stream   s   zSession.add_streamr7   c                 C   s   t t| jd t| dS )zs
        Remove the stream with the given `ssrc` from the SRTP session.

        :param ssrc: :class:`int`
        r   N)r   r   srtp_remove_streamr\   r   )r%   r7   r   r   r   remove_stream   s   zSession.remove_streampacketc                 C      |  |tjtS )z
        Apply SRTP protection to the RTP `packet`.

        :param packet: :class:`bytes`
        :rtype: :class:`bytes`
        )_Session__processr   srtp_protectSRTP_MAX_TRAILER_LENr%   rb   r   r   r   protect      zSession.protectc                 C   rc   )z
        Apply SRTCP protection to the RTCP `packet`.

        :param packet: :class:`bytes`
        :rtype: :class:`bytes`
        )rd   r   srtp_protect_rtcpSRTP_MAX_SRTCP_TRAILER_LENrg   r   r   r   protect_rtcp   ri   zSession.protect_rtcpc                 C      |  |tjS )z
        Verify SRTP protection of the SRTP packet.

        :param packet: :class:`bytes`
        :rtype: :class:`bytes`
        )rd   r   srtp_unprotectrg   r   r   r   	unprotect      zSession.unprotectc                 C   rm   )z
        Verify SRTCP protection of the SRTCP packet.

        :param packet: :class:`bytes`
        :rtype: :class:`bytes`
        )rd   r   srtp_unprotect_rtcprg   r   r   r   unprotect_rtcp   rp   zSession.unprotect_rtcpr   c                 C   s   t |ts	tdt|t| j| krtdtd}t||d< || jdt|< t	|| j
d | j| | jd|d  S )Nzpacket must be byteszpacket is too longzint *r   )r/   r0   r1   r2   rY   r3   r   r   rZ   r   r\   )r%   datafunctrailerlen_pr   r   r   	__process	  s   

zSession.__processr   )r   )r   r   r   r   r   r   r&   r_   rP   ra   r0   rh   rl   ro   rr   rd   r   r   r   r   r	      s    					r	   N)socketr   typingr   _bindingr   r   __all__r
   r   rf   rk   	Exceptionr   r   r   r	   	srtp_initr   r   r   r   <module>   s    ! W