Class Net::SSH::Transport::IncomingPacketStream
In: lib/net/ssh/transport/packet-stream.rb
Parent: PacketStream

Handles the decompression and dencryption of incoming packets.

Methods

get   new   set_algorithms  

Attributes

buffers  [W]  A handle to the buffer factory to use when creating buffers
log  [W]  A handle to the logger instance to use for writing log messages

Public Class methods

Create a new IncomingPacketStream.

[Source]

     # File lib/net/ssh/transport/packet-stream.rb, line 132
132:         def initialize( ciphers, hmacs, decompressors )
133:           super( ciphers, hmacs )
134:           @decompressor = decompressors.fetch( "none" )
135:           @mutex = Mutex.new
136:         end

Public Instance methods

Retrieve the next packet from the string, after (possibly) decrypting and decompressing it. The packet is returned as a reader buffer.

[Source]

     # File lib/net/ssh/transport/packet-stream.rb, line 146
146:         def get
147:           @mutex.synchronize do
148:             # get the first block of data
149:             if @log.debug?
150:               @log.debug "reading #{@cipher.block_size} bytes from socket..."
151:             end
152: 
153:             data = read( @cipher.block_size )
154: 
155:             # decipher it
156:             reader = @buffers.reader( @cipher.update( data ) )
157: 
158:             # determine the packet length and how many bytes remain to be read
159:             packet_length = reader.read_long
160:             remaining_to_read = packet_length + 4 - @cipher.block_size
161:             if @log.debug?
162:               @log.debug "packet length(#{packet_length}) " +
163:                 "remaining(#{remaining_to_read})"
164:             end
165: 
166:             # read the remainder of the packet and decrypt it.
167:             data = read( remaining_to_read )
168: 
169:             # get the hmac from the tail of the packet (if one exists), and
170:             # then validate it.
171:             hmac = @hmac.mac_length > 0 ? read( @hmac.mac_length ) : ""
172: 
173:             reader.append @cipher.update( data )
174:             reader.append @cipher.final
175: 
176:             padding_length = reader.read_byte
177: 
178:             payload = reader.read( packet_length - padding_length - 1 )
179:             padding = reader.read( padding_length ) if padding_length > 0
180: 
181:             my_computed_hmac = compute_hmac( reader.content )
182:             raise Net::SSH::Exception, "corrupted mac detected" if hmac != my_computed_hmac
183: 
184:             # decompress the payload
185:             payload = @decompressor.decompress( payload )
186: 
187:             increment_sequence_number
188: 
189:             buffer = @buffers.reader( payload )
190:             @log.debug "received: #{buffer.content.inspect}" if @log.debug?
191: 
192:             return buffer
193:           end
194:         end

Set the cipher, mac, and decompressor algorithms to the given values.

[Source]

     # File lib/net/ssh/transport/packet-stream.rb, line 139
139:         def set_algorithms( cipher, mac, decompressor )
140:           super( cipher, mac )
141:           @decompressor = decompressor
142:         end

[Validate]