
    ht                     :   d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	m
Z
mZmZmZmZmZ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 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(m)Z)m*Z* d Z+d Z,d Z-d@dZ.d Z/d Z0d Z1dAdZ2d Z3d Z4d Z5d@dZ6g dZ7d Z8d Z9d Z:de_;        de_<        d  Z=d! Z>d" Z?d# Z@d$ ZAd% ZBd& ZCd' ZDd( ZEd) ZFd* ZGd+ ZHd, ZId- ZJd. ZKd/ ZLd0 ZMd1 ZNd2 ZOd3 ZPd4 ZQd5 ZRd6 ZSd7 ZTd8 ZUd9 ZVd: ZWd; ZXd< ZYd= ZZ G d> d?e          Z[dS )Bz
Utility functions for IPv6.
    N)conf)Net)IPV6_ADDR_GLOBALIPV6_ADDR_LINKLOCALIPV6_ADDR_SITELOCALIPV6_ADDR_LOOPBACKIPV6_ADDR_UNICASTIPV6_ADDR_MULTICASTIPV6_ADDR_6TO4IPV6_ADDR_UNSPECIFIED)strxorstrorstrand)orbchb)	inet_pton	inet_ntop)RandMACRandBin)warningScapy_Exception)reduce
cmp_to_key)IteratorListOptionalTupleUnioncastc                    d }t          g           }t          |           st          |           rd |D             }nt          |           rd |D             }nt	          |           rd |D             }nt          |           r~t          |           rd ddt          j        ffD             }nt          |           rd |D             }nct          |           rd	 |D             }nGt          |           rd
 |D             }n+| dk    r|dk    rd |D             }n| dk    rd |D             }d |D             }|                    t          |                     |S )a  
    Given all addresses assigned to a specific interface ('laddr' parameter),
    this function returns the "candidate set" associated with 'addr/plen'.

    Basically, the function filters all interface addresses to keep only those
    that have the same scope as provided prefix.

    This is on this list of addresses that the source selection mechanism
    will then be performed to select the best source address associated
    with some specific destination that uses this prefix.
    c                     d}t          |           rd}d}t          |          rd}||z
  }|dk    s|dk    r|S t          |           sdS | S )Nr      )in6_isgladdrin6_isaddr6to4)xyx_globaly_globalress        P/mounts/lovelace/software/anaconda3/lib/python3.11/site-packages/scapy/utils6.py	cset_sortz1construct_source_candidate_set.<locals>.cset_sort:   sn    ?? 	H?? 	H!!88x1}}Ja   	2t    c              3   <   K   | ]}|d          t           k    |V  dS r"   Nr   .0r&   s     r+   	<genexpr>z1construct_source_candidate_set.<locals>.<genexpr>L   1      ==aAaD,<$<$<$<$<$<$<==r-   c              3   <   K   | ]}|d          t           k    |V  dS r/   r   r1   s     r+   r3   z1construct_source_candidate_set.<locals>.<genexpr>N   1      @@aAaD,?$?$?$?$?$?$?@@r-   c              3   <   K   | ]}|d          t           k    |V  dS r/   r   r1   s     r+   r3   z1construct_source_candidate_set.<locals>.<genexpr>P   r7   r-   c              3      K   | ]}|V  d S N r1   s     r+   r3   z1construct_source_candidate_set.<locals>.<genexpr>S   s"      AA!AAAAAAAr-   ::1   c              3   <   K   | ]}|d          t           k    |V  dS r/   r0   r1   s     r+   r3   z1construct_source_candidate_set.<locals>.<genexpr>U   s1      AA!!0@(@(@A(@(@(@(@AAr-   c              3   <   K   | ]}|d          t           k    |V  dS r/   r6   r1   s     r+   r3   z1construct_source_candidate_set.<locals>.<genexpr>W   1      DD!!0C(C(CA(C(C(C(CDDr-   c              3   <   K   | ]}|d          t           k    |V  dS r/   r9   r1   s     r+   r3   z1construct_source_candidate_set.<locals>.<genexpr>Y   rA   r-   ::r   c              3   <   K   | ]}|d          t           k    |V  dS r/   r0   r1   s     r+   r3   z1construct_source_candidate_set.<locals>.<genexpr>[   r4   r-   c              3   <   K   | ]}|d          t           k    |V  dS r/   )r   r1   s     r+   r3   z1construct_source_candidate_set.<locals>.<genexpr>]   s1      ??aAaD,>$>$>$>$>$>$>??r-   c                     g | ]
}|d          S )r   r<   r1   s     r+   
<listcomp>z2construct_source_candidate_set.<locals>.<listcomp>^   s       aQqT   r-   )key)iterr$   in6_isuladdrin6_islladdrin6_issladdrin6_ismaddrin6_ismnladdrr   loopback_namein6_ismgladdrin6_ismlladdrin6_ismsladdrsortr   )addrplenladdrr,   csetaddrss         r+   construct_source_candidate_setrY   )   s   "    88DD @\$// @==5===	d		 @@@5@@@	d		 @@@5@@@	T		 @ 	EAAD,> ?@AAADD4   	EAAuAAADD4   	EDDuDDDDD4   	EDDuDDDD	$!))==5===	??5???  4   E	JJ:i((J)))Lr-   c                 x     d  fd}|sdS |                     t          |          d           |d         S )a  
    This function implement a limited version of source address selection
    algorithm defined in section 5 of RFC 3484. The format is very different
    from that described in the document because it operates on a set
    of candidate source address for some specific route.
    c                     t           dt          dt          dt          di}t	          |           }|dk    rt          }t	          |          }|dk    rt          }||         }||         }||k    rdS ||k    rdS dS )zd
        Given two addresses, returns -1, 0 or 1 based on comparison of
        their scope
                 r"   r#   r   )r   r   r   r   in6_getscope)abscope_mappersasbs        r+   	scope_cmpz5get_source_addr_from_candidate_set.<locals>.scope_cmpm   s     )!+Q+Q*A/ !__88#B!__88#B""881771rr-   c                    | k    rdS |k    rdS  | |          }|dk    r |           dk    rdS dS |dk    r |          dk    rdS dS t          |           }t          |          }||k    rdS ||k    rdS dS )z
        The function implements a limited version of the rules from Source
        Address selection algorithm defined section of RFC 3484.
        r"   r#   r   )in6_get_common_plen)source_asource_btmptmp1tmp2dstre   s        r+   rfc3484_cmpz7get_source_addr_from_candidate_set.<locals>.rfc3484_cmp   s     s??1s??1 i(++"99y3''2--qrAXXy3''2--qr #8S11"8S11$;;1D[[2qr-    T)rH   reverser   )rS   r   )rm   candidate_setrn   re   s   `  @r+   "get_source_addr_from_candidate_setrr   d   sm      4' ' ' ' ' 'R  r:k22DAAAr-   c                    t          t          j        |           }t          t          j        |          }d}t	          |d                   dz  dk    r(t
          t          z  }|d d         dk    r
|t          z  }nt	          |d                   dk    rD|d         }|dk    rt          t          z  }n|d	k    rt          t          z  }nt          t          z  }nqt	          |d                   d
k    r-t          |d         d          dz  dk    rt
          t          z  }n+|dk    rt          }n|dk    rt          }nt          t
          z  }|S )Nr          r^          r]   2e   r>         r=   rC   )r   socketAF_INET6r   r   r	   r   r   r   r
   intr   r   )rT   naddrpaddraddrType	addrScopes        r+   in6_getAddrTyper      s:   fot,,Efou--EH 
U1X	$&&%(88!9  &H	U1X$		!H	+.AAHH#(+>>HH(+>>HHuQx==D  E!Hb(9(9C(?C'G'G%(;;	%%	$( %'88Or-   c                    t          |           dk    rt          d          d                    |                     d                    }t          |          dk    rt          d          t	          |dd         d          }||dk    s|d
k    sg d|dz           }|dz  }d|dz  |z  z  }||dd         z   dz   |dd         z   dz   |dd         z   dz   |dd         z   }|                                S )a<  
    Compute the interface ID in modified EUI-64 format associated
    to the Ethernet address provided as input.
    value taken by U/L bit in the interface identifier is basically
    the reversed value of that in given MAC address it can be forced
    to a specific value by using optional 'ulbit' parameter.
       zInvalid MACro   :r{   r   r^   r>   Nr"   )r"   r   r   %.02x   r\      zFF:FEr|   )len
ValueErrorjoinsplitr   upper)maculbitmfirstfirst_beui64s         r+   in6_mactoifaceidr      s    3xx2~~'''
		#A
1vv||'''!A#OOE}UaZZ5A::		%$,'	QJE%$,%/0Ga!fs"QqsV+g5!A#>Dq2wNE;;==r-   c                    	 t          t          j        d| z             dd         }n# t          $ r Y dS w xY w|dd         dk    rdS t	          j        d|dd	                   d
         }dg d|dz           z  }t	          j        d|dz  |z            }||d	d         z   }|dd         }d t          ||z             D             }d                    |          S )z
    Extract the mac address from provided iface ID. Iface ID is provided
    in printable format ("XXXX:XXFF:FEXX:XXXX", eventually compressed). None
    is returned on error.
    rC   r|   r>   Nr]      s   Br"   r   r^   )r"   -r   r   c                 2    g | ]}d t          |          z  S )r   )r   r1   s     r+   rG   z$in6_ifaceidtomac.<locals>.<listcomp>  s#    ;;;a3q66!;;;r-   r   )	r   r}   r~   	Exceptionstructunpackpacklistr   )	ifaceid_sifaceidr   r   ouiend	mac_bytess          r+   in6_ifaceidtomacr      s   FOTI-=>>qtD   tt qs|{""t M#wrr{++A.EEDL))EKut|u466E
'!A#,
C
!""+C;;4c	??;;;I88Is   %( 
66c                     t          t          j        d          }t          |t          t          j        |                     }t	          t          j        |          dd         }t          |          S )zW
    Extract the mac address from provided address. None is returned
    on error.
    z::ffff:ffff:ffff:ffffr^   N)r   r}   r~   in6_andr   r   )rT   maskr&   r   s       r+   in6_addrtomacr     sY     V_&=>>Di6677A++ABB/GG$$$r-   c                     t          |           }|t          j        sdS t          j                            |          }t	          |          dk    r|                    d          dk    rd}|S )ad  
    Extract the MAC address from a modified EUI-64 constructed IPv6
    address provided and use the IANA oui.txt file to get the vendor.
    The database used for the conversion is the one loaded by Scapy
    from a Wireshark installation if discovered in a well-known
    location. None is returned on error, "UNKNOWN" if the vendor is
    unknown.
    Nr   r   r   UNKNOWN)r   r   manufdb
_get_manufr   count)rT   r   r*   s      r+   in6_addrtovendorr     sc     

C
{$,{t
,
!
!#
&
&C
3xx2~~#))C..A--Jr-   r^   c                 f   |dvrdS 	 t          |           sdS t          t          j        |           }n # t          $ r t          d           Y dS w xY w|dd         }|d}nd}t          |t          t          f          r	 t          |t                    r't          |          dk    rt          |d          dz  }nKt          |t                    r/t          |          d	k    rt          j        d
|          d         }nt          nI# t          $ r t          d           Y dS w xY wt          |t                    r|}nt          d           dS t          j        d
|          }t          j        ddd|z  z            }d}d}	d|z   |	z   |z   |z   |z   }
t          t          j        |
          S )a  
    Generate a Link-Scoped Multicast Address as described in RFC 4489.
    Returned value is in printable notation.

    'addr' parameter specifies the link-local address to use for generating
    Link-scoped multicast address IID.

    By default, the function returns a ::/96 prefix (aka last 32 bits of
    returned address are null). If a group id is provided through 'grpid'
    parameter, last 32 bits of the address are set to that value (accepted
    formats : b'4Vx' or '12345678' or 0x12345678 or 305419896).

    By default, generated address scope is Link-Local (2). That value can
    be modified by passing a specific 'scope' value as an argument of the
    function. RFC 4489 only authorizes scope values <= 2. Enforcement
    is performed by the function (None will be returned).

    If no link-local address can be used to generate the Link-Scoped IPv6
    Multicast address, or if another error occurs, None is returned.
    )r   r"   r^   Nz8in6_getLinkScopedMcastPrefix(): Invalid address providedr|   s       r-   r>   l    r\   !Ir   z9in6_getLinkScopedMcastPrefix(): Invalid group id providedr   rw   0          )rK   r   r}   r~   r   r   
isinstancestrbytesr   r   r   r   r   r   r   )rT   grpidscopebaddriidb_grpidi_grpidflgscoperU   r*   r`   s              r+   in6_getLinkScopedMcastAddrr   1  s   , ItD!! 	4&/400   JKKKtt )C}%ec5\** 	eS)) %c%jjAoo!%nnz9GGu-- %#e**//$mD%88;GG$$      tt s## 	GG   4+dG,,{3U(: ;<<HD
C(S 4'#-7AV_a(((s&   4 4 AABD D('D(c                     	 t          t          j        |           }t          t          j        d|z   dz             S # t
          $ r Y dS w xY w)z
    Returns the /48 6to4 prefix associated with provided IPv4 address
    On error, None is returned. No check is performed on public/private
    status of the address
    rv   
             N)r   r}   AF_INETr   r~   r   rT   r   s     r+   in6_get6to4Prefixr   x  sV    &.$//+*=*LMMM   tts   9< 
A
	A
c                     	 t          t          j        |           }n# t          $ r Y dS w xY w|dd         dk    rdS t	          t          j        |dd                   S )z~
    Extract IPv4 address embedded in 6to4 address. Passed address must be
    a 6to4 address. None is returned on error.
    Nr^   rv   r   )r   r}   r~   r   r   r   r   s     r+   in6_6to4ExtractAddrr     sn    &/400   ttRaRyHtV^U1Q3Z000s    
++c            	         t          j                     } t          |           }t          | |z
  dz            }t          j        d||          }t	                      }t          t          j        dt          t          |                    z             dd         }ddl
}|                    ||z                                             dd         }t          t          j        d|z   d	z             S )
z
    Returns a pseudo-randomly generated Local Unique prefix. Function
    follows recommendation of Section 3.2.2 of RFC 4193 for prefix
    generation.
            z!IIrC   r|   Nr   r      r   )timer   r   r   r   r   r}   r~   r   r   hashlibsha1digestr   )todijbtodr   r   r   globalids           r+   in6_getLocalUniquePrefixr     s    $ )++CCAS1W  A;ua##D
))Cfot.>s3xx.H.H'HII!""MENNN||D5L))00222A26HV_g&8<&GHHHr-   c                 X   d}|t          t          d                    }n%t          t          j        d|z             dd         }t          t          j        d| z             dd         |z   }ddl}|                    |                                          }|dd         |dd         }}t          t          |d                   dz            |dd         z   }t          t          j        d|z             d	d         }t          t          j        d|z             d	d         }||fS )
ap  
    Implements the interface ID generation algorithm described in RFC 3041.
    The function takes the Modified EUI-64 interface identifier generated
    as described in RFC 4291 and an optional previous history value (the
    first element of the output of this function). If no previous interface
    identifier is provided, a random one is generated. The function returns
    a tuple containing the randomized interface identifier and the history
    value (for possible future use). Input and output values are provided in
    a "printable" format as depicted below.

    ex::
        >>> in6_getRandomizedIfaceId('20b:93ff:feeb:2d3')
        ('4c61:76ff:f46a:a5f3', 'd006:d540:db11:b092')
        >>> in6_getRandomizedIfaceId('20b:93ff:feeb:2d3',
                                     previous='d006:d540:db11:b092')
        ('fe97:46fe:9871:bd38', 'eeed:d79c:2e3f:62e')
    r-   Nr|   rC   r   r"   s      )r   r   r   r}   r~   r   md5r   r   r   r   )	r   previouss
b_previousr   s1s2bs1bs2s	            r+   in6_getRandomizedIfaceIdr     s   ( 	A71::&&

vx@@D
&/4'>221226CANNNAArrUAabbEB	SAZZ5!	"	"RV	+B
FO[2%5
6
6rss
;C
FO[2%5
6
6rss
;C:r-   )U01rx   3456789Ar   CDEFGHIJKLMNOPQRSTUVWXYZr`   ra   cdry   fghr   r   klr   nopqrr   tuvwr&   r'   z!#$%&()*+r   ;<=>?@^_`{|}~c                    t          |           dk    st          d d | D                       sdS d}| D ]$}t                              |          }d|z  |z   }%g }t	          d          D ]2}|                    t          j        d|d	z                       |d	z  }3|                                 t          t          j        d
                    |                    S )z
    Convert an IPv6 address in Compact Representation Notation
    (RFC 1924) to printable representation ;-)
    Returns None on error.
    r   c                 
    | o|S r;   r<   )r&   r'   s     r+   <lambda>zin6_ctop.<locals>.<lambda>  s
    !' r-   c                      g | ]}|t           v S r<   )_rfc1924mapr1   s     r+   rG   zin6_ctop.<locals>.<listcomp>  s    %E%E%E1a;&6%E%E%Er-   Nr   U   r\   r   r   r-   )r   r   r.  indexrangeappendr   r   rp   r   r}   r~   r   )rT   r   r  r   r*   s        r+   in6_ctopr3    s     4yyBf%9%9%E%E%E%E%EG Gt	A  a  FQJ
C1XX  

6;tQY//000%LKKMMMV_chhsmm444r-   c                    	 t          j        dt          t          j        |                     }n# t
          $ r Y dS w xY wd}ddz  dddg}t          d	          D ]}|||         ||         z  z  }g }|r*|                    t          |d
z                      |d
z  }|*|	                                 d
                    |          S )z
    Converts an IPv6 address in printable representation to RFC
    1924 Compact Representation ;-)
    Returns None on error.
    z!IIIINr   r^   `   l            r   r"   r\   r/  ro   )r   r   r   r}   r~   r   r1  r2  r.  rp   r   )rT   r  remr   r   r*   s         r+   in6_ptocr7    s    M'9V_d#C#CDD   tt
C	
Bua A1XX  qtad{
C
 

;sRx()))Ri   KKMMM773<<s   -0 
>>c                 R    t          t          j        |           }|dd         dk    S )zk
    Return True if provided address (in printable format) is a 6to4
    address (being in 2002::/16).
    Nr^   rv   r   r}   r~   )r&   bxs     r+   r%   r%     s(     
6?A	&	&Bbqb6Xr-   z2001::i  c                     t          t          j        |           dd         }t          t          j        t          j                  dd         }||k    S )z
    Return True if provided address is a Teredo, meaning it is under
    the /32 conf.teredoPrefix prefix value (by default, 2001::).
    Otherwise, False is returned. Address must be passed in printable
    format.
    r   r\   )r   r}   r~   r   teredoPrefix)r&   ourr<  s      r+   in6_isaddrTeredor>    sF     FOQ
'
'!
,CV_d.?@@1EL3r-   c                    t          t          j        |           }t          t          j        |dd                   }t          j        d|dd                   d         }t          j        dt          |dd         d                    d         }t          t          j        t          |dd         d	                    }||||fS )
a	  
    Extract information from a Teredo address. Return value is
    a 4-tuple made of IPv4 address of Teredo server, flag value (int),
    mapped address (non obfuscated) and mapped port (non obfuscated).
    No specific checks are performed on passed address.
    r\   r|   z!H
   r   r{   s   r>   s   )r   r}   r~   r   r   r   r   r   )r&   rT   serverflag
mappedport
mappedaddrs         r+   teredoAddrExtractInforE  &  s     V_a((Dv~tAaCy11F=tAbDz**1-DtVDBK%E%EFFqIJ6>6$r"u+{+K+KLLJ4Z//r-   c                     t          t          j        d          }t          t          t          j        |           |          }||k    S )z
    Return True if provided address has an interface identifier part
    created in modified EUI-64 format (meaning it matches ``*::*:*ff:fe*:*``).
    Otherwise, False is returned. Address must be passed in printable
    format.
    z::ff:fe00:0)r   r}   r~   r   )r&   r   r:  s      r+   in6_iseui64rG  6  s:     fo}55E	6?A..	6	6B;r-   c                     t          |           rLd}t          t          j        |           }t          t          j        |          }t	          ||          }||k    S t          d           dS )Nz::fdff:ffff:ffff:ff80z in6_isanycast(): TODO not EUI-64F)rG  r   r}   r~   r   r   )r&   r   packed_xpacked_sx_and_ss        r+   in6_isanycastrL  C  sg    1~~ #V_a00V_a00(H--("" 	2333ur-   c                 "    t          | |          S )z
    Provides a bit to bit OR of provided addresses. They must be
    passed in network format. Return value is also an IPv6 address
    in network format.
    )r   a1a2s     r+   in6_orrQ  V  s     R==r-   c                 "    t          | |          S )z
    Provides a bit to bit AND of provided addresses. They must be
    passed in network format. Return value is also an IPv6 address
    in network format.
    )r   rN  s     r+   r   r   `       "b>>r-   c                 "    t          | |          S )z
    Provides a bit to bit XOR of provided addresses. They must be
    passed in network format. Return value is also an IPv6 address
    in network format.
    )r   rN  s     r+   in6_xorrU  j  rS  r-   c                 $   | dk    s| dk     rt          d| z            g }t          dd          D ]A}|                    t          ddddt	          d|           z
  z  z
                       | dz  } Bd                    d	 |D                       S )
u   
    Return the mask (bitstring) associated with provided length
    value. For instance if function is called on 48, return value is
    b'ÿÿÿÿÿÿ          '.

       r   z<value provided to in6_cidr2mask outside [0, 128] domain (%d)r\   r   r^   ru   r-   c              3   @   K   | ]}t          j        d |          V  dS )r   N)r   r   r1   s     r+   r3   z in6_cidr2mask.<locals>.<genexpr>  s.      44QFKa((444444r-   )r   r1  r2  maxminr   )r   r  r   s      r+   in6_cidr2maskr[  t  s     	3ww!a%%\_``aaa
A1a[[  	QBRO 4455666	R8844!444444r-   c                    t          |           dk    rt          d          t          dd          D ]W}t          j        d| |dz  |dz   dz                     d         }t          d          D ]}|dd|z
  z  z  s|dz  |z   c c S Xd	S )
z#
    Opposite of in6_cidr2mask
    r>   zvalue must be 16 octets longr   r\   r   r"   ru      rW  )r   r   r1  r   r   )r   r   r   r   s       r+   in6_mask2cidrr^    s    
 1vv||<===1a[[ " "M$!a%Q!"3 455a8r 	" 	"Ab1f& "2vz!!!!!"	" 3r-   c                     t          | t          t          j        d                    }t	          t          t          j        d          |          }|S )z
    Return link-local solicited-node multicast address for given
    address. Passed address must be provided in network format.
    Returned value is also in network format.
    z	::ff:ffffzff02::1:ff00:0)r   r   r}   r~   rQ  )r`   r  s     r+   in6_getnsmar`    s@     	9V_k::;;Ay*:;;Q??AHr-   c                     t          j        d|           dd         }d}|d                    d |D                       z  }|S )z
    Return the multicast mac address associated with provided
    IPv6 address. Passed address must be in network format.
    16BNz33:33:r   c              3       K   | ]	}d |z  V  
dS )z%.2xNr<   r1   s     r+   r3   zin6_getnsmac.<locals>.<genexpr>  s&      ++1FQJ++++++r-   )r   r   r   )r`   bar   s      r+   in6_getnsmacrf    sM     
ua	 	 	%B
C388++++++++CJr-   c                     t          t          t          j        |           t	          d                    }t          |t          t          j        d                    }t          t          j        |          S )z[
    Return the anycast address associated with all home agents on a given
    subnet.
    @   z::fdff:ffff:ffff:fffe)r   r   r}   r~   r[  rQ  r   )prefixr  s     r+   	in6_getharj    sU     		&/622M"4E4EFFAq)FO-DEEFFAV_a(((r-   c                 f    t          t          j        t          t          j        |                     S )z
    Normalizes IPv6 addresses provided in printable format, returning the
    same address in printable format. (2001:0db8:0:0::1 -> 2001:db8::1)
    )r   r}   r~   r   r   s    r+   in6_ptoprm    s"     V_i&E&EFFFr-   c                     t          t          j        |           }t          |          }t          t          j        |          }|t	          ||          k    S )zK
    Returns True when 'addr' belongs to prefix/plen. False otherwise.
    )r   r}   r~   r[  r   )rT   ri  rU   tempprefzeros         r+   in6_isincludedrr    sH    
 V_d++DDV_f--D74&&&&r-   c                 b    t          dt          t          j        |                     }d}||k    S )z
    Return True if provided address is a link-local solicited node
    multicast address, i.e. belongs to ff02::1:ff00:0/104. False is
    returned otherwise.
    s      s               )r   r   r}   r~   )r   ro  temp2s      r+   in6_isllsnmaddrru    s/     -y#/N/NOODOE5=r-   c                 $    t          | dd          S )z
    Returns True if provided address in printable format belongs to
    2001:db8::/32 address space reserved for documentation (as defined
    in RFC 3849).
    z
2001:db8::ru   rr  rl  s    r+   in6_isdocaddrrx    s     #|R000r-   c                 $    t          | dd          S )z
    Returns True if provided address in printable format belongs to
    _allocated_ link-local unicast address space (fe80::/10)
    zfe80::r@  rw  rl  s    r+   rK   rK          #x,,,r-   c                 $    t          | dd          S )z
    Returns True if provided address in printable format belongs to
    _allocated_ site-local address space (fec0::/10). This prefix has
    been deprecated, address being now reserved by IANA. Function
    will remain for historic reasons.
    zfec0::r@  rw  rl  s    r+   rL   rL     s     #x,,,r-   c                 $    t          | dd          S )zt
    Returns True if provided address in printable format belongs to
    Unique local address space (fc00::/7).
    zfc00::   rw  rl  s    r+   rJ   rJ          #x+++r-   c                 $    t          | dd          S )z
    Returns True if provided address in printable format belongs to
    _allocated_ global address space (2000::/3). Please note that,
    Unique Local addresses (FC00::/7) are not part of global address
    space, and won't match.
    z2000::r]   rw  rl  s    r+   r$   r$     s     #x+++r-   c                 $    t          | dd          S )z{
    Returns True if provided address in printable format belongs to
    allocated Multicast address space (ff00::/8).
    zff00::r|   rw  rl  s    r+   rM   rM     r~  r-   c                 $    t          | dd          S )zq
    Returns True if address belongs to node-local multicast address
    space (ff01::/16) as defined in RFC
    zff01::r>   rw  rl  s    r+   rN   rN     rz  r-   c                 $    t          | dd          S )z\
    Returns True if address belongs to global multicast address
    space (ff0e::/16).
    zff0e::r>   rw  rl  s    r+   rP   rP   %  rz  r-   c                 $    t          | dd          S )z_
    Returns True if address belongs to link-local multicast address
    space (ff02::/16)
    zff02::r>   rw  rl  s    r+   rQ   rQ   .  rz  r-   c                 $    t          | dd          S )z
    Returns True if address belongs to site-local multicast address
    space (ff05::/16). Site local address space has been deprecated.
    Function remains for historic reasons.
    zff05::r>   rw  rl  s    r+   rR   rR   7  s     #x,,,r-   c                 n    t          t          j        d          t          t          j        |           k    S )z^
    Returns True if address is the link-local all-nodes multicast
    address (ff02::1).
    zff02::1r9  rl  s    r+   in6_isaddrllallnodesr  A  ,     foy11fos++, -r-   c                 n    t          t          j        d          t          t          j        |           k    S )z`
    Returns True if address is the link-local all-servers multicast
    address (ff02::2).
    zff02::2r9  rl  s    r+   in6_isaddrllallserversr  K  r  r-   c                    t          |           st          |           rt          }nt          |           rt          }nt          |           rt          }n~t          |           r_t          |           rt          }nXt          |           rt          }nAt          |           rt          }n*t          |           rt          }nd}n| dk    rt          }nd}|S )z+
    Returns the scope of the address.
    r#   r=   )r$   rJ   r   rK   r   rL   r   rM   rP   rQ   rR   rN   r   )rT   r   s     r+   r_   r_   U  s    
 D \$//  	d		 #	d		 #	T		  		$EE4   	'EE4   	'EE4   	&EEEE	"Lr-   c                    d }t          t          j        |           }t          t          j        |          }t          d          D ]D} |t	          ||                   t	          ||                             }|dk    r
d|z  |z   c S EdS )z@
    Return common prefix length of IPv6 addresses a and b.
    c                 T    t          d          D ]}d|z	  }| |z  ||z  k    r|c S dS )Nr|   rW  )r1  )byte1byte2r   cur_masks       r+   matching_bitsz*in6_get_common_plen.<locals>.matching_bitsw  sH    q 	 	AqyH eh&677 8qr-   r>   r|   rW  )r   r}   r~   r1  r   )r`   ra   r  tmpAtmpBr   mbitss          r+   rg   rg   r  s    
   V_a((DV_a((D2YY ! !c$q'llCQLL99A::q55=    3r-   c                 ^    	 t          t          j        |            dS # t          $ r Y dS w xY w)zPReturn True if 'address' is a valid IPv6 address string, False
       otherwise.TF)r   r}   r~   r   )addresss    r+   in6_isvalidr    s@    
&/7+++t   uus    
,,c                   T    e Zd ZdZd Zej        ZdZe	d             Z
ed             ZdS )Net6z6Network object from an IP address or hostname and maskrW  c           	          t          j        dt          t          j        |                     |                              \  }}t          t          |dz  |z             S )N!QQrh  )r   r   r   r}   r~   	name2addrr   r   )clsrT   val1val2s       r+   ip2intzNet6.ip2int  sQ     ]9V_cmmD.A.ABB
 

d C$"*,---r-   c           	      j    t          t          j        t          j        d| dz	  | dz                      S )Nr  rh  l    )r   r}   r~   r   r   )vals    r+   int2ipzNet6.int2ip  s6     OKsby#0B*BCC
 
 	
r-   N)__name__
__module____qualname____doc__namer}   r~   familymax_maskclassmethodr  staticmethodr  r<   r-   r+   r  r    s_        @@D_FH. . [. 
 
 \
 
 
r-   r  r;   )Nr^   )\r  r}   r   r   scapy.configr   scapy.base_classesr   
scapy.datar   r   r   r   r	   r
   r   r   scapy.utilsr   r   r   scapy.compatr   r   scapy.pton_ntopr   r   scapy.volatiler   r   scapy.errorr   r   	functoolsr   r   typingr   r   r   r   r   r   rY   rr   r   r   r   r   r   r   r   r   r   r   r.  r3  r7  r%   r<  teredoServerPortr>  rE  rG  rL  rQ  r   rU  r[  r^  r`  rf  rj  rm  rr  ru  rx  rK   rL   rJ   r$   rM   rN   rP   rQ   rR   r  r  r_   rg   r  r  r<   r-   r+   <module>r     s             " " " " " "? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?         
 " ! ! ! ! ! ! ! 0 0 0 0 0 0 0 0 + + + + + + + + 0 0 0 0 0 0 0 0 ( ( ( ( ( ( ( (               8 8 8vR R Rp  B   .  8	% 	% 	%  *D) D) D) D)N  1 1 1I I I<       FA A A5 5 5,  .     
 
 
0 0 0 
 
 
  &      5 5 5&   
 
 

 
 
) ) )G G G' ' '	 	 	1 1 1- - -- - -, , ,, , ,, , ,- - -- - -- - -- - -- - -- - -  :  ,	 	 	
 
 
 
 
3 
 
 
 
 
r-   