Class: Net::SSH::Packet

Inherits:
Buffer
  • Object
show all
Includes:
Authentication::Constants, Connection::Constants, Transport::Constants
Defined in:
lib/net/ssh/packet.rb

Overview

A specialization of Buffer that knows the format of certain common packet types. It auto-parses those packet types, and allows them to be accessed via the #[] accessor.

data = some_channel_request_packet
packet = Net::SSH::Packet.new(data)

p packet.type #-> 98 (CHANNEL_REQUEST)
p packet[:request]
p packet[:want_reply]

This is used exclusively internally by Net::SSH, and unless you’re doing protocol-level manipulation or are extending Net::SSH in some way, you’ll never need to use this class directly.

Constant Summary collapse

@@types =
{}

Constants included from Transport::Constants

Transport::Constants::DEBUG, Transport::Constants::DISCONNECT, Transport::Constants::IGNORE, Transport::Constants::KEXDH_INIT, Transport::Constants::KEXDH_REPLY, Transport::Constants::KEXINIT, Transport::Constants::NEWKEYS, Transport::Constants::SERVICE_ACCEPT, Transport::Constants::SERVICE_REQUEST, Transport::Constants::UNIMPLEMENTED

Constants included from Authentication::Constants

Authentication::Constants::USERAUTH_BANNER, Authentication::Constants::USERAUTH_FAILURE, Authentication::Constants::USERAUTH_METHOD_RANGE, Authentication::Constants::USERAUTH_PASSWD_CHANGEREQ, Authentication::Constants::USERAUTH_PK_OK, Authentication::Constants::USERAUTH_REQUEST, Authentication::Constants::USERAUTH_SUCCESS

Constants included from Connection::Constants

Connection::Constants::CHANNEL_CLOSE, Connection::Constants::CHANNEL_DATA, Connection::Constants::CHANNEL_EOF, Connection::Constants::CHANNEL_EXTENDED_DATA, Connection::Constants::CHANNEL_FAILURE, Connection::Constants::CHANNEL_OPEN, Connection::Constants::CHANNEL_OPEN_CONFIRMATION, Connection::Constants::CHANNEL_OPEN_FAILURE, Connection::Constants::CHANNEL_REQUEST, Connection::Constants::CHANNEL_SUCCESS, Connection::Constants::CHANNEL_WINDOW_ADJUST, Connection::Constants::GLOBAL_REQUEST, Connection::Constants::REQUEST_FAILURE, Connection::Constants::REQUEST_SUCCESS

Instance Attribute Summary collapse

Attributes inherited from Buffer

#content, #position

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Buffer

#==, #append, #available, #clear!, #consume!, #empty?, #eof?, from, #length, #read, #read!, #read_bignum, #read_bool, #read_buffer, #read_byte, #read_int64, #read_key, #read_keyblob, #read_long, #read_string, #read_to, #remainder_as_buffer, #reset!, #to_s, #write, #write_bignum, #write_bool, #write_byte, #write_int64, #write_key, #write_long, #write_string

Constructor Details

#initialize(payload) ⇒ Packet

Create a new packet from the given payload. This will automatically parse the packet if it is one that has been previously registered with Packet.register; otherwise, the packet will need to be manually parsed using the methods provided in the Net::SSH::Buffer superclass.



73
74
75
76
77
78
# File 'lib/net/ssh/packet.rb', line 73

def initialize(payload)
  @named_elements = {}
  super
  @type = read_byte
  instantiate!
end

Instance Attribute Details

#typeObject (readonly)

The (integer) type of this packet.



67
68
69
# File 'lib/net/ssh/packet.rb', line 67

def type
  @type
end

Class Method Details

.register(type, *pairs) ⇒ Object

Register a new packet type that should be recognized and auto-parsed by Net::SSH::Packet. Note that any packet type that is not preregistered will not be autoparsed.

The pairs parameter must be either empty, or an array of two-element tuples, where the first element of each tuple is the name of the field, and the second is the type.

register DISCONNECT, [:reason_code, :long], [:description, :string], [:language, :string]


34
35
36
# File 'lib/net/ssh/packet.rb', line 34

def self.register(type, *pairs)
  @@types[type] = pairs
end

Instance Method Details

#[](name) ⇒ Object

Access one of the auto-parsed fields by name. Raises an error if no element by the given name exists.

Raises:

  • (ArgumentError)


82
83
84
85
86
# File 'lib/net/ssh/packet.rb', line 82

def [](name)
  name = name.to_sym
  raise ArgumentError, "no such element #{name}" unless @named_elements.key?(name)
  @named_elements[name]
end