rounding
[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 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   uint32_t trust;
51 };
52
53
54 static void
55 dump_pc (struct PrintContext *pc)
56 {
57   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
58   unsigned int i;
59
60   GNUNET_CRYPTO_hash_to_enc (&pc->peer.hashPubKey, &enc);
61   printf (_("Peer `%s' with trust %8u\n"), 
62           (const char *) &enc,
63           pc->trust);
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, size_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, size_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, trust 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, uint32_t trust)
164 {
165   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
166   struct PrintContext *pc;
167
168   if (peer == NULL)    
169     return;    
170   if (be_quiet)
171     {
172       GNUNET_CRYPTO_hash_to_enc (&peer->hashPubKey, &enc);
173       printf ("%s\n", (const char *) &enc);
174       return;
175     }
176   pc = GNUNET_malloc (sizeof (struct PrintContext));
177   pc->peer = *peer;  
178   pc->trust = trust;
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 s the scheduler to use
194  * @param args remaining command-line arguments
195  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
196  * @param c configuration
197  */
198 static void
199 run (void *cls,
200      struct GNUNET_SCHEDULER_Handle *s,
201      char *const *args,
202      const char *cfgfile, 
203      const struct GNUNET_CONFIGURATION_Handle *c)
204 {
205   struct GNUNET_CRYPTO_RsaPrivateKey *priv;
206   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
207   struct GNUNET_PeerIdentity pid;
208   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
209   char *fn;
210
211   sched = s;
212   cfg = c;
213   if (get_self != GNUNET_YES)
214     {
215       (void) GNUNET_PEERINFO_iterate (cfg,
216                                       sched,
217                                       NULL,
218                                       0,
219                                       GNUNET_TIME_relative_multiply
220                                       (GNUNET_TIME_UNIT_SECONDS, 2),
221                                       &print_peer_info, NULL);
222     }
223   else
224     {
225       if (GNUNET_OK !=
226           GNUNET_CONFIGURATION_get_value_filename (cfg,
227                                                    "GNUNETD",
228                                                    "HOSTKEY", &fn))
229         {
230           fprintf (stderr, 
231                    _("Could not find option `%s:%s' in configuration.\n"), 
232                    "GNUNETD",
233                    "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  * gnunet-peerinfo command line options
258  */
259 static struct GNUNET_GETOPT_CommandLineOption options[] = {
260   {'n', "numeric", NULL,
261    gettext_noop ("don't resolve host names"),
262    0, &GNUNET_GETOPT_set_one, &no_resolve},
263   {'q', "quiet", NULL,
264    gettext_noop ("output only the identity strings"),
265    0, &GNUNET_GETOPT_set_one, &be_quiet},
266   {'s', "self", NULL,
267    gettext_noop ("output our own identity only"),
268    0, &GNUNET_GETOPT_set_one, &get_self},
269   GNUNET_GETOPT_OPTION_END
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   return (GNUNET_OK ==
283           GNUNET_PROGRAM_run (argc,
284                               argv,
285                               "gnunet-peerinfo",
286                               gettext_noop ("Print information about peers."),
287                               options, &run, NULL)) ? 0 : 1;
288 }
289
290 /* end of gnunet-peerinfo.c */