ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Linux-NBD/lib/Linux/NBD.pm
Revision: 1.1
Committed: Thu May 8 23:43:43 2003 UTC (23 years, 5 months ago) by root
Branch: MAIN
Log Message:
*** empty log message ***

File Contents

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