a
    kÄ#d›  ã                   @   s    d 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 ddlmZmZmZmZ ddlmZmZmZmZmZmZ G d	d
„ d
ƒZeƒ e_ej ¡  dS )z-
Routing and handling of network interfaces.
é    )Úabsolute_import)Ú	plain_str)Úconf)ÚScapy_ExceptionÚwarning)Úresolve_iface)ÚatolÚltoaÚitomÚpretty_list)ÚAnyÚDictÚListÚOptionalÚTupleÚUnionc                   @   sv   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zddd„Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zd	ejfdd„Zdd„ Zd	S )ÚRoutec                 C   s   g | _ |  ¡  d S ©N)ÚroutesÚresync©Úself© r   úB/var/www/html/gsl.tools/lib/python3.9/site-packages/scapy/route.pyÚ__init__"   s    zRoute.__init__c                 C   s
   i | _ d S r   )Úcacher   r   r   r   Úinvalidate_cache'   s    zRoute.invalidate_cachec                 C   s    ddl m} |  ¡  |ƒ | _d S )Nr   )Úread_routes)Ú
scapy.archr   r   r   )r   r   r   r   r   r   +   s    zRoute.resyncc           	   
   C   sT   g }| j D ]<\}}}}}}t|ƒj}| t|ƒt|ƒ|||t|ƒf¡ q
t|dgƒS )N)ZNetworkZNetmaskZGatewayZIfacez	Output IPZMetric)r   r   ÚdescriptionÚappendr	   Ústrr   )	r   ZrtlstÚnetÚmskÚgwÚifaceÚaddrÚmetricZif_reprr   r   r   Ú__repr__1   s    

ûÿzRoute.__repr__Né   c                 C   sœ   ddl m} |d ur |d }}n(|d ur@| d¡\}}	t|	ƒ}ntdƒ‚|d u rTd}|d u r||rf|}
n|}
|  |
¡\}}}n||ƒ}t|ƒt|ƒ||||fS )Nr   )Úget_if_addré    ú/zDmake_route: Incorrect parameters. You should specify a host or a netú0.0.0.0)r   r*   ÚsplitÚintr   Úrouter   r
   )r   Úhostr"   r$   Údevr'   r*   Zthenetr#   Zmsk_bZnhopZifaddrÚ_r   r   r   Ú
make_route@   s     
zRoute.make_routec                 O   s$   |   ¡  | j | j|i |¤Ž¡ dS )z;Ex:
        add(net="192.168.1.0/24",gw="1.2.3.4")
        N)r   r   r    r4   )r   ÚargsÚkargsr   r   r   Úadd\   s    z	Route.addc                 O   sP   |   ¡  | j|i |¤Ž}z| j |¡}| j|= W n tyJ   tdƒ‚Y n0 dS )zdelt(host|net, gw|dev)zNo matching route found!N)r   r4   r   ÚindexÚ
ValueError)r   r5   r6   r0   Úir   r   r   Údeltd   s    z
Route.deltc                 C   s²   |   ¡  | d¡dg d d… \}}tt|ƒƒ}t|ƒ}||@ }t| jƒD ]X\}}	|	\}
}}}}}||krlqJ|dkrŒ||||||f| j|< qJ|
|||||f| j|< qJtj 	¡  d S )Nr,   Ú32é   r-   )
r   r.   r
   r/   r   Ú	enumerater   r   ZnetcacheÚflush)r   Úiffr&   Úthe_addrÚ	the_msk_bÚthe_mskÚthe_rawaddrÚthe_netr:   r0   r"   r#   r$   r%   r'   r   r   r   Úifchangeo   s    zRoute.ifchangec                 C   s:   |   ¡  g }| jD ]}||d kr$q| |¡ q|| _d S )Né   )r   r   r    )r   r@   Z
new_routesÚrtr   r   r   Úifdel�   s    
zRoute.ifdelc                 C   s\   |   ¡  | d¡dg d d… \}}tt|ƒƒ}t|ƒ}||@ }| j ||d||df¡ d S )Nr,   r<   r=   r-   r)   )r   r.   r
   r/   r   r   r    )r   r@   r&   rA   rB   rC   rD   rE   r   r   r   Úifadd‹   s    zRoute.ifaddc                 C   sn  |pd}t |tƒr:zt|ƒ}W n ty8   tdƒ‚Y n0 || jv rN| j| S | d¡d  dd¡}| d¡}|dk rxq¬||d… d	  d	¡}|d|… ||| d…  }qdt	|ƒ}g }| j
D ]d\}}}	}
}}|sÔq¾t	|ƒ}||krü| d
dtj|dff¡ ||@ ||@ kr¾| |||
||	ff¡ q¾|�sD|�r8tdƒ tjddfS |jdd„ d� |d d }|| j|< |S )aM  Returns the IPv4 routes to a host.
        parameters:
         - dst: the IPv4 of the destination host

        returns: (iface, output_ip, gateway_ip)
         - iface: the interface used to connect to the host
         - output_ip: the outgoing IP that will be used
         - gateway_ip: the gateway IP that will be used
        r-   z Unknown IP address input (bytes)r,   r   Ú*Ú0ú-NÚ.ì   ÿÿ r)   z"No route found (no default route?)c                 S   s   | d  | d fS )Nr   r)   r   )Úxr   r   r   Ú<lambda>Ã   ó    zRoute.route.<locals>.<lambda>)Úkeyr=   )Ú
isinstanceÚbytesr   ÚUnicodeDecodeErrorÚ	TypeErrorr   r.   ÚreplaceÚfindr   r   r    r   Zloopback_namer   Úsort)r   ÚdstÚverboseZ_dstÚidxÚmZatol_dstÚpathsÚdr$   r:   ÚaÚmeZaaÚretr   r   r   r0   ”   sD    



ÿ
zRoute.routec           
      C   sj   g }| j D ]L\}}}}}}|dkr&q
n
|dkr0q
||kr:q
|| d@ B }	| t|	ƒ¡ q
|sftd|ƒ |S )Nr   rO   z(No broadcast address found for iface %s
)r   r    r	   r   )
r   r@   Z
bcast_listr"   r#   r$   r%   r&   r'   Zbcastr   r   r   Úget_if_bcastÉ   s    
zRoute.get_if_bcast)NNNNr)   )Ú__name__Ú
__module__Ú__qualname__r   r   r   r(   r4   r7   r;   rF   rI   rJ   r   Úverbr0   rd   r   r   r   r   r   !   s"        û

	5r   N)Ú__doc__Ú
__future__r   Zscapy.compatr   Zscapy.configr   Zscapy.errorr   r   Zscapy.interfacesr   Zscapy.utilsr   r	   r
   r   r   r   r   r   r   r   r   r0   ZifacesÚreloadr   r   r   r   Ú<module>   s     :