
    h2                         d Z ddlZddlZddlZddlmZ ddlZddlZddlm	Z	 ddl
mZmZmZmZ ddlmZmZ ddlmZ ddlmZmZ dd	lmZmZmZ dd
lmZmZmZmZmZ d Z 	 ddZ!d Z"d Z#d Z$d Z%d Z&dS )zL
Common customizations for all Unix-like operating systems other than Linux
    N)ioctl)conf)FREEBSDNETBSDOPENBSDSOLARIS)log_runtimewarning)	inet_pton)in6_getscopeconstruct_source_candidate_set)in6_isvalidin6_ismlladdrin6_ismnladdr)ListOptionalTupleUnioncastc           
          t          j                     }	 t          ||t          j        d|                     d                              |                                 S # |                                 w xY w)zEase SIOCGIF* ioctl calls16s16xutf8)socketr   structpackencodeclose)iffcmdscks      S/mounts/lovelace/software/anaconda3/lib/python3.11/site-packages/scapy/arch/unix.pyget_ifr"   "   s\     -//CS#v{8SZZ5G5GHHII				s   6A   A6c           
      x    |ddl m} |}t          dt          j        dt          | |                              S )zGet the raw MAC address of a local interface.

    This function uses SIOCGIFHWADDR calls, therefore only works
    on some distros.

    :param iff: the network interface name as a string
    :returns: the corresponding raw MAC address
    Nr   )SIOCGIFHWADDRzTuple[int, bytes]16xH6s8x)
scapy.archr$   r   r   unpackr"   )r   siocgifhwaddrr$   s      r!   get_if_raw_hwaddrr)   -   sU     ,,,,,,%3&&	
 	
      c                 H    t          j        dt          j        j        z            5 }|                                                                                    d          }ddd           n# 1 swxY w Y    fd|D             }t          |          dk    r|d         S dS )z
    We attempt to guess the name of interfaces that are truncated from the
    output of ifconfig -l.
    If there is only one possible candidate matching the interface name then we
    return it.
    If there are none or more, then we return None.
    %s -l Nc                 >    g | ]}|                               |S  )
startswith).0ifacenetifs     r!   
<listcomp>z%_guess_iface_name.<locals>.<listcomp>U   s,    DDDE,<,<U,C,CDuDDDr*      r   )	ospopenr   progifconfigreadlinestripsplitlen)r3   fdescifacesmatchess   `   r!   _guess_iface_namerA   J   s     
'DI..	/	/ 55!!''))//445 5 5 5 5 5 5 5 5 5 5 5 5 5 5DDDD&DDDG
7||qqz4s   :A..A25A2c            	         t           rt          j        d          } n0t          rt          j        d          } nt          j        d          } d}d}d}d}d}g }g }|                                 D ]}|s n|                                                                }|                    d          dk    rH|s0|                    d          dk    rd}d	|v }d
|v }d|v }d|v pd|v }z|s n<|                                }	t           r|	dd         \  }
}}}|	d|z   |z            }n<|	dd         \  }
}}t          o|	d         dk    }||z   |z   |z   }||z  }|	d|z            }|                    d          dk    r|
dk    rd}d}nt           r?t          j                            |
          }t          j                            |          }nd|
v rE|
                    d          \  }
}t          j                            t          |                    }n8t          j                            |
                    d          dz   dz            }|
dd|
                    d          z
  z  z  }
t          j                            |
          }d}d|vrd}|xddlm} 	  ||          }|dk    r4t%          |          }| ||          }|}nt'          j        d|           |                    ||||||f           # t,          $ r  w xY w|                    |||f           |                                  |D ]\  }}}t          j                            |          }d\  }}}|dd         D ]\  }}}}}}||z  |k    r||k    r|}|}|}d}|r|r|                    ||||||f           vt1          d|           |S )z_Return a list of IPv4 routes than can be used by Scapy.

    This function parses netstat.
    znetstat -rvn -f inetznetstat -rnW -f inetznetstat -rn -f inetr   Fz----destinationr5   mtupriorefusenhopN         llcdefault/.   z.0gz0.0.0.0)get_if_addrz*Could not guess partial interface name: %s)r   NNz1Did not find output interface to reach gateway %s)r   r6   r7   r   	readlinesr;   lowerfindr<   r   scapyutilsatolitomintcountr&   rS   rA   r	   infoappendOSErrorr   r
   ) fokmtu_presentprio_presentrefs_presentuse_presentroutes
pending_iflinertdest_netmask_gwr3   flglockedoffsetdestnetmaskmetricrS   ifaddrguessed_netifgw_l
max_rtmaskgw_if
gw_if_addrrtdstrtmask_rtifrtaddrs                                    r!   read_routesr~   [   s     ,H+,,	 ,H+,,H*++	
BKLLKFJ C3 C3 	Ezz||!!##99V!! 	yy''1,,#tm%~$}#tm=v~ 	EZZ\\ 	#)+BQB&E8RQ_|34CCVNE2s-A#F </,>GFk!Fq6zNE88D>>QiDGG 
	+;##E**Dk&&x00GGe||"'++c"2"2x+**3x==99+**EKK,<,<q,@A+EFFTQS!1!1122E;##E**Dc>>B......$U++Y&& %6e$<$<M$0!,]!;!; -#(H!   tWb%HIIII    tWb12222GGIII
 ( M Mgr{##(5%
E:17 	( 	(-E61dFAf}%%Z''!'J E!'J 	MZ 	MMM4"eZHIIIIGLLLLMs   A LLc                 B   	 t          j        t          j        j        d|           }n&# t
          $ r t          j        d           g cY S w xY wg }|D ]}d|v r/|                                	                    dd          d         }n6d|v r|	                    dd          d         }	 t          t          j        |           n# t          j        t          f$ r Y w xY wt          |          }|                    ||| f           |                                 |S )	zN
    Returns a list of IPv6 addresses configured on the interface ifname.
    r-   Failed to execute ifconfig.inet6N   r5   %r   )r6   r7   r   r8   r9   r_   r	   r
   rstripr<   r   r   AF_INET6error
ValueErrorr   r^   r   )ifnamer`   retrh   addrscopes         r!   _in6_getifaddrr      sE   H	 2 2 2FF;<<   9:::			
 C * *d??;;==&&tQ//2DD$;;::c1%%a(D	fot,,,,j) 	 	 	H	 T""

D%())))GGIIIJs!   (+  AA,CC C c                     t           st          rt          rd} nd} 	 t          j        | t          j        j        z            }n&# t          $ r t          j	        d           g cY S w xY wg }|D ]H}d|v rB|
                                d                             d          }|                    |           In	 t          j        dt          j        j        z            }n&# t          $ r t          j	        d           g cY S w xY w|                                                                
                                }g }|D ]}|t          |          z  }|                                 |S )z
    Returns a list of 3-tuples of the form (addr, scope, iface) where
    'addr' is the address of scope 'scope' associated to the interface
    'iface'.

    This is the list of all addresses of all interfaces available on
    the system.
    z%s -a6z%sr   flagsr   :r,   )r   r   r6   r7   r   r8   r9   r_   r	   r
   r<   r   r^   r:   r   r   )r   r`   splitted_linerh   r2   r   is          r!   in6_getifaddrr      s     6' 6 	CCC	ty1122AA 	 	 	 =>>>III	
  	, 	,D$

Q..s33$$U+++	,	49#5566AA 	 	 	 =>>>III	
 

++--3355
C ! !~a   GGIIIJs#   &A  A&%A&8&C  DDc            	      h   t          j        d          } t                      }|s|                                  g S d}d}d}g }|                                 D ]D}|sd|dd         k    r
d}d|v }d|v }|                                }t          st          rBd	|z   |z   }t          |          |k     rt          d
           k|dd         \  }	}
}||         n2t          |          dk     rt          d
           |dd         \  }	}
}d}d|vrd|v rd|v rd|
v rd}
d}d|	v r4|	                    d          \  }	dv r                    d          \  }d|
v r|
                    d          \  }
t          |
          sd}
|	dk    rd\  }	}nd|	v r|	                    d          \  }	}dv r                    d          \  }t          |	          st          d           	 t          |          }n # t          $ r t          d           Y w xY wt          |	          st          |	          rt          j        v rdg}d}
nfd|D             }t#          |	||          }t          |          r|                    |	||
||f           F|                                  |S )z_Return a list of IPv6 routes than can be used by Scapy.

    This function parses netstat.
    znetstat -rn -f inet6FDestinationN   TMtuPrio   z#Not enough columns in route entry !rJ   rI   r5   URmlink::   r   rO   rN   )r   r   z1Invalid destination IPv6 address in route entry !z+Invalid IPv6 prefix length in route entry !z::1c              3   4   K   | ]}|d          k    |V  dS )r   Nr/   )r1   xdevs     r!   	<genexpr>zread_routes6.<locals>.<genexpr>  s+      ::aadckkkkkk::r*   )r6   r7   r   r   rT   r<   r   r   r=   r
   r   r[   	Exceptionr   r   r   loopback_namer   r^   )
fd_netstatlifaddr
got_headerrb   rc   rf   rh   r   indexrC   next_hopr   rr   destination_plencsetdevaddrsr   s                   @r!   read_routes6r   "  s    011J ooG 	 JKL F$$&& ]X ]X  	SbS	))!
#tm%~ 

 	Bf 	BOl2E=!!E))=>>>+8!+<(K5&CC =!!A%%=>>>0=bqb0A-K5#  e%<<%<< XH  +*0055Kczz(+		#%%(??$NN3//MHc 8$$ 	H )##,3)K))K,7,=,=c,B,B)K)#::$'IIcNN!C! ;'' 	GHHH	"#344 	 	 	ABBBH	 %% 	{)C)C 	$$7DHH ;:::7:::H1+?OQYZZDt99 	XMM;(8(CvVWWWMs   /G??HH)N)'__doc__r6   r   r   fcntlr   scapy.configrW   scapy.utilsr   scapy.constsr   r   r   r   scapy.errorr	   r
   scapy.pton_ntopr   scapy.utils6r   r   r   r   r   typingr   r   r   r   r   r"   r)   rA   r~   r   r   r   r/   r*   r!   <module>r      s    
			                       : : : : : : : : : : : : , , , , , , , , % % % % % % E E E E E E E E B B B B B B B B B B                %)   :  "l l lf" " "J, , ,^w w w w wr*   