… | |
… | |
182 | #$meta->{tail} = substr $data, pos $data; |
182 | #$meta->{tail} = substr $data, pos $data; |
183 | |
183 | |
184 | $meta; |
184 | $meta; |
185 | } |
185 | } |
186 | |
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 | |
187 | =item $fcp = new Net::FCP [host => $host][, port => $port][, progress => \&cb] |
234 | =item $fcp = new Net::FCP [host => $host][, port => $port][, progress => \&cb] |
188 | |
235 | |
189 | 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 |
190 | 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>). |
191 | |
238 | |
… | |
… | |
262 | sub txn { |
309 | sub txn { |
263 | my ($self, $type, %attr) = @_; |
310 | my ($self, $type, %attr) = @_; |
264 | |
311 | |
265 | $type = touc $type; |
312 | $type = touc $type; |
266 | |
313 | |
267 | 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); |
268 | |
315 | |
269 | $txn; |
316 | $txn; |
270 | } |
317 | } |
271 | |
318 | |
272 | { # transactions |
319 | { # transactions |
… | |
… | |
355 | |
402 | |
356 | =item $txn = $fcp->txn_generate_svk_pair |
403 | =item $txn = $fcp->txn_generate_svk_pair |
357 | |
404 | |
358 | =item ($public, $private) = @{ $fcp->generate_svk_pair } |
405 | =item ($public, $private) = @{ $fcp->generate_svk_pair } |
359 | |
406 | |
360 | 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. |
361 | |
409 | |
362 | [ |
410 | [ |
363 | "hKs0-WDQA4pVZyMPKNFsK1zapWY", |
411 | "acLx4dux9fvvABH15Gk6~d3I-yw", |
364 | "ZnmvMITaTXBMFGl4~jrjuyWxOWg" |
412 | "cPoDkDMXDGSMM32plaPZDhJDxSs", |
|
|
413 | "BH7LXCov0w51-y9i~BoB3g", |
365 | ] |
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! |
366 | |
425 | |
367 | =cut |
426 | =cut |
368 | |
427 | |
369 | $txn->(generate_svk_pair => sub { |
428 | $txn->(generate_svk_pair => sub { |
370 | my ($self) = @_; |
429 | my ($self) = @_; |
371 | |
430 | |
372 | $self->txn ("generate_svk_pair"); |
431 | $self->txn ("generate_svk_pair"); |
373 | }); |
432 | }); |
374 | |
433 | |
375 | =item $txn = $fcp->txn_insert_private_key ($private) |
434 | =item $txn = $fcp->txn_invert_private_key ($private) |
376 | |
435 | |
377 | =item $public = $fcp->insert_private_key ($private) |
436 | =item $public = $fcp->invert_private_key ($private) |
378 | |
437 | |
379 | 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 |
380 | 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. |
381 | back from C<generate_svk_pair>). |
440 | the private value you get back from C<generate_svk_pair>). |
382 | |
441 | |
383 | Returns the public key. |
442 | Returns the public key. |
384 | |
443 | |
385 | UNTESTED. |
|
|
386 | |
|
|
387 | =cut |
444 | =cut |
388 | |
445 | |
389 | $txn->(insert_private_key => sub { |
446 | $txn->(invert_private_key => sub { |
390 | my ($self, $privkey) = @_; |
447 | my ($self, $privkey) = @_; |
391 | |
448 | |
392 | $self->txn (invert_private_key => private => $privkey); |
449 | $self->txn (invert_private_key => private => $privkey); |
393 | }); |
450 | }); |
394 | |
451 | |
… | |
… | |
396 | |
453 | |
397 | =item $length = $fcp->get_size ($uri) |
454 | =item $length = $fcp->get_size ($uri) |
398 | |
455 | |
399 | 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 |
400 | given document. |
457 | given document. |
401 | |
|
|
402 | UNTESTED. |
|
|
403 | |
458 | |
404 | =cut |
459 | =cut |
405 | |
460 | |
406 | $txn->(get_size => sub { |
461 | $txn->(get_size => sub { |
407 | my ($self, $uri) = @_; |
462 | my ($self, $uri) = @_; |
… | |
… | |
444 | |
499 | |
445 | =item my $uri = $fcp->client_put ($uri, $metadata, $data, $htl, $removelocal); |
500 | =item my $uri = $fcp->client_put ($uri, $metadata, $data, $htl, $removelocal); |
446 | |
501 | |
447 | 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 |
448 | 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 |
449 | CHK. |
504 | CHK. If the key is a private SSK key, the node will calculcate the public |
|
|
505 | key and the resulting public URI. |
450 | |
506 | |
451 | 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. |
452 | |
509 | |
453 | 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>. |
454 | |
511 | |
455 | =cut |
512 | =cut |
456 | |
513 | |
457 | $txn->(client_put => sub { |
514 | $txn->(client_put => sub { |
458 | my ($self, $uri, $meta, $data, $htl, $removelocal) = @_; |
515 | my ($self, $uri, $meta, $data, $htl, $removelocal) = @_; |
459 | |
516 | |
460 | $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), |
461 | remove_local_key => $removelocal ? "true" : "false", |
521 | remove_local_key => $removelocal ? "true" : "false", |
462 | data => "$meta$data", metadata_length => xeh length $meta); |
522 | data => "$meta$data", metadata_length => xeh length $meta); |
463 | }); |
523 | }); |
464 | |
524 | |
465 | } # transactions |
525 | } # transactions |
466 | |
|
|
467 | =item MISSING: (ClientPut), InsertKey |
|
|
468 | |
526 | |
469 | =back |
527 | =back |
470 | |
528 | |
471 | =head2 THE Net::FCP::Txn CLASS |
529 | =head2 THE Net::FCP::Txn CLASS |
472 | |
530 | |
… | |
… | |
759 | |
817 | |
760 | use base Net::FCP::Txn; |
818 | use base Net::FCP::Txn; |
761 | |
819 | |
762 | sub rcv_success { |
820 | sub rcv_success { |
763 | my ($self, $attr) = @_; |
821 | my ($self, $attr) = @_; |
764 | $self->set_result ([$attr->{PublicKey}, $attr->{PrivateKey}]); |
822 | $self->set_result ([$attr->{public_key}, $attr->{private_key}, $attr->{crypto_key}]); |
765 | } |
823 | } |
766 | |
824 | |
767 | package Net::FCP::Txn::InsertPrivateKey; |
825 | package Net::FCP::Txn::InvertPrivateKey; |
768 | |
826 | |
769 | use base Net::FCP::Txn; |
827 | use base Net::FCP::Txn; |
770 | |
828 | |
771 | sub rcv_success { |
829 | sub rcv_success { |
772 | my ($self, $attr) = @_; |
830 | my ($self, $attr) = @_; |
773 | $self->set_result ($attr->{PublicKey}); |
831 | $self->set_result ($attr->{public_key}); |
774 | } |
832 | } |
775 | |
833 | |
776 | package Net::FCP::Txn::GetSize; |
834 | package Net::FCP::Txn::GetSize; |
777 | |
835 | |
778 | use base Net::FCP::Txn; |
836 | use base Net::FCP::Txn; |
779 | |
837 | |
780 | sub rcv_success { |
838 | sub rcv_success { |
781 | my ($self, $attr) = @_; |
839 | my ($self, $attr) = @_; |
782 | $self->set_result (hex $attr->{Length}); |
840 | $self->set_result (hex $attr->{length}); |
783 | } |
841 | } |
784 | |
842 | |
785 | package Net::FCP::Txn::GetPut; |
843 | package Net::FCP::Txn::GetPut; |
786 | |
844 | |
787 | # base class for get and put |
845 | # base class for get and put |