a
    kÄ#d¯!  ã                   @   sb  d Z ddlmZ ddlZddl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mZmZ ddlmZmZ dd	lmZmZ dd
lmZmZmZmZ ddlm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( ddl)m*  m+Z+ dZ,dZ-dZ.dZ/dZ0G dd„ deƒZ1G dd„ de&ƒZ2G dd„ de&ƒZ3G dd„ de2ƒZ4G dd„ de(ƒZ5dS ) zt
Implementation of TUN/TAP interfaces.

These allow Scapy to act as the remote side of a virtual network interface.
é    )Úabsolute_importN)Úioctl)ÚrawÚbytes_encode)Úconf)Ú
BIG_ENDIANÚBSDÚLINUX)ÚETHER_TYPESÚMTU)ÚwarningÚlog_runtime)ÚFieldÚ
FlagsFieldÚStrFixedLenFieldÚXShortEnumField)Únetwork_name)ÚIP)ÚIPv46ÚIPv6)ÚEther)ÚPacket)ÚSimpleSocketiÊT@é   é   i   é   c                   @   s   e Zd Zdd„ ZdS )ÚNativeShortFieldc                 C   s   t  | ||d¡ d S )Nz@H)r   Ú__init__)ÚselfÚnameÚdefault© r!   úJ/var/www/html/gsl.tools/lib/python3.9/site-packages/scapy/layers/tuntap.pyr   ,   s    zNativeShortField.__init__N)Ú__name__Ú
__module__Ú__qualname__r   r!   r!   r!   r"   r   +   s   r   c                   @   s   e Zd ZegZdS )ÚTunPacketInfoN)r#   r$   r%   r   Z
aliastypesr!   r!   r!   r"   r&   0   s   r&   c                   @   s&   e Zd ZdZedddƒeddƒgZdS )ÚLinuxTunIfReqz¡
    Structure to request a specific device name for a tun/tap
    Linux  ``struct ifreq``.

    See linux/if.h (struct ifreq) and tuntap.txt for reference.
    Ú	ifrn_nameó    r   Ú
ifru_flagsr   N)r#   r$   r%   Ú__doc__r   r   Úfields_descr!   r!   r!   r"   r'   4   s   
ür'   c                	   @   sD   e Zd ZdZedddd„ dgdd„ ed	d
ƒD ƒ ƒeddeƒgZdS )ÚLinuxTunPacketInfozV
    Base for TUN packets.

    See linux/if_tun.h (struct tun_pi) for reference.
    Úflagsr   c                 C   s   t rdS dS )Nr   iðÿÿÿ)r   )Ú_r!   r!   r"   Ú<lambda>L   r)   zLinuxTunPacketInfo.<lambda>ZTUN_VNET_HDRc                 C   s   g | ]}d | ‘qS )z
reserved%dr!   )Ú.0Úxr!   r!   r"   Ú
<listcomp>N   r)   zLinuxTunPacketInfo.<listcomp>r   r   Útypei �  N)	r#   r$   r%   r+   r   Úranger   r
   r,   r!   r!   r!   r"   r-   C   s   ÿþ
ùr-   c                       sH   e Zd ZdZdZddedf‡ fdd„	Zdd„ Zdd	d
„Zdd„ Z	‡  Z
S )ÚTunTapInterfacea
  
    A socket to act as the host's peer of a tun / tap interface.

    This implements kernel interfaces for tun and tap devices.

    :param iface: The name of the interface to use, eg: 'tun0'
    :param mode_tun: If True, create as TUN interface (layer 3).
                     If False, creates a TAP interface (layer 2).
                     If not supplied, attempts to detect from the ``iface``
                     name.
    :type mode_tun: bool
    :param strip_packet_info: If True (default), strips any TunPacketInfo from
                              the packet. If False, leaves it in tact. Some
                              operating systems and tunnel types don't include
                              this sort of data.
    :type strip_packet_info: bool

    FreeBSD references:

    * tap(4): https://www.freebsd.org/cgi/man.cgi?query=tap&sektion=4
    * tun(4): https://www.freebsd.org/cgi/man.cgi?query=tun&sektion=4

    Linux references:

    * https://www.kernel.org/doc/Documentation/networking/tuntap.txt

    z/Act as the host's peer of a tun / tap interfaceNTc                    s˜  t t|d u rtjn|ƒƒ| _|| _| jd u rd| j d¡r@d| _n$| j d¡rTd| _ntd| jf ƒ‚t|ƒ| _d| _	| jr~t
nt| _trÜd}| jr t| _d| _	ntd	ƒ d| _t| jƒtkrÚtd
tf ƒ | jd t… | _nTt�r(| j d¡�s| j d¡�stdƒ‚d| j }| j�s0tdƒ d| _ntdƒ‚t|ddd�}t�rx| j�rRt}	nttB }	tt| j|	d�ƒ}
t|t|
ƒ d| _|| _tt| ƒ  |¡ d S )Ns   tunTs   tapFzECould not determine interface type for %r; set `mode_tun` explicitly.r   s   /dev/net/tuné   z0tap devices on Linux do not include packet info!z:Linux interface names are limited to %d bytes, truncating!z@Interface names must start with `tun` or `tap` on BSD and Darwins   /dev/z<tun/tap devices on BSD and Darwin never include packet info!z2TunTapInterface is not supported on this platform!zr+b)Ú	buffering)r(   r*   )!r   r   r   ÚifaceÚmode_tunÚ
startswithÚ
ValueErrorÚboolÚstrip_packet_infoÚmtu_overheadr   r   Úkernel_packet_classr	   r-   r   ÚlenÚLINUX_IFNAMSIZr   ÚNotImplementedErrorÚopenÚLINUX_IFF_TUNÚLINUX_IFF_TAPÚLINUX_IFF_NO_PIr   r'   r   ÚLINUX_TUNSETIFFÚclosedÚdefault_read_sizeÚsuperr6   r   )r   r9   r:   rJ   r>   ÚargsÚkwargsZdevnameÚsockr.   Ztsetiff©Ú	__class__r!   r"   r   r   sh    ÿ
ÿÿ
ÿ
ÿ
þzTunTapInterface.__init__c                 O   s   | S )z}Needed when using an instantiated TunTapInterface object for
        conf.L2listen, conf.L2socket or conf.L3socket.

        r!   )r   ÚargZkargr!   r!   r"   Ú__call__È   s    zTunTapInterface.__call__c                 C   s¢   |d u r| j }|| j7 }tjr2t | j ¡ |¡}n| j |¡}| j|t	 	¡ f}| jdkrš| j
rš|d |d d | j… ƒ d¡}||d | jd … |d fS |S d S )Nr   r   r)   r   )rJ   r?   ÚsixÚPY2ÚosÚreadZinsÚfilenor@   Útimer>   Zguess_payload_class)r   r2   ZdatÚrÚclsr!   r!   r"   Úrecv_rawÏ   s    
 zTunTapInterface.recv_rawc                 C   s¢   t |dƒrt ¡ |_| jtkr8t|ttfƒsPtƒ | }nt|| jƒsP|  ¡ | }t|ƒ}z| j	 
|¡}| j	 ¡  |W S  tjyœ   tjd| jjdd� Y n0 d S )NÚ	sent_timez%s sendT)Úexc_info)ÚhasattrrX   r\   r@   r   Ú
isinstancer   r   r   ZoutsÚwriteÚflushÚsocketÚerrorr   rP   r#   )r   r2   ÚsxrY   r!   r!   r"   Úsendæ   s     



ÿzTunTapInterface.send)N)r#   r$   r%   r+   Údescr   r   rR   r[   re   Ú__classcell__r!   r!   rO   r"   r6   T   s   ÿV
r6   )6r+   Ú
__future__r   rU   rb   rX   Zfcntlr   Zscapy.compatr   r   Zscapy.configr   Zscapy.constsr   r   r	   Z
scapy.datar
   r   Zscapy.errorr   r   Zscapy.fieldsr   r   r   r   Zscapy.interfacesr   Zscapy.layers.inetr   Zscapy.layers.inet6r   r   Zscapy.layers.l2r   Zscapy.packetr   Zscapy.supersocketr   Zscapy.libs.sixZlibsrS   rH   rE   rF   rG   rB   r   r&   r'   r-   r6   r!   r!   r!   r"   Ú<module>   s8   