3
[_ð]s8  ã               @   s<  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 yddlZW n ek
rŒ   ddlZY nX dZefdd„ZdZefd	d
„ZedƒZdd„ Zdd„ ZG dd„ deƒZG dd„ deƒZi ZdZ ej!ƒ Z"dd„ Z#d!dd„Z$d"dd„Z%G dd„ dej&j'ƒZ(dej)j*fdd „Z+dS )#z
DNS rdata.é    )ÚBytesIONé   )ÚxrangeÚstring_typesÚ	text_typeé    c                s4   t j| ƒ‰dj‡ ‡fdd„tdtˆƒˆ ƒD ƒƒjƒ S )z{Convert a binary string into its hex encoding, broken up into chunks
    of chunksize characters separated by a space.
    ó    c                s   g | ]}ˆ||ˆ  … ‘qS © r	   )Ú.0Úi)Ú	chunksizeÚliner	   ú./tmp/pip-build-yz_zf6az/dnspython/dns/rdata.pyú
<listcomp>.   s   z_hexify.<locals>.<listcomp>r   )ÚbinasciiÚhexlifyÚjoinÚrangeÚlenÚdecode)Údatar   r	   )r   r   r   Ú_hexify(   s    
r   c                s4   t j| ƒ‰dj‡ ‡fdd„tdtˆƒˆ ƒD ƒƒjƒ S )z~Convert a binary string into its base64 encoding, broken up into chunks
    of chunksize characters separated by a space.
    r   c                s   g | ]}ˆ||ˆ  … ‘qS r	   r	   )r
   r   )r   r   r	   r   r   ;   s   z_base64ify.<locals>.<listcomp>r   )Úbase64Ú	b64encoder   r   r   r   )r   r   r	   )r   r   r   Ú
_base64ify5   s    
r   s   "\c             C   s~   t | tƒr| jƒ } t | tƒs$t| ƒ} d}xP| D ]H}|tkrL|dt|ƒ 7 }q.|dkrj|dk rj|t|ƒ7 }q.|d| 7 }q.W |S )z7Escape the characters in a quoted string which need it.Ú ú\r   é   z\%03d)Ú
isinstancer   ÚencodeÚ	bytearrayÚ	__escapedÚchr)ZqstringÚtextÚcr	   r	   r   Ú	_escapifyA   s    


r%   c             C   sF   x8t t| ƒd ddƒD ] }| | dkr| d|d … S qW | dd… S )z‹Determine 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   r   )Úwhatr   r	   r	   r   Ú_truncate_bitmapT   s    r(   c               @   sÌ   e Zd ZdZddgZdd„ Zdd„ Zdd	„ Zd.dd„Zd/dd„Z	d0dd„Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zed1d(d)„ƒZed2d*d+„ƒZd3d,d-„Zd
S )4ÚRdataz#Base class for all DNS rdata types.ÚrdclassÚrdtypec             C   s   || _ || _dS )z˜Initialize an rdata.

        *rdclass*, an ``int`` is the rdataclass of the Rdata.
        *rdtype*, an ``int`` is the rdatatype of the Rdata.
        N)r*   r+   )Úselfr*   r+   r	   r	   r   Ú__init__d   s    zRdata.__init__c             C   s   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)r,   r	   r	   r   Úcoversn   s    zRdata.coversc             C   s   | j ƒ d> | jB S )zÒReturn 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``.
        é   )r1   r+   )r,   r	   r	   r   Úextended_rdatatype|   s    zRdata.extended_rdatatypeNTc             K   s   t ‚dS )zFConvert an rdata to text format.

        Returns a ``text``.
        N)ÚNotImplementedError)r,   ÚoriginÚ
relativizeÚkwr	   r	   r   Úto_text†   s    zRdata.to_textc             C   s   t ‚dS )zHConvert an rdata to wire format.

        Returns a ``binary``.
        N)r4   )r,   ÚfileÚcompressr5   r	   r	   r   Úto_wireŽ   s    zRdata.to_wirec             C   s   t ƒ }| j|d|ƒ |jƒ S )z”Convert rdata to a format suitable for digesting in hashes.  This
        is also the DNSSEC canonical form.

        Returns a ``binary``.
        N)r   r;   Úgetvalue)r,   r5   Úfr	   r	   r   Úto_digestable–   s    zRdata.to_digestablec             C   s   t jj| j| j| jƒ ƒ dS )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)r.   ÚrdataÚ	from_textr*   r+   r8   )r,   r	   r	   r   Úvalidate¡   s    zRdata.validatec             C   sf   | j ƒ }|tjjkrd}ndtjj|ƒ d }dtjj| jƒ d tjj| jƒ | d t| ƒ d S )Nr   ú(ú)z<DNS ú z rdata: ú>)	r1   r.   r/   r0   r8   Ú
rdataclassr*   r+   Ústr)r,   r1   Zctextr	   r	   r   Ú__repr__°   s
    zRdata.__repr__c             C   s   | j ƒ S )N)r8   )r,   r	   r	   r   Ú__str__º   s    zRdata.__str__c             C   s<   | j tjjƒ}|j tjjƒ}||kr(dS ||kr4dS dS dS )zÆCompare 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   Nr&   )r>   r.   ÚnameÚroot)r,   ÚotherZourZtheirr	   r	   r   Ú_cmp½   s    z
Rdata._cmpc             C   s8   t |tƒsdS | j|jks&| j|jkr*dS | j|ƒdkS )NFr   )r   r)   r*   r+   rM   )r,   rL   r	   r	   r   Ú__eq__Î   s
    
zRdata.__eq__c             C   s8   t |tƒsdS | j|jks&| j|jkr*dS | j|ƒdkS )NTr   )r   r)   r*   r+   rM   )r,   rL   r	   r	   r   Ú__ne__Õ   s
    
zRdata.__ne__c             C   s6   t |tƒ s$| j|jks$| j|jkr(tS | j|ƒdk S )Nr   )r   r)   r*   r+   ÚNotImplementedrM   )r,   rL   r	   r	   r   Ú__lt__Ü   s    zRdata.__lt__c             C   s6   t |tƒ s$| j|jks$| j|jkr(tS | j|ƒdkS )Nr   )r   r)   r*   r+   rP   rM   )r,   rL   r	   r	   r   Ú__le__ã   s    zRdata.__le__c             C   s6   t |tƒ s$| j|jks$| j|jkr(tS | j|ƒdkS )Nr   )r   r)   r*   r+   rP   rM   )r,   rL   r	   r	   r   Ú__ge__é   s    zRdata.__ge__c             C   s6   t |tƒ s$| j|jks$| j|jkr(tS | j|ƒdkS )Nr   )r   r)   r*   r+   rP   rM   )r,   rL   r	   r	   r   Ú__gt__ï   s    zRdata.__gt__c             C   s   t | jtjjƒƒS )N)Úhashr>   r.   rJ   rK   )r,   r	   r	   r   Ú__hash__õ   s    zRdata.__hash__c             C   s   t ‚d S )N)r4   )Úclsr*   r+   Útokr5   r6   r	   r	   r   r@   ø   s    zRdata.from_textc             C   s   t ‚d S )N)r4   )rW   r*   r+   ÚwireÚcurrentÚrdlenr5   r	   r	   r   Ú	from_wireü   s    zRdata.from_wirec             C   s   dS )zWConvert any domain names in the rdata to the specified
        relativization.
        Nr	   )r,   r5   r6   r	   r	   r   Úchoose_relativity   s    zRdata.choose_relativity)NT)NN)N)NT)N)NT)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r-   r1   r3   r8   r;   r>   rA   rH   rI   rM   rN   rO   rQ   rR   rS   rT   rV   Úclassmethodr@   r\   r]   r	   r	   r	   r   r)   _   s0   





r)   c                   sV   e Zd ZdZdgZ‡ fdd„Zddd„Zedd	d
„ƒZddd„Z	eddd„ƒZ
‡  ZS )ÚGenericRdataz�Generic 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                s   t t| ƒj||ƒ || _d S )N)Úsuperrd   r-   r   )r,   r*   r+   r   )Ú	__class__r	   r   r-     s    zGenericRdata.__init__NTc             K   s   dt | jƒ t| jƒ S )Nz\# %d )r   r   r   )r,   r5   r6   r7   r	   r	   r   r8     s    zGenericRdata.to_textc             C   s”   |j ƒ }|jƒ  s|jdkr(tjjdƒ‚|jƒ }g }x&|j ƒ }|jƒ rHP |j|jj	ƒ ƒ q6W dj
|ƒ}	tj|	ƒ}
t|
ƒ|krˆtjjdƒ‚| |||
ƒS )Nz\#z$generic rdata does not start with \#ó    z'generic rdata hex data has wrong length)ÚgetÚis_identifierÚvaluer.   Ú	exceptionÚSyntaxErrorZget_intZis_eol_or_eofÚappendr   r   r   Ú	unhexlifyr   )rW   r*   r+   rX   r5   r6   ÚtokenÚlengthÚchunksÚhexr   r	   r	   r   r@     s"    

zGenericRdata.from_textc             C   s   |j | jƒ d S )N)Úwriter   )r,   r9   r:   r5   r	   r	   r   r;   *  s    zGenericRdata.to_wirec             C   s   | |||||| … ƒS )Nr	   )rW   r*   r+   rY   rZ   r[   r5   r	   r	   r   r\   -  s    zGenericRdata.from_wire)NT)NT)NN)N)r^   r_   r`   ra   rb   r-   r8   rc   r@   r;   r\   Ú__classcell__r	   r	   )rf   r   rd     s   

rd   zdns.rdtypesc             C   sè   dd„ }t j| |fƒ}tjj| ƒ}tjj|ƒ}|jddƒ}|sÐt jtjj|fƒ}|sÐy$|djt	||gƒƒ}|t | |f< W nV t
k
rÎ   y(|djt	d|gƒƒ}|t tjj|f< W n t
k
rÈ   d }Y nX Y nX |ràt||ƒ}nt}|S )Nc             S   sF   t �8 t| ƒ}| jdƒ}x|dd … D ]}t||ƒ}q&W |S Q R X d S )NÚ.r   )Ú_import_lockÚ
__import__ÚsplitÚgetattr)rJ   ÚmodÚ
componentsÚcompr	   r	   r   Úimport_module7  s    
z&get_rdata_class.<locals>.import_moduleú-Ú_ru   ÚANY)Ú_rdata_modulesrh   r.   rF   r8   r/   Úreplacer€   r   Ú_module_prefixÚImportErrorry   rd   )r*   r+   r}   rz   Zrdclass_textÚrdtype_textrW   r	   r	   r   Úget_rdata_class5  s.    r†   Tc             C   sŠ   t |tƒrtjj|ƒ}t| |ƒ}|tkrx|jƒ }|j|ƒ |j	ƒ rx|j
dkrxt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.
    z\#r   )r   r   r.   Ú	tokenizerÚ	Tokenizerr†   rd   rh   Úungetri   rj   r@   r\   r   r   )r*   r+   rX   r5   r6   rW   ro   r?   r	   r	   r   r@   X  s    



r@   c             C   s*   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.
    )r.   ZwiredataZ
maybe_wrapr†   r\   )r*   r+   rY   rZ   r[   r5   rW   r	   r	   r   r\   Š  s    
r\   c               @   s   e Zd ZdZddhZdZdS )ÚRdatatypeExistszDNS rdatatype already exists.r*   r+   z8The rdata type with class {rdclass} and rdtype {rdtype} zalready exists.NzGThe rdata type with class {rdclass} and rdtype {rdtype} already exists.)r^   r_   r`   ra   Zsupp_kwargsÚfmtr	   r	   r	   r   rŠ   «  s    rŠ   Fc             C   s>   t ||ƒ}|tkrt||d�‚| t||f< tjj|||ƒ dS )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.
    )r*   r+   N)r†   rd   rŠ   r�   r.   r/   Úregister_type)Úimplementationr+   r…   Zis_singletonr*   Zexisting_clsr	   r	   r   rŒ   ²  s
    
rŒ   )NT)N),ra   Úior   r   r   Zdns.exceptionr.   Zdns.nameZdns.rdataclassZdns.rdatatypeZdns.tokenizerZdns.wiredataÚ_compatr   r   r   Ú	threadingÚ
_threadingr„   Údummy_threadingZ_hex_chunksizer   Z_base64_chunksizer   r    r!   r%   r(   Úobjectr)   rd   r�   rƒ   ÚLockrv   r†   r@   r\   rk   ZDNSExceptionrŠ   rF   ÚINrŒ   r	   r	   r	   r   Ú<module>   sB   

 ',#
2
!