Handles the decompression and dencryption of incoming packets.
Methods
Attributes
| [W] | buffers | A handle to the buffer factory to use when creating buffers |
| [W] | log | A handle to the logger instance to use for writing log messages |
Public Class methods
Create a new IncomingPacketStream.
[ show source ]
# File lib/net/ssh/transport/packet-stream.rb, line 141
141: def initialize( ciphers, hmacs, decompressors )
142: super( ciphers, hmacs )
143: @decompressor = decompressors.fetch( "none" )
144: @mutex = Mutex.new
145: 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.
[ show source ]
# File lib/net/ssh/transport/packet-stream.rb, line 155
155: def get
156: @mutex.synchronize do
157: # get the first block of data
158: if @log.debug?
159: @log.debug "reading #{@cipher.block_size} bytes from socket..."
160: end
161:
162: data = read( @cipher.block_size )
163:
164: # decipher it
165: reader = @buffers.reader( @cipher.update( data ) )
166:
167: # determine the packet length and how many bytes remain to be read
168: packet_length = reader.read_long
169: remaining_to_read = packet_length + 4 - @cipher.block_size
170: if @log.debug?
171: @log.debug "packet length(#{packet_length}) " +
172: "remaining(#{remaining_to_read})"
173: end
174:
175: # read the remainder of the packet and decrypt it.
176: data = read( remaining_to_read )
177:
178: # get the hmac from the tail of the packet (if one exists), and
179: # then validate it.
180: hmac = @hmac.mac_length > 0 ? read( @hmac.mac_length ) : ""
181:
182: reader.append @cipher.update( data ) unless data.empty?
183: reader.append @cipher.final
184:
185: padding_length = reader.read_byte
186:
187: payload = reader.read( packet_length - padding_length - 1 )
188: padding = reader.read( padding_length ) if padding_length > 0
189:
190: my_computed_hmac = compute_hmac( reader.content )
191: raise Net::SSH::Exception, "corrupted mac detected" if hmac != my_computed_hmac
192:
193: # decompress the payload
194: payload = @decompressor.decompress( payload )
195:
196: increment_sequence_number
197:
198: buffer = @buffers.reader( payload )
199: @log.debug "received: #{buffer.content.inspect}" if @log.debug?
200:
201: return buffer
202: end
203: end
Set the cipher, mac, and decompressor algorithms to the given values.
[ show source ]
# File lib/net/ssh/transport/packet-stream.rb, line 148
148: def set_algorithms( cipher, mac, decompressor )
149: super( cipher, mac )
150: @decompressor = decompressor
151: end