
    hV                         d Z ddlZddlZddlZddlmZmZmZmZ ddl	m
Z
  ej        d          Z ej        d          Zd Zej        ej        ej        eiZd Zd	 Zej        ej        ej        eiZd
 ZdS )z
Convert IPv6 addresses between textual representation and binary.

These functions are missing when python is compiled
without IPv6 support, on Windows for instance.
    N)	plain_str	hex_bytesbytes_encode	bytes_hex)Unionz(?::|^)(0(?::0)+)(?::|$)z-illegal IP address string passed to inet_ptonc                 r   d}d}t          |           } | dk    rdS |                     d          r
| dd         } |                     d          r
| dd         } |                     d          }t	          |          }t          |          D ]\  }}|s|t	          |          }t          |dz   |k    rWd|v rS|                    d          d	k    rt          	 |t          j	        |          z  }f# t          j
        $ r t          w xY w	 |t          |                    d
d                    z  }# t          j        t          f$ r t          w xY w|Et	          |          dk    rt          |d|         ddt	          |          z
  z  z   ||d         z   }t	          |          dk    rt          |S )zuConvert an IPv6 address from text representation into binary form,
used when socket.inet_pton is not available.

    N    z::s                      :.      0       )r   
startswithendswithsplitlen	enumerate_INET6_PTON_EXCcountsocket	inet_atonerrorr   rjustbinasciiError	TypeError)addr	joker_posresultpartsnpartsiparts          S/mounts/lovelace/software/anaconda3/lib/python3.11/site-packages/scapy/pton_ntop.py_inet6_ptonr)      s    IFT??Dt|||t ABBx}}T CRCyJJsOOEZZFU## & &4 	& KK		 &%Uf__zz#!## &%&&*4000< & & &%%&&)DJJq#$6$6777NI. & & &%%& v;;"!!)$w"s6{{2B'CC$%
6{{bMs   C33D
&D55Ec                    t          |          }	 t          j        st          t          j        | |          S # t          $ r; 	 t          |          |          cY S # t          $ r t          j        d          w xY ww xY w)z@Convert an IP address from text representation into binary form.z(Address family not supported by protocol)r   r   has_ipv6AttributeError	inet_pton
_INET_PTONKeyErrorr   afr!   s     r(   r-   r-   S   s     T??DK 	!  D))) K K K	Kb>$''''' 	K 	K 	K,IJJJ	KKs!   '9 
A>AA>A::A>c                     t                     dk    rt          d          d                     fdt          ddd          D                       }	 t	          t
                              |          d           }d	                    |d
|                                         ||	                                d
                   S # t          $ r |cY S w xY w)zuConvert an IPv6 address from binary form into text representation,
used when socket.inet_pton is not available.

    r   z*invalid length of packed IP address stringr   c           	   3      K   | ]@}t          t          ||d z                                                    d          pdV  AdS )   r   N)r   r   lstrip).0idxr!   s     r(   	<genexpr>z_inet6_ntop.<locals>.<genexpr>o   sg       3 3 !4C!G+<!=!=>>EEcJJQc 3 3 3 3 3 3r	   r   r4   c                 X    |                      d          |                     d          z
  S )Nr
   )endstart)ms    r(   <lambda>z_inet6_ntop.<locals>.<lambda>y   s    !%%((QWWQZZ"7 r	   )keyz{}::{}N)
r   
ValueErrorjoinrangemax
_IP6_ZEROSfinditerformatr;   r:   )r!   addressmatchs   `  r(   _inet6_ntoprH   d   s     4yyBEFFF hh 3 3 3 3"'2q//3 3 3 3 3G
 J''00779 9 9w~~6		8MNNN   s   A3C	 	CCc                     t          |          }	 t          j        st          t          j        | |          S # t          $ r9 	 t          |          |          cY S # t          $ r t          d| z            w xY ww xY w)z@Convert an IP address from binary form into text representation.zunknown address family %d)r   r   r+   r,   	inet_ntop
_INET_NTOPr/   r?   r0   s     r(   rJ   rJ      s     D? 	!  D))) ? ? ?	?b>$''''' 	? 	? 	?82=>>>	??s!   '9 
A<AA<A88A<)__doc__r   rer   scapy.compatr   r   r   r   typingr   compilerC   r   r   r)   AF_INETr   AF_INET6r.   r-   rH   	inet_ntoarK   rJ    r	   r(   <module>rU      s     				  F F F F F F F F F F F F      RZ233
&,NOO1 1 1j NF$
O[
K K K"  8 NF$
O[
? ? ? ? ?r	   