a
    kÄ#dÑ  ã                   @   sÊ   d Z ddlmZ ddlZddlmZ ddlmZmZ ddl	m
Z
 ddlmZ ddlmZ g d	¢g d
¢fZer–ejjdu r€edƒ‚ejjddgg d¢fZddd„Zddd„Zddd„Zdd„ Zddd„ZdS )z(
VoIP (Voice over IP) related functions
é    )Úabsolute_importN)Úsniff)ÚIPÚUDP)ÚRTP)ÚWINDOWS)Úconf)Úsoxú-tú.ul)ú-r
   Zossdspz/dev/dspz*Sox must be installed to play VoIP packetsr
   r   )r   r
   Z	waveaudioé   c                 C   sž   d}|}d}t | ƒt |ƒkr"|}nt | ƒt |ƒk r6| }t |ƒ}t|| ƒD ]8}|| || ||d  … ||| ||d  …  7 }qJ| |d … ||d … |fS )NÚ r   é   )ÚlenÚrange)ÚxÚyZsample_sizeÚmÚssZmin_Zr_Úi© r   úI/var/www/html/gsl.tools/lib/python3.9/site-packages/scapy/modules/voip.pyÚ_merge_sound_bytes   s    6r   c                    s˜   t jtd td  t jt jd�}|j|j ‰ }‡ ‡fdd„}zD|du r\tf d|dœ|¤Ž n|D ]}||ƒ q`W ˆ  ¡  | ¡  nˆ  ¡  | ¡  0 dS )a—  Play VoIP packets with RAW data that
    are either sniffed either from an IP, or
    specified as a list.

    It will play only the incoming packets !

    :param s1: The IP of the src of all VoIP packets.
    :param lst: (optional) A list of packets to load
    :type s1: string
    :type lst: list

    :Example:

    >>> voip_play("64.2.142.189")
    while calling '411@ideasip.com'

    >>> voip_play("64.2.142.189", lst)
    with list a list of packets with VoIP data
    in their RAW layer

    .. seealso:: voip_play2
    to play both the outcoming and incoming packets
    at the same time.

    .. seealso:: voip_play3
    to read RTP VoIP packets
    r   r   ©ÚstdinÚstdoutc                    sT   | sd S |   t¡r|   t¡s d S |  t¡}ˆ|jkrPˆ  |  tj¡jdd … ¡ d S ©Né   )	Úhaslayerr   r   ÚgetlayerÚsrcÚwriter   Ú	raw_layerÚload)ÚpktÚip©ÚdspÚs1r   r   ÚplayQ   s    

zvoip_play.<locals>.playN©ÚstoreZprn)Ú
subprocessÚPopenÚsox_baseÚPIPEr   r   r   Úclose)r)   ÚlstÚkargsÚprocÚrdr*   Úpr   r'   r   Ú	voip_play0   s    ÿ
ÿr7   c                 K   s   t | |fi |¤ŽS )z0Same than voip_play, backward compatibility
    )r7   )r)   r2   r3   r   r   r   Ú
voip_play1d   s    r8   c                    s¼   t jtd ddg td  t jt jd�}|j|j ‰ }dada‡ ‡fdd„}z@tf d|d	œ|¤Ž W zˆ  	¡  | 	¡  W q¸ t
yŠ   Y q¸0 n*zˆ  	¡  | 	¡  W n t
y´   Y n0 0 d
S )zÞ
    Same than voip_play, but will play
    both incoming and outcoming packets.
    The sound will surely suffer distortion.

    Only supports sniffing.

    .. seealso:: voip_play
    to play only incoming packets.
    r   z-cÚ2r   r   r   c                    s˜   | sd S |   t¡r|   t¡s d S |  t¡}ˆ|j|jfv r”|jˆkr`t|  tj¡j	dd … 7 ant
|  tj¡j	dd … 7 a
ttt
ƒ\aa
}ˆ  |¡ d S r   )r   r   r   r    r!   ÚdstÚx1r   r#   r$   Úx2r   r"   )r%   r&   Úrr'   r   r   r*   |   s    

zvoip_play2.<locals>.playr+   N)r-   r.   r/   r0   r   r   r;   r<   r   r1   Ú	Exception)r)   r3   r4   r5   r*   r   r'   r   Ú
voip_play2j   s&    ÿür?   c                 K   sÆ   t jtd td  t jt jd�}|j|j }}|fdd„}z\| du rZtf d|dœ|¤Ž n| D ]}||ƒ q^W z| ¡  | ¡  W qÂ ty”   Y qÂ0 n*z| ¡  | ¡  W n ty¾   Y n0 0 dS )z™Same than voip_play, but made to
    read and play VoIP RTP packets, without
    checking IP.

    .. seealso:: voip_play
    for basic VoIP packets
    r   r   r   c                 S   s.   | r*|   t¡r*|   t¡r*| |  t¡j¡ d S )N)r   r   r   r"   r    r$   )r%   r(   r   r   r   r*   ¡   s    zvoip_play3.<locals>.playNr+   )	r-   r.   r/   r0   r   r   r   r1   r>   )r2   r3   r4   r(   r5   r*   r6   r   r   r   Ú
voip_play3•   s(    ÿür@   )r   )N)N)N)Ú__doc__Ú
__future__r   r-   Zscapy.sendrecvr   Zscapy.layers.inetr   r   Zscapy.layers.rtpr   Zscapy.constsr   Zscapy.configr   r/   Úprogr	   ÚOSErrorr   r7   r8   r?   r@   r   r   r   r   Ú<module>   s"   

4
+