a
    sd_                     @   s^   d Z ddlZddlZddlZejZejZdd Zdd Zdd Z	d	d
 Z
dd ZdddZdS )z*Generic Internet address helper functions.    Nc                 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)familytext r   */usr/lib/python3/dist-packages/dns/inet.py	inet_pton!   s
    r   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   Z	inet_ntoar   r   r   )r	   addressr   r   r   	inet_ntop6   s
    r   c                 C   sZ   zt j|  tW S  tyT   zt j| d tW  Y S  tyN   tY n0 Y n0 dS )zDetermine 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   sr   z"t j| d }|dko |dkW S  tyl   z t j| dd }|dkW  Y S  tyf   tY n0 Y n0 dS )zIs 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   sX   zt j|  W dS  tyR   zt j| d W Y dS  tyL   Y Y dS 0 Y n0 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   c           
      C   s   | \}}|du 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y   tj	}tj
|||d^^ }}	}|	 Y S 0 ntd| dS )aY  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      )flagszunknown address family )r   r   r   findisdigitintsocketZif_nametoindexAttributeErrorZAI_NUMERICHOSTZgetaddrinfor   )
Z
high_tupleZafr   ZportiZaddrpartZscopeZai_flags_tupr   r   r   low_level_address_tuple   s(    

r%   )N)__doc__r    Zdns.ipv4r   Zdns.ipv6r   r   r   r   r   r   r   r%   r   r   r   r   <module>   s   