temporary hack for gnunet-arm. given enough time processes seem to go away, so give...
[oweals/gnunet.git] / src / peerinfo-tool / gnunet-peerinfo.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2003, 2004, 2006, 2009, 2010 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 2, 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
34 static int no_resolve;
35
36 static int be_quiet;
37
38 static int get_self;
39
40 static struct GNUNET_SCHEDULER_Handle *sched;
41
42 static struct GNUNET_PEERINFO_Handle *peerinfo;
43
44 static const struct GNUNET_CONFIGURATION_Handle *cfg;
45
46 struct PrintContext
47 {
48   struct GNUNET_PeerIdentity peer;
49   char **address_list;
50   unsigned int num_addresses;
51   uint32_t off;
52   uint32_t trust;
53 };
54
55
56 static void
57 dump_pc (struct PrintContext *pc)
58 {
59   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
60   unsigned int i;
61
62   GNUNET_CRYPTO_hash_to_enc (&pc->peer.hashPubKey, &enc);
63   printf (_("Peer `%s' with trust %8u\n"), 
64           (const char *) &enc,
65           pc->trust);
66   for (i=0;i<pc->num_addresses;i++)
67     {
68       printf ("\t%s\n",
69               pc->address_list[i]);
70       GNUNET_free (pc->address_list[i]);
71     }
72   printf ("\n");
73   GNUNET_array_grow (pc->address_list,
74                      pc->num_addresses,
75                      0);
76   GNUNET_free (pc);
77 }
78
79
80 /**
81  * Function to call with a human-readable format of an address
82  *
83  * @param cls closure
84  * @param address NULL on error, otherwise 0-terminated printable UTF-8 string
85  */
86 static void
87 process_resolved_address (void *cls,
88                           const char *address)
89 {
90   struct PrintContext *pc = cls;
91
92   if (address == NULL)
93     {
94       pc->off--;
95       if (pc->off == 0)
96         dump_pc (pc);
97       return;
98     }
99   GNUNET_array_append (pc->address_list,
100                        pc->num_addresses,
101                        GNUNET_strdup (address));
102 }
103
104
105 /**
106  * Iterator callback to go over all addresses.
107  *
108  * @param cls closure
109  * @param tname name of the transport
110  * @param expiration expiration time
111  * @param addr the address
112  * @param addrlen length of the address
113  * @return GNUNET_OK to keep the address and continue
114  */
115 static int
116 count_address (void *cls,
117                const char *tname,
118                struct GNUNET_TIME_Absolute expiration,
119                const void *addr, uint16_t addrlen)
120 {
121   struct PrintContext *pc = cls;
122   pc->off++;
123   return GNUNET_OK;
124 }
125
126
127 /**
128  * Iterator callback to go over all addresses.
129  *
130  * @param cls closure
131  * @param tname name of the transport
132  * @param expiration expiration time
133  * @param addr the address
134  * @param addrlen length of the address
135  * @return GNUNET_OK to keep the address and continue
136  */
137 static int
138 print_address (void *cls,
139                const char *tname,
140                struct GNUNET_TIME_Absolute expiration,
141                const void *addr, uint16_t addrlen)
142 {
143   struct PrintContext *pc = cls;
144   GNUNET_TRANSPORT_address_lookup (sched,
145                                    cfg,
146                                    addr,
147                                    addrlen,
148                                    no_resolve,
149                                    tname,
150                                    GNUNET_TIME_UNIT_SECONDS,
151                                    &process_resolved_address,
152                                    pc);
153   return GNUNET_OK;
154 }
155
156
157 /**
158  * Print information about the peer.
159  * Currently prints the GNUNET_PeerIdentity, trust and the IP.
160  * Could of course do more (e.g. resolve via DNS).
161  */
162 static void
163 print_peer_info (void *cls,
164                  const struct GNUNET_PeerIdentity *peer,
165                  const struct GNUNET_HELLO_Message *hello, uint32_t trust)
166 {
167   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
168   struct PrintContext *pc;
169
170   if (peer == NULL)    
171     {
172       GNUNET_PEERINFO_disconnect (peerinfo);
173       switch (trust)
174         {
175         case 0:
176           break;
177         case 1:
178           fprintf (stderr,
179                    _("Timeout trying to interact with PEERINFO service\n"));
180           break;
181         case 2:
182           fprintf (stderr,
183                    _("Error in communication with PEERINFO service\n"));
184           break;
185         default:
186           GNUNET_break (0);
187           break;
188         }
189       return;    
190     }
191   if (be_quiet)
192     {
193       GNUNET_CRYPTO_hash_to_enc (&peer->hashPubKey, &enc);
194       printf ("%s\n", (const char *) &enc);
195       return;
196     }
197   pc = GNUNET_malloc (sizeof (struct PrintContext));
198   pc->peer = *peer;  
199   pc->trust = trust;
200   GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &count_address, pc);
201   if (0 == pc->off)
202     {
203       dump_pc (pc);
204       return;
205     }
206   GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &print_address, pc);
207 }
208
209
210 /**
211  * Main function that will be run by the scheduler.
212  *
213  * @param cls closure
214  * @param s the scheduler to use
215  * @param args remaining command-line arguments
216  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
217  * @param c configuration
218  */
219 static void
220 run (void *cls,
221      struct GNUNET_SCHEDULER_Handle *s,
222      char *const *args,
223      const char *cfgfile, 
224      const struct GNUNET_CONFIGURATION_Handle *c)
225 {
226   struct GNUNET_CRYPTO_RsaPrivateKey *priv;
227   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
228   struct GNUNET_PeerIdentity pid;
229   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
230   char *fn;
231   int delta;
232
233   sched = s;
234   cfg = c;
235   delta = 0;
236   if ( (args[0] != NULL) &&
237        (args[1] != NULL) &&
238        (1 == sscanf(args[0], "%d", &delta)) &&
239        (GNUNET_OK == 
240         GNUNET_CRYPTO_hash_from_string (args[1],
241                                         &pid.hashPubKey)) )
242     {
243       peerinfo = GNUNET_PEERINFO_connect (sched, cfg);
244       GNUNET_PEERINFO_iterate (peerinfo,
245                                &pid,
246                                delta,
247                                GNUNET_TIME_UNIT_SECONDS,
248                                &print_peer_info, NULL);                            
249       return;
250     }
251   else if (args[0] != NULL)
252     {
253       fprintf (stderr,
254                _("Invalid command line argument `%s'\n"),
255                args[0]);
256       return;    
257     }
258   if (get_self != GNUNET_YES)
259     {
260       peerinfo = GNUNET_PEERINFO_connect (sched, cfg);
261       if (peerinfo == NULL)
262         {
263           fprintf (stderr,
264                    _("Could not access PEERINFO service.  Exiting.\n"));
265           return;
266         }
267       (void) GNUNET_PEERINFO_iterate (peerinfo,
268                                       NULL,
269                                       0,
270                                       GNUNET_TIME_relative_multiply
271                                       (GNUNET_TIME_UNIT_SECONDS, 2),
272                                       &print_peer_info, NULL);
273     }
274   else
275     {
276       if (GNUNET_OK !=
277           GNUNET_CONFIGURATION_get_value_filename (cfg,
278                                                    "GNUNETD",
279                                                    "HOSTKEY", &fn))
280         {
281           fprintf (stderr, 
282                    _("Could not find option `%s:%s' in configuration.\n"), 
283                    "GNUNETD",
284                    "HOSTKEYFILE");
285           return;
286         }
287       priv = GNUNET_CRYPTO_rsa_key_create_from_file (fn);
288       if (priv == NULL)
289         {
290           fprintf (stderr, _("Loading hostkey from `%s' failed.\n"), fn);
291           GNUNET_free (fn);
292           return;
293         }
294       GNUNET_free (fn);
295       GNUNET_CRYPTO_rsa_key_get_public (priv, &pub);
296       GNUNET_CRYPTO_rsa_key_free (priv);
297       GNUNET_CRYPTO_hash (&pub, sizeof (pub), &pid.hashPubKey);
298       GNUNET_CRYPTO_hash_to_enc (&pid.hashPubKey, &enc);
299       if (be_quiet)
300         printf ("%s\n", (char *) &enc);
301       else
302         printf (_("I am peer `%s'.\n"), (const char *) &enc);
303     }
304 }
305
306
307 /**
308  * gnunet-peerinfo command line options
309  */
310 static struct GNUNET_GETOPT_CommandLineOption options[] = {
311   {'n', "numeric", NULL,
312    gettext_noop ("don't resolve host names"),
313    0, &GNUNET_GETOPT_set_one, &no_resolve},
314   {'q', "quiet", NULL,
315    gettext_noop ("output only the identity strings"),
316    0, &GNUNET_GETOPT_set_one, &be_quiet},
317   {'s', "self", NULL,
318    gettext_noop ("output our own identity only"),
319    0, &GNUNET_GETOPT_set_one, &get_self},
320   GNUNET_GETOPT_OPTION_END
321 };
322
323 /**
324  * The main function to obtain peer information.
325  *
326  * @param argc number of arguments from the command line
327  * @param argv command line arguments
328  * @return 0 ok, 1 on error
329  */
330 int
331 main (int argc, char *const *argv)
332 {
333   return (GNUNET_OK ==
334           GNUNET_PROGRAM_run (argc,
335                               argv,
336                               "gnunet-peerinfo",
337                               gettext_noop ("Print information about peers."),
338                               options, &run, NULL)) ? 0 : 1;
339 }
340
341 /* end of gnunet-peerinfo.c */