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
public class
public instance
Constants
DEFAULT_PORT | = | 22 | The standard port for the SSH protocol. |
Attributes
algorithms | [R] | The Algorithms instance used to perform key exchanges. |
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. |
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. |
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. |
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.
# 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.
# 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.
# 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.
# File lib/net/ssh/transport/session.rb, line 229 229: def configure_client(options={}) 230: socket.client.set(options) 231: 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.
# File lib/net/ssh/transport/session.rb, line 236 236: def configure_server(options={}) 237: socket.server.set(options) 238: end
Enqueues the given message, such that it will be sent at the earliest opportunity. This does not block, but returns immediately.
# File lib/net/ssh/transport/session.rb, line 222 222: def enqueue_message(message) 223: socket.enqueue_packet(message) 224: end
Sets a new hint for the packet stream, which the packet stream may use to change its behavior. (See PacketStream#hints).
# File lib/net/ssh/transport/session.rb, line 242 242: def hint(which, value=true) 243: socket.hints[which] = value 244: end
Returns the host (and possibly IP address) in a format compatible with SSH known-host files.
# 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.
# File lib/net/ssh/transport/session.rb, line 148 148: def next_message 149: poll_message(:block) 150: end
Returns a hash of information about the peer (remote) side of the socket, including :ip, :port, :host, and :canonized (see host_as_string).
# File lib/net/ssh/transport/session.rb, line 142 142: def peer 143: @peer ||= { :ip => socket.peer_ip, :port => @port.to_i, :host => @host, :canonized => host_as_string } 144: 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.
# File lib/net/ssh/transport/session.rb, line 163 163: def poll_message(mode=:nonblock, consume_queue=true) 164: loop do 165: if consume_queue && @queue.any? && algorithms.allow?(@queue.first) 166: return @queue.shift 167: end 168: 169: packet = socket.next_packet(mode) 170: return nil if packet.nil? 171: 172: case packet.type 173: when DISCONNECT 174: raise Net::SSH::Disconnect, "disconnected: #{packet[:description]} (#{packet[:reason_code]})" 175: 176: when IGNORE 177: debug { "IGNORE packet recieved: #{packet[:data].inspect}" } 178: 179: when UNIMPLEMENTED 180: lwarn { "UNIMPLEMENTED: #{packet[:number]}" } 181: 182: when DEBUG 183: send(packet[:always_display] ? :fatal : :debug) { packet[:message] } 184: 185: when KEXINIT 186: algorithms.accept_kexinit(packet) 187: 188: else 189: return packet if algorithms.allow?(packet) 190: push(packet) 191: end 192: end 193: 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.
# File lib/net/ssh/transport/session.rb, line 210 210: def push(packet) 211: @queue.push(packet) 212: end
Requests a rekey operation, and blocks until the operation completes. If a rekey is already pending, this returns immediately, having no effect.
# File lib/net/ssh/transport/session.rb, line 125 125: def rekey! 126: if !algorithms.pending? 127: algorithms.rekey! 128: wait { algorithms.initialized? } 129: end 130: 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.
# File lib/net/ssh/transport/session.rb, line 135 135: def rekey_as_needed 136: return if algorithms.pending? 137: socket.if_needs_rekey? { rekey! } 138: end
Sends the given message via the packet stream, blocking until the entire message has been sent.
# File lib/net/ssh/transport/session.rb, line 216 216: def send_message(message) 217: socket.send_packet(message) 218: end
Returns a new service_request packet for the given service name, ready for sending to the server.
# File lib/net/ssh/transport/session.rb, line 118 118: def service_request(service) 119: Net::SSH::Buffer.from(:byte, SERVICE_REQUEST, :string, service) 120: end
Performs a “hard” shutdown of the connection. In general, this should never be done, but it might be necessary (in a rescue clause, for instance, when the connection needs to close but you don’t know the status of the underlying protocol’s state).
# File lib/net/ssh/transport/session.rb, line 111 111: def shutdown! 112: error { "forcing connection closed" } 113: socket.close 114: 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).
# File lib/net/ssh/transport/session.rb, line 198 198: def wait 199: loop do 200: break if block_given? && yield 201: message = poll_message(:nonblock, false) 202: push(message) if message 203: break if !block_given? 204: end 205: end