ϪfW<dZddlZddlZddlZddlZddlmZmZmZm Z m Z m Z ddl m Z mZddlmZmZmZddlmZmZmZmZmZmZmZmZddlmZddlmZm Z m!Z!m"Z"dd l#m$Z$dd l%m&Z&dd l'm(Z( dd lm)Z*e*Z)e,ed se+ddZ-GddZ.GddZ/e)re.Z.ne/Z.eej`Gdde.ejbZ1dZ2GddZ3Gdde3ejhZ4eej`Gdde.ejjZ6GddejnZ8eejrGdd e3ejhZ:eejvgeejxGd!d"e:Z=y#e+$rdZ)Y$wxYw)#z UNIX socket support for Twisted. End users shouldn't use this module directly - use the reactor APIs instead. Maintainer: Itamar Shtull-Trauring N)EAGAIN ECONNREFUSEDEINTREMSGSIZEENOBUFS EWOULDBLOCK)OptionalType) implementedBy implementerimplementer_only)addressbaseerror interfacesmainprotocoltcpudp)FileDescriptor)failurelockfilelogreflect) lazyByteSlice)_coerceToFilesystemEncoding)untilConcludes)sendmsgAF_UNIXz+UNIX sockets not supported on this platformcrtjd|}tjtj |fgS)zh Pack an integer into an ancillary data structure suitable for use with L{sendmsg.sendmsg}. i)structpacksocket SOL_SOCKETr SCM_RIGHTS)fdpackeds 7/usr/lib/python3/dist-packages/twisted/internet/unix.py_ancillaryDescriptorr*)s0 [[b !F    2 2F ; <<cTeZdZUdZdZeeeed<dZ dZ dZ dZ dZ d Zd Zy) _SendmsgMixina  Mixin for stream-oriented UNIX transports which uses sendmsg and recvmsg to offer additional functionality, such as copying file descriptors into other processes. @ivar _writeSomeDataBase: The class which provides the basic implementation of C{writeSomeData}. Ultimately this should be a subclass of L{twisted.internet.abstract.FileDescriptor}. Subclasses which mix in L{_SendmsgMixin} must define this. @ivar _sendmsgQueue: A C{list} of C{int} holding file descriptors which are currently buffered before being sent. @ivar _fileDescriptorBufferSize: An C{int} giving the maximum number of file descriptors to accept and queue for sending before pausing the registered producer, if there is one. N_writeSomeDataBase@cg|_yN) _sendmsgQueueselfs r)__init__z_SendmsgMixin.__init__Hs r+ct|j|jkDxs|jj |S)a Determine whether the user-space send buffer for this transport is full or not. This extends the base determination by adding consideration of how many file descriptors need to be sent using L{sendmsg.sendmsg}. When there are more than C{self._fileDescriptorBufferSize}, the buffer is considered full. @return: C{True} if it is full, C{False} otherwise. )lenr2_fileDescriptorBufferSizer._isSendBufferFullr3s r)r9z_SendmsgMixin._isSendBufferFullKsD&     * *+ .2.E.E.W.W /  r+cz|jj||j|jy)zY Queue the given file descriptor to be sent and start trying to send it. N)r2append_maybePauseProducer startWriting)r4filenos r)sendFileDescriptorz _SendmsgMixin.sendFileDescriptords0 !!&)   " r+ct|jt|kDrtjSd} |t|jkrc|j|} t t j |j |||dzt||dz }|t|jkrc|jd|=t||}|jj!||} ||zS#t$rZ}|jdttfvr|cYd}~|jd|=StjcYd}~|jd|=Sd}~wwxYw#|jd|=wxYw#t"$r|cYSwxYw)zg Send as much of C{data} as possible. Also send any pending file descriptors. rN)r7r2rFileDescriptorOverrunrrr$r*OSErrorargsrrrCONNECTION_LOSTrr. writeSomeData TypeError)r4dataindexr'se limitedDataresults r)rFz_SendmsgMixin.writeSomeDatals t!! "SY ...0 0  +#d0011''. " UUQY/,R0 QJE#d0011"""6E6*$D%0 ((66t[I 6> !4wwqzk7%;;$ ""6E6* $333""6E6*4""6E6* M s_'E!5C1E,E+1 E:EEE+E:E;EEEE(+ E98E9c 4 ttj|j|j\}}}|D]\}}}|tjk(r%|tjk(r|j|?tjd|j|j!|j#|j$|||j'|S#t $r5}|j dtk(rYd}~ytjcYd}~Sd}~wwxYw)a Calls {IProtocol.dataReceived} with all available data and L{IFileDescriptorReceiver.fileDescriptorReceived} once for each received file descriptor in ancillary data. This reads up to C{self.bufferSize} bytes of data from its socket, then dispatches the data to protocol callbacks to be handled. If the connection is not lost through an error in the underlying recvmsg(), this function will return the result of the dataReceived call. rNz%(protocolName)s (on %(hostAddress)r) received unsupported ancillary data (level=%(cmsgLevel)r, type=%(cmsgType)r) from %(peerAddress)r.)format hostAddress peerAddress protocolName cmsgLevelcmsgType)rrrecvmsgr$ bufferSizerCrDrrrEr%r&%_ancillaryLevelSOLSOCKETTypeSCMRIGHTSrmsggetHostgetPeer _getLogPrefixr _dataReceived)r4rH ancillaryflagsrJrRrScmsgDatas r)doReadz_SendmsgMixin.doReads ,%3doo& "D)U.7  )IxF---(g>P>P2P::8D0 !%  $ !%!3!3DMM!B'%   $!!$''1 ,wwqz[(+++  ,s#3C D"D=D DDct|dz}tjd|z|}tjj |j r#|D]}|j j|ytjd|j|j|j|j |D]}tj|y)a Processes ancillary data with level SOL_SOCKET and type SCM_RIGHTS, indicating that the ancillary data payload holds file descriptors. Calls L{IFileDescriptorReceiver.fileDescriptorReceived} once for each received file descriptor or logs a message if the protocol does not implement L{IFileDescriptorReceiver}. @param cmsgData: Ancillary data payload. @type cmsgData: L{bytes} r!z%(protocolName)s (on %(hostAddress)r) does not provide IFileDescriptorReceiver; closing file descriptor received (from %(peerAddress)r).)rNrOrPrQN)r7r"unpackrIFileDescriptorReceiver providedByrfileDescriptorReceivedrrWrXrYrZosclose)r4r^fdCountfdsr's r)rVz3_SendmsgMixin._ancillaryLevelSOLSOCKETTypeSCMRIGHTSsh-1$mmC'M84  - - 8 8 G 9 44R8 9 GGB!LLN LLN!// >    r+)__name__ __module__ __qualname____doc__r.r r r__annotations__r8r5r9r?rFr_rVr+r)r-r-2sD$:>n!56= "  2,\'(Rr+r-ceZdZdZy)_UnsupportedSendmsgMixinz Behaviorless placeholder used when C{twisted.python.sendmsg} is not available, preventing L{IUNIXTransport} from being supported. N)rjrkrlrmror+r)rqrqsr+rqcFeZdZejZdZedZdZ dZ y)Serverc |tj|tjj||||df|||yr1)r-r5rrs)r4sockrclientserver sessionnoreactors r)r5zServer.__init__s5t$  $64.&)W r+ctj|tjtj}t j |j }|j|}||jyd}||||jd||}dj|jjj|j|j |_dj|jjj|j|j |_|j#||S)a Create a new L{Server} based on an existing connected I{SOCK_STREAM} socket. Arguments are the same as to L{Server.__init__}, except where noted. @param fileDescriptor: An integer file descriptor associated with a connected socket. The socket must be in non-blocking mode. Any additional attributes desired, such as I{FD_CLOEXEC}, must also be set already. @return: A new instance of C{cls} wrapping the socket given by C{fileDescriptor}. Nrz<{} #{} on {}>z{},{},{})r$fromfdr SOCK_STREAMr UNIXAddress getsockname buildProtocolrg getpeernamerNr __class__rjrxrepstrlogstrmakeConnection) clsfileDescriptorfactoryryskt protocolAddrprotorxr4s r)_fromConnectedSocketzServer._fromConnectedSockets mmNFNNF:r+rsc|dddvS)a Determine whether the given unix socket path is in a filesystem namespace. While most PF_UNIX sockets are entries in the filesystem, Linux 2.2 and above support PF_UNIX sockets in an "abstract namespace" that does not correspond to any path. This function returns C{True} if the given socket path is stored in the filesystem and C{False} if the path is in this abstract namespace. NrA)ro)paths r)_inFilesystemNamespacer*s 8= ((r+ceZdZdZy) _UNIXPortc\tj|jjS)zV Returns a UNIXAddress. This indicates the server's address. rr3s r)rXz_UNIXPort.getHost8s" ""4;;#:#:#<==r+N)rjrkrlrXror+r)rr7s>r+rceZdZejZej ZeZ dZ d dZ e dZ defdZdZdZdZd Zy) PortNctjj||j|j|||||_||_d|_y)Nry)rrr5 _buildAddrnamemodewantPID_preexistingSocket)r4fileNamerbacklogrryrs r)r5z Port.__init__HsM  $//(+00'7G    "&r+ctj||j|j}||j ||}||_|S)a Create a new L{Port} based on an existing listening I{SOCK_STREAM} socket. Arguments are the same as to L{Port.__init__}, except where noted. @param fd: An integer file descriptor associated with a listening socket. The socket must be in non-blocking mode. Any additional attributes desired, such as I{FD_CLOEXEC}, must also be set already. @return: A new instance of C{cls} wrapping the socket given by C{fd}. r)r$r{ addressFamily socketTyper~r)rryr'rportr4s r)_fromListeningDescriptorzPort._fromListeningDescriptorRsD}}R!2!2CNNC4##%w@"& r+returnctj|jj}t |dr&dj |t d|jSd|dS)Nr$z <{} on {!r}>< (not listening)>)rqualrrhasattrrNrr)r4 factoryNames r)__repr__z Port.__repr__esZll4<<#9#9: 4 "!((+B :  {m#45 5r+c,tj|Sr1rr})r4rs r)rzPort._buildAddro""4((r+ctjjtj|j |j dtd|j|jrtj|jdz|_ |jjs!tjd|jd|jj s\ t#j$t'j"|jj(rt'j*|j|j j/ |j0|j0}d|_n+|j3}|j5|jt7|jr*t'j8|j|j:|j=|j>d|_ ||_!|jBjD|_"d|_#|jIy#t,$rY wxYw#tJ$r&}tjd|j|d}~wwxYw)z Create and bind my socket, and begin listening on it. This is called on unserialization, and must be called after creating a server to begin listening on the specified port. starting on r.lockNzCannot acquire lockTd)&r _reservedFDreserverrWrZrrrrrFilesystemLocklockFilelockrCannotListenErrorcleanstatS_ISSOCKrfst_moderemove BaseExceptiondoStartrcreateInternetSocketbindrchmodrlistenr connectedr$r> numberAccepts startReadingrCr4rles r)startListeningzPort.startListeningrs ! ""4<<0+B :   <<$33DII4HIDM==%%'--dDII?TUU}}**   ==);)C)CDIIdii0  &&2--*.'//1#&dii0DII. JJt|| $!DNDK++,,DK!$D     /) ?))$ 2> > ?s,+AI!A I II J !JJc\tjdtd|jzy)z0 Log message for closing socket z(UNIX Port %s Closed)rN)rrWrrr3s r)_logConnectionLostMsgzPort._logConnectionLostMsgs*  #+II  r+ct|jrtj|j|j|jj t jj||yr1) rrrfunlinkrunlockrrconnectionLostr4reasons r)rzPort.connectionLostsL !$)) , IIdii == $ MM " f-r+)2Nr)rjrkrlr$rrr|rrs transportrr5rrstrrrrrrror+r)rrAsaNNM##JIHPQ'$6#6)4 l  .r+rcleZdZdZej ZejZe jZ ddZ dZ dZy)ClientzA client for Unix sockets.Ncptj|tj|j}||_|x|_|_|r?tj|dzs'|jddtj|||j|j|jd|y)Nr)r-r5rr}r connector realAddressaddrrisLocked _finishInitr BadFileError doConnectr)r4filenamerrycheckPIDs r)r5zClient.__init__st$&&x055"'//49 H--h.AB   T4););H)Ew O )B)B)DdGTr+c@tj|jSr1)rr}rr3s r)rYzClient.getPeers""499--r+c,tjdSr1rr3s r)rXzClient.getHostrr+)Nr)rjrkrlrmr$rrr|rr BaseClientr.r5rYrXror+r)rrs3$NNM##JU.)r+rceZdZdZdZdZy) Connectorcdtjj||||||_||_yr1)r BaseConnectorr5rr)r4rrtimeoutryrs r)r5zConnector.__init__s* ##D'7GD   r+cZt|j||j|jSr1)rrryrr3s r)_makeTransportzConnector._makeTransportsdllD$,, FFr+c@tj|jSr1rr3s r)getDestinationzConnector.getDestinations""4<<00r+N)rjrkrlr5rrror+r)rrs! G1r+rcVeZdZdZej Zd dZdefdZ dZ dZ d dZ d Z y) DatagramPortz4 Datagram UNIX port, listening for packets. NcZtjj|||||||_y)z%Initialize with address to listen on.) maxPacketSizeryN)rrr5r)r4rrrrrys r)r5zDatagramPort.__init__s-  $]G   r+rctj|jj}t |drd|d|j dSd|dS)Nr$rz on >r)rrrrrr)r4rQs r)rzDatagramPort.__repr__sR|| MM # #  4 "|nD Q7 7|n$56 6r+cBtj|jjdt |j  |j }|j r|j|j |j r?t|j r*tj|j |jd|_||_|j j"|_y#t$r&}tjd|j |d}~wwxYw)NrrA)rrWrrreprrrrrCrrrrfrrrr$r>rs r) _bindSocketzDatagramPort._bindSockets 4==**+=dii8IJK ?++-Cyy# 99/ : HHTYY * kk((  ?))$ 2> > ?s7C// D8!DDc |jj||S#t$rb}|jd}|tk(r|j ||cYd}~S|t k(rtjd|tk(rnYd}~yd}~wwxYw)zWrite a datagram.rNmessage too long) r$sendtorCrDrwriterrMessageLengthErrorr)r4datagramrrJnos r)rzDatagramPort.write s ;;%%h8 8 BU{zz(G44x../ABBv s  B )BB )BB ctjdt|jztj j ||t|dr|jjd|_ |jj|` |` t|dr|jjd|`yy)zCleans up my socket.z(Port %s Closed)rrdN)rrWrrrBasePortrrrdoStoprr$rgr>rcallbackrs r)rzDatagramPort.connectionLosts "T$))_45 $$T62 4 $ MM "  K K 4  FFOOD ! r+cftj|jjdz|_y)Nz (UDP))rrrrrr3s r) setLogStrzDatagramPort.setLogStr-s!ll4==#:#:;hF r+) rNr1)rjrkrlrmr$rrr5rrrrrr ror+r)rrs8NNM7#7 )$ Gr+rc>eZdZdZ d dZdZdZdZdZdZ y) ConnectedDatagramPortz+ A connected datagram UNIX socket. Nc~t|tjsJtj ||||||||_yr1) isinstancerConnectedDatagramProtocolrr5 remoteaddr)r4rrrr bindAddressrys r)r5zConnectedDatagramPort.__init__9s9%!C!CDDDdK tWUr+c |j|jj|j|j y#t $r&|j tjYywxYwr1) rr$connectr_connectToProtocolrconnectionFailedrFailurer3s r)rz$ConnectedDatagramPort.startListeningFsY 5     KK   0  # # % 5  ! !'//"3 4 5sAA,A76A7c^|j|jj||`y)z Called when a connection fails. Stop listening on the socket. @type reason: L{Failure} @param reason: Why the connection failed. N) stopListeningrrrs r)rz&ConnectedDatagramPort.connectionFailedNs'  &&v. Mr+cd}||jkrc |jj|j\}}|t |z }|j j |||jkrbyy#t$rV}|jd}|tttfvrYd}~y|tk(r|j jnYd}~ld}~wt$rtj YwxYw)z= Called when my socket is ready for reading. rN) maxThroughputr$recvfromrr7rdatagramReceivedrCrDrrrrconnectionRefusedrrdeferr)r4readrHrrJrs r)r_zConnectedDatagramPort.doReadYsT''' ![[11$2D2DE dD ! ..t4 T'''  WWQZ&%55%MM3356!   s$AA66 C3?"C&%C C32C3cf |jj|S#t$r}|jd}|tk(r|j |cYd}~S|t k(rtjd|tk(r|jjn|tk(rnYd}~yYd}~yd}~wwxYw)z# Write a datagram. rNr) r$sendrCrDrrrrrrrrr)r4rHrJrs r)rzConnectedDatagramPort.writens ;;##D) ) BU{zz$''x../ABB|# //1v 2 s! B0(B+B0A B++B0c@tj|jSr1)rr}rr3s r)rYzConnectedDatagramPort.getPeers""4??33r+)r rNN) rjrkrlrmr5rrr_rrYror+r)r r 1s3  5 *,4r+r )>rmrfr$rr"errnorrrrrrtypingr r zope.interfacer r r twisted.internetrrrrrrrrtwisted.internet.abstractrtwisted.pythonrrrrtwisted.python.compatrtwisted.python.filepathrtwisted.python.utilrr_sendmsg ImportErrorrr*r-rqIUNIXTransportrsrrrrrrrIUNIXDatagramTransportrIUNIXDatagramConnectedTransportrr ror+r)r2s  MM!GGWWW4::/?.2Gvy! C DD=ood !M,M Z & &'5:]CJJ5:(5:p )>>z.9chhz.z Z & &')]CNN)()0 1"" 1 Z . ./IG9chhIG0IGX..2? 2NQ4LQ4Q4iGs.FFF