o
    šhŽ&  ã                   @   s¨   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mZmZ G dd„ deƒZG dd„ deƒZG dd„ deƒZdS )	é    N)ÚQUIC_MAX_DATAGRAMÚAsyncQuicConnectionÚAsyncQuicManagerÚBaseQuicStreamÚUnexpectedEOFc                       sh   e Zd Z‡ fdd„Zdd„ Zdd„ Zdd„ Zdd
d„Zddd„Zdd„ Z	dd„ Z
dd„ Zdd„ Z‡  ZS )ÚAsyncioQuicStreamc                    s   t ƒ  ||¡ t ¡ | _d S ©N)ÚsuperÚ__init__ÚasyncioÚ	ConditionÚ_wake_up)ÚselfÚ
connectionÚ	stream_id©Ú	__class__© úD/var/www/html/venv/lib/python3.10/site-packages/dns/quic/_asyncio.pyr
      s   zAsyncioQuicStream.__init__c              	   Ã   sR   | j 4 I d H š | j  ¡ I d H  W d   ƒI d H  d S 1 I d H s"w   Y  d S r   )r   Úwait©r   r   r   r   Ú_wait_for_wake_up   s   €.ÿz#AsyncioQuicStream._wait_for_wake_upc                 Ã   s^   	 |   |¡}| j |¡rd S || _zt |  ¡ |¡I d H  W n ty*   tj	j
‚w d| _q)NTr   )Ú_timeout_from_expirationÚ_bufferZhaveZ
_expectingr   Úwait_forr   ÚTimeoutErrorÚdnsÚ	exceptionÚTimeout)r   ÚamountÚ
expirationÚtimeoutr   r   r   r   "   s   €
ÿ÷zAsyncioQuicStream.wait_forc                 Ã   sP   	 |   |¡}| j ¡ rd S zt |  ¡ |¡I d H  W n ty&   tjj	‚w qr   )
r   r   Zseen_endr   r   r   r   r   r   r   )r   r    r!   r   r   r   Úwait_for_end.   s   €

ÿúzAsyncioQuicStream.wait_for_endNc                 Ã   sv   |   |¡}| j ¡ r|  |¡I d H  | j ¡ S |  d|¡I d H  t d| j 	d¡¡\}|  ||¡I d H  | j 	|¡S )Né   z!H)
Z_expiration_from_timeoutÚ_connectionÚis_h3r"   r   Úget_allr   ÚstructÚunpackÚget)r   r!   r    Úsizer   r   r   Úreceive8   s   €


zAsyncioQuicStream.receiveFc                 Ã   s(   |   |¡}| j | j||¡I d H  d S r   )Z_encapsulater$   ÚwriteZ
_stream_id)r   ÚdatagramÚis_endÚdatar   r   r   ÚsendC   s   €
zAsyncioQuicStream.sendc              	   Ã   s\   |   ||¡r,| j4 I d H š | j ¡  W d   ƒI d H  d S 1 I d H s%w   Y  d S d S r   )Z_common_add_inputr   Únotify)r   r/   r.   r   r   r   Ú
_add_inputG   s   €.ÿÿzAsyncioQuicStream._add_inputc                 Ã   s   |   ¡  d S r   )Ú_closer   r   r   r   ÚcloseL   s   €zAsyncioQuicStream.closec                 Ã   ó   | S r   r   r   r   r   r   Ú
__aenter__Q   ó   €zAsyncioQuicStream.__aenter__c              	   Ã   sZ   |   ¡ I d H  | j4 I d H š | j ¡  W d   ƒI d H  dS 1 I d H s&w   Y  dS ©NF)r4   r   r1   )r   Úexc_typeÚexc_valÚexc_tbr   r   r   Ú	__aexit__T   s   €ÿþzAsyncioQuicStream.__aexit__r   ©F)Ú__name__Ú
__module__Ú__qualname__r
   r   r   r"   r+   r0   r2   r4   r6   r<   Ú__classcell__r   r   r   r   r      s    


r   c                       sj   e Zd Zd‡ fdd„	Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Zddd„Z	dd„ Z
ddd„Zdd„ Z‡  ZS )ÚAsyncioQuicConnectionNc                    sP   t ƒ  ||||||¡ d | _t ¡ | _t ¡ | _t ¡ | _d | _	d | _
d| _d S r8   )r	   r
   Ú_socketr   ÚEventÚ_handshake_completeÚ_socket_createdr   Ú_wake_timerÚ_receiver_taskÚ_sender_taskÚ_wake_pending)r   r   ÚaddressÚportÚsourceÚsource_portÚmanagerr   r   r   r
   \   s   



zAsyncioQuicConnection.__init__c              	   Ã   s|  z¬z~t j | j¡}t j d¡}| |tjd| j	| j
d | j
d f¡I d H | _| j ¡  | j4 I d H šA | jsj| j td ¡I d H \}}|d | j
d ksU|d | j
d krVq4| j ||t ¡ ¡ |  ¡ I d H  | jr7W d   ƒI d H  n1 I d H szw   Y  W n	 ty‰   Y nw W d| _|  ¡ I d H  | j ¡  d S W d| _|  ¡ I d H  | j ¡  d S d| _|  ¡ I d H  | j ¡  w )Nr   r   é   T)r   ZinetZaf_for_addressZ_addressZasyncbackendÚget_backendZmake_socketÚsocketÚ
SOCK_DGRAMÚ_sourceÚ_peerrC   rF   ÚsetÚ_doneÚrecvfromr   r$   Zreceive_datagramÚtimeÚ_wakeupÚ	ExceptionrE   )r   ÚafÚbackendr-   rK   r   r   r   Ú	_receiverf   sD   €ÿ
ÿ$÷(ÿ€ÿ€üþzAsyncioQuicConnection._receiverc              	   Ã   sR   d| _ | j4 I d H š | j ¡  W d   ƒI d H  d S 1 I d H s"w   Y  d S )NT)rJ   rG   Ú
notify_allr   r   r   r   rZ   ‚   s
   €.ÿzAsyncioQuicConnection._wakeupc              	   Ã   s\   | j 4 I d H š | js| j  ¡ I d H  W d   ƒI d H  n1 I d H s$w   Y  d| _d S r8   )rG   rJ   r   r   r   r   r   Ú_wait_for_wake_timer‡   s   €€(þ
z*AsyncioQuicConnection._wait_for_wake_timerc                 Ã   s¼   | j  ¡ I d H  | js\| j t ¡ ¡}|D ]\}}|| jks!J ‚| j || jd ¡I d H  q|  	¡ \}}zt
 |  ¡ |¡I d H  W n	 tyJ   Y nw |  |¡ |  ¡ I d H  | jrd S d S r   )rF   r   rW   r$   Zdatagrams_to_sendrY   rU   rC   ÚsendtoZ_get_timer_valuesr   r   r`   r[   Z_handle_timerÚ_handle_events)r   Z	datagramsr-   rK   r    Úintervalr   r   r   Ú_sender   s    €ÿ
õzAsyncioQuicConnection._senderc                 Ã   sª  d}	 | j  ¡ }|d u rd S t|tjjjƒr‰|  ¡ rt| j 	|¡}|D ]O}t|tj
jjƒrV| j |j¡}|rU|jd u r@|j|_n	|jd u rI|j|_|jrU| dd¡I d H  q#t|tj
jjƒrr| j |j¡}|rr| |j|j¡I d H  q#nN| j |j¡}|rˆ| |j|j¡I d H  n9t|tjjjƒr—| j ¡  n+t|tjjjƒr¨d| _| j ¡  nt|tjjjƒrÂ| j |j¡}|rÂ| dd¡I d H  |d7 }|dkrÔd}t  d¡I d H  q)Nr   Tó    rP   é
   )!r$   Z
next_eventÚ
isinstanceÚaioquicZquicÚeventsZStreamDataReceivedr%   Z_h3_connZhandle_eventÚh3ZHeadersReceivedÚ_streamsr)   r   Ú_headersÚheadersZ	_trailersZstream_endedr2   ZDataReceivedr/   Z
end_streamZHandshakeCompletedrE   rV   ZConnectionTerminatedrW   rH   ÚcancelZStreamResetr   Úsleep)r   ÚcountÚeventZ	h3_eventsZh3_eventÚstreamr   r   r   rb   œ   s\   €



€
ÿ€ó€×z$AsyncioQuicConnection._handle_eventsFc                 Ã   s$   | j  |||¡ |  ¡ I d H  d S r   )r$   Zsend_stream_datarZ   )r   rr   r/   r.   r   r   r   r,   É   s   €zAsyncioQuicConnection.writec                 C   s.   | j rd S t |  ¡ ¡| _t |  ¡ ¡| _d S r   )Ú_closedr   ÚTaskr^   rH   rd   rI   r   r   r   r   ÚrunÍ   s   zAsyncioQuicConnection.runc                 Ã   sd   zt  | j ¡ |¡I d H  W n ty   tjj‚w | jr t	‚| j
 d¡}t| |ƒ}|| j|< |S r8   )r   r   rE   r   r   r   r   r   rW   r   r$   Zget_next_available_stream_idr   rk   )r   r!   r   rr   r   r   r   Úmake_streamÓ   s   €ÿ

z!AsyncioQuicConnection.make_streamc                 Ã   s®   | j sU| j | jd | jd ¡ d| _ | j ¡  | j ¡  |  ¡ I d H  z| j	I d H  W n
 t
jy7   Y nw z| jI d H  W n
 t
jyJ   Y nw | j ¡ I d H  d S d S )Nr   rP   T)rs   Ú_managerÚclosedrU   r$   r4   rF   rV   rZ   rH   r   ZCancelledErrorrI   rC   r   r   r   r   r4   ß   s&   €

ÿÿñzAsyncioQuicConnection.closer   r=   )r>   r?   r@   r
   r^   rZ   r`   rd   rb   r,   ru   rv   r4   rA   r   r   r   r   rB   [   s    

-
rB   c                       sD   e Zd Zdejddf‡ fdd„	Z	ddd	„Zd
d„ Zdd„ Z‡  Z	S )ÚAsyncioQuicManagerNFc                    s   t ƒ  ||t||¡ d S r   )r	   r
   rB   )r   ZconfÚverify_modeZserver_namerj   r   r   r   r
   ó   s   zAsyncioQuicManager.__init__éU  r   Tc                 C   s&   |   |||||¡\}}|r| ¡  |S r   )Ú_connectru   )r   rK   rL   rM   rN   Zwant_session_ticketr   Ústartr   r   r   Úconnectø   s   
ÿzAsyncioQuicManager.connectc                 Ã   r5   r   r   r   r   r   r   r6     r7   zAsyncioQuicManager.__aenter__c                 Ã   s,   t | j ¡ ƒ}|D ]	}| ¡ I d H  q
dS r8   )ÚlistZ_connectionsÚvaluesr4   )r   r9   r:   r;   Úconnectionsr   r   r   r   r<     s
   €zAsyncioQuicManager.__aexit__)r{   Nr   T)
r>   r?   r@   ÚsslÚCERT_REQUIREDr
   r~   r6   r<   rA   r   r   r   r   ry   ò   s    
ÿ
ÿ
ry   )r   rR   r‚   r'   rY   Zaioquic.quic.configurationrh   Zaioquic.quic.connectionZaioquic.quic.eventsZdns.asyncbackendr   Zdns.exceptionZdns.inetZdns.quic._commonr   r   r   r   r   r   rB   ry   r   r   r   r   Ú<module>   s    	B 