
                            d Z ddlm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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  G d dej4                  j6                        Z G d	 d
ej4                  j6                        Z G d dej4                  j<                        Z G d dej4                  j6                        Z  G d dej4                  j6                        Z! G d dej4                  j<                        Z"dZ#dZ$dZ%dZ& G d de'      Z( G d de'      Z)	 	 	 	 d!dZ* G d de'      Z+d Z,d Z-ej\                  j^                  ddddddfdZ0	 	 d"d Z1y)#zDNS Messages    )absolute_import)StringION   )longxrangestring_typesc                       e Zd ZdZy)ShortHeaderz2The DNS packet passed to from_wire() is too short.N__name__
__module____qualname____doc__     lib/third_party/dns/message.pyr
   r
   ,   s    <r   r
   c                       e Zd ZdZy)TrailingJunkzEThe DNS packet passed to from_wire() has extra junk at the end of it.Nr   r   r   r   r   r   0   s    Or   r   c                       e Zd ZdZy)UnknownHeaderFieldzVThe header field name was not recognized when converting from text
    into a message.Nr   r   r   r   r   r   4   s    r   r   c                       e Zd ZdZy)BadEDNSzYAn OPT record occurred somewhere other than the start of
    the additional data section.Nr   r   r   r   r   r   9       $r   r   c                       e Zd ZdZy)BadTSIGzWA TSIG record occurred somewhere other than the end of
    the additional data section.Nr   r   r   r   r   r   >   r   r   r   c                       e Zd ZdZy)UnknownTSIGKeyz(A TSIG with an unknown key was received.Nr   r   r   r   r   r   C   s    2r   r         c                   *   e Zd ZdZddZd Zd ZddZd Zd Z	d	 Z
d
 Zd Zej                  j                  dddfdZej                  j                  dddfdZddZdddddej&                  j(                  fdZ	 	 ddZddZd Zd Zd Zd Zy)MessagezA DNS message.Nc                    |$t         j                  j                         | _        n|| _        d| _        g | _        g | _        g | _        g | _        d| _	        d| _
        d| _        g | _        d| _        d | _        d | _        t         j                   j"                  | _        d| _        d| _        d| _        d| _        | j                  | _        d| _        d| _        d | _        d | _        d| _        d| _        d| _        i | _        y )Nr   r   ,  FT) dnsentropy	random_16idflagsquestionanswer	authority
additionaledns	ednsflagspayloadoptionsrequest_payloadkeyringkeynametsigdefault_algorithmkeyalgorithmrequest_mac
other_data
tsig_errorfudgeoriginal_idmacxfrorigintsig_ctxhad_tsigmultifirstindex)selfr(   s     r   __init__zMessage.__init__V   s    :kk++-DGDG
	 HH66
77


r   c                 8    dt        | j                        z   dz   S )Nz<DNS message, ID >)reprr(   rE   s    r   __repr__zMessage.__repr__v   s    "T$'']2S88r   c                 "    | j                         S N)to_textrJ   s    r   __str__zMessage.__str__y   s    ||~r   c                 r   t               }|j                  d| j                  z         |j                  dt        j                  j                  t        j                  j                  | j                              z         t        j                  j                  | j                  | j                        }|j                  dt        j                  j                  |      z         |j                  dt        j                  j                  | j                        z         | j                  dk\  r|j                  d| j                  z         | j                  dk7  r;|j                  dt        j                  j                  | j                        z         |j                  d| j                  z         | j                  D ]$  }|j                  d	|j                         z         & t        j                  j                  | j                        }|r|j                  d
       n|j                  d       | j                  D ]6  }|j                   |j
                  ||fi |       |j                  d       8 |r|j                  d       n|j                  d       | j                   D ]6  }|j                   |j
                  ||fi |       |j                  d       8 |r|j                  d       n|j                  d       | j"                  D ]6  }|j                   |j
                  ||fi |       |j                  d       8 |j                  d       | j$                  D ]6  }|j                   |j
                  ||fi |       |j                  d       8 |j'                         dd S )zConvert the message to text.

        The *origin*, *relativize*, and any other keyword
        arguments are passed to the RRset ``to_wire()`` method.

        Returns a ``text``.
        zid %d
z
opcode %s
z	rcode %s
z	flags %s
r   zedns %s
z
eflags %s
zpayload %d
z
option %s
z;ZONE
z
;QUESTION

z;PREREQ
z;ANSWER
z;UPDATE
z;AUTHORITY
z;ADDITIONAL
Nr#   )r   writer(   r%   opcoderN   
from_flagsr)   rcoder/   r.   edns_to_textr0   r1   	is_updater*   r+   r,   r-   getvalue)	rE   r?   
relativizekwsrcoptrW   rrsets	            r   rN   zMessage.to_text|   s    J	
TWW$%	

""3::#8#8#DEF 	GYY!!$**dnn=			 1 1" 556			 1 1$** ==>99>GGL499,-~~"		..t~~>? @GGOdll23<<CGGNS[[]23  JJ((4	GGJGGN#]]EGGMEMM&*;;<GGEN # GGL!GGL![[EGGMEMM&*;;<GGEN ! GGL!GGO$^^EGGMEMM&*;;<GGEN $ 	
 !__EGGMEMM&*;;<GGEN % zz|CR  r   c                    t        |t              sy| j                  |j                  k7  ry| j                  |j                  k7  ry| j                  D ]  }||j                  vs y |j                  D ]  }|| j                  vs y | j
                  D ]  }||j
                  vs y |j
                  D ]  }|| j
                  vs y | j                  D ]  }||j                  vs y |j                  D ]  }|| j                  vs y y)zTwo messages are equal if they have the same content in the
        header, question, answer, and authority sections.

        Returns a ``bool``.
        FT)
isinstancer!   r(   r)   r*   r+   r,   rE   otherns      r   __eq__zMessage.__eq__   s     %)77ehh::$A&  A%   A$  A#  A'   A& ! r   c                 &    | j                  |       S rM   )rd   )rE   rb   s     r   __ne__zMessage.__ne__   s    ;;u%%%r   c                    |j                   t        j                   j                  z  dk(  sl| j                  |j                  k7  sSt        j                  j                  | j                         t        j                  j                  |j                         k7  ryt        j                  j                  |j                   |j                        t        j                  j                  k7  ryt        j                  j                  | j                         ry| j                  D ]  }||j                  vs y |j                  D ]  }|| j                  vs y y)zLIs this message a response to *other*?

        Returns a ``bool``.
        r   FT)r)   r%   QRr(   rS   rT   rU   r/   NOERRORrW   r*   ra   s      r   is_responsezMessage.is_response   s     ;;%*77ehh::  ,::  -.99U__=		!!"::

+A&  A%   r   c                     || j                   u rt        S || j                  u rt        S || j                  u rt
        S || j                  u rt        S t        d      aq  Return the "section number" of the specified section for use
        in indexing.  The question section is 0, the answer section is 1,
        the authority section is 2, and the additional section is 3.

        *section* is one of the section attributes of this message.

        Raises ``ValueError`` if the section isn't known.

        Returns an ``int``.
        zunknown section)	r*   QUESTIONr+   ANSWERr,   	AUTHORITYr-   
ADDITIONAL
ValueError)rE   sections     r   section_numberzMessage.section_number   sV     dmm#O#M&'.//r   c                     |t         k(  r| j                  S |t        k(  r| j                  S |t        k(  r| j
                  S |t        k(  r| j                  S t        d      rl   )	rm   r*   rn   r+   ro   r,   rp   r-   rq   )rE   numbers     r   section_from_numberzMessage.section_from_number  sX     X== v;;y >>!z!??".//r   Fc	                    t        |t              r|}	| j                  |	      }n| j                  |      }	|	|||||f}
|sK| j                  | j                  j                  |
      }|"|S |D ]  }|j                  |||||      s|c S  |st        t        j                  j                  |||||      }|j                  |       | j                  || j                  |
<   |S )aP  Find the RRset with the given attributes in the specified section.

        *section*, an ``int`` section number, or one of the section
        attributes of this message.  This specifies the
        the section of the message to search.  For example::

            my_message.find_rrset(my_message.answer, name, rdclass, rdtype)
            my_message.find_rrset(dns.message.ANSWER, name, rdclass, rdtype)

        *name*, a ``dns.name.Name``, the name of the RRset.

        *rdclass*, an ``int``, the class of the RRset.

        *rdtype*, an ``int``, the type of the RRset.

        *covers*, an ``int`` or ``None``, the covers value of the RRset.
        The default is ``None``.

        *deleting*, an ``int`` or ``None``, the deleting value of the RRset.
        The default is ``None``.

        *create*, a ``bool``.  If ``True``, create the RRset if it is not found.
        The created RRset is appended to *section*.

        *force_unique*, a ``bool``.  If ``True`` and *create* is also ``True``,
        create a new RRset regardless of whether a matching RRset exists
        already.  The default is ``False``.  This is useful when creating
        DDNS Update messages, as order matters for them.

        Raises ``KeyError`` if the RRset was not found and create was
        ``False``.

        Returns a ``dns.rrset.RRset object``.
        )r`   intrv   rs   rD   getmatchKeyErrorr%   r^   RRsetappend)rE   rr   namerdclassrdtypecoversdeletingcreateforce_uniquers   keyr^   s               r   
find_rrsetzMessage.find_rrset  s    L gs#$N..~>G!009NtWffhGzz%

s+$ L$E{{4&&(K$ % N		gvvxHu::!#DJJsOr   c	           
      \    	 | j                  ||||||||      }	|	S # t        $ r d}	Y |	S w xY w)a5  Get the RRset with the given attributes in the specified section.

        If the RRset is not found, None is returned.

        *section*, an ``int`` section number, or one of the section
        attributes of this message.  This specifies the
        the section of the message to search.  For example::

            my_message.get_rrset(my_message.answer, name, rdclass, rdtype)
            my_message.get_rrset(dns.message.ANSWER, name, rdclass, rdtype)

        *name*, a ``dns.name.Name``, the name of the RRset.

        *rdclass*, an ``int``, the class of the RRset.

        *rdtype*, an ``int``, the type of the RRset.

        *covers*, an ``int`` or ``None``, the covers value of the RRset.
        The default is ``None``.

        *deleting*, an ``int`` or ``None``, the deleting value of the RRset.
        The default is ``None``.

        *create*, a ``bool``.  If ``True``, create the RRset if it is not found.
        The created RRset is appended to *section*.

        *force_unique*, a ``bool``.  If ``True`` and *create* is also ``True``,
        create a new RRset regardless of whether a matching RRset exists
        already.  The default is ``False``.  This is useful when creating
        DDNS Update messages, as order matters for them.

        Returns a ``dns.rrset.RRset object`` or ``None``.
        N)r   r{   )
rE   rr   r~   r   r   r   r   r   r   r^   s
             r   	get_rrsetzMessage.get_rrset\  sJ    J	OOGT7FF$,flDE   	E	s    ++r   c           
         |dk(  r| j                   dk7  r| j                   }nd}|dk  rd}n|dkD  rd}t        j                  j                  | j                  | j
                  ||      }| j                  D ]3  }|j                  |j                  |j                  |j                         5 | j                  D ].  } |j                  t        j                  j                  |fi | 0 | j                  D ].  } |j                  t        j                  j                  |fi | 0 | j                   dk\  r<|j#                  | j                   | j$                  | j&                  | j(                         | j*                  D ].  } |j                  t        j                  j,                  |fi | 0 |j/                          | j0                  |j3                  | j0                  | j4                  | j0                     | j6                  | j8                  | j:                  | j<                  | j>                  | j@                         |jB                  | _!        |jE                         S )a1  Return a string containing the message in DNS compressed wire
        format.

        Additional keyword arguments are passed to the RRset ``to_wire()``
        method.

        *origin*, a ``dns.name.Name`` or ``None``, the origin to be appended
        to any relative names.

        *max_size*, an ``int``, the maximum size of the wire format
        output; default is 0, which means "the message's request
        payload, if nonzero, or 65535".

        Raises ``dns.exception.TooBig`` if *max_size* was exceeded.

        Returns a ``binary``.
        r   i  i   )#r2   r%   rendererRendererr(   r)   r*   add_questionr~   r   r   r+   	add_rrsetrn   r,   ro   r.   add_ednsr/   r0   r1   r-   rp   write_headerr4   add_tsigr3   r;   r<   r:   r9   r8   r7   r=   get_wire)rE   r?   max_sizerZ   rr^   s         r   to_wirezMessage.to_wire  s   & q=##q(// c>HHLL!!$''4::xH]]ENN5::u||U]]C #[[EAKK++U9b9 !^^EAKK..<< $99>JJtyy$..$,,M__EAKK//="= %	<<#JJt||T\\$,,%?zz4#3#3T__(8(8((* uuDHzz|r   r$   r   c                 H   || _         |,t        | j                   j                               d   | _        n6t	        |t
              rt        j                  j                  |      }|| _        || _	        || _
        || j                  | _        n|| _        || _        || _        y)aL  When sending, a TSIG signature using the specified keyring
        and keyname should be added.

        See the documentation of the Message class for a complete
        description of the keyring dictionary.

        *keyring*, a ``dict``, the TSIG keyring to use.  If a
        *keyring* is specified but a *keyname* is not, then the key
        used will be the first key in the *keyring*.  Note that the
        order of keys in a dictionary is not defined, so applications
        should supply a keyname when a keyring is used, unless they
        know the keyring contains only one key.

        *keyname*, a ``dns.name.Name`` or ``None``, the name of the TSIG key
        to use; defaults to ``None``. The key must be defined in the keyring.

        *fudge*, an ``int``, the TSIG time fudge.

        *original_id*, an ``int``, the TSIG original id.  If ``None``,
        the message's id is used.

        *tsig_error*, an ``int``, the TSIG error code.

        *other_data*, a ``binary``, the TSIG other data.

        *algorithm*, a ``dns.name.Name``, the TSIG algorithm to use.
        Nr   )r3   listkeysr4   r`   r   r%   r~   	from_textr7   r;   r(   r<   r:   r9   )rE   r3   r4   r;   r<   r:   r9   	algorithms           r   use_tsigzMessage.use_tsig  s    > ? 1 1 34Q7DL'<0((,,W5"DL%
#wwD*D$$r   c                     ||du rd}|du rd}||}|dk  r	d}d}d}g }n|t        d      z  }||dz  z  }|g }|| _        || _        || _        || _        || _        y)a6  Configure EDNS behavior.

        *edns*, an ``int``, is the EDNS level to use.  Specifying
        ``None``, ``False``, or ``-1`` means "do not use EDNS", and in this case
        the other parameters are ignored.  Specifying ``True`` is
        equivalent to specifying 0, i.e. "use EDNS0".

        *ednsflags*, an ``int``, the EDNS flag values.

        *payload*, an ``int``, is the EDNS sender's payload field, which is the
        maximum size of UDP datagram the sender can handle.  I.e. how big
        a response to this message can be.

        *request_payload*, an ``int``, is the EDNS payload size to use when
        sending this message.  If not specified, defaults to the value of
        *payload*.

        *options*, a list of ``dns.edns.Option`` objects or ``None``, the EDNS
        options.
        NFr#   Tr   l   ~    )r   r.   r/   r0   r1   r2   )rE   r.   r/   r0   r2   r1   s         r   use_ednszMessage.use_edns  s    . <45=D4<D"%O!8IGOG j))I$"*%I	".r   c                    |rM| j                   dk  r| j                          | xj                  t        j                  j
                  z  c_        y| j                   dk\  r/| xj                  t        j                  j
                   z  c_        yy)a!  Enable or disable 'DNSSEC desired' flag in requests.

        *wanted*, a ``bool``.  If ``True``, then DNSSEC data is
        desired in the response, EDNS is enabled if required, and then
        the DO bit is set.  If ``False``, the DO bit is cleared if
        EDNS is enabled.
        r   N)r.   r   r/   r%   r)   DO)rE   wanteds     r   want_dnsseczMessage.want_dnssec  sY     yy1}NNciill*NYY!^NNsyy||m+N r   c                 j    t         j                  j                  | j                  | j                        S )z7Return the rcode.

        Returns an ``int``.
        )r%   rU   rT   r)   r/   rJ   s    r   rU   zMessage.rcode%  s#    
 yy##DJJ??r   c                 P   t         j                  j                  |      \  }}| xj                  dz  c_        | xj                  |z  c_        | xj                  t        d      z  c_        | xj                  |z  c_        | j                  dk7  r| j                  dk  rd| _        yyy)zJSet the rcode.

        *rcode*, an ``int``, is the rcode to set.
        i  i r   N)r%   rU   to_flagsr)   r/   r   r.   )rE   rU   valueevalues       r   	set_rcodezMessage.set_rcode,  s~    
 )),,U3

f


e
$z**& >>Q499q=DI $1r   c                 T    t         j                  j                  | j                        S )z8Return the opcode.

        Returns an ``int``.
        )r%   rS   rT   r)   rJ   s    r   rS   zMessage.opcode9  s    
 zz$$TZZ00r   c                     | xj                   dz  c_         | xj                   t        j                  j                  |      z  c_         y)zMSet the opcode.

        *opcode*, an ``int``, is the opcode to set.
        i  N)r)   r%   rS   r   )rE   rS   s     r   
set_opcodezMessage.set_opcode@  s/    
 	

f


cjj))&11
r   rM   )NT)Nr   )r   r   i   NN)T)r   r   r   r   rF   rK   rO   rN   rd   rf   rj   rs   rv   r%   	rdatatypeNONEr   r   r   r5   r6   r   r   r   rU   r   rS   r   r   r   r   r!   r!   S   s    @97!rB&00.00 --,,tE %;| ++d5$*X.` )-C!aC8855-%^ KO,/\, @12r   r!   c                   .    e Zd ZdZ	 	 ddZd Zd Zd Zy)_WireReadera  Wire format reader.

    wire: a binary, is the wire-format message.
    message: The message object being built
    current: When building a message object from wire format, this
    variable contains the offset from the beginning of wire of the next octet
    to be read.
    updating: Is the message a dynamic update?
    one_rr_per_rrset: Put each RR into its own RRset?
    ignore_trailing: Ignore trailing junk at end of request?
    zone_rdclass: The class of the zone in messages which are
    DNS dynamic updates.
    c                     t         j                  j                  |      | _        || _        d| _        d| _        t         j                  j                  | _	        || _
        || _        || _        y )Nr   F)r%   wiredata
maybe_wrapwiremessagecurrentupdating
rdataclassINzone_rdclassquestion_onlyone_rr_per_rrsetignore_trailing)rE   r   r   r   r   r   s         r   rF   z_WireReader.__init__Y  sW    LL++D1	NN--* 0.r   c           	         | j                   r|dkD  rt        j                  j                  t	        d|      D ]$  }t        j
                  j                  | j                  | j                        \  }}| j                  j                  %|j                  | j                  j                        }| j                  |z   | _        t        j                  d| j                  | j                  | j                  dz          \  }}| j                  dz   | _        | j                  j                  | j                  j                  |||dd       | j                   s|| _        ' y)zhRead the next *qcount* records from the wire data and add them to
        the question section.
        r   r   N!HH   Tr   r   )r   r%   	exception	FormErrorr   r~   	from_wirer   r   r   r?   rY   structunpackr   r*   r   )rE   qcountiqnameusedr   r   s          r   _get_questionz_WireReader._get_questiond  s   
 ==VaZ--)))6"AHH..tyy$,,GMUD||"".(()<)<=<<$.DLe"iiT\\A5EFH VW  <<!+DLLL##DLL$9$95$+VD15 $ 7 }}$+! #r   c                 ~   | j                   s| j                  rd}nd}d}t        d|      D ]  }| j                  }t        j
                  j                  | j                  | j                        \  }}|}	| j                  j                  %|j                  | j                  j                        }| j                  |z   | _        t        j                  d| j                  | j                  | j                  dz          \  }
}}}| j                  dz   | _        |
t        j                  j                  k(  r|| j                  j                  us|rt         || j                  _        || j                  _        |dz  dz	  | j                  _        g | j                  _        | j                  }|}|dkD  rt        j                  d	| j                  ||d
z          \  }}|d
z   }t        j&                  j+                  || j                  ||      }| j                  j(                  j-                  |       ||z   }|d
z
  |z
  }|dkD  rd}n_|
t        j                  j.                  k(  r|| j                  j                  u r||dz
  k(  st0        | j                  j2                  t5        d      | j                  j2                  j7                  |	      }|t5        d|z        |	| j                  _        t        j:                  j=                  | j                  | j                  |      \  | j                  _        | j                  _         t        j:                  jC                  | j                  |	|tE        tG        jF                               | j                  jH                  || j                  || j                  jJ                  | j                  jL                  | j                  jN                        | j                  _%        d| j                  _(        n|dk  rd}| j                   rI|t        jR                  jT                  k(  s|t        jR                  jV                  k(  r|}| jX                  }nd}|t        jR                  jT                  k(  s5|t        jR                  jV                  k(  r5|| j                  jZ                  u rt        j                  jV                  }d}n\t        j\                  j                  ||
| j                  | j                  || j                  j                        }|j_                         }| j                  j`                  r|
t        j                  jb                  k(  rd}| j                  je                  ||||
||d|      }||jg                  ||       | j                  |z   | _         y)zRead the next I{count} records from the wire data and add them to
        the specified section.

        section: the section of the message to which to add records
        count: the number of records to read
        TFr   Nz!HHIH
   i   r   r   r   r   z"got signed message without keyringzkey '%s' unknown)4r   r   r   r   r%   r~   r   r   r   r?   rY   r   r   r   OPTr-   r   r0   r/   r.   r1   option_from_wirer}   TSIGr   r3   r   ry   r4   r5   get_algorithm_and_macr7   r=   validaterx   timer8   r@   rB   rC   rA   r   ANYr   r   r+   rdatar   r>   SOAr   add)rE   rr   countr   seen_optr   rr_startr~   r   absolute_namer   r   ttlrdlenr   optslenotypeolenr]   secretr   r   rdr^   s                           r   _get_sectionz_WireReader._get_section{  s    ==D11L L5!A||H88--diiFLT4 M||"".t||':':;<<$.DLg"iiT\\B5FGI *VWc5  <<",DL***$,,"9"99X!M'.$),&%(8^$:!')$,,ke&*ii!&DF "UD &kG((33tyy'49CLL((//4%nG%kD0G k  3==---4<<#:#::eai(!M<<''/()MNN--11-@>();d)BCC'4$HH22499dll38: >*DLL,< HH%%dii&3&,&)$))+&6&*ll&>&>&.&*ll&+&*ll&;&;&*ll&8&8&*ll&8&8
: % )-%7C==s~~1113>>#6#66&H"//G#Hs~~111 3 334<<#6#66 ]]//FB,,Wfdii-1\\5-1\\-@-@BB  YY[F<<###--2C2C(C#'L//0708$N >IIb#&<<%/DLi "r   c                    t        | j                        }|dk  rt        t        j                  d| j                  dd       \  | j
                  _        | j
                  _        }}}}d| _        t        j                  j                  | j
                  j                        rd| _        | j                  |       | j                  ry| j                  | j
                  j                   |       | j                  | j
                  j"                  |       | j                  | j
                  j$                  |       | j&                  s| j                  |k7  rt(        | j
                  j*                  r^| j
                  j,                  rG| j
                  j.                  s0| j
                  j,                  j1                  | j                         yyyy)zNRead a wire format DNS message and build a dns.message.Message
        object.   z!HHHHHHNT)lenr   r
   r   r   r   r(   r)   r   r%   rS   rW   r   r   r   r   r+   r,   r-   r   r   rB   r@   rA   update)rE   lr   ancountaucountadcounts         r   readz_WireReader.read  sP    		Nr6#]]9diinE	$,,,fg	':: 2 23 DM6"$,,--w7$,,00':$,,117;##(9<<$,,"7"7LL))LL!!((3 * #8r   N)FFF)r   r   r   r   rF   r   r   r   r   r   r   r   r   I  s%     5:9>	/,.a0F4r   r   Fc                     t        d      }||_        ||_        ||_        ||_        ||_        ||_        ||_        t        | |||	|
      }|j                          |S )a  Convert a DNS wire format message into a message
    object.

    *keyring*, a ``dict``, the keyring to use if the message is signed.

    *request_mac*, a ``binary``.  If the message is a response to a
    TSIG-signed request, *request_mac* should be set to the MAC of
    that request.

    *xfr*, a ``bool``, should be set to ``True`` if this message is part of
    a zone transfer.

    *origin*, a ``dns.name.Name`` or ``None``.  If the message is part
    of a zone transfer, *origin* should be the origin name of the
    zone.

    *tsig_ctx*, a ``hmac.HMAC`` objext, the ongoing TSIG context, used
    when validating zone transfers.

    *multi*, a ``bool``, should be set to ``True`` if this message
    part of a multiple message sequence.

    *first*, a ``bool``, should be set to ``True`` if this message is
    stand-alone, or the first message in a multi-message sequence.

    *question_only*, a ``bool``.  If ``True``, read only up to
    the end of the question section.

    *one_rr_per_rrset*, a ``bool``.  If ``True``, put each RR into its
    own RRset.

    *ignore_trailing*, a ``bool``.  If ``True``, ignore trailing
    junk at end of the message.

    Raises ``dns.message.ShortHeader`` if the message is less than 12 octets
    long.

    Raises ``dns.messaage.TrailingJunk`` if there were octets in the message
    past the end of the proper DNS message, and *ignore_trailing* is ``False``.

    Raises ``dns.message.BadEDNS`` if an OPT record was in the
    wrong section, or occurred more than once.

    Raises ``dns.message.BadTSIG`` if a TSIG record was not the last
    record of the additional data section.

    Returns a ``dns.message.Message``.
    r   )r(   )
r!   r3   r8   r>   r?   r@   rB   rC   r   r   )r   r3   r8   r>   r?   r@   rB   rC   r   r   r   mreaders                r   r   r     sc    j 	1AAIAMAEAHAJAGAGq-1A(*F
KKMHr   c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)_TextReadera/  Text format reader.

    tok: the tokenizer.
    message: The message object being built.
    updating: Is the message a dynamic update?
    zone_rdclass: The class of the zone in messages which are
    DNS dynamic updates.
    last_name: The most recently read name when building a message object.
    c                     || _         t        j                  j                  |      | _        d | _        t        j                  j                  | _        d| _	        y )NF)
r   r%   	tokenizer	Tokenizertok	last_namer   r   r   r   )rE   textr   s      r   rF   z_TextReader.__init__H  s?    ==**40NN--r   c                 t   | j                   j                         }|j                  }|dk(  r+| j                   j                         | j                  _        nH|dk(  r	 | j                   j                         }|j                         s| j                   j                  |       nP| j                  j                  t        j                  j                  |j                        z  | j                  _        t        j                  j                  | j                  j                        rxd| _        no|dk(  rj| j                   j                         | j                  _        | j                  j                  | j                  j                  dz  z  | j                  _        n |dk(  r| j                  j                  dk  rd| j                  _        	 | j                   j                         }|j                         s| j                   j                  |       n| j                  j                  t        j                  j!                  |j                        z  | j                  _        |dk(  rT| j                   j                         | j                  _        | j                  j                  dk  rd| j                  _        n|d	k(  r}| j                   j%                         }| j                  j                  t        j                  j'                  t        j                  j                  |            z  | j                  _        n^|d
k(  rS| j                   j%                         }| j                  j)                  t        j*                  j                  |             nt,        | j                   j/                          y)z5Process one line from the text format header section.r(   r)   Tr.   r   eflagsr   r0   rS   rU   N)r   ry   r   get_intr   r(   is_identifierungetr)   r%   r   rS   rW   r   r.   r/   edns_from_textr0   
get_stringr   r   rU   r   get_eol)rE   rr   tokenwhatr   s        r   _header_linez_TextReader._header_lineO  s    {{4<"hh..0DLLOW_**,HHNN5)%)\\%7%7II''4&5"  zz##DLL$6$67 $V^ $ 0 0 2DLL%)\\%;%;""b(&*DLL"X||  1$$%!**,HHNN5))-)?)?II,,U[[9*:&  Y#'88#3#3#5DLL ||  1$$%!X88&&(D!%!3!3

##CJJ$8$8$>?"@DLLW_88&&(DLL""399#6#6t#<=$$r   c                    | j                   j                  d      }|j                         s/t        j                  j                  |j                  d      | _        | j                  }| j                   j                         }|j                         st        j                  j                  	 t        j                  j                  |j                        }| j                   j                         }|j                         st        j                  j                  	 t        j                  j                  |j                        }| j                  j!                  | j                  j"                  |||dd       | j$                  r|| _        | j                   j)                          y# t        j                  j                  $ r t        j                  j                  t        $ r t        j                  j                  }Y w xY w)z7Process one line from the text format question section.Twant_leadingNr   )r   ry   is_whitespacer%   r~   r   r   r   r   r   SyntaxErrorr   	Exceptionr   r   r   r   r*   r   r   r   )rE   rr   r   r~   r   r   s         r   _question_linez_TextReader._question_line}  sm    $/""$ XX//TBDN~~""$--+++	(nn..u{{;GHHLLNE&&(mm/// ) ((5 5 5t '-1 	  	3 == 'D }}(( 	,--+++ 	(nn''G	(s   -A-F' 'AHHc           
         d}| j                   j                  d      }|j                         s/t        j                  j                  |j                  d      | _        | j                  }| j                   j                         }|j                         st        j                  j                  	 t        |j                  d      }| j                   j                         }|j                         st        j                  j                  	 	 t        j                  j                  |j                        }| j                   j                         }|j                         st        j                  j                  |t        j                  j                  k(  s|t        j                  j                  k(  r|}| j                   }t        j$                  j                  |j                        }| j                   j                         }|j'                         sX| j                   j)                  |       t        j*                  j                  ||| j                   d      }|j-                         }	nd}t        j$                  j                  }	| j.                  j1                  |||||	|d| j2                        }
||
j5                  ||       yy# t        j                  j                  $ r t        j                  j                  t        $ r d}Y w xY w# t        j                  j                  $ r t        j                  j                  t        $ r t        j                  j"                  }Y w xY w)zfProcess one line from the text format answer, authority, or
        additional data sections.
        NTr  r   )r   ry   r  r%   r~   r   r   r   r   r   r  rx   r  r   r   r   r   r   r   is_eol_or_eofr   r   r   r   r   r   r   )rE   rr   r   r   r~   r   r   r   r   r   r^   s              r   _rr_linez_TextReader._rr_line  sy   
 $/""$ XX//TBDN~~""$--+++	ekk1%CHHLLNE&&(mm/// )	(nn..u{{;GHHLLNE&&(mm///#..,,,3>>;N;N0N"++ ((5""$HHNN5!$$WfdhhEBYY[FB]]''F''(/(0$G >IIb# = }}(( 	,--+++ 	C	 }}(( 	,--+++ 	(nn''G	(s(   /AK	 B5L 	ALLAM.-M.c                    | j                   }d}	 | j                  j                  dd      }|j                         ry|j	                         r|j
                  j                         }|dk(  r| j                   }n|dk(  s|dk(  r#| j                  }| j                  j                  }n|dk(  s|dk(  r#| j                  }| j                  j                  }nT|dk(  s|d	k(  r#| j                  }| j                  j                  }n'|d
k(  r"| j                  }| j                  j                  }| j                  j                          4| j                  j                  |        ||       X)zNRead a text format DNS message and build a dns.message.Message
        object.NTHEADERrm   ZONErn   PREREQro   UPDATErp   )r   r   ry   r	  
is_commentr   upperr  r   r*   r
  r+   r,   r-   r   r   )rE   line_methodrr   r   us        r   r   z_TextReader.read  s,    ''HHLLt,E""$!KK%%'="&"3"3K*_V"&"5"5K"ll33G(]a8m"&--K"ll11G+%h"&--K"ll44G,&"&--K"ll55G  "HHNN5! / r   N)	r   r   r   r   rF   r   r  r
  r   r   r   r   r   r   <  s"    ,\:3j!r   r   c                 R    t               }t        | |      }|j                          |S )a)  Convert the text format message into a message object.

    *text*, a ``text``, the text format message.

    Raises ``dns.message.UnknownHeaderField`` if a header is unknown.

    Raises ``dns.exception.SyntaxError`` if the text is badly formed.

    Returns a ``dns.message.Message object``
    )r!   r   r   )r   r   r   s      r   r   r     s%      		Aq!F
KKMHr   c                     t         }d}t        | |      rt        | |      } d}nd}	 t        |       }|r| j	                          |S # |r| j	                          w w xY w)ac  Read the next text format message from the specified file.

    *f*, a ``file`` or ``text``.  If *f* is text, it is treated as the
    pathname of a file to open.

    Raises ``dns.message.UnknownHeaderField`` if a header is unknown.

    Raises ``dns.exception.SyntaxError`` if the text is badly formed.

    Returns a ``dns.message.Message object``
    rUTF)r   r`   openr   close)fstr_typeopts
want_closer   s        r   	from_filer    sa     HD!XDM

aLGGIH GGI s   A Ac	                 |   t        | t              rt        j                  j	                  |       } t        |t              rt        j
                  j	                  |      }t        |t              rt        j                  j	                  |      }t               }	|	xj                  t        j                  j                  z  c_        |	j                  |	j                  | ||dd       i }
|	||
d<   |d}|	||
d<   |d}|	||
d<   |d}|	||
d<   |d}||
d<    |	j                  d	i |
 |	j                  |       |	S )
a  Make a query message.

    The query name, type, and class may all be specified either
    as objects of the appropriate type, or as strings.

    The query will have a randomly chosen query id, and its DNS flags
    will be set to dns.flags.RD.

    qname, a ``dns.name.Name`` or ``text``, the query name.

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

    *rdclass*, an ``int`` or ``text``,  the desired rdata class; the default
    is class IN.

    *use_edns*, an ``int``, ``bool`` or ``None``.  The EDNS level to use; the
    default is None (no EDNS).
    See the description of dns.message.Message.use_edns() for the possible
    values for use_edns and their meanings.

    *want_dnssec*, a ``bool``.  If ``True``, DNSSEC data is desired.

    *ednsflags*, an ``int``, the EDNS flag values.

    *payload*, an ``int``, is the EDNS sender's payload field, which is the
    maximum size of UDP datagram the sender can handle.  I.e. how big
    a response to this message can be.

    *request_payload*, an ``int``, is the EDNS payload size to use when
    sending this message.  If not specified, defaults to the value of
    *payload*.

    *options*, a list of ``dns.edns.Option`` objects or ``None``, the EDNS
    options.

    Returns a ``dns.message.Message``
    Tr   r/   r   r0   r2   r1   r.   r   )r`   r   r%   r~   r   r   r   r!   r)   RDr   r*   r   r   )r   r   r   r   r   r/   r0   r2   r1   r   kwargss              r   
make_queryr!  %  s=   R %&""5)&,'((0'<(..**73	AGGsyy||GLLUGVD"  $
 F'{H#yH"$3 !H#yHF6NAJJMM+Hr   c           	      @   | j                   t        j                   j                  z  rt        j                  j	                  d      t        j
                  j                  | j                        }t        j                   j                  | j                   t        j                   j                  z  z  |_         |r-|xj                   t        j                   j                  z  c_         |j                  | j                                t        | j                        |_        | j                  dk\  r|j                  dd|| j                          | j"                  rF|j%                  | j&                  | j(                  |ddd| j*                         | j,                  |_        |S )a  Make a message which is a response for the specified query.
    The message returned is really a response skeleton; it has all
    of the infrastructure required of a response, but none of the
    content.

    The response's question section is a shallow copy of the query's
    question section, so the query's question RRsets should not be
    changed.

    *query*, a ``dns.message.Message``, the query to respond to.

    *recursion_available*, a ``bool``, should RA be set in the response?

    *our_payload*, an ``int``, the payload size to advertise in EDNS
    responses.

    *fudge*, an ``int``, the TSIG time fudge.

    Returns a ``dns.message.Message`` object.
    z&specified query message is not a queryr   Nr   )r)   r%   rh   r   r   r   r!   r(   r  RAr   rS   r   r*   r.   r   r0   rA   r   r3   r4   r7   r=   r8   )queryrecursion_availableour_payloadr;   responses        r   make_responser(  r  s   . {{SYY\\!mm%%&NOO{{""588,HYY\\U[[399<<%?@HN#)),,&'U^^,HzzQ!QU]];~~%--tQ,,	.$yyOr   )
Nr   FNNFTFFF)Fi    r$   )2r   
__future__r   ior   r   r   dns.ednsr%   dns.exception	dns.flagsdns.name
dns.opcodedns.entropy	dns.rcode	dns.rdatadns.rdataclassdns.rdatatype	dns.rrsetdns.rendererdns.tsigdns.wiredata_compatr   r   r   r   r   r
   r   DNSExceptionr   r   r   r   rm   rn   ro   rp   objectr!   r   r   r   r   r  r   r   r!  r(  r   r   r   <module>r<     si  $  &                  / /=#--)) =P3==** P33 
$cmm%% $
$cmm%% $
3S]]// 3
  
 	 
s2f s2lk4& k4\ FJ0449#BJp!& p!f0< '*nn&7&7$ D$#TJZ AE%r   