| 1 |
=head1 NAME |
| 2 |
|
| 3 |
Linux::NBD - interface to the linux network block device. |
| 4 |
|
| 5 |
=head1 SYNOPSIS |
| 6 |
|
| 7 |
# Kernel 2.4+ is required |
| 8 |
use Linux::NBD; |
| 9 |
|
| 10 |
=head1 DESCRIPTION |
| 11 |
|
| 12 |
See L<Linux::NBD::Client> and L<Linux::NBD::Server>, or no, better idea: |
| 13 |
use the source :) |
| 14 |
|
| 15 |
NOTICE: Only the Linux 2.4 and newer nbd device is supported by this |
| 16 |
module. For older kernels you are on your own. |
| 17 |
|
| 18 |
=head1 FUNCTIONS |
| 19 |
|
| 20 |
=over 4 |
| 21 |
|
| 22 |
=cut |
| 23 |
|
| 24 |
package Linux::NBD; |
| 25 |
|
| 26 |
BEGIN { |
| 27 |
$VERSION = '1.0'; |
| 28 |
|
| 29 |
require XSLoader; |
| 30 |
XSLoader::load Linux::NBD, $VERSION; |
| 31 |
} |
| 32 |
|
| 33 |
use Socket; |
| 34 |
|
| 35 |
=item ($a, $b) = tcp_socketpair; |
| 36 |
|
| 37 |
Creates a pair of interconnected tcp sockets, which is extremely ugly, so be |
| 38 |
happy that this function exists. |
| 39 |
|
| 40 |
=cut |
| 41 |
|
| 42 |
sub tcp_socketpair { |
| 43 |
open my $fh, "</dev/urandom" or die "/dev/urandom: $!"; |
| 44 |
|
| 45 |
# this works by creating a listening socket |
| 46 |
# and connect to it, then exchange a 256 bit random cookie |
| 47 |
# to authentify. |
| 48 |
# the server is created first, and thus cannot be spoofed. i think. |
| 49 |
while () { |
| 50 |
socket my $a, PF_INET, SOCK_STREAM, 0 or die "tcp_socketpair/socket: $!"; |
| 51 |
bind $a, sockaddr_in (0, inet_aton "127.0.0.1") or die "tcp_socketpair/bind $!"; |
| 52 |
listen $a, 1 or die "tcp_socketpair/listen $!"; |
| 53 |
socket my $b, PF_INET, SOCK_STREAM, 0 or die "tcp_socketpair/socket: $!"; |
| 54 |
connect $b, getsockname $a or die "tcp_socketpair/connect: $!"; |
| 55 |
accept $a, $a or die "tcp_socketpair/accept: $!"; |
| 56 |
|
| 57 |
sysread $fh, (my $rand), 32; |
| 58 |
syswrite $b, $rand or die "tcp_socketpair/write_cookie: $!"; |
| 59 |
sysread $a, (my $cookie), 32 or die "tcp_socketpair/read_cookie: $!"; |
| 60 |
|
| 61 |
return ($a, $b) if $rand eq $cookie; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
1; |
| 66 |
|
| 67 |
=back |
| 68 |
|
| 69 |
=head1 AUTHOR |
| 70 |
|
| 71 |
Marc Lehmann <schmorp@schmorp.de> |
| 72 |
http://home.schmorp.de/ |
| 73 |
|
| 74 |
=cut |
| 75 |
|