moving gnunet-peerinfo.c to allow it to use transport_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 const struct GNUNET_CONFIGURATION_Handle *cfg;
43
44 #define FIXME 0
45
46 #if FIXME
47 /**
48  * Function to call with a human-readable format of an address
49  *
50  * @param cls closure
51  * @param address NULL on error, otherwise 0-terminated printable UTF-8 string
52  */
53 static void
54 print_resolved_address (void *cls,
55                         const char *address)
56 {
57   /* FIXME: need to buffer output from all requests and print it at
58      once, otherwise we mix results... */
59   if (address == NULL)
60     {
61       return;
62     }
63   fprintf (stderr, " %s\n", address);
64 }
65 #endif
66
67 /**
68  * Iterator callback to go over all addresses.
69  *
70  * @param cls closure
71  * @param tname name of the transport
72  * @param expiration expiration time
73  * @param addr the address
74  * @param addrlen length of the address
75  * @return GNUNET_OK to keep the address and continue
76  */
77 static int
78 print_address (void *cls,
79                const char *tname,
80                struct GNUNET_TIME_Absolute expiration,
81                const void *addr, size_t addrlen)
82 {
83 #if FIXME
84   GNUNET_TRANSPORT_address_lookup (sched,
85                                    cfg,
86                                    addr,
87                                    addrlen,
88                                    no_resolve,
89                                    tname,
90                                    GNUNET_TIME_UNIT_SECONDS,
91                                    &print_resolved_address,
92                                    NULL);
93 #endif
94   return GNUNET_OK;
95 }
96
97
98 /**
99  * Print information about the peer.
100  * Currently prints the GNUNET_PeerIdentity, trust and the IP.
101  * Could of course do more (e.g. resolve via DNS).
102  */
103 static void
104 print_peer_info (void *cls,
105                  const struct GNUNET_PeerIdentity *peer,
106                  const struct GNUNET_HELLO_Message *hello, uint32_t trust)
107 {
108   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
109
110   if (peer == NULL)    
111     return;    
112   GNUNET_CRYPTO_hash_to_enc (&peer->hashPubKey, &enc);
113   if (be_quiet)
114     {
115       printf ("%s\n", (const char *) &enc);
116       return;
117     }
118   printf (_("Peer `%s' with trust %8u\n"), (const char *) &enc, trust);
119   GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &print_address, NULL);
120 }
121
122 /**
123  * Main function that will be run by the scheduler.
124  *
125  * @param cls closure
126  * @param s the scheduler to use
127  * @param args remaining command-line arguments
128  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
129  * @param c configuration
130  */
131 static void
132 run (void *cls,
133      struct GNUNET_SCHEDULER_Handle *s,
134      char *const *args,
135      const char *cfgfile, 
136      const struct GNUNET_CONFIGURATION_Handle *c)
137 {
138   struct GNUNET_CRYPTO_RsaPrivateKey *priv;
139   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
140   struct GNUNET_PeerIdentity pid;
141   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
142   char *fn;
143
144   sched = s;
145   cfg = c;
146   if (get_self != GNUNET_YES)
147     {
148       (void) GNUNET_PEERINFO_iterate (cfg,
149                                       sched,
150                                       NULL,
151                                       0,
152                                       GNUNET_TIME_relative_multiply
153                                       (GNUNET_TIME_UNIT_SECONDS, 2),
154                                       &print_peer_info, NULL);
155     }
156   else
157     {
158       if (GNUNET_OK !=
159           GNUNET_CONFIGURATION_get_value_filename (cfg,
160                                                    "GNUNETD",
161                                                    "HOSTKEY", &fn))
162         {
163           fprintf (stderr, 
164                    _("Could not find option `%s:%s' in configuration.\n"), 
165                    "GNUNETD",
166                    "HOSTKEYFILE");
167           return;
168         }
169       priv = GNUNET_CRYPTO_rsa_key_create_from_file (fn);
170       if (priv == NULL)
171         {
172           fprintf (stderr, _("Loading hostkey from `%s' failed.\n"), fn);
173           GNUNET_free (fn);
174           return;
175         }
176       GNUNET_free (fn);
177       GNUNET_CRYPTO_rsa_key_get_public (priv, &pub);
178       GNUNET_CRYPTO_rsa_key_free (priv);
179       GNUNET_CRYPTO_hash (&pub, sizeof (pub), &pid.hashPubKey);
180       GNUNET_CRYPTO_hash_to_enc (&pid.hashPubKey, &enc);
181       if (be_quiet)
182         printf ("%s\n", (char *) &enc);
183       else
184         printf (_("I am peer `%s'.\n"), (const char *) &enc);
185     }
186 }
187
188
189 /**
190  * gnunet-peerinfo command line options
191  */
192 static struct GNUNET_GETOPT_CommandLineOption options[] = {
193   {'n', "numeric", NULL,
194    gettext_noop ("don't resolve host names"),
195    0, &GNUNET_GETOPT_set_one, &no_resolve},
196   {'q', "quiet", NULL,
197    gettext_noop ("output only the identity strings"),
198    0, &GNUNET_GETOPT_set_one, &be_quiet},
199   {'s', "self", NULL,
200    gettext_noop ("output our own identity only"),
201    0, &GNUNET_GETOPT_set_one, &get_self},
202   GNUNET_GETOPT_OPTION_END
203 };
204
205 /**
206  * The main function to obtain peer information.
207  *
208  * @param argc number of arguments from the command line
209  * @param argv command line arguments
210  * @return 0 ok, 1 on error
211  */
212 int
213 main (int argc, char *const *argv)
214 {
215   return (GNUNET_OK ==
216           GNUNET_PROGRAM_run (argc,
217                               argv,
218                               "gnunet-peerinfo",
219                               gettext_noop ("Print information about peers."),
220                               options, &run, NULL)) ? 0 : 1;
221 }
222
223 /* end of gnunet-peerinfo.c */