aboutsummaryrefslogblamecommitdiffstats
path: root/lib/ssh/doc/src/ssh_channel.xml
blob: 66b3b8b65613dc945fc9ac92b1eb61896974c489 (plain) (tree)
1
2
3
4
5
6
7
8
                                            





                                     
                       















                                                                            
                              

                              
                                         

                  


                                                                     
                                                                  






                                                                        
        
 

                                                                                      
                                                              
                                                                                     
                                                              
                


                
                              








                                                                      
                                                           

                                                                
                                                                                                                    




                                           
                                                                            




                                                                  
                                   

                                         
 



                                                                     
                                                       
                                                              

                                                                     
















                                                                   
                                                                                
                               





                                          
                                                                              
            
                                                                                                        

             
                                                           
                                                                  
                                                                       



                                                                            
                                                                                  




             
                                                                                         

                                                            



                                          






                                                            
                                                                        

                                                             
                                                               


                                                              

                                                                         

                                                                  
                                              


                    





                                                                  














                                                                      
                                                                                    


                                                                     

                                                                          






                                                                             
                                                                           


                                                   

                                                                                                              







                                                                            



                                                                     






             

                                      

                                                                        

                                                                                           
                                                             



            
                                                            


                                                                        











                                                                      

             
                                                            
 

















                                                                                    
          



             
                                                                       






                                                                      











                                                                           
                                                                 






                                                                  

                                                                                  

                                                                                  
                                



                                           
                                                                  






                                                                        
                                                           




                                                 
                                                                        
                                      
                                 







                                                                        



             
                                                          

                                     
                                                                    


                                                           
                                           












                                                                          

                                                                   
                                                                




                                                                    





                  
                                                                     
                              

                                                                      
                                                                                         
                                           


                             
                                                                  

                                   

                                                                    
                                

                 
                                                                  







                                                                      
                                                       






                                                                  





                                                                    






             
<?xml version="1.0" encoding="iso-8859-1" ?>
<!DOCTYPE erlref SYSTEM "erlref.dtd">

<erlref>
  <header>
    <copyright>
      <year>2009</year>
      <year>2013</year>
      <holder>Ericsson AB, All Rights Reserved</holder>
    </copyright>
    <legalnotice>
      The contents of this file are subject to the Erlang Public License,
      Version 1.1, (the "License"); you may not use this file except in
      compliance with the License. You should have received a copy of the
      Erlang Public License along with this software. If not, it can be
      retrieved online at http://www.erlang.org/.

      Software distributed under the License is distributed on an "AS IS"
      basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
      the License for the specific language governing rights and limitations
      under the License.

      The Initial Developer of the Original Code is Ericsson AB.
    </legalnotice>
    <title>ssh_channel</title>
  </header>
  <module>ssh_channel</module>
  <modulesummary>-behaviour(ssh_channel).
  </modulesummary>
  <description>
    <p>SSH services (clients and servers) are implemented as channels
    that are multiplexed over an SSH connection and communicates via
    the <url href="http://www.ietf.org/rfc/rfc4254.txt"> SSH
    Connection Protocol</url>. This module provides a callback API
    that takes care of generic channel aspects such as flow control
    and close messages and lets the callback functions take care of
    the service (application) specific parts. This behavior also ensures
    that the channel process honors the principal of an OTP-process so
    that it can be part of a supervisor tree. This is a requirement of
    channel processes implementing a subsystem that will be added to
    the SSH applications supervisor tree.
    </p>

    <note> <p>When implementing a SSH subsystem use the
    <c>-behaviour(ssh_daemon_channel).</c> instead of  <c>-behaviour(ssh_channel).</c>
    as the only relevant callback functions for subsystems are
    init/1, handle_ssh_msg/2, handle_msg/2 and terminate/2, so the ssh_daemon_channel
    behaviour is limited version of the ssh_channel behaviour.
    </p> </note>
  </description>

  <section>
    <title>DATA TYPES </title>

    <p>Type definitions that are used more than once in this module
    and/or abstractions to indicate the intended use of the data
    type:</p>
    
    <p><c>boolean() = true | false </c></p>
    <p><c>string() = list of ASCII characters</c></p>
    <p><c>timeout() =  infinity | integer() - in milliseconds.</c></p>
    <p><c>ssh_connection_ref() - opaque to the user returned by
    ssh:connect/3 or sent to an SSH channel process</c></p>
    <p><c>ssh_channel_id() = integer() </c></p>
    <p><c>ssh_data_type_code() = 1 ("stderr") | 0 ("normal") are
    currently valid values see <url href="http://www.ietf.org/rfc/rfc4254.txt">RFC 4254  </url> section 5.2.</c></p>
  </section>

  <funcs>
    <func>
      <name>call(ChannelRef, Msg) -></name>
      <name>call(ChannelRef, Msg, Timeout) -> Reply | {error, Reason}</name>
      <fsummary> Makes a synchronous call to a channel.</fsummary>
      <type>
	<v>ChannelRef = pid() </v>
	<d>As returned by start_link/4 </d>
	<v>Msg = term() </v>
	<v>Timeout = timeout() </v>
	<v>Reply = term() </v>
	<v>Reason = closed | timeout </v>

      </type>
      <desc>
	<p>Makes a synchronous call to the channel process by sending
	a message and waiting until a reply arrives or a timeout
	occurs. The channel will call <seealso marker =
	"#Module:handle_call-3">Module:handle_call/3</seealso>
	to handle the message.  If the channel process does not exist
	<c>{error, closed}</c> is returned.
	</p>
      </desc>
    </func>
    
    <func>
      <name>cast(ChannelRef, Msg) -> ok </name>
      <fsummary>Sends an asynchronous message to the channel
	ChannelRef and returns ok.</fsummary>
      <type>
	<v>ChannelRef = pid() </v>
	<d>As returned by start_link/4 </d>
	<v>Msg = term() </v>
      </type>
      <desc>
	<p>Sends an asynchronous message to the channel process and
	returns ok immediately, ignoring if the destination node or
	channel process does not exist. The channel will call
	<seealso marker = "#Module:handle_cast-2">Module:handle_cast/2</seealso>
	 to handle the message.
	</p>
      </desc>
    </func>

       <func>
      <name>enter_loop(State) -> _ </name>
      <fsummary> Makes an existing process an ssh_channel process. </fsummary>
      <type>
	<v> State = term() - as returned by <seealso marker = "#init-1">ssh_channel:init/1</seealso></v>
      </type>
      <desc>
	<p> Makes an existing process an <c>ssh_channel</c>
	process. Does not return, instead the calling process will
	enter the <c>ssh_channel</c> process receive loop and become an
	<c>ssh_channel process.</c> The process must have been started using
	one of the start functions in proc_lib, see <seealso
	marker="stdlib:proc_lib">proc_lib(3)</seealso>. The
	user is responsible for any initialization of the process
	and needs to call <seealso marker = "#init-1">ssh_channel:init/1</seealso>
	</p>
      </desc>
    </func>
    
    <func>
      <name>init(Options) -> {ok, State} |  {ok, State, Timeout} | {stop, Reason} </name>
      <fsummary> Initiates a ssh_channel process.</fsummary>
      <type>
	<v>Options = [{Option, Value}]</v>
	<v>State = term()</v>
	<v>Timeout = timeout() </v>
	<v>Reason = term() </v>
      </type>
      <desc>
	<p>
	  The following options must be present:
	</p>
	  <taglist>
	  <tag><c><![CDATA[{channel_cb, atom()}]]></c></tag>
	  <item>The module that implements the channel behaviour.</item>
	  
	  <tag><c><![CDATA[{init_args(), list()}]]></c></tag>
	  <item> The list of arguments to the callback module's
	  init function.</item>

	  <tag><c><![CDATA[{cm, connection_ref()}]]></c></tag>
	  <item> Reference to the ssh connection as returned by  <seealso
	  marker="ssh#connect-3">ssh:connect/3</seealso></item>
	  
	  <tag><c><![CDATA[{channel_id, channel_id()}]]></c></tag>
	  <item> Id of the SSH channel.</item>
	  
	  </taglist>

	  <note><p>This function is normally not called by the
	  user. The user only needs to call if for some reason the
	  channel process needs to be started with help of
	  <c>proc_lib</c> instead of calling
	  <c>ssh_channel:start/4</c> or
	  <c>ssh_channel:start_link/4</c> </p>
	  </note>
      </desc>
    </func>

    <func>
      <name>reply(Client, Reply) -> _</name>
      <fsummary>Send a reply to a client.</fsummary>
      <type>
        <v>Client - opaque to the user, see explanation below</v>
        <v>Reply = term()</v>
      </type>
      <desc>
        <p>This function can be used by a channel to explicitly send a
        reply to a client that called <c>call/[2,3]</c> when the reply
        cannot be defined in the return value of
	<seealso marker ="#Module:handle_call-3">Module:handle_call/3</seealso>.</p>
        <p><c>Client</c> must be the <c>From</c> argument provided to
          the callback function <c>handle_call/3</c>.
	  <c>Reply</c> is an arbitrary term,
	  which will be given back to the client as the return value of
          <seealso marker="#call-2">ssh_channel:call/[2,3].</seealso>></p>
      </desc>
    </func>
    
    <func>
      <name>start(SshConnection, ChannelId, ChannelCb, CbInitArgs) -> </name>
      <name>start_link(SshConnection, ChannelId, ChannelCb, CbInitArgs) ->
      {ok, ChannelRef} | {error, Reason}</name>
      <fsummary> Starts a processes that handles a SSH channel. </fsummary>
      <type>
	<v>SshConnection = ssh_connection_ref()</v>
	<v>ChannelId = ssh_channel_id() </v>
	<d> As returned by cannot be defined in the return value of
	<seealso marker ="ssh_connection#session_channel/2">ssh_connection:session_channel/[2,4]</seealso></d>
	<v>ChannelCb = atom()</v>
	<d> The name of the module implementing the service specific parts
	of the channel.</d>
	<v>CbInitArgs = [term()]</v>
	<d>Argument  list for the init function in the callback module. </d>
	<v>ChannelRef = pid()</v>
      </type>
      <desc>
	<p>Starts a processes that handles an SSH channel. It will be
	called internally by the SSH daemon or explicitly by the SSH
	client implementations. The behavior will set the
	<c>trap_exit</c> flag to true.
	</p>
      </desc>
    </func>
    
  </funcs>
  
  <section>
    <marker id="cb_timeouts"></marker>
    <title> CALLBACK TIMEOUTS</title>

    <p>The timeout values that may be returned by the callback functions
    has the same semantics as in a <seealso marker="stdlib:gen_server">gen_server</seealso>
    If the timeout occurs <seealso marker="#Module:handle_msg-2">handle_msg/2</seealso>
    will be called as <c>handle_msg(timeout, State). </c></p>
  </section>

  <funcs>
    <func>
      <name>Module:code_change(OldVsn, State, Extra) -> {ok,
      NewState}</name>
      <fsummary> Converts process state when code is changed.</fsummary>
      <type>
	<v>OldVsn = term()</v>
	<d>In the case of an upgrade, <c>OldVsn</c> is <c>Vsn</c>, and
	in the case of a downgrade, <c>OldVsn</c> is
	<c>{down,Vsn}</c>. <c>Vsn</c> is defined by the <c>vsn</c>
	attribute(s) of the old version of the callback module
	<c>Module</c>. If no such attribute is defined, the version is
	the checksum of the BEAM file.</d>
	<v>State = term()</v>
	<d>The internal state of the channel.</d>
        <v>Extra = term()</v>
	<d>Passed as-is from the <c>{advanced,Extra}</c>
	part of the update instruction.</d>
      </type>
      <desc>
	<p> Converts process state when code is changed.</p>

	<p>This function is called by a client side channel when it
	should update its internal state during a release
	upgrade/downgrade, i.e. when the instruction
	<c>{update,Module,Change,...}</c> where
	<c>Change={advanced,Extra}</c> is given in the <c>appup</c>
	file. See <seealso marker="doc/design_principles:release_handling#instr">OTP
	Design Principles</seealso> for more information.
	</p>

	<note><p>Soft upgrade according to the OTP release concept
	is not straight forward for the server side, as subsystem
	channel processes are spawned by the SSH application and
	hence added to its supervisor tree. It could be possible to
	upgrade the subsystem channels, when upgrading the user
	application, if the callback functions can handle two
	versions of the state, but this function can not be used in
	the normal way.</p>
	</note>
	  
      </desc>
    </func>
    
    <func>
      <name>Module:init(Args) -> {ok, State} | {ok, State, timeout()} |
      {stop, Reason}</name>
      <fsummary> Makes necessary initializations and returns the
      initial channel state if the initializations succeed.</fsummary>
      <type>
	<v> Args = term() </v>
	<d> Last argument to ssh_channel:start_link/4.</d>
	<v> State = term() </v>
	<v> Reason = term() </v>
      </type>
      <desc>
	<p> Makes necessary initializations and returns the initial channel
	state if the initializations succeed.
	</p>
	<p>For more detailed information on timeouts see the section 
	<seealso marker="#cb_timeouts">CALLBACK TIMEOUTS</seealso>. </p>
      </desc>
    </func>
   
    <func>
      <name>Module:handle_call(Msg, From, State) -> Result</name>
      <fsummary> Handles messages sent by calling
      <c>ssh_channel:call/[2,3]</c></fsummary>
      <type>
	<v>Msg = term()</v>
	<v>From = opaque to the user should be used as argument to
	ssh_channel:reply/2</v>
	<v>State = term()</v>
	<v>Result = {reply, Reply, NewState} | {reply, Reply, NewState, timeout()}
	| {noreply, NewState} | {noreply , NewState, timeout()}
	| {stop, Reason, Reply, NewState} | {stop, Reason, NewState} </v>
	<v>Reply = term() - will be the return value of ssh_channel:call/[2,3]</v>
	<v>NewState = term()</v>
	<v>Reason = term()</v>
      </type>
      <desc>
	<p>Handles messages sent by calling
	<seealso marker="#call-2">ssh_channel:call/[2,3]</seealso>
	</p>
	<p>For more detailed information on timeouts see the section 
	<seealso marker="#cb_timeouts">CALLBACK TIMEOUTS</seealso>. </p>
      </desc>
    </func>

    <func>
      <name>Module:handle_cast(Msg, State) -> Result</name>
      <fsummary> Handles messages sent by calling
      <c>ssh_channel:cact/2</c></fsummary>
      <type>
	<v>Msg = term()</v>
	<v>State = term()</v>
	<v>Result = {noreply, NewState} | {noreply, NewState, timeout()}
	| {stop, Reason, NewState}</v>
	<v>NewState = term() </v>
	<v>Reason = term()</v>
      </type>
      <desc>
	<p> Handles messages sent by calling
	<c>ssh_channel:cast/2</c>
	</p>
	<p>For more detailed information on timeouts see the section 
	<seealso marker="#cb_timeouts">CALLBACK TIMEOUTS</seealso>. </p>
      </desc>
    </func>
 
    <func>
      <name>Module:handle_msg(Msg, State) -> {ok, State} |
      {stop, ChannelId, State}</name>

      <fsummary> Handle other messages than SSH connection protocol,
      call or cast messages sent to the channel.</fsummary>
      <type>
	<v>Msg = timeout | term()</v>
	<v>ChannelId = ssh_channel_id()</v>
	<v>State = term() </v>
      </type>
      <desc>
	<p>Handle other messages than ssh connection protocol, call or
	cast messages sent to the channel. 
	</p>

	<p> Possible erlang 'EXIT'-messages should be handled by this
	function and all channels should handle the following message.</p>
	
	<taglist>
	  <tag><c><![CDATA[{ssh_channel_up, ssh_channel_id(), 
	  ssh_connection_ref()}]]></c></tag>
	  <item>This is the first messages that will be received by
	  the channel, it is sent just before the <seealso
	  marker="#init-1">ssh_channel:init/1</seealso> function
	  returns successfully.  This is especially useful if the
	  server wants to send a message to the client without first
	  receiving a message from it. If the message is not
	  useful for your particular scenario just ignore it by
	  immediately returning {ok, State}.
	  </item>
	</taglist>
      </desc>
    </func>

    <func>
      <name>Module:handle_ssh_msg(Msg, State) -> {ok, State} | {stop,
      ChannelId, State}</name>
      <fsummary> Handles ssh connection protocol messages. </fsummary>
      <type>
	<v>Msg = <seealso marker="ssh_connection"> ssh_connection:event() </seealso> </v>
	<v>ChannelId = ssh_channel_id()</v>
	<v>State = term()</v>
      </type>
      <desc>
	<p> Handles SSH connection protocol messages that may need
	service specific attention.
	</p>
	
	<p> The following message is completely taken care of by the
	SSH channel behavior</p>
	
	<taglist>
	  <tag><c><![CDATA[{closed, ssh_channel_id()}]]></c></tag>
	  <item> The channel behavior will send a close message to the
	  other side if such a message has not already been sent and
	  then terminate the channel with reason normal.</item>
	</taglist>
      </desc>
    </func>

    <func>
      <name>Module:terminate(Reason, State) -> _</name>
      <fsummary> </fsummary>
      <type>
	<v>Reason = term()</v>
	<v>State = term()</v>
      </type>
      <desc>
	<p>This function is called by a channel process when it is
	about to terminate. Before this function is called <seealso
	marker="ssh_connection#close-2"> ssh_connection:close/2
	</seealso> will be called if it has not been called earlier.
	This function should do any necessary cleaning
	up. When it returns, the channel process terminates with
	reason <c>Reason</c>. The return value is ignored.
	</p>
      </desc>
    </func>
    
  </funcs>
  
</erlref>