activating new peerinfo API
[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, size_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, size_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       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   pc->trust = trust;
184   GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &count_address, pc);
185   if (0 == pc->off)
186     {
187       dump_pc (pc);
188       return;
189     }
190   GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &print_address, pc);
191 }
192
193
194 /**
195  * Main function that will be run by the scheduler.
196  *
197  * @param cls closure
198  * @param s the scheduler to use
199  * @param args remaining command-line arguments
200  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
201  * @param c configuration
202  */
203 static void
204 run (void *cls,
205      struct GNUNET_SCHEDULER_Handle *s,
206      char *const *args,
207      const char *cfgfile, 
208      const struct GNUNET_CONFIGURATION_Handle *c)
209 {
210   struct GNUNET_CRYPTO_RsaPrivateKey *priv;
211   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
212   struct GNUNET_PeerIdentity pid;
213   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
214   char *fn;
215
216   sched = s;
217   cfg = c;
218   if (get_self != GNUNET_YES)
219     {
220       peerinfo = GNUNET_PEERINFO_connect (sched, cfg);
221       if (peerinfo == NULL)
222         {
223           fprintf (stderr,
224                    _("Could not access PEERINFO service.  Exiting.\n"));
225           return;
226         }
227       (void) GNUNET_PEERINFO_iterate (peerinfo,
228                                       NULL,
229                                       0,
230                                       GNUNET_TIME_relative_multiply
231                                       (GNUNET_TIME_UNIT_SECONDS, 2),
232                                       &print_peer_info, NULL);
233     }
234   else
235     {
236       if (GNUNET_OK !=
237           GNUNET_CONFIGURATION_get_value_filename (cfg,
238                                                    "GNUNETD",
239                                                    "HOSTKEY", &fn))
240         {
241           fprintf (stderr, 
242                    _("Could not find option `%s:%s' in configuration.\n"), 
243                    "GNUNETD",
244                    "HOSTKEYFILE");
245           return;
246         }
247       priv = GNUNET_CRYPTO_rsa_key_create_from_file (fn);
248       if (priv == NULL)
249         {
250           fprintf (stderr, _("Loading hostkey from `%s' failed.\n"), fn);
251           GNUNET_free (fn);
252           return;
253         }
254       GNUNET_free (fn);
255       GNUNET_CRYPTO_rsa_key_get_public (priv, &pub);
256       GNUNET_CRYPTO_rsa_key_free (priv);
257       GNUNET_CRYPTO_hash (&pub, sizeof (pub), &pid.hashPubKey);
258       GNUNET_CRYPTO_hash_to_enc (&pid.hashPubKey, &enc);
259       if (be_quiet)
260         printf ("%s\n", (char *) &enc);
261       else
262         printf (_("I am peer `%s'.\n"), (const char *) &enc);
263     }
264 }
265
266
267 /**
268  * gnunet-peerinfo command line options
269  */
270 static struct GNUNET_GETOPT_CommandLineOption options[] = {
271   {'n', "numeric", NULL,
272    gettext_noop ("don't resolve host names"),
273    0, &GNUNET_GETOPT_set_one, &no_resolve},
274   {'q', "quiet", NULL,
275    gettext_noop ("output only the identity strings"),
276    0, &GNUNET_GETOPT_set_one, &be_quiet},
277   {'s', "self", NULL,
278    gettext_noop ("output our own identity only"),
279    0, &GNUNET_GETOPT_set_one, &get_self},
280   GNUNET_GETOPT_OPTION_END
281 };
282
283 /**
284  * The main function to obtain peer information.
285  *
286  * @param argc number of arguments from the command line
287  * @param argv command line arguments
288  * @return 0 ok, 1 on error
289  */
290 int
291 main (int argc, char *const *argv)
292 {
293   return (GNUNET_OK ==
294           GNUNET_PROGRAM_run (argc,
295                               argv,
296                               "gnunet-peerinfo",
297                               gettext_noop ("Print information about peers."),
298                               options, &run, NULL)) ? 0 : 1;
299 }
300
301 /* end of gnunet-peerinfo.c */