--- cvsroot/Net-FCP/FCP.pm 2003/09/09 06:13:18 1.9 +++ cvsroot/Net-FCP/FCP.pm 2003/12/10 02:36:37 1.26 @@ -37,6 +37,33 @@ You should specify the event module to use only in the main program. +If no event model has been specified, FCP tries to autodetect it on first +use (e.g. first transaction), in this order: Coro, Event, Glib, Tk. + +=head2 FREENET BASICS + +Ok, this section will not explain any freenet basics to you, just some +problems I found that you might want to avoid: + +=over 4 + +=item freenet URIs are _NOT_ URIs + +Whenever a "uri" is required by the protocol, freenet expects a kind of +URI prefixed with the "freenet:" scheme, e.g. "freenet:CHK...". However, +these are not URIs, as freeent fails to parse them correctly, that is, you +must unescape an escaped characters ("%2c" => ",") yourself. Maybe in the +future this library will do it for you, so watch out for this incompatible +change. + +=item Numbers are in HEX + +Virtually every number in the FCP protocol is in hex. Be sure to use +C on all such numbers, as the module (currently) does nothing to +convert these for you. + +=back + =head2 THE Net::FCP CLASS =over 4 @@ -46,12 +73,12 @@ package Net::FCP; use Carp; -use IO::Socket::INET; -$VERSION = 0.04; +$VERSION = 0.6; + +no warnings; our $EVENT = Net::FCP::Event::Auto::; -$EVENT = Net::FCP::Event::Event::;#d# sub import { shift; @@ -59,9 +86,10 @@ for (@_) { if (/^event=(\w+)$/) { $EVENT = "Net::FCP::Event::$1"; + eval "require $EVENT"; } } - eval "require $EVENT"; + die $@ if $@; } sub touc($) { @@ -73,38 +101,46 @@ sub tolc($) { local $_ = shift; + 1 while s/(SVK|CHK|URI)/\L$1\_/; s/(?<=[a-z])(?=[A-Z])/_/g; lc $_; } +# the opposite of hex +sub xeh($) { + sprintf "%x", $_[0]; +} + =item $meta = Net::FCP::parse_metadata $string Parse a metadata string and return it. -The metadata will be a hashref with key C (containing -the mandatory version header entries). +The metadata will be a hashref with key C (containing the +mandatory version header entries) and key C containing the original +metadata string. All other headers are represented by arrayrefs (they can be repeated). -Since this is confusing, here is a rather verbose example of a parsed -manifest: +Since this description is confusing, here is a rather verbose example of a +parsed manifest: ( + raw => "Version...", version => { revision => 1 }, document => [ { - "info.format" => "image/jpeg", + info => { format" => "image/jpeg" }, name => "background.jpg", - "redirect.target" => "freenet:CHK\@ZcagI,ra726bSw" + redirect => { target => "freenet:CHK\@ZcagI,ra726bSw" }, }, { - "info.format" => "text/html", + info => { format" => "text/html" }, name => ".next", - "redirect.target" => "freenet:SSK\@ilUPAgM/TFEE/3" + redirect => { target => "freenet:SSK\@ilUPAgM/TFEE/3" }, }, { - "info.format" => "text/html", - "redirect.target" => "freenet:CHK\@8M8Po8ucwI,8xA" + info => { format" => "text/html" }, + redirect => { target => "freenet:CHK\@8M8Po8ucwI,8xA" }, } ] ) @@ -112,25 +148,32 @@ =cut sub parse_metadata { - my $meta; - my $data = shift; + my $meta = { raw => $data }; + if ($data =~ /^Version\015?\012/gc) { my $hdr = $meta->{version} = {}; for (;;) { while ($data =~ /\G([^=\015\012]+)=([^\015\012]*)\015?\012/gc) { my ($k, $v) = ($1, $2); - $hdr->{tolc $k} = $v; + my @p = split /\./, tolc $k, 3; + + $hdr->{$p[0]} = $v if @p == 1; # lamest code I ever wrote + $hdr->{$p[0]}{$p[1]} = $v if @p == 2; + $hdr->{$p[0]}{$p[1]}{$p[2]} = $v if @p == 3; + die "FATAL: 4+ dot metadata" if @p >= 4; } if ($data =~ /\GEndPart\015?\012/gc) { - } elsif ($data =~ /\GEnd\015?\012/gc) { + # nop + } elsif ($data =~ /\GEnd(\015?\012|$)/gc) { last; } elsif ($data =~ /\G([A-Za-z0-9.\-]+)\015?\012/gcs) { push @{$meta->{tolc $1}}, $hdr = {}; } elsif ($data =~ /\G(.*)/gcs) { - die "metadata format error ($1)"; + print STDERR "metadata format error ($1), please report this string: <<$data>>"; + die "metadata format error"; } } } @@ -146,9 +189,15 @@ 127.0.0.1:8481, or the environment variables C and C). Connections are virtual because no persistent physical connection is -established. However, the existance of the node is checked by executing a +established. + +=begin comment + +However, the existance of the node is checked by executing a C transaction. +=end + =cut sub new { @@ -156,23 +205,45 @@ my $self = bless { @_ }, $class; $self->{host} ||= $ENV{FREDHOST} || "127.0.0.1"; - $self->{port} ||= $ENV{FREDPORt} || 8481; + $self->{port} ||= $ENV{FREDPORT} || 8481; - $self->{nodehello} = $self->client_hello - or croak "unable to get nodehello from node\n"; + #$self->{nodehello} = $self->client_hello + # or croak "unable to get nodehello from node\n"; $self; } sub progress { my ($self, $txn, $type, $attr) = @_; - warn "progress<$txn,$type," . (join ":", %$attr) . ">\n"; + #warn "progress<$txn,$type," . (join ":", %$attr) . ">\n"; } =item $txn = $fcp->txn(type => attr => val,...) The low-level interface to transactions. Don't use it. +Here are some examples of using transactions: + +The blocking case, no (visible) transactions involved: + + my $nodehello = $fcp->client_hello; + +A transaction used in a blocking fashion: + + my $txn = $fcp->txn_client_hello; + ... + my $nodehello = $txn->result; + +Or shorter: + + my $nodehello = $fcp->txn_client_hello->result; + +Setting callbacks: + + $fcp->txn_client_hello->cb( + sub { my $nodehello => $_[0]->result } + ); + =cut sub txn { @@ -185,11 +256,13 @@ $txn; } -sub _txn($&) { +{ # transactions + +my $txn = sub { my ($name, $sub) = @_; - *{"$name\_txn"} = $sub; + *{"txn_$name"} = $sub; *{$name} = sub { $sub->(@_)->result }; -} +}; =item $txn = $fcp->txn_client_hello @@ -205,11 +278,11 @@ =cut -_txn client_hello => sub { +$txn->(client_hello => sub { my ($self) = @_; $self->txn ("client_hello"); -}; +}); =item $txn = $fcp->txn_client_info @@ -243,25 +316,29 @@ =cut -_txn client_info => sub { +$txn->(client_info => sub { my ($self) = @_; $self->txn ("client_info"); -}; +}); -=item $txn = $fcp->txn_generate_chk ($metadata, $data) +=item $txn = $fcp->txn_generate_chk ($metadata, $data[, $cipher]) -=item $uri = $fcp->generate_chk ($metadata, $data) +=item $uri = $fcp->generate_chk ($metadata, $data[, $cipher]) -Creates a new CHK, given the metadata and data. UNTESTED. +Calculcates a CHK, given the metadata and data. C<$cipher> is either +C or C, with the latter being the default. =cut -_txn generate_chk => sub { - my ($self, $metadata, $data) = @_; +$txn->(generate_chk => sub { + my ($self, $metadata, $data, $cipher) = @_; - $self->txn (generate_chk => data => "$data$metadata", metadata_length => length $metadata); -}; + $self->txn (generate_chk => + data => "$metadata$data", + metadata_length => xeh length $metadata, + cipher => $cipher || "Twofish"); +}); =item $txn = $fcp->txn_generate_svk_pair @@ -276,19 +353,19 @@ =cut -_txn generate_svk_pair => sub { +$txn->(generate_svk_pair => sub { my ($self) = @_; $self->txn ("generate_svk_pair"); -}; +}); =item $txn = $fcp->txn_insert_private_key ($private) -=item $uri = $fcp->insert_private_key ($private) +=item $public = $fcp->insert_private_key ($private) Inserts a private key. $private can be either an insert URI (must start -with freenet:SSK@) or a raw private key (i.e. the private value you get back -from C). +with C) or a raw private key (i.e. the private value you get +back from C). Returns the public key. @@ -296,11 +373,11 @@ =cut -_txn insert_private_key => sub { +$txn->(insert_private_key => sub { my ($self, $privkey) = @_; $self->txn (invert_private_key => private => $privkey); -}; +}); =item $txn = $fcp->txn_get_size ($uri) @@ -313,11 +390,11 @@ =cut -_txn get_size => sub { +$txn->(get_size => sub { my ($self, $uri) = @_; $self->txn (get_size => URI => $uri); -}; +}); =item $txn = $fcp->txn_client_get ($uri [, $htl = 15 [, $removelocal = 0]]) @@ -337,21 +414,49 @@ =cut -_txn client_get => sub { +$txn->(client_get => sub { my ($self, $uri, $htl, $removelocal) = @_; - $self->txn (client_get => URI => $uri, hops_to_live => ($htl || 15), remove_local => $removelocal*1); -}; + $uri =~ s/^freenet://; + $uri = "freenet:$uri"; + + $self->txn (client_get => URI => $uri, hops_to_live => xeh (defined $htl ? $htl : 15), + remove_local_key => $removelocal ? "true" : "false"); +}); + +=item $txn = $fcp->txn_client_put ($uri, $metadata, $data, $htl, $removelocal) + +=item my $uri = $fcp->client_put ($uri, $metadata, $data, $htl, $removelocal); + +Insert a new key. If the client is inserting a CHK, the URI may be +abbreviated as just CHK@. In this case, the node will calculate the +CHK. + +C<$meta> can be a reference or a string (ONLY THE STRING CASE IS IMPLEMENTED!). + +THIS INTERFACE IS UNTESTED AND SUBJECT TO CHANGE. + +=cut -=item MISSING: ClientPut +$txn->(client_put => sub { + my ($self, $uri, $meta, $data, $htl, $removelocal) = @_; + + $self->txn (client_put => URI => $uri, xeh (defined $htl ? $htl : 15), + remove_local_key => $removelocal ? "true" : "false", + data => "$meta$data", metadata_length => xeh length $meta); +}); + +} # transactions + +=item MISSING: (ClientPut), InsertKey =back =head2 THE Net::FCP::Txn CLASS -All requests (or transactions) are executed in a asynchroneous way (LIE: -uploads are blocking). For each request, a C object is -created (worse: a tcp connection is created, too). +All requests (or transactions) are executed in a asynchronous way. For +each request, a C object is created (worse: a tcp +connection is created, too). For each request there is actually a different subclass (and it's possible to subclass these, although of course not documented). @@ -364,6 +469,9 @@ package Net::FCP::Txn; +use Fcntl; +use Socket; + =item new arg => val,... Creates a new C object. Not normally used. @@ -374,6 +482,10 @@ my $class = shift; my $self = bless { @_ }, $class; + $self->{signal} = $EVENT->new_signal; + + $self->{fcp}{txn}{$self} = $self; + my $attr = ""; my $data = delete $self->{attr}{data}; @@ -382,66 +494,123 @@ } if (defined $data) { - $attr .= "DataLength=" . (length $data) . "\012"; + $attr .= sprintf "DataLength=%x\012", length $data; $data = "Data\012$data"; } else { $data = "EndMessage\012"; } - my $fh = new IO::Socket::INET - PeerHost => $self->{fcp}{host}, - PeerPort => $self->{fcp}{port} - or Carp::croak "FCP::txn: unable to connect to $self->{fcp}{host}:$self->{fcp}{port}: $!\n"; - + socket my $fh, PF_INET, SOCK_STREAM, 0 + or Carp::croak "unable to create new tcp socket: $!"; binmode $fh, ":raw"; + fcntl $fh, F_SETFL, O_NONBLOCK; + connect $fh, (sockaddr_in $self->{fcp}{port}, inet_aton $self->{fcp}{host}) + and !$!{EWOULDBLOCK} + and !$!{EINPROGRESS} + and Carp::croak "FCP::txn: unable to connect to $self->{fcp}{host}:$self->{fcp}{port}: $!\n"; + + $self->{sbuf} = + "\x00\x00\x00\x02" + . (Net::FCP::touc $self->{type}) + . "\012$attr$data"; - if (0) { - print - Net::FCP::touc $self->{type}, "\012", - $attr, - $data, "\012"; - } - - print $fh - "\x00\x00", "\x00\x02", # SESSID, PRESID - Net::FCP::touc $self->{type}, "\012", - $attr, - $data; - - #$fh->shutdown (1); # freenet buggy?, well, it's java... + #shutdown $fh, 1; # freenet buggy?, well, it's java... $self->{fh} = $fh; - $EVENT->reg_r_cb ($self); + $self->{w} = $EVENT->new_from_fh ($fh)->cb(sub { $self->fh_ready_w })->poll(0, 1, 1); $self; } -=item $userdata = $txn->userdata ([$userdata]) +=item $txn = $txn->cb ($coderef) + +Sets a callback to be called when the request is finished. The coderef +will be called with the txn as it's sole argument, so it has to call +C itself. + +Returns the txn object, useful for chaining. -Get and/or set user-specific data. This is useful in progress callbacks. +Example: + + $fcp->txn_client_get ("freenet:CHK....") + ->userdata ("ehrm") + ->cb(sub { + my $data = shift->result; + }); =cut -sub userdata($;$) { +sub cb($$) { + my ($self, $cb) = @_; + $self->{cb} = $cb; + $self; +} + +=item $txn = $txn->userdata ([$userdata]) + +Set user-specific data. This is useful in progress callbacks. The data can be accessed +using C<< $txn->{userdata} >>. + +Returns the txn object, useful for chaining. + +=cut + +sub userdata($$) { my ($self, $data) = @_; - $self->{userdata} = $data if @_ >= 2; - $self->{userdata}; + $self->{userdata} = $data; + $self; } -sub fh_ready { +=item $txn->cancel (%attr) + +Cancels the operation with a C exception anf the given attributes +(consider at least giving the attribute C). + +UNTESTED. + +=cut + +sub cancel { + my ($self, %attr) = @_; + $self->throw (Net::FCP::Exception->new (cancel => { %attr })); + $self->set_result; + $self->eof; +} + +sub fh_ready_w { + my ($self) = @_; + + my $len = syswrite $self->{fh}, $self->{sbuf}; + + if ($len > 0) { + substr $self->{sbuf}, 0, $len, ""; + unless (length $self->{sbuf}) { + fcntl $self->{fh}, F_SETFL, 0; + $self->{w}->cb(sub { $self->fh_ready_r })->poll (1, 0, 1); + } + } elsif (defined $len) { + $self->throw (Net::FCP::Exception->new (network_error => { reason => "unexpected end of file while writing" })); + } else { + $self->throw (Net::FCP::Exception->new (network_error => { reason => "$!" })); + } +} + +sub fh_ready_r { my ($self) = @_; if (sysread $self->{fh}, $self->{buf}, 65536, length $self->{buf}) { for (;;) { if ($self->{datalen}) { + #warn "expecting new datachunk $self->{datalen}, got ".(length $self->{buf})."\n";#d# if (length $self->{buf} >= $self->{datalen}) { - $self->rcv_data (substr $self->{buf}, 0, $self->{datalen}, ""); + $self->rcv_data (substr $self->{buf}, 0, delete $self->{datalen}, ""); } else { last; } } elsif ($self->{buf} =~ s/^DataChunk\015?\012Length=([0-9a-fA-F]+)\015?\012Data\015?\012//) { $self->{datalen} = hex $1; + #warn "expecting new datachunk $self->{datalen}\n";#d# } elsif ($self->{buf} =~ s/^([a-zA-Z]+)\015?\012(?:(.+?)\015?\012)?EndMessage\015?\012//s) { $self->rcv ($1, { map { my ($a, $b) = split /=/, $_, 2; ((Net::FCP::tolc $a), $b) } @@ -452,20 +621,10 @@ } } } else { - $EVENT->unreg_r_cb ($self); - delete $self->{fh}; $self->eof; } } -sub rcv_data { - my ($self, $chunk) = @_; - - $self->{data} .= $chunk; - - $self->progress ("data", { chunk => length $chunk, total => length $self->{data}, end => $self->{datalength} }); -} - sub rcv { my ($self, $type, $attr) = @_; @@ -480,22 +639,46 @@ } } +# used as a default exception thrower +sub rcv_throw_exception { + my ($self, $attr, $type) = @_; + $self->throw (Net::FCP::Exception->new ($type, $attr)); +} + +*rcv_failed = \&Net::FCP::Txn::rcv_throw_exception; +*rcv_format_error = \&Net::FCP::Txn::rcv_throw_exception; + sub throw { my ($self, $exc) = @_; $self->{exception} = $exc; - $self->set_result (1); + $self->set_result; + $self->eof; # must be last to avoid loops } sub set_result { my ($self, $result) = @_; - $self->{result} = $result unless exists $self->{result}; + unless (exists $self->{result}) { + $self->{result} = $result; + $self->{cb}->($self) if exists $self->{cb}; + $self->{signal}->send; + } } sub eof { my ($self) = @_; - $self->set_result; + + delete $self->{w}; + delete $self->{fh}; + + delete $self->{fcp}{txn}{$self}; + + unless (exists $self->{result}) { + $self->throw (Net::FCP::Exception->new (short_data => { + reason => "unexpected eof or internal node error", + })); + } } sub progress { @@ -508,25 +691,23 @@ Waits until a result is available and then returns it. This waiting is (depending on your event model) not very efficient, as it -is done outside the "mainloop". +is done outside the "mainloop". The biggest problem, however, is that it's +blocking one thread of execution. Try to use the callback mechanism, if +possible, and call result from within the callback (or after is has been +run), as then no waiting is necessary. =cut sub result { my ($self) = @_; - $EVENT->wait_event while !exists $self->{result}; + $self->{signal}->wait while !exists $self->{result}; die $self->{exception} if $self->{exception}; return $self->{result}; } -sub DESTROY { - $EVENT->unreg_r_cb ($_[0]); - #$EVENT->unreg_w_cb ($_[0]); -} - package Net::FCP::Txn::ClientHello; use base Net::FCP::Txn; @@ -554,7 +735,7 @@ sub rcv_success { my ($self, $attr) = @_; - $self->set_result ($attr); + $self->set_result ($attr->{uri}); } package Net::FCP::Txn::GenerateSVKPair; @@ -563,17 +744,15 @@ sub rcv_success { my ($self, $attr) = @_; - $self->set_result ([$attr->{PublicKey}, $attr->{PrivateKey}]); } -package Net::FCP::Txn::InvertPrivateKey; +package Net::FCP::Txn::InsertPrivateKey; use base Net::FCP::Txn; sub rcv_success { my ($self, $attr) = @_; - $self->set_result ($attr->{PublicKey}); } @@ -583,13 +762,49 @@ sub rcv_success { my ($self, $attr) = @_; + $self->set_result (hex $attr->{Length}); +} + +package Net::FCP::Txn::GetPut; + +# base class for get and put + +use base Net::FCP::Txn; + +*rcv_uri_error = \&Net::FCP::Txn::rcv_throw_exception; +*rcv_route_not_found = \&Net::FCP::Txn::rcv_throw_exception; - $self->set_result ($attr->{Length}); +sub rcv_restarted { + my ($self, $attr, $type) = @_; + + delete $self->{datalength}; + delete $self->{metalength}; + delete $self->{data}; + + $self->progress ($type, $attr); } package Net::FCP::Txn::ClientGet; -use base Net::FCP::Txn; +use base Net::FCP::Txn::GetPut; + +*rcv_data_not_found = \&Net::FCP::Txn::rcv_throw_exception; + +sub rcv_data { + my ($self, $chunk) = @_; + + $self->{data} .= $chunk; + + $self->progress ("data", { chunk => length $chunk, received => length $self->{data}, total => $self->{datalength} }); + + if ($self->{datalength} == length $self->{data}) { + my $data = delete $self->{data}; + my $meta = Net::FCP::parse_metadata substr $data, 0, $self->{metalength}, ""; + + $self->set_result ([$meta, $data]); + $self->eof; + } +} sub rcv_data_found { my ($self, $attr, $type) = @_; @@ -600,49 +815,86 @@ $self->{metalength} = hex $attr->{metadata_length}; } -sub rcv_route_not_found { - my ($self, $attr, $type) = @_; +package Net::FCP::Txn::ClientPut; - $self->throw (new Net::FCP::Exception $type, $attr); -} +use base Net::FCP::Txn::GetPut; -sub rcv_data_not_found { - my ($self, $attr, $type) = @_; +*rcv_size_error = \&Net::FCP::Txn::rcv_throw_exception; +*rcv_key_collision = \&Net::FCP::Txn::rcv_throw_exception; - $self->throw (new Net::FCP::Exception $type, $attr); -} - -sub rcv_format_error { +sub rcv_pending { my ($self, $attr, $type) = @_; - - $self->throw (new Net::FCP::Exception $type, $attr); + $self->progress ($type, $attr); } -sub rcv_restarted { +sub rcv_success { my ($self, $attr, $type) = @_; - $self->progress ($type, $attr); + $self->set_result ($attr); } -sub eof { - my ($self) = @_; +=back - my $data = delete $self->{data}; - my $meta = Net::FCP::parse_metadata substr $data, 0, $self->{metalength}, ""; +=head2 The Net::FCP::Exception CLASS - $self->set_result ([$meta, $data]); -} +Any unexpected (non-standard) responses that make it impossible to return +the advertised result will result in an exception being thrown when the +C method is called. + +These exceptions are represented by objects of this class. + +=over 4 + +=cut package Net::FCP::Exception; use overload '""' => sub { - "Net::FCP::Exception<<$_[0][0]," . (join ":", %{$_[0][1]}) . ">>\n"; + "Net::FCP::Exception<<$_[0][0]," . (join ":", %{$_[0][1]}) . ">>"; }; +=item $exc = new Net::FCP::Exception $type, \%attr + +Create a new exception object of the given type (a string like +C), and a hashref containing additional attributes +(usually the attributes of the message causing the exception). + +=cut + sub new { my ($class, $type, $attr) = @_; - bless [$type, { %$attr }], $class; + bless [Net::FCP::tolc $type, { %$attr }], $class; +} + +=item $exc->type([$type]) + +With no arguments, returns the exception type. Otherwise a boolean +indicating wether the exception is of the given type is returned. + +=cut + +sub type { + my ($self, $type) = @_; + + @_ >= 2 + ? $self->[0] eq $type + : $self->[0]; +} + +=item $exc->attr([$attr]) + +With no arguments, returns the attributes. Otherwise the named attribute +value is returned. + +=cut + +sub attr { + my ($self, $attr) = @_; + + @_ >= 2 + ? $self->[1]{$attr} + : $self->[1]; } =back @@ -660,5 +912,37 @@ =cut +package Net::FCP::Event::Auto; + +my @models = ( + [Coro => Coro::Event:: ], + [Event => Event::], + [Glib => Glib:: ], + [Tk => Tk::], +); + +sub AUTOLOAD { + $AUTOLOAD =~ s/.*://; + + for (@models) { + my ($model, $package) = @$_; + if (defined ${"$package\::VERSION"}) { + $EVENT = "Net::FCP::Event::$model"; + eval "require $EVENT"; die if $@; + goto &{"$EVENT\::$AUTOLOAD"}; + } + } + + for (@models) { + my ($model, $package) = @$_; + $EVENT = "Net::FCP::Event::$model"; + if (eval "require $EVENT") { + goto &{"$EVENT\::$AUTOLOAD"}; + } + } + + die "No event module selected for Net::FCP and autodetect failed. Install any of these: Coro, Event, Glib or Tk."; +} + 1;