
    s8                        d Z ddlmZ ddlZddlZddlZddlZddlZddl	Zddl
ZddlZddlmZmZmZ 	 ddlZdZefdZdZefdZ ed	      Zd
 Zd Z G d de      Z G d de      Zi ZdZ  ejB                         Z"d Z#ddZ$ddZ% G d dejL                  jN                        Z(dejR                  jT                  fdZ+y# e$ r ddlZY w xY w)z
DNS rdata.    )BytesION   )xrangestring_types	text_type    c           	          t        j                  |       }dj                  t        dt	        |      |      D cg c]
  }||||z     c}      j                         S c c}w )z{Convert a binary string into its hex encoding, broken up into chunks
    of chunksize characters separated by a space.
        r   )binasciihexlifyjoinrangelendecodedata	chunksizelineis       lib/third_party/dns/rdata.py_hexifyr   (   sh    
 D!D99q#d)Y797  1Q]+79 ::@&(C 9   Ac           	          t        j                  |       }dj                  t        dt	        |      |      D cg c]
  }||||z     c}      j                         S c c}w )z~Convert a binary string into its base64 encoding, broken up into chunks
    of chunksize characters separated by a space.
    r
   r   )base64	b64encoder   r   r   r   r   s       r   
_base64ifyr   5   sh    
 D!D99q#d)Y797  1Q]+79 ::@&(C 9r   s   "\c                    t        | t              r| j                         } t        | t              st        |       } d}| D ]=  }|t        v r|dt        |      z   z  }|dk\  r|dk  r|t        |      z  }6|d|z  z  }? |S )z7Escape the characters in a quoted string which need it. \r      z\%03d)
isinstancer   encode	bytearray	__escapedchr)qstringtextcs      r   	_escapifyr)   A   s     '9%.."gy)G$D	>D3q6M!D$Y1t8CFNDHqL D  K    c                 l    t        t        |       dz
  dd      D ]  }| |   dk7  s| d|dz    c S  | dd S )zDetermine the index of greatest byte that isn't all zeros, and
    return the bitmap that contains all the bytes less than that index.
    r   r   )r   r   )whatr   s     r   _truncate_bitmapr.   T   sH    
 CIM2r*7a<1q5>! + !9r*   c                       e Zd ZdZddgZd Zd Zd ZddZdd	Z	dd
Z
d Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zedd       Zedd       ZddZy)Rdataz#Base class for all DNS rdata types.rdclassrdtypec                      || _         || _        y)zInitialize an rdata.

        *rdclass*, an ``int`` is the rdataclass of the Rdata.
        *rdtype*, an ``int`` is the rdatatype of the Rdata.
        Nr1   r2   )selfr1   r2   s      r   __init__zRdata.__init__d   s     r*   c                 6    t         j                  j                  S )a  Return the type a Rdata covers.

        DNS SIG/RRSIG rdatas apply to a specific type; this type is
        returned by the covers() function.  If the rdata type is not
        SIG or RRSIG, dns.rdatatype.NONE is returned.  This is useful when
        creating rdatasets, allowing the rdataset to contain only RRSIGs
        of a particular type, e.g. RRSIG(NS).

        Returns an ``int``.
        )dns	rdatatypeNONEr5   s    r   coverszRdata.coversn   s     }}!!!r*   c                 B    | j                         dz  | j                  z  S )zReturn a 32-bit type value, the least significant 16 bits of
        which are the ordinary DNS type, and the upper 16 bits of which are
        the "covered" type, if any.

        Returns an ``int``.
           )r<   r2   r;   s    r   extended_rdatatypezRdata.extended_rdatatype|   s     {{}"T[[00r*   Nc                     t         )zFConvert an rdata to text format.

        Returns a ``text``.
        NotImplementedErrorr5   origin
relativizekws       r   to_textzRdata.to_text   
     "!r*   c                     t         )zHConvert an rdata to wire format.

        Returns a ``binary``.
        rA   r5   filecompressrD   s       r   to_wirezRdata.to_wire   rH   r*   c                 \    t               }| j                  |d|       |j                         S )zConvert rdata to a format suitable for digesting in hashes.  This
        is also the DNSSEC canonical form.

        Returns a ``binary``.
        N)r   rM   getvalue)r5   rD   fs      r   to_digestablezRdata.to_digestable   s'     IQf%zz|r*   c                     t         j                  j                  | j                  | j                  | j                                y)a9  Check that the current contents of the rdata's fields are
        valid.

        If you change an rdata by assigning to its fields,
        it is a good idea to call validate() when you are done making
        changes.

        Raises various exceptions if there are problems.

        Returns ``None``.
        N)r8   rdata	from_textr1   r2   rG   r;   s    r   validatezRdata.validate   s(     			DLL$++t||~Fr*   c                    | j                         }|t        j                  j                  k(  rd}n%dt        j                  j	                  |      z   dz   }dt        j
                  j	                  | j                        z   dz   t        j                  j	                  | j                        z   |z   dz   t        |       z   dz   S )Nr   ()z<DNS  z rdata: >)	r<   r8   r9   r:   rG   
rdataclassr1   r2   str)r5   r<   ctexts      r   __repr__zRdata.__repr__   s    S]]'''E#--//77#=E//==C}}$$T[[1249:<FG4y 	r*   c                 "    | j                         S N)rG   r;   s    r   __str__zRdata.__str__   s    ||~r*   c                     | j                  t        j                  j                        }|j                  t        j                  j                        }||k(  ry||kD  ryy)zCompare an rdata with another rdata of the same rdtype and
        rdclass.

        Return < 0 if self < other in the DNSSEC ordering, 0 if self
        == other, and > 0 if self > other.

        r   r   r,   )rQ   r8   nameroot)r5   otherourtheirs       r   _cmpz
Rdata._cmp   sK       /##CHHMM2%<5[r*   c                     t        |t              sy| j                  |j                  k7  s| j                  |j                  k7  ry| j	                  |      dk(  S )NFr   r!   r0   r1   r2   rh   r5   re   s     r   __eq__zRdata.__eq__   sE    %'<<5==(DKK5<<,Gyy1$$r*   c                     t        |t              sy| j                  |j                  k7  s| j                  |j                  k7  ry| j	                  |      dk7  S )NTr   rj   rk   s     r   __ne__zRdata.__ne__   sE    %'<<5==(DKK5<<,Gyy1$$r*   c                     t        |t              r2| j                  |j                  k7  s| j                  |j                  k7  rt        S | j                  |      dk  S Nr   r!   r0   r1   r2   NotImplementedrh   rk   s     r   __lt__zRdata.__lt__   sE    %'-1L!!yy!##r*   c                     t        |t              r2| j                  |j                  k7  s| j                  |j                  k7  rt        S | j                  |      dk  S rp   rq   rk   s     r   __le__zRdata.__le__   E    %'-1L!!yy1$$r*   c                     t        |t              r2| j                  |j                  k7  s| j                  |j                  k7  rt        S | j                  |      dk\  S rp   rq   rk   s     r   __ge__zRdata.__ge__   rv   r*   c                     t        |t              r2| j                  |j                  k7  s| j                  |j                  k7  rt        S | j                  |      dkD  S rp   rq   rk   s     r   __gt__zRdata.__gt__   sE    %'-1L!!yy!##r*   c                 f    t        | j                  t        j                  j                              S r`   )hashrQ   r8   rc   rd   r;   s    r   __hash__zRdata.__hash__   s     D&&sxx}}566r*   c                     t         r`   rA   )clsr1   r2   tokrD   rE   s         r   rT   zRdata.from_text       !!r*   c                     t         r`   rA   r   r1   r2   wirecurrentrdlenrD   s          r   	from_wirezRdata.from_wire   r   r*   c                      y)zWConvert any domain names in the rdata to the specified
        relativization.
        N )r5   rD   rE   s      r   choose_relativityzRdata.choose_relativity   s    r*   NTNNr`   )__name__
__module____qualname____doc__	__slots__r6   r<   r?   rG   rM   rQ   rU   r^   ra   rh   rl   rn   rs   ru   rx   rz   r}   classmethodrT   r   r   r   r*   r   r0   r0   _   s    -H%I"1""	G"%%$%%$7 " " " "r*   r0   c                   \     e Zd ZdZdgZ fdZddZedd       Zd	dZ	ed
d       Z
 xZS )GenericRdatazGeneric Rdata Class

    This class is used for rdata types for which we have no better
    implementation.  It implements the DNS "unknown RRs" scheme.
    r   c                 <    t         t        |   ||       || _        y r`   )superr   r6   r   )r5   r1   r2   r   	__class__s       r   r6   zGenericRdata.__init__  s    lD*7F;	r*   c                 ^    dt        | j                        z  t        | j                        z   S )Nz\# %d )r   r   r   rC   s       r   rG   zGenericRdata.to_text  s#    3tyy>)GDII,>>>r*   c                    |j                         }|j                         r|j                  dk7  rt        j                  j                  d      |j                         }g }	 |j                         }|j                         rn*|j                  |j                  j                                Kdj                  |      }	t        j                  |	      }
t        |
      |k7  rt        j                  j                  d       | |||
      S )N\#z$generic rdata does not start with \#r*   z'generic rdata hex data has wrong length)getis_identifiervaluer8   	exceptionSyntaxErrorget_intis_eol_or_eofappendr"   r   r   	unhexlifyr   )r   r1   r2   r   rD   rE   tokenlengthchunkshexr   s              r   rT   zGenericRdata.from_text  s    	""$u(<--++79 9GGIE""$MM%++,,./	 
 hhv!!#&t9--++9; ;7FD))r*   c                 :    |j                  | j                         y r`   )writer   rJ   s       r   rM   zGenericRdata.to_wire*  s    

499r*   c                 "     | ||||||z          S r`   r   r   s          r   r   zGenericRdata.from_wire-  s    7FD'E/$BCCr*   r   r   r`   )r   r   r   r   r   r6   rG   r   rT   rM   r   __classcell__)r   s   @r   r   r     sJ     I? * *& D Dr*   r   zdns.rdtypesc                    d }t         j                  | |f      }t        j                  j	                  |       }t        j
                  j	                  |      }|j                  dd      }|s[t         j                  t        j
                  j                  |f      }|s*	  |dj                  t        ||g            }|t         | |f<   |rt        ||      }|S t        }|S # t        $ rW 	  |dj                  t        d|g            }|t         t        j                  j                  |f<   n# t        $ r d }Y nw xY wY ww xY w)Nc                     t         5  t        |       }| j                  d      }|dd  D ]  }t        ||      } |cd d d        S # 1 sw Y   y xY w)N.r   )_import_lock
__import__splitgetattr)rc   mod
componentscomps       r   import_modulez&get_rdata_class.<locals>.import_module7  sE    T"CCJ"12c4( ' \\s   4AA-_r   ANY)_rdata_modulesr   r8   r[   rG   r9   replacer   r   _module_prefixImportErrorr   r   )r1   r2   r   r   rdclass_textrdtype_textr   s          r   get_rdata_classr   5  s@    

gv.
/C>>))'2L--''/K%%c3/K  #--"3"3V!<=
#CHHn.:K.I %J K4701 c;' J J  '.272F )G HCCFNCNN$6$6#?@" Cs7   )C   	E *AD,+E ,D:7E 9D::E ?E c           	         t        |t              rt        j                  j	                  |      }t        | |      }|t        k7  r|j                         }|j                  |       |j                         rU|j                  dk(  rFt        j                  | ||||      }t        | ||j                  dt        |j                        |      S |j                  | ||||      S )aN  Build an rdata object from text format.

    This function attempts to dynamically load a class which
    implements the specified rdata class and type.  If there is no
    class-and-type-specific implementation, the GenericRdata class
    is used.

    Once a class is chosen, its from_text() class method is called
    with the parameters to this function.

    If *tok* is a ``text``, then a tokenizer is created and the string
    is used as its input.

    *rdclass*, an ``int``, the rdataclass.

    *rdtype*, an ``int``, the rdatatype.

    *tok*, a ``dns.tokenizer.Tokenizer`` or a ``text``.

    *origin*, a ``dns.name.Name`` (or ``None``), the
    origin to use for relative names.

    *relativize*, a ``bool``.  If true, name will be relativized to
    the specified origin.

    Returns an instance of the chosen Rdata subclass.
    r   r   )r!   r   r8   	tokenizer	Tokenizerr   r   r   ungetr   r   rT   r   r   r   )r1   r2   r   rD   rE   r   r   rS   s           r   rT   rT   X  s    : #|$mm%%c*
'6
*C
l			% ;;% !**7FC+57EWfejj!S_#% %==&#vzBBr*   c                     t         j                  j                  |      }t        | |      }|j	                  | |||||      S )a2  Build an rdata object from wire format

    This function attempts to dynamically load a class which
    implements the specified rdata class and type.  If there is no
    class-and-type-specific implementation, the GenericRdata class
    is used.

    Once a class is chosen, its from_wire() class method is called
    with the parameters to this function.

    *rdclass*, an ``int``, the rdataclass.

    *rdtype*, an ``int``, the rdatatype.

    *wire*, a ``binary``, the wire-format message.

    *current*, an ``int``, the offset in wire of the beginning of
    the rdata.

    *rdlen*, an ``int``, the length of the wire-format rdata

    *origin*, a ``dns.name.Name`` (or ``None``).  If not ``None``,
    then names will be relativized to this origin.

    Returns an instance of the chosen Rdata subclass.
    )r8   wiredata
maybe_wrapr   r   )r1   r2   r   r   r   rD   r   s          r   r   r     s=    8 <<""4(D
'6
*C==&$GGr*   c                       e Zd ZdZddhZdZy)RdatatypeExistszDNS rdatatype already exists.r1   r2   zGThe rdata type with class {rdclass} and rdtype {rdtype} already exists.N)r   r   r   r   supp_kwargsfmtr   r*   r   r   r     s    'h'KCr*   r   Fc                     t        ||      }|t        k7  rt        ||      | t        ||f<   t        j
                  j                  |||       y)a  Dynamically register a module to handle an rdatatype.

    *implementation*, a module implementing the type in the usual dnspython
    way.

    *rdtype*, an ``int``, the rdatatype to register.

    *rdtype_text*, a ``text``, the textual form of the rdatatype.

    *is_singleton*, a ``bool``, indicating if the type is a singleton (i.e.
    RRsets of the type can have only one member.)

    *rdclass*, the rdataclass of the type, or ``dns.rdataclass.ANY`` if
    it applies to all classes.
    r4   N)r   r   r   r   r8   r9   register_type)implementationr2   r   is_singletonr1   existing_clss         r   r   r     sJ    $ #7F3L|#gf==(6NGV$%MM\Br*   r   r`   ),r   ior   r   r   dns.exceptionr8   dns.namedns.rdataclassdns.rdatatypedns.tokenizerdns.wiredata_compatr   r   r   	threading
_threadingr   dummy_threading_hex_chunksizer   _base64_chunksizer   r#   r$   r)   r.   objectr0   r   r   r   Lockr   r   rT   r   r   DNSExceptionr   r[   INr   r   r*   r   <module>r      s   $           4 4)"  + C    1 C f	&dF dL*D5 *DX z  F/CdHBcmm00  EJ..++Ca  )()s   C 	CC