… | |
… | |
99 | $_; |
99 | $_; |
100 | } |
100 | } |
101 | |
101 | |
102 | sub tolc($) { |
102 | sub tolc($) { |
103 | local $_ = shift; |
103 | local $_ = shift; |
|
|
104 | 1 while s/(SVK|CHK|URI)([^_])/$1\_$2/i; |
|
|
105 | 1 while s/([^_])(SVK|CHK|URI)/$1\_$2/i; |
104 | s/(?<=[a-z])(?=[A-Z])/_/g; |
106 | s/(?<=[a-z])(?=[A-Z])/_/g; |
105 | lc $_; |
107 | lc $_; |
106 | } |
108 | } |
107 | |
109 | |
108 | # the opposite of hex |
110 | # the opposite of hex |
… | |
… | |
180 | #$meta->{tail} = substr $data, pos $data; |
182 | #$meta->{tail} = substr $data, pos $data; |
181 | |
183 | |
182 | $meta; |
184 | $meta; |
183 | } |
185 | } |
184 | |
186 | |
|
|
187 | =item $string = Net::FCP::build_metadata $meta |
|
|
188 | |
|
|
189 | Takes a hash reference as returned by C<Net::FCP::parse_metadata> and |
|
|
190 | returns the corresponding string form. If a string is given, it's returned |
|
|
191 | as is. |
|
|
192 | |
|
|
193 | =cut |
|
|
194 | |
|
|
195 | sub build_metadata_subhash($$$) { |
|
|
196 | my ($prefix, $level, $hash) = @_; |
|
|
197 | |
|
|
198 | join "", |
|
|
199 | map |
|
|
200 | ref $hash->{$_} ? build_metadata_subhash ($prefix . (Net::FCP::touc $_) . ".", $level + 1, $hash->{$_}) |
|
|
201 | : $prefix . ($level > 1 ? $_ : Net::FCP::touc $_) . "=" . $hash->{$_} . "\n", |
|
|
202 | keys %$hash; |
|
|
203 | } |
|
|
204 | |
|
|
205 | sub build_metadata_hash($$) { |
|
|
206 | my ($header, $hash) = @_; |
|
|
207 | |
|
|
208 | if (ref $hash eq ARRAY::) { |
|
|
209 | join "", map build_metadata_hash ($header, $_), @$hash |
|
|
210 | } else { |
|
|
211 | (Net::FCP::touc $header) . "\n" |
|
|
212 | . (build_metadata_subhash "", 0, $hash) |
|
|
213 | . "EndPart\n"; |
|
|
214 | } |
|
|
215 | } |
|
|
216 | |
|
|
217 | sub build_metadata($) { |
|
|
218 | my ($meta) = @_; |
|
|
219 | |
|
|
220 | return $meta unless ref $meta; |
|
|
221 | |
|
|
222 | $meta = { %$meta }; |
|
|
223 | |
|
|
224 | delete $meta->{raw}; |
|
|
225 | |
|
|
226 | my $res = |
|
|
227 | (build_metadata_hash version => delete $meta->{version}) |
|
|
228 | . (join "", map +(build_metadata_hash $_, $meta->{$_}), keys %$meta); |
|
|
229 | |
|
|
230 | substr $res, 0, -5; # get rid of "Part". Broken Syntax.... |
|
|
231 | } |
|
|
232 | |
|
|
233 | |
185 | =item $fcp = new Net::FCP [host => $host][, port => $port] |
234 | =item $fcp = new Net::FCP [host => $host][, port => $port][, progress => \&cb] |
186 | |
235 | |
187 | Create a new virtual FCP connection to the given host and port (default |
236 | Create a new virtual FCP connection to the given host and port (default |
188 | 127.0.0.1:8481, or the environment variables C<FREDHOST> and C<FREDPORT>). |
237 | 127.0.0.1:8481, or the environment variables C<FREDHOST> and C<FREDPORT>). |
189 | |
238 | |
190 | Connections are virtual because no persistent physical connection is |
239 | Connections are virtual because no persistent physical connection is |
191 | established. |
240 | established. |
|
|
241 | |
|
|
242 | You can install a progress callback that is being called with the Net::FCP |
|
|
243 | object, a txn object, the type of the transaction and the attributes. Use |
|
|
244 | it like this: |
|
|
245 | |
|
|
246 | sub progress_cb { |
|
|
247 | my ($self, $txn, $type, $attr) = @_; |
|
|
248 | |
|
|
249 | warn "progress<$txn,$type," . (join ":", %$attr) . ">\n"; |
|
|
250 | } |
192 | |
251 | |
193 | =begin comment |
252 | =begin comment |
194 | |
253 | |
195 | However, the existance of the node is checked by executing a |
254 | However, the existance of the node is checked by executing a |
196 | C<ClientHello> transaction. |
255 | C<ClientHello> transaction. |
… | |
… | |
212 | $self; |
271 | $self; |
213 | } |
272 | } |
214 | |
273 | |
215 | sub progress { |
274 | sub progress { |
216 | my ($self, $txn, $type, $attr) = @_; |
275 | my ($self, $txn, $type, $attr) = @_; |
217 | #warn "progress<$txn,$type," . (join ":", %$attr) . ">\n"; |
276 | |
|
|
277 | $self->{progress}->($self, $txn, $type, $attr) |
|
|
278 | if $self->{progress}; |
218 | } |
279 | } |
219 | |
280 | |
220 | =item $txn = $fcp->txn(type => attr => val,...) |
281 | =item $txn = $fcp->txn(type => attr => val,...) |
221 | |
282 | |
222 | The low-level interface to transactions. Don't use it. |
283 | The low-level interface to transactions. Don't use it. |
… | |
… | |
248 | sub txn { |
309 | sub txn { |
249 | my ($self, $type, %attr) = @_; |
310 | my ($self, $type, %attr) = @_; |
250 | |
311 | |
251 | $type = touc $type; |
312 | $type = touc $type; |
252 | |
313 | |
253 | my $txn = "Net::FCP::Txn::$type"->new(fcp => $self, type => tolc $type, attr => \%attr); |
314 | my $txn = "Net::FCP::Txn::$type"->new (fcp => $self, type => tolc $type, attr => \%attr); |
254 | |
315 | |
255 | $txn; |
316 | $txn; |
256 | } |
317 | } |
257 | |
318 | |
258 | { # transactions |
319 | { # transactions |
… | |
… | |
323 | |
384 | |
324 | =item $txn = $fcp->txn_generate_chk ($metadata, $data[, $cipher]) |
385 | =item $txn = $fcp->txn_generate_chk ($metadata, $data[, $cipher]) |
325 | |
386 | |
326 | =item $uri = $fcp->generate_chk ($metadata, $data[, $cipher]) |
387 | =item $uri = $fcp->generate_chk ($metadata, $data[, $cipher]) |
327 | |
388 | |
328 | Calculcates a CHK, given the metadata and data. C<$cipher> is either |
389 | Calculates a CHK, given the metadata and data. C<$cipher> is either |
329 | C<Rijndael> or C<Twofish>, with the latter being the default. |
390 | C<Rijndael> or C<Twofish>, with the latter being the default. |
330 | |
391 | |
331 | =cut |
392 | =cut |
332 | |
393 | |
333 | $txn->(generate_chk => sub { |
394 | $txn->(generate_chk => sub { |
… | |
… | |
341 | |
402 | |
342 | =item $txn = $fcp->txn_generate_svk_pair |
403 | =item $txn = $fcp->txn_generate_svk_pair |
343 | |
404 | |
344 | =item ($public, $private) = @{ $fcp->generate_svk_pair } |
405 | =item ($public, $private) = @{ $fcp->generate_svk_pair } |
345 | |
406 | |
346 | Creates a new SVK pair. Returns an arrayref. |
407 | Creates a new SVK pair. Returns an arrayref with the public key, the |
|
|
408 | private key and a crypto key, which is just additional entropy. |
347 | |
409 | |
348 | [ |
410 | [ |
349 | "hKs0-WDQA4pVZyMPKNFsK1zapWY", |
411 | "acLx4dux9fvvABH15Gk6~d3I-yw", |
350 | "ZnmvMITaTXBMFGl4~jrjuyWxOWg" |
412 | "cPoDkDMXDGSMM32plaPZDhJDxSs", |
|
|
413 | "BH7LXCov0w51-y9i~BoB3g", |
351 | ] |
414 | ] |
|
|
415 | |
|
|
416 | A private key (for inserting) can be constructed like this: |
|
|
417 | |
|
|
418 | SSK@<private_key>,<crypto_key>/<name> |
|
|
419 | |
|
|
420 | It can be used to insert data. The corresponding public key looks like this: |
|
|
421 | |
|
|
422 | SSK@<public_key>PAgM,<crypto_key>/<name> |
|
|
423 | |
|
|
424 | Watch out for the C<PAgM>-part! |
352 | |
425 | |
353 | =cut |
426 | =cut |
354 | |
427 | |
355 | $txn->(generate_svk_pair => sub { |
428 | $txn->(generate_svk_pair => sub { |
356 | my ($self) = @_; |
429 | my ($self) = @_; |
357 | |
430 | |
358 | $self->txn ("generate_svk_pair"); |
431 | $self->txn ("generate_svk_pair"); |
359 | }); |
432 | }); |
360 | |
433 | |
361 | =item $txn = $fcp->txn_insert_private_key ($private) |
434 | =item $txn = $fcp->txn_invert_private_key ($private) |
362 | |
435 | |
363 | =item $public = $fcp->insert_private_key ($private) |
436 | =item $public = $fcp->invert_private_key ($private) |
364 | |
437 | |
365 | Inserts a private key. $private can be either an insert URI (must start |
438 | Inverts a private key (returns the public key). C<$private> can be either |
366 | with C<freenet:SSK@>) or a raw private key (i.e. the private value you get |
439 | an insert URI (must start with C<freenet:SSK@>) or a raw private key (i.e. |
367 | back from C<generate_svk_pair>). |
440 | the private value you get back from C<generate_svk_pair>). |
368 | |
441 | |
369 | Returns the public key. |
442 | Returns the public key. |
370 | |
443 | |
371 | UNTESTED. |
|
|
372 | |
|
|
373 | =cut |
444 | =cut |
374 | |
445 | |
375 | $txn->(insert_private_key => sub { |
446 | $txn->(invert_private_key => sub { |
376 | my ($self, $privkey) = @_; |
447 | my ($self, $privkey) = @_; |
377 | |
448 | |
378 | $self->txn (invert_private_key => private => $privkey); |
449 | $self->txn (invert_private_key => private => $privkey); |
379 | }); |
450 | }); |
380 | |
451 | |
… | |
… | |
382 | |
453 | |
383 | =item $length = $fcp->get_size ($uri) |
454 | =item $length = $fcp->get_size ($uri) |
384 | |
455 | |
385 | Finds and returns the size (rounded up to the nearest power of two) of the |
456 | Finds and returns the size (rounded up to the nearest power of two) of the |
386 | given document. |
457 | given document. |
387 | |
|
|
388 | UNTESTED. |
|
|
389 | |
458 | |
390 | =cut |
459 | =cut |
391 | |
460 | |
392 | $txn->(get_size => sub { |
461 | $txn->(get_size => sub { |
393 | my ($self, $uri) = @_; |
462 | my ($self, $uri) = @_; |
… | |
… | |
400 | =item ($metadata, $data) = @{ $fcp->client_get ($uri, $htl, $removelocal) |
469 | =item ($metadata, $data) = @{ $fcp->client_get ($uri, $htl, $removelocal) |
401 | |
470 | |
402 | Fetches a (small, as it should fit into memory) file from |
471 | Fetches a (small, as it should fit into memory) file from |
403 | freenet. C<$meta> is the metadata (as returned by C<parse_metadata> or |
472 | freenet. C<$meta> is the metadata (as returned by C<parse_metadata> or |
404 | C<undef>). |
473 | C<undef>). |
|
|
474 | |
|
|
475 | The C<$uri> should begin with C<freenet:>, but the scheme is currently |
|
|
476 | added, if missing. |
405 | |
477 | |
406 | Due to the overhead, a better method to download big files should be used. |
478 | Due to the overhead, a better method to download big files should be used. |
407 | |
479 | |
408 | my ($meta, $data) = @{ |
480 | my ($meta, $data) = @{ |
409 | $fcp->client_get ( |
481 | $fcp->client_get ( |
… | |
… | |
414 | =cut |
486 | =cut |
415 | |
487 | |
416 | $txn->(client_get => sub { |
488 | $txn->(client_get => sub { |
417 | my ($self, $uri, $htl, $removelocal) = @_; |
489 | my ($self, $uri, $htl, $removelocal) = @_; |
418 | |
490 | |
|
|
491 | $uri =~ s/^freenet://; |
|
|
492 | $uri = "freenet:$uri"; |
|
|
493 | |
419 | $self->txn (client_get => URI => $uri, hops_to_live => xeh (defined $htl ? $htl : 15), |
494 | $self->txn (client_get => URI => $uri, hops_to_live => xeh (defined $htl ? $htl : 15), |
420 | remove_local_key => $removelocal ? "true" : "false"); |
495 | remove_local_key => $removelocal ? "true" : "false"); |
421 | }); |
496 | }); |
422 | |
497 | |
423 | =item $txn = $fcp->txn_client_put ($uri, $metadata, $data, $htl, $removelocal) |
498 | =item $txn = $fcp->txn_client_put ($uri, $metadata, $data, $htl, $removelocal) |
424 | |
499 | |
425 | =item my $uri = $fcp->client_put ($uri, $metadata, $data, $htl, $removelocal); |
500 | =item my $uri = $fcp->client_put ($uri, $metadata, $data, $htl, $removelocal); |
426 | |
501 | |
427 | Insert a new key. If the client is inserting a CHK, the URI may be |
502 | Insert a new key. If the client is inserting a CHK, the URI may be |
428 | abbreviated as just CHK@. In this case, the node will calculate the |
503 | abbreviated as just CHK@. In this case, the node will calculate the |
429 | CHK. |
504 | CHK. If the key is a private SSK key, the node will calculcate the public |
|
|
505 | key and the resulting public URI. |
430 | |
506 | |
431 | C<$meta> can be a reference or a string (ONLY THE STRING CASE IS IMPLEMENTED!). |
507 | C<$meta> can be a hash reference (same format as returned by |
|
|
508 | C<Net::FCP::parse_metadata>) or a string. |
432 | |
509 | |
433 | THIS INTERFACE IS UNTESTED AND SUBJECT TO CHANGE. |
510 | The result is an arrayref with the keys C<uri>, C<public_key> and C<private_key>. |
434 | |
511 | |
435 | =cut |
512 | =cut |
436 | |
513 | |
437 | $txn->(client_put => sub { |
514 | $txn->(client_put => sub { |
438 | my ($self, $uri, $meta, $data, $htl, $removelocal) = @_; |
515 | my ($self, $uri, $meta, $data, $htl, $removelocal) = @_; |
439 | |
516 | |
440 | $self->txn (client_put => URI => $uri, xeh (defined $htl ? $htl : 15), |
517 | $meta = build_metadata $meta; |
|
|
518 | |
|
|
519 | $self->txn (client_put => URI => $uri, |
|
|
520 | hops_to_live => xeh (defined $htl ? $htl : 15), |
441 | remove_local_key => $removelocal ? "true" : "false", |
521 | remove_local_key => $removelocal ? "true" : "false", |
442 | data => "$meta$data", metadata_length => xeh length $meta); |
522 | data => "$meta$data", metadata_length => xeh length $meta); |
443 | }); |
523 | }); |
444 | |
524 | |
445 | } # transactions |
525 | } # transactions |
446 | |
|
|
447 | =item MISSING: (ClientPut), InsertKey |
|
|
448 | |
526 | |
449 | =back |
527 | =back |
450 | |
528 | |
451 | =head2 THE Net::FCP::Txn CLASS |
529 | =head2 THE Net::FCP::Txn CLASS |
452 | |
530 | |
… | |
… | |
677 | } |
755 | } |
678 | } |
756 | } |
679 | |
757 | |
680 | sub progress { |
758 | sub progress { |
681 | my ($self, $type, $attr) = @_; |
759 | my ($self, $type, $attr) = @_; |
|
|
760 | |
682 | $self->{fcp}->progress ($self, $type, $attr); |
761 | $self->{fcp}->progress ($self, $type, $attr); |
683 | } |
762 | } |
684 | |
763 | |
685 | =item $result = $txn->result |
764 | =item $result = $txn->result |
686 | |
765 | |
… | |
… | |
738 | |
817 | |
739 | use base Net::FCP::Txn; |
818 | use base Net::FCP::Txn; |
740 | |
819 | |
741 | sub rcv_success { |
820 | sub rcv_success { |
742 | my ($self, $attr) = @_; |
821 | my ($self, $attr) = @_; |
743 | $self->set_result ([$attr->{PublicKey}, $attr->{PrivateKey}]); |
822 | $self->set_result ([$attr->{public_key}, $attr->{private_key}, $attr->{crypto_key}]); |
744 | } |
823 | } |
745 | |
824 | |
746 | package Net::FCP::Txn::InsertPrivateKey; |
825 | package Net::FCP::Txn::InvertPrivateKey; |
747 | |
826 | |
748 | use base Net::FCP::Txn; |
827 | use base Net::FCP::Txn; |
749 | |
828 | |
750 | sub rcv_success { |
829 | sub rcv_success { |
751 | my ($self, $attr) = @_; |
830 | my ($self, $attr) = @_; |
752 | $self->set_result ($attr->{PublicKey}); |
831 | $self->set_result ($attr->{public_key}); |
753 | } |
832 | } |
754 | |
833 | |
755 | package Net::FCP::Txn::GetSize; |
834 | package Net::FCP::Txn::GetSize; |
756 | |
835 | |
757 | use base Net::FCP::Txn; |
836 | use base Net::FCP::Txn; |
758 | |
837 | |
759 | sub rcv_success { |
838 | sub rcv_success { |
760 | my ($self, $attr) = @_; |
839 | my ($self, $attr) = @_; |
761 | $self->set_result (hex $attr->{Length}); |
840 | $self->set_result (hex $attr->{length}); |
762 | } |
841 | } |
763 | |
842 | |
764 | package Net::FCP::Txn::GetPut; |
843 | package Net::FCP::Txn::GetPut; |
765 | |
844 | |
766 | # base class for get and put |
845 | # base class for get and put |
767 | |
846 | |
768 | use base Net::FCP::Txn; |
847 | use base Net::FCP::Txn; |
769 | |
848 | |
770 | *rcv_urierror = \&Net::FCP::Txn::rcv_throw_exception; |
849 | *rcv_uri_error = \&Net::FCP::Txn::rcv_throw_exception; |
771 | *rcv_route_not_found = \&Net::FCP::Txn::rcv_throw_exception; |
850 | *rcv_route_not_found = \&Net::FCP::Txn::rcv_throw_exception; |
772 | |
851 | |
773 | sub rcv_restarted { |
852 | sub rcv_restarted { |
774 | my ($self, $attr, $type) = @_; |
853 | my ($self, $attr, $type) = @_; |
775 | |
854 | |
776 | delete $self->{datalength}; |
855 | delete $self->{datalength}; |
… | |
… | |
909 | =cut |
988 | =cut |
910 | |
989 | |
911 | package Net::FCP::Event::Auto; |
990 | package Net::FCP::Event::Auto; |
912 | |
991 | |
913 | my @models = ( |
992 | my @models = ( |
914 | [Coro => Coro::Event:: ], |
993 | [Coro => Coro::Event::], |
915 | [Event => Event::], |
994 | [Event => Event::], |
916 | [Glib => Glib:: ], |
995 | [Glib => Glib::], |
917 | [Tk => Tk::], |
996 | [Tk => Tk::], |
918 | ); |
997 | ); |
919 | |
998 | |
920 | sub AUTOLOAD { |
999 | sub AUTOLOAD { |
921 | $AUTOLOAD =~ s/.*://; |
1000 | $AUTOLOAD =~ s/.*://; |