2012-06-28 11:34:36 +01:00
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
require 'socket'
|
|
|
|
require 'timeout'
|
|
|
|
|
|
|
|
require 'flexnbd/constants'
|
|
|
|
|
|
|
|
module FlexNBD
|
2012-07-02 15:04:45 +01:00
|
|
|
class FakeDest
|
2012-06-28 11:34:36 +01:00
|
|
|
|
2012-07-02 15:04:45 +01:00
|
|
|
class Client
|
|
|
|
def initialize( sock )
|
|
|
|
@sock = sock
|
|
|
|
end
|
2012-06-28 11:34:36 +01:00
|
|
|
|
|
|
|
|
2012-07-02 15:04:45 +01:00
|
|
|
def write_hello( opts = {} )
|
2012-07-03 14:23:20 +01:00
|
|
|
@sock.write( "NBDMAGIC" )
|
2012-07-02 15:04:45 +01:00
|
|
|
|
|
|
|
if opts[:magic] == :wrong
|
2012-07-03 14:23:20 +01:00
|
|
|
write_rand( @sock, 8 )
|
2012-07-02 15:04:45 +01:00
|
|
|
else
|
2012-07-03 14:23:20 +01:00
|
|
|
@sock.write( "\x00\x00\x42\x02\x81\x86\x12\x53" )
|
2012-06-28 11:34:36 +01:00
|
|
|
end
|
2012-07-02 15:04:45 +01:00
|
|
|
|
|
|
|
if opts[:size] == :wrong
|
2012-07-03 14:23:20 +01:00
|
|
|
write_rand( @sock, 8 )
|
2012-07-02 15:04:45 +01:00
|
|
|
else
|
2012-07-03 14:23:20 +01:00
|
|
|
@sock.write( "\x00\x00\x00\x00\x00\x00\x10\x00" )
|
2012-07-02 15:04:45 +01:00
|
|
|
end
|
|
|
|
|
2012-07-03 14:23:20 +01:00
|
|
|
@sock.write( "\x00" * 128 )
|
2012-06-28 11:34:36 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2012-07-03 14:23:20 +01:00
|
|
|
def write_rand( sock, len )
|
|
|
|
len.times do sock.write( rand(256).chr ) end
|
|
|
|
end
|
|
|
|
|
2012-06-28 11:34:36 +01:00
|
|
|
|
2012-07-03 14:23:20 +01:00
|
|
|
def read_request()
|
|
|
|
req = @sock.read(28)
|
2012-06-28 11:34:36 +01:00
|
|
|
|
2012-07-02 15:04:45 +01:00
|
|
|
magic_s = req[0 ... 4 ]
|
|
|
|
type_s = req[4 ... 8 ]
|
|
|
|
handle_s = req[8 ... 16]
|
|
|
|
from_s = req[16 ... 24]
|
|
|
|
len_s = req[24 ... 28]
|
|
|
|
|
|
|
|
{
|
|
|
|
:magic => magic_s,
|
|
|
|
:type => type_s.unpack("N").first,
|
|
|
|
:handle => handle_s,
|
2012-07-03 14:23:20 +01:00
|
|
|
:from => self.class.parse_be64( from_s ),
|
2012-07-02 15:04:45 +01:00
|
|
|
:len => len_s.unpack( "N").first
|
|
|
|
}
|
2012-06-28 11:34:36 +01:00
|
|
|
end
|
|
|
|
|
2012-07-03 17:01:39 +01:00
|
|
|
REPLY_MAGIC="\x67\x44\x66\x98"
|
2012-07-02 15:04:45 +01:00
|
|
|
|
2012-07-03 14:23:20 +01:00
|
|
|
def write_error( handle )
|
2012-07-03 17:01:39 +01:00
|
|
|
write_reply( handle, 1 )
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def write_reply( handle, err=0, opts={} )
|
|
|
|
if opts[:magic] == :wrong
|
|
|
|
write_rand( @sock, 4 )
|
|
|
|
else
|
|
|
|
@sock.write( REPLY_MAGIC )
|
|
|
|
end
|
|
|
|
|
|
|
|
@sock.write( [err].pack("N") )
|
2012-07-03 14:23:20 +01:00
|
|
|
@sock.write( handle )
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def close
|
|
|
|
@sock.close
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2012-07-03 17:01:39 +01:00
|
|
|
def read_data( len )
|
|
|
|
@sock.read( len )
|
|
|
|
end
|
|
|
|
|
2012-07-03 14:23:20 +01:00
|
|
|
|
2012-07-02 15:04:45 +01:00
|
|
|
def self.parse_be64(str)
|
|
|
|
raise "String is the wrong length: 8 bytes expected (#{str.length} received)" unless
|
|
|
|
str.length == 8
|
|
|
|
|
|
|
|
top, bottom = str.unpack("NN")
|
|
|
|
(top << 32) + bottom
|
2012-06-28 11:34:36 +01:00
|
|
|
end
|
|
|
|
|
2012-07-02 15:04:45 +01:00
|
|
|
|
|
|
|
end # class Client
|
|
|
|
|
|
|
|
|
|
|
|
def initialize( addr, port )
|
2012-07-03 14:23:20 +01:00
|
|
|
@sock = TCPServer.new( addr, port )
|
2012-07-02 15:04:45 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2012-07-03 14:23:20 +01:00
|
|
|
def accept( err_msg = "Timed out waiting for a connection", timeout = 2)
|
2012-07-02 15:04:45 +01:00
|
|
|
client_sock = nil
|
|
|
|
|
|
|
|
begin
|
|
|
|
Timeout.timeout(timeout) do
|
2012-07-03 14:23:20 +01:00
|
|
|
client_sock = @sock.accept
|
2012-07-02 15:04:45 +01:00
|
|
|
end
|
|
|
|
rescue Timeout::Error
|
|
|
|
$stderr.puts err_msg
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
|
|
|
client_sock
|
2012-07-03 14:23:20 +01:00
|
|
|
|
|
|
|
Client.new( client_sock )
|
2012-06-28 14:45:53 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2012-07-03 14:23:20 +01:00
|
|
|
def close
|
|
|
|
@sock.close
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-28 14:45:53 +01:00
|
|
|
end # module FakeDest
|
2012-06-28 11:34:36 +01:00
|
|
|
end # module FlexNBD
|