U mf@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)rrr6/opt/hc_python/lib/python3.8/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 inet_ntoar r r)rrrrr inet_ntop6s   r)rrc Cs^ztj|tWStk rXztj|dtWYStk rRtYnXYnXdS)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 Csvz"tj|d}|dko |dkWStk rpz tj|dd}|dkWYStk rjtYnXYnXdS)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 Cs\ztj|WdStk rVztj|dWYdStk rPYYdSXYnXdS)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|\}}|dkrt|}|tkr(||fS|tkr|d}|dkrN||ddfS|d|}||dd}|r||dt|fSz||dt|fWStk rtj }tj |||d^^}} }| YSXnt 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 findisdigitintsocketif_nametoindexAttributeErrorAI_NUMERICHOST getaddrinfor) rr rportiZaddrpartscopeZai_flags_tuprrrlow_level_address_tuples(     r2cCs.|tjkrdS|tjkrdStd|dS)z:Return the 'any' address for the specified address family.z0.0.0.0z::r$N)r(rr r)r rrr any_for_afs   r3c CsTztj|WStk rNztj|WYStk rHtYnXYnXdS)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 rrrrrr4sr4)N)__doc__r(typingrrrZdns.ipv4r Zdns.ipv6rr r'strbytesrrrboolrrr2r3r4rrrrs&  $