3 /fVy @s ddddgZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl m Z ddl m Z m Z dd lmZejZy$ddlZdd lmZmZmZmZWn$ek rejd kr‚dZYnXd Zd ZdZejZdZdgZe edrdZedg7Zejd krdZedg7ZefddZ!ddZ"ddZ#ddZ$ddZ%GdddZ&ernGddde&Z'Gd d!d!e&Z(Gd"dde)Z*dPd#dZ+ejd krdQd%dZ,n dRd&dZ,Gd'd(d(e)Z-d)d*Z.ejd krGd+d,d,e)Z/d-d.Z0d/Z1d0Z2d1Z3d2Z4d3d4Z5d5d6Z6Gd7d8d8e)Z7d9d:Z8d;d<Z9Gd=d>d>e*Z:d?d@Z;ejd krzdAdBZhZ?dSdCdZ@n,ddlAZAe eAdDreAjBZCneAjDZCdTdEdZ@ejd krdFdGZEdHdIZFejGe(eEdJdKZHdLdMZIejGe'eHndNdGZEdOdIZFejGe(eEdS)UClientListenerPipewaitN)util)AuthenticationErrorBufferTooShort) reduction) WAIT_OBJECT_0WAIT_ABANDONED_0 WAIT_TIMEOUTINFINITEwin32i g4@Zsha256AF_INETAF_UNIXAF_PIPEcCs tj|S)N)time monotonic)timeoutr"/usr/lib64/python3.6/connection.py _init_timeout?srcCs tj|kS)N)rr)trrr_check_timeoutBsrcCsX|dkr d S|dkr&tjdtjdS|dkrLtjdtjttfd dStd d S) z? Return an arbitrary free address for the given family r localhostrrz listener-)prefixdirrz\\.\pipe\pyc-%d-%d-zunrecognized familyN)rr) tempfileZmktemprZ get_temp_dirosgetpidnext _mmap_counter ValueError)familyrrrarbitrary_addressIsr&cCsJtjdkr|dkrtd|tjdkrF|dkrFtt|sFtd|dS)zD Checks if the family is valid for the current environment. rrzFamily %s is not recognized.rN)sysplatformr$hasattrsocket)r%rrr_validate_familyWs   r+cCsJt|tkrdSt|tkr*|jdr*dSt|tkr:dStd|dS)z] Return the types of the address This can be 'AF_INET', 'AF_UNIX', or 'AF_PIPE' rz\\rrzaddress type of %r unrecognizedN)typetuplestr startswithr$)addressrrr address_typecs  r1c@seZdZdZd+ddZddZddZd d Zd d Zd dZ e ddZ e ddZ e ddZ ddZddZd,ddZddZd-ddZd.d d!Zd"d#Zd/d%d&Zd'd(Zd)d*ZdS)0_ConnectionBaseNTcCsB|j}|dkrtd| r,| r,td||_||_||_dS)Nrzinvalid handlez6at least one of `readable` and `writable` must be True) __index__r$_handle _readable _writable)selfhandlereadablewritablerrr__init__ys z_ConnectionBase.__init__cCs|jdk r|jdS)N)r4_close)r7rrr__del__s z_ConnectionBase.__del__cCs|jdkrtddS)Nzhandle is closed)r4OSError)r7rrr _check_closeds z_ConnectionBase._check_closedcCs|jstddS)Nzconnection is write-only)r5r>)r7rrr_check_readablesz_ConnectionBase._check_readablecCs|jstddS)Nzconnection is read-only)r6r>)r7rrr_check_writablesz_ConnectionBase._check_writablecCs"|jrd|_n|jtddS)NFzbad message length)r6r5closer>)r7rrr_bad_message_lengthsz#_ConnectionBase._bad_message_lengthcCs |jdkS)z True if the connection is closedN)r4)r7rrrclosedsz_ConnectionBase.closedcCs|jS)z"True if the connection is readable)r5)r7rrrr9sz_ConnectionBase.readablecCs|jS)z"True if the connection is writable)r6)r7rrrr:sz_ConnectionBase.writablecCs|j|jS)z+File descriptor or handle of the connection)r?r4)r7rrrfilenosz_ConnectionBase.filenoc Cs$|jdk r z |jWdd|_XdS)zClose the connectionN)r4r<)r7rrrrBs  z_ConnectionBase.closercCs|j|jt|}|jdkr.tt|}t|}|dkrFtd||krVtd|dkrh||}n&|dkrztdn|||krtd|j||||dS)z,Send the bytes data from a bytes-like objectrrzoffset is negativezbuffer length < offsetNzsize is negativezbuffer length < offset + size)r?rA memoryviewitemsizebyteslenr$ _send_bytes)r7bufoffsetsizemnrrr send_bytess"     z_ConnectionBase.send_bytescCs$|j|j|jtj|dS)zSend a (picklable) objectN)r?rArJ_ForkingPicklerdumps)r7objrrrsendsz_ConnectionBase.sendcCsJ|j|j|dk r(|dkr(td|j|}|dkrB|j|jS)z7 Receive bytes data as a bytes object. Nrznegative maxlength)r?r@r$ _recv_bytesrCgetvalue)r7Z maxlengthrKrrr recv_bytess z_ConnectionBase.recv_bytesc Cs|j|jt|}|j}|t|}|dkr>tdn||krNtd|j}|j}|||krvt|j |j d|j |||||||SQRXdS)zq Receive bytes data into a writeable bytes-like object. Return the number of bytes read. rznegative offsetzoffset too largeN) r?r@rFrGrIr$rUtellr rVseekreadinto)r7rKrLrNrGZbytesizeresultrMrrrrecv_bytes_intos"       z_ConnectionBase.recv_bytes_intocCs&|j|j|j}tj|jS)zReceive a (picklable) object)r?r@rUrQloads getbuffer)r7rKrrrrecvsz_ConnectionBase.recvcCs|j|j|j|S)z/Whether there is any input available to be read)r?r@_poll)r7rrrrpollsz_ConnectionBase.pollcCs|S)Nr)r7rrr __enter__sz_ConnectionBase.__enter__cCs |jdS)N)rB)r7exc_type exc_valueexc_tbrrr__exit__ sz_ConnectionBase.__exit__)TT)rN)N)r)r`)__name__ __module__ __qualname__r4r;r=r?r@rArCpropertyrDr9r:rErBrPrTrWr\r_rbrcrgrrrrr2vs(       r2c@sDeZdZdZdZejfddZddZddd Z d d Z d d Z dS)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. FcCs||jdS)N)r4)r7Z _CloseHandlerrrr<szPipeConnection._closecCshtj|j|dd\}}zwinerrorERROR_BROKEN_PIPEEOFError RuntimeError) r7maxsizeZbsizertrurvZnreadferrrrU*s6      zPipeConnection._recv_bytescCs.|jstj|jddkrdStt|g|S)NrT)rxrn PeekNamedPiper4boolr)r7rrrrraJszPipeConnection._pollcCs|j}tj}|j|tj|jd}|dk rJt|||krJ|jtj |j|dd\}}|j d\}}|j|j|S)NrT)rm) r^ryrzr}rnrr4rIrCr|rs)r7rtrrKrleftruZrbytesrrrr~Ps zPipeConnection._get_more_data)N) rhrirj__doc__rxrn CloseHandler<rJrUrar~rrrrrls rlc@s|eZdZdZer,ejfddZejZ ej Z ne j fddZe jZ e jZ e fddZe fddZd d Zdd d ZddZd S) Connectionzo Connection class based on an arbitrary file descriptor (Unix only), or a socket handle (Windows). cCs||jdS)N)r4)r7r<rrrr<gszConnection._closecCs||jdS)N)r4)r7r<rrrr<lscCs<t|}x.||j|}||8}|dkr(P||d}q WdS)Nr)rIr4)r7rKr} remainingrOrrr_sendqs zConnection._sendcCsftj}|j}|}xN|dkr`|||}t|}|dkrL||krDtntd|j|||8}qW|S)Nrzgot end of file during message)ryrzr4rIrr>r})r7rMreadrKr8rchunkrOrrr_recvzs    zConnection._recvcCsDt|}tjd|}|dkr2|j||j|n|j||dS)Nz!ii@)rIstructpackr)r7rKrOheaderrrrrJs    zConnection._send_bytesNcCs:|jd}tjd|j\}|dk r0||kr0dS|j|S)Nz!i)rrunpackrV)r7rrKrMrrrrUs  zConnection._recv_bytescCst|g|}t|S)N)rr)r7rrrrrras zConnection._poll)N)rhrirjrrn_multiprocessingZ closesocketr<rTZ_writer_Z_readr rBr}rrrrJrUrarrrrr`s  rc@sReZdZdZdddZddZdd Zed d Zed d Z d dZ ddZ dS)rz Returns a listener object. This is a wrapper for a bound socket which is 'listening' for connections, or for a Windows named pipe. NrcCsr|p|rt|pt}|pt|}t||dkr>t|||_nt||||_|dk rht|t rht d||_ dS)Nrzauthkey should be a byte string) r1default_familyr&r+ PipeListener _listenerSocketListener isinstancerH TypeError_authkey)r7r0r%backlogauthkeyrrrr;s zListener.__init__cCs>|jdkrtd|jj}|jr:t||jt||j|S)zz Accept a connection on the bound socket or named pipe of `self`. Returns a `Connection` object. Nzlistener is closed)rr>acceptrdeliver_challengeanswer_challenge)r7crrrrs    zListener.acceptcCs |j}|dk rd|_|jdS)zA Close the bound socket or named pipe of `self`. N)rrB)r7ZlistenerrrrrBszListener.closecCs|jjS)N)r_address)r7rrrszListener.cCs|jjS)N)r_last_accepted)r7rrrrscCs|S)Nr)r7rrrrcszListener.__enter__cCs |jdS)N)rB)r7rdrerfrrrrgszListener.__exit__)NNrN) rhrirjrr;rrBrkr0Z last_acceptedrcrgrrrrrs   cCsj|p t|}t||dkr&t|}nt|}|dk rJt|t rJtd|dk rft||t|||S)z= Returns a connection to the address of a `Listener` rNzauthkey should be a byte string) r1r+ PipeClient SocketClientrrHrrr)r0r%rrrrrrs    TcCsj|r>tj\}}|jd|jdt|j}t|j}n$tj\}}t|dd}t|dd}||fS)zL Returns pair of connection objects at either end of a pipe TF)r:)r9)r*Z socketpair setblockingrdetachr pipe)duplexs1s2c1c2Zfd1Zfd2rrrrs       c Cstd}|r*tj}tjtjB}tt}}ntj}tj}dt}}tj||tjBtj Btj tj Btj Bd||tj tj}tj||dtjtjtjtj}tj|tj ddtj|dd}|jd\} } t||d} t||d} | | fS) zL Returns pair of connection objects at either end of a pipe rrrNT)rm)r:)r9)r&rnPIPE_ACCESS_DUPLEX GENERIC_READ GENERIC_WRITEBUFSIZEZPIPE_ACCESS_INBOUNDCreateNamedPipeFILE_FLAG_OVERLAPPEDFILE_FLAG_FIRST_PIPE_INSTANCEPIPE_TYPE_MESSAGEPIPE_READMODE_MESSAGE PIPE_WAITNMPWAIT_WAIT_FOREVERNULL CreateFile OPEN_EXISTINGSetNamedPipeHandleStateConnectNamedPipersrl) rr0ZopenmodeaccessZobsizeZibsizeZh1Zh2rm_rurrrrrr s2         c@s*eZdZdZd ddZddZddZd S) rzO Representation of a socket which is bound to an address and listening rcCstjtt||_yRtjdkr2|jjtjtjd|jjd|jj ||jj ||jj |_ Wn t k r|jjYnX||_d|_|dkrtj|tj|fdd|_nd|_dS)NposixrTrr)args exitpriority)r*getattr_socketr nameZ setsockoptZ SOL_SOCKETZ SO_REUSEADDRrZbindZlistenZ getsocknamerr>rBZ_familyrrFinalizeunlink_unlink)r7r0r%rrrrr;<s$       zSocketListener.__init__cCs&|jj\}|_|jdt|jS)NT)rrrrrr)r7srrrrTs zSocketListener.acceptc Cs0z|jjWd|j}|dk r*d|_|XdS)N)rrBr)r7rrrrrBYs zSocketListener.closeN)r)rhrirjrr;rrBrrrrr8s rc CsDt|}tjtt|"}|jd|j|t|jSQRXdS)zO Return a connection object connected to the socket given by `address` TN)r1r*rrZconnectrr)r0r%rrrrrcs   rc@s8eZdZdZd ddZd ddZdd Zed d ZdS)rz0 Representation of a named pipe NcCsL||_|jddg|_d|_tjd|jtj|tj|j|jfdd|_ dS)NT)firstz listener created with address=%rr)rr) r _new_handle _handle_queuerr sub_debugrr_finalize_pipe_listenerrB)r7r0rrrrr;wszPipeListener.__init__Fc CsHtjtjB}|r|tjO}tj|j|tjtjBtjBtj t t tj tj S)N) rnrrrrrrrrZPIPE_UNLIMITED_INSTANCESrrr)r7rflagsrrrrs   zPipeListener._new_handlecCs|jj|j|jjd}ytj|dd}Wn0tk r^}z|jtjkrNWYdd}~XnPXzrZ ERROR_NO_DATArprqrrrrrsrl)r7r8rtrresrrurrrrs"    zPipeListener.acceptcCs(tjd|x|D]}tj|qWdS)Nz closing listener with address=%r)rrrnr)Zqueuer0r8rrrrs  z$PipeListener._finalize_pipe_listener)N)F) rhrirjrr;rr staticmethodrrrrrrss  rcCst}x~y6tj|dtj|tjtjBdtjtjtjtj}Wn>t k r|}z"|j tj tj fksjt |rlWYdd}~XqXPqWtj|tjddt|S)zU Return a connection object connected to the pipe given by `address` irN)rrnZ WaitNamedPiperrrrrrr>rZERROR_SEM_TIMEOUTZERROR_PIPE_BUSYrrrrl)r0rhrrrrrs"   rs #CHALLENGE#s #WELCOME#s #FAILURE#cCsfddl}tjt}|jt||j||tj}|j d}||krP|jt n|jt t ddS)Nrzdigest received was wrong) hmacr urandomMESSAGE_LENGTHrP CHALLENGEnewHMAC_DIGEST_NAMEdigestrWWELCOMEFAILUREr) connectionrrmessagerresponserrrrs    rcCs\ddl}|jd}|ttd}|j||tj}|j||jd}|tkrXt ddS)Nrrzdigest sent was rejected) rrWrIrrrrrPrr)rrrrrrrrrrs   rc@s$eZdZddZddZddZdS)ConnectionWrappercCs:||_||_||_x"dD]}t||}t|||qWdS)NrErBrbrWrP)rErBrbrWrP)_conn_dumps_loadsrsetattr)r7connrRr]attrrSrrrr;s   zConnectionWrapper.__init__cCs|j|}|jj|dS)N)rrrP)r7rSrrrrrTs zConnectionWrapper.sendcCs|jj}|j|S)N)rrWr)r7rrrrr_s zConnectionWrapper.recvN)rhrirjr;rTr_rrrrrsrcCstj|fddddjdS)Nrzutf-8) xmlrpclibrRencode)rSrrr _xml_dumpssrcCstj|jd\\}}|S)Nzutf-8)rr]decode)rrSmethodrrr _xml_loadssrc@seZdZddZdS) XmlListenercCs ddljatj|}t|ttS)Nr) xmlrpc.clientclientrrrrrr)r7rSrrrrs  zXmlListener.acceptN)rhrirjrrrrrrsrcOsddljatt||ttS)Nr)rrrrrrr)rkwdsrrr XmlClient s rcCst|}g}x|rtj|d|}|tkr,Pn\t|koFtt|knrV|t8}n2t|koptt|knr|t8}ntd|j||||dd}d}qW|S)NFzShould not get hererr) listrnrpr r rIr rr)ZhandlesrLreadyrrrr_exhaustive_waits    rc .sn|dkrt}n|dkrd}nt|dd}t|}ig}tt}zHx2|D](}yt|d}Wn tk r||j<YqTXytj|dd\}}Wn8t k r}zd|j }}|t krʂWYdd}~XnX|tj kr|j |||j<qT|rptjdddkrpy|jd \} }Wn*t k rT}z |j }WYdd}~XnX| rpt|d rpd|_j|d}qTWtj|}Wdx|D]}|jqWx|D]}y|jd\} }Wn6t k r}z|j }|t krWYdd}~XnX|tjkr|j}j||dkrt|d rd|_qWXjfd d |Dfd d|DS)z Wait till an object in object_list is ready/readable. Returns list of those objects in object_list which are ready/readable. Nrig?rETFrxc3s|]}|VqdS)Nr).0r)waithandle_to_objrr vszwait..csg|]}|kr|qSrr)ro) ready_objectsrr wszwait..)rr)rintrsetrAttributeErrorr3rnr|r>r _ready_errorsrorrqr'Zgetwindowsversionrsr)rxaddrkeysrrZERROR_OPERATION_ABORTEDupdate) object_listrZov_listZ ready_handlesrrErtrurrr)rrrr+sh              PollSelectorc Cstx}x|D]}|j|tjqW|dk r8tj|}x@|j|}|rVdd|DS|dk r:|tj}|dkr:|Sq:WWdQRXdS)z Wait till an object in object_list is ready/readable. Returns list of those objects in object_list which are ready/readable. NcSsg|]\}}|jqSr)Zfileobj)rkeyZeventsrrrrszwait..r) _WaitSelectorregister selectorsZ EVENT_READrrZselect)r rZselectorrSZdeadlinerrrrrs    c CsN|j}tj|tjtj*}ddlm}|j|}t||j |j ffSQRXdS)Nr)resource_sharer) rEr*ZfromfdrZ SOCK_STREAMrrZ DupSocketrebuild_connectionr9r:)rr8rrdsrrrreduce_connections   rcCs|j}t|j||S)N)rr)rr9r:ZsockrrrrsrcCsB|jr tjnd|jrtjndB}tj|j|}t||j|jffS)Nr) r9rnZFILE_GENERIC_READr:ZFILE_GENERIC_WRITEr Z DupHandlerErebuild_pipe_connection)rrdhrrrreduce_pipe_connectionsrcCs|j}t|||S)N)rrl)rr9r:r8rrrrsrcCs tj|j}t||j|jffS)N)r ZDupFdrErr9r:)rdfrrrrscCs|j}t|||S)N)rr)rr9r:fdrrrrs)NN)T)T)N)N)J__all__ryr r'r*rrr itertoolsrrrrr contextr ZForkingPicklerrQrnr r r r ImportErrorr(rZCONNECTION_TIMEOUTrcountr#rZfamiliesr)rrr&r+r1r2rlrobjectrrrrrrrrrrrrrrrrrrrrZERROR_NETNAME_DELETEDrrrr rZSelectSelectorrrrrrrrrr s           PJ8    ,+ 8    P