fix
[oweals/gnunet.git] / src / peerinfo / gnunet-peerinfo.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2003, 2004, 2006, 2009 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/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_program_lib.h"
32
33 static int no_resolve;
34
35 static int be_quiet;
36
37 static int get_self;
38
39 /**
40  * Print information about the peer.
41  * Currently prints the GNUNET_PeerIdentity, trust and the IP.
42  * Could of course do more (e.g. resolve via DNS).
43  */
44 static void
45 print_peer_info (void *cls,
46                  const struct GNUNET_PeerIdentity *peer,
47                  const struct GNUNET_HELLO_Message *hello, uint32_t trust)
48 {
49   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
50
51   /* FIXME: add printing of address information!
52      => need extended transport API! */
53   GNUNET_CRYPTO_hash_to_enc (&peer->hashPubKey, &enc);
54   if (be_quiet)
55     printf ("%s\n", (const char *) &enc);
56   else
57     printf (_("Peer `%s' with trust %8u\n"), (const char *) &enc, trust);
58 }
59
60 /**
61  * Main function that will be run by the scheduler.
62  *
63  * @param cls closure
64  * @param sched the scheduler to use
65  * @param args remaining command-line arguments
66  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
67  * @param cfg configuration
68  */
69 static void
70 run (void *cls,
71      struct GNUNET_SCHEDULER_Handle *sched,
72      char *const *args,
73      const char *cfgfile, struct GNUNET_CONFIGURATION_Handle *cfg)
74 {
75   struct GNUNET_CRYPTO_RsaPrivateKey *priv;
76   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
77   struct GNUNET_PeerIdentity pid;
78   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
79   char *fn;
80
81   if (get_self != GNUNET_YES)
82     {
83       GNUNET_PEERINFO_for_all (cfg,
84                                sched,
85                                NULL,
86                                0,
87                                GNUNET_TIME_relative_multiply
88                                (GNUNET_TIME_UNIT_SECONDS, 30),
89                                &print_peer_info, NULL);
90     }
91   else
92     {
93       if (GNUNET_OK !=
94           GNUNET_CONFIGURATION_get_value_filename (cfg,
95                                                    "GNUNET",
96                                                    "HOSTKEYFILE", &fn))
97         return;
98       priv = GNUNET_CRYPTO_rsa_key_create_from_file (fn);
99       if (priv == NULL)
100         {
101           fprintf (stderr, _("Loading hostkey from `%s' failed.\n"), fn);
102           GNUNET_free (fn);
103           return;
104         }
105       GNUNET_free (fn);
106       GNUNET_CRYPTO_rsa_key_get_public (priv, &pub);
107       GNUNET_CRYPTO_rsa_key_free (priv);
108       GNUNET_CRYPTO_hash (&pub, sizeof (pub), &pid.hashPubKey);
109       GNUNET_CRYPTO_hash_to_enc (&pid.hashPubKey, &enc);
110       if (be_quiet)
111         printf ("%s\n", (char *) &enc);
112       else
113         printf (_("I am peer `%s'.\n"), (const char *) &enc);
114     }
115 }
116
117
118 /**
119  * gnunet-peerinfo command line options
120  */
121 static struct GNUNET_GETOPT_CommandLineOption options[] = {
122   {'n', "numeric", NULL,
123    gettext_noop ("don't resolve host names"),
124    0, &GNUNET_GETOPT_set_one, &no_resolve},
125   {'q', "quiet", NULL,
126    gettext_noop ("output only the identity strings"),
127    0, &GNUNET_GETOPT_set_one, &be_quiet},
128   {'s', "self", NULL,
129    gettext_noop ("output our own identity only"),
130    0, &GNUNET_GETOPT_set_one, &get_self},
131   GNUNET_GETOPT_OPTION_END
132 };
133
134 /**
135  * The main function to obtain peer information.
136  *
137  * @param argc number of arguments from the command line
138  * @param argv command line arguments
139  * @return 0 ok, 1 on error
140  */
141 int
142 main (int argc, char *const *argv)
143 {
144   return (GNUNET_OK ==
145           GNUNET_PROGRAM_run (argc,
146                               argv,
147                               "gnunet-peerinfo",
148                               gettext_noop ("Print information about peers."),
149                               options, &run, NULL)) ? 0 : 1;
150 }
151
152 /* end of gnunet-peerinfo.c */