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