lib/em-ssh/packet-stream.rb
# Tries to read the next packet. If there is insufficient data to read
# an entire packet, this returns immediately, otherwise the packet is
# read, post-processed according to the cipher, hmac, and compression
# algorithms specified in the server state object, and returned as a
# new Packet object.
# Copyright (c) 2008 Jamis Buck
Tries to read the next packet. If there is insufficient data to read
an entire packet, this returns immediately, otherwise the packet is
read, post-processed according to the cipher, hmac, and compression
algorithms specified in the server state object, and returned as a
new Packet object.
Copyright (c) 2008 Jamis Buck
Tries to read the next packet . If there is insufficient data to read an entire packet this returns immediately otherwise the packet is read post - processed according to the cipher hmac and compression algorithms specified in the server state object and returned as a new Packet object . Copyright ( c ) 2008 Jamis Buck
def poll_next_packet
        if @packet.nil?
          minimum = server.block_size < 4 ? 4 : server.block_size
          return nil if available < minimum
          data = read_available(minimum)
          @packet = Net::SSH::Buffer.new(server.update_cipher(data))
          @packet_length = @packet.read_long
        end
        need = @packet_length + 4 - server.block_size
        if need % server.block_size != 0
          @connection.fire(:error, SshError.new("padding error, need #{need} block #{server.block_size}"))
        end
        return nil if available < need + server.hmac.mac_length
        if need > 0
          data = read_available(need)
          @packet.append(server.update_cipher(data))
        end
        real_hmac = read_available(server.hmac.mac_length) || ""
        @packet.append(server.final_cipher)
        padding_length = @packet.read_byte
        payload = @packet.read(@packet_length - padding_length - 1)
        padding = @packet.read(padding_length) if padding_length > 0
        my_computed_hmac = server.hmac.digest([server.sequence_number, @packet.content].pack("NA*"))
        if real_hmac != my_computed_hmac
          @connection.fire(:error, Net::SSH::Exception.new("corrupted mac detected"))
          return
        end
        payload = server.decompress(payload)
        log.debug("received packet nr #{server.sequence_number} type #{payload.getbyte(0)} len #{@packet_length}")
        server.increment(@packet_length)
        @packet = nil
        return Net::SSH::Packet.new(payload)
      end
def poll_next_packet if @packet . nil? minimum = server . block_size < 4 ? 4 : server . block_size return nil if available < minimum data = read_available ( minimum ) @packet = Net :: SSH :: Buffer . new ( server . update_cipher ( data ) ) @packet_length = @packet . read_long end need = @packet_length + 4 - server . block_size if need % server . block_size != 0 @connection . fire ( :error , SshError . new ( "padding error, need #{need} block #{server.block_size}" ) ) end return nil if available < need + server . hmac . mac_length if need > 0 data = read_available ( need ) @packet . append ( server . update_cipher ( data ) ) end real_hmac = read_available ( server . hmac . mac_length ) || "" @packet . append ( server . final_cipher ) padding_length = @packet . read_byte payload = @packet . read ( @packet_length - padding_length - 1 ) padding = @packet . read ( padding_length ) if padding_length > 0 my_computed_hmac = server . hmac . digest ( [ server . sequence_number , @packet . content ] . pack ( "NA*" ) ) if real_hmac != my_computed_hmac @connection . fire ( :error , Net :: SSH :: Exception . new ( "corrupted mac detected" ) ) return end payload = server . decompress ( payload ) log . debug ( "received packet nr #{server.sequence_number} type #{payload.getbyte(0)} len #{@packet_length}" ) server . increment ( @packet_length ) @packet = nil return Net :: SSH :: Packet . new ( payload ) end
Tries to read the next packet.
Tries to read the next packet .
# decipher it # read the remainder of the packet and decrypt it. # get the hmac from the tail of the packet (if one exists), and # then validate it. # try to decompress the payload, in case compression is active