o
    'h                     @   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
dededefddZded	edefd
dZdedefddZdedefddZdedefddZ	ddeeef dee defddZdd ZdedefddZdS )z*Generic Internet address helper functions.    N)AnyOptionalTuplefamilytextreturnc                 C   s.   | t kr
tj|S | tkrtj|dS t)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``.
    T)AF_INETdnsipv4	inet_atonAF_INET6ipv6NotImplementedError)r   r    r   D/var/www/html/olx_land/venv/lib/python3.10/site-packages/dns/inet.py	inet_pton!   s
   r   addressc                 C   s,   | t kr
tj|S | tkrtj|S t)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``.
    )r   r	   r
   	inet_ntoar   r   r   )r   r   r   r   r   	inet_ntop6   s
   r   c                 C   sN   z	t j|  tW S  ty&   zt j| d tW  Y S  ty%   tw w )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``.
    T)r	   r
   r   r   	Exceptionr   r   
ValueErrorr   r   r   r   af_for_addressK   s   
r   c                 C   sf   zt j| d }|dko|dkW S  ty2   zt j| dd }|dkW  Y S  ty1   tw w )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   )r	   r
   r   r   r   r   )r   firstr   r   r   is_multicasta   s   r   c                 C   sP   z	t j|  W dS  ty'   zt j| d W Y dS  ty&   Y Y dS w w )z{Is the specified string an IPv4 or IPv6 address?

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

    Returns a ``bool``.
    TF)r	   r
   r   r   r   r   r   r   r   
is_addressw   s   r   
high_tupleafc           
      C   s   | \}}|du rt |}|tkr||fS |tkrg|d}|dk r'||ddfS |d| }||d d }| rA||dt|fS z
||dt|fW S  tyf   tj	}tj
|||d^^ }}	}|	 Y S w td| )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iaddrpartscopeai_flags_tupr   r   r   low_level_address_tuple   s*   
r4   c                 C   s*   | t jkrdS | t jkrdS td|  )z:Return the 'any' address for the specified address family.z0.0.0.0z::r$   )r(   r   r   r   )r    r   r   r   
any_for_af   s
   

r5   c                 C   sD   zt j| W S  ty!   z	t j| W  Y S  ty    tw w )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.
    )r	   r   canonicalizer   r
   r   r   r   r   r   r6      s   r6   )N)__doc__r(   typingr   r   r   dns.ipv4r	   dns.ipv6r   r   r'   strbytesr   r   r   boolr   r   r4   r5   r6   r   r   r   r   <module>   s,   

$	