U
    <æ{fŒ  ã                   @   sÌ   d Z ddlZddlmZmZmZ ddlZddlZej	Z	ej
Z
eeedœdd„Zeeedœdd	„Zeed
œdd„Zeed
œdd„Zeed
œdd„Zdeeef ee edœdd„Zdd„ Zeed
œdd„ZdS )z*Generic Internet address helper functions.é    N)ÚAnyÚOptionalÚTuple)ÚfamilyÚtextÚreturnc                 C   s2   | t krtj |¡S | tkr*tj |d¡S t‚dS )a  Convert the textual form of a network address into its binary form.

    *family* is an ``int``, the address family.

    *text* is a ``str``, the textual address.

    Raises ``NotImplementedError`` if the address family specified is not
    implemented.

    Returns a ``bytes``.
    TN)ÚAF_INETÚdnsÚipv4Ú	inet_atonÚAF_INET6Úipv6ÚNotImplementedError)r   r   © r   úG/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/dns/inet.pyÚ	inet_pton!   s
    r   )r   Úaddressr   c                 C   s0   | t krtj |¡S | tkr(tj |¡S t‚dS )a0  Convert the binary form of a network address into its textual form.

    *family* is an ``int``, the address family.

    *address* is a ``bytes``, the network address in binary form.

    Raises ``NotImplementedError`` if the address family specified is not
    implemented.

    Returns a ``str``.
    N)r   r	   r
   Ú	inet_ntoar   r   r   )r   r   r   r   r   Ú	inet_ntop6   s
    r   )r   r   c                 C   s^   zt j | ¡ tW S  tk
rX   zt j | d¡ tW  Y S  tk
rR   t‚Y nX Y nX dS )zäDetermine the address family of a textual-form network address.

    *text*, a ``str``, the textual address.

    Raises ``ValueError`` if the address family cannot be determined
    from the input.

    Returns an ``int``.
    TN)r	   r
   r   r   Ú	Exceptionr   r   Ú
ValueError©r   r   r   r   Úaf_for_addressK   s    
r   c                 C   sv   z"t j | ¡d }|dko |dkW S  tk
rp   z t j | d¡d }|dkW  Y S  tk
rj   t‚Y nX Y nX dS )zÝIs the textual-form network address a multicast address?

    *text*, a ``str``, the textual address.

    Raises ``ValueError`` if the address family cannot be determined
    from the input.

    Returns a ``bool``.
    r   éà   éï   Téÿ   N)r	   r
   r   r   r   r   )r   Úfirstr   r   r   Úis_multicasta   s    r   c                 C   s\   zt j | ¡ W dS  tk
rV   zt j | d¡ W Y dS  tk
rP   Y Y dS X Y nX dS )z{Is the specified string an IPv4 or IPv6 address?

    *text*, a ``str``, the textual address.

    Returns a ``bool``.
    TFN)r	   r
   r   r   r   r   r   r   r   Ú
is_addressw   s    r   )Ú
high_tupleÚafr   c           
      C   sä   | \}}|dkrt |ƒ}|tkr(||fS |tkrÒ| d¡}|dk rN||ddfS |d|… }||d d… }| ¡ r‚||dt|ƒfS z||dt |¡fW S  tk
rÎ   tj	}tj
|||d^^ }}	}|	 Y S X ntd|› ƒ‚dS )aX  Given a "high-level" address tuple, i.e.
    an (address, port) return the appropriate "low-level" address tuple
    suitable for use in socket calls.

    If an *af* other than ``None`` is provided, it is assumed the
    address in the high-level tuple is valid and has that af.  If af
    is ``None``, then af_for_address will be called.
    Nú%r   é   )Úflagsúunknown address family )r   r   r   ÚfindÚisdigitÚintÚsocketÚif_nametoindexÚAttributeErrorÚAI_NUMERICHOSTÚgetaddrinfor   )
r   r    r   ÚportÚiZaddrpartÚscopeZai_flagsÚ_Útupr   r   r   Úlow_level_address_tupleŠ   s(    
r2   c                 C   s.   | t jkrdS | t jkrdS td| › ƒ‚dS )z:Return the 'any' address for the specified address family.z0.0.0.0z::r$   N)r(   r   r   r   )r    r   r   r   Ú
any_for_af®   s
    

r3   c                 C   sT   zt j | ¡W S  tk
rN   zt j | ¡W  Y S  tk
rH   t‚Y nX Y nX dS )a   Verify that *address* is a valid text form IPv4 or IPv6 address and return its
    canonical text form.  IPv6 addresses with scopes are rejected.

    *text*, a ``str``, the address in textual form.

    Raises ``ValueError`` if the text is not valid.
    N)r	   r   Úcanonicalizer   r
   r   r   r   r   r   r4   ·   s    r4   )N)Ú__doc__r(   Útypingr   r   r   Zdns.ipv4r	   Zdns.ipv6r   r   r'   ÚstrÚbytesr   r   r   Úboolr   r   r2   r3   r4   r   r   r   r   Ú<module>   s&    ÿ
 þ$	