
    Rh                        g d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlmZ ddlmZmZ ddlmZ ej"                  Z	 ddlZddlmZmZmZmZ d	Zd
Z e	j8                         ZdZdgZ e ed      rdZedgz  Zej2                  dk(  rdZedgz  ZefdZ!d Z"d Z#d Z$d Z% G d d      Z&er G d de&      Z' G d de&      Z( G d de)      Z*dEdZ+ej2                  dk7  rdFdZ,ndFdZ, G d de)      Z-d  Z.ej2                  dk(  r G d! d"e)      Z/d# Z0d$Z1d%Z2d&Z3d'Z4 e5h d(      Z6 e7d) e6D              Z8d*Z9d+Z:e9e:fZ;d,e<d-e=e<ffd.Z>d/ Z?d0 Z@dGd1e<fd2ZAd1e<fd3ZB G d4 d5e)      ZCd6 ZDd7 ZE G d8 d9e*      ZFd: ZGej2                  dk(  r d; ZHej                  ej                  hZKdHd<ZLn*ddlMZM e eMd=      reMj                  ZOneMj                  ZOdHd>ZLej2                  dk(  r3d? ZQd@ ZR ej                  e(eQ       dA ZTdB ZU ej                  e'eT       ydC ZQdD ZR ej                  e(eQ       y# e$ r ej2                  dk(  r dZY w xY w)I)ClientListenerPipewait    N   )util)AuthenticationErrorBufferTooShort)	reduction)WAIT_OBJECT_0WAIT_ABANDONED_0WAIT_TIMEOUTINFINITEwin32i    g      4@AF_INETAF_UNIXAF_PIPEc                 0    t        j                         | z   S Ntime	monotonic)timeouts    1/usr/lib/python3.12/multiprocessing/connection.py_init_timeoutr   <   s    >>g%%    c                 0    t        j                         | kD  S r   r   )ts    r   _check_timeoutr   ?   s    >>ar   c                     | dk(  ry| dk(  r)t        j                  dt        j                               S | dk(  r;t        j                  dt	        j
                         t        t              fz  d      S t        d	      )
z?
    Return an arbitrary free address for the given family
    r   )	localhostr   r   z	listener-)prefixdirr   z\\.\pipe\pyc-%d-%d- zunrecognized family)	tempfilemktempr   get_temp_dirosgetpidnext_mmap_counter
ValueErrorfamilys    r   arbitrary_addressr/   F   sy     	9	kt7H7H7JKK	9	&< "		T--@A'BGIK 	K .//r   c                     t         j                  dk7  r| dk(  rt        d| z        t         j                  dk(  r%| dk(  rt        t        |       st        d| z        yyy)zD
    Checks if the family is valid for the current environment.
    r   r   zFamily %s is not recognized.r   N)sysplatformr,   hasattrsocketr-   s    r   _validate_familyr5   T   sc     ||w6Y#67&@AA
||w6Y#6vv&;fDEE ' $7r   c                     t        |       t        k(  ryt        |       t        u r| j                  d      ryt        |       t        u st	        j
                  |       ryt        d| z        )z]
    Return the types of the address

    This can be 'AF_INET', 'AF_UNIX', or 'AF_PIPE'
    r   z\\r   r   zaddress type of %r unrecognized)typetuplestr
startswithr   is_abstract_socket_namespacer,   )addresss    r   address_typer=   `   s\     G}	g#	'"4"4V"<	g#	!B!B7!K:WDEEr   c                       e Zd ZdZddZd Zd Zd Zd Zd Z	e
d        Ze
d	        Ze
d
        Zd Zd ZddZd ZddZddZd ZddZd Zd Zy)_ConnectionBaseNc                     |j                         }|dk  rt        d      |s|st        d      || _        || _        || _        y )Nr   zinvalid handlez6at least one of `readable` and `writable` must be True)	__index__r,   _handle	_readable	_writable)selfhandlereadablewritables       r   __init__z_ConnectionBase.__init__v   sO    !!#A:-..HJ J!!r   c                 >    | j                   | j                          y y r   rB   _closerE   s    r   __del__z_ConnectionBase.__del__   s    <<#KKM $r   c                 2    | j                   t        d      y )Nhandle is closed)rB   OSErrorrM   s    r   _check_closedz_ConnectionBase._check_closed   s    <<,--  r   c                 2    | j                   st        d      y )Nzconnection is write-only)rC   rQ   rM   s    r   _check_readablez_ConnectionBase._check_readable   s    ~~455 r   c                 2    | j                   st        d      y )Nzconnection is read-only)rD   rQ   rM   s    r   _check_writablez_ConnectionBase._check_writable   s    ~~344 r   c                 t    | j                   rd| _        t        d      | j                          t        d      )NFzbad message length)rD   rC   closerQ   rM   s    r   _bad_message_lengthz#_ConnectionBase._bad_message_length   s5    >>"DN *++ JJL*++r   c                     | j                   du S )z True if the connection is closedNrB   rM   s    r   closedz_ConnectionBase.closed   s     ||t##r   c                     | j                   S )z"True if the connection is readable)rC   rM   s    r   rG   z_ConnectionBase.readable        ~~r   c                     | j                   S )z"True if the connection is writable)rD   rM   s    r   rH   z_ConnectionBase.writable   r^   r   c                 :    | j                          | j                  S )z+File descriptor or handle of the connection)rR   rB   rM   s    r   filenoz_ConnectionBase.fileno   s    ||r   c                 f    | j                   	 | j                          d| _         yy# d| _         w xY w)zClose the connectionNrK   rM   s    r   rX   z_ConnectionBase.close   s1    <<#$#	 $  $s   ' 	0c                 v   | j                          | j                          t        |      }|j                  dkD  r|j	                  d      }|j
                  }|dk  rt        d      ||k  rt        d      |||z
  }n#|dk  rt        d      ||z   |kD  rt        d      | j                  ||||z           y)	z,Send the bytes data from a bytes-like objectr   Br   zoffset is negativezbuffer length < offsetNzsize is negativezbuffer length < offset + size)rR   rV   
memoryviewitemsizecastnbytesr,   _send_bytes)rE   bufoffsetsizemns         r   
send_bytesz_ConnectionBase.send_bytes   s    sO::>sAHHA:122v:566<v:DAX/00d]Q<==6&4-01r   c                     | j                          | j                          | j                  t        j	                  |             y)zSend a (picklable) objectN)rR   rV   ri   _ForkingPicklerdumpsrE   objs     r   sendz_ConnectionBase.send   s3    ..s34r   c                     | j                          | j                          ||dk  rt        d      | j                  |      }|| j	                          |j                         S )z7
        Receive bytes data as a bytes object.
        r   znegative maxlength)rR   rT   r,   _recv_bytesrY   getvalue)rE   	maxlengthrj   s      r   
recv_bytesz_ConnectionBase.recv_bytes   sa     	 Y]122y);$$&||~r   c                    | j                          | j                          t        |      5 }|j                  }|t	        |      z  }|dk  rt        d      ||kD  rt        d      | j                         }|j                         }|||z   k  rt        |j                               |j                  d       |j                  |||z  ||z   |z          |cddd       S # 1 sw Y   yxY w)zq
        Receive bytes data into a writeable bytes-like object.
        Return the number of bytes read.
        r   znegative offsetzoffset too largeN)rR   rT   re   rf   lenr,   rw   tellr
   rx   seekreadinto)rE   rj   rk   rm   rf   bytesizeresultrl   s           r   recv_bytes_intoz_ConnectionBase.recv_bytes_into   s    
 	_ 	zzH#a&(Hz !233(" !344%%'F;;=D&4-'$V__%677KKNOOAf0%}9; <!	 	 	s   B+C!!C*c                     | j                          | j                          | j                         }t        j	                  |j                               S )zReceive a (picklable) object)rR   rT   rw   rq   loads	getbuffer)rE   rj   s     r   recvz_ConnectionBase.recv   s?     $$S]]_55r   c                 d    | j                          | j                          | j                  |      S )z/Whether there is any input available to be read)rR   rT   _pollrE   r   s     r   pollz_ConnectionBase.poll   s)    zz'""r   c                     | S r    rM   s    r   	__enter__z_ConnectionBase.__enter__      r   c                 $    | j                          y r   rX   rE   exc_type	exc_valueexc_tbs       r   __exit__z_ConnectionBase.__exit__      

r   )TT)r   Nr   )r   )g        )__name__
__module____qualname__rB   rI   rN   rR   rT   rV   rY   propertyr\   rG   rH   ra   rX   ro   ru   rz   r   r   r   r   r   r   r   r   r?   r?   s   s    G	".65, $ $    
$2(526#r   r?   c                   P    e Zd ZdZdZdZej                  fdZd Z	d	dZ
d Zd Zy)
PipeConnectionz
        Connection class based on a Windows named pipe.
        Overlapped I/O is used, so the handles must have been created
        with FILE_FLAG_OVERLAPPED.
        FNc                 d    | j                   }||j                           || j                         y r   )_send_ovcancelrB   )rE   _CloseHandleovs      r   rL   zPipeConnection._close  s%    B~		&r   c                 >   | j                   t        d      t        j                  | j                  |d      \  }}|| _         	 |t        j
                  k(  r1t        j                  |j                  gdt              }|t        k(  sJ d | _         |j                  d      \  }}|t        j                  k(  rt        t        j                  d      |dk(  sJ |t!        |      k(  sJ y #  |j                           xY w# d | _         |j                  d      \  }}w xY w)Nz/concurrent send_bytes() calls are not supportedT
overlappedFrP   r   )r   r,   _winapi	WriteFilerB   ERROR_IO_PENDINGWaitForMultipleObjectseventr   r   r   GetOverlappedResultERROR_OPERATION_ABORTEDrQ   errnoEPIPEr|   )rE   rj   r   errwaitresnwrittens         r   ri   zPipeConnection._send_bytes  s
   }}(  "5 6 6''cdKGBDM
='222%<<
E85G"m333
 !% " 6 6t <#g555 ekk+=>>!8O8s3x'''		 $ " 6 6t <#s   AC) )C<<C? ?Dc                    | j                   rd| _         t        j                         S |dnt        |d      }	 t	        j
                  | j                  |d      \  }}	 |t        j                  k(  r1t	        j                  |j                  gdt              }|t        k(  sJ |j                  d      \  }}|dk(  r5t        j                         }|j                  |j                                |S |t        j                   k(  r| j#                  ||      S 	 t-        d      #  |j                           xY w# |j                  d      \  }}|dk(  r9t        j                         }|j                  |j                                |c cY S |t        j                   k(  r| j#                  ||      c cY S w xY w# t$        $ r)}|j&                  t        j(                  k(  rt*         d }~ww xY w)NF   Tr   r   z.shouldn't get here; expected KeyboardInterrupt)_got_empty_messageioBytesIOminr   ReadFilerB   r   r   r   r   r   r   r   writer   ERROR_MORE_DATA_get_more_datarQ   winerrorERROR_BROKEN_PIPEEOFErrorRuntimeError)	rE   maxsizebsizer   r   r   nreadfes	            r   rw   zPipeConnection._recv_bytes6  s   &&*/'zz|#&C4E%..t||U;?AGBD'":"::&-&D&D!#
E8'=G#*m#;;#;
 &(%;%;D%A
s!8 "

AGGBLLN3#$H G$;$;;#'#6#6r7#CC < OPP!		%'%;%;D%A
s!8 "

AGGBLLN3#$H G$;$;;#'#6#6r7#CC < zzW%>%>>&	s\   %F8 AD" #AF8 1$F8 "D55D8 8AF5	F8 'F52F8 4F55F8 8	G*$G%%G*c                     | j                   s%t        j                  | j                        d   dk7  ryt	        t        | g|            S )Nr   T)r   r   PeekNamedPiperB   boolr   r   s     r   r   zPipeConnection._pollV  s>    ''--dll;A>!CdVW-..r   c                    |j                         }t        j                         }|j                  |       t	        j
                  | j                        d   }|dkD  sJ |!t        |      |z   |kD  r| j                          t	        j                  | j                  |d      \  }}|j                  d      \  }}|dk(  sJ ||k(  sJ |j                  |j                                |S )Nr   r   Tr   )r   r   r   r   r   r   rB   r|   rY   r   r   )rE   r   r   rj   r   leftr   rbytess           r   r   zPipeConnection._get_more_data\  s    ,,.C

AGGCL((6q9D!8O8"s3x$'@((*&&t||TdKGB006KFC!8O8T>!>GGBLLN#Hr   r   )r   r   r   __doc__r   r   r   CloseHandlerL   ri   rw   r   r   r   r   r   r   r     s8    	
 #&-&9&9 	'	(4	Q@	/	r   r   c                       e Zd ZdZer(ej                  fdZej                  Z	ej                  Zn'ej                  fdZej                  Z	ej                  Ze	fdZefdZd Zd
dZd	 Zy)
Connectionzo
    Connection class based on an arbitrary file descriptor (Unix only), or
    a socket handle (Windows).
    c                 (     || j                          y r   r[   rE   rL   s     r   rL   zConnection._closes      4<< r   c                 (     || j                          y r   r[   r   s     r   rL   zConnection._closex  r   r   c                 b    t        |      }	  || j                  |      }||z  }|dk(  ry ||d  }$Nr   )r|   rB   )rE   rj   r   	remainingrn   s        r   _sendzConnection._send}  sA    H	dllC(ANIA~ab'C r   c                     t        j                         }| j                  }|}|dkD  rK |||      }t        |      }|dk(  r||k(  rt        t        d      |j                  |       ||z  }|dkD  rK|S )Nr   zgot end of file during message)r   r   rB   r|   r   rQ   r   )rE   rl   readrj   rF   r   chunkrn   s           r   _recvzConnection._recv  s|    jjl	!m+EE
AAv$"N!"BCCIIeNI !m 
r   c                    t        |      }|dkD  r`t        j                  dd      }t        j                  d|      }| j                  |       | j                  |       | j                  |       y t        j                  d|      }|dkD  r#| j                  |       | j                  |       y | j                  ||z          y )Ni!i!Qi @  )r|   structpackr   )rE   rj   rn   
pre_headerheaders        r   ri   zConnection._send_bytes  s    Hz>T2.J[[q)FJJz"JJvJJsO [[q)F5y 

6"

3 

6C<(r   Nc                    | j                  d      }t        j                  d|j                               \  }|dk(  r7| j                  d      }t        j                  d|j                               \  }|||kD  ry | j                  |      S )N   r   r      r   )r   r   unpackrx   )rE   r   rj   rl   s       r   rw   zConnection._recv_bytes  sq    jjmdCLLN32:**Q-CMM$7ED4'>zz$r   c                 2    t        | g|      }t        |      S r   )r   r   )rE   r   rs      r   r   zConnection._poll  s    $!Awr   r   )r   r   r   r   r   _multiprocessingclosesocketrL   ru   _writer   _readr(   rX   r   r   r   r   ri   rw   r   r   r   r   r   r   l  so    
  0 < < 	!!&& %% " 	!%   %  ). r   r   c                   P    e Zd ZdZd
dZd Zd Zed        Zed        Z	d Z
d	 Zy)r   z
    Returns a listener object.

    This is a wrapper for a bound socket which is 'listening' for
    connections, or for a Windows named pipe.
    Nc                    |xs |xr t        |      xs t        }|xs t        |      }t        |       |dk(  rt	        ||      | _        nt        |||      | _        |t        |t              st        d      || _
        y )Nr   authkey should be a byte string)r=   default_familyr/   r5   PipeListener	_listenerSocketListener
isinstancebytes	TypeError_authkey)rE   r<   r.   backlogauthkeys        r   rI   zListener.__init__  s     #G=W(= #" 	6.v6 Y)'7;DN+GVWEDNz'5'A=>>r   c                     | j                   t        d      | j                   j                         }| j                  ,t	        || j                         t        || j                         |S )zz
        Accept a connection on the bound socket or named pipe of `self`.

        Returns a `Connection` object.
        zlistener is closed)r   rQ   acceptr   deliver_challengeanswer_challenge)rE   cs     r   r   zListener.accept  sW     >>!.//NN!!#==$a/Q.r   c                 P    | j                   }|d| _         |j                          yy)zA
        Close the bound socket or named pipe of `self`.
        N)r   rX   )rE   listeners     r   rX   zListener.close  s)     >>!DNNN  r   c                 .    | j                   j                  S r   )r   _addressrM   s    r   r<   zListener.address  s    ~~&&&r   c                 .    | j                   j                  S r   )r   _last_acceptedrM   s    r   last_acceptedzListener.last_accepted  s    ~~,,,r   c                     | S r   r   rM   s    r   r   zListener.__enter__  r   r   c                 $    | j                          y r   r   r   s       r   r   zListener.__exit__  r   r   )NNr   N)r   r   r   r   rI   r   rX   r   r<   r  r   r   r   r   r   r   r     sH       ' ' - -r   r   c                     |xs t        |       }t        |       |dk(  rt        |       }nt        |       }|t	        |t
              st        d      |t        ||       t        ||       |S )z=
    Returns a connection to the address of a `Listener`
    r   r   )	r=   r5   
PipeClientSocketClientr   r   r   r   r   )r<   r.   r   r   s       r   r   r     sq     ,|G,FVw!:gu#=9::G$!W%Hr   c                 N   | rot        j                         \  }}|j                  d       |j                  d       t        |j	                               }t        |j	                               }||fS t        j                         \  }}t        |d      }t        |d      }||fS )L
        Returns pair of connection objects at either end of a pipe
        TFrH   rG   )r4   
socketpairsetblockingr   detachr(   pipe)duplexs1s2c1c2fd1fd2s          r   r   r     s     &&(FBNN4 NN4 BIIK(BBIIK(B 2v	 wwyHCC%0BC%0B2vr   c           
         t        d      }| r>t        j                  }t        j                  t        j                  z  }t
        t
        }}n(t        j                  }t        j                  }dt
        }}t        j                  ||t        j                  z  t        j                  z  t        j                  t        j                  z  t        j                  z  d||t        j                  t        j                        }t        j                  ||dt        j                  t        j                   t        j                  t        j                        }t        j"                  |t        j                  dd       t        j$                  |d      }|j'                  d      \  }	}
|
dk(  sJ t)        ||       }t)        ||       }||fS )	r  r   r   r   NTr   r  r  )r/   r   PIPE_ACCESS_DUPLEXGENERIC_READGENERIC_WRITEBUFSIZEPIPE_ACCESS_INBOUNDCreateNamedPipeFILE_FLAG_OVERLAPPEDFILE_FLAG_FIRST_PIPE_INSTANCEPIPE_TYPE_MESSAGEPIPE_READMODE_MESSAGE	PIPE_WAITNMPWAIT_WAIT_FOREVERNULL
CreateFileOPEN_EXISTINGSetNamedPipeHandleStateConnectNamedPiper   r   )r  r<   openmodeaccessobsizeibsizeh1h2r   _r   r  r  s                r   r   r   (  si    $I.11H))G,A,AAF$gFF22H**FFF$$X < <<112%%(E(EEvvw;;LL VQg.C.C((',, 	''--tT	 --bTB
//53axxB0B02vr   c                   $    e Zd ZdZddZd Zd Zy)r   zO
    Representation of a socket which is bound to an address and listening
    c                    t        j                   t        t         |            | _        	 t        j                  dk(  r9| j                  j                  t         j                  t         j                  d       | j                  j                  d       | j                  j                  |       | j                  j                  |       | j                  j                         | _        || _        d | _        |dk(  rCt#        j$                  |      s.t#        j&                  | t        j(                  |fd      | _        y d | _        y # t        $ r | j                  j                           w xY w)Nposixr   Tr   r   argsexitpriority)r4   getattr_socketr(   name
setsockopt
SOL_SOCKETSO_REUSEADDRr  bindlistengetsocknamer  rQ   rX   _familyr  r   r;   Finalizeunlink_unlink)rE   r<   r.   r   s       r   rI   zSocketListener.__init__X  s   }}WVV%<=	ww'!''(9(9(.(;(;Q@LL$$T*LLg&LL( LL446DM "Yt'H'H'Q==biiwjqDL  DL  	LL 	s   B<E %E)c                     | j                   j                         \  }| _        |j                  d       t	        |j                               S )NT)r9  r   r  r  r   r  rE   ss     r   r   zSocketListener.acceptq  s9    !%!4!4!64	d!((*%%r   c                     	 | j                   j                          | j                  }|d | _         |        y y # | j                  }|d | _         |        w w xY wr   )r9  rX   rD  )rE   rC  s     r   rX   zSocketListener.closev  s[    	LL \\F!# " \\F!# "s	   : AN)r   )r   r   r   r   rI   r   rX   r   r   r   r   r   T  s     2&
r   r   c                     t        |       }t        j                  t        t        |            5 }|j                  d       |j	                  |        t        |j                               cddd       S # 1 sw Y   yxY w)zO
    Return a connection object connected to the socket given by `address`
    TN)r=   r4   r8  r  connectr   r  )r<   r.   rG  s      r   r	  r	    s^     '"F	/	1 &Q	d			'!((*%& & &s   ;A44A=c                   6    e Zd ZdZddZddZd Zed        Zy)	r   z0
        Representation of a named pipe
        Nc                    || _         | j                  d      g| _        d | _        t	        j
                  d| j                          t	        j                  | t        j                  | j                  | j                   fd      | _	        y )NT)firstz listener created with address=%rr   r5  )
r  _new_handle_handle_queuer  r   	sub_debugrB  r   _finalize_pipe_listenerrX   )rE   r<   r   s      r   rI   zPipeListener.__init__  sl    #DM"&"2"2"2">!?D"&DNN=t}}Ml::(($--8qDJr   c           
      ~   t         j                  t         j                  z  }|r|t         j                  z  }t        j                  | j
                  |t         j                  t         j                  z  t         j                  z  t         j                  t        t        t         j                  t         j                        S r   )r   r  r   r!  r  r  r"  r#  r$  PIPE_UNLIMITED_INSTANCESr  r%  r&  )rE   rM  flagss      r   rN  zPipeListener._new_handle  s    ..1M1MME>>>**u))G,I,II!!"00'7,,gll r   c                 h   | j                   j                  | j                                | j                   j                  d      }	 t	        j
                  |d      }	 t	        j                  |j                  gdt              }	 |j                  d      \  }}|dk(  sJ t        |      S #  |j                          t	        j                  |        xY w# |j                  d      \  }}|dk(  sJ w xY w# t        $ r2}|j                  t        j                  k7  r Y d }~t        |      S d }~ww xY w)Nr   Tr   F)rO  appendrN  popr   r*  r   r   r   r   r   r   rQ   r   ERROR_NO_DATAr   )rE   rF   r   resr1  r   r   s          r   r   zPipeListener.accept  s   %%d&6&6&89''++A.F$--fF	$!88
E85C  33D9FAs!8O8!&))IIK''/33D9FAs!8O8  ::!6!66 7 "&))!s0   C6 &B+ +(CC C36	D1?D,,D1c                 h    t        j                  d|       | D ]  }t        j                  |        y )Nz closing listener with address=%r)r   rP  r   r   )queuer<   rF   s      r   rQ  z$PipeListener._finalize_pipe_listener  s.    NN=wG ,##F+,r   r   )F)	r   r   r   r   rI   rN  r   staticmethodrQ  r   r   r   r   r     s*    			
		*. 
	, 
	,r   r   c           	      .   t               }	 	 t        j                  | d       t        j                  | t        j                  t        j
                  z  dt        j                  t        j                  t        j                  t        j                        }	 t        j                  |t        j                  dd       t!        |      S # t        $ rB}|j                  t        j                  t        j                  fvst        |      r Y d}~nd}~ww xY w)zU
        Return a connection object connected to the pipe given by `address`
          r   N)r   r   WaitNamedPiper'  r  r  r&  r(  r   rQ   r   ERROR_SEM_TIMEOUTERROR_PIPE_BUSYr   r)  r#  r   )r<   r   hr   s       r   r  r    s     O%%gt4&&W11G4I4IIw||W%:%:00',,  	''w,,dD	 a    ::g&?&?&-&=&=&? ?BPQRBS CT s   BC 	D8DD(   s   #CHALLENGE#s	   #WELCOME#s	   #FAILURE#>      md5   sha256   sha384   sha3_256   sha3_384c              #   2   K   | ]  }t        |        y wr   )r|   ).0r1  s     r   	<genexpr>rk  F  s     7c!f7s         messagereturnc                     t        |       t        v rd| fS | j                  d      rG| j                  ddt        dz         x}dkD  r(| d| }|t
        v r| |dz   d }|j                  d      |fS t        d	|       )
a  Returns a digest name and the payload for a response hash.

    If a legacy protocol is detected based on the message length
    or contents the digest name returned will be empty to indicate
    legacy mode where MD5 and no digest prefix should be sent.
    r$      {   }r      r   NasciizRunsupported message length, missing digest prefix, or unsupported digest: message=)r|   _LEGACY_LENGTHSr:   find_MAX_DIGEST_LEN_ALLOWED_DIGESTSdecoder	   )rn  curlydigestpayloads       r   _get_digest_name_and_payloadr}  Q  s     7|&
 7{4 ,,tQ(9:	:a?5!%%eAgh'G==)722
/&-Z12 2r   c                     ddl }t        |      d   }|s"	 |j                  | |d      j                         S |j                  | ||      j                         }d|j                  d      |fz  S # t        $ r d}Y Dw xY w)a  Create a MAC based on authkey and message

    The MAC algorithm defaults to HMAC-MD5, unless MD5 is not available or
    the message has a '{digest_name}' prefix. For legacy HMAC-MD5, the response
    is the raw MAC, otherwise the response is prefixed with '{digest_name}',
    e.g. b'{sha256}abcdefg...'

    Note: The MAC protects the entire message including the digest_name prefix.
    r   Nmd5sha256   {%s}%srt  )hmacr}  newr{  r,   encode)r   rn  r  digest_nameresponses        r   _create_responser  k  s     .w7:K	#88GWe4;;== xx+6==?H**73X>>>  	# #K		#s    A. .A<;A<c           
      d   ddl }t        |      \  }}|xs d}	 |j                  | ||      j                         }t        |      t        |      k7  r&t        d|dt        |       dt        |             |j                  ||      st        d	      y# t        $ r t        d|d      w xY w)
a`  Verify MAC challenge

    If our message did not include a digest_name prefix, the client is allowed
    to select a stronger digest_name from _ALLOWED_DIGESTS.

    In case our message is prefixed, a client cannot downgrade to a weaker
    algorithm, because the MAC is calculated over the entire message
    including the '{digest_name}' prefix.
    r   Nr  zresponse_digest=z unsupportedz	expected z of length z got zdigest received was wrong)r  r}  r  r{  r,   r	   r|   compare_digest)r   rn  r  r  response_digestresponse_macexpecteds          r   _verify_challenger    s     $@$J!O\%.OE88GWo>EEG 8}L))!O.k#h- I<()+, 	, x6!"=>> 7  E!%5_$6l"CDDEs   !B B/r   c                    t        |t              s#t        dj                  t	        |                  t
        t        kD  sJ d       t        j                  t
              }d|j                  d      |fz  }| j                  t        |z          | j                  d      }	 t        |||       | j                  t               y # t        $ r | j                  t                 w xY w)N Authkey must be bytes, not {0!s}zprotocol constraintr  rt     )r   r   r,   formatr7   MESSAGE_LENGTH_MD5ONLY_MESSAGE_LENGTHr(   urandomr  ro   
_CHALLENGErz   r  _WELCOMEr	   _FAILURE)
connectionr   r  rn  r  s        r   r   r     s    gu%.55d7mDF 	F33J5JJ3jj(G;--g6@@G *w./$$S)H('7H5
 	h'	  h's   !C  C$c                    t        |t              s#t        dj                  t	        |                  | j                  d      }|j                  t              st        d|      |t        t              d  }t        |      t        k  rt        d      t        ||      }| j                  |       | j                  d      }|t        k7  rt        d      y )Nr  r  z,Protocol error, expected challenge: message=z)challenge too short: {len(message)} byteszdigest sent was rejected)r   r   r,   r  r7   rz   r:   r  r	   r|   r  r  ro   r  )r  r   rn  r{  r  s        r   r   r     s    gu%.55d7mDF 	F##C(Gj)!?wjAC 	Cc*o&'G
7|--!"MNNgw/F&!$$S)H8!"<== r   c                       e Zd Zd Zd Zd Zy)ConnectionWrapperc                 n    || _         || _        || _        dD ]  }t        ||      }t	        | ||        y )N)ra   rX   r   rz   ro   )_conn_dumps_loadsr8  setattr)rE   connrr   r   attrrt   s         r   rI   zConnectionWrapper.__init__  s>    
K 	%D$%CD$$	%r   c                 \    | j                  |      }| j                  j                  |       y r   )r  r  ro   )rE   rt   rG  s      r   ru   zConnectionWrapper.send  s"    KK

a r   c                 X    | j                   j                         }| j                  |      S r   )r  rz   r  rF  s     r   r   zConnectionWrapper.recv  s"    JJ!!#{{1~r   N)r   r   r   rI   ru   r   r   r   r   r  r    s    %!r   r  c                 T    t         j                  | fd d d d      j                  d      S )Nr   utf-8)	xmlrpclibrr   r  )rt   s    r   
_xml_dumpsr    s&    ??C64tQ7>>wGGr   c                 X    t         j                  | j                  d            \  \  }}|S )Nr  )r  r   ry  )rG  rt   methods      r   
_xml_loadsr    s$    __QXXg%67NFSFJr   c                       e Zd Zd Zy)XmlListenerc                 b    dd l ma t        j	                  |       }t        |t        t              S r   )xmlrpc.clientclientr  r   r   r  r  r  rs   s     r   r   zXmlListener.accept  s"    )ood# j*==r   N)r   r   r   r   r   r   r   r  r    s    >r   r  c                  J    dd l ma t        t	        | i |t
        t              S r   )r  r  r  r  r   r  r  )r6  kwdss     r   	XmlClientr    s    %VT2T2J
KKr   c                    t        |       }g }|rt        j                  |d|      }|t        k(  r	 |S t        |cxk  rt        t        |      z   k  rn n
|t        z  }nAt        |cxk  rt        t        |      z   k  rn t        d      |t        z  }nt        d      |j                  ||          ||dz   d  }d}|r|S )NFzShould not get herer   r   )	listr   r   r   r   r|   r   r   rV  )handlesr   LreadyrY  s        r   _exhaustive_waitr    s     M00E7CCl"  #>A(>>}$!SD+;c!f+DD ##899 ''"#899LL3 #a%&	AG  r   c                 V   |t         }n|dk  rd}nt        |dz  dz         }t        |       } i g }t               }t               }	 | D ]  }	 t	        |d      }	 t        j                   |       dd      \  }}|t
        j                  k(  r!|j                  |       ||j                  <   d|rDt        j                         dd dk\  r*	 |j                  d	      \  }
}|st!        |d
      rd|_        |j%                  |       d} t+        j-                         |      }|D ]  }|j/                           |D ]e  }	 |j                  d      \  }
}|t
        j0                  k7  s,|j                     }|j%                  |       |dk(  sRt!        |d
      s_d|_        g 	 |j3                  fd|D               | D cg c]	  }||v s| c}S # t        $ r"}	d|	j                  }}|t        vr Y d}	~	d}	~	ww xY w# t        $ r}	|	j                  }Y d}	~	>d}	~	ww xY w# t&        $ r ||j)                         <   Y w xY w# t        $ r }	|	j                  }|t        vr Y d}	~	d}	~	ww xY w# |D ]  }|j/                           |D ]  }	 |j                  d      \  }
}n+# t        $ r}	|	j                  }|t        vr Y d}	~	nd}	~	ww xY w|t
        j0                  k7  sX|j                     }|j%                  |       |dk(  s~t!        |d
      sd|_         w xY wc c}w )
        Wait till an object in object_list is ready/readable.

        Returns list of those objects in object_list which are ready/readable.
        Nr   r^  g      ?ra   Trs  )   rs  Fr   c              3   (   K   | ]	  }|     y wr   r   )rj  rb  waithandle_to_objs     r   rk  zwait.<locals>.<genexpr>S  s     Ia.q1Is   )r   intr  setr8  r   r   rQ   r   _ready_errorsr   rV  r   r1   getwindowsversionr   r3   r   addAttributeErrorrA   r  keysr   r   update)object_listr   ov_listready_objectsready_handlesora   r   r   r   r1  r  s              @r   r   r     sB    ?Gq[G'D.3./G;'6	8  $$$Q1F
"")"2"268Q"EC
 g666r*67)"((3
 #"7"7"9"1"="G1)+)?)?)F3 $'716J+K7; 4%))!,"#=$@ -->-C-C-EwOM  		  833D9FAs
 '999)"((3A!%%a(ax #1&:;37A08  	I=II&=a!}*<==_ # ""&

Cm3! 4"  $+ 1&'jj1- & 978%akkm49N  **C-/ 0  		  833D9FAs **C-/ 0 '999)"((3A!%%a(ax #1&:;37A08" >s   I4 H%G:AI4 HAI4  I	L&L&	G?G:4I4 :G??I4 	H"HI4 H""I4 %II4 II4 	I1I,,I14L#J'&L#'	K	0K
	L#
K	L#&%L#L#
L#PollSelectorc                    t               5 }| D ]"  }|j                  |t        j                         $ |t	        j
                         |z   }	 |j                  |      }|r(|D cg c]  \  }}|j                   c}}cddd       S |'t	        j
                         z
  }|dk  r|cddd       S ec c}}w # 1 sw Y   yxY w)r  Nr   )_WaitSelectorregister	selectors
EVENT_READr   r   selectfileobj)r  r   selectorrt   deadliner  keyeventss           r   r   r   b  s     _ 	)" =!!#y';';<= ">>+g5 0=BCMS&CKKC	) 	) *"*T^^-="="Q;#(	) 	)  D	) 	)s$   AB8%B2;B8B81B88Cc                     | j                         }t        j                  |t        j                  t        j                        5 }ddlm} |j                  |      }t        || j                  | j                  ffcd d d        S # 1 sw Y   y xY w)Nr   )resource_sharer)ra   r4   fromfdr   SOCK_STREAMr$   r  	DupSocketrebuild_connectionrG   rH   )r  rF   rG  r  dss        r   reduce_connectionr  ~  sl    ]]66>>63E3EF 	J!) **1-B%DMM4=='II	J 	J 	Js   6BBc                 X    | j                         }t        |j                         ||      S r   r  r   )r  rG   rH   socks       r   r  r    s"    yy{$++-8<<r   c                    | j                   rt        j                  nd| j                  rt        j                  ndz  }t        j                  | j                         |      }t        || j                   | j                  ffS r   )	rG   r   FILE_GENERIC_READrH   FILE_GENERIC_WRITEr   	DupHandlera   rebuild_pipe_connection)r  r,  dhs      r   reduce_pipe_connectionr    s^    047,,1157--AG  7&T]]DMM(JJJr   c                 <    | j                         }t        |||      S r   )r  r   )r  rG   rH   rF   s       r   r  r    s    fh99r   c                     t        j                  | j                               }t        || j                  | j
                  ffS r   )r   DupFdra   r  rG   rH   )r  dfs     r   r  r    s0    __T[[]+!Bt}}#EEEr   c                 <    | j                         }t        |||      S r   r  )r  rG   rH   fds       r   r  r    s    YY["h11r   )NN)T)r  r   )V__all__r   r   r(   r1   r4   r   r   r%   	itertoolsr   r$   r   r	   r
   contextr   ForkingPicklerrq   r   r   r   r   r   ImportErrorr2   r  CONNECTION_TIMEOUTcountr+   r   familiesr3   r   r   r/   r5   r=   r?   r   r   objectr   r   r   r   r	  r   r  r  r  r  r  	frozensetrx  maxrw  r  _MD5_DIGEST_LENru  r   r9   r}  r  r  r   r   r  r  r  r  r  r  r   ERROR_NETNAME_DELETEDr  r   r  r  r  SelectSelectorr  r  r  r  r  r   r   r   <module>r     s   3  	 	 
        1 **OO  	!;
69NH<<7NH - & 0
FF&T Tn ] ]@M Mh;v ;|* <<7&&X)V )X& <<76,v 6,p!> 
r @B 7&677  *O<2% 2S%L 24?8?2(5 (*>% >* H>( >L <<7( ..0M0MNML>` 
 y.)!..!00)6 <<7J= Iz#45K
: I~'=>F2 Iz#45s#  
||wGs   H? ?II