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 97
 97:     def close
 98:       socket.cleanup
 99:       socket.close
100:     end

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

[Source]

     # File lib/net/ssh/transport/session.rb, line 97
 97:     def close
 98:       socket.cleanup
 99:       socket.close
100:     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 215
215:     def configure_client(options={})
216:       socket.client.set(options)
217:     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 215
215:     def configure_client(options={})
216:       socket.client.set(options)
217:     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 222
222:     def configure_server(options={})
223:       socket.server.set(options)
224:     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 222
222:     def configure_server(options={})
223:       socket.server.set(options)
224:     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 208
208:     def enqueue_message(message)
209:       socket.enqueue_packet(message)
210:     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 208
208:     def enqueue_message(message)
209:       socket.enqueue_packet(message)
210:     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 228
228:     def hint(which, value=true)
229:       socket.hints[which] = value
230:     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 228
228:     def hint(which, value=true)
229:       socket.hints[which] = value
230:     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 134
134:     def next_message
135:       poll_message(:block)
136:     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 134
134:     def next_message
135:       poll_message(:block)
136:     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 128
128:     def peer
129:       @peer ||= { :ip => socket.peer_ip, :port => @port.to_i, :host => @host, :canonized => host_as_string }
130:     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 128
128:     def peer
129:       @peer ||= { :ip => socket.peer_ip, :port => @port.to_i, :host => @host, :canonized => host_as_string }
130:     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 149
149:     def poll_message(mode=:nonblock, consume_queue=true)
150:       loop do
151:         if consume_queue && @queue.any? && algorithms.allow?(@queue.first)
152:           return @queue.shift
153:         end
154: 
155:         packet = socket.next_packet(mode)
156:         return nil if packet.nil?
157: 
158:         case packet.type
159:         when DISCONNECT
160:           raise Net::SSH::Disconnect, "disconnected: #{packet[:description]} (#{packet[:reason_code]})"
161: 
162:         when IGNORE
163:           debug { "IGNORE packet recieved: #{packet[:data].inspect}" }
164: 
165:         when UNIMPLEMENTED
166:           lwarn { "UNIMPLEMENTED: #{packet[:number]}" }
167: 
168:         when DEBUG
169:           send(packet[:always_display] ? :fatal : :debug) { packet[:message] }
170: 
171:         when KEXINIT
172:           algorithms.accept_kexinit(packet)
173: 
174:         else
175:           return packet if algorithms.allow?(packet)
176:           push(packet)
177:         end
178:       end
179:     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 149
149:     def poll_message(mode=:nonblock, consume_queue=true)
150:       loop do
151:         if consume_queue && @queue.any? && algorithms.allow?(@queue.first)
152:           return @queue.shift
153:         end
154: 
155:         packet = socket.next_packet(mode)
156:         return nil if packet.nil?
157: 
158:         case packet.type
159:         when DISCONNECT
160:           raise Net::SSH::Disconnect, "disconnected: #{packet[:description]} (#{packet[:reason_code]})"
161: 
162:         when IGNORE
163:           debug { "IGNORE packet recieved: #{packet[:data].inspect}" }
164: 
165:         when UNIMPLEMENTED
166:           lwarn { "UNIMPLEMENTED: #{packet[:number]}" }
167: 
168:         when DEBUG
169:           send(packet[:always_display] ? :fatal : :debug) { packet[:message] }
170: 
171:         when KEXINIT
172:           algorithms.accept_kexinit(packet)
173: 
174:         else
175:           return packet if algorithms.allow?(packet)
176:           push(packet)
177:         end
178:       end
179:     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 196
196:     def push(packet)
197:       @queue.push(packet)
198:     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 196
196:     def push(packet)
197:       @queue.push(packet)
198:     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 111
111:     def rekey!
112:       if !algorithms.pending?
113:         algorithms.rekey!
114:         wait { algorithms.initialized? }
115:       end
116:     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 111
111:     def rekey!
112:       if !algorithms.pending?
113:         algorithms.rekey!
114:         wait { algorithms.initialized? }
115:       end
116:     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 121
121:     def rekey_as_needed
122:       return if algorithms.pending?
123:       socket.if_needs_rekey? { rekey! }
124:     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 121
121:     def rekey_as_needed
122:       return if algorithms.pending?
123:       socket.if_needs_rekey? { rekey! }
124:     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 202
202:     def send_message(message)
203:       socket.send_packet(message)
204:     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 202
202:     def send_message(message)
203:       socket.send_packet(message)
204:     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 104
104:     def service_request(service)
105:       Net::SSH::Buffer.from(:byte, SERVICE_REQUEST, :string, service)
106:     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 104
104:     def service_request(service)
105:       Net::SSH::Buffer.from(:byte, SERVICE_REQUEST, :string, service)
106:     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 184
184:     def wait
185:       loop do
186:         break if block_given? && yield
187:         message = poll_message(:nonblock, false)
188:         push(message) if message
189:         break if !block_given?
190:       end
191:     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 184
184:     def wait
185:       loop do
186:         break if block_given? && yield
187:         message = poll_message(:nonblock, false)
188:         push(message) if message
189:         break if !block_given?
190:       end
191:     end

[Validate]