| Class | Net::SSH::Transport::OutgoingPacketStream |
| In: |
lib/net/ssh/transport/packet-stream.rb
|
| Parent: | PacketStream |
Handles the compression and encryption of outgoing packets.
Create a new OutgoingPacketStream.
# File lib/net/ssh/transport/packet-stream.rb, line 70
70: def initialize( ciphers, hmacs, compressors )
71: super( ciphers, hmacs )
72: @compressor = compressors.fetch( "none" )
73: @mutex = Mutex.new
74: end
Send the given payload over the socket, after (possibly) compressing and encrypting it. The payload is converted to a string (using to_s) before being manipulated.
# File lib/net/ssh/transport/packet-stream.rb, line 85
85: def send( payload )
86: @mutex.synchronize do
87: # force the payload into a string
88: payload = @compressor.compress( payload.to_s )
89:
90: # the length of the packet, minus the padding
91: actual_length = 4 + payload.length + 1
92:
93: # compute the padding length
94: padding_length = @cipher.block_size -
95: ( actual_length % @cipher.block_size )
96: padding_length += @cipher.block_size if padding_length < 4
97:
98: # compute the packet length (sans the length field itself)
99: packet_length = payload.length + padding_length + 1
100:
101: if packet_length < 16
102: padding_length += @cipher.block_size
103: packet_length = payload.length + padding_length + 1
104: end
105:
106: padding = Array.new( padding_length ) { rand(256) }.pack("C*")
107:
108: unencrypted_data = [ packet_length, padding_length, payload,
109: padding ].pack( "NCA*A*" )
110: mac = compute_hmac( unencrypted_data )
111:
112: encrypted_data = @cipher.update( unencrypted_data ) << @cipher.final
113: message = encrypted_data + mac
114: @socket.send message, 0
115:
116: increment_sequence_number
117: end
118: end