aboutsummaryrefslogtreecommitdiff
path: root/src/peerinfo-tool
diff options
context:
space:
mode:
authorGabor X Toth <*@tg-x.net>2012-11-18 18:09:34 +0000
committerGabor X Toth <*@tg-x.net>2012-11-18 18:09:34 +0000
commita618961d96910d622476341e7efdf6d1180acdd3 (patch)
tree0b6b383f354d1a27b5259a993723cc5a60dced8b /src/peerinfo-tool
parentff6b5bb3cfeeaaf353bf734c09619c4adbd02ee6 (diff)
downloadgnunet-a618961d96910d622476341e7efdf6d1180acdd3.tar.gz
gnunet-a618961d96910d622476341e7efdf6d1180acdd3.zip
moved hello uri functions to hello lib
Diffstat (limited to 'src/peerinfo-tool')
-rw-r--r--src/peerinfo-tool/gnunet-peerinfo.c335
1 files changed, 20 insertions, 315 deletions
diff --git a/src/peerinfo-tool/gnunet-peerinfo.c b/src/peerinfo-tool/gnunet-peerinfo.c
index c2520fac0..61185bc04 100644
--- a/src/peerinfo-tool/gnunet-peerinfo.c
+++ b/src/peerinfo-tool/gnunet-peerinfo.c
@@ -27,18 +27,13 @@
27#include "gnunet_crypto_lib.h" 27#include "gnunet_crypto_lib.h"
28#include "gnunet_configuration_lib.h" 28#include "gnunet_configuration_lib.h"
29#include "gnunet_getopt_lib.h" 29#include "gnunet_getopt_lib.h"
30#include "gnunet_peerinfo_service.h"
31#include "gnunet_transport_service.h"
32#include "gnunet_program_lib.h" 30#include "gnunet_program_lib.h"
33#include "gnunet_transport_plugin.h" 31#include "gnunet_hello_lib.h"
32#include "gnunet_transport_service.h"
33#include "gnunet_peerinfo_service.h"
34#include "gnunet-peerinfo_plugins.h" 34#include "gnunet-peerinfo_plugins.h"
35 35
36/** 36/**
37 * Prefix that every HELLO URI must start with.
38 */
39#define HELLO_URI_PREFIX "gnunet://hello/"
40
41/**
42 * How long until we time out during peerinfo iterations? 37 * How long until we time out during peerinfo iterations?
43 */ 38 */
44#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5) 39#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
@@ -115,37 +110,6 @@ struct PrintContext
115 110
116 111
117/** 112/**
118 * Context used for building our own URI.
119 */
120struct GetUriContext
121{
122 /**
123 * Final URI.
124 */
125 char *uri;
126
127};
128
129
130/**
131 * Context for 'add_address_to_hello'.
132 */
133struct GNUNET_PEERINFO_HelloAddressParsingContext
134{
135 /**
136 * Position in the URI with the next address to parse.
137 */
138 const char *pos;
139
140 /**
141 * Set to GNUNET_SYSERR to indicate parse errors.
142 */
143 int ret;
144
145};
146
147
148/**
149 * Option '-n' 113 * Option '-n'
150 */ 114 */
151static int no_resolve; 115static int no_resolve;
@@ -233,42 +197,6 @@ state_machine (void *cls,
233 const struct GNUNET_SCHEDULER_TaskContext *tc); 197 const struct GNUNET_SCHEDULER_TaskContext *tc);
234 198
235 199
236
237/**
238 * Replace all characters in the input 'in' according
239 * to the mapping. The mapping says to map each character
240 * in 'oldchars' to the corresponding character (by offset)
241 * in 'newchars'.
242 *
243 * @param in input string to remap
244 * @param oldchars characters to replace
245 * @param newchars replacement characters, must have same length as 'oldchars'
246 * @return copy of string with replacement applied.
247 */
248static char *
249map_characters (const char *in,
250 const char *oldchars,
251 const char *newchars)
252{
253 char *ret;
254 const char *off;
255 size_t i;
256
257 GNUNET_assert (strlen (oldchars) == strlen (newchars));
258 ret = GNUNET_strdup (in);
259 i = 0;
260 while (ret[i] != '\0')
261 {
262 off = strchr (oldchars, ret[i]);
263 if (NULL != off)
264 ret[i] = newchars[off - oldchars];
265 i++;
266 }
267 return ret;
268}
269
270
271
272/* ********************* 'get_info' ******************* */ 200/* ********************* 'get_info' ******************* */
273 201
274/** 202/**
@@ -438,65 +366,6 @@ print_peer_info (void *cls, const struct GNUNET_PeerIdentity *peer,
438 366
439 367
440/** 368/**
441 * Function that is called on each address of this peer.
442 * Expands the corresponding URI string.
443 *
444 * @param cls the 'GetUriContext'
445 * @param address address to add
446 * @param expiration expiration time for the address
447 * @return GNUNET_OK (continue iteration).
448 */
449static int
450compose_uri (void *cls, const struct GNUNET_HELLO_Address *address,
451 struct GNUNET_TIME_Absolute expiration)
452{
453 struct GetUriContext *guc = cls;
454 struct GNUNET_TRANSPORT_PluginFunctions *papi;
455 const char *addr;
456 char *uri_addr;
457 char *ret;
458 char tbuf[16];
459 struct tm *t;
460 time_t seconds;
461
462 papi = GPI_plugins_find (address->transport_name);
463 if (papi == NULL)
464 {
465 /* Not an error - we might just not have the right plugin. */
466 return GNUNET_OK;
467 }
468 if (NULL == papi->address_to_string)
469 {
470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
471 "URI conversion not implemented for plugin `%s'\n",
472 address->transport_name);
473 return GNUNET_OK;
474 }
475 addr = papi->address_to_string (papi->cls, address->address, address->address_length);
476 if ( (addr == NULL) || (strlen(addr) == 0) )
477 return GNUNET_OK;
478 /* For URIs we use '(' and ')' instead of '[' and ']' as brackets are reserved
479 characters in URIs */
480 uri_addr = map_characters (addr, "[]", "()");
481 seconds = expiration.abs_value / 1000;
482 t = gmtime (&seconds);
483 GNUNET_assert (0 != strftime (tbuf, sizeof (tbuf),
484 "%Y%m%d%H%M%S",
485 t));
486 GNUNET_asprintf (&ret,
487 "%s!%s!%s!%s",
488 guc->uri,
489 tbuf,
490 address->transport_name,
491 uri_addr);
492 GNUNET_free (uri_addr);
493 GNUNET_free (guc->uri);
494 guc->uri = ret;
495 return GNUNET_OK;
496}
497
498
499/**
500 * Print URI of the peer. 369 * Print URI of the peer.
501 * 370 *
502 * @param cls the 'struct GetUriContext' 371 * @param cls the 'struct GetUriContext'
@@ -509,8 +378,6 @@ print_my_uri (void *cls, const struct GNUNET_PeerIdentity *peer,
509 const struct GNUNET_HELLO_Message *hello, 378 const struct GNUNET_HELLO_Message *hello,
510 const char *err_msg) 379 const char *err_msg)
511{ 380{
512 struct GetUriContext *guc = cls;
513
514 if (peer == NULL) 381 if (peer == NULL)
515 { 382 {
516 pic = NULL; 383 pic = NULL;
@@ -518,157 +385,24 @@ print_my_uri (void *cls, const struct GNUNET_PeerIdentity *peer,
518 FPRINTF (stderr, 385 FPRINTF (stderr,
519 _("Error in communication with PEERINFO service: %s\n"), 386 _("Error in communication with PEERINFO service: %s\n"),
520 err_msg); 387 err_msg);
521 GNUNET_free_non_null (guc->uri);
522 GNUNET_free (guc);
523 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL); 388 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
524 return; 389 return;
525 }
526 if (NULL != hello)
527 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &compose_uri, guc);
528 printf ("%s\n", (const char *) guc->uri);
529}
530
531
532/* ************************* import HELLO by URI ********************* */
533
534
535/**
536 * We're building a HELLO. Parse the next address from the
537 * parsing context and append it.
538 *
539 * @param cls the 'struct GNUNET_PEERINFO_HelloAddressParsingContext'
540 * @param max number of bytes available for HELLO construction
541 * @param buffer where to copy the next address (in binary format)
542 * @return number of bytes added to buffer
543 */
544static size_t
545add_address_to_hello (void *cls, size_t max, void *buffer)
546{
547 struct GNUNET_PEERINFO_HelloAddressParsingContext *ctx = cls;
548 const char *tname;
549 const char *address;
550 char *uri_address;
551 char *plugin_address;
552 const char *end;
553 char *plugin_name;
554 struct tm expiration_time;
555 time_t expiration_seconds;
556 struct GNUNET_TIME_Absolute expire;
557 struct GNUNET_TRANSPORT_PluginFunctions *papi;
558 void *addr;
559 size_t addr_len;
560 struct GNUNET_HELLO_Address haddr;
561 size_t ret;
562
563 if (NULL == ctx->pos)
564 return 0;
565 if ('!' != ctx->pos[0])
566 {
567 ctx->ret = GNUNET_SYSERR;
568 GNUNET_break (0);
569 return 0;
570 } 390 }
571 ctx->pos++;
572 memset (&expiration_time, 0, sizeof (expiration_time));
573 tname = strptime (ctx->pos,
574 "%Y%m%d%H%M%S",
575 &expiration_time);
576 391
577 if (NULL == tname) 392 if (NULL == hello)
578 { 393 return;
579 ctx->ret = GNUNET_SYSERR; 394
580 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 395 char *uri = GNUNET_HELLO_compose_uri(hello, &GPI_plugins_find);
581 _("Failed to parse HELLO message: missing expiration time\n")); 396 if (NULL != uri) {
582 GNUNET_break (0); 397 printf ("%s\n", (const char *) uri);
583 return 0; 398 GNUNET_free (uri);
584 }
585 expiration_seconds = mktime (&expiration_time);
586 if (expiration_seconds == (time_t) -1)
587 {
588 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
589 _("Failed to parse HELLO message: invalid expiration time\n"));
590 ctx->ret = GNUNET_SYSERR;
591 GNUNET_break (0);
592 return 0;
593 }
594 expire.abs_value = expiration_seconds * 1000;
595 if ('!' != tname[0])
596 {
597 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
598 _("Failed to parse HELLO message: malformed\n"));
599 ctx->ret = GNUNET_SYSERR;
600 GNUNET_break (0);
601 return 0;
602 }
603 tname++;
604 address = strchr (tname, (int) '!');
605 if (NULL == address)
606 {
607 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
608 _("Failed to parse HELLO message: missing transport plugin\n"));
609 ctx->ret = GNUNET_SYSERR;
610 GNUNET_break (0);
611 return 0;
612 }
613 address++;
614 end = strchr (address, (int) '!');
615 ctx->pos = end;
616 plugin_name = GNUNET_strndup (tname, address - (tname+1));
617 papi = GPI_plugins_find (plugin_name);
618 if (NULL == papi)
619 {
620 /* Not an error - we might just not have the right plugin.
621 * Skip this part, advance to the next one and recurse.
622 * But only if this is not the end of string.
623 */
624 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
625 _("Plugin `%s' not found\n"),
626 plugin_name);
627 GNUNET_free (plugin_name);
628 GNUNET_break (0);
629 return 0;
630 }
631 if (NULL == papi->string_to_address)
632 {
633 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
634 _("Plugin `%s' does not support URIs yet\n"),
635 plugin_name);
636 GNUNET_free (plugin_name);
637 GNUNET_break (0);
638 return 0;
639 }
640 uri_address = GNUNET_strndup (address, end - address);
641 /* For URIs we use '(' and ')' instead of '[' and ']' as brackets are reserved
642 characters in URIs; need to convert back to '[]' for the plugin */
643 plugin_address = map_characters (uri_address, "()", "[]");
644 GNUNET_free (uri_address);
645 if (GNUNET_OK !=
646 papi->string_to_address (papi->cls,
647 plugin_address,
648 strlen (plugin_address) + 1,
649 &addr,
650 &addr_len))
651 {
652 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
653 _("Failed to parse `%s' as an address for plugin `%s'\n"),
654 plugin_address,
655 plugin_name);
656 GNUNET_free (plugin_name);
657 GNUNET_free (plugin_address);
658 return 0;
659 } 399 }
660 GNUNET_free (plugin_address);
661 /* address.peer is unset - not used by add_address() */
662 haddr.address_length = addr_len;
663 haddr.address = addr;
664 haddr.transport_name = plugin_name;
665 ret = GNUNET_HELLO_add_address (&haddr, expire, buffer, max);
666 GNUNET_free (addr);
667 GNUNET_free (plugin_name);
668 return ret;
669} 400}
670 401
671 402
403/* ************************* import HELLO by URI ********************* */
404
405
672/** 406/**
673 * Continuation called from 'GNUNET_PEERINFO_add_peer' 407 * Continuation called from 'GNUNET_PEERINFO_add_peer'
674 * 408 *
@@ -698,31 +432,13 @@ add_continuation (void *cls,
698static int 432static int
699parse_hello_uri (const char *put_uri) 433parse_hello_uri (const char *put_uri)
700{ 434{
701 const char *pks;
702 const char *exc;
703 struct GNUNET_HELLO_Message *hello; 435 struct GNUNET_HELLO_Message *hello;
704 struct GNUNET_PEERINFO_HelloAddressParsingContext ctx; 436
705 437 int ret = GNUNET_HELLO_parse_uri(put_uri, &my_public_key, &hello, &GPI_plugins_find);
706 if (0 != strncmp (put_uri, 438
707 HELLO_URI_PREFIX, 439 if (NULL != hello) {
708 strlen (HELLO_URI_PREFIX)))
709 return GNUNET_SYSERR;
710 pks = &put_uri[strlen (HELLO_URI_PREFIX)];
711 exc = strstr (pks, "!");
712
713 if (GNUNET_OK != GNUNET_STRINGS_string_to_data (pks,
714 (NULL == exc) ? strlen (pks) : (exc - pks),
715 (unsigned char *) &my_public_key,
716 sizeof (my_public_key)))
717 return GNUNET_SYSERR;
718 ctx.pos = exc;
719 ctx.ret = GNUNET_OK;
720 hello = GNUNET_HELLO_create (&my_public_key, &add_address_to_hello, &ctx);
721
722 if (NULL != hello)
723 {
724 /* WARNING: this adds the address from URI WITHOUT verification! */ 440 /* WARNING: this adds the address from URI WITHOUT verification! */
725 if (GNUNET_OK == ctx.ret) 441 if (GNUNET_OK == ret)
726 ac = GNUNET_PEERINFO_add_peer (peerinfo, hello, &add_continuation, NULL); 442 ac = GNUNET_PEERINFO_add_peer (peerinfo, hello, &add_continuation, NULL);
727 else 443 else
728 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL); 444 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
@@ -732,7 +448,7 @@ parse_hello_uri (const char *put_uri)
732 /* wait 1s to give peerinfo operation a chance to succeed */ 448 /* wait 1s to give peerinfo operation a chance to succeed */
733 /* FIXME: current peerinfo API sucks to require this; not to mention 449 /* FIXME: current peerinfo API sucks to require this; not to mention
734 that we get no feedback to determine if the operation actually succeeded */ 450 that we get no feedback to determine if the operation actually succeeded */
735 return ctx.ret; 451 return ret;
736} 452}
737 453
738 454
@@ -907,20 +623,9 @@ state_machine (void *cls,
907 } 623 }
908 if (GNUNET_YES == get_uri) 624 if (GNUNET_YES == get_uri)
909 { 625 {
910 struct GetUriContext *guc;
911 char *pkey;
912
913 guc = GNUNET_malloc (sizeof (struct GetUriContext));
914 pkey = GNUNET_CRYPTO_rsa_public_key_to_string (&my_public_key);
915 GNUNET_asprintf (&guc->uri,
916 "%s%s",
917 HELLO_URI_PREFIX,
918 pkey);
919 GNUNET_free (pkey);
920 GPI_plugins_load (cfg); 626 GPI_plugins_load (cfg);
921 pic = GNUNET_PEERINFO_iterate (peerinfo, &my_peer_identity, 627 pic = GNUNET_PEERINFO_iterate (peerinfo, &my_peer_identity,
922 TIMEOUT, 628 TIMEOUT, &print_my_uri, NULL);
923 &print_my_uri, guc);
924 get_uri = GNUNET_NO; 629 get_uri = GNUNET_NO;
925 return; 630 return;
926 } 631 }