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