o
    g0                  !   @   s  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mZmZmZmZ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 Zdd Z ej!"dZ#e#rddl$Z%ddl&Z&e%j'Z(e%j)j*j+Z,G d	d
 d
e(Z-G dd de&j.Z/nG dd dZ/e#Z0zddl1Z1W n e2y   G dd dZ1Y nw ejZ3G dd dej4j5Z6G dd dej4j7Z8G dd dej4j5Z9G dd dej4j5Z:ej;j<Z<dd Z=dd Z>dd Z?e@edrejAaBnejCaBdd  ZDd!d" ZEd#d$ ZFd%d& ZG	'dd(d)ZHdd*d+ZIdd,ddd-d-dd.d'dd'dejJfd/ejKjLd0eMd1eeN d2eOd3eeM d4eOd5ePd6ePd7ee d8eMd9ePd:eeM d;eePeMf d<ed= d>eeO d?ejKjLf d@dAZQdBdC ZRdDdE ZS	ddFedGeejKjLeTf dHedIeeN d?eeOeNf f
dJdKZU			-	-		L	-	-	-	ddFedHee dIeeN dMePd5ePdNeeejVjWejXjYf  dOeeT d6ePdPePdQePdReejKjL d?efdSdTZZ		U			-	-	-	-		-dd/ejKjLd0eMd1eeN d2eOd3eeM d4eOdMePd5ePd6ePdPePdFee dQePd?ejKjLfdVdWZ[		U			-	-	-			-dd/ejKjLd0eMd1eeN d2eOd3eeM d4eOdMePd5ePd6ePdXee dYee dQePd?eejKjLePf fdZd[Z\d\d] Z]d^d_ Z^	ddFedGeejKjLeTf dIeeN d?eeOeNf fd`daZ_		-		L	-ddFedIeeN d5ePdNeeejVjWejXjYf  dOeeT d6ePd?eejKjLeNf fdbdcZ`ddde Za		U			-	-	dd/ejKjLd0eMd1eeN d2eOd3eeM d4eOd5ePd6ePdFee d?ejKjLfdfdgZbdhdi ZcdjeeM d;eePeMf d?e1jdfdkdlZe		m			-	-				'dd/ejKjLd0eMd1eeN d2eOd3eeM d4eOd5ePd6ePdFee1jf dnee1jd djeeM d;eePeMf d?ejKjLfdodpZg		m			-	-		'	dd/ejKjLd0eMd1eeN d2eOd3eeM d4eOd5ePd6ePdqeejhji d;eePeMf djeeM d?ejKjLfdrdsZhejjjkejljmddUddd'ddddd-ejXjnfd0eMdteejVjWeMf dueejjjoeMf dveejljpeMf d1eeN d2eOdNeeejVjWejXjYf  dweeejVjWeMf  dxePdyeeN d3eeM d4eOdzeOd{ePd|eejVjWeMf d?ef d}d~Z;G dd dejqZrddUdddderjsfd0eMdejtjudReejKjL d2eOd1eeN dyeeN d3eeM d4eOderd?dfddZvdS )zTalk to a DNS server.    N)AnyDictOptionalTupleUnionc                 C   s,   | d u rd S | t    }|dkrtjj|S )N        )timedns	exceptionTimeout)
expirationtimeout r   V/var/www/html/portale_awareness/portale-venv/lib/python3.10/site-packages/dns/query.py
_remaining/   s   r   c                 C   s   |d u rd S t t |  |S N)minr   )r   r   r   r   r   _expiration_for_this_attempt8   s   r   dohc                       s4   e Zd Z fddZ	dddZ	dddZ  ZS )	_NetworkBackendc                    s&   t    || _|| _|| _|| _d S r   )super__init___local_port	_resolver_bootstrap_address_family)selfresolver
local_portbootstrap_addressfamily	__class__r   r   r   G   s
   

z_NetworkBackend.__init__Nc              	   C   s  g }t |\}}tj|r|| n(| jd ur || j nt|}| j}	|r/tj|}	| j	j
||	|d}
|
 }|D ]G}tj|}|d usO| jdkrZtj|| jf|}nd }t|tj|}td|}zt|tj||f|| t|W   S  ty   Y q>w tj)N)r    lifetimer   g       @)_compute_timesr	   inet
is_addressappendr   r   r   af_for_addressr   resolve_name	addressesr   low_level_address_tuple_make_socketsocketSOCK_STREAMr   _connect_CoreSyncStream	ExceptionhttpcoreConnectError)r   hostportr   local_addresssocket_optionsr*   _r   r    answersaddressafsourcesockattempt_expirationr   r   r   connect_tcpN   sD   


z_NetworkBackend.connect_tcpc                 C      t r   NotImplementedError)r   pathr   r7   r   r   r   connect_unix_socketu   s   z#_NetworkBackend.connect_unix_socketr   )__name__
__module____qualname__r   r?   rD   __classcell__r   r   r!   r   r   F   s    
(r   c                       s*   e Zd Zdddejd fdd
Z  ZS )_HTTPTransportr   N)r   r   r   r    c                   sB   |d u rdd l }|j }t j|i | t||||| j_d S Nr   )dns.resolverr   Resolverr   r   r   _pool_network_backend)r   r   r   r   r    argskwargsr	   r!   r   r   r   {   s   	
z_HTTPTransport.__init__)rE   rF   rG   r-   	AF_UNSPECr   rH   r   r   r!   r   rI   z   s    rI   c                   @   s   e Zd Zdd ZdS )rI   c                 C   r@   r   rA   )r   r4   r5   r   r6   r   r   r   r?      s   z_HTTPTransport.connect_tcpN)rE   rF   rG   r?   r   r   r   r   rI      s    c                   @   sX   e Zd ZdZG dd deZG dd deZG dd dZG dd	 d	Ze	d
d Z
dS )sslr   c                   @      e Zd ZdS )zssl.WantReadExceptionNrE   rF   rG   r   r   r   r   WantReadException       rU   c                   @   rS   )zssl.WantWriteExceptionNrT   r   r   r   r   WantWriteException   rV   rW   c                   @   rS   )zssl.SSLContextNrT   r   r   r   r   
SSLContext   rV   rX   c                   @   rS   )zssl.SSLSocketNrT   r   r   r   r   	SSLSocket   rV   rY   c                 O   s   t d)Nzno ssl support)r1   )clsrO   rP   r   r   r   create_default_context   s   zssl.create_default_contextN)rE   rF   rG   	CERT_NONEr1   rU   rW   rX   rY   classmethodr[   r   r   r   r   rR      s    rR   c                   @      e Zd ZdZdS )UnexpectedSourcez=A DNS query response came from an unexpected address or port.NrE   rF   rG   __doc__r   r   r   r   r_          r_   c                   @   r^   )BadResponsez<A DNS query response does not respond to the question asked.Nr`   r   r   r   r   rc      rb   rc   c                   @   r^   )NoDOHzMDNS over HTTPS (DOH) was requested but the httpx module is not
    available.Nr`   r   r   r   r   rd      rb   rd   c                   @   r^   )NoDOQzNDNS over QUIC (DOQ) was requested but the aioquic module is not
    available.Nr`   r   r   r   r   re      rb   re   c                 C   s$   t   }| d u r|d fS |||  fS r   )r   )r   nowr   r   r   r$      s   r$   c                 C   s   |rt | tjr|  dkrdS t }d}|r|tjO }|r#|tjO }|r+|| | |d u r2d }n|t		  }|dkr@t
jj||sIt
jjd S )Nr   Tr   )
isinstancerR   rY   pending_selector_class	selectors
EVENT_READEVENT_WRITEregisterr   r	   r
   r   select)fdreadablewritabler8   r   seleventsr   r   r   r   	_wait_for   s$   


rt   c                 C   s   | a d S r   )ri   )selector_classr   r   r   _set_selector_class   s   rv   PollSelectorc                 C   s   t | ddd| d S )NTFrt   sr   r   r   r   _wait_for_readable     r{   c                 C   s   t | ddd| d S )NFTrx   ry   r   r   r   _wait_for_writable  r|   r}   c                 C   sb   zt j| |d }t j| |d }W n t jjy    Y dS w ||ko0|dd  |dd  kS )Nr   F   )r	   r%   	inet_ptonr
   SyntaxError)r;   a1a2n1n2r   r   r   _addresses_equal
  s    r   c                 C   s\   |sdS t | ||stj|d r |dd  |dd  kr dS |r$dS td| d| )NTr   r~   Fzgot a response from z instead of )r   r	   r%   is_multicastr_   )r;   from_addressdestinationignore_unexpectedr   r   r   _matches_destination  s   r   Tc                 C   s   d }d }z
t j| }| }W n ty   |r Y nw |r0t j|}|r.||kr-tdn|}|rH|sHzt j|}W n tyG   tdw |rSt j||f|}|r^t j||f|}|||fS )Nz5different address families for source and destinationz3source_port specified but address family is unknown)r	   r%   r(   r1   
ValueError
any_for_afr+   )wherer5   r<   source_portwhere_must_be_addressr;   r   safr   r   r   _destination_and_source&  s<   
r   c                 C   s\   t | |}z|d |d ur|| |r|j|d|dW S |W S  ty-   |   w )NF)do_handshake_on_connectserver_hostname)socket_factorysetblockingbindwrap_socketr1   close)r;   typer<   ssl_contextr   rz   r   r   r   r,   R  s    


r,   i  Fz
/dns-queryqr   r   r5   r<   r   one_rr_per_rrsetignore_trailingsessionrC   postr   verifyr   zdns.resolver.Resolverr    returnc              
   C   s  t st|rt|tjstd|  }t||||d\}}}d}ddi}|durItj	
|rI|tjkr<d|||	}n|tjkrHd|||	}n|}|du rTd}d}n|d }|d	 }t|d
d
|||||d}|rpt|}n	tjd
d
||d}|7}|
r|dtt|d |j||||d}nt|d}| }|j|||d|id}W d   n1 sw   Y  |jdk s|jdkrtd||j|jtjj|j| j| j ||d}|j!" |_#| $|st%|S )a  Return the response obtained after sending a query via DNS-over-HTTPS.

    *q*, a ``dns.message.Message``, the query to send.

    *where*, a ``str``, the nameserver IP address or the full URL. If an IP address is
    given, the URL will be constructed using the following schema:
    https://<IP-address>:<port>/<path>.

    *timeout*, a ``float`` or ``None``, the number of seconds to wait before the query
    times out. If ``None``, the default, wait forever.

    *port*, a ``int``, the port to send the query to. The default is 443.

    *source*, a ``str`` containing an IPv4 or IPv6 address, specifying the source
    address.  The default is the wildcard address.

    *source_port*, an ``int``, the port from which to send the message. The default is
    0.

    *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
    received message.

    *session*, an ``httpx.Client``.  If provided, the client session to use to send the
    queries.

    *path*, a ``str``. If *where* is an IP address, then *path* will be used to
    construct the URL to send the DNS query to.

    *post*, a ``bool``. If ``True``, the default, POST method will be used.

    *bootstrap_address*, a ``str``, the IP address to use to bypass resolution.

    *verify*, a ``bool`` or ``str``.  If a ``True``, then TLS certificate verification
    of the server is done using the default CA bundle; if ``False``, then no
    verification is done; if a `str` then it specifies the path to a certificate file or
    directory which will be used for verification.

    *resolver*, a ``dns.resolver.Resolver`` or ``None``, the resolver to use for
    resolution of hostnames in URLs.  If not specified, a new resolver with a default
    configuration will be used; note this is *not* the default resolver as that resolver
    might have been configured to use DoH causing a chicken-and-egg problem.  This
    parameter only has an effect if the HTTP library is httpx.

    *family*, an ``int``, the address family.  If socket.AF_UNSPEC (the default), both A
    and AAAA records will be retrieved.

    Returns a ``dns.message.Message``.
    z)session parameter must be an httpx.ClientFNacceptzapplication/dns-messagezhttps://{}:{}{}zhttps://[{}]:{}{}r   r~   T)r6   http1http2r   r   r   r   r    )r   r   r   	transport)zcontent-typezcontent-length)headerscontentr      =r	   )r   r   params   i+  z2{} responded with status code {}
Response body: {}keyringrequest_macr   r   )&have_dohrd   rg   httpxClientr   to_wirer   r	   r%   r&   r-   AF_INETformatAF_INET6rI   
contextlibnullcontextupdatestrlenr   base64urlsafe_b64encoderstripdecodegetstatus_coder   message	from_wirer   r   elapsedtotal_secondsr   is_responserc   )r   r   r   r5   r<   r   r   r   r   rC   r   r   r   r   r    wirer;   r8   
the_sourcer   r   urlr6   r   cmresponsetwirerr   r   r   httpsf  s   D





r   c                 C   s.   	 z|  |W S  ty   t| | Y nw q)zReads a datagram from the socket.
    A Timeout exception will be raised if the operation is not completed
    by the expiration time.
    )recvfromBlockingIOErrorr{   )r=   max_sizer   r   r   r   	_udp_recv  s   r   c                 C   s@   	 z|r|  ||W S | |W S  ty   t| | Y nw q)zSends the specified datagram to destination over the socket.
    A Timeout exception will be raised if the operation is not completed
    by the expiration time.
    )sendtosendr   r}   )r=   datar   r   r   r   r   	_udp_send  s   r   r=   whatr   r   c                 C   s4   t |tjjr| }t }t| |||}||fS )a  Send a DNS message to the specified UDP socket.

    *sock*, a ``socket``.

    *what*, a ``bytes`` or ``dns.message.Message``, the message to send.

    *destination*, a destination tuple appropriate for the address family
    of the socket, specifying where to send the query.

    *expiration*, a ``float`` or ``None``, the absolute time at which
    a timeout exception should be raised.  If ``None``, no timeout will
    occur.

    Returns an ``(int, float)`` tuple of bytes sent and the sent time.
    )rg   r	   r   Messager   r   r   )r=   r   r   r   	sent_timenr   r   r   send_udp  s
   r       r   r   r   raise_on_truncationignore_errorsqueryc              
   C   s   d}	 t | d|\}}t| j|||sqt }ztjj||||||d}W n- tjjyH } z|	rC|
durC|
| sCW Y d}~q d}~w t	yS   |	rRY q w |	r`|
dur`|
|s`q|rf||fS |||fS )a  Read a DNS message from a UDP socket.

    *sock*, a ``socket``.

    *destination*, a destination tuple appropriate for the address family
    of the socket, specifying where the message is expected to arrive from.
    When receiving a response, this would be where the associated query was
    sent.

    *expiration*, a ``float`` or ``None``, the absolute time at which
    a timeout exception should be raised.  If ``None``, no timeout will
    occur.

    *ignore_unexpected*, a ``bool``.  If ``True``, ignore responses from
    unexpected sources.

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

    *keyring*, a ``dict``, the keyring to use for TSIG.

    *request_mac*, a ``bytes`` or ``None``, the MAC of the request (for TSIG).

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

    *raise_on_truncation*, a ``bool``.  If ``True``, raise an exception if
    the TC bit is set.

    Raises if the message is malformed, if network errors occur, of if
    there is a timeout.

    If *destination* is not ``None``, returns a ``(dns.message.Message, float)``
    tuple of the received message and the received time.

    If *destination* is ``None``, returns a
    ``(dns.message.Message, float, tuple)``
    tuple of the received message, the received time, and the address where
    the message arrived from.

    *ignore_errors*, a ``bool``.  If various format errors or response
    mismatches occur, ignore them and keep listening for a valid response.
    The default is ``False``.

    *query*, a ``dns.message.Message`` or ``None``.  If not ``None`` and
    *ignore_errors* is ``True``, check that the received message is a response
    to this query, and if not keep listening for a valid response.
    r   T  )r   r   r   r   r   N)
r   r   r    r   r	   r   r   	Truncatedr   r1   )r=   r   r   r   r   r   r   r   r   r   r   r   r   received_timer   er   r   r   receive_udp2  sH   >


r   5   c                 C   s   |   }t||||\}}}t|\}}|
rt|
}nt|tj|}|1}t|||| t	|||||| j
| j||	|| \}}|| |_|sM| |sMt|W  d   S 1 sYw   Y  	J )a  Return the response obtained after sending a query via UDP.

    *q*, a ``dns.message.Message``, the query to send

    *where*, a ``str`` containing an IPv4 or IPv6 address,  where
    to send the message.

    *timeout*, a ``float`` or ``None``, the number of seconds to wait before the
    query times out.  If ``None``, the default, wait forever.

    *port*, an ``int``, the port send the message to.  The default is 53.

    *source*, a ``str`` containing an IPv4 or IPv6 address, specifying
    the source address.  The default is the wildcard address.

    *source_port*, an ``int``, the port from which to send the message.
    The default is 0.

    *ignore_unexpected*, a ``bool``.  If ``True``, ignore responses from
    unexpected sources.

    *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 received message.

    *raise_on_truncation*, a ``bool``.  If ``True``, raise an exception if
    the TC bit is set.

    *sock*, a ``socket.socket``, or ``None``, the socket to use for the
    query.  If ``None``, the default, a socket is created.  Note that
    if a socket is provided, it must be a nonblocking datagram socket,
    and the *source* and *source_port* are ignored.

    *ignore_errors*, a ``bool``.  If various format errors or response
    mismatches occur, ignore them and keep listening for a valid response.
    The default is ``False``.

    Returns a ``dns.message.Message``.
    N)r   r   r$   r   r   r,   r-   
SOCK_DGRAMr   r   r   macr   r   rc   )r   r   r   r5   r<   r   r   r   r   r   r=   r   r   r;   r   
begin_timer   r   rz   r   r   r   r   r   udp  s<   8

 r   udp_socktcp_sockc                 C   s`   zt | ||||||||d|	|}|dfW S  tjjy/   t| ||||||||
	}|df Y S w )a|  Return the response to the query, trying UDP first and falling back
    to TCP if UDP results in a truncated response.

    *q*, a ``dns.message.Message``, the query to send

    *where*, a ``str`` containing an IPv4 or IPv6 address,  where to send the message.

    *timeout*, a ``float`` or ``None``, the number of seconds to wait before the query
    times out.  If ``None``, the default, wait forever.

    *port*, an ``int``, the port send the message to.  The default is 53.

    *source*, a ``str`` containing an IPv4 or IPv6 address, specifying the source
    address.  The default is the wildcard address.

    *source_port*, an ``int``, the port from which to send the message. The default is
    0.

    *ignore_unexpected*, a ``bool``.  If ``True``, ignore responses from unexpected
    sources.

    *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
    received message.

    *udp_sock*, a ``socket.socket``, or ``None``, the socket to use for the UDP query.
    If ``None``, the default, a socket is created.  Note that if a socket is provided,
    it must be a nonblocking datagram socket, and the *source* and *source_port* are
    ignored for the UDP query.

    *tcp_sock*, a ``socket.socket``, or ``None``, the connected socket to use for the
    TCP query.  If ``None``, the default, a socket is created.  Note that if a socket is
    provided, it must be a nonblocking connected stream socket, and *where*, *source*
    and *source_port* are ignored for the TCP query.

    *ignore_errors*, a ``bool``.  If various format errors or response mismatches occur
    while listening for UDP, ignore them and keep listening for a valid response. The
    default is ``False``.

    Returns a (``dns.message.Message``, tcp) tuple where tcp is ``True`` if and only if
    TCP was used.
    TF)r   r	   r   r   tcp)r   r   r   r5   r<   r   r   r   r   r   r   r   r   r   r   r   udp_with_fallback  s<   9
r   c              	   C   s   d}|dkrAz|  |}|dkrt|t|8 }||7 }W n ttjfy.   t| | Y n tjy<   t| | Y nw |dks|S )zRead 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   )	recvEOFErrorr   r   rR   SSLWantReadErrorr{   SSLWantWriteErrorr}   )r=   countr   rz   r   r   r   r   	_net_readO  s   
r   c              	   C   s~   d}t |}||k r=z|| ||d 7 }W n ttjfy(   t| | Y n tjy6   t| | Y nw ||k s
dS dS )zWrite the specified data to the socket.
    A Timeout exception will be raised if the operation is not completed
    by the expiration time.
    r   N)r   r   r   rR   r   r}   r   r{   )r=   r   r   currentlr   r   r   
_net_writed  s   r   c                 C   sP   t |tjjr|jdd}n
t|dd| }t }t| || t||fS )a{  Send a DNS message to the specified TCP socket.

    *sock*, a ``socket``.

    *what*, a ``bytes`` or ``dns.message.Message``, the message to send.

    *expiration*, a ``float`` or ``None``, the absolute time at which
    a timeout exception should be raised.  If ``None``, no timeout will
    occur.

    Returns an ``(int, float)`` tuple of bytes sent and the sent time.
    T)prepend_length   big)	rg   r	   r   r   r   r   to_bytesr   r   )r=   r   r   tcpmsgr   r   r   r   send_tcpt  s   r   c                 C   sL   t | d|}td|\}t | ||}t }	tjj|||||d}
|
|	fS )a  Read a DNS message from a TCP socket.

    *sock*, a ``socket``.

    *expiration*, a ``float`` or ``None``, the absolute time at which
    a timeout exception should be raised.  If ``None``, no timeout will
    occur.

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

    *keyring*, a ``dict``, the keyring to use for TSIG.

    *request_mac*, a ``bytes`` or ``None``, the MAC of the request (for TSIG).

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

    Raises if the message is malformed, if network errors occur, of if
    there is a timeout.

    Returns a ``(dns.message.Message, float)`` tuple of the received message
    and the received time.
    r   !Hr   )r   structunpackr   r	   r   r   )r=   r   r   r   r   r   ldatar   r   r   r   r   r   r   receive_tcp  s   !r   c                 C   s`   |  |}|dkrd S |tjtjtjfv r"t| | | tjtj	}|dkr.t
|t|d S rJ   )
connect_exerrnoEINPROGRESSEWOULDBLOCKEALREADYr}   
getsockoptr-   
SOL_SOCKETSO_ERROROSErrorosstrerror)rz   r:   r   errr   r   r   r/     s   

r/   c	                 C   s   |   }	t|\}
}|rt|}nt||||\}}}t|tj|}|1}|s.t||| t	||	| t
|||| j| j|\}}||
 |_| |sMt|W  d   S 1 sYw   Y  	J )a  Return the response obtained after sending a query via TCP.

    *q*, a ``dns.message.Message``, the query to send

    *where*, a ``str`` containing an IPv4 or IPv6 address, where
    to send the message.

    *timeout*, a ``float`` or ``None``, the number of seconds to wait before the
    query times out.  If ``None``, the default, wait forever.

    *port*, an ``int``, the port send the message to.  The default is 53.

    *source*, a ``str`` containing an IPv4 or IPv6 address, specifying
    the source address.  The default is the wildcard address.

    *source_port*, an ``int``, the port from which to send the message.
    The default is 0.

    *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 received message.

    *sock*, a ``socket.socket``, or ``None``, the connected socket to use for the
    query.  If ``None``, the default, a socket is created.  Note that
    if a socket is provided, it must be a nonblocking connected stream
    socket, and *where*, *port*, *source* and *source_port* are ignored.

    Returns a ``dns.message.Message``.
    N)r   r$   r   r   r   r,   r-   r.   r/   r   r   r   r   r   r   rc   )r   r   r   r5   r<   r   r   r   r=   r   r   r   r   r;   r   rz   r   r   r   r   r   r     s,   +


 r   c                 C   sN   	 z|    W d S  tjy   t| | Y n tjy%   t| | Y nw qr   )do_handshakerR   r   r{   r   r}   ry   r   r   r   _tls_handshake  s   r  r   c                 C   s   d }d }t |trtj|r|}ntj|r|}ntdtj||d}tj	j
|_| d u r2d|_|dg |du r@tj|_|S )Nzinvalid verify string)cafilecapathFdot)rg   r   r	  rC   isfileisdirr   rR   r[   
TLSVersionTLSv1_2minimum_versioncheck_hostnameset_alpn_protocolsr\   verify_mode)r   r   r  r  r   r   r   r   _make_dot_ssl_context  s    

r  U  r   c              
   C   s   |rt | ||||||||	S |  }t|\}}t||||\}}}|	du r-|s-t|
|}	t|tj||	|
d4}t||| t	|| t
||| t|||| j| j|\}}|| |_| |sbt|W  d   S 1 snw   Y  	J )ak  Return the response obtained after sending a query via TLS.

    *q*, a ``dns.message.Message``, the query to send

    *where*, a ``str`` containing an IPv4 or IPv6 address,  where
    to send the message.

    *timeout*, a ``float`` or ``None``, the number of seconds to wait before the
    query times out.  If ``None``, the default, wait forever.

    *port*, an ``int``, the port send the message to.  The default is 853.

    *source*, a ``str`` containing an IPv4 or IPv6 address, specifying
    the source address.  The default is the wildcard address.

    *source_port*, an ``int``, the port from which to send the message.
    The default is 0.

    *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 received message.

    *sock*, an ``ssl.SSLSocket``, or ``None``, the socket to use for
    the query.  If ``None``, the default, a socket is created.  Note
    that if a socket is provided, it must be a nonblocking connected
    SSL stream socket, and *where*, *port*, *source*, *source_port*,
    and *ssl_context* are ignored.

    *ssl_context*, an ``ssl.SSLContext``, the context to use when establishing
    a TLS connection. If ``None``, the default, creates one with the default
    configuration.

    *server_hostname*, a ``str`` containing the server's hostname.  The
    default is ``None``, which means that no hostname is known, and if an
    SSL context is created, hostname checking will be disabled.

    *verify*, a ``bool`` or ``str``.  If a ``True``, then TLS certificate verification
    of the server is done using the default CA bundle; if ``False``, then no
    verification is done; if a `str` then it specifies the path to a certificate file or
    directory which will be used for verification.

    Returns a ``dns.message.Message``.

    N)r   r   )r   r   r$   r   r  r,   r-   r.   r/   r  r   r   r   r   r   r   rc   )r   r   r   r5   r<   r   r   r   r=   r   r   r   r   r   r   r;   r   rz   r   r   r   r   r   tls1  sP   =




 r  
connectionc              	   C   s  t jjstdd| _|  }|rtd}|}n
t jj|	|
d}|}|> |s0|	||||}t
|\}}||}||d |t|}W d   n1 sSw   Y  t }W d   n1 sfw   Y  t jj|| j| j||d}t|| d|_| |st|S )a  Return the response obtained after sending a query via DNS-over-QUIC.

    *q*, a ``dns.message.Message``, the query to send.

    *where*, a ``str``, the nameserver IP address.

    *timeout*, a ``float`` or ``None``, the number of seconds to wait before the query
    times out. If ``None``, the default, wait forever.

    *port*, a ``int``, the port to send the query to. The default is 853.

    *source*, a ``str`` containing an IPv4 or IPv6 address, specifying the source
    address.  The default is the wildcard address.

    *source_port*, an ``int``, the port from which to send the message. The default is
    0.

    *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
    received message.

    *connection*, a ``dns.quic.SyncQuicConnection``.  If provided, the
    connection to use to send the query.

    *verify*, a ``bool`` or ``str``.  If a ``True``, then TLS certificate verification
    of the server is done using the default CA bundle; if ``False``, then no
    verification is done; if a `str` then it specifies the path to a certificate file or
    directory which will be used for verification.

    *server_hostname*, a ``str`` containing the server's hostname.  The
    default is ``None``, which means that no hostname is known, and if an
    SSL context is created, hostname checking will be disabled.

    Returns a ``dns.message.Message``.
    zDNS-over-QUIC is not available.r   N)r  server_nameTr   r   )r	   quic	have_quicre   idr   r   r   SyncQuicManagerconnectr$   make_streamr   receiver   r   r   r   r   r   maxr   rc   )r   r   r   r5   r<   r   r   r   r  r   r   r   managerthe_connectionthe_managerstartr   streamfinishr   r   r   r   r    s@   2


r  zonerdtyperdclasskeyname
relativizer#   serialuse_udpkeyalgorithmc           '      c   s   t |trtj|}tjj|}tj	|||}|tjj
kr3tj|dddd| }|j| |dur?|j|||d | }t| ||
|\}}}
|rY|tjj
krYtd|r^tjntj}t|||
q}t|	\}}t||| t|}|rt||d| ntd|| }t||| d	}d
}d	}d}|r|}tjj}nd}|}d} |st|\}}!|!du s|dur|!|kr|}!|rt|d|!\}}nt|d|!}"t d|"\}t|||!}|tjj
k}#tjj!||j"|j#d
|| d
|#d}$|$$ }%|%tj$j%krt&|%|$j'} d}&|du rP|$j(r|$j(d j|krtj)*d|$j(d }|j+tjj,kr2tj)*dd}&|- }|tjj
krPtj./|d j.|krNd
}nd
}|$j(|&d D ][}|rbtj)*d|j+tjj,kr|j|kr|r|d j.|krtj)*dd	}n
|tjj
kr| }||kr|tjj0ks|tjj
kr|rd
}qW|rtjj0}d	}qW|r|j"r|$j1stj)*d|$V  |rW d   dS W d   dS 1 sw   Y  dS )a  Return a generator for the responses to a zone transfer.

    *where*, a ``str`` containing an IPv4 or IPv6 address,  where
    to send the message.

    *zone*, a ``dns.name.Name`` or ``str``, the name of the zone to transfer.

    *rdtype*, an ``int`` or ``str``, the type of zone transfer.  The
    default is ``dns.rdatatype.AXFR``.  ``dns.rdatatype.IXFR`` can be
    used to do an incremental transfer instead.

    *rdclass*, an ``int`` or ``str``, the class of the zone transfer.
    The default is ``dns.rdataclass.IN``.

    *timeout*, a ``float``, the number of seconds to wait for each
    response message.  If None, the default, wait forever.

    *port*, an ``int``, the port send the message to.  The default is 53.

    *keyring*, a ``dict``, the keyring to use for TSIG.

    *keyname*, a ``dns.name.Name`` or ``str``, the name of the TSIG
    key to use.

    *relativize*, a ``bool``.  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()`` if using this generator to make a zone.

    *lifetime*, a ``float``, 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.

    *source*, a ``str`` containing an IPv4 or IPv6 address, specifying
    the source address.  The default is the wildcard address.

    *source_port*, an ``int``, the port from which to send the message.
    The default is 0.

    *serial*, an ``int``, the SOA serial number to use as the base for
    an IXFR diff sequence (only meaningful if *rdtype* is
    ``dns.rdatatype.IXFR``).

    *use_udp*, a ``bool``.  If ``True``, use UDP (only meaningful for IXFR).

    *keyalgorithm*, a ``dns.name.Name`` or ``str``, the TSIG algorithm to use.

    Raises on errors, and so does the generator.

    Returns a generator of ``dns.message.Message`` objects.
    r   INSOAz. . %u 0 0 0 0N)	algorithmzcannot do a UDP AXFRr   FTr   r   r   r   xfrorigintsig_ctxmultir   z No answer or RRset not for qnamezfirst RRset is not an SOAr~   zanswers after final SOAzIXFR base serial mismatchmissing TSIG)2rg   r   r	   name	from_text	rdatatype	RdataTypemaker   
make_queryIXFRrrset	authorityr'   use_tsigr   r   r   r-   r   r.   r,   r$   r/   r   r   r   packr   emptyr   r   r   r   r   r   rcodeNOERRORTransferErrorr:  answerr
   	FormErrorr-  r5  copyr1  SerialAXFRhad_tsig)'r   r,  r-  r.  r   r5   r   r/  r0  r#   r<   r   r1  r2  r3  r   rD  r   r;   r   	sock_typerz   r8   r   r   r   donedelete_modeexpecting_SOA	soa_rrsetr9  onamer:  mexpirationr   is_ixfrr   rI  answer_indexr   r   r   r8    s   
E





$r8  c                   @   s   e Zd ZdZdZdZdZdS )UDPModea  How should UDP be used in an IXFR from :py:func:`inbound_xfr()`?

    NEVER means "never use UDP; always use TCP"
    TRY_FIRST means "try to use UDP but fall back to TCP if needed"
    ONLY means "raise ``dns.xfr.UseTCP`` if trying UDP does not succeed"
    r   r~   r   N)rE   rF   rG   ra   NEVER	TRY_FIRSTONLYr   r   r   r   r[    s
    r[  txn_managerudp_modec	                 C   sT  |du rt j|\}}	nt j|}	|jd j}
|
t jjk}| }|	 }t
| |||\}}}t|\}}d}|r(d}|rK|tjkrKtj}d}ntj}d}t|||}t||| |rgt||d| ntdt|| }t||| t j||
|	|}d}d}|st|\}}|du s|dur||kr|}|rt|d|\}}nt|d|}td|\}t|||}t jj||j|jd||| |d}z| |}W n t jj!y   |sJ |tj"kr d}d}tj}Y qw |j#}|r|s|jr|j$st j%&d	W d   n	1 sw   Y  W d   n	1 sw   Y  |s<dS dS )
a  Conduct an inbound transfer and apply it via a transaction from the
    txn_manager.

    *where*, a ``str`` containing an IPv4 or IPv6 address,  where
    to send the message.

    *txn_manager*, a ``dns.transaction.TransactionManager``, the txn_manager
    for this transfer (typically a ``dns.zone.Zone``).

    *query*, the query to send.  If not supplied, a default query is
    constructed using information from the *txn_manager*.

    *port*, an ``int``, the port send the message to.  The default is 53.

    *timeout*, a ``float``, the number of seconds to wait for each
    response message.  If None, the default, wait forever.

    *lifetime*, a ``float``, 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.

    *source*, a ``str`` containing an IPv4 or IPv6 address, specifying
    the source address.  The default is the wildcard address.

    *source_port*, an ``int``, the port from which to send the message.
    The default is 0.

    *udp_mode*, a ``dns.query.UDPMode``, determines how UDP is used
    for IXFRs.  The default is ``dns.UDPMode.NEVER``, i.e. only use
    TCP.  Other possibilities are ``dns.UDPMode.TRY_FIRST``, which
    means "try UDP but fallback to TCP if needed", and
    ``dns.UDPMode.ONLY``, which means "try UDP and raise
    ``dns.xfr.UseTCP`` if it does not succeed.

    Raises on errors.
    Nr   TFr   r   r   r7  r<  )'r	   r8  rB  extract_serial_from_queryquestionr-  r?  rC  from_wire_originr   r   r$   r[  r\  r-   r   r.   r,   r/   r   r   rG  r   r   Inboundr   r   r   r   r   r   r   process_messageUseTCPr^  r:  rQ  r
   rM  )r   r_  r   r5   r   r#   r<   r   r`  r1  r-  rY  r9  r   r;   r   r8   r   retryrR  is_udprz   r   inboundrS  r:  rX  rwirer   r   r   r   r   r   inbound_xfr  s   /


!rk  )T)NNr   )
NNFFNr   FFFN)
Nr   Nr   FFFFNF)
Nr   Nr   FFFNNF)NFNr   F)Nr   Nr   FFN)
Nr  Nr   FFNNNT)	Nr  Nr   FFNTN)wra   r   r   enumr  r	  os.pathrj   r-   r   r   typingr   r   r   r   r   dns._featuresr	   dns.exceptiondns.inetdns.messagedns.namedns.quic	dns.rcodedns.rdataclassdns.rdatatype
dns.serialdns.transactiondns.tsigdns.xfrr   r   	_featureshave_have_httpxhttpcore._backends.syncr2   r   NetworkBackend_CoreNetworkBackend	_backendssync
SyncStreamr0   r   HTTPTransportrI   r   rR   ImportErrorr   r
   DNSExceptionr_   rM  rc   rd   re   r8  rK  r$   rt   rv   hasattrrw   ri   SelectSelectorr{   r}   r   r   r   r,   rQ   r   r   r   floatintboolr   r   r   bytesr   r=  NametsigKeyr   r   r   r   r   r   r   r/   r   r  rX   r  rY   r  r  SyncQuicConnectionr?  rP  
rdataclassr4  default_algorithmr@  
RdataClassIntEnumr[  r\  transactionTransactionManagerrk  r   r   r   r   <module>   s~  	
4

,	


 

	


m	

^	

X

 
/	

D

	


n
	


Z	

 ;
	
