-fix
[oweals/gnunet.git] / src / peerinfo-tool / gnunet-peerinfo.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001-2012 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file peerinfo-tool/gnunet-peerinfo.c
23  * @brief Print information about other known peers.
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_crypto_lib.h"
28 #include "gnunet_configuration_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"
33 #include "gnunet_transport_plugin.h"
34 #include "gnunet-peerinfo_plugins.h"
35
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?
43  */
44 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
45
46 /**
47  * Structure we use to collect printable address information.
48  */
49 struct PrintContext;
50
51 /**
52  * Record we keep for each printable address.
53  */
54 struct AddressRecord
55 {
56   /**
57    * Current address-to-string context (if active, otherwise NULL).
58    */
59   struct GNUNET_TRANSPORT_AddressToStringContext *atsc;
60
61   /**
62    * Printable address.
63    */
64   char *result;
65   
66   /**
67    * Print context this address record belongs to.
68    */
69   struct PrintContext *pc;
70 };
71
72
73 /**
74  * Structure we use to collect printable address information.
75  */
76 struct PrintContext
77 {
78
79   /**
80    * Kept in DLL.
81    */
82   struct PrintContext *next;
83
84   /**
85    * Kept in DLL.
86    */
87   struct PrintContext *prev;
88
89   /**
90    * Identity of the peer.
91    */
92   struct GNUNET_PeerIdentity peer;
93   
94   /**
95    * List of printable addresses.
96    */
97   struct AddressRecord *address_list;
98
99   /**
100    * Number of completed addresses in 'address_list'.
101    */
102   unsigned int num_addresses;
103
104   /**
105    * Number of addresses allocated in 'address_list'.
106    */
107   unsigned int address_list_size;
108
109   /**
110    * Current offset in 'address_list' (counted down).
111    */
112   unsigned int off;
113
114 };
115
116
117 /**
118  * Context used for building our own URI.
119  */
120 struct GetUriContext
121 {
122   /**
123    * Final URI.
124    */
125   char *uri;
126
127 };
128
129
130 /**
131  * Context for 'add_address_to_hello'.
132  */
133 struct 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'
150  */
151 static int no_resolve;
152
153 /**
154  * Option '-q'
155  */
156 static int be_quiet;
157
158 /**
159  * Option '-s'
160  */
161 static int get_self;
162
163 /**
164  * Option 
165  */
166 static int get_uri;
167
168 /**
169  * Option '-i'
170  */
171 static int get_info;
172
173 /**
174  * Option 
175  */
176 static char *put_uri;
177
178 /**
179  * Handle to peerinfo service.
180  */
181 static struct GNUNET_PEERINFO_Handle *peerinfo;
182
183 /**
184  * Configuration handle.
185  */
186 static const struct GNUNET_CONFIGURATION_Handle *cfg;
187
188 /**
189  * Main state machine task (if active).
190  */
191 static GNUNET_SCHEDULER_TaskIdentifier tt;
192
193 /**
194  * Current iterator context (if active, otherwise NULL).
195  */
196 static struct GNUNET_PEERINFO_IteratorContext *pic;
197
198 /**
199  * My peer identity.
200  */
201 static struct GNUNET_PeerIdentity my_peer_identity;
202
203 /**
204  * My public key.
205  */
206 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
207
208 /**
209  * Head of list of print contexts.
210  */
211 static struct PrintContext *pc_head;
212
213 /**
214  * Tail of list of print contexts.
215  */
216 static struct PrintContext *pc_tail;
217
218
219 /**
220  * Main state machine that goes over all options and
221  * runs the next requested function.
222  *
223  * @param cls unused
224  * @param tc unused
225  */
226 static void
227 state_machine (void *cls,
228                const struct GNUNET_SCHEDULER_TaskContext *tc);
229
230
231 /* ********************* 'get_info' ******************* */
232
233 /**
234  * Print the collected address information to the console and free 'pc'.
235  *
236  * @param pc printing context
237  */
238 static void
239 dump_pc (struct PrintContext *pc)
240 {
241   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
242   unsigned int i;
243
244   GNUNET_CRYPTO_hash_to_enc (&pc->peer.hashPubKey, &enc);
245   printf (_("Peer `%s'\n"), 
246           (const char *) &enc);
247   for (i = 0; i < pc->num_addresses; i++)
248   {
249     if (NULL != pc->address_list[i].result)
250     {
251       printf ("\t%s\n", pc->address_list[i].result);
252       GNUNET_free (pc->address_list[i].result);
253     }
254   }
255   printf ("\n");
256   GNUNET_free_non_null (pc->address_list);
257   GNUNET_CONTAINER_DLL_remove (pc_head,
258                                pc_tail,
259                                pc);
260   GNUNET_free (pc);
261   if ( (NULL == pc_head) &&
262        (NULL == pic) )
263     tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);  
264 }
265
266
267 /* ************************* list all known addresses **************** */
268
269
270 /**
271  * Function to call with a human-readable format of an address
272  *
273  * @param cls closure
274  * @param address NULL on error, otherwise 0-terminated printable UTF-8 string
275  */
276 static void
277 process_resolved_address (void *cls, const char *address)
278 {
279   struct AddressRecord * ar = cls;
280   struct PrintContext *pc = ar->pc;
281
282   if (NULL != address)
283   {
284     if (NULL == ar->result)
285       ar->result = GNUNET_strdup (address);
286     return;
287   }
288   ar->atsc = NULL;
289   pc->num_addresses++;
290   if (pc->num_addresses == pc->address_list_size)
291     dump_pc (pc);
292 }
293
294
295 /**
296  * Iterator callback to go over all addresses and count them.
297  *
298  * @param cls 'struct PrintContext' with 'off' to increment
299  * @param address the address
300  * @param expiration expiration time
301  * @return GNUNET_OK to keep the address and continue
302  */
303 static int
304 count_address (void *cls, const struct GNUNET_HELLO_Address *address,
305                struct GNUNET_TIME_Absolute expiration)
306 {
307   struct PrintContext *pc = cls;
308
309   pc->off++;
310   return GNUNET_OK;
311 }
312
313
314 /**
315  * Iterator callback to go over all addresses.
316  *
317  * @param cls closure
318  * @param address the address
319  * @param expiration expiration time
320  * @return GNUNET_OK to keep the address and continue
321  */
322 static int
323 print_address (void *cls, const struct GNUNET_HELLO_Address *address,
324                struct GNUNET_TIME_Absolute expiration)
325 {
326   struct PrintContext *pc = cls;
327   struct AddressRecord *ar;
328
329   GNUNET_assert (0 < pc->off);
330   ar = &pc->address_list[--pc->off];
331   ar->pc = pc;
332   ar->atsc = GNUNET_TRANSPORT_address_to_string (cfg, address, no_resolve,
333                                                  GNUNET_TIME_relative_multiply
334                                                  (GNUNET_TIME_UNIT_SECONDS, 10),
335                                                  &process_resolved_address, ar);
336   return GNUNET_OK;
337 }
338
339
340 /**
341  * Print information about the peer.
342  * Currently prints the GNUNET_PeerIdentity and the transport address.
343  *
344  * @param cls the 'struct PrintContext'
345  * @param peer identity of the peer 
346  * @param hello addresses of the peer
347  * @param err_msg error message
348  */
349 static void
350 print_peer_info (void *cls, const struct GNUNET_PeerIdentity *peer,
351                  const struct GNUNET_HELLO_Message *hello, const char *err_msg)
352 {
353   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
354   struct PrintContext *pc;
355
356   if (peer == NULL)
357   {
358     pic = NULL; /* end of iteration */
359     if (err_msg != NULL)
360     {
361       FPRINTF (stderr, 
362                _("Error in communication with PEERINFO service: %s\n"),
363                err_msg);
364     }
365     if (NULL == pc_head)
366       tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
367     return;
368   }
369   if ((GNUNET_YES == be_quiet) || (NULL == hello))
370   {
371     GNUNET_CRYPTO_hash_to_enc (&peer->hashPubKey, &enc);
372     printf ("%s\n", (const char *) &enc);
373     return;
374   }
375   pc = GNUNET_malloc (sizeof (struct PrintContext));
376   GNUNET_CONTAINER_DLL_insert (pc_head,
377                                pc_tail, 
378                                pc);
379   pc->peer = *peer;
380   GNUNET_HELLO_iterate_addresses (hello, 
381                                   GNUNET_NO, 
382                                   &count_address, 
383                                   pc);
384   if (0 == pc->off)
385   {
386     dump_pc (pc);
387     return;
388   }
389   pc->address_list_size = pc->off;
390   pc->address_list = GNUNET_malloc (sizeof (struct AddressRecord) * pc->off);
391   GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, 
392                                   &print_address, pc);
393 }
394
395
396 /* ************************* GET URI ************************** */
397
398
399 /**
400  * Function that is called on each address of this peer.
401  * Expands the corresponding URI string.
402  *
403  * @param cls the 'GetUriContext'
404  * @param address address to add
405  * @param expiration expiration time for the address
406  * @return GNUNET_OK (continue iteration).
407  */
408 static int
409 compose_uri (void *cls, const struct GNUNET_HELLO_Address *address,
410              struct GNUNET_TIME_Absolute expiration)
411 {
412   struct GetUriContext *guc = cls;
413   struct GNUNET_TRANSPORT_PluginFunctions *papi;
414   const char *addr;
415   char *ret;
416   char tbuf[16];
417   struct tm *t;
418   time_t seconds;
419
420   papi = GPI_plugins_find (address->transport_name);
421   if (papi == NULL)
422   {
423     /* Not an error - we might just not have the right plugin. */
424     return GNUNET_OK;
425   }
426   if (NULL == papi->address_to_string)
427   {
428     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
429                 "URI conversion not implemented for plugin `%s'\n",
430                 address->transport_name);
431     return GNUNET_OK;
432   }
433   addr = papi->address_to_string (papi->cls, address->address, address->address_length);
434   if ( (addr == NULL) || (strlen(addr) == 0) )
435     return GNUNET_OK;
436   seconds = expiration.abs_value / 1000;
437   t = gmtime (&seconds);
438   GNUNET_assert (0 != strftime (tbuf, sizeof (tbuf),
439                                 "%Y%m%d%H%M%S",
440                                 t));
441   GNUNET_asprintf (&ret,
442                    "%s!%s!%s!%s",
443                    guc->uri,
444                    tbuf,
445                    address->transport_name, 
446                    addr);
447   GNUNET_free (guc->uri);
448   guc->uri = ret;
449   return GNUNET_OK;
450 }
451
452
453 /**
454  * Print URI of the peer.
455  *
456  * @param cls the 'struct GetUriContext'
457  * @param peer identity of the peer (unused)
458  * @param hello addresses of the peer
459  * @param err_msg error message
460  */
461 static void
462 print_my_uri (void *cls, const struct GNUNET_PeerIdentity *peer,
463               const struct GNUNET_HELLO_Message *hello, 
464               const char *err_msg)
465 {
466   struct GetUriContext *guc = cls;
467
468   if (peer == NULL)
469   {
470     pic = NULL;
471     if (err_msg != NULL)
472       FPRINTF (stderr,
473                _("Error in communication with PEERINFO service: %s\n"), 
474                err_msg);
475     GNUNET_free_non_null (guc->uri);
476     GNUNET_free (guc);  
477     tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
478     return;
479   } 
480   if (NULL != hello)
481     GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &compose_uri, guc);   
482   printf ("%s\n", (const char *) guc->uri);
483 }
484
485
486 /* ************************* import HELLO by URI ********************* */
487
488
489 /**
490  * We're building a HELLO.  Parse the next address from the
491  * parsing context and append it.
492  *
493  * @param cls the 'struct GNUNET_PEERINFO_HelloAddressParsingContext'
494  * @param max number of bytes available for HELLO construction
495  * @param buffer where to copy the next address (in binary format)
496  * @return number of bytes added to buffer
497  */ 
498 static size_t
499 add_address_to_hello (void *cls, size_t max, void *buffer)
500 {
501   struct GNUNET_PEERINFO_HelloAddressParsingContext *ctx = cls;
502   const char *tname;
503   const char *address;
504   const char *end;
505   char *plugin_name;
506   struct tm expiration_time;
507   time_t expiration_seconds;
508   struct GNUNET_TIME_Absolute expire;
509   struct GNUNET_TRANSPORT_PluginFunctions *papi;
510   void *addr;
511   size_t addr_len;
512   struct GNUNET_HELLO_Address haddr;
513   size_t ret;
514
515   if (NULL == ctx->pos)
516     return 0;
517   if ('!' != ctx->pos[0])
518   {
519     ctx->ret = GNUNET_SYSERR;
520     return 0;
521   }
522   ctx->pos++;
523   memset (&expiration_time, 0, sizeof (expiration_time));
524   tname = strptime (ctx->pos,
525                     "%Y%m%d%H%M%S",
526                     &expiration_time);
527   if (NULL == tname)
528   {
529     ctx->ret = GNUNET_SYSERR;
530     return 0;
531   }
532   expiration_seconds = mktime (&expiration_time);
533   if (expiration_seconds == (time_t) -1)
534   {
535     ctx->ret = GNUNET_SYSERR;
536     return 0;
537   }
538   expire.abs_value = expiration_seconds * 1000;
539   if ('!' != tname[0])
540   {
541     ctx->ret = GNUNET_SYSERR;
542     return 0;
543   }
544   tname++;
545   address = strchr (tname, (int) '!');
546   if (NULL == address)
547   {
548     ctx->ret = GNUNET_SYSERR;
549     return 0;
550   }
551   address++;
552   end = strchr (address, (int) '!');
553   if (NULL == end)
554   {
555     ctx->pos = NULL;
556     end = address + strlen (address);
557   }
558   else
559   {
560     ctx->pos = end;
561   }
562   plugin_name = GNUNET_strndup (tname, address - tname);  
563   papi = GPI_plugins_find (plugin_name);
564   if (NULL == papi)
565   {
566     /* Not an error - we might just not have the right plugin.
567      * Skip this part, advance to the next one and recurse.
568      * But only if this is not the end of string.
569      */
570     GNUNET_free (plugin_name);
571     return 0;
572   }
573   if (NULL == papi->string_to_address)
574   {
575     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
576                 _("Plugin `%s' does not support URIs yet\n"),
577                 ctx->pos);
578     GNUNET_free (plugin_name);
579     return 0;
580   }
581   if (GNUNET_OK !=
582       papi->string_to_address (papi->cls, 
583                                address,
584                                end - address,
585                                &addr,
586                                &addr_len))
587   {
588     GNUNET_free (plugin_name);  
589     return GNUNET_SYSERR;
590   }
591   /* address.peer is unset - not used by add_address() */
592   haddr.address_length = addr_len;
593   haddr.address = addr;
594   haddr.transport_name = plugin_name;
595   ret = GNUNET_HELLO_add_address (&haddr, expire, buffer, max);
596   GNUNET_free (addr);
597   GNUNET_free (plugin_name);
598   return ret;
599 }
600
601
602 /**
603  * Parse the PUT URI given at the command line and add it to our peerinfo 
604  * database.
605  *
606  * @param put_uri URI string to parse
607  * @return GNUNET_OK on success, GNUNET_SYSERR if the URI was invalid, GNUNET_NO on other errors
608  */
609 static int
610 parse_hello_uri (const char *put_uri)
611 {
612   const char *pks;
613   const char *exc;
614   struct GNUNET_HELLO_Message *hello;
615   struct GNUNET_PEERINFO_HelloAddressParsingContext ctx;
616
617   if (0 != strncmp (put_uri,
618                     HELLO_URI_PREFIX,
619                     strlen (HELLO_URI_PREFIX)))
620     return GNUNET_SYSERR;
621   pks = &put_uri[strlen (HELLO_URI_PREFIX)];
622   exc = strstr (pks, "!");
623   if (GNUNET_OK != GNUNET_STRINGS_string_to_data (pks,
624                                                   (NULL == exc) ? strlen (pks) : (exc - pks),
625                                                   (unsigned char *) &my_public_key, 
626                                                   sizeof (my_public_key)))
627     return GNUNET_SYSERR;
628   ctx.pos = exc;
629   ctx.ret = GNUNET_OK;
630   hello = GNUNET_HELLO_create (&my_public_key, &add_address_to_hello, &ctx);
631
632   if (NULL != hello)
633   {
634     /* WARNING: this adds the address from URI WITHOUT verification! */
635     if (GNUNET_OK == ctx.ret)
636       GNUNET_PEERINFO_add_peer (peerinfo, hello);
637     GNUNET_free (hello);
638   }
639
640   /* wait 1s to give peerinfo operation a chance to succeed */
641   /* FIXME: current peerinfo API sucks to require this; not to mention
642      that we get no feedback to determine if the operation actually succeeded */
643   tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
644                                      &state_machine, NULL);
645   return ctx.ret;
646 }
647
648
649 /* ************************ Main state machine ********************* */
650
651
652 /**
653  * Main state machine that goes over all options and
654  * runs the next requested function.
655  *
656  * @param cls unused
657  * @param tc scheduler context
658  */
659 static void
660 shutdown_task (void *cls,
661                const struct GNUNET_SCHEDULER_TaskContext *tc)
662 {
663   struct PrintContext *pc;
664   struct AddressRecord *ar;
665   unsigned int i;
666
667   if (GNUNET_SCHEDULER_NO_TASK != tt)
668   {
669     GNUNET_SCHEDULER_cancel (tt);
670     tt = GNUNET_SCHEDULER_NO_TASK;
671   }
672   if (NULL != pic)
673   {
674     GNUNET_PEERINFO_iterate_cancel (pic);
675     pic = NULL;
676   }
677   while (NULL != (pc = pc_head))
678   {
679     GNUNET_CONTAINER_DLL_remove (pc_head,
680                                  pc_tail,
681                                  pc);
682     for (i=0;i<pc->address_list_size;i++)
683     {
684       ar = &pc->address_list[i];
685       GNUNET_free_non_null (ar->result);
686       if (NULL != ar->atsc)
687       {
688         GNUNET_TRANSPORT_address_to_string_cancel (ar->atsc);
689         ar->atsc = NULL;
690       }
691     }
692     GNUNET_free_non_null (pc->address_list);
693     GNUNET_free (pc);
694   }
695   GPI_plugins_unload ();
696   if (NULL != peerinfo)
697   {
698     GNUNET_PEERINFO_disconnect (peerinfo);
699     peerinfo = NULL;
700   }
701 }
702
703
704 /**
705  * Main function that will be run by the scheduler.
706  *
707  * @param cls closure
708  * @param args remaining command-line arguments
709  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
710  * @param c configuration
711  */
712 static void
713 run (void *cls, char *const *args, const char *cfgfile,
714      const struct GNUNET_CONFIGURATION_Handle *c)
715 {
716   struct GNUNET_CRYPTO_RsaPrivateKey *priv;
717   char *fn;
718
719   cfg = c;
720   if (args[0] != NULL)
721   {
722     FPRINTF (stderr, _("Invalid command line argument `%s'\n"), args[0]);
723     return;
724   }
725   peerinfo = GNUNET_PEERINFO_connect (cfg);
726   if (peerinfo == NULL)
727   {
728     FPRINTF (stderr, "%s",  _("Could not access PEERINFO service.  Exiting.\n"));
729     return;
730   }
731   if ( (GNUNET_YES == get_self) || (GNUNET_YES == get_uri) )
732   {
733     /* load private key */
734     if (GNUNET_OK !=
735         GNUNET_CONFIGURATION_get_value_filename (cfg, "GNUNETD", "HOSTKEY",
736                                                  &fn))
737     {
738       FPRINTF (stderr, _("Could not find option `%s:%s' in configuration.\n"),
739                "GNUNETD", "HOSTKEYFILE");
740       return;
741     }
742
743     if (NULL == (priv = GNUNET_CRYPTO_rsa_key_create_from_file (fn)))
744     {
745       FPRINTF (stderr, _("Loading hostkey from `%s' failed.\n"), fn);
746       GNUNET_free (fn);
747       return;
748     }
749     GNUNET_free (fn);
750     GNUNET_CRYPTO_rsa_key_get_public (priv, &my_public_key);
751     GNUNET_CRYPTO_rsa_key_free (priv);
752     GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key), &my_peer_identity.hashPubKey);
753   }
754
755   tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
756   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
757                                 &shutdown_task,
758                                 NULL);
759 }
760
761
762 /**
763  * Main state machine that goes over all options and
764  * runs the next requested function.
765  *
766  * @param cls unused
767  * @param tc scheduler context
768  */
769 static void
770 state_machine (void *cls,
771                const struct GNUNET_SCHEDULER_TaskContext *tc)
772 {
773   tt = GNUNET_SCHEDULER_NO_TASK;
774
775   if (NULL != put_uri)
776   {
777     GPI_plugins_load (cfg);
778     if (GNUNET_SYSERR == parse_hello_uri (put_uri))
779       fprintf (stderr,
780                _("Invalid URI `%s'\n"),
781                put_uri);    
782     GNUNET_free (put_uri);
783     put_uri = NULL;
784     return;
785   }
786   if (GNUNET_YES == get_info)
787   {
788     get_info = GNUNET_NO;
789     GPI_plugins_load (cfg);
790     pic = GNUNET_PEERINFO_iterate (peerinfo, NULL,
791                                    TIMEOUT,
792                                    &print_peer_info, NULL);
793     return;
794   }
795   if (GNUNET_YES == get_self)
796   {
797     struct GNUNET_CRYPTO_HashAsciiEncoded enc;
798
799     get_self = GNUNET_NO;
800     GNUNET_CRYPTO_hash_to_enc (&my_peer_identity.hashPubKey, &enc);
801     if (be_quiet)
802       printf ("%s\n", (char *) &enc);
803     else
804       printf (_("I am peer `%s'.\n"), (const char *) &enc);
805   }
806   if (GNUNET_YES == get_uri)
807   {
808     struct GetUriContext *guc;
809     char *pkey;
810
811     guc = GNUNET_malloc (sizeof (struct GetUriContext));
812     pkey = GNUNET_CRYPTO_rsa_public_key_to_string (&my_public_key);
813     GNUNET_asprintf (&guc->uri,
814                      "%s%s",
815                      HELLO_URI_PREFIX,
816                      pkey);
817     GNUNET_free (pkey);
818     GPI_plugins_load (cfg);
819     pic = GNUNET_PEERINFO_iterate (peerinfo, &my_peer_identity,
820                                    TIMEOUT,
821                                    &print_my_uri, guc);
822     get_uri = GNUNET_NO;
823     return;
824   }
825   GNUNET_SCHEDULER_shutdown ();
826 }
827
828
829 /**
830  * The main function to obtain peer information.
831  *
832  * @param argc number of arguments from the command line
833  * @param argv command line arguments
834  * @return 0 ok, 1 on error
835  */
836 int
837 main (int argc, char *const *argv)
838 {
839   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
840     {'n', "numeric", NULL,
841      gettext_noop ("don't resolve host names"),
842      0, &GNUNET_GETOPT_set_one, &no_resolve},
843     {'q', "quiet", NULL,
844      gettext_noop ("output only the identity strings"),
845      0, &GNUNET_GETOPT_set_one, &be_quiet},
846     {'s', "self", NULL,
847      gettext_noop ("output our own identity only"),
848      0, &GNUNET_GETOPT_set_one, &get_self},
849     {'i', "info", NULL,
850      gettext_noop ("list all known peers"),
851      0, &GNUNET_GETOPT_set_one, &get_info},
852     {'g', "get-hello", NULL,
853      gettext_noop ("also output HELLO uri(s)"),
854      0, &GNUNET_GETOPT_set_one, &get_uri},
855     {'p', "put-hello", "HELLO",
856      gettext_noop ("add given HELLO uri to the database"),
857      1, &GNUNET_GETOPT_set_string, &put_uri},
858     GNUNET_GETOPT_OPTION_END
859   };
860   return (GNUNET_OK ==
861           GNUNET_PROGRAM_run (argc, argv, "gnunet-peerinfo",
862                               gettext_noop ("Print information about peers."),
863                               options, &run, NULL)) ? 0 : 1;
864 }
865
866 /* end of gnunet-peerinfo.c */