Class Net::SSH::Transport::Session
In: lib/net/ssh/transport/session.rb
lib/net/ssh/transport/session.rb
Parent: Object

The transport layer represents the lowest level of the SSH protocol, and implements basic message exchanging and protocol initialization. It will never be instantiated directly (unless you really know what you‘re about), but will instead be created for you automatically when you create a new SSH session via Net::SSH.start.

Methods

Included Modules

Constants Loggable Constants Loggable

Constants

DEFAULT_PORT = 22   The standard port for the SSH protocol.
DEFAULT_PORT = 22   The standard port for the SSH protocol.

Attributes

algorithms  [R]  The Algorithms instance used to perform key exchanges.
algorithms  [R]  The Algorithms instance used to perform key exchanges.
host  [R]  The host to connect to, as given to the constructor.
host  [R]  The host to connect to, as given to the constructor.
host_key_verifier  [R]  The host-key verifier object used to verify host keys, to ensure that the connection is not being spoofed.
host_key_verifier  [R]  The host-key verifier object used to verify host keys, to ensure that the connection is not being spoofed.
options  [R]  The hash of options that were given to the object at initialization.
options  [R]  The hash of options that were given to the object at initialization.
port  [R]  The port number to connect to, as given in the options to the constructor. If no port number was given, this will default to DEFAULT_PORT.
port  [R]  The port number to connect to, as given in the options to the constructor. If no port number was given, this will default to DEFAULT_PORT.
server_version  [R]  The ServerVersion instance that encapsulates the negotiated protocol version.
server_version  [R]  The ServerVersion instance that encapsulates the negotiated protocol version.
socket  [R]  The underlying socket object being used to communicate with the remote host.
socket  [R]  The underlying socket object being used to communicate with the remote host.

Public Class methods

Instantiates a new transport layer abstraction. This will block until the initial key exchange completes, leaving you with a ready-to-use transport session.

[Source]

    # File lib/net/ssh/transport/session.rb, line 56
56:     def initialize(host, options={})
57:       self.logger = options[:logger]
58: 
59:       @host = host
60:       @port = options[:port] || DEFAULT_PORT
61:       @options = options
62: 
63:       debug { "establishing connection to #{@host}:#{@port}" }
64:       factory = options[:proxy] || TCPSocket
65:       @socket = timeout(options[:timeout] || 0) { factory.open(@host, @port) }
66:       @socket.extend(PacketStream)
67:       @socket.logger = @logger
68: 
69:       debug { "connection established" }
70: 
71:       @queue = []
72: 
73:       @host_key_verifier = select_host_key_verifier(options[:paranoid])
74: 
75:       @server_version = ServerVersion.new(socket, logger)
76: 
77:       @algorithms = Algorithms.new(self, options)
78:       wait { algorithms.initialized? }
79:     end

Instantiates a new transport layer abstraction. This will block until the initial key exchange completes, leaving you with a ready-to-use transport session.

[Source]

    # File lib/net/ssh/transport/session.rb, line 56
56:     def initialize(host, options={})
57:       self.logger = options[:logger]
58: 
59:       @host = host
60:       @port = options[:port] || DEFAULT_PORT
61:       @options = options
62: 
63:       debug { "establishing connection to #{@host}:#{@port}" }
64:       factory = options[:proxy] || TCPSocket
65:       @socket = timeout(options[:timeout] || 0) { factory.open(@host, @port) }
66:       @socket.extend(PacketStream)
67:       @socket.logger = @logger
68: 
69:       debug { "connection established" }
70: 
71:       @queue = []
72: 
73:       @host_key_verifier = select_host_key_verifier(options[:paranoid])
74: 
75:       @server_version = ServerVersion.new(socket, logger)
76: 
77:       @algorithms = Algorithms.new(self, options)
78:       wait { algorithms.initialized? }
79:     end

Public Instance methods

Cleans up (see PacketStream#cleanup) and closes the underlying socket.

[Source]

     # File lib/net/ssh/transport/session.rb, line 102
102:     def close
103:       socket.cleanup
104:       socket.close
105:     end

Cleans up (see PacketStream#cleanup) and closes the underlying socket.

[Source]

     # File lib/net/ssh/transport/session.rb, line 102
102:     def close
103:       socket.cleanup
104:       socket.close
105:     end

Returns true if the underlying socket has been closed.

[Source]

    # File lib/net/ssh/transport/session.rb, line 97
97:     def closed?
98:       socket.closed?
99:     end

Returns true if the underlying socket has been closed.

[Source]

    # File lib/net/ssh/transport/session.rb, line 97
97:     def closed?
98:       socket.closed?
99:     end

Configure‘s the packet stream‘s client state with the given set of options. This is typically used to define the cipher, compression, and hmac algorithms to use when sending packets to the server.

[Source]

     # File lib/net/ssh/transport/session.rb, line 220
220:     def configure_client(options={})
221:       socket.client.set(options)
222:     end

Configure‘s the packet stream‘s client state with the given set of options. This is typically used to define the cipher, compression, and hmac algorithms to use when sending packets to the server.

[Source]

     # File lib/net/ssh/transport/session.rb, line 220
220:     def configure_client(options={})
221:       socket.client.set(options)
222:     end

Configure‘s the packet stream‘s server state with the given set of options. This is typically used to define the cipher, compression, and hmac algorithms to use when reading packets from the server.

[Source]

     # File lib/net/ssh/transport/session.rb, line 227
227:     def configure_server(options={})
228:       socket.server.set(options)
229:     end

Configure‘s the packet stream‘s server state with the given set of options. This is typically used to define the cipher, compression, and hmac algorithms to use when reading packets from the server.

[Source]

     # File lib/net/ssh/transport/session.rb, line 227
227:     def configure_server(options={})
228:       socket.server.set(options)
229:     end

Enqueues the given message, such that it will be sent at the earliest opportunity. This does not block, but returns immediately.

[Source]

     # File lib/net/ssh/transport/session.rb, line 213
213:     def enqueue_message(message)
214:       socket.enqueue_packet(message)
215:     end

Enqueues the given message, such that it will be sent at the earliest opportunity. This does not block, but returns immediately.

[Source]

     # File lib/net/ssh/transport/session.rb, line 213
213:     def enqueue_message(message)
214:       socket.enqueue_packet(message)
215:     end

Sets a new hint for the packet stream, which the packet stream may use to change its behavior. (See PacketStream#hints).

[Source]

     # File lib/net/ssh/transport/session.rb, line 233
233:     def hint(which, value=true)
234:       socket.hints[which] = value
235:     end

Sets a new hint for the packet stream, which the packet stream may use to change its behavior. (See PacketStream#hints).

[Source]

     # File lib/net/ssh/transport/session.rb, line 233
233:     def hint(which, value=true)
234:       socket.hints[which] = value
235:     end

Returns the host (and possibly IP address) in a format compatible with SSH known-host files.

[Source]

    # File lib/net/ssh/transport/session.rb, line 83
83:     def host_as_string
84:       @host_as_string ||= begin
85:         string = "#{host}"
86:         string = "[#{string}]:#{port}" if port != DEFAULT_PORT
87:         if socket.peer_ip != host
88:           string2 = socket.peer_ip
89:           string2 = "[#{string2}]:#{port}" if port != DEFAULT_PORT
90:           string << "," << string2
91:         end
92:         string
93:       end
94:     end

Returns the host (and possibly IP address) in a format compatible with SSH known-host files.

[Source]

    # File lib/net/ssh/transport/session.rb, line 83
83:     def host_as_string
84:       @host_as_string ||= begin
85:         string = "#{host}"
86:         string = "[#{string}]:#{port}" if port != DEFAULT_PORT
87:         if socket.peer_ip != host
88:           string2 = socket.peer_ip
89:           string2 = "[#{string2}]:#{port}" if port != DEFAULT_PORT
90:           string << "," << string2
91:         end
92:         string
93:       end
94:     end

Blocks until a new packet is available to be read, and returns that packet. See poll_message.

[Source]

     # File lib/net/ssh/transport/session.rb, line 139
139:     def next_message
140:       poll_message(:block)
141:     end

Blocks until a new packet is available to be read, and returns that packet. See poll_message.

[Source]

     # File lib/net/ssh/transport/session.rb, line 139
139:     def next_message
140:       poll_message(:block)
141:     end

Returns a hash of information about the peer (remote) side of the socket, including :ip, :port, :host, and :canonized (see host_as_string).

[Source]

     # File lib/net/ssh/transport/session.rb, line 133
133:     def peer
134:       @peer ||= { :ip => socket.peer_ip, :port => @port.to_i, :host => @host, :canonized => host_as_string }
135:     end

Returns a hash of information about the peer (remote) side of the socket, including :ip, :port, :host, and :canonized (see host_as_string).

[Source]

     # File lib/net/ssh/transport/session.rb, line 133
133:     def peer
134:       @peer ||= { :ip => socket.peer_ip, :port => @port.to_i, :host => @host, :canonized => host_as_string }
135:     end

Tries to read the next packet from the socket. If mode is :nonblock (the default), this will not block and will return nil if there are no packets waiting to be read. Otherwise, this will block until a packet is available. Note that some packet types (DISCONNECT, IGNORE, UNIMPLEMENTED, DEBUG, and KEXINIT) are handled silently by this method, and will never be returned.

If a key-exchange is in process and a disallowed packet type is received, it will be enqueued and otherwise ignored. When a key-exchange is not in process, and consume_queue is true, packets will be first read from the queue before the socket is queried.

[Source]

     # File lib/net/ssh/transport/session.rb, line 154
154:     def poll_message(mode=:nonblock, consume_queue=true)
155:       loop do
156:         if consume_queue && @queue.any? && algorithms.allow?(@queue.first)
157:           return @queue.shift
158:         end
159: 
160:         packet = socket.next_packet(mode)
161:         return nil if packet.nil?
162: 
163:         case packet.type
164:         when DISCONNECT
165:           raise Net::SSH::Disconnect, "disconnected: #{packet[:description]} (#{packet[:reason_code]})"
166: 
167:         when IGNORE
168:           debug { "IGNORE packet recieved: #{packet[:data].inspect}" }
169: 
170:         when UNIMPLEMENTED
171:           lwarn { "UNIMPLEMENTED: #{packet[:number]}" }
172: 
173:         when DEBUG
174:           send(packet[:always_display] ? :fatal : :debug) { packet[:message] }
175: 
176:         when KEXINIT
177:           algorithms.accept_kexinit(packet)
178: 
179:         else
180:           return packet if algorithms.allow?(packet)
181:           push(packet)
182:         end
183:       end
184:     end

Tries to read the next packet from the socket. If mode is :nonblock (the default), this will not block and will return nil if there are no packets waiting to be read. Otherwise, this will block until a packet is available. Note that some packet types (DISCONNECT, IGNORE, UNIMPLEMENTED, DEBUG, and KEXINIT) are handled silently by this method, and will never be returned.

If a key-exchange is in process and a disallowed packet type is received, it will be enqueued and otherwise ignored. When a key-exchange is not in process, and consume_queue is true, packets will be first read from the queue before the socket is queried.

[Source]

     # File lib/net/ssh/transport/session.rb, line 154
154:     def poll_message(mode=:nonblock, consume_queue=true)
155:       loop do
156:         if consume_queue && @queue.any? && algorithms.allow?(@queue.first)
157:           return @queue.shift
158:         end
159: 
160:         packet = socket.next_packet(mode)
161:         return nil if packet.nil?
162: 
163:         case packet.type
164:         when DISCONNECT
165:           raise Net::SSH::Disconnect, "disconnected: #{packet[:description]} (#{packet[:reason_code]})"
166: 
167:         when IGNORE
168:           debug { "IGNORE packet recieved: #{packet[:data].inspect}" }
169: 
170:         when UNIMPLEMENTED
171:           lwarn { "UNIMPLEMENTED: #{packet[:number]}" }
172: 
173:         when DEBUG
174:           send(packet[:always_display] ? :fatal : :debug) { packet[:message] }
175: 
176:         when KEXINIT
177:           algorithms.accept_kexinit(packet)
178: 
179:         else
180:           return packet if algorithms.allow?(packet)
181:           push(packet)
182:         end
183:       end
184:     end

Adds the given packet to the packet queue. If the queue is non-empty, poll_message will return packets from the queue in the order they were received.

[Source]

     # File lib/net/ssh/transport/session.rb, line 201
201:     def push(packet)
202:       @queue.push(packet)
203:     end

Adds the given packet to the packet queue. If the queue is non-empty, poll_message will return packets from the queue in the order they were received.

[Source]

     # File lib/net/ssh/transport/session.rb, line 201
201:     def push(packet)
202:       @queue.push(packet)
203:     end

Requests a rekey operation, and blocks until the operation completes. If a rekey is already pending, this returns immediately, having no effect.

[Source]

     # File lib/net/ssh/transport/session.rb, line 116
116:     def rekey!
117:       if !algorithms.pending?
118:         algorithms.rekey!
119:         wait { algorithms.initialized? }
120:       end
121:     end

Requests a rekey operation, and blocks until the operation completes. If a rekey is already pending, this returns immediately, having no effect.

[Source]

     # File lib/net/ssh/transport/session.rb, line 116
116:     def rekey!
117:       if !algorithms.pending?
118:         algorithms.rekey!
119:         wait { algorithms.initialized? }
120:       end
121:     end

Returns immediately if a rekey is already in process. Otherwise, if a rekey is needed (as indicated by the socket, see PacketStream#if_needs_rekey?) one is performed, causing this method to block until it completes.

[Source]

     # File lib/net/ssh/transport/session.rb, line 126
126:     def rekey_as_needed
127:       return if algorithms.pending?
128:       socket.if_needs_rekey? { rekey! }
129:     end

Returns immediately if a rekey is already in process. Otherwise, if a rekey is needed (as indicated by the socket, see PacketStream#if_needs_rekey?) one is performed, causing this method to block until it completes.

[Source]

     # File lib/net/ssh/transport/session.rb, line 126
126:     def rekey_as_needed
127:       return if algorithms.pending?
128:       socket.if_needs_rekey? { rekey! }
129:     end

Sends the given message via the packet stream, blocking until the entire message has been sent.

[Source]

     # File lib/net/ssh/transport/session.rb, line 207
207:     def send_message(message)
208:       socket.send_packet(message)
209:     end

Sends the given message via the packet stream, blocking until the entire message has been sent.

[Source]

     # File lib/net/ssh/transport/session.rb, line 207
207:     def send_message(message)
208:       socket.send_packet(message)
209:     end

Returns a new service_request packet for the given service name, ready for sending to the server.

[Source]

     # File lib/net/ssh/transport/session.rb, line 109
109:     def service_request(service)
110:       Net::SSH::Buffer.from(:byte, SERVICE_REQUEST, :string, service)
111:     end

Returns a new service_request packet for the given service name, ready for sending to the server.

[Source]

     # File lib/net/ssh/transport/session.rb, line 109
109:     def service_request(service)
110:       Net::SSH::Buffer.from(:byte, SERVICE_REQUEST, :string, service)
111:     end

Waits (blocks) until the given block returns true. If no block is given, this just waits long enough to see if there are any pending packets. Any packets read are enqueued (see push).

[Source]

     # File lib/net/ssh/transport/session.rb, line 189
189:     def wait
190:       loop do
191:         break if block_given? && yield
192:         message = poll_message(:nonblock, false)
193:         push(message) if message
194:         break if !block_given?
195:       end
196:     end

Waits (blocks) until the given block returns true. If no block is given, this just waits long enough to see if there are any pending packets. Any packets read are enqueued (see push).

[Source]

     # File lib/net/ssh/transport/session.rb, line 189
189:     def wait
190:       loop do
191:         break if block_given? && yield
192:         message = poll_message(:nonblock, false)
193:         push(message) if message
194:         break if !block_given?
195:       end
196:     end

[Validate]