a
    sd_¬¶  ã                   @   s2  d 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
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G dd„ dejjƒZG dd„ deƒZG dd„ deƒZG d	d
„ d
eƒZG dd„ dƒZG dd„ dƒZdejjdeddddfdd„Zdejjdedddfdd„Zeddfdd„ZdS )z
DNS Zones.é    Nc                   @   s   e Zd ZdZdS )ÚBadZonezThe DNS zone is malformed.N©Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   ú*/usr/lib/python3/dist-packages/dns/zone.pyr   '   s   r   c                   @   s   e Zd ZdZdS )ÚNoSOAz)The DNS zone has no SOA RR at its origin.Nr   r   r   r   r	   r
   ,   s   r
   c                   @   s   e Zd ZdZdS )ÚNoNSz+The DNS zone has no NS RRset at its origin.Nr   r   r   r   r	   r   1   s   r   c                   @   s   e Zd ZdZdS )ÚUnknownOriginz!The DNS zone's origin is unknown.Nr   r   r   r   r	   r   6   s   r   c                   @   sR  e Zd ZdZejjZg d¢Zej	j
df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dd„ Zdd„ Zd<dd „Zd=d!d"„Zd#d$„ Zejjdfd%d&„Zejjdfd'd(„Zejjfd)d*„Zd+d,„ Z ejjfd-d.„Z!ejjfd/d0„Z"ejj#ejjfd1d2„Z$ejj#ejjfd3d4„Z%d>d6d7„Z&d?d8d9„Z'd:d;„ Z(d5S )@ÚZoneaz  A DNS zone.

    A ``Zone`` is a mapping from names to nodes.  The zone object may be
    treated like a Python dictionary, e.g. ``zone[name]`` will retrieve
    the node associated with that name.  The *name* may be a
    ``dns.name.Name object``, or it may be a string.  In either case,
    if the name is relative it is treated as relative to the origin of
    the zone.
    )ÚrdclassÚoriginÚnodesÚ
relativizeTc                 C   sb   |durFt |tƒr tj |¡}nt |tjjƒs6tdƒ‚| ¡ sFtdƒ‚|| _|| _	i | _
|| _dS )aÔ  Initialize a zone object.

        *origin* is the origin of the zone.  It may be a ``dns.name.Name``,
        a ``str``, or ``None``.  If ``None``, then the zone's origin will
        be set by the first ``$ORIGIN`` line in a masterfile.

        *rdclass*, an ``int``, the zone's rdata class; the default is class IN.

        *relativize*, a ``bool``, determine's whether domain names are
        relativized to the zone's origin.  The default is ``True``.
        Nz2origin parameter must be convertible to a DNS namez)origin parameter must be an absolute name)Ú
isinstanceÚstrÚdnsÚnameÚ	from_textÚNameÚ
ValueErrorÚis_absoluter   r   r   r   )Úselfr   r   r   r   r   r	   Ú__init__K   s    
zZone.__init__c                 C   s:   t |tƒsdS | j|jks2| j|jks2| j|jkr6dS dS )zqTwo zones are equal if they have the same origin, class, and
        nodes.

        Returns a ``bool``.
        FT)r   r   r   r   r   ©r   Úotherr   r   r	   Ú__eq__e   s    

ÿ
þzZone.__eq__c                 C   s   |   |¡ S )z>Are two zones not equal?

        Returns a ``bool``.
        )r   r   r   r   r	   Ú__ne__t   s    zZone.__ne__c                 C   sb   t |tƒrtj |d ¡}nt |tjjƒs0tdƒ‚| ¡ r^| | j	¡sLtdƒ‚| j
r^| 
| j	¡}|S )Nz0name parameter must be convertible to a DNS namez5name parameter must be a subdomain of the zone origin)r   r   r   r   r   r   ÚKeyErrorr   Úis_subdomainr   r   ©r   r   r   r   r	   Ú_validate_name|   s    
ÿzZone._validate_namec                 C   s   |   |¡}| j| S ©N©r#   r   ©r   Úkeyr   r   r	   Ú__getitem__‰   s    
zZone.__getitem__c                 C   s   |   |¡}|| j|< d S r$   r%   )r   r'   Úvaluer   r   r	   Ú__setitem__   s    
zZone.__setitem__c                 C   s   |   |¡}| j|= d S r$   r%   r&   r   r   r	   Ú__delitem__‘   s    
zZone.__delitem__c                 C   s
   | j  ¡ S r$   )r   Ú__iter__©r   r   r   r	   r,   •   s    zZone.__iter__c                 C   s
   | j  ¡ S r$   )r   Úkeysr-   r   r   r	   r.   ˜   s    z	Zone.keysc                 C   s
   | j  ¡ S r$   )r   Úvaluesr-   r   r   r	   r/   ›   s    zZone.valuesc                 C   s
   | j  ¡ S r$   )r   Úitemsr-   r   r   r	   r0   ž   s    z
Zone.itemsc                 C   s   |   |¡}| j |¡S r$   )r#   r   Úgetr&   r   r   r	   r1   ¡   s    
zZone.getc                 C   s
   || j v S r$   )r   r   r   r   r	   Ú__contains__¥   s    zZone.__contains__Fc                 C   s<   |   |¡}| j |¡}|du r8|s&t‚|  ¡ }|| j|< |S )aN  Find a node in the zone, possibly creating it.

        *name*: the name of the node to find.
        The value may be a ``dns.name.Name`` or a ``str``.  If absolute, the
        name must be a subdomain of the zone's origin.  If ``zone.relativize``
        is ``True``, then the name will be relativized.

        *create*, a ``bool``.  If true, the node will be created if it does
        not exist.

        Raises ``KeyError`` if the name is not known and create was
        not specified, or if the name was not a subdomain of the origin.

        Returns a ``dns.node.Node``.
        N)r#   r   r1   r    Únode_factory©r   r   ÚcreateÚnoder   r   r	   Ú	find_node¨   s    

zZone.find_nodec                 C   s,   z|   ||¡}W n ty&   d}Y n0 |S )a  Get a node in the zone, possibly creating it.

        This method is like ``find_node()``, except it returns None instead
        of raising an exception if the node does not exist and creation
        has not been requested.

        *name*: the name of the node to find.
        The value may be a ``dns.name.Name`` or a ``str``.  If absolute, the
        name must be a subdomain of the zone's origin.  If ``zone.relativize``
        is ``True``, then the name will be relativized.

        *create*, a ``bool``.  If true, the node will be created if it does
        not exist.

        Raises ``KeyError`` if the name is not known and create was
        not specified, or if the name was not a subdomain of the origin.

        Returns a ``dns.node.Node`` or ``None``.
        N)r7   r    r4   r   r   r	   Úget_nodeÂ   s
    
zZone.get_nodec                 C   s    |   |¡}|| jv r| j|= dS )ak  Delete the specified node if it exists.

        *name*: the name of the node to find.
        The value may be a ``dns.name.Name`` or a ``str``.  If absolute, the
        name must be a subdomain of the zone's origin.  If ``zone.relativize``
        is ``True``, then the name will be relativized.

        It is not an error if the node does not exist.
        Nr%   r"   r   r   r	   Údelete_nodeÝ   s    

zZone.delete_nodec                 C   sL   |   |¡}tjj |¡}|dur.tjj |¡}|  ||¡}| | j|||¡S )a²  Look for an rdataset with the specified name and type in the zone,
        and return an rdataset encapsulating it.

        The rdataset returned is not a copy; changes to it will change
        the zone.

        KeyError is raised if the name or type are not found.

        *name*: the name of the node to find.
        The value may be a ``dns.name.Name`` or a ``str``.  If absolute, the
        name must be a subdomain of the zone's origin.  If ``zone.relativize``
        is ``True``, then the name will be relativized.

        *rdtype*, an ``int`` or ``str``, the rdata type desired.

        *covers*, an ``int`` or ``str`` or ``None``, the covered type.
        Usually this value is ``dns.rdatatype.NONE``, but if the
        rdtype is ``dns.rdatatype.SIG`` or ``dns.rdatatype.RRSIG``,
        then the covers value will be the rdata type the SIG/RRSIG
        covers.  The library treats the SIG and RRSIG types as if they
        were a family of types, e.g. RRSIG(A), RRSIG(NS), RRSIG(SOA).
        This makes RRSIGs much easier to work with than if RRSIGs
        covering different rdata types were aggregated into a single
        RRSIG rdataset.

        *create*, a ``bool``.  If true, the node will be created if it does
        not exist.

        Raises ``KeyError`` if the name is not known and create was
        not specified, or if the name was not a subdomain of the origin.

        Returns a ``dns.rdataset.Rdataset``.
        N)r#   r   Ú	rdatatypeÚ	RdataTypeÚmaker7   Úfind_rdatasetr   )r   r   ÚrdtypeÚcoversr5   r6   r   r   r	   r=   ì   s    $
zZone.find_rdatasetc                 C   s0   z|   ||||¡}W n ty*   d}Y n0 |S )a  Look for an rdataset with the specified name and type in the zone.

        This method is like ``find_rdataset()``, except it returns None instead
        of raising an exception if the rdataset does not exist and creation
        has not been requested.

        The rdataset returned is not a copy; changes to it will change
        the zone.

        *name*: the name of the node to find.
        The value may be a ``dns.name.Name`` or a ``str``.  If absolute, the
        name must be a subdomain of the zone's origin.  If ``zone.relativize``
        is ``True``, then the name will be relativized.

        *rdtype*, an ``int`` or ``str``, the rdata type desired.

        *covers*, an ``int`` or ``str`` or ``None``, the covered type.
        Usually this value is ``dns.rdatatype.NONE``, but if the
        rdtype is ``dns.rdatatype.SIG`` or ``dns.rdatatype.RRSIG``,
        then the covers value will be the rdata type the SIG/RRSIG
        covers.  The library treats the SIG and RRSIG types as if they
        were a family of types, e.g. RRSIG(A), RRSIG(NS), RRSIG(SOA).
        This makes RRSIGs much easier to work with than if RRSIGs
        covering different rdata types were aggregated into a single
        RRSIG rdataset.

        *create*, a ``bool``.  If true, the node will be created if it does
        not exist.

        Raises ``KeyError`` if the name is not known and create was
        not specified, or if the name was not a subdomain of the origin.

        Returns a ``dns.rdataset.Rdataset`` or ``None``.
        N)r=   r    )r   r   r>   r?   r5   Úrdatasetr   r   r	   Úget_rdataset  s
    %
zZone.get_rdatasetc                 C   sj   |   |¡}tjj |¡}|dur.tjj |¡}|  |¡}|durf| | j||¡ t|ƒdkrf|  	|¡ dS )aÁ  Delete the rdataset matching *rdtype* and *covers*, if it
        exists at the node specified by *name*.

        It is not an error if the node does not exist, or if there is no
        matching rdataset at the node.

        If the node has no rdatasets after the deletion, it will itself
        be deleted.

        *name*: the name of the node to find.
        The value may be a ``dns.name.Name`` or a ``str``.  If absolute, the
        name must be a subdomain of the zone's origin.  If ``zone.relativize``
        is ``True``, then the name will be relativized.

        *rdtype*, an ``int`` or ``str``, the rdata type desired.

        *covers*, an ``int`` or ``str`` or ``None``, the covered type.
        Usually this value is ``dns.rdatatype.NONE``, but if the
        rdtype is ``dns.rdatatype.SIG`` or ``dns.rdatatype.RRSIG``,
        then the covers value will be the rdata type the SIG/RRSIG
        covers.  The library treats the SIG and RRSIG types as if they
        were a family of types, e.g. RRSIG(A), RRSIG(NS), RRSIG(SOA).
        This makes RRSIGs much easier to work with than if RRSIGs
        covering different rdata types were aggregated into a single
        RRSIG rdataset.
        Nr   )
r#   r   r:   r;   r<   r8   Údelete_rdatasetr   Úlenr9   )r   r   r>   r?   r6   r   r   r	   rB   B  s    

zZone.delete_rdatasetc                 C   s.   |j | j krtdƒ‚|  |d¡}| |¡ dS )aÃ  Replace an rdataset at name.

        It is not an error if there is no rdataset matching I{replacement}.

        Ownership of the *replacement* object is transferred to the zone;
        in other words, this method does not store a copy of *replacement*
        at the node, it stores *replacement* itself.

        If the node does not exist, it is created.

        *name*: the name of the node to find.
        The value may be a ``dns.name.Name`` or a ``str``.  If absolute, the
        name must be a subdomain of the zone's origin.  If ``zone.relativize``
        is ``True``, then the name will be relativized.

        *replacement*, a ``dns.rdataset.Rdataset``, the replacement rdataset.
        z#replacement.rdclass != zone.rdclassTN)r   r   r7   Úreplace_rdataset)r   r   Zreplacementr6   r   r   r	   rD   h  s    zZone.replace_rdatasetc                 C   sf   |   |¡}tjj |¡}|dur.tjj |¡}| j|  | j||¡}tj 	|| j||¡}| 
|¡ |S )a®  Look for an rdataset with the specified name and type in the zone,
        and return an RRset encapsulating it.

        This method is less efficient than the similar
        ``find_rdataset()`` because it creates an RRset instead of
        returning the matching rdataset.  It may be more convenient
        for some uses since it returns an object which binds the owner
        name to the rdataset.

        This method may not be used to create new nodes or rdatasets;
        use ``find_rdataset`` instead.

        *name*: the name of the node to find.
        The value may be a ``dns.name.Name`` or a ``str``.  If absolute, the
        name must be a subdomain of the zone's origin.  If ``zone.relativize``
        is ``True``, then the name will be relativized.

        *rdtype*, an ``int`` or ``str``, the rdata type desired.

        *covers*, an ``int`` or ``str`` or ``None``, the covered type.
        Usually this value is ``dns.rdatatype.NONE``, but if the
        rdtype is ``dns.rdatatype.SIG`` or ``dns.rdatatype.RRSIG``,
        then the covers value will be the rdata type the SIG/RRSIG
        covers.  The library treats the SIG and RRSIG types as if they
        were a family of types, e.g. RRSIG(A), RRSIG(NS), RRSIG(SOA).
        This makes RRSIGs much easier to work with than if RRSIGs
        covering different rdata types were aggregated into a single
        RRSIG rdataset.

        *create*, a ``bool``.  If true, the node will be created if it does
        not exist.

        Raises ``KeyError`` if the name is not known and create was
        not specified, or if the name was not a subdomain of the origin.

        Returns a ``dns.rrset.RRset`` or ``None``.
        N)r#   r   r:   r;   r<   r   r=   r   ÚrrsetZRRsetÚupdate)r   r   r>   r?   r@   rE   r   r   r	   Ú
find_rrset€  s    '

zZone.find_rrsetc                 C   s.   z|   |||¡}W n ty(   d}Y n0 |S )a¦  Look for an rdataset with the specified name and type in the zone,
        and return an RRset encapsulating it.

        This method is less efficient than the similar ``get_rdataset()``
        because it creates an RRset instead of returning the matching
        rdataset.  It may be more convenient for some uses since it
        returns an object which binds the owner name to the rdataset.

        This method may not be used to create new nodes or rdatasets;
        use ``get_rdataset()`` instead.

        *name*: the name of the node to find.
        The value may be a ``dns.name.Name`` or a ``str``.  If absolute, the
        name must be a subdomain of the zone's origin.  If ``zone.relativize``
        is ``True``, then the name will be relativized.

        *rdtype*, an ``int`` or ``str``, the rdata type desired.

        *covers*, an ``int`` or ``str`` or ``None``, the covered type.
        Usually this value is ``dns.rdatatype.NONE``, but if the
        rdtype is ``dns.rdatatype.SIG`` or ``dns.rdatatype.RRSIG``,
        then the covers value will be the rdata type the SIG/RRSIG
        covers.  The library treats the SIG and RRSIG types as if they
        were a family of types, e.g. RRSIG(A), RRSIG(NS), RRSIG(SOA).
        This makes RRSIGs much easier to work with than if RRSIGs
        covering different rdata types were aggregated into a single
        RRSIG rdataset.

        *create*, a ``bool``.  If true, the node will be created if it does
        not exist.

        Raises ``KeyError`` if the name is not known and create was
        not specified, or if the name was not a subdomain of the origin.

        Returns a ``dns.rrset.RRset`` or ``None``.
        N)rG   r    )r   r   r>   r?   rE   r   r   r	   Ú	get_rrset°  s
    &
zZone.get_rrsetc                 c   sn   t jj |¡}|dur$t jj |¡}|  ¡ D ]<\}}|D ].}|t jjks\|j|kr8|j|kr8||fV  q8q,dS )a|  Return a generator which yields (name, rdataset) tuples for
        all rdatasets in the zone which have the specified *rdtype*
        and *covers*.  If *rdtype* is ``dns.rdatatype.ANY``, the default,
        then all rdatasets will be matched.

        *rdtype*, an ``int`` or ``str``, the rdata type desired.

        *covers*, an ``int`` or ``str`` or ``None``, the covered type.
        Usually this value is ``dns.rdatatype.NONE``, but if the
        rdtype is ``dns.rdatatype.SIG`` or ``dns.rdatatype.RRSIG``,
        then the covers value will be the rdata type the SIG/RRSIG
        covers.  The library treats the SIG and RRSIG types as if they
        were a family of types, e.g. RRSIG(A), RRSIG(NS), RRSIG(SOA).
        This makes RRSIGs much easier to work with than if RRSIGs
        covering different rdata types were aggregated into a single
        RRSIG rdataset.
        N)r   r:   r;   r<   r0   ÚANYr>   r?   )r   r>   r?   r   r6   Úrdsr   r   r	   Úiterate_rdatasetsÜ  s    ÿÿzZone.iterate_rdatasetsc                 c   s|   t jj |¡}|dur$t jj |¡}|  ¡ D ]J\}}|D ]<}|t jjks\|j|kr8|j|kr8|D ]}||j|fV  q`q8q,dS )ax  Return a generator which yields (name, ttl, rdata) tuples for
        all rdatas in the zone which have the specified *rdtype*
        and *covers*.  If *rdtype* is ``dns.rdatatype.ANY``, the default,
        then all rdatas will be matched.

        *rdtype*, an ``int`` or ``str``, the rdata type desired.

        *covers*, an ``int`` or ``str`` or ``None``, the covered type.
        Usually this value is ``dns.rdatatype.NONE``, but if the
        rdtype is ``dns.rdatatype.SIG`` or ``dns.rdatatype.RRSIG``,
        then the covers value will be the rdata type the SIG/RRSIG
        covers.  The library treats the SIG and RRSIG types as if they
        were a family of types, e.g. RRSIG(A), RRSIG(NS), RRSIG(SOA).
        This makes RRSIGs much easier to work with than if RRSIGs
        covering different rdata types were aggregated into a single
        RRSIG rdataset.
        N)	r   r:   r;   r<   r0   rI   r>   r?   Úttl)r   r>   r?   r   r6   rJ   Úrdatar   r   r	   Úiterate_rdatasù  s    ÿÿzZone.iterate_rdatasNc              
   C   sD  t  ¡ $}t|tƒr&| t|dƒ¡}t|ddƒ}|du r>d}|du rXtj 	|¡}d}n"t|tƒrn| 	|¡}n|}| 
¡ }|r”t|  ¡ ƒ}| ¡  n|  ¡ }|D ]~}	| |	 j|	| j|d}
t|
tƒrÐ|
 	|¡}n|
}|
 
¡ }
z| |¡ | |¡ W q  ty   | |
¡ | |¡ Y q 0 q W d  ƒ n1 s60    Y  dS )a÷  Write a zone to a file.

        *f*, a file or `str`.  If *f* is a string, it is treated
        as the name of a file to open.

        *sorted*, a ``bool``.  If True, the default, then the file
        will be written with the names sorted in DNSSEC order from
        least to greatest.  Otherwise the names will be written in
        whatever order they happen to have in the zone's dictionary.

        *relativize*, a ``bool``.  If True, the default, then domain
        names in the output will be relativized to the zone's origin
        if possible.

        *nl*, a ``str`` or None.  The end of line string.  If not
        ``None``, the output will use the platform's native
        end-of-line marker (i.e. LF on POSIX, CRLF on Windows).
        ÚwbÚencodingNzutf-8Ú
)r   r   )Ú
contextlibÚ	ExitStackr   r   Úenter_contextÚopenÚgetattrÚosÚlinesepÚencodeÚdecodeÚlistr.   ÚsortÚto_textr   ÚwriteÚ	TypeError)r   ÚfÚsortedr   ÚnlÚstackZfile_encZnl_bÚnamesÚnÚlZl_br   r   r	   Úto_file  s>    


ÿ


zZone.to_filec                 C   s,   t  ¡ }|  ||||¡ | ¡ }| ¡  |S )aÌ  Return a zone's text as though it were written to a file.

        *sorted*, a ``bool``.  If True, the default, then the file
        will be written with the names sorted in DNSSEC order from
        least to greatest.  Otherwise the names will be written in
        whatever order they happen to have in the zone's dictionary.

        *relativize*, a ``bool``.  If True, the default, then domain
        names in the output will be relativized to the zone's origin
        if possible.

        *nl*, a ``str`` or None.  The end of line string.  If not
        ``None``, the output will use the platform's native
        end-of-line marker (i.e. LF on POSIX, CRLF on Windows).

        Returns a ``str``.
        )ÚioÚStringIOrg   ÚgetvalueÚclose)r   ra   r   rb   Ztemp_bufferZreturn_valuer   r   r	   r]   T  s
    zZone.to_textc                 C   sJ   | j rtjj}n| j}|  |tjj¡du r.t‚|  |tjj	¡du rFt
‚dS )zçDo some simple checking of the zone's origin.

        Raises ``dns.zone.NoSOA`` if there is no SOA RRset.

        Raises ``dns.zone.NoNS`` if there is no NS RRset.

        Raises ``KeyError`` if there is no origin node.
        N)r   r   r   Úemptyr   rA   r:   ÚSOAr
   ZNSr   r"   r   r   r	   Úcheck_originl  s    	
zZone.check_origin)F)F)TTN)TTN))r   r   r   r   r   r6   ZNoder3   Ú	__slots__Ú
rdataclassÚINr   r   r   r#   r(   r*   r+   r,   r.   r/   r0   r1   r2   r7   r8   r9   r:   ZNONEr=   rA   rB   rD   rG   rH   rI   rK   rN   rg   r]   rn   r   r   r   r	   r   ;   sL   


ÿ
+ÿ
+&0,ÿ
ÿ

=
r   c                   @   sH   e Zd ZdZeddfdd„Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dS )Ú_MasterReadera(  Read a DNS master file

    @ivar tok: The tokenizer
    @type tok: dns.tokenizer.Tokenizer object
    @ivar last_ttl: The last seen explicit TTL for an RR
    @type last_ttl: int
    @ivar last_ttl_known: Has last TTL been detected
    @type last_ttl_known: bool
    @ivar default_ttl: The default TTL from a $TTL directive or SOA RR
    @type default_ttl: int
    @ivar default_ttl_known: Has default TTL been detected
    @type default_ttl_known: bool
    @ivar last_name: The last name read
    @type last_name: dns.name.Name object
    @ivar current_origin: The current origin
    @type current_origin: dns.name.Name object
    @ivar relativize: should names in the zone be relativized?
    @type relativize: bool
    @ivar zone: the zone
    @type zone: dns.zone.Zone object
    @ivar saved_state: saved reader state (used when processing $INCLUDE)
    @type saved_state: list of (tokenizer, current_origin, last_name, file,
    last_ttl, last_ttl_known, default_ttl, default_ttl_known) tuples.
    @ivar current_file: the file object of the $INCLUDed file being parsed
    (None if no $INCLUDE is active).
    @ivar allow_include: is $INCLUDE allowed?
    @type allow_include: bool
    @ivar check_origin: should sanity checks of the origin node be done?
    The default is True.
    @type check_origin: bool
    FTc                 C   st   t |tƒrtj |¡}|| _|| _|| _d| _d| _	d| _
d| _| j| _||||d| _g | _d | _|| _|| _d S )Nr   F©r   )r   r   r   r   r   ÚtokÚcurrent_originr   Úlast_ttlÚlast_ttl_knownÚdefault_ttlÚdefault_ttl_knownÚ	last_nameÚzoneÚsaved_stateÚcurrent_fileÚallow_includern   )r   rt   r   r   r   Úzone_factoryr~   rn   r   r   r	   r   ¡  s    
z_MasterReader.__init__c                 C   s   | j  ¡ }| ¡ r qq d S r$   )rt   r1   Úis_eol_or_eof)r   Útokenr   r   r	   Ú	_eat_line³  s    
z_MasterReader._eat_linec              
   C   sö  | j du rt‚| jjdd}| ¡ s8| j || j ¡| _n"| j ¡ }| ¡ rNdS | j |¡ | j}| 	| j
j¡sz|  ¡  dS | jrŽ| | j
j¡}| j ¡ }| ¡ s¨tjj‚d}z8tj |j¡}|| _d| _| j ¡ }| ¡ sâtjj‚W n6 tjjy   | jr| j}n| jr| j}Y n0 z.tj |j¡}| j ¡ }| ¡ sHtjj‚W n4 tjjyd   ‚ Y n ty~   | j
j}Y n0 || j
jkrštj d¡‚ztj |j¡}W n& tyÒ   tj d|j ¡‚Y n0 | j
j |¡}|du r| j
 ¡ }|| j
j|< z$tj  ||| j| j | j| j
j¡}W n\ tjjy@   ‚ Y nD ty‚   t! "¡ dd… \}}	tj d #t$|ƒt$|	ƒ¡¡‚Y n0 | js¸|tjj%kr¸|j&| _d| _|du r¸|j&}|du rÎtj d¡‚| '¡ }
| (|||
d¡}| )||¡ dS )	z(Process one line from a DNS master file.NT)Zwant_leadingúRR class is not zone's classúunknown rdatatype '%s'é   zcaught exception {}: {}úMissing default TTL value)*ru   r   rt   r1   Zis_whitespaceZas_namerz   r€   Úungetr!   r{   r   r‚   r   Úis_identifierr   Ú	exceptionÚSyntaxErrorrL   r   r)   rv   rw   ÚBadTTLry   rx   rp   Ú	Exceptionr   r:   r   r3   rM   ÚsysÚexc_infoÚformatr   rm   Zminimumr?   r=   Úadd)r   r   r   rL   r   r>   re   ÚrdÚtyÚvar?   rJ   r   r   r	   Ú_rr_line¹  s”    





ÿ


þÿ


z_MasterReader._rr_linec                 C   sä   t  d¡}t  d¡}t  d¡}| |¡}|rJ| ¡ \}}}}	}
|dkrJd}| |¡}|rz| ¡ \}}}|dkrrd}d}	d}
| |¡}|r¨| ¡ \}}}}	|dkr¤d}d}
|sÈ|sÈ|sÈd}d}d}d}	d}
|
dkrÖtƒ ‚||||	|
fS )Nz"^.*\$({(\+|-?)(\d+),(\d+),(.)}).*$z^.*\$({(\+|-?)(\d+)}).*$z^.*\$({(\+|-?)(\d+),(\d+)}).*$Ú ú+r   Úd)ÚreÚcompileÚmatchÚgroupsÚNotImplementedError)r   ZsideZis_generate1Zis_generate2Zis_generate3Zg1ÚmodZsignÚoffsetÚwidthÚbaseZg2Zg3r   r   r	   Ú_parse_modify  s<    





z_MasterReader._parse_modifyc           !   
   C   s  | j du rt‚| j ¡ }z2tj |j¡\}}}| j ¡ }| ¡ sHtj	j
‚W n tyd   tj	j
‚Y n0 z$|j}| j ¡ }| ¡ sˆtj	j
‚W n ty¤   tj	j
‚Y n0 z8tj |j¡}|| _d| _| j ¡ }| ¡ sÜtj	j
‚W nR tjjy0   | js| jstj	 
d¡‚| jr| j}n| jr,| j}Y n0 z.tj |j¡}| j ¡ }| ¡ s^tj	j
‚W n: tj	j
y€   tj	j
‚Y n tyš   | jj}Y n0 || jjkr¶tj	 
d¡‚z.tj |j¡}| j ¡ }| ¡ sâtj	j
‚W n& ty
   tj	 
d|j ¡‚Y n0 |j}	|  |¡\}
}}}}|  |	¡\}}}}}t||d |ƒD ]Ê}|dkrh|t|ƒ }n|dkr~|t|ƒ }|dkr–|t|ƒ }n|dkr¬|t|ƒ }t|ƒ t|ƒ¡}t|ƒ t|ƒ¡}| d	|
 |¡}|	 d	| |¡}tj || j | jj¡| _| j}| | jj ¡s,|  !¡   dS | j"rB| "| jj ¡}| jj# |¡}|du rp| j $¡ }|| jj#|< z"tj% |||| j | j"| jj ¡}W n\ tj	j
y¬   ‚ Y nD tyî   t& '¡ dd
… \}}tj	 
dt|ƒt|ƒf ¡‚Y n0 | (¡ }| )|||d¡} |  *||¡ qJdS )zRProcess one line containing the GENERATE statement from a DNS
        master file.NTr†   rƒ   r„   é   r–   ú-z$%sr…   zcaught exception %s: %s)+ru   r   rt   r1   r   Zgranger   r)   rˆ   r‰   rŠ   rŒ   rL   rv   rw   r‹   ry   rx   rp   r{   r   r:   r¡   ÚrangeÚintr   ÚzfillÚreplacer   Ú
idna_codecrz   r!   r   r‚   r   r   r3   rM   r   rŽ   r?   r=   r   )!r   r   ÚstartÚstopÚstepZlhsrL   r   r>   ZrhsZlmodZlsignZloffsetZlwidthZlbaseZrmodZrsignZroffsetZrwidthZrbaseÚiZlindexÚrindexZlzfindexZrzfindexr   rM   re   r‘   r’   r“   r?   rJ   r   r   r	   Ú_generate_lineB  s¾    








ÿ



ÿ

þÿz_MasterReader._generate_linec           	   
   C   sÌ  z>| j  dd¡}| ¡ rr| jdur.| j ¡  t| jƒdkrj| j d¡\| _ | _| _	| _| _
| _| _| _qq>n¶| ¡ r€qn¨| ¡ r˜| j  ¡  qn|jd dkr(|j ¡ }|dkrú| j  ¡ }| ¡ sØtj d¡‚tj |j¡| _d| _| j  ¡  q|dkr4| j  ¡ | _| j  ¡  | jjdu r&| j| j_q|d	krþ| jrþ| j  ¡ }|j}| j  ¡ }| ¡ rŽtj |j| j| j j¡}| j  ¡  n| ¡ s¦tj d
¡‚n| j}| j  | j | j| j	| j| j
| j| j| jf¡ t!|dƒ| _tj" #| j|¡| _ || _q|dkr|  $¡  qtj d| d ¡‚q| j  %|¡ |  &¡  qW nt tjjy´ } zV| j  '¡ \}}|du rrd}tj d|||f ¡}t( )¡ d }| *|¡d‚W Y d}~n
d}~0 0 | j+rÈ| j +¡  dS )zÈRead a DNS master file and build a zone object.

        @raises dns.zone.NoSOA: No SOA RR was found at the zone origin
        @raises dns.zone.NoNS: No NS RRset was found at the zone origin
        TNr   éÿÿÿÿú$z$TTLzbad $TTLz$ORIGINz$INCLUDEzbad origin in $INCLUDEÚrz	$GENERATEzUnknown master file directive 'ú'zsyntax errorz	%s:%d: %sr…   ),rt   r1   Zis_eofr}   rk   rC   r|   Úpopru   rz   rv   rw   rx   ry   Zis_eolZ
is_commentZget_eolr)   Úupperrˆ   r   r‰   rŠ   rL   r   Zget_namer{   r   r~   r   r¨   r€   ÚappendrU   Ú	tokenizerÚ	Tokenizerr®   r‡   r”   Úwherer   rŽ   Úwith_tracebackrn   )	r   r   ÚcÚfilenameZ
new_originZdetailZline_numberÚexÚtbr   r   r	   Úread»  sª    


ù








þÿ
ÿ
ù
ÿ


ÿ
ÿ"z_MasterReader.readN)r   r   r   r   r   r   r‚   r”   r¡   r®   r¾   r   r   r   r	   rr     s    ÿ
a(yrr   TFc	              	   C   sB   |du rd}t jj| ||d}	t|	||||||d}
|
 ¡  |
jS )a6  Build a zone object from a master file format string.

    *text*, a ``str``, the master file format input.

    *origin*, a ``dns.name.Name``, a ``str``, or ``None``.  The origin
    of the zone; if not specified, the first ``$ORIGIN`` statement in the
    masterfile will determine the origin of the zone.

    *rdclass*, an ``int``, the zone's rdata class; the default is class IN.

    *relativize*, a ``bool``, determine's whether domain names are
    relativized to the zone's origin.  The default is ``True``.

    *zone_factory*, the zone factory to use or ``None``.  If ``None``, then
    ``dns.zone.Zone`` will be used.  The value may be any class or callable
    that returns a subclass of ``dns.zone.Zone``.

    *filename*, a ``str`` or ``None``, the filename to emit when
    describing where an error occurred; the default is ``'<string>'``.

    *allow_include*, a ``bool``.  If ``True``, the default, then ``$INCLUDE``
    directives are permitted.  If ``False``, then encoutering a ``$INCLUDE``
    will raise a ``SyntaxError`` exception.

    *check_origin*, a ``bool``.  If ``True``, the default, then sanity
    checks of the origin node will be made by calling the zone's
    ``check_origin()`` method.

    *idna_codec*, a ``dns.name.IDNACodec``, specifies the IDNA
    encoder/decoder.  If ``None``, the default IDNA 2003 encoder/decoder
    is used.

    Raises ``dns.zone.NoSOA`` if there is no SOA RRset.

    Raises ``dns.zone.NoNS`` if there is no NS RRset.

    Raises ``KeyError`` if there is no origin node.

    Returns a subclass of ``dns.zone.Zone``.
    Nz<string>)r¨   )r~   rn   )r   r¶   r·   rr   r¾   r{   )Útextr   r   r   r   r»   r~   rn   r¨   rt   Úreaderr   r   r	   r     s    0þr   c           	   
   C   sf   t  ¡ J}t| tƒr.|du r | }| t| ƒ¡} t| |||||||ƒW  d  ƒ S 1 sX0    Y  dS )aY  Read a master file and build a zone object.

    *f*, a file or ``str``.  If *f* is a string, it is treated
    as the name of a file to open.

    *origin*, a ``dns.name.Name``, a ``str``, or ``None``.  The origin
    of the zone; if not specified, the first ``$ORIGIN`` statement in the
    masterfile will determine the origin of the zone.

    *rdclass*, an ``int``, the zone's rdata class; the default is class IN.

    *relativize*, a ``bool``, determine's whether domain names are
    relativized to the zone's origin.  The default is ``True``.

    *zone_factory*, the zone factory to use or ``None``.  If ``None``, then
    ``dns.zone.Zone`` will be used.  The value may be any class or callable
    that returns a subclass of ``dns.zone.Zone``.

    *filename*, a ``str`` or ``None``, the filename to emit when
    describing where an error occurred; the default is ``'<string>'``.

    *allow_include*, a ``bool``.  If ``True``, the default, then ``$INCLUDE``
    directives are permitted.  If ``False``, then encoutering a ``$INCLUDE``
    will raise a ``SyntaxError`` exception.

    *check_origin*, a ``bool``.  If ``True``, the default, then sanity
    checks of the origin node will be made by calling the zone's
    ``check_origin()`` method.

    *idna_codec*, a ``dns.name.IDNACodec``, specifies the IDNA
    encoder/decoder.  If ``None``, the default IDNA 2003 encoder/decoder
    is used.

    Raises ``dns.zone.NoSOA`` if there is no SOA RRset.

    Raises ``dns.zone.NoNS`` if there is no NS RRset.

    Raises ``KeyError`` if there is no origin node.

    Returns a subclass of ``dns.zone.Zone``.
    N)rR   rS   r   r   rT   rU   r   )	r`   r   r   r   r   r»   r~   rn   rc   r   r   r	   Ú	from_fileQ  s    -

ÿrÁ   c                 C   sÀ   d}| D ]¦}|du rF|r |j }n|jd j}|jd j}||||d}|jD ]`}|j |j¡}	|	sv| ¡ }	|	|j|j< |	 |j|j|j	d¡}
|
 
|j¡ |D ]}|
 |¡ qœqLq|r¼| ¡  |S )a  Convert the output of a zone transfer generator into a zone object.

    *xfr*, a generator of ``dns.message.Message`` objects, typically
    ``dns.query.xfr()``.

    *relativize*, a ``bool``, determine's whether domain names are
    relativized to the zone's origin.  The default is ``True``.
    It is essential that the relativize setting matches the one specified
    to the generator.

    *check_origin*, a ``bool``.  If ``True``, the default, then sanity
    checks of the origin node will be made by calling the zone's
    ``check_origin()`` method.

    Raises ``dns.zone.NoSOA`` if there is no SOA RRset.

    Raises ``dns.zone.NoNS`` if there is no NS RRset.

    Raises ``KeyError`` if there is no origin node.

    Returns a subclass of ``dns.zone.Zone``.
    Nr   rs   T)r   Zanswerr   r   r   r1   r3   r=   r>   r?   Z
update_ttlrL   r   rn   )Zxfrr   r   rn   Úzr±   r   r   rE   ZznodeZzrdsr‘   r   r   r	   Úfrom_xfr‡  s,    
ÿrÃ   )r   rR   rh   rW   r˜   r   Zdns.exceptionr   Zdns.nameZdns.nodeZdns.rdataclassZdns.rdatatypeZ	dns.rdataZdns.rdtypes.ANY.SOAZ	dns.rrsetZdns.tokenizerZdns.ttlZ
dns.granger‰   ZDNSExceptionr   r
   r   r   r   rr   rp   rq   r   rÁ   rÃ   r   r   r   r	   Ú<module>   sL       H   þ
:þ
6