Linux::NBD::Server - server (data provider) side of a network block device
use Linux::NBD::Server;
You must subclass Linux::NBD::Server to get meaningful results. You
should overwrite req_read and/or req_write. The default
implementations just return EIO.
Create a new server. All arguments are put into a hashref which is blessed
and returned. The only required argument is socket which should be
connected to a Linux::NBD::Client.
Enters a server loop, serving requests. Only returns when the socket is closed or a disconnect message is received.
If you need non-blocking access your best bet is to use
$server-one_request>, but note that it still might block to read the
write data from the socket.
Waits for and handles a single request and returns, returning true if no further requests will arrive.
Formats (and returns as a string) a reply message. If the request was a
read request and $error is zero (or missing, both meaning no error),
you should append the read data to it.
If $data is given and $error is zero, it is appended to the string
before it is returned.
Formats and sends a reply to the server.
Writes the given data to the client socket.
Reads the specified number of bytes and returns them.
This callback is called for every read request. It should send back a reply and the requested number of bytes, e.g.:
my ($self, $handle, $ofs, $len) = @_; $self->reply($handle, 0, "\xff" x $len);
Same as req_read, but you should use recv to read $len bytes from
the socket (even if you intend to return an error, of course).
my ($self, $handle, $ofs, $len) = @_; my $data = $self->recv($len); $self->reply($handle);
Standard NBD tools add a handshake phase before starting to serve/send requests. The server-side implementation looks like this:
$size = new Math::BigInt $size;
my $size_hi = $size->copy->brsft(32)->blsft(32);
my $size_lo = $size->copy->bsub($size_hi)->numify;
$size_hi = $size_hi->brsft(32)->numify;
$s->send ("NBDMAGIC\x00\x00\x42\x02\x81\x86\x12\x53");
$s->send (pack "NN", $size_hi, $size_lo);
$s->send ("\x00" x 128);
# now serve
my $server = new ServerClass socket => $s;
$server->run;
Should support AnyEvent or something similar for non-blocking behaviour.
Marc Lehmann <schmorp@schmorp.de> http://home.schmorp.de/