
    F2j                      U d dl mZ d dlZd dlZd dlZd dlmZ d dlmZ d dl	m
Z
 d dlmZmZ d dlmZmZmZ erdd	lmZ dd
lmZ ddlmZ ddlmZmZ ddlmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$ ddlm%Z& ddlm'Z( ddlm)Z* ddlm+Z+ ddl,m-Z-m.Z.m/Z/m0Z0 ddl1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZB ddlCmDZDmEZEmFZFmGZGmHZH ddlImJZJmKZKmLZL ddlMmNZNmOZOmPZP  ejQ        d          ZdZRejS        jT        ejS        jU        ejS        jV        ejS        jW        dZXdZYdZZdZ[dZ\g dg d gZ]d!Z^d"Z_d#Z`d$ZaeZbd%Zcddez  z   Zddez   ZedZfddez  z   Zgddez  z   dz   e2z   e6z   Zhd&Zid&ZjdZkdd'ez  z   Zldez   Zmddez  z   Zndez   Zodd'ez  z   ZpdTd,ZqdUd1ZrdVd4Zsd5 e:D             Ztd6eud7<   e:jT        ejS        jT        e:jV        ejS        jV        e:jU        ejS        jU        e:jv        ejS        jW        e:jw        ejS        jW        e:jW        ejS        jW        iZxd8eud9<   dWd=ZydXd?ZzdXd@Z{ e            dYdB            Z| e            dYdC            Z} G dD dE          Z~ G dF dGe          Z G dH dIej                  Z edZi e G dJ dK                      Z G dL dMe
          Z G dN dO          Z edZi e G dP dQ                      Z eej        ej        ej        g          Z G dR dS          ZdS )[    )annotationsN)deque)	dataclass)IntEnum)	lru_cachepartial)TYPE_CHECKINGAnySequence   )QuicConfiguration)QuicLoggerTrace   )tls)DATACLASS_KWARGSUINT_VAR_MAX_SIZE)	BufferBufferReadErrorpull_ack_framepull_crypto_framepull_stream_framepush_ack_framepush_crypto_frame_bodypush_stream_frame_bodysize_uint_var)Certificate)pull_quic_header)skip_padding)events)CryptoError
CryptoPairKeyUnavailableError
NoCallback)CONNECTION_ID_MAX_SIZENON_ACK_ELICITING_FRAME_TYPESPROBING_FRAME_TYPESRETRY_INTEGRITY_TAG_SIZESTATELESS_RESET_TOKEN_SIZEQuicErrorCodeQuicFrameType
QuicHeaderQuicPacketTypeQuicProtocolVersionQuicTransportParametersQuicVersionInformationget_retry_integrity_tagget_spin_bitpretty_protocol_versionpull_quic_transport_parameterspush_quic_transport_parameters)MTU_PROBE_SIZESSMALLEST_MAX_DATAGRAM_SIZEQuicDeliveryStateQuicPacketBuilderQuicPacketBuilderStop)K_GRANULARITYQuicPacketRecoveryQuicPacketSpace)FinalSizeError
QuicStreamStreamFinishedErrorquic @  )IH01           d   )NCLIENT_EARLY_TRAFFIC_SECRETCLIENT_HANDSHAKE_TRAFFIC_SECRETCLIENT_TRAFFIC_SECRET_0)NNSERVER_HANDSHAKE_TRAFFIC_SECRETSERVER_TRAFFIC_SECRET_0   l               i   @   	      shortcutstrreturnfrozenset[tls.Epoch]c                4    t          d | D                       S )Nc              3  0   K   | ]}t           |         V  d S N)EPOCH_SHORTCUTS).0is     [/home/thesage/.hermes/hermes-agent/venv/lib/python3.11/site-packages/qh3/quic/connection.py	<genexpr>zEPOCHS.<locals>.<genexpr>u   s'      ::A_Q'::::::    )	frozenset)rT   s    r^   EPOCHSrb   t   s    ::::::::r`   from_versionint
to_versionboolc                >    | |ht           j        t           j        hk    S )zz
    Return whether it is possible to perform compatible version negotiation
    from `from_version` to `to_version`.
    )r-   	VERSION_1	VERSION_2)rc   re   s     r^   is_version_compatiblerj   x   s'     *%%%*  r`   cidbytesc                P    t          j        |                               d          S )Nascii)binasciihexlifydecode)rk   s    r^   dump_cidrr      s!    C  ''000r`   c                .    i | ]}t          |          |S  )rd   )r\   vs     r^   
<dictcomp>rv      s     3V3V3V!CFFA3V3V3Vr`   zdict[int, QuicPacketType]_PACKET_TYPE_FROM_INTzdict[QuicPacketType, tls.Epoch]_EPOCH_FROM_PACKET_TYPEpacket_typer,   	tls.Epochc                    t           |          S rZ   )rx   )ry   s    r^   	get_epochr|      s    ";//r`   	stream_idc                    | dz   S )z9
    Returns True if the stream is client initiated.
    r   rt   r}   s    r^   stream_is_client_initiatedr      s     Ar`   c                    | dz  rdS dS )z7
    Returns True if the stream is unidirectional.
    r   TFrt   r   s    r^   stream_is_unidirectionalr      s     1} t5r`   	is_clientc                D    t          |          | u pt          |           S rZ   r   r   r   r}   s     r^   check_stream_id_for_sendingr      s2    % 	 >29===>r`   c                D    t          |          | upt          |           S rZ   r   r   s     r^   check_stream_id_for_receivingr      s4    %  B6yAAABr`   c                      e Zd ZddZdS )	Limit
frame_typerd   namerU   valuec                L    || _         || _        || _        d| _        || _        d S Nr   )r   r   sentusedr   )selfr   r   r   s       r^   __init__zLimit.__init__   s)    $			


r`   N)r   rd   r   rU   r   rd   )__name__
__module____qualname__r   rt   r`   r^   r   r      s(             r`   r   c                      e Zd ZddZdd	Zd
S )QuicConnectionError
error_coderd   r   
int | Nonereason_phraserU   c                0    || _         || _        || _        d S rZ   r   r   r   r   r   r   r   s       r^   r   zQuicConnectionError.__init__   s    $$*r`   rV   c                R    d| j          d| j         }| j        |d| j         z  }|S )NzError: z
, reason: z, frame_type: )r   r   r   )r   ss     r^   __str__zQuicConnectionError.__str__   s>    EdoEE1CEE?&3$/333Ar`   N)r   rd   r   r   r   rU   )rV   rU   )r   r   r   r   r   rt   r`   r^   r   r      s<        + + + +
     r`   r   c                      e Zd Zd	dZdS )
QuicConnectionAdaptermsgrU   kwargsr
   rV   tuple[str, Any]c                H    d                     | j        d         |          |fS )Nz[{}] {}id)formatextra)r   r   r   s      r^   processzQuicConnectionAdapter.process   s$    
4 0#66>>r`   N)r   rU   r   r
   rV   r   )r   r   r   r   rt   r`   r^   r   r      s(        ? ? ? ? ? ?r`   r   c                  @    e Zd ZU ded<   ded<   dZded<   dZded	<   d
S )QuicConnectionIdrl   rk   rd   sequence_numberr`   stateless_reset_tokenFrf   was_sentN)r   r   r   __annotations__r   r   rt   r`   r^   r   r      sJ         JJJ#&&&&&Hr`   r   c                  "    e Zd ZdZdZdZdZdZdS )QuicConnectionStater   r   r   rS      N)r   r   r   FIRSTFLIGHT	CONNECTEDCLOSINGDRAINING
TERMINATEDrt   r`   r^   r   r      s'        KIGHJJJr`   r   c                       e Zd ZdddZdd
ZdS )QuicNetworkPathFaddrNetworkAddressis_validatedrf   c                r    || _         d| _        d| _        || _        d| _        t                      | _        d S )Nr   F)r   bytes_received
bytes_sentr   local_challenge_sentr   remote_challenges)r   r   r   s      r^   r   zQuicNetworkPath.__init__   s:    $(	#$ ".*/!/4wwr`   sizerd   rV   c                <    | j         p| j        |z   d| j        z  k    S )NrS   )r   r   r   )r   r   s     r^   can_sendzQuicNetworkPath.can_send   s$     WT_t%;DDW@W$WWr`   NF)r   r   r   rf   )r   rd   rV   rf   )r   r   r   r   r   rt   r`   r^   r   r      sG        7 7 7 7 7X X X X X Xr`   r   c                  L    e Zd ZU ded<   ded<   ded<   ded<   d	ed
<   ded<   dS )QuicReceiveContextrz   epochrl   host_cidr   network_pathzlist[Any] | Nonequic_logger_framesfloattimer   versionN)r   r   r   r   rt   r`   r^   r   r      sT         OOO!!!!((((KKKr`   r   c                     e Zd ZdZdZdddddddZedd            Zedd            Zedd            Z	ddZ
ddZddZedd            Zedd            Zedd            Zedd             Zdd!Zej        dd"fdd(Zdd-Zdd/Zddd1Zdd3Zdd4Zdd5Zdd7Zdd9Zdd:Zdd<Zdd>Zdd?Z 	 dddAZ!ddBZ"ddDZ#ddEZ$ddFZ%ddGZ&ddIZ'ddJZ(ddKZ)ddNZ*ddOZ+ddQZ,ddSZ-ddTZ.ddWZ/ddYZ0dd^Z1dd_Z2dd`Z3ddaZ4ddbZ5ddcZ6dddZ7ddeZ8ddfZ9ddgZ:ddhZ;ddiZ<ddjZ=ddkZ>ddlZ?ddmZ@ddnZAddoZBddpZCddqZDddrZEddsZFddvZGddwZHdd}ZIddZJddZKddZLddZMddZNddZOddZPddZQ	 dddZRddZSddZTddZUddZVddZWddZXddZY	 dddZZddZ[ddZ\ddZ]ddZ^ddZ_ddZ`ddZaddZbddZcddZdddZeddZfddZgddZhddZig d"fd dZjddZkddZlddZmddZndS (  QuicConnectiona  
    A QUIC connection.

    The state machine is driven by three kinds of sources:

    - the API user requesting data to be send out (see :meth:`connect`,
      :meth:`reset_stream`, :meth:`send_ping`, :meth:`send_datagram_frame`
      and :meth:`send_stream_data`)
    - data being received from the network (see :meth:`receive_datagram`)
    - a timer firing (see :meth:`handle_timer`)

    :param configuration: The QUIC configuration to use.
    )b_configuration
_is_client
_ack_delay	_close_at_close_event_connect_called_cryptos_cryptos_initial_crypto_buffers_crypto_frame_type_crypto_packet_version_crypto_retransmitted_crypto_streams_events_handshake_complete_handshake_confirmed
_host_cids_host_cid_seq_map_has_unsent_cidsr   _host_cid_seq_local_ack_delay_exponent!_local_active_connection_id_limit_local_grease_quic_version!_local_grease_transport_parameter_local_challenges#_local_initial_source_connection_id_local_max_data!_local_max_stream_data_bidi_local"_local_max_stream_data_bidi_remote_local_max_stream_data_uni_local_max_streams_bidi_local_max_streams_uni_local_next_stream_id_bidi_local_next_stream_id_uni_loss_at_network_paths
_pacing_at	_peer_cid_peer_cid_available_peer_cid_sequence_numbers_peer_retire_prior_to_peer_token_quic_logger_remote_ack_delay_exponent"_remote_active_connection_id_limit$_remote_initial_source_connection_id_remote_max_idle_timeout_remote_max_ack_delay!_ack_eliciting_sent_since_receive_remote_max_data_remote_max_data_used_remote_max_datagram_frame_size"_remote_max_stream_data_bidi_local#_remote_max_stream_data_bidi_remote_remote_max_stream_data_uni_remote_max_streams_bidi_remote_max_streams_uni_remote_version_information_retry_count_retry_source_connection_id_spaces	_spin_bit_spin_highest_pn_state_streams_streams_dirty_limits_streams_queue_streams_blocked_bidi_streams_blocked_uni_streams_finished_version_version_negotiated_compatible _version_negotiated_incompatible#_original_destination_connection_id_logger_loss_close_pending"_close_packets_received_since_send_close_packets_send_threshold_handshake_keys_discard_pending_datagrams_pending_handshake_done_pending_ping_pending_probe_pending_retire_connection_ids_streams_blocked_pending_session_ticket_fetcher_session_ticket_handler__frame_handlersr   _local_max_data_used_max_datagram_size_mtu_probe_sizes_mtu_probe_pending_initial_source_connection_id_ech_retry_configs_effective_idle_timeoutN)"original_destination_connection_idretry_source_connection_idsession_ticket_fetchersession_ticket_handlerconfigurationr   r4  bytes | Noner5  r6  tls.SessionTicketFetcher | Noner7  tls.SessionTicketHandler | NonerV   Nonec               Z    |j         r|
J d            |
J d            n.|j        
J d            |j        
J d            |
J d            | _        |j          _        |j         _         j        r|j        r fdt          D             ng  _	        d  _
        t           _        d  _        d  _        d _        i  _        i  _        i  _        d  _        d  _        d _        i  _        t/                       _        d _        d _        t7          t9          j        |j                  d j        st9          j        d	          nd d
          g _         j        d         j          _!         j        d         j         di _"        d _#        d _$        d _%        |j&        |j&        nd _'        tP          )                    t9          j        d          d          dz  dz  dz   _*        dtP          )                    t9          j        d          d          z  dz    _+        i  _,         j        d         j          _-        t]          t^          j0        d|j1                   _2        |j3         _4        |j3         _5        |j3         _6        t]          t^          j7        dd           _8        t]          t^          j9        dd           _:         j        rdnd _;         j        rdnd _<        d  _=        g  _>        d  _?        t7          t9          j        |j                  d            _@        g  _A        dh _B        d _C        d _D        d  _E        d _F        d _G        d  _H        d _I        d  _J        d _K         j        jL         _M        d _N        d _O        d  _P        d _Q        d _R        d _S        d _T        d _U        d  _V        d _W        | _X        d  _Y        i  _Z        d _[        d _\        t          j^         __        i  _`        t                       _b        g  _c        g  _d        g  _e        t                       _f        d  _g        d _h        d _i         j        r j@        j          _j        n| _j        t          t          d!t           jj                  i           _n        |jo        r+|jo        p                    |j          jj        "           _E        t          |jr         j          js         j         jE         jn        #           _t        d jt        _u        d _v        d _w        d _x        d _y        t/                       _z        d _{        g  _|        d _}        g  _~        d _        | _        | _        i d j        t          d$          fd j        t          d$          fd j        t          d%          fd j        t          d%          fd j        t          d&          fd' j        t          d&          fd( j        t          d%          fd) j        t          d*          fd j        t          d&          fd+ j        t          d&          fd, j        t          d&          fd- j        t          d&          fd. j        t          d&          fd/ j        t          d&          fd0 j        t          d&          fd1 j        t          d&          fd	 j        t          d&          fi d2 j        t          d&          fd3 j        t          d&          fd4 j        t          d&          fd5 j        t          d&          fd6 j        t          d&          fd7 j        t          d&          fd8 j        t          d&          fd j        t          d&          fd9 j        t          d&          fd: j        t          d&          fd j        t          d&          fd; j        t          d$          fd< j        t          d&          fd= j        t          d*          fd> j        t          d&          fd? j        t          d&          f _        d S )@Nz:Cannot set original_destination_connection_id for a clientz2Cannot set retry_source_connection_id for a clientz(SSL certificate is required for a serverz(SSL private key is required for a serverz;original_destination_connection_id is required for a serverc                *    g | ]}|j         k    |S rt   )r.  )r\   r   r   s     r^   
<listcomp>z+QuicConnection.__init__.<locals>.<listcomp>  s&    GGG11t/F+F+FQ+F+F+Fr`   Fr      T)rk   r   r   r   r   rS   rP   big   i



rF      r      max_data)r   r   r   max_streams_bidirI   max_streams_unig   r   )rk   r   r`           g?r   )r   odcid)initial_rtt!peer_completed_address_validation
send_probemax_datagram_sizequic_loggerloggerIH01IH101rH      rO   rE   rR   
                                                      0   1   )r   certificateprivate_keyr   r   rN  r.  probe_datagram_sizer5   r/  r0  r:   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   osurandomconnection_id_lengthr   rk   r   r   r   r   r   active_connection_id_limitr   rd   
from_bytesr   r   r   r   r   r*   MAX_DATArE  r   max_stream_datar   r   r   MAX_STREAMS_BIDIr   MAX_STREAMS_UNIr   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  idle_timeoutr3  r  r  r  r  r	  r
  r  r  r  r  r  r2  r  r  r  r   r   r  r  setr  r  r  r  r  r  r  r  r  r   rP  rr   r  rO  start_tracer;   rK  _send_prober  max_ack_delayr   r!  r"  r#  r$  r%  r&  r'  r(  r)  r*  r+  _handle_padding_framerb   _handle_ping_frame_handle_ack_frame_handle_reset_stream_frame_handle_stop_sending_frame_handle_crypto_frame_handle_new_token_frame_handle_stream_frame_handle_max_data_frame_handle_max_stream_data_frame_handle_max_streams_bidi_frame_handle_max_streams_uni_frame_handle_data_blocked_frame!_handle_stream_data_blocked_frame_handle_streams_blocked_frame_handle_new_connection_id_frame"_handle_retire_connection_id_frame_handle_path_challenge_frame_handle_path_response_frame_handle_connection_close_frame_handle_handshake_done_frame_handle_datagram_frame_QuicConnection__frame_handlers)r   r8  r4  r5  r6  r7  s   `     r^   r   zQuicConnection.__init__w  sp	    " 	5==L >== .55D 6555 !,88: 988 !,88: 988 6AAM BAA
 ,'1"/"A #0#DGGGGGGGG 	
 /3''+@D$57798:.226#%*"<>05#( $)!J}ABB !<@O&UbjnnnQU	  
 *.48OA4F4JA3N %)*& 7C 44 	. ^^BJqMM511R7:E+' 
1u555: 	. @B37?13E3I0$$-( 
  
  

 2?1N.2?2O/*7*G'',$5#(
 (
 (
$
 ',$4;LTW'
 '
 '
# 04*E!!A'.2o)D1&&*57(,)
==>>PT
 
 
 <> 56C'%&"48*+'23/BF1(+%%*"16..2.A.N$ !%&";?,23/340+,(()%'($JN(+E(049; !)5/169ee"0279"68!+.55$(.3+05-? 	7;~7ID44 3 4
 -T8D$LMMN
 
 $ 	 - 9 E E'1> !F ! !D (%126/.A'"5)<
 
 

 $'
  $23/-.*/4,05',$(*#13#(-% (>$'=$"!
4-vf~~>"!
4*F6NN;"!
 4)6%==9"!
 4)6%==9	"!

 42F4LLA"!
 42F4LLA"!
 4,fUmm<"!
 4/="!
 4,fTll;"!
 4,fTll;"!
 4,fTll;"!
 4,fTll;"!
 4,fTll;"!
 4,fTll;"!
 4,fTll;"!
  4,fTll;!"!
" 4.t=#"!
 "!
$ 45vd||D%"!
& 46tE'"!
( 45vd||D)"!
* 42F4LLA+"!
, 496$<<H-"!
. 45vd||D/"!
0 45vd||D1"!
2 47F3"!
4 4:F4LLI5"!
6 44fTllC7"!
8 43VD\\B9"!
: 46vG;"!
< 46tE="!
> 44fSkkB?"!
@ 4.t=A"!
B 4.t=C"!
 "!
r`   rd   c                *    t          | j                  S rZ   )lenr  r   s    r^   open_outbound_streamsz$QuicConnection.open_outbound_streamsZ  s    4=!!!r`   c                    | j         S rZ   )r  r  s    r^   max_concurrent_bidi_streamsz*QuicConnection.max_concurrent_bidi_streams^  s    ,,r`   c                    | j         S rZ   )r  r  s    r^   max_concurrent_uni_streamsz)QuicConnection.max_concurrent_uni_streamsb  s    ++r`   tls.CipherSuite | Nonec                @    | j         j        r| j         j        j        nd S rZ   )r   key_schedulecipher_suiter  s    r^   
get_cipherzQuicConnection.get_cipherf  s     59X5JTtx$11PTTr`   X509Certificate | Nonec                    | j         j        S rZ   )r   peer_certificater  s    r^   get_peercertzQuicConnection.get_peercerti  s    x((r`   list[X509Certificate]c                    | j         j        S rZ   )r   peer_certificate_chainr  s    r^   get_issuercertszQuicConnection.get_issuercertsl  s    x..r`   c                    | j         S rZ   )r   r  s    r^   r8  zQuicConnection.configurationo  s    ""r`   rl   c                    | j         S rZ   )r  r  s    r^   r4  z1QuicConnection.original_destination_connection_ids  s    77r`   c                    | j         S )a  
        ECH retry configurations from the server, if ECH was rejected.

        When the server rejects ECH and provides updated configurations,
        they are stored here. The application can use these to retry
        the connection with updated ECH configs.
        )r2  r  s    r^   ech_retry_configsz QuicConnection.ech_retry_configsw  s     &&r`   rf   c                L    t          | d          r| j        | j        j        S dS )zE
        Whether ECH was offered and accepted by the server.
        r   NF)hasattrr   ech_acceptedr  s    r^   r  zQuicConnection.ech_accepted  s-    
 4 	)DH$88((ur`   c                t    | j         r0|                     | j                   |                                  dS dS )z
        Switch to the next available connection ID and retire
        the previous one.

        After calling this method call :meth:`datagrams_to_send` to retrieve data
        which needs to be sent.
        N)r   _retire_peer_cidr   _consume_peer_cidr  s    r^   change_connection_idz#QuicConnection.change_connection_id  sI     # 	%!!$.111 ""$$$$$	% 	%r`    r   r   r   r   rU   c                ~    | j         3| j        t          vr't          j        |||          | _         d| _        dS dS dS )a  
        Close the connection.

        :param error_code: An error code indicating why the connection is
                           being closed.
        :param reason_phrase: A human-readable explanation of why the
                              connection is being closed.
        Nr   T)r   r  
END_STATESr   ConnectionTerminatedr   r   s       r^   closezQuicConnection.close  sZ     $J)F)F & ;%%+! ! !D
 #'D %$)F)Fr`   r   r   nowr   c                   | j         r| j        r
J d            d| _        t          |d          g| _        | j        j        | j        j        | _        n| j        j        d         | _        |                     |           dS )aK  
        Initiate the TLS handshake.

        This method can only be called for clients and a single time.

        After calling this method call :meth:`datagrams_to_send` to retrieve data
        which needs to be sent.

        :param addr: The network address of the remote peer.
        :param now: The current time.
        z:connect() can only be called for clients and a single timeT)r   Nr   r  )	r   r   r   r   r   original_versionr  supported_versions_connect)r   r   r  s      r^   connectzQuicConnection.connect  s      	
t'; 	
 	
H	
 	
;  $.t$GGGH/; /@DMM /B1EDM#r`   "list[tuple[bytes, NetworkAddress]]c                   | j         d         }| j        t          j        j                 }|j                                        r|j        j        nd}|                    |           | j	        t          j        k    r| j        r| j        n| j	        t          v rg S t          | j        | j        | j        d | j                                        D             | j        j        | j        | j        | j        | j        	  	        }| j        rAg }| j        s=|t          j        j        t:          j        ft          j        j        t:          j        fgz  }|                    t          j        j        t:          j        f           |D ]y\  }}| j        |         }	|	j                                        rN|                     ||	           | !                    ||| j        j"        | j        j#        | j        j$                   z| j%        &                    d| j        j"        | j        j$                   d| _        | '                    d|	           n| j(        j)        | j(        j*        z
  |_+        | j,        r|j+        | j        k     r| j        |_+        |j-        s|j.        d
z  |j/        z
  |_0        	 | j        s<t          j        j        t          j        j        fD ]}| 1                    |||           | 2                    |||           n# tf          $ r Y nw xY w|4                                \  }
}| j5        r| j6        | j        r| j        t          j        j                 j                                        rN| j5        d         }|| _6        t          | j        | j        |to          |j8                  | j        j        | j        | j        | j        | j        	  	        }|                     t:          j        | j        t          j        j                            |9                    tt          j;        d| j<        |f           |j=        }|dk    r'|j>        ?                    t          |                     |4                                \  }}|r8|D ]	}d|_A        
|
B                    |           |B                    |           |}|
r| j                                        D ](\  }}|j8        C                    |          }|||_D        )d}| j        }| j        }| j(        jE        }|D ]}||_F         ||||jG                            |jG        t          j        j        k    rd}|jH        r>| jI        s7d| _I        | J                    |          }|| jK        || jK        k    r|| _K        ||L                    dd|jM        |jD        |N                    |jN                  |jN        t:          j        k    rdnt          | j                  t          | j        j                  dd|jP        id           |r+| j        r$| Q                    t          j        j                   |[|j                                        rB|j        j        |k    r2|R                    |d
| j(        S                                z  z              |jT        |j/        }|
D ]}|t          |          z  }||_/        | j        @|
D ]=}t          |          }| j        L                    dddt          |z   |dgd           >fd|
D             S )a1  
        Return a list of `(data, addr)` tuples of datagrams which need to be
        sent, and the network address to which they need to be sent.

        After calling this method call :meth:`get_timer` to know when the next
        timer needs to be set.

        :param now: The current time.
        r   Nc                $    i | ]\  }}||j         S rt   )packet_number)r\   r   spaces      r^   rv   z4QuicConnection.datagrams_to_send.<locals>.<dictcomp>  s.       /;ueu*  r`   )	r   r   rN  packet_numberspeer_cid
peer_tokenrO  spin_bitr   )builderr   r   r   r   z,Connection close sent (code 0x%X, reason %s)FTis_initiatorr  rS   r   capacityhandlerhandler_args)packetr  	transportpacket_sentr  )r  ry   sciddcidlengthframesheaderrawcategoryeventdatadatagrams_sentr  payload_lengthcountr  c                    g | ]}|fS rt   rt   )r\   datagramr   s     r^   r?  z4QuicConnection.datagrams_to_send.<locals>.<listcomp>  s    ;;;X4 ;;;r`   )Wr   r   r   EpochONE_RTTsendis_valid	key_phaseexpire_previous_keysr  r   r   r   r   r  r8   r   r   r.  r  itemsr   rk   r   r   r  r  r   INITIALr,   	HANDSHAKEappendstart_packet_write_connection_close_framer   r   r   r  debug_close_beginr  congestion_windowbytes_in_flightmax_flight_bytesr'  r   r   r   max_total_bytes_write_handshake_write_applicationr9   flushr/  r0  dictr  start_framer*   PING_on_mtu_probe_deliveryremaining_flight_space_buffer
push_bytesrl   is_pmtu_probeextendgetr  on_packet_sent	sent_timer   is_ack_elicitingr  _idle_deadliner   	log_eventr   ry   rr   
sent_bytes_discard_epochretain_previous_keysget_probe_timeoutr   r  UDP_HEADER_SIZE)r   r  r   _onertt_crypto
_kp_beforer  epoch_packet_typesr   ry   crypto	datagramspackets
probe_sizeprobe_builderpad_sizeprobe_datagramsprobe_packets	probe_pktr  pnsent_handshakerO  spacesloss_on_packet_sentr  deadlinenp_bytes_sentr  r  r   s                                @r^   datagrams_to_sendz QuicConnection.datagrams_to_send  s    *1- sy'89-;-@-I-I-K-KUN))QU 	 	++C000
 K.666# 7!-[J&&I $]o"5 ?C|?Q?Q?S?S   ^'')^M
 
 
  1	!#, "Y&(>?Y(.*BC' " %%sy'8.:P&QRRR&8 
 
"{u-;'')) ((f===66 '##'#4#?#'#4#?&*&7&E 7    L>!,!/  
 #(D4S9999 
,tz/II $ #C,t/FFF+/+B(  ,  /!3l6MM '0 C"%)"3SY5H!I C C--gucBBBB''sCCCC(    %]]__	7 !'	('/) 0ci/05>>@@ 0 .q1J&0D#-/",#G$:;;++ -
 
 
M &&&ci6G(H   %%"3(]	 &    %;H!||%00xAAA-:-@-@-B-B*O] ( "/ 3 3I.2I++  111}---' 8	7 $ 2 2 4 4 - -u+//66>*,E' #N+K\F"&*";! ( (#& ##69MNNNN<39#666%)N
 +	2 B	2 >BD:#22377H+.(T^2K2K)1 *))!,+&,&?171E/:/F/F$*$60" 0"
 (.'9^=S'S'S %'B)1$-)@)@(01C(D(D' ' %-f.?#@  *   ,  7$/ 7##CI$5666 "#,,.. ##-;;//a$*668888  
  $/! 	+ 	+HS]]*MM"/(%  !$X!++(*!" +:N*J2@    ,     <;;;;;;;s   *AL 
LLFc                "    |r| j         S | j        S )zT
        Return the stream ID for the next stream created by this endpoint.
        )r   r   )r   is_unidirectionals     r^   get_next_available_stream_idz+QuicConnection.get_next_available_stream_id  s      	31122r`   float | Nonec                ,   | j         }| j        t          vr~| j        j        D ]}|j        ||j        |k     r|j        }| j                                        | _        | j        || j        |k     r| j        }| j        || j        |k     r| j        }|S )z_
        Return the time at which the timer should fire or None if no timer is needed.
        )	r   r  r  r  r  ack_atget_loss_detection_timer   r   )r   timer_atr  s      r^   	get_timerzQuicConnection.get_timer  s     >;j((* , ,<+$x(?(?$|H !J>>@@DM}( DMH$<$<= * DOh$>$>?r`   c                ~    | j         }|dk    rdS d| j                                        z  }|t          ||          z   S )z
        Compute the next idle-timeout deadline, or None if no idle
        timeout is in effect. RFC 9000 10.1.1: the idle timeout period
        MUST be at least three times the current PTO.
        r   NrS   )r3  r  r	  max)r   r  timeoutfloors       r^   r  zQuicConnection._idle_deadline  sG     .a<<4DJ00222S%((((r`   c                Z   | j         X|| j         k    rM| j        0t          j        t          j        t          j        d          | _        |                                  dS | j	        B|| j	        k    r9| j
                            d           | j                            |           dS dS dS )z
        Handle the timer.

        After calling this method call :meth:`datagrams_to_send` to retrieve data
        which needs to be sent.

        :param now: The current time.
        NzIdle timeoutr   zLoss detection triggeredr  )r   r   r   r  r)   INTERNAL_ERRORr*   PADDING
_close_endr   r  r  r  on_loss_detection_timeoutr   r  s     r^   handle_timerzQuicConnection.handle_timer  s     >%#*?*? ($*$?,;,4"0% % %!
 OOF =$)=)=L9:::J00S099999 %$)=)=r`   events.QuicEvent | Nonec                F    | j         r| j                                         S dS )zy
        Retrieve the next event from the event buffer.

        Returns `None` if there are no buffered events.
        N)r   popleftr  s    r^   
next_eventzQuicConnection.next_event  s'     < 	*<'')))tr`   r  c                   | j         t          v r\| j         t          j        k    rE| j        >| xj        dz  c_        | j        | j        k    rd| _        | xj        dz  c_        d| _        dS t          |          }| j	        }| j
        }| j        j        }|'|                    dddt          |z   |dgd	
           |                     |          }|j        s|xj        |z  c_        | j        |                     |          | _        t          |          }	d}
| j        j        }|
|	k     	r(|
}	 t-          ||
|          \
  }}}}}}}}}}n3# t.          $ r& | |                    dddd|	|z
  id
           Y dS w xY wt0          |         }|}
 ||          }|r |||                    ddddi
           dS |t2          j        k    r=t7          |||||||t9          |                    }|                     ||           dS |/|| j        j        vr!||                    dddd|id
           dS |t2          j        k    rNt7          |||||||t9          |                    }|                      ||||tB          z
           |           dS d}|sj| j         t          j"        u rW|t2          j#        k    r!||                    dddd|id
           dS d}|g| _$        || _%        | &                    |           tN          |         }|tP          j)        j#        k    r| j*        |         }n| j+        |         }|tP          j)        j,        k    r| j-        tP          j)        j.                 }n| j-        |         }	 |tP          j)        j.        tP          j)        j,        fv r|j/        j0        nd}||1                    |           |2                    |||         ||j3                  \  }} }!|B|j/        j0        |k    r2|4                    |d| j5        6                                z  z              n[# tn          $ r}"| j8        9                    |"           ||                    dddd|id
           |rM|tP          j)        j:        tP          j)        j.        fv r)| j;        s"| j5        <                    |           d| _;        Y d}"~"d}"~"wtz          $ r}"| j8        9                    |"           ||                    dddd|id
           | >                    |          rZ| j8        ?                    d           t          jA        t          jC        dd          | _        | D                                 Y d}"~"dS Y d}"~"gd}"~"ww xY w|t2          j.        k    rd}#nd}#|d         |#z  r.| E                    t          jF        t          jH        d            dS d}$|Rg }$|                    dd!|$|!|I                    |          t          |          t          |          d"d|id#
           |!|j3        k    r
|!dz   |_3        |!|jK        v r%| j8        | j8        9                    d$||!           V|s9|tP          j)        j:        k    r$| L                    tP          j)        j#                   | jM        jN        || jM        _O        d| jM        _N        | j         t          j"        u r&|| _P        | Q                    t          jR                   |t2          j.        k    r9|!| jS        k    r.t          |d                   }%|r	|% | _U        n|%| _U        |!| _S        |sh| jV        ra|tP          j)        j.        k    rLd| _V        | L                    tP          j)        j:                   ||                    d%d&d'| jU        i
           t          ||||$||(          }&	 | X                    |&| |          \  }'}(nX# t          $ rK}"| j8        9                    |"           | E                    |"jZ        |"j[        |"j\                   Y d}"~"nd}"~"ww xY w| j         t          v s| j        rdS |                     |          | _        d| _]        |ss|&j^        | j^        k    rc|tP          j)        j.        k    rN| j8        9                    d)t          |&j^                  |           |&j^        | _^        | _                                 |j        s<|tP          j)        j:        k    r'| j8        9                    d*|j`                   d|_        || j$        vr| j$        a                    |           | j$        b                    |          })|)r{|(sy|!|jc        k    rn| j8        9                    d+|j`                   | j$        d                    |)           | j$        e                    d|           | j5        f                                 |jg        sK|!|jc        k    r|!|_c        ||_h        |jK        i                    |!           |'r|jj        || jk        z   |_j        |
|	k     	&dS dS ),aM  
        Handle an incoming datagram.

        After calling this method call :meth:`datagrams_to_send` to retrieve data
        which needs to be sent.

        :param data: The datagram which was received.
        :param addr: The network address from which the datagram was received.
        :param now: The current time.
        Nr   r   r   Tr  datagrams_receivedr  r  r  packet_droppedheader_parse_errorr  triggerr  r;  unknown_connection_id)r   ry   packet_lengthdestination_cid
source_cidtokenintegrity_tagr  )r  r  unsupported_version)r  packet_without_tagr  Ffirst_packet_not_initialrS   key_unavailabler  payload_decrypt_errorz"Stateless reset received from peerzStateless resetr   rB  rW  zReserved bits must be zeropacket_received)r  ry   r  r  r  z+Discarding duplicate packet: epoch=%s pn=%dconnectivityspin_bit_updatedstate)r   r   r   r   r   r   zPeer switching to CID %s (%d)z&Network path %s validated by handshakezNetwork path %s promoted)lr  r  r   r   r   r!  r"  r   r  r   r   r   rn  r  r
  _find_network_pathr   r   r   r  r   r   _pull_quic_header_raw
ValueErrorrw   r,   VERSION_NEGOTIATIONr+   list#_receive_version_negotiation_packetr  RETRY_receive_retry_packetr'   r   r  r   r  _initializerx   r   r  r   r   ZERO_RTTr  r  recvr  r  decrypt_packetexpected_packet_numberr  r  r	  r"   r  r  r  r   reschedule_datar    _is_stateless_resetinfor   r  r)   NO_ERRORr.  r  PROTOCOL_VIOLATIONr*   r-  ry   rr   	ack_queuer  r   r   rk   r  
_set_stater   r  r1   r  r#  r   _payload_receivedr   r   r   r   r  r   r  r   r  indexlargest_received_packetpopinsertreset_for_new_path	discardedlargest_received_timeaddr#  r   )*r   r  r   r  r  rO  r   
cid_lengthr   	_data_len_offsethost_cid_seq_map_get	start_offr  _packet_type_int_packet_length_destination_cid_source_cid_token_integrity_tag_supported_versionsencrypted_offend_off_packet_typedestination_cid_seqr  crypto_frame_requiredr   r  r  r  plain_headerplain_payloadr  excreserved_maskr   r  contextr  
is_probingidxs*                                             r^   receive_datagramzQuicConnection.receive_datagram  s    ;*$$ 2:::%1771<77;9: : ?@D;66!;66*.D'FT'O	(=
 "!!$* '6&F.<   "   0 ..t44( 	:''>9'' >!!0055DNII	#59	!!I *$DD$"$"'!G 
 
 
*))!,.';$,i).C#D  *    
 11ABLG #7"67G"H"H 08*))!,.')@A *   
  ~AAA#$ ,"0$4* "0'+,?'@'@	 	 	 88C8PPP $D$7$JJJ*))!,.'<$,n#=  *     ~333#$ ,"0$4* "0'+,?'@'@	 	 	 **!'+!G.F$FF(  +    $)!  30C0O!O!O>#999 #.#--%0"2+E(0.'A" " .    F(,%'3n# (  !1222 ,L9E	))).x8u-	***SY%67U+@ !2CI4F GGG K))  )//444=C=R=R7*+]E<X> >:m]
 )fk.Cz.Q.Q//a$*">">"@"@@@   '   ""3'''*))!,.'8$,n#=  *    6#)"5sy7H!III 6 J J..3.77715D.   ""3'''*))!,.'>$,n#=  *    ++D11 L%%&JKKK(.(C#0#9#'&7) ) )D%
 OO%%%FFFFF/4 ~555 $ $A. 

,?,4">    
  59&%'"%%(+"4-:+6+B+B<+P+P$,-=$>$>$,[$9$9	# # !).9	 	 &     u;;;/<q/@, //<+L&&E%  
   7#)*=!=!=##CI$5666 ~-5%0"12.{1===<G9 3 =>>>  666!D$999'Q88 .)1\DNN%-DN(5% 8 SY...7<4##CI$7888*))!/0%t~6 *    )))#5   G
/3/E/E],A0 0, ** '   ""3'''

"~"~"%"3          {j((D,?( "0055DN5:D2 ,$55SY...""3W-..'  
 !( 0))+++  , 1#):M1M1M""<l>O   -1)4#666#**<888%++L99C 0: 0-%:W*W*W""#=|?PQQQ#'',,,#**1l;;; 
--/// ? 9 5#@@@4AE125E/##M222# 9(<#&#8EL}
 	!!!!!!sR   1E ,E>=E>B5P; ;
VBSV B"VV_9 9
aAa		ac                    | j         s
J d            | j        t          j        j                                                  dS )z;
        Request an update of the encryption keys.
        z/cannot change key before handshake is confirmedN)r   r   r   r  r  
update_keyr  s    r^   request_key_updatez!QuicConnection.request_key_update  sJ     ( 	
 	
=	
 	
( 	ci'(3355555r`   r}   c                d    |                      |          }|j                            |           dS )ah  
        Abruptly terminate the sending part of a stream.

        This method has no effect if a reset has already been triggered either by a
        call to :meth:`reset_stream` or by the reception of a STOP_SENDING frame.

        :param stream_id: The stream's ID.
        :param error_code: An error code indicating why the stream is being reset.
        N)_get_or_create_stream_for_sendsenderresetr   r}   r   streams       r^   reset_streamzQuicConnection.reset_stream  s3     44Y??J'''''r`   uidc                :    | j                             |           dS )z`
        Send a PING frame to the peer.

        :param uid: A unique ID for this PING.
        N)r&  r  )r   r  s     r^   	send_pingzQuicConnection.send_ping  s!     	!!#&&&&&r`   c                :    | j                             |           dS )zS
        Send a DATAGRAM frame.

        :param data: The data to be sent.
        N)r$  r  )r   r  s     r^   send_datagram_framez"QuicConnection.send_datagram_frame  s!     	&&t,,,,,r`   
end_streamc                h    |                      |          }|j                            ||           dS )z
        Send data on the specific stream.

        :param stream_id: The stream's ID.
        :param data: The data to be sent.
        :param end_stream: If set to `True`, the FIN bit will be set.
        )r  N)r  r  write)r   r}   r  r  r  s        r^   send_stream_datazQuicConnection.send_stream_data  s8     44Y??DZ88888r`   c                    t          | j        |          st          d          | j                            |d          }|t          d          |j                            |           dS )z
        Request termination of the receiving part of a stream.

        :param stream_id: The stream's ID.
        :param error_code: An error code indicating why the stream is being stopped.
        z@Cannot stop receiving on a local-initiated unidirectional streamNz*Cannot stop receiving on an unknown stream)r   r   rM  r  r   receiverstopr  s       r^   stop_streamzQuicConnection.stop_stream  sv     -T_iHH 	R   ""9d33>IJJJZ(((((r`   alpn_protocolc                   | j         j        D ]1\  }}|t           j        j        k    r|                     |            n42t          t          j        t           j        j	        z   | j
        d          | j        s| j        s| j        | j        j        D ]}|| j        k    r n|| j        j        v rrt%          | j        |          r]|| _        | j        |         | j        t           j        j        <   t           j        j        |                                 fg| j         _         nd| _        | j                            dt7          | j                             | j                            t=          j        |                     dS )z
        Callback which is invoked by the TLS engine at most once, when the
        ALPN negotiation completes.

        At this point, TLS extensions have been received so we can parse the
        transport parameters.
        z%No QUIC transport parameters receivedr   NTNegotiated protocol version %s)r  ) r   received_extensionsExtensionTypeQUIC_TRANSPORT_PARAMETERS_parse_transport_parametersr   r)   CRYPTO_ERRORAlertDescriptionmissing_extensionr   r   r  r  available_versionsr  r   r  rj   r   r   r  r  _serialize_transport_parametershandshake_extensionsr  r  r2   r   r  r   ProtocolNegotiated)r   r  ext_typeext_datar   s        r^   _alpn_handlerzQuicConnection._alpn_handler   s    #'("> 
	 
	Hh3,FFF00::: G &(5&892E	     	t'J 	/;  $?R  G$-//4#6#III1$-II J )0;?;P#<ci&78 !$ 1 K $ D D F F95 26D/L02I$-2X2X  
 	F5MRRRSSSSSr`   c                h    t          | j        |          st          t          j        |d          dS )z^
        Check the specified stream can receive data or raises a QuicConnectionError.
        zStream is send-onlyr   N)r   r   r   r)   STREAM_STATE_ERRORr   r   r}   s      r^   _assert_stream_can_receivez)QuicConnection._assert_stream_can_receive8  sE     -T_iHH 	%(;%3   	 	r`   c                h    t          | j        |          st          t          j        |d          dS )z[
        Check the specified stream can send data or raises a QuicConnectionError.
        zStream is receive-onlyr   N)r   r   r   r)   r  r  s      r^   _assert_stream_can_sendz&QuicConnection._assert_stream_can_sendC  sE     +4?IFF 	%(;%6   	 	r`   c                    | j                             d          | _        | j                            dt          | j        j                  | j        j                   dS )z
        Update the destination connection ID by taking the next
        available connection ID provided by the peer.
        r   zSwitching to CID %s (%d)N)r   rb  r   r  r  rr   rk   r   r  s    r^   r  z QuicConnection._consume_peer_cidN  s[     155a88&T^'((N*	
 	
 	
 	
 	
r`   r  c                    |d| j                                         z  z   | _        |r!|                     t          j                   dS |                     t          j                   dS )z,
        Begin the close procedure.
        rS   N)r  r	  r   r^  r   r   r   )r   r  r  s      r^   r  zQuicConnection._close_begin[  sc     q4:#?#?#A#AAA 	:OO/788888OO/899999r`   c                Z   d| _         | j                                        D ]}|                     |           | j                            | j                   |                     t          j	                   | j
        -| j        j                            | j
                   d| _
        dS dS )z*
        End the close procedure.
        N)r   r  keysr  r   r  r   r^  r   r   r   r   rO  	end_trace)r   r   s     r^   r.  zQuicConnection._close_ende  s     \&&(( 	' 	'E&&&&D-...+6777 (+55d6GHHH $D )(r`   c                    | j         sJ |                     |          | _        |                     | j        j                   | j                            d| j                   | 	                                 dS )z-
        Start the client handshake.
        r`   N)
r   r  r   rS  r   rk   r   handle_messager   _push_crypto_datar0  s     r^   r  zQuicConnection._connectt  sr     ,,S11+,,,T%9:::     r`   r   rz   c                   | j         |         j        s| j                            d|           | j        |                                          |t          j        j        k    rR| j	        
                                D ]8}t          |j        _        t          |j        _        |                                 9| j                            | j         |                    d| j         |         _        d S d S )NzDiscarding epoch %sT)r  re  r  r  r   teardownr   r  r  r   valuesr#   rU  _teardown_cbr  r  discard_space)r   r   r  s      r^   r  zQuicConnection._discard_epoch  s    |E", 	1L4e<<<M% ))+++	))) #3::<< & &F/9FK,/9FK,OO%%%%J$$T\%%8999,0DL)))	1 	1r`   c                   | j         t          j        j                 }|j                                        s|j                                        r0| j                            d           |	                                 dS dS )a  
        RFC 9001 4.9.3: 0-RTT keys MUST be discarded once they are no
        longer needed.

        - A server MUST discard 0-RTT keys after receiving a 1-RTT
          packet, or earlier if it has decided not to accept 0-RTT.
        - A client MUST discard 0-RTT keys when it receives the
          Handshake keys (or as soon as the handshake is confirmed).

        0-RTT shares the 1-RTT packet-number space, so we only tear down
        the crypto pair; there is no separate ``QuicPacketSpace`` to
        discard.
        zDiscarding 0-RTT keysN)
r   r   r  rT  r  r  rU  r  r  r  )r   zero_rtts     r^   _discard_zero_rtt_keysz%QuicConnection._discard_zero_rtt_keys  sy     =!34=!!## 	 x}'='='?'? 	 L6777	  	 r`   r   c                    | j         D ]}|j        |k    r|c S | j                            d|           t	          |          S )NzNetwork path %s discovered)r   r   r  r  r   )r   r   r   s      r^   rK  z!QuicConnection._find_network_path  s\     / 	$ 	$L D((#### ) 	7>>>t$$$r`   r>   c                x   || j         v rt          | j                            |d          }|t	          |          | j        u rt          t          j        |d          t          |          r| j
        }d}| j        }n| j        }| j        }| j        }|dz  dz   }||j        k    rt          t          j        |d          ||j        k    r||_        | j                            d| d	           t)          |||t          |           
          x}| j        |<   | j                            |           |S )zI
        Get or create a stream in response to a received frame.
        NzWrong stream initiatorr   r   r   r   zToo many streams openStream z created by peer)r}   max_stream_data_localmax_stream_data_remotewritable)r  r?   r  r   r   r   r   r)   r  r   r   r   r   r  r   r   STREAM_LIMIT_ERRORr   r  r  r>   r  r  )r   r   r}   r  r  r  max_streamsstream_counts           r^   _get_or_create_streamz$QuicConnection._get_or_create_stream  s    ...%%""9d33>))44GG),?)":    (	22 ;(,(G%)*&"9(,(O%)-)P&": &Na/Lk///),?)"9   
  000#/  LDDDDEEE0:#&;'=5i@@@	1 1 1 FT]9- &&v...r`   c                l   t          | j        |          st          d          | j                            |d          }|t          |          | j        urt          d          t          |          rd}| j        }| j        }| j	        }n| j
        }| j        }| j        }| j        }t          |          }t          ||||           x}| j        |<   | j                            |           |r|dz   | _        n
|dz   | _        |dz  |k    r#d|_        |                    |           d| _        |S )z
        Get or create a QUIC stream in order to send data to the peer.

        This always occurs as a result of an API call.
        z8Cannot send data on peer-initiated unidirectional streamNz1Cannot send data on unknown peer-initiated streamr   )r}   r  r  readabler   T)r   r   rM  r  r   r   r   r
  r  r  r   r	  r  r  r>   r  r  r   r   
is_blockedr)  )r   r}   r  r  r  r  streams_blockedr  s           r^   r  z-QuicConnection._get_or_create_stream_for_send  su    +4?IFF 	YWXXX""9d33>))44DOKK !TUUU (	22 	=()%)-)I&":"&";(,(N%)-)Q&";"&"< !9 C C0:#&;'=..	1 1 1 FT]9- &&v...  @1:Q..2;a-/ A~,,$(!&&v...04-r`   session_tickettls.SessionTicketc                    |j         >|j         t          k    r.t          t          j        t
          j        d|j                    |                     |           d S )NzInvalid max_early_data value r   )max_early_data_sizeMAX_EARLY_DATAr   r)   r\  r*   CRYPTOr+  )r   r  s     r^   _handle_session_ticketz%QuicConnection._handle_session_ticket  sm    .:2nDD%(;(/8!58 8    	$$^44444r`   r  c                ^
    t          j        d i d j        j        d j        j        d j        j        d j        j        d j        j        d j	        d j
        d j	        rd nt          d	 j        j        d
 j        j        d j        j        d j        j        d j        j        d j        j        d j        j        d j        j        d j        j         _          j        j        .t-           j        j        t.                    st1          d           j        j         j         _         j        j        r4t-           j        j        d         t.                    st1          d           j        j         j         _         j        j        r7dt7          t9           j        j                            v rt1          d           j        j         j         _        t           j        j                                          fg j         _!         j        j"        } j	        r|}|j#        rv|j         j        j        k    ra j        j"         j         _"        |j$        t          k    r;|j%        D ]3\  }}|t           j        j        k    r &                    |d            n4 j'         j         _(         j)         j)         j         _*         j+         j,         j         _-         j.         j         _/        d! fdi  _0         j        j1        D ]7}te                      }|3                    | j	        |           | j0        |<   8fdt           j4        j5        t           j4        j6        t           j4        j7        t           j4        j8        fD              _9         j0         j:                  j9        t           j4        j5        <   t           j4        j5        tw          tx                    t           j4        j7        tw          tx                    t           j4        j8        tw          tx                    i _=        t           j4        j5        t}                      t           j4        j7        t}                      t           j4        j8        t}                      i _?        t           j4        j5        t                      t           j4        j7        t                      t           j4        j8        t                      i _A        t           jA        C                                           jD        _E        d S )"Nalpn_protocolscadatacafilecapathcipher_suitesr   rP  max_early_dataserver_nameverify_modehostname_checks_common_nameassert_fingerprintverify_hostnameech_config_listsignature_algorithmsoffer_ec_key_shares offer_certificate_status_requestzqh3 v1.0+ no longer support passing cryptography certificate objects within a QuicConfiguration object. Use configuration.load_cert_chain(...) instead using PEM encoded values.r   cryptographyzqh3 v1.0+ no longer support passing cryptography private key object within a QuicConfiguration object. Use configuration.load_cert_chain(...) instead using PEM encoded values.T)from_session_ticketr   rz   rV   r!   c           	     .   g d| j                  }d| dd| dg}|j                  }|j                 }t          t          j        |          t          j        |          t          j        |          t          j        |                    S )N)initial0rtt	handshake1rttserver__secretclient_)recv_setup_cbrecv_teardown_cbsend_setup_cbsend_teardown_cb)r   r   r!   r   _log_key_updated_log_key_retired)r   
epoch_namesecret_namesrecv_secret_namesend_secret_namer   s        r^   create_crypto_pairz6QuicConnection._initialize.<locals>.create_crypto_pairw  s    AAA%+NJ-*----*---L  ,,?@+DO<%d&;=MNN!()>@P!Q!Q%d&;=MNN!()>@P!Q!Q	   r`   )rk   r   r   c                (    i | ]}| |          S rt   rt   )r\   r   r  s     r^   rv   z.QuicConnection._initialize.<locals>.<dictcomp>  s7     
 
 
 %%e,,
 
 
r`   r  rt   )r   rz   rV   r!   )Fr   Contextr   r  r  r  r  r8  r  r   r  r  r  r  r  r  r  r  r  r  r  ri  
isinstanceX509CertificateRuntimeErrorcertificate_chainrj  rU   typecertificate_private_keyr  r  r  r  r  r  r  other_extensionsr  r  alpn_cbr*  get_session_ticket_cbr+  r  new_session_ticket_cb_update_traffic_keyupdate_traffic_key_cbr   r  r!   setup_initialr  r  rT  r  r  r   r  r   CRYPTO_BUFFER_SIZEr   r>   r   r<   r  rO  r  r  r  )r   r  r  r  r  r   pairr  s   `      @r^   rS  zQuicConnection._initialize  sS   ; 
 
 
.==
&--
 &--
 &--	

 ,::
 oo
 <<
 $(?F44
 +77
 +77
 )-(;(W(W
  $2EE
 !/??
 !/??
 "&!4!I!I
  !% 3 G G!
$ #DD%
* *6z+_@
 @
6 &    $2>0 	1!4o:
 :
 	 &   &*%8%J"* 	~$011B
 B
 0
 0
 &   ,0+>+J( !;4466)
% ,;O	*' +*d.A.MMM&*&9&HDH# 1^CC*8*I  &Hh3#4#NNN88$$ 9    	 O  -'3-1-IDH*'3-1-HDH*)-)A&	 	 	 	 	 	" !#*= 	2 	2G<<D8tPWXXX-1D!'**
 
 
 
 	!	"	#	!	
 
 
 ,0+@+Oci'( Iv/ABBBI1C!D!D!DIv/ABBB 
 Iz||IIz|| 
 I00I!2!2I00
 !!4!4!6!677
r`   r}  r   bufr   c                   t          |          \  }}|t          j        k    r<|                                 |                                 |                                 || j        z  dz  }| j        3|j                            | j                            ||                     | j	        |j
                 }|                                d         dz
  }||j        k    rt          t          j        |d          | j        j        s5|j
        t$          j        j        t$          j        j        fv rd| j        _        | j                            ||||j        | j        o|j
        t$          j        j        k                dS )z&
        Handle an ACK frame.
        @B Nr   zACK acknowledges unsent packetr   T)r  ack_rangeset	ack_delayr  reset_pto_count)r   r*   ACK_ECNpull_uint_varr   r   r   r  encode_ack_framer  r   boundsr  r   r)   r\  r  rL  r   r  r  r  on_ack_receivedr   r   r  )	r   r}  r   r  r  ack_delay_encodedr  r  largest_ackeds	            r^   r|  z QuicConnection._handle_ack_frame  s    +9*=*=''...&$*IIWT	 (&--!22<KK   W]+$++--a014E///%(;%>    z; 	@IIR
 A
 A
 <@DJ8
""% FGMSY5F$F 	# 	
 	
 	
 	
 	
r`   c                N   |                                 }|t          j        k    r|                                 }nd}|                                 }	 |                    |                              d          }n# t
          $ r d}Y nw xY w| j        5|j                            | j        	                    |||                     | j
                            d||           | j        :t          j        |||          | _        |                     d|j                   dS dS )z2
        Handle a CONNECTION_CLOSE frame.
        Nutf8r  r   z0Connection close received (code 0x%X, reason %s)Fr  )r  r*   TRANSPORT_CLOSE
pull_bytesrq   UnicodeDecodeErrorr   r   r  encode_connection_close_framer  r  r   r   r  r  r   )r   r}  r   r  r   reason_lengthr   s          r^   r  z-QuicConnection._handle_connection_close_frame  s_    &&((
666**,,JJJ))++	NN=99@@HHMM! 	 	 	MMM	 (&--!??))"/ @     	>	
 	
 	

 $ & ;%%+! ! !D
 5glCCCCC %$s   (A: :B	B	c                   	 t          |          \  }}n;# t          $ r.}t          t          j        |t          |                    d}~ww xY wt          |          }| j        3|j        	                    | j        
                    ||                     | j        |j                 }||z   |j                                        z
  }	|	t          k    rt          t          j        |d          |j                            ||          }
|
A|| _        |j        | _        	 | j                            |
j        | j        |j                   |                                  n# t*          j        $ rT}| j        j        | _        t          t          j        t=          |j                  z   |t          |                    d}~wt*          j         $ rC}t          t          j        t=          |j                  z   |t          |                    d}~ww xY w| j!        s*| j        j"        t*          j#        j$        t*          j#        j%        fv rd| _!        | j&        s:d| _'        d| _(        d| _)        | j*        | j+        _,        | -                                 | .                                 | j/        	                    ta          j1        | j        j2        | j        j3        | j        j4                             | 5                    d           | 5                    d           | j6        7                    d	| j        j2                   dS dS dS | j6        7                    d
|j                   | j&        sJ|j        t*          j8        j9        k    r2| j:        s-| j+        ;                    |j<                   d| _:        dS dS dS dS )z(
        Handle a CRYPTO frame.
        r   Nztoo much crypto buffering)r   T)r  early_data_acceptedsession_resumedFr  zALPN negotiated protocol %sz+Duplicate CRYPTO data received for epoch %sr  )=r   rM  r   r)   FRAME_ENCODING_ERRORrU   r  r   r   r  encode_crypto_framer   r   r  starting_offsetMAX_PENDING_CRYPTOCRYPTO_BUFFER_EXCEEDEDhandle_framer   r   r   r   r  r  r   r  AlertECHRequiredr  r2  r  rd   descriptionAlertr   rJ  StateCLIENT_POST_HANDSHAKESERVER_POST_HANDSHAKEr   r#  r   r%  r  r  ry  r  _replenish_connection_idsr   r   HandshakeCompletedalpn_negotiatedr   r!  _unblock_streamsr  r  r  r  r   rX  r   )r   r}  r   r  offsetr  r{  r  r  pendingr  s              r^   r  z#QuicConnection._handle_crypto_frame	  s   	,S11LFDD 	 	 	%(=%!#hh   	 T (&--!55dFCC   %gm46/FO$C$C$E$EE'''%(?%9    ,,VT:: '1D#*1/D'''J 4GM (    &&(((('    +/(*D'),9C<P<PP)"%c((   
 9   ),9C<P<PP)"%c((    + &	/	/C 1 1 ,0(  2 <@D804D-37D0 04/IDJ,
 //111..000##-&*h&>,0H,H(,(@     %%%>>>%%%===""1483K    I& & 1 1P L=w}   O2MSY%6662 7 
**w|*<<<-1***2 26666s:    
A)AA#A E$ $H3AGH>HHc                P   |                                 }| j        3|j                            | j                            |                     || j        j        k    rG| j        xj        dz  c_        | j                            d| j        j	        | j        j                   dS dS )z.
        Handle a DATA_BLOCKED frame.
        N)limitr   z1Local %s raised to %d in response to DATA_BLOCKED)
r  r   r   r  encode_data_blocked_framer   r   r  r  r   r   r}  r   r  r6  s        r^   r  z)QuicConnection._handle_data_blocked_frame}  s     !!## (&--!;;%;HH   D(... &&!+&&LC$)$*     /.r`   c                   |                                 }|t          j        k    r|                                }n
|j        |z
  }|                    |          }| j        3|j                            | j        	                    |                     | j
        j        %|                                 |z
  | j
        j        k    rt          t          j        |d          | j                            t!          j        |                     dS )z*
        Handle a DATAGRAM frame.
        Nr  zUnexpected DATAGRAM framer   r  )tellr*   DATAGRAM_WITH_LENGTHr  r  r  r   r   r  encode_datagram_framer   max_datagram_frame_sizer   r)   r\  r   r   DatagramFrameReceived)r   r}  r   r  startr  r  s          r^   r  z%QuicConnection._handle_datagram_frame  s    

;;;&&((FF\E)F~~f%% (&--!77v7FF   7?xxzzE!T%8%PPP%(;%9    	F8dCCCDDDDDr`   c                   | j         1|j                            | j                                                    | j        st          t          j        |d          | j        s^| 	                    t          j        j                   d| _        d| j        _        | j        | j        _        |                                  dS dS )z0
        Handle a HANDSHAKE_DONE frame.
        Nz+Clients must not send HANDSHAKE_DONE framesr   T)r   r   r  encode_handshake_done_framer   r   r)   r\  r   r  r   r  r  r  rL  r  ry  r  r   r}  r   r  s       r^   r  z+QuicConnection._handle_handshake_done_frame  s     (&--!==??    	%(;%K    ( 
	*	 3444(,D%;?DJ8 (,'ADJ$ '')))))
	* 
	*r`   c                   |                                 }| j        4|j                            | j                            ||                     || j        k    r$| j                            d|           || _        dS dS )zm
        Handle a MAX_DATA frame.

        This adjusts the total amount of we can send to the peer.
        Nr   maximumzRemote max_data raised to %d)r  r   r   r  encode_connection_limit_framer  r  r  )r   r}  r   r  rE  s        r^   r  z%QuicConnection._handle_max_data_frame  s     $$&& (&--!??)8 @     d+++L=xHHH$,D!!! ,+r`   c                   |                                 }|                                 }| j        4|j                            | j                            ||                     |                     ||           |                     ||          }||j        k    r%| j        	                    d||           ||_        dS dS )z|
        Handle a MAX_STREAM_DATA frame.

        This adjusts the amount of data we can send on a specific stream.
        NrG  r}   z-Stream %d remote max_stream_data raised to %d)
r  r   r   r  encode_max_stream_data_framer  r  r  r  r  )r   r}  r   r  r}   rr  r  s          r^   r  z,QuicConnection._handle_max_stream_data_frame  s     %%''	++-- (&--!>>+y ?     	$$Z;;;++J	BBV:::L?  
 -<F))) ;:r`   c                |   |                                 }|t          k    rt          t          j        |d          | j        4|j                            | j                            ||                     || j	        k    r:| j
                            d|           || _	        |                     d           dS dS )z
        Handle a MAX_STREAMS_BIDI frame.

        This raises number of bidirectional streams we can initiate to the peer.
        "Maximum Streams cannot exceed 2^60r   NrF  z$Remote max_streams_bidi raised to %dFr"  )r  STREAM_COUNT_MAXr   r)   r#  r   r   r  rH  r  r  r  r2  r   r}  r   r  r  s        r^   r  z-QuicConnection._handle_max_streams_bidi_frame	  s     '')))))%(=%B    (&--!??); @     666LE{SSS,7D)!!E!::::: 76r`   c                |   |                                 }|t          k    rt          t          j        |d          | j        4|j                            | j                            ||                     || j	        k    r:| j
                            d|           || _	        |                     d           dS dS )z
        Handle a MAX_STREAMS_UNI frame.

        This raises number of unidirectional streams we can initiate to the peer.
        rM  r   NrF  z#Remote max_streams_uni raised to %dTr"  )r  rN  r   r)   r#  r   r   r  rH  r  r  r  r2  rO  s        r^   r  z,QuicConnection._handle_max_streams_uni_frame)	  s     '')))))%(=%B    (&--!??); @     555LDkRRR+6D(!!D!99999 65r`   c                    |                                 }|                                 }|                                }|                    |          }|                    t                    }|rt	          |          t
          k    rt          t          j        |d           j	        6|j
                             j	                            ||||                     ||k    rt          t          j        |d          t          | j                   _        d}	 fd j        D             }
 j        j        |k     rd}	|
                    d	 j                    fd
 j        D              _        | j        k    rM| j        vrD j                            t+          |||                      j                            |           |
D ]}                     |           |	r                                  dt	           j                  z    j        k    rt          t          j        |d          t	           j                  t9           j        dz  t:                    k    rt          t          j        |d          dS )z3
        Handle a NEW_CONNECTION_ID frame.
        z.Length must be greater than 0 and less than 20r   Nconnection_idretire_prior_tor   r   z/Retire Prior To is greater than Sequence NumberFc                4    g | ]}|j         j        k     |S rt   r   r   r\   rk   r   s     r^   r?  zBQuicConnection._handle_new_connection_id_frame.<locals>.<listcomp>p	  s4     
 
 
"T%??? ???r`   Tr   c                4    g | ]}|j         j        k    |S rt   rV  rW  s     r^   r?  zBQuicConnection._handle_new_connection_id_frame.<locals>.<listcomp>z	  s4     $
 $
 $
"d&@@@ @@@r`   rk   r   r   r   zToo many active connection IDsr   z'Too many pending retired connection IDs)r  
pull_uint8r  r(   r  r$   r   r)   r#  r   r   r  encode_new_connection_id_framer\  r(  r   r   r   r   rc  r   r   rg  r  r  r   CONNECTION_ID_LIMIT_ERRORr(  minMAX_PENDING_RETIRES)r   r}  r   r  r   rT  r  rS  r   
change_cidretirequic_connection_ids   `           r^   r  z.QuicConnection._handle_new_connection_id_frameF	  s    ++--++--!!v.. #/I J J 	M 2 25K K K%(=%N    (&--!@@"/$3$3*?	 A     _,,%(;%O    &)$:T%U%U" 

 
 
 
/
 
 

 >)O;;JMM!T^,,,$
 $
 $
 $
/$
 $
 $
  t999t'FFF$++ %$3*?     +//@@@ #) 	6 	6!!"45555  	%""$$$ s4+,,,t/UUU%(B%>    t*++c2Q68K/
 /
 
 
 &(B%G   
 
r`   c                   |                                 }|                    |          }| j        3|j                            | j                            |                     | j        st          t          j	        |d          dS )z+
        Handle a NEW_TOKEN frame.
        N)r@  z&Clients must not send NEW_TOKEN framesr   )
r  r  r   r   r  encode_new_token_framer   r   r)   r\  )r   r}  r   r  r  r@  s         r^   r  z&QuicConnection._handle_new_token_frame	  s     ""$$v&& (&--!88u8EE    	%(;%F   	 	r`   c                    t          |           | j        3|j                            | j                                                   dS dS )z)
        Handle a PADDING frame.
        N)_skip_paddingr   r   r  encode_padding_framerD  s       r^   rz  z$QuicConnection._handle_padding_frame	  sQ     	c (&--d.?.T.T.V.VWWWWW )(r`   c                *   |                     d          }| j        3|j                            | j                            |                     t          |j        j                  t          k     r!|j        j                            |           dS dS )z0
        Handle a PATH_CHALLENGE frame.
        rP   Nr;  )	r  r   r   r  encode_path_challenge_framer  r   r   MAX_REMOTE_CHALLENGES)r   r}  r   r  r  s        r^   r  z+QuicConnection._handle_path_challenge_frame	  s     ~~a   (&--!==4=HH   w#5669NNN 299$????? ONr`   c                ~   |                     d          }| j        3|j                            | j                            |                     	 | j                            |          }n*# t          $ r t          t          j
        |d          w xY w| j                            d|j                   d|_        dS )z/
        Handle a PATH_RESPONSE frame.
        rP   Nr;  z!Response does not match challenger   z&Network path %s validated by challengeT)r  r   r   r  encode_path_response_framer   rb  KeyErrorr   r)   r\  r  r  r   r   )r   r}  r   r  r  r   s         r^   r  z*QuicConnection._handle_path_response_frame	  s     ~~a   (&--!<<$<GG  	155d;;LL 	 	 	%(;%A   	 	C\EVWWW$(!!!s   A, ,'Bc                z    | j         3|j                            | j                                                    dS dS )z&
        Handle a PING frame.
        N)r   r   r  encode_ping_framerD  s       r^   r{  z!QuicConnection._handle_ping_frame	  sA     (&--d.?.Q.Q.S.STTTTT )(r`   c                   |                                 }|                                 }|                                 }| j        5|j                            | j                            |||                     |                     ||           |                     ||          }||j        k    rt          t          j
        |d          t          d||j        j        z
            }| j        j        |z   | j        j        k    rt          t          j
        |d          | j                            d|||           	 |j                            ||          }	n;# t(          $ r.}
t          t          j        |t-          |
                    d}
~
ww xY w|	| j                            |	           | j        xj        |z  c_        |dk    r| j                            |           dS dS )	z.
        Handle a RESET_STREAM frame.
        Nr   
final_sizer}   Over stream data limitr   r   Over connection data limitz6Stream %d reset by peer (error code %d, final size %d))r   rq  )r  r   r   r  encode_reset_stream_framer  r  r  r   r)   FLOW_CONTROL_ERRORr(  r  highest_offsetr   r   r   r  r  handle_resetr=   FINAL_SIZE_ERRORrU   r   r  rg  )r   r}  r   r  r}   r   rq  r  newly_receivedr  r{  s              r^   r}  z)QuicConnection._handle_reset_stream_frame
  s8    %%''	&&((
&&((
 (&--!;;)jI <     	''
I>>> ++J	BB444%(;%6   
 Q
V_-K KLL$~58L8RRR%(;%:    	D		
 	
 	
		O00%* 1  EE  	 	 	%(9%!#hh   	 L&&&!!^3!!A&**622222 s    E 
F')FFc                   |                                 }| j        2|j                            | j                            |                     || j        k    rt          t          j        |d          t          | j
                  D ]\  }}|j        |k    r|j        |j        k    rt          t          j        |d          | j                            dt!          |j                  |j                   | j
        |= | j                            |j        d           | j                            t)          j        |j                              n|                                  dS )z6
        Handle a RETIRE_CONNECTION_ID frame.
        Nz#Cannot retire unknown connection IDr   z#Cannot retire current connection IDzPeer retiring CID %s (%d)rS  )r  r   r   r  !encode_retire_connection_id_framer   r   r)   r\  	enumerater   r   rk   r   r  r  rr   r   rb  r   r   ConnectionIdRetiredr/  )r   r}  r   r  r   r`  rS  s          r^   r  z1QuicConnection._handle_retire_connection_id_frameA
  s    ++-- (&--!CCOTT   d000%(;%C    %.do$>$> 	 	 E=,?? $(888-#0#C#-&K   
 ""/].//!1  
 OE*&**=+<dCCC##.]=NOOO   # @( 	&&(((((r`   c                   |                                 }|                                 }| j        4|j                            | j                            ||                     |                     ||           |                     ||          }|j                            |           | j	                            t          j        ||                     dS )z.
        Handle a STOP_SENDING frame.
        Nr   r}   )r   )r  r   r   r  encode_stop_sending_framer  r  r  r  r   r   StopSendingReceived)r   r}  r   r  r}   r   r  s          r^   r~  z)QuicConnection._handle_stop_sending_framen
  s     %%''	&&((
 (&--!;;)Y <     	$$Z;;; ++J	BBz222&*	RRR	
 	
 	
 	
 	
r`   c                   	 t          ||          \  }}}}n;# t          $ r.}t          t          j        |t          |                    d}~ww xY wt          |          }	| j        6|j        	                    | j        
                    ||||                     |                     ||           |                     ||          }
||	z   |
j        k    rt          t          j        |d          t          d||	z   |
j        j        z
            }| j        j        |z   | j        j        k    rt          t          j        |d          	 |
j                            |||          }n;# t,          $ r.}t          t          j        |t          |                    d}~ww xY w|| j        	                    |           | j        xj        |z  c_        |dk    r| j                            |
           dS dS )z(
        Handle a STREAM frame.
        r   Nr   rr  r   rs  )r   rM  r   r)   r#  rU   r  r   r   r  encode_stream_framer  r  r  ru  r(  r  rv  r   r   r   r(  r=   rx  r   r  rg  )r   r}  r   r  r}   r3  r  finr{  r  r  ry  r  s                r^   r  z#QuicConnection._handle_stream_frame
  sO   	+<S*+M+M(IvtSS 	 	 	%(=%!#hh   	 T (&--!55v 6     	''
I>>> ++J	BBF?V999%(;%6   
 Q&/2P PQQ$~58L8RRR%(;%:   	O00sCCEE 	 	 	%(9%!#hh   	 L&&&!!^3!!A&**622222 s,    
A)AAE, ,
F$6)FF$c                   |                                 }|                                 }| j        4|j                            | j                            ||                     |                     ||           |                     ||          }|j        j        s_|j	        rZ||j	        k    rQ|xj	        dz  c_	        | j
                            d||j	                   | j                            |           dS dS dS dS )z5
        Handle a STREAM_DATA_BLOCKED frame.
        N)r6  r}   r   zOStream %d local max_stream_data raised to %d in response to STREAM_DATA_BLOCKED)r  r   r   r   encode_stream_data_blocked_framer  r  r  is_finishedr  r  r  r  rg  )r   r}  r   r  r}   r6  r  s          r^   r  z0QuicConnection._handle_stream_data_blocked_frame
  s;    %%''	!!## (&--!BB9 C     	''
I>>>++J	BB +	3,	3 555((A-((L&,	   &**622222	3 	3 	3 	3 65r`   c                   |                                 }|t          k    rt          t          j        |d          | j        D|j                            | j                            |t          j
        k    |                     dS dS )z1
        Handle a STREAMS_BLOCKED frame.
        rM  r   Nr  r6  )r  rN  r   r)   r#  r   r   r  encode_streams_blocked_framer*   STREAMS_BLOCKED_UNIr8  s        r^   r  z,QuicConnection._handle_streams_blocked_frame
  s     !!#####%(=%B    (&--!>>&0M4U&U ?       )(r`   key_typer;  c                X    | j         "| j                             dd||d           dS dS )z'
        Log a key retirement.
        Nsecuritykey_retiredr  r;  r  r   r  r   r  r;  s      r^   r  zQuicConnection._log_key_retired  P     (''##"*w?? (      )(r`   c                X    | j         "| j                             dd||d           dS dS )z#
        Log a key update.
        Nr  key_updatedr  r  r  r  s      r^   r  zQuicConnection._log_key_updated  r  r`   deliveryr7   r  r<   highest_ackedc                f    |t           j        k    r |j                            d|dz              dS dS )zE
        Callback when an ACK frame is acknowledged or lost.
        r   r   N)r7   ACKEDr]  subtract)r   r  r  r  s       r^   _on_ack_deliveryzQuicConnection._on_ack_delivery  s>     (...O$$Q(9::::: /.r`   r6  r   c                8    |t           j        k    r	d|_        dS dS )zX
        Callback when a MAX_DATA or MAX_STREAMS frame is acknowledged or lost.
        r   N)r7   r  r   )r   r  r6  s      r^   _on_connection_limit_deliveryz,QuicConnection._on_connection_limit_delivery#  s&     (...EJJJ /.r`   r  c                r    |t           j        k    r$|r| j        r	d| _        dS dS | j        rd| _        dS dS dS )z
        Callback when a STREAMS_BLOCKED frame is acknowledged or lost.

        Re-arms the pending flag on loss if blocked streams still exist,
        per RFC 9000 13.3 (SHOULD retransmit if conditions persist).
        TN)r7   r  r  r)  r  )r   r  r  s      r^   _on_streams_blocked_deliveryz+QuicConnection._on_streams_blocked_delivery,  sk     (...  9, 948D1119 9 - 948D111 /.
9 9r`   c                8    |t           j        k    r	d| _        dS dS )zO
        Callback when a HANDSHAKE_DONE frame is acknowledged or lost.
        TN)r7   r  r%  )r   r  s     r^   _on_handshake_done_deliveryz*QuicConnection._on_handshake_done_delivery=  s)     (...+/D((( /.r`   r  c                l    |t           j        k    r#d|_        | j                            |           dS dS )zP
        Callback when a MAX_STREAM_DATA frame is acknowledged or lost.
        r   N)r7   r  max_stream_data_local_sentr  rg  )r   r  r  s      r^   _on_max_stream_data_deliveryz+QuicConnection._on_max_stream_data_deliveryD  s@     (...01F-&**622222 /.r`   rS  r   c                F    |t           j        k    rd|_        d| _        dS dS )zR
        Callback when a NEW_CONNECTION_ID frame is acknowledged or lost.
        FTN)r7   r  r   r   )r   r  rS  s      r^   _on_new_connection_id_deliveryz-QuicConnection._on_new_connection_id_deliveryN  s1     (...%*M"$(D!!! /.r`   uidsSequence[int]c                    |t           j        k    rS| j                            d|rdnd           |D ]/}| j                            t          j        |                     0dS | j        	                    |           dS )zE
        Callback when a PING frame is acknowledged or lost.
        zReceived PING%s responser  z (probe))r  N)
r7   r  r  r  r   r  r   PingAcknowledgedr&  r  )r   r  r  r  s       r^   _on_ping_deliveryz QuicConnection._on_ping_deliveryX  s     (...L9;U22:VVV F F##F$;$D$D$DEEEEF F %%d+++++r`   r  c                x   |t           j        k    rn| j                            d|           || _        || j        j        _        | j        r+| j        d         |k    r| j                            d           d| _	        dS | j                            d|           | j        
                                 d| _	        dS )zP
        Callback when an MTU probe PING frame is acknowledged or lost.
        z%MTU probe ACK'd, datagram size now %dr   NzMTU probe for %d lost, stopping)r7   r  r  r  r.  r  _ccr/  rb  r0  clear)r   r  r  s      r^   r  z%QuicConnection._on_mtu_probe_deliverye  s     (...LF
SSS&0D#0:DJN-$ -)>q)AZ)O)O%))!,,,&*D###L@*MMM!'')))&*D###r`   r   c                ^    |t           j        k    r| j                            |           dS dS )zU
        Callback when a RETIRE_CONNECTION_ID frame is acknowledged or lost.
        N)r7   r  r(  r  )r   r  r   s      r^   !_on_retire_connection_id_deliveryz0QuicConnection._on_retire_connection_id_deliveryw  s8     (...'..????? /.r`   plainrx  tuple[bool, bool]c                X   t          |          }d}d}d}d}|j        }	|j        }
| j        } |	            s	  |
            }n*# t          $ r t          t          j        dd          w xY w	 ||         \  }}n*# t          $ r t          t          j        |d          w xY w|j	        |vrt          t          j
        |d          	  ||||           n5# t          $ r t          t          j        |d          t          $ r Y nw xY wd	}|t          j        k    rd	}|t          vrd	}|t          vrd}n|d	} |	            |s&t          t          j
        t          j        d
          |r(|s&t          t          j
        t          j        d          |t#          |          fS )z/
        Handle a QUIC packet payload.
        r;  FNzMalformed frame typer   zUnknown frame typezUnexpected frame typezFailed to parse frameTzPacket contains no frameszPacket contains no CRYPTO frame)r   eofr  r  r   r   r)   r#  rl  r   r\  r?   r*   r  r%   r&   r-  rf   )r   r}  r  rx  r  crypto_frame_foundframe_foundr  r~  buf_eofbuf_pull_uint_varframe_handlersr   frame_handlerframe_epochss                  r^   r_  z QuicConnection._payload_received  sv    %   " 
'-.')) 6	"..00

"   ),A#"8   .<Z.H+||   ),A)"6    }L00),?)"9   
gz37777"   ),A)"9   
 '   
 K]111%)"!>>>#' !444"

#!
m ')) 6	"p  	%(;(09    ! 	); 	%(;(0?     j!1!111s)   
A 'A,0A< <'B#C /DDr  r+   rC  c           
        | j         r| j        s|j        | j        k    r|j        t          || j        j        |j                  k    r| j	        P| j	        
                    ddg dt          |j                  t          |j                  dd|j        id	           |j        | j        _        |j        | _        | xj        d
z  c_        |j        | _        | j                            dt'          |j                   d           |                     |           dS | j	        )| j	        
                    dddd|j        id	           dS dS )z(
        Handle a retry packet.
        )r   Nr  rG  retryry   r  r  r  r  r  r   zRetrying with token (z bytes)r  r8  unexpected_packetr:  )r   r  r>  r   rA  r0   r   rk   r   r   r  rr   r?  r=  r@  r   r  r  r  r  r  )r   r  rC  r  s       r^   rR  z$QuicConnection._receive_retry_packet  s    O*	%*	 &$-77$&""     ,!++(+"$+2$,V->$?$?$,V-C$D$D# #
 !)&*>?  ,    "(!2DN%|D"/5/@D,LQs6<7H7HQQQRRRMMcM"""""  ,!++(*#6 (&*>?  ,      -,r`   c           
     2   | j         r| j        t          j        u r| j        s| j        m| j                            ddg | j                            j                  t          j	                  t          j
                  ddj        id           | j        j        v r/| j                            dt!          | j                             dS fd	| j        j        D             }|r|d
         nd}| j        0| j                            ddj        | j        j        |d           |`| j                            d           t%          j        t(          j        t,          j        d          | _        |                                  dS || _        d| _        | j                            dt!          | j                             |                     |           dS | j        )| j                            ddddj        id           dS dS )z
        Handle a version negotiation packet.

        This is used in "Incompatible Version Negotiation", see:
        https://datatracker.ietf.org/doc/html/rfc9368#section-2.2
        Nr  rG  r  r  r  r  z7Version negotiation packet contains protocol version %sc                &    g | ]}|j         v |S rt   )r  )r\   xr  s     r^   r?  zFQuicConnection._receive_version_negotiation_packet.<locals>.<listcomp>@  s1       111 111r`   r   version_information)server_versionsclient_versionschosen_versionz(Could not find a common protocol versionr   Tz!Retrying with protocol version %sr  r8  r  r:  )r   r  r   r   r  r   r  ry   rr   r?  r>  r=  r  r  r  r  r2   r   r   r  r)   r,  r*   r-  r   r.  r  )r   r  r  commonr  s    `   r^   rP  z2QuicConnection._receive_version_negotiation_packet  s    ON	2>>>9 ?  ,!++(+"$+/+<+H+H & 2, , %-V->$?$?$,V-C$D$D# # !)&*>?
 
 ,   ( } 999""M+DM::      ,?  F +1:VAYYdN ,!++(/+1+D+/+>+Q*8  ,    %""#MNNN$*$?,;,4"L% % %!
 !!!*DM48D1L3'66   MMcM"""""  ,!++(*#6 (&*>?  ,      -,r`   c           	        d}t          | j                  t          d| j                  k     rt	          j        | j        j                  }| j        }| j        	                    t          ||t	          j        d                               || j        |<   | xj        dz  c_        d}t          | j                  t          d| j                  k     |r	d| _        dS dS )z.
        Generate new connection IDs.
        FrP   r@  rY  r   TN)r  r   r]  r   rl  rm  r   rn  r   r  r   r   r   )r   addedrk   seqs       r^   r/  z(QuicConnection._replenish_connection_idsn  s     $/""SD,S%T%TTT*T0EFFC$CO"" $'*,*R..     +.D"3'!#E $/""SD,S%T%TTT  	)$(D!!!	) 	)r`   c                    | j                             dt          |j                  |j        t          | j                  dz              | j                            |j                   dS )z5
        Retire a destination connection ID.
        zRetiring CID %s (%d) [%d]r   N)r  r  rr   rk   r   r  r(  r  )r   rS  s     r^   r  zQuicConnection._retire_peer_cid  sj     	']&'')+,,q0		
 	
 	
 	#**=+HIIIIIr`   c                    | j                                         D ]D\  }}| j        |         j                            |j                   |                    d           Ed S r   )r   r  r   r  r  r  seek)r   r   r  s      r^   r  z QuicConnection._push_crypto_data  s`    .4466 	 	JE3 '.44SX>>>HHQKKKK	 	r`   c                    d| _         d S )NT)r'  r  s    r^   rx  zQuicConnection._send_probe  s    "r`   r  c                    t          |          t          k     rdS |t           d         }| j        j        r|| j        j        k    rdS | j        D ]}|j        r||j        k    r dS dS )z
        RFC 9000 10.3.1: detect a stateless reset by matching the trailing
        16 bytes of the UDP datagram against the stateless reset tokens
        the peer has shared with us.
        FNT)r  r(   r   r   r   )r   r  r@  rk   s       r^   rY  z"QuicConnection._is_stateless_reset  s     x==555544556N0	===4+ 	 	C( Uc6O-O-Ottur`   r  c                :	   	 t          t          |                    }n4# t          $ r' t          t          j        t          j        d          w xY w| j        9|s7| j        	                    dd| j        
                    d|          	           | j        s>d
D ];}t          ||          )t          t          j        t          j        | d          <|s|j        | j        k    r&t          t          j        t          j        d          | j        r6|j        | j        k    r&t          t          j        t          j        d          | j        r6|j        | j        k    r&t          t          j        t          j        d          |j        1|j        dk     r&t          t          j        t          j        d          |j        1|j        dk    r&t          t          j        t          j        d          |j        1|j        dk    r&t          t          j        t          j        d          |j        >|j        t.          k     r.t          t          j        t          j        dt.                     |j        x|j        }| j        s4|j        |j        vr&t          t          j        t          j        d          |j        | j        k    r&t          t          j        t          j        d          |s||j        |j        | _        |j        '|j        dz  | _        | j        r| j        | j         _        | j        r(| j!        j"        dk    r|j#        |j#        | j!        _#        |j        | _$        |j        |j        | _%        |j&        g|j&        dz  | _'        | j(        j)        }| j'        }|dk    r|dk    rtU          ||          | _+        n#|dk    r|| _+        n|dk    r|| _+        nd| _+        |j,        | _-        dD ]+}t          |d|z             }	|	t]          | d|z   |	           ,| j/        ra|j        \|j        }
g }| j/        D ]@}tU          ||
          }|| j0        k    r#|r||d         k    r|1                    |           A|| _/        dS dS dS )z
        Parse and apply remote transport parameters.

        `from_session_ticket` is `True` when restoring saved transport parameters,
        and `False` when handling received transport parameters.
        r;  z)Could not parse QUIC transport parametersr   Nr  parameters_setremoteowner
parametersr  )r4  preferred_addressr5  r   z is not allowed for clientsz+initial_source_connection_id does not matchz1original_destination_connection_id does not matchz)retry_source_connection_id does not matchr   z1active_connection_id_limit must be no less than 2r^  z ack_delay_exponent must be <= 20rA   zmax_ack_delay must be < 2^14z max_udp_payload_size must be >= zJversion_information's chosen_version is not included in available_versionszFversion_information's chosen_version does not match the version in useg     @@r   rI  )rE  max_stream_data_bidi_localmax_stream_data_bidi_remotemax_stream_data_unirF  rG  initial__remote_)2r3   r   rM  r   r)   TRANSPORT_PARAMETER_ERRORr*   r  r   r  encode_transport_parametersr   getattrinitial_source_connection_idr  r4  r  r5  r  ro  ack_delay_exponentry  max_udp_payload_sizer6   r  r  r  r   VERSION_NEGOTIATION_ERRORr   r  r   r  r   r   r   r  r   max_idle_timeoutr  r   ru  r]  r3  r?  r  setattrr/  r.  r  )r   r  r  quic_transport_parametersattrr  localr  paramr   peer_maxcappedr   r   s                 r^   r  z*QuicConnection._parse_transport_parameters  s   		(FD!!!) )%%  	 	 	%(B(/I   	 (1D(''$&&BB"/H C   (     	   4d;;G-#0#J#0#7)-&J&J&J    H # d	)F<= = *,F,3"O   
  )L;< < *,F,3"U   
  )D34 4 *,F,3"M    *DP-H1LL),F,3"U    *<H-@2EE),F,3"D    *7C-;uDD),F,3"@    *>J-B,- - *,F,3#:7#: #:    )<H&?&S# +:.AB B .#0#J#0#74	    '59TTT-#0#J#0#71	    # 	(;G-@ / )6B-;fD *
 , J/3/IDJ,N2a77-CO .C 4 *= , %?K)D 3 %5A):VC ) '4E2FqyyVaZZ/25&/A/A,,/4,,!/5,,/2,%= 	,
 
	9 
	9E 5zE7IJJE j50%888 !	+)>J0EH "F* ( (1h''$111 2"&&*"4"4MM$'''$*D!!!	+ 	+JJs	     1Ac                n   t          | j        | j        t          | j        j        dz            | j        j        | j        | j	        | j
        | j        j        | j        j        | j        d| j        j        | j        j        r
dt           z  nd | j        d         j        t'          | j        | j        j                            }| j        rhd |_        d |_        | j        j        |_        d |_        d|_        d|_        d	|_        | j        d
f|_        | j        g| j        j        |j        _         n| j!        |_"        | j#        |_$        | j%        7| j%        &                    dd| j%        '                    d|                     tQ          d| j)        z            }tU          ||           |j+        S )Ni  rb     Qr   )r  r  )r  ro  r  initial_max_data"initial_max_stream_data_bidi_local#initial_max_stream_data_bidi_remoteinitial_max_stream_data_uniinitial_max_streams_bidiinitial_max_streams_unir  ry  r?  quantum_readinessr   r  i  i   s   ORIGr`   r  r  r  r  r  rS   r  ),r.   r   r   rd   r   ru  r   r   r   r   r   r   r   r   r?  quantum_readiness_testr6   r   r   r/   r  r  r   r  ry  ro  r  google_connection_optionsr   greased_transport_parameterr   r  r  r  r4  r  r5  r   r  r  r   r.  r4   r  )r   r  r  s      r^   r  z.QuicConnection._serialize_transport_parameters  s   $;#='+'M !4!AD!HII!17/3/U040W(,(G%)%A%G$($?$E)-)Q$($7$O &=111"&/!"4"J 6#}#'#6#I! ! !'%
 %
 %
!0 ? 	 <@%86:%3#> &@ ?C%;=A%:@E%=BI%?6E%A
 /P$7P%9LL 8 &H 0 &@
 (''$&&BB!.G C   (    a$"99:::&s,EFFFxr`   rJ  r   c                j    | j                             d| j        j        |j                   || _        d S )Nz%s -> %s)r  r  r  r   )r   rJ  s     r^   r^  zQuicConnection._set_state  s.    :t{'7DDDr`   c                0   |r| j         }| j        }| j        }n| j        }| j        }| j        }|rM|d         j        dz  |k     r9|                    d          }d|_        ||_	        |r|d         j        dz  |k     9| j        s| j        sd| _
        d S d S d S )Nr   r   F)r
  r  r  r	  r  r  r}   rb  r  r  r)  )r   r  r  r  r  r  s         r^   r2  zQuicConnection._unblock_streams  s     	9%)%E"6K"7OO%)%M"7K"8O 	C/!"4">!"Ck"Q"Q$((++F %F,BF)  	C/!"4">!"Ck"Q"Q
 ) 	2$2K 	2,1D)))	2 	2 	2 	2r`   	directiontls.Directionr  tls.CipherSuitesecretc                   | j         rN| j        G| j        s@| j        | _        d| _        | j                            dt          | j                             | j        j        }|| j         |t          j
        j        k    k    }t          |         |j                 }|                    | d| j	        j                                         d|                                 d           |                                 | j        |         }|t          j
        j        k    r$|j                            ||| j                   dS |j                            ||| j                   dS )zj
        Callback which is invoked by the TLS engine when new traffic keys are
        available.
        NTr   
)r  r  r   )r   r   r  r  r  r  r2   r   secrets_log_filer   	DirectionDECRYPTSECRETS_LABELSr   r  client_randomhexr  r   ENCRYPTr  setuprU  )	r   r  r   r  r  r  	label_rowlabelr  s	            r^   r  z"QuicConnection._update_traffic_key  s~    O		+77 8 !7DM26D/L02I$-2X2X    .?'I9N,NOI"9-ek:E""JJ4815577JJ&**,,JJJ   ""$$$u%---K)&$-       K)&$-      r`   	challenger   c                   || j         |<   t          | j                   t          k    rZt          t	          | j                                                             }| j         |= t          | j                   t          k    Xd S d S rZ   )r   r  MAX_LOCAL_CHALLENGESnextiterr  )r   r  r   keys       r^   _add_local_challengez#QuicConnection._add_local_challenge  s|    ,8y)$()),@@@ tD27799::;;C&s+	 $()),@@@@@@@r`   r  r8   c                   d }| j         t          j        j                 j                                        rE| j         t          j        j                 }| j        t          j        j                 }t          j        }n^| j         t          j        j                 j                                        r)| j         t          j        j                 }t          j        }nd S | j	        t          j        j                 }| j
        }| j        j        }	| j        }
| j        }	 |j        |j        |k    r+| j        s$|	                    |          | _        | j        d S |                    ||           |r|j        sP|j        sIt-          j        d          }|                     ||           |                     ||           d|_        |j        #|j        |k    r|                     |||           | j        r|                     |           d| _        |j        rB|                     ||j        d	                    |j                                         |j        B| j         r5d}| j!        D ]"}|j"        s| #                    ||
           d}#|rd| _         | j$        r:| j$        D ]}| %                    ||           | j$        &                                 | j'        rc| j(        r'| )                    |tT          j+        | j,                   | j-        r'| )                    |tT          j.        | j/                   d| _'        | j0        | j1        | j2        fD ]}|j3        dz  |j4        k    r6|xj4        dz  c_4        | j5        6                    d|j7        |j4                   |j4        |j8        k    r|9                    |j:        tv          | j<        |f          }|=                    |j4                   |j4        |_8        |9|j>        ?                    |@                    |j:        |j4                             | jA        }|r6|D ]}|jB        jC        r|jD        rN|jB        jE        dz  |jD        k    r6|xjD        dz  c_D        | j5        6                    d|jF        |jD                   |jG        |jD        k    r|9                    tT          jH        t          | jJ        |f          }|=                    |jF                   |=                    |jD                   |jD        |_G        |9|j>        ?                    |K                    |jD        |jF                              |&                                 | jL        r4| M                    || jL                   | jL        &                                 | j        r| M                    |d           d| _        |$|jN        jO        s| P                    |||           | jQ        rn| jQ                                        }	 | R                    ||tT          jS                   n*# t          $ r | jQ        U                    |           Y nw xY w| jQ        ng }d	}t          |
          }d	}|jW        }|jX        }|jY        }|j9        }| jZ        }|| j[        z
  }t          }	 ||k     r |
|         }|dz  }|jN        } |jB        }!| jC        rq|!jC        rj| j5        6                    d|jF         d           | j]        |jF        = | j^        _                    |jF                   | jA        `                    |           |!ja        r| b                    ||           | jc        r| d                    ||           n|je        s| jO        s||f                                z
  |z
  }"|"dk     r||
|<   |dz  }n| jE        |z   }#|jg        }$|$|#k     r|$}#| h                    |"|#          }%|%|%\  }&}'}(})}*}+|jF        }, ||'|+| ji        |(|)f            |||,|(|&           |A|j>        ?                    |j                    t          |'dz            |&|(|,                     | jE        |*z
  }-||-z  }| xj[        |-z  c_[        |-d	k    r|?                    |           ||
|<   |dz  }||k      ||k     r|
|         |
|<   |dz  }|dz  }||k     |
|d = |
l                    |           n@# ||k     r|
|         |
|<   |dz  }|dz  }||k     |
|d = |
l                    |           w xY w|jm        rd S |	n                    |           N)NTr  rP   )r  r  )r  r   r  r  r  )r  Fr   )r  rS  )r  r   )r  r   r6  r   zLocal %s raised to %dr  rF  z,Stream %d local max_stream_data raised to %drJ  probecommentr  r  r  )r  r  r   r   r  z
 discarded)r  r  r^  r   )or   r   r  r  r  r  r   r,   rT  r  r   r  _pacerr  r   r#  r'  next_send_timer   r  r   r   rl  rm  _write_path_challenge_framer  _write_ack_framer%  _write_handshake_done_framer   _write_path_response_framer4  r   r   r   _write_new_connection_id_framer(  !_write_retire_connection_id_framer  r)  r  _write_streams_blocked_framer*   STREAMS_BLOCKED_BIDIr  r  r  r  r   r   r   r   r   r  r  r   r   r  r   CONNECTION_LIMIT_FRAME_CAPACITYr  push_uint_varr   r  rH  r  r  r  r  rv  r}   r  MAX_STREAM_DATAMAX_STREAM_DATA_FRAME_CAPACITYr  rK  r&  _write_ping_framer  buffer_is_empty_write_crypto_framer$  _write_datagram_framer=  r9   
appendleftr  r  _flight_capacity_aead_tag_sizer  r  r   r  r  rg  discardstop_pending_write_stop_sending_framereset_pending_write_reset_stream_framer  r<  r  prepare_stream_frameon_data_deliveryr  rf   r  packet_is_emptyupdate_after_send).r   r  r   r  crypto_streamr  ry   r  handshake_completepacerstreams_queuer   r  all_sentrS  r   r6  r  dirty_limitsr  datagram_pendingr   	write_idx	queue_lenread_idx	_b_buffer	_b_flight_b_aead_b_start_framer  _remote_data_remaining_push_stream_frame_bodyr  r  flight_space
max_offsetsdrresultf_datar   f_offsetstop_offsetprevious_send_highestframe_overheadr}   r   s.                                                 r^   r  z!QuicConnection._write_application  s    ,0=*+099;; 	]39#45F 01BCM(0KK]39-.3<<>> 	]39#56F(1KKFSY./ "5
!+(]	1
 $(;(;) )<"'"6"63"6"?"??.E  f555! \$1 =\5V = "
1I44 '9 5    --"+, .    9=L5 <+0C0C))'C)PPP / 944W4EEE38D0 #4 =33 '<3QRS3T 4    !2::<<<	 #4 = ( 	6#H)- - -,5 - ??(/} @    (-H 605- . 8+/+F  >>$+_ ?     /55777 0 :1 99$+'4'I"&"? :   
 0 99$+'4'H"&"> :   
 5:D1 (0/  E
 zA~33q(**3UZ   {ej00%11!,%D$($F*/	 2   ))%+666%*[
 (3#6== , J J/4/?,1K !K !" !"    5L (%* & &F 2 ! 4
"O:Q> 67 7 44944**J","8   9!78 8 &11)9%C$($E*0	 2   ))&*:;;;))&*FGGG<B<X9 (3#6== , I I,2,H.4.> !J !" !"   ""$$$ ! +&&w0BCCC"((*** " ,&&w&@@@&+# (1E1U(((#5 )   
 ) 
#'#:#B#B#D#D .. '-#0#E /    
 -   +667GHHHE ) 
 &(DIM**IH  I0I,G$0N#4%58R%R"&<#V+***84FMH#]F%H ) !h.B !**+QV5E+Q+Q+QRRR M&*:;.2263CDDD2::6BBB , W66wv6VVV+ 9)66wv6VVVV#. 7)v7M 7)'09>>3C3C'Cg'M (",,7=M)4%NI! &,%:=S%S
$;++),J!'!<!<\:!V!V!- !' & * ( + 5 .(.(8I*N * . & 7!); 7	   43 )9h    ,7 ' : A A$0$D$D(,Z!^(<(<(.(02;	 %E %& %&!" !" !" $*#8;P#PD2d:2 66$>66#axx $F 3 3 3 (/5M),NI[ **` **/<X/FM),NIMH ** ")**-$$T**** **/<X/FM),NIMH ** ")**-$$T****& 1''C'000{]	1s   #"Y $Y-,Y-Hd =ec                   | j         |         }|j                                        sd S | j        |         }| j        |         }	 |t
          j        j        k    rt          j        }nt          j	        }|
                    ||           |j        |                     |||           |j        j        s|                     |||          rd| _        | j        rm| j        sf|t
          j        j	        k    s3| j         t
          j        j	                 j                                        s|                     |d           d| _        |j        rd S )NTr  r  Fr  r  )r   r  r  r   r  r   r  r  r,   r  r  r#  r  r  r(  r)  r'  r   r'  r5  )r   r  r   r  r  r7  r  ry   s           r^   r  zQuicConnection._write_handshakeO  sj    u%{##%% 	F,U3U# 		))),4,6  f555 |'%%gU%LLL !'7 0++#5 ,   0 +0D' #	,0	, SY000=)<=BKKMM 1 &&w&@@@&+#& A 	r`   c                F   ||j         z
  }t          |dz            | j        z	  }t          |j                  }t          |j        dz
  d          }t          |dz
  dz  d          }||k    rc|dk    r]t          |j                  |k    r1|j                                         t          |j                  |k    1t          |j                  }ddt          |d          z  z   }	|                    t          j
        |	| j        ||j        f          }
t          |
|j        |          }d |_        | j        9|j                            | j                            |j        |                     |dk    r'|j        dz  dk    r|                     |d	
           d S d S d S )Nr  r   r   rG   r@  r  )rangesdelayrP   zACK-of-ACK triggerr  )rf  rd   r   r  r]  r(  remaining_buffer_spaceshiftr  r*   ACKr  ra  r   r#  r   r   r  r  r  r'  )r   r  r  r  r  r  ranges_countmax_payloadmax_ranges_by_spacer  r  rR  s               r^   r  zQuicConnection._write_ack_frame{  s    %55		G 3448VV 5?++ '81<a@@!;#3":A>>---2E2I2I eo&&)<<<%%''' eo&&)<<<u//LSq1111!!)!>?	 " 
 
  U_6GHH (&--!22 ?) 3     A::'/!3q88""74H"IIIII :88r`   c                   |>|t           j        j        t           j        j        fv rt          j        }t          j        }d}|                    d          }t          |          }|i|
                    t          j        t          |z             }|                    |           |                    |           |                    |           n}|
                    t          j        t           |z             }|                    |           |                    |           |                    |           |                    |           | j        7|j                            | j                            |||                     d S d S )Nr  r  r  r   )r   r  r  r  r)   APPLICATION_ERRORr*   r-  encoder  r  APPLICATION_CLOSE APPLICATION_CLOSE_FRAME_CAPACITYr$  r  r  TRANSPORT_CLOSE_FRAME_CAPACITYr   r   r  r  )	r   r  r   r   r   r   reason_bytesr  r  s	            r^   r  z,QuicConnection._write_connection_close_frame  s    %CI,=sy?R+S"S"S&8J&.JM$++F33L))%%/9MI &  C j)))m,,,NN<((((%%-7-G &  C j)))j)))m,,,NN<((( (&--!??))"/ @       )(r`   c           
        |j         }dt          |j                  z   }|j        |j                                        z
  |j        z
  }|                    ||z
            }||\  }}	}
|                    t          j
        ||j        |
|
t          |          z   f          }t          ||
|           | j        3|j                            | j                            ||
                     dS dS )NrS   TF)r  r   next_offsetr,  r  r<  r-  	get_framer  r*   r  r4  r  r   r   r   r  r$  )r   r  r  r  r  rO  rG  framerK  f_finrL  r  s               r^   r)  z"QuicConnection._write_crypto_frame  s    ]6+=>>>$w';';'='==@VV 	   !>??&+#FE8%%$'8c&kk12	 C #3&999  ,*11%99&(KK   4ur`   r*   c                h   |t           j        k    sJ t          |          }dt          |          z   |z   }|                    ||          }|                    |           |                    |           | j        3|j        	                    | j        
                    |                     dS )zl
        Write a DATAGRAM frame.

        Returns True if the frame was processed, False otherwise.
        r   r  Nr:  T)r*   r=  r  r   r  r$  r  r   r   r  r>  )r   r  r  r   r  
frame_sizer  s          r^   r*  z$QuicConnection._write_datagram_frame  s     ]?????Tv...7
!!*z!BB&!!!t (&--!77v7FF   tr`   c                    |                     t          j        t          | j                   | j        3|j                            | j                                                   d S d S )N)r  r  )	r  r*   HANDSHAKE_DONEHANDSHAKE_DONE_FRAME_CAPACITYr  r   r   r  rC  )r   r  s     r^   r  z*QuicConnection._write_handshake_done_frame  sx    (24 	 	
 	
 	
 (&--!==??     )(r`   c                   d}|                     t          j        t          | j        |f          }|                    |j                   |                    |           |                    t          |j	                             |
                    |j	                   |
                    |j                   d|_        | j                            t          j        |j	                             | j        G|j                            | j                            |j	        ||j        |j                             d S d S )Nr   r  Tr{  rR  )r  r*   NEW_CONNECTION_ID NEW_CONNECTION_ID_FRAME_CAPACITYr  r$  r   
push_uint8r  rk   r  r   r   r   r  r   ConnectionIdIssuedr   r   r[  )r   r  rS  rT  r  s        r^   r  z-QuicConnection._write_new_connection_id_frame  sD    !!+57')	 " 
 
 	-7888/***s=,--...}()))}:;;;!%F5MDUVVVWWW (&--!@@"/"3$3$1$A*7*M	 A       )(r`   c                   |                     t          j        t                    }|                    |           |                                 | j        5|j                            | j        	                    |                     d S d S Nr  r;  )
r  r*   PATH_CHALLENGEPATH_CHALLENGE_FRAME_CAPACITYr  pad_datagramr   r   r  rh  r   r  r  r  s       r^   r  z*QuicConnection._write_path_challenge_frame8  s     !!(3P " 
 
 	y!!!
 	 (&--!==9=MM     )(r`   c                   |                     t          j        t                    }|                    |           |                                 | j        5|j                            | j        	                    |                     d S d S rq  )
r  r*   PATH_RESPONSEPATH_RESPONSE_FRAME_CAPACITYr  rt  r   r   r  rk  ru  s       r^   r  z)QuicConnection._write_path_response_frameK  s     !!'2N " 
 
 	y!!! 	 (&--!<<)<LL     )(r`   	list[int]c                B   |                     t          j        t          | j        t          |          f           | j                            d|rd| dnd|j                   | j	        3|j
                            | j	                                                   d S d S )Nr  zSending PING%s in packet %dz ()r  )r  r*   r  PING_FRAME_CAPACITYr  tupler  r  r  r   r   r  rn  )r   r  r  r  s       r^   r'  z QuicConnection._write_ping_frame]  s     	(*++	 	 	
 	
 	
 	)&.OOOOOB!	
 	
 	
 (&--d.?.Q.Q.S.STTTTT )(r`   c                   |                     t          j        t          |j        j                  }|j                                        }|\  }}}|                    |           |                    |           |                    |           | j        7|j	        
                    | j                            |||                     d S d S )Nr   r  r  rp  )r  r*   RESET_STREAMRESET_STREAM_FRAME_CAPACITYr  on_reset_deliveryget_reset_framer$  r   r   r  rt  )r   r  r  r  rd  r   rq  frame_stream_ids           r^   r2  z(QuicConnection._write_reset_stream_framep  s    
 !!$10M3 " 
 

 --//27/
J/****%%%*%%% (&--!;;))- <       )(r`   c                   |                     t          j        t          | j        |f          }|                    |           | j        4|j                            | j        	                    |                     d S d S )Nr  )
r  r*   RETIRE_CONNECTION_IDRETIRE_CONNECTION_ID_CAPACITYr  r$  r   r   r  r|  )r   r  r   r  s       r^   r   z0QuicConnection._write_retire_connection_id_frame  s     !!.2:)+	 " 
 
 	/*** (&--!CCOTT     )(r`   c                   |                     t          j        t          |j        j                  }|j                                        }|                    |j                   |                    |j	                   | j
        @|j                            | j
                            |j	        |j                             d S d S )Nr  r  )r  r*   STOP_SENDINGSTOP_SENDING_FRAME_CAPACITYr  on_stop_sending_deliveryget_stop_framer$  r}   r   r   r   r  r  )r   r  r  r  rd  s        r^   r0  z(QuicConnection._write_stop_sending_frame  s    
 !!$10O< " 
 

 ..00%/***%*+++ (&--!;;$/5? <       )(r`   c                .   |t           j        k    }|                    |t          | j        |f          }|                    |           | j        D|j                            | j        	                    |t           j        k    |                     d S d S )Nr  r  )
r*   r  r  STREAMS_BLOCKED_CAPACITYr  r$  r   r   r  r  )r   r  r   r6  is_unir  s         r^   r!  z+QuicConnection._write_streams_blocked_frame  s     }@@!!-5 	 " 
 
 	%    (&--!>>&0M4U&U ?       )(r`   )r8  r   r4  r9  r5  r9  r6  r:  r7  r;  rV   r<  )rV   rd   )rV   r  )rV   r  )rV   r  )rV   r   )rV   rl   )rV   r9  )rV   rf   )rV   r<  )r   rd   r   r   r   rU   rV   r<  )r   r   r  r   rV   r<  )r  r   rV   r  r   )rV   r!  )r  r   rV   r!  )r  r   rV   r<  )rV   r2  )r  rl   r   r   r  r   rV   r<  )r}   rd   r   rd   rV   r<  )r  rd   rV   r<  )r  rl   rV   r<  )r}   rd   r  rl   r  rf   rV   r<  )r  rU   rV   r<  )r   rd   r}   rd   rV   r<  )r  rf   r  r   rV   r<  )r   rz   rV   r<  )r   r   rV   r   )r   rd   r}   rd   rV   r>   )r}   rd   rV   r>   )r  r  rV   r<  )r  rl   rV   r<  )r}  r   r   rd   r  r   rV   r<  )r  rU   r;  rU   rV   r<  )r  r7   r  r<   r  rd   rV   r<  )r  r7   r6  r   rV   r<  )r  r7   r  rf   rV   r<  )r  r7   rV   r<  )r  r7   r  r>   rV   r<  )r  r7   rS  r   rV   r<  )r  r7   r  r  rV   r<  )r  r7   r  rd   rV   r<  )r  r7   r   rd   rV   r<  )r}  r   r  rl   rx  rf   rV   r  )r  r+   rC  rl   r  r   rV   r<  )r  r+   r  r   rV   r<  )rS  r   rV   r<  )r  rl   rV   rf   )r  rl   r  rf   rV   r<  )rJ  r   rV   r<  )r  rf   rV   r<  )
r  r  r   rz   r  r  r  rl   rV   r<  )r  rl   r   r   )r  r8   r   r   r  r   rV   r<  )r  r8   r   rz   r  r   rV   r<  )r  r8   r  r<   r  r   rV   r<  )r  r8   r   rz   r   rd   r   r   r   rU   rV   r<  )r  r8   r  r<   r  r>   rV   rf   )r  r8   r  rl   r   r*   rV   rf   )r  r8   rV   r<  )r  r8   rS  r   rV   r<  )r  r8   r  rl   rV   r<  )r  r8   r  ry  )r  r8   r  r>   rV   r<  )r  r8   r   rd   rV   r<  )r  r8   r   r*   r6  rd   rV   r<  )or   r   r   __doc__	__slots__r   propertyr  r  r  r  r  r  r8  r4  r  r  r  r)   r[  r  r  r  r   r&  r  r1  r5  r  r  r  r  r  r  r  r  r  r  r  r  r.  r  r  r  rK  r  r  r  rS  r|  r  r  r  r  r  r  r  r  r  r  r  rz  r  r  r{  r}  r  r~  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r_  rR  rP  r/  r  r  rx  rY  r  r  r^  r2  r  r  r  r  r  r  r)  r*  r  r  r  r  r'  r2  r   r0  r!  rt   r`   r^   r   r     s[	        cIR <@37BFBFa
 a
 a
 a
 a
 a
F " " " X" - - - X- , , , X,U U U U) ) ) )/ / / / # # # X# 8 8 8 X8 ' ' ' X'    X% % % %" (0!%	' ' ' ' ',   0n< n< n< n<`3 3 3 3 3   8
) 
) 
) 
): : : :2   i9 i9 i9 i9V	6 	6 	6 	6( ( ( (' ' ' '- - - - ?D9 9 9 9 9) ) ) )(6T 6T 6T 6Tp	 	 	 		 	 	 	
 
 
 
: : : :% % % %
! 
! 
! 
!1 1 1 1       &	% 	% 	% 	%2 2 2 2h0 0 0 0d5 5 5 5L8 L8 L8 L8\2
 2
 2
 2
h&D &D &D &DPr2 r2 r2 r2h   4E E E E@* * * *@- - - -,< < < <>; ; ; ;:: : : ::c c c cJ   ,X X X X@ @ @ @() ) ) )2U U U U:3 :3 :3 :3x+) +) +) +)Z
 
 
 
:83 83 83 83t&3 &3 &3 &3P   .	 	 	 		 	 	 	; ; ; ;   9 9 9 9"0 0 0 03 3 3 3) ) ) ), , , ,+ + + +$@ @ @ @ ',	\2 \2 \2 \2 \2|0 0 0 0d\ \ \ \|) ) ) )*
J 
J 
J 
J   
# # # #   ( 8=_+ _+ _+ _+ _+BE E E EN   2 2 2 2$) ) ) )V, , , ,r1 r1 r1 r1h	* * * *X.J .J .J .J`+ + + +Z   8   0      >   &   & =?U U U U U&   4   "   ,     r`   r   )rT   rU   rV   rW   )rc   rd   re   rd   rV   rf   )rk   rl   rV   rU   )ry   r,   rV   rz   )r}   rd   rV   rf   )r   rf   r}   rd   rV   rf   rt   )
__future__r   ro   loggingrl  collectionsr   dataclassesr   enumr   	functoolsr   r   typingr	   r
   r   r8  r   rP  r   r  r   _compatr   r   _hazmatr   r   r   r   r   r   r   r   r   r   r  r   rL  r   re  r   r  r    r!   r"   r#   r  r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   packet_builderr5   r6   r7   r8   r9   recoveryr:   r;   r<   r  r=   r>   r?   	getLoggerr	  r  r  r  rT  r  r[   r  ri  r  r^  r  STREAM_FLAGSrN  r
  r&  r   ACK_FRAME_CAPACITYr^  r#  rj  r&  rm  rs  rx  r|  r  r  r  r  r_  rb   rj   rr   rw   r   rQ  rN  rx   r|   r   r   r   r   r   	Exceptionr   LoggerAdapterr   r   r   r   r   ra   r   r   r   r  r   rt   r`   r^   <module>r     s   " " " " " " "   				       ! ! ! ! ! !       ( ( ( ( ( ( ( ( / / / / / / / / / / (000000''''''       9 9 9 9 9 9 9 9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 5 4 4 4 4 4 ? ? ? ? ? ? 3 3 3 3 3 3       L L L L L L L L L L L L                                     &              I H H H H H H H H H C C C C C C C C C C		6	"	" 									          %    #$q+<'<#<  "#&7"7  ! !"Q):%:!: !$::=WW ! !& $  !&7"77  !$5 5 !&7"77 00 !"Q):%:!: ; ; ; ;
 
 
 
1 1 1 1
 4W3V~3V3V3V  V V V V CI-SY/ci1#)+&	(9CI-<     0 0 0 0       > > > > B B B B       
 
 
 
 
) 
 
 
? ? ? ? ?G1 ? ? ?
            '   
X 
X 
X 
X 
X 
X 
X 
X         Y#$& 
A? A? A? A? A? A? A? A? A? A?r`   