poll_next_packet (Net::SSH::Transport::PacketStream)

         # File lib/net/ssh/transport/packet_stream.rb, line 185
    185:       def poll_next_packet
    186:         if @packet.nil?
    187:           minimum = server.block_size < 4 ? 4 : server.block_size
    188:           return nil if available < minimum
    189:           data = read_available(minimum)
    190:
    191:           # decipher it
    192:           @packet = Net::SSH::Buffer.new(server.update_cipher(data))
    193:           @packet_length = @packet.read_long
    194:         end
    195:
    196:         need = @packet_length + 4 - server.block_size
    197:         raise Net::SSH::Exception, "padding error, need #{need} block #{server.block_size}" if need % server.block_size != 0
    198:
    199:         return nil if available < need + server.hmac.mac_length
    200:
    201:         if need > 0
    202:           # read the remainder of the packet and decrypt it.
    203:           data = read_available(need)
    204:           @packet.append(server.update_cipher(data))
    205:         end
    206:
    207:         # get the hmac from the tail of the packet (if one exists), and
    208:         # then validate it.
    209:         real_hmac = read_available(server.hmac.mac_length) || ""
    210:
    211:         @packet.append(server.final_cipher)
    212:         padding_length = @packet.read_byte
    213:
    214:         payload = @packet.read(@packet_length - padding_length - 1)
    215:         padding = @packet.read(padding_length) if padding_length > 0
    216:
    217:         my_computed_hmac = server.hmac.digest([server.sequence_number, @packet.content].pack("NA*"))
    218:         raise Net::SSH::Exception, "corrupted mac detected" if real_hmac != my_computed_hmac
    219:
    220:         # try to decompress the payload, in case compression is active
    221:         payload = server.decompress(payload)
    222:
    223:         debug { "received packet nr #{server.sequence_number} type #{payload.getbyte(0)} len #{@packet_length}" }
    224:
    225:         server.increment(@packet_length)
    226:         @packet = nil
    227:
    228:         return Packet.new(payload)
    229:       end