
Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­
<!DOCTYPE html>
<html>
3
‡Of½L  ã               @   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Z
ddlZ
ddlZ
ddlZ
ddlmZmZ ejd/kr�eZnejZejZG dd„ de
jjƒZG d	d
„ d
e
jjƒZdd„ Zdd„ Zdd„ Zdd„ Z dd„ Z!e"edƒrüea#nea#dd„ Z$dd„ Z%dd„ Z&dd„ Z'dd„ Z(d0d"d#„Z)d$d%„ Z*d&d'„ Z+d(d)„ Z,d1d*d+„Z-e
j.j/e
j0j1dd ddd,dddddd!e
j2j3fd-d.„Z4dS )2zTalk to a DNS server.é    )Ú
generatorsNé   )ÚlongÚstring_typesé   c               @   s   e Zd ZdZdS )ÚUnexpectedSourcez=A DNS query response came from an unexpected address or port.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   ú/usr/lib/python3.6/query.pyr   ,   s   r   c               @   s   e Zd ZdZdS )ÚBadResponsez<A DNS query response does not respond to the question asked.N)r   r	   r
   r   r   r   r   r   r   1   s   r   c             C   s   | d krd S t j ƒ |  S d S )N)Útime)Útimeoutr   r   r   Ú_compute_expiration6   s    r   c             C   sj   d}|r|t jO }|r |t jO }|r.|t jO }t jƒ }|j| |ƒ |rZ|jt|d ƒƒ}n|jƒ }t|ƒS )aq  Poll polling backend.
    @param fd: File descriptor
    @type fd: int
    @param readable: Whether to wait for readability
    @type readable: bool
    @param writable: Whether to wait for writability
    @type writable: bool
    @param timeout: Deadline timeout (expiration time, in seconds)
    @type timeout: float
    @return True on success, False on timeout
    r   iè  )ÚselectZPOLLINZPOLLOUTZPOLLERRÚpollÚregisterr   Úbool)ÚfdÚreadableÚwritableÚerrorr   Z
event_maskZpollableZ
event_listr   r   r   Ú	_poll_for=   s    


r   c             C   sr   g g g   }}}|r| g}|r$| g}|r.| g}|dkrLt j |||ƒ\}}	}
nt j ||||ƒ\}}	}
t|pn|	pn|
ƒS )as  Select polling backend.
    @param fd: File descriptor
    @type fd: int
    @param readable: Whether to wait for readability
    @type readable: bool
    @param writable: Whether to wait for writability
    @type writable: bool
    @param timeout: Deadline timeout (expiration time, in seconds)
    @type timeout: float
    @return True on success, False on timeout
    N)r   r   )r   r   r   r   r   ZrsetZwsetZxsetZrcountZwcountZxcountr   r   r   Ú_select_for\   s    r   c             C   s”   d}xŠ|sŽ|d krd }n|t j ƒ  }|dkr4tjj‚yt| ||||ƒsNtjj‚W n6 tk
r† } z|jd tjkrv|‚W Y d d }~X nX d}qW d S )NFg        r   T)	r   ÚdnsÚ	exceptionZTimeoutÚ_polling_backendÚselect_errorÚargsÚerrnoZEINTR)r   r   r   r   Ú
expirationÚdoner   Úer   r   r   Ú	_wait_fory   s    r%   c             C   s   | a dS )z#
    Internal API. Do not use.
    N)r   )Úfnr   r   r   Ú_set_polling_backend‹   s    r'   r   c             C   s   t | ddd|ƒ d S )NTF)r%   )Úsr"   r   r   r   Ú_wait_for_readableœ   s    r)   c             C   s   t | ddd|ƒ d S )NFT)r%   )r(   r"   r   r   r   Ú_wait_for_writable    s    r*   c             C   sD   t jj| |d ƒ}t jj| |d ƒ}||koB|dd … |dd … kS )Nr   r   )r   ÚinetZ	inet_pton)ÚafZa1Za2Zn1Zn2r   r   r   Ú_addresses_equal¤   s    r-   c             C   s`   |sdS t | ||ƒs<tjj|d ƒr@|dd … |dd … kr@dS |rHdS td|› d|› �ƒ‚d S )NTr   r   Fzgot a response from z instead of )r-   r   r+   Zis_multicastr   )r,   Úfrom_addressÚdestinationÚignore_unexpectedr   r   r   Ú_matches_destination­   s    (r1   c             C   sº   | d kr6yt jj|ƒ} W n tk
r4   t jj} Y nX | t jjkrp||f}|d k	sZ|dkr°|d krfd}||f}n@| t jjkr°||ddf}|d k	s˜|dkr°|d kr¤d}||ddf}| ||fS )Nr   z0.0.0.0z::)r   r+   Zaf_for_addressÚ	ExceptionZAF_INETZAF_INET6)r,   ÚwhereÚportÚsourceÚsource_portr/   r   r   r   Ú_destination_and_source½   s$    
r7   é5   Fc
             C   s"  | j ƒ }
t|||||ƒ\}}}t|tjdƒ}d}zât|ƒ}|jdƒ |dk	rV|j|ƒ t||ƒ t	j	ƒ }|j
|
|ƒ xšt||ƒ |jdƒ\}
}t|j|||ƒs qvt	j	ƒ | }y"tjj|
| j| j|d�}||_	W n tk
rì   |	ræwvn‚ Y nX | j|ƒ� rþ|S |	�rqvqvt‚qvW W d|jƒ  X dS )a�  Return the response obtained after sending a query via UDP.

    @param q: the query
    @type q: dns.message.Message
    @param where: where to send the message
    @type where: string containing an IPv4 or IPv6 address
    @param timeout: The number of seconds to wait before the query times out.
    If None, the default, wait forever.
    @type timeout: float
    @param port: The port to which to send the message.  The default is 53.
    @type port: int
    @param af: the address family to use.  The default is None, which
    causes the address family to use to be inferred from the form of where.
    If the inference attempt fails, AF_INET is used.
    @type af: int
    @rtype: dns.message.Message object
    @param source: source address.  The default is the wildcard address.
    @type source: string
    @param source_port: The port from which to send the message.
    The default is 0.
    @type source_port: int
    @param ignore_unexpected: If True, ignore responses from unexpected
    sources.  The default is False.
    @type ignore_unexpected: bool
    @param one_rr_per_rrset: Put each RR into its own RRset
    @type one_rr_per_rrset: bool
    r   Niÿÿ  )ÚkeyringÚrequest_macÚone_rr_per_rrset)Úto_wirer7   Úsocket_factoryÚsocketÚ
SOCK_DGRAMr   ÚsetblockingÚbindr*   r   Zsendtor)   Úrecvfromr1   Zfamilyr   ÚmessageÚ	from_wirer9   Úmacr2   Úis_responser   Úclose)Úqr3   r   r4   r,   r5   r6   r0   r;   Úignore_errorsÚwirer/   r(   Ú
begin_timer"   r.   Úresponse_timeÚrr   r   r   ÚudpÔ   sD    




rN   c             C   sJ   d}x@|dkrDt | |ƒ | j|ƒ}|dkr.t‚|t|ƒ }|| }qW |S )zßRead the specified number of bytes from sock.  Keep trying until we
    either get the desired amount, or we hit EOF.
    A Timeout exception will be raised if the operation is not completed
    by the expiration time.
    ó    r   )r)   ZrecvÚEOFErrorÚlen)ÚsockÚcountr"   r(   Únr   r   r   Ú	_net_read  s    


rU   c             C   s>   d}t |ƒ}x,||k r8t| |ƒ || j||d… ƒ7 }qW dS )z‘Write the specified data to the socket.
    A Timeout exception will be raised if the operation is not completed
    by the expiration time.
    r   N)rQ   r*   Úsend)rR   Údatar"   ZcurrentÚlr   r   r   Ú
_net_write/  s
    

rY   c             C   sp   y| j |ƒ W n\ tjk
rj   tjƒ d d… \}}t|dƒrF|j}n|d }|tjtjtj	gkrf|‚Y nX d S )Né   r!   r   )
Zconnectr>   r   ÚsysÚexc_infoÚhasattrr!   ZEINPROGRESSZEWOULDBLOCKZEALREADY)r(   ZaddressZtyÚvZv_errr   r   r   Ú_connect;  s    
r_   c             C   s  | j ƒ }t|||||ƒ\}}	}t|tjdƒ}
d}z„t|ƒ}|
jdƒ tjƒ }|dk	r^|
j|ƒ t	|
|	ƒ t
|ƒ}tjd|ƒ| }t|
||ƒ t|
d|ƒ}tjd|ƒ\}t|
||ƒ}W d|dkrÄd}ntjƒ | }|
jƒ  X tjj|| j| j|d�}||_| j|ƒ�st‚|S )a  Return the response obtained after sending a query via TCP.

    @param q: the query
    @type q: dns.message.Message object
    @param where: where to send the message
    @type where: string containing an IPv4 or IPv6 address
    @param timeout: The number of seconds to wait before the query times out.
    If None, the default, wait forever.
    @type timeout: float
    @param port: The port to which to send the message.  The default is 53.
    @type port: int
    @param af: the address family to use.  The default is None, which
    causes the address family to use to be inferred from the form of where.
    If the inference attempt fails, AF_INET is used.
    @type af: int
    @rtype: dns.message.Message object
    @param source: source address.  The default is the wildcard address.
    @type source: string
    @param source_port: The port from which to send the message.
    The default is 0.
    @type source_port: int
    @param one_rr_per_rrset: Put each RR into its own RRset
    @type one_rr_per_rrset: bool
    r   Nz!HrZ   )r9   r:   r;   )r<   r7   r=   r>   ÚSOCK_STREAMr   r@   r   rA   r_   rQ   ÚstructÚpackrY   rU   ÚunpackrG   r   rC   rD   r9   rE   rF   r   )rH   r3   r   r4   r,   r5   r6   r;   rJ   r/   r(   rK   r"   rX   ÚtcpmsgÚldatarL   rM   r   r   r   ÚtcpI  s8    



rf   Tc       &      c   sÀ  t |tƒrtjj|ƒ}t |tƒr,tjj|ƒ}tjj|||ƒ}|tjjkrltj	j|dddd| ƒ}|j
j|ƒ |dk	r„|j|||d� |jƒ }t|	| |||ƒ\}	}}|rÊ|tjjkrºtdƒ‚t|	tjdƒ}nt|	tjdƒ}|jdƒ |dk	rô|j|ƒ t|
ƒ}t||ƒ t|ƒ}|�r*t||ƒ |j|ƒ ntjd|ƒ| }t|||ƒ d	}d
}d	}d}|�rj|}tjj}nd}|}d}d
}�x6|�s²t|ƒ} | dk�sª|dk	�r®| |k�r®|} |�rÎt||ƒ |j dƒ\}}!n&t!|d| ƒ}"tj"d|"ƒ\}t!||| ƒ}|tjjk}#tjj#||j$|j%d
||d
||#d�	}$|$j&}d	}d}%|dk�r¼|$j' �sV|$j'd j|k�rbtj(j)dƒ‚|$j'd }|j*tjj+k�rˆtj(j)dƒ‚d}%|j,ƒ }|tjjk�r¼|d j-|k�r¸d
}nd
}xÆ|$j'|%d… D ]´}|�râtj(j)dƒ‚|j*tjj+k�rl|j|k�rl|�r&|d j-|k�r tj(j)dƒ‚d	}n|tjjk�r:| }||k�r~|tjj.k�sf|tjjk�r~|�r~d
}n|�rÌtjj.}d	}�qÌW |�r¨|j$�r¨|$j/ �r¨tj(j)dƒ‚|$V  �q~W |j0ƒ  dS )aƒ  Return a generator for the responses to a zone transfer.

    @param where: where to send the message
    @type where: string containing an IPv4 or IPv6 address
    @param zone: The name of the zone to transfer
    @type zone: dns.name.Name object or string
    @param rdtype: The type of zone transfer.  The default is
    dns.rdatatype.AXFR.
    @type rdtype: int or string
    @param rdclass: The class of the zone transfer.  The default is
    dns.rdataclass.IN.
    @type rdclass: int or string
    @param timeout: The number of seconds to wait for each response message.
    If None, the default, wait forever.
    @type timeout: float
    @param port: The port to which to send the message.  The default is 53.
    @type port: int
    @param keyring: The TSIG keyring to use
    @type keyring: dict
    @param keyname: The name of the TSIG key to use
    @type keyname: dns.name.Name object or string
    @param relativize: If True, all names in the zone will be relativized to
    the zone origin.  It is essential that the relativize setting matches
    the one specified to dns.zone.from_xfr().
    @type relativize: bool
    @param af: the address family to use.  The default is None, which
    causes the address family to use to be inferred from the form of where.
    If the inference attempt fails, AF_INET is used.
    @type af: int
    @param lifetime: The total number of seconds to spend doing the transfer.
    If None, the default, then there is no limit on the time the transfer may
    take.
    @type lifetime: float
    @rtype: generator of dns.message.Message objects.
    @param source: source address.  The default is the wildcard address.
    @type source: string
    @param source_port: The port from which to send the message.
    The default is 0.
    @type source_port: int
    @param serial: The SOA serial number to use as the base for an IXFR diff
    sequence (only meaningful if rdtype == dns.rdatatype.IXFR).
    @type serial: int
    @param use_udp: Use UDP (only meaningful for IXFR)
    @type use_udp: bool
    @param keyalgorithm: The TSIG algorithm to use; defaults to
    dns.tsig.default_algorithm
    @type keyalgorithm: string
    r   ÚINÚSOAz. . %u 0 0 0 0N)Ú	algorithmzcannot do a UDP AXFRz!HFTiÿÿ  rZ   )r9   r:   ÚxfrÚoriginÚtsig_ctxZmultiÚfirstr;   z No answer or RRset not for qnamezfirst RRset is not an SOAr   zanswers after final SOAzIXFR base serial mismatchzmissing TSIG)1Ú
isinstancer   r   ÚnameZ	from_textÚ	rdatatyperC   Z
make_queryZIXFRÚrrsetZ	authorityÚappendZuse_tsigr<   r7   Ú
ValueErrorr=   r>   r?   r`   r@   rA   r   r_   rQ   r*   rV   ra   rb   rY   Úemptyr)   rB   rU   rc   rD   r9   rE   rl   Zanswerr   Ú	FormErrorÚrdtyperh   ÚcopyÚserialÚAXFRZhad_tsigrG   )&r3   Zzonerv   Zrdclassr   r4   r9   ZkeynameZ
relativizer,   Zlifetimer5   r6   rx   Zuse_udpZkeyalgorithmrH   rq   rJ   r/   r(   r"   rX   rd   r#   Zdelete_modeZexpecting_SOAZ	soa_rrsetrk   Zonamerl   rm   Zmexpirationr.   re   Zis_ixfrrM   Zanswer_indexr   r   r   rj   ‰  s¾    5














rj   )r   )Nr8   NNr   FFF)Nr8   NNr   F)5r   Z
__future__r   r!   r   r>   ra   r[   r   Zdns.exceptionr   Zdns.inetZdns.nameZdns.messageZdns.rdataclassZdns.rdatatypeZ_compatr   r   Úversion_infoÚOSErrorr   r   r=   r   ZDNSExceptionr   ru   r   r   r   r   r%   r'   r]   r   r)   r*   r-   r1   r7   rN   rU   rY   r_   rf   rp   ry   Z
rdataclassrg   ZtsigZdefault_algorithmrj   r   r   r   r   Ú<module>   sX   

	 
I 
?

