a Me@sdZddlZddlmZmZmZddlZddlZej Z ej Z e e e dddZe e e ddd Ze e d d d Ze ed d dZe ed ddZdee e fee edddZddZe e d ddZdS)z*Generic Internet address helper functions.N)AnyOptionalTuple)familytextreturncCs2|tkrtj|S|tkr*tj|dStdS)aConvert 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_INETdnsipv4 inet_atonAF_INET6ipv6NotImplementedError)rrr,/usr/lib/python3.9/site-packages/dns/inet.py inet_pton!s  r)raddressrcCs0|tkrtj|S|tkr(tj|StdS)a0Convert 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)rr r Z inet_ntoar r r)rrrrr inet_ntop6s   r)rrc CsZztj|tWStyTztj|dtWYStyNtYn0Yn0dS)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 ValueErrorrrrraf_for_addressKs     rc Csrz"tj|d}|dko |dkWStylz tj|dd}|dkWYStyftYn0Yn0dS)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``. rTN)r r r rr r)rfirstrrr is_multicastas   rc CsXztj|WdStyRztj|dWYdStyLYYdS0Yn0dS)z{Is the specified string an IPv4 or IPv6 address? *text*, a ``str``, the textual address. Returns a ``bool``. TFN)r r r rr rrrr is_addressws   r) high_tupleafrc Cs|\}}|durt|}|tkr(||fS|tkr|d}|dkrN||ddfS|d|}||dd}|r||dt|fSz||dt|fWStytj }tj |||d^^}} }| YS0nt d|dS)aXGiven 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)flagsunknown address family ) rrr findisdigitintsocketZif_nametoindexAttributeErrorZAI_NUMERICHOSTZ getaddrinfor) rrrportiZaddrpartZscopeZai_flags_tuprrrlow_level_address_tuples(      r-cCs.|tjkrdS|tjkrdStd|dS)z:Return the 'any' address for the specified address family.z0.0.0.0z::r#N)r'rr r)rrrr any_for_afs   r.c CsPztj|WStyJztj|WYStyDtYn0Yn0dS)aVerify 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 canonicalizerr rrrrrr/s  r/)N)__doc__r'typingrrrZdns.ipv4r Zdns.ipv6rr r&strbytesrrrboolrrr-r.r/rrrrs$ $