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