
    Vh&                         d Z ddlmZ ddlmZ ddlmZ ddlmZ  eej                         G d dej                               Z
y)	z
The parent class for all the SSH Channels.  Currently implemented channels
are session, direct-tcp, and forwarded-tcp.

Maintainer: Paul Swartz
    )implementer)
interfaces)Logger)logc                       e Zd ZU dZ e       ZdZeed<   	 	 	 	 	 	 	 ddZ	de
fdZdefdZd Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)
SSHChannela  
    A class that represents a multiplexed channel over an SSH connection.
    The channel has a local window which is the maximum amount of data it will
    receive, and a remote which is the maximum amount of data the remote side
    will accept.  There is also a maximum packet size for any individual data
    packet going each way.

    @ivar name: the name of the channel.
    @type name: L{bytes}
    @ivar localWindowSize: the maximum size of the local window in bytes.
    @type localWindowSize: L{int}
    @ivar localWindowLeft: how many bytes are left in the local window.
    @type localWindowLeft: L{int}
    @ivar localMaxPacket: the maximum size of packet we will accept in bytes.
    @type localMaxPacket: L{int}
    @ivar remoteWindowLeft: how many bytes are left in the remote window.
    @type remoteWindowLeft: L{int}
    @ivar remoteMaxPacket: the maximum size of a packet the remote side will
        accept in bytes.
    @type remoteMaxPacket: L{int}
    @ivar conn: the connection this channel is multiplexed through.
    @type conn: L{SSHConnection}
    @ivar data: any data to send to the other side when the channel is
        requested.
    @type data: L{bytes}
    @ivar avatar: an avatar for the logged-in user (if a server channel)
    @ivar localClosed: True if we aren't accepting more data.
    @type localClosed: L{bool}
    @ivar remoteClosed: True if the other side isn't accepting more data.
    @type remoteClosed: L{bool}
    Nnamec                    |xs d| _         | j                   | _        |xs d| _        || _        || _        d| _        || _        || _        || _        d| _	        d| _
        g | _        d| _        d| _        d| _        d | _        y )Ni   i          r   )localWindowSizelocalWindowLeftlocalMaxPacketremoteWindowLeftremoteMaxPacket
areWritingconndataavatarspecificDatabufextBufclosinglocalClosedremoteClosedid)selflocalWindowr   remoteWindowr   r   r   r   s           I/home/dcms/DCMS/lib/python3.12/site-packages/twisted/conch/ssh/channel.py__init__zSSHChannel.__init__9   s      +4f#33,5 ,.		r   returnc                 @    | j                         j                  d      S )Nascii)	__bytes__decoder   s    r    __str__zSSHChannel.__str__T   s    ~~&&w//r   c                 Z    | j                   }|sd}d|| j                  | j                  fz  S )zD
        Return a byte string representation of the channel
        s   Nones   <SSHChannel %b (lw %d rw %d)>)r	   r   r   )r   r	   s     r    r%   zSSHChannel.__bytes__W   s<     yyD/  !!3
 
 	
r   c                     | j                   d uxr t        | j                         xs d}| j                  r| j                  j                  d      }nd}d| d| d| j                  j                          S )Nunknownr$   NonezSSHChannel z (z) on )r   strr	   r&   r   	logPrefix)r   r   r	   s      r    r.   zSSHChannel.logPrefixe   si    ggT!2c$''l@y9999##G,DDTF"RDdii.A.A.C-DEEr   c                 :    | j                   j                  d       y)z
        Called when the channel is opened.  specificData is any data that the
        other side sent us when opening the channel.

        @type specificData: L{bytes}
        zchannel openN_loginfo)r   r   s     r    channelOpenzSSHChannel.channelOpenm   s     			~&r   c                 >    | j                   j                  d|       y)z
        Called when the open failed for some reason.
        reason.desc is a string descrption, reason.code the SSH error code.

        @type reason: L{error.ConchError}
        z(other side refused open
reason: {reason})reasonN)r1   error)r   r5   s     r    
openFailedzSSHChannel.openFailedv   s     			CFSr   c                 b   | j                   |z   | _         | j                  s#| j                  sd| _        | j                          | j                  r$| j                  }d| _        | j                  |       | j                  r0| j                  }g | _        |D ]  \  }}| j                  ||        yy)z
        Called when bytes are added to the remote window.  By default it clears
        the data buffers.

        @type data:    L{bytes}
        Tr   N)r   r   r   startWritingr   writer   writeExtended)r   r   btypes       r    addWindowByteszSSHChannel.addWindowBytes   s     !% 5 5 <t||"DO88ADHJJqM;;ADK /
d""4./ r   c                     |j                  dd      j                  d      }t        | d|z   d      }|r ||      S | j                  j	                  d|       y)	aJ  
        Called when a request is sent to this channel.  By default it delegates
        to self.request_<requestType>.
        If this function returns true, the request succeeded, otherwise it
        failed.

        @type requestType:  L{bytes}
        @type data:         L{bytes}
        @rtype:             L{bool}
           -   _r$   request_Nz#unhandled request for {requestType})requestTyper   )replacer&   getattrr1   r2   )r   rC   r   foofs        r    requestReceivedzSSHChannel.requestReceived   sX     !!$-44W=D*s*D1T7N		<+Vr   c                 >    | j                   j                  d|       y)zL
        Called when we receive data.

        @type data: L{bytes}
        zgot data {data})r   Nr1   debugr   r   s     r    dataReceivedzSSHChannel.dataReceived   s     			)5r   c                 @    | j                   j                  d||       y)z
        Called when we receive extended data (usually standard error).

        @type dataType: L{int}
        @type data:     L{str}
        z%got extended data {dataType} {data!r})dataTyper   NrJ   r   rO   r   s      r    extReceivedzSSHChannel.extReceived   s      			3hT 	 	
r   c                 :    | j                   j                  d       y)zD
        Called when the other side will send no more data.
        z
remote eofNr0   r'   s    r    eofReceivedzSSHChannel.eofReceived   s     			|$r   c                 Z    | j                   j                  d       | j                          y)zD
        Called when the other side has closed the channel.
        zremote closeN)r1   r2   loseConnectionr'   s    r    closeReceivedzSSHChannel.closeReceived   s      			~&r   c                 :    | j                   j                  d       y)z
        Called when the channel is closed.  This means that both our side and
        the remote side have closed the channel.
        closedNr0   r'   s    r    rX   zSSHChannel.closed   s    
 			x r   c                    | j                   r| xj                   |z  c_         yt        |      }|| j                  kD  rG|d| j                   || j                  d c}| _         d| _        | j	                          | j                  }| j
                  }| j                  j                  }t        d||      }|D ]  } || ||||z            | xj                  |z  c_        | j                  r| j                   s| j                          yyy)z
        Write some data to the channel.  If there is not enough remote window
        available, buffer until it is.  Otherwise, split the data into
        packets of length remoteMaxPacket and send them.

        @type data: L{bytes}
        Nr   )r   lenr   r   stopWritingr   r   sendDataranger   rU   )r   r   toprmpr:   roffsets          r    r:   zSSHChannel.write   s     88HHH$i&&&,t,,-T**,- D$(  DO''C""		""!S# 	5F$Vfsl34	5$<<! !)<r   c                 *   | j                   rN| j                   d   d   |k(  r| j                   d   dxx   |z  cc<   y| j                   j                  ||g       yt        |      | j                  kD  r>|d| j                   ||| j                  d ggc}| _         d| _        | j                          t        |      | j                  kD  rq| j                  j                  | ||d| j                          || j                  d }| xj                  | j                  z  c_        t        |      | j                  kD  rq|r;| j                  j                  | ||       | xj                  t        |      z  c_        | j                  r| j                          yy)a  
        Send extended data to this channel.  If there is not enough remote
        window available, buffer until there is.  Otherwise, split the data
        into packets of length remoteMaxPacket and send them.

        @type dataType: L{int}
        @type data:     L{bytes}
        r   r   N)r   appendrZ   r   r   r[   r   r   sendExtendedDatar   rU   rP   s      r    r;   zSSHChannel.writeExtended   sq    ;;{{2q!X-B"d*"  ""Hd#34t9t,,,,t,,-D!6!6!89:; D$+  DO$i$...II&&tXt<Rd>R>R7ST,,./D!!T%9%99! $i$... II&&tXt<!!SY.!<<! r   c                 D    | j                  dj                  |             y)z
        Part of the Transport interface.  Write a list of strings to the
        channel.

        @type data: C{list} of L{str}
        r   N)r:   joinrL   s     r    writeSequencezSSHChannel.writeSequence
  s     	

388D>"r   c                 |    d| _         | j                  s)| j                  s| j                  j	                  |        yyy)zr
        Close the channel if there is no buferred data.  Otherwise, note the
        request and return.
        r   N)r   r   r   r   	sendCloser'   s    r    rU   zSSHChannel.loseConnection  s1    
 xxII% !,xr   c                 J    | j                   j                  j                         S )z
        See: L{ITransport.getPeer}

        @return: The remote address of this connection.
        @rtype: L{SSHTransportAddress}.
        )r   	transportgetPeerr'   s    r    rm   zSSHChannel.getPeer       yy""**,,r   c                 J    | j                   j                  j                         S )z
        See: L{ITransport.getHost}

        @return: An address describing this side of the connection.
        @rtype: L{SSHTransportAddress}.
        )r   rl   getHostr'   s    r    rp   zSSHChannel.getHost%  rn   r   c                      y)z
        Called when the remote buffer is full, as a hint to stop writing.
        This can be ignored, but it can be helpful.
        N r'   s    r    r[   zSSHChannel.stopWriting.      r   c                      y)ze
        Called when the remote buffer has more room, as a hint to continue
        writing.
        Nrr   r'   s    r    r9   zSSHChannel.startWriting4  rs   r   )r   r   r   r   NNN)__name__
__module____qualname____doc__r   r1   r	   bytes__annotations__r!   r-   r(   r%   r.   r3   r7   r>   rH   rM   rQ   rS   rV   rX   r:   r;   rh   rU   rm   rp   r[   r9   rr   r   r    r   r      s    @ 8DD% 60 0
5 
F'T/*$6	
%!":"@#&--r   r   N)rx   zope.interfacer   twisted.internetr   twisted.loggerr   twisted.pythonr   
ITransportr   rr   r   r    <module>r      sE   
 ' ' !  Z""#c c $cr   