7e72be36620d69aaedcc5852198710f5679fe3f8
[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   if (peer == NULL)
54     {
55       return;    
56     }
57   GNUNET_CRYPTO_hash_to_enc (&peer->hashPubKey, &enc);
58   if (be_quiet)
59     printf ("%s\n", (const char *) &enc);
60   else
61     printf (_("Peer `%s' with trust %8u\n"), (const char *) &enc, trust);
62 }
63
64 /**
65  * Main function that will be run by the scheduler.
66  *
67  * @param cls closure
68  * @param sched the scheduler to use
69  * @param args remaining command-line arguments
70  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
71  * @param cfg configuration
72  */
73 static void
74 run (void *cls,
75      struct GNUNET_SCHEDULER_Handle *sched,
76      char *const *args,
77      const char *cfgfile, 
78      const struct GNUNET_CONFIGURATION_Handle *cfg)
79 {
80   struct GNUNET_CRYPTO_RsaPrivateKey *priv;
81   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
82   struct GNUNET_PeerIdentity pid;
83   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
84   char *fn;
85
86   if (get_self != GNUNET_YES)
87     {
88       (void) GNUNET_PEERINFO_iterate (cfg,
89                                       sched,
90                                       NULL,
91                                       0,
92                                       GNUNET_TIME_relative_multiply
93                                       (GNUNET_TIME_UNIT_SECONDS, 2),
94                                       &print_peer_info, NULL);
95     }
96   else
97     {
98       if (GNUNET_OK !=
99           GNUNET_CONFIGURATION_get_value_filename (cfg,
100                                                    "GNUNETD",
101                                                    "HOSTKEY", &fn))
102         {
103           fprintf (stderr, 
104                    _("Could not find option `%s:%s' in configuration.\n"), 
105                    "GNUNETD",
106                    "HOSTKEYFILE");
107           return;
108         }
109       priv = GNUNET_CRYPTO_rsa_key_create_from_file (fn);
110       if (priv == NULL)
111         {
112           fprintf (stderr, _("Loading hostkey from `%s' failed.\n"), fn);
113           GNUNET_free (fn);
114           return;
115         }
116       GNUNET_free (fn);
117       GNUNET_CRYPTO_rsa_key_get_public (priv, &pub);
118       GNUNET_CRYPTO_rsa_key_free (priv);
119       GNUNET_CRYPTO_hash (&pub, sizeof (pub), &pid.hashPubKey);
120       GNUNET_CRYPTO_hash_to_enc (&pid.hashPubKey, &enc);
121       if (be_quiet)
122         printf ("%s\n", (char *) &enc);
123       else
124         printf (_("I am peer `%s'.\n"), (const char *) &enc);
125     }
126 }
127
128
129 /**
130  * gnunet-peerinfo command line options
131  */
132 static struct GNUNET_GETOPT_CommandLineOption options[] = {
133   {'n', "numeric", NULL,
134    gettext_noop ("don't resolve host names"),
135    0, &GNUNET_GETOPT_set_one, &no_resolve},
136   {'q', "quiet", NULL,
137    gettext_noop ("output only the identity strings"),
138    0, &GNUNET_GETOPT_set_one, &be_quiet},
139   {'s', "self", NULL,
140    gettext_noop ("output our own identity only"),
141    0, &GNUNET_GETOPT_set_one, &get_self},
142   GNUNET_GETOPT_OPTION_END
143 };
144
145 /**
146  * The main function to obtain peer information.
147  *
148  * @param argc number of arguments from the command line
149  * @param argv command line arguments
150  * @return 0 ok, 1 on error
151  */
152 int
153 main (int argc, char *const *argv)
154 {
155   return (GNUNET_OK ==
156           GNUNET_PROGRAM_run (argc,
157                               argv,
158                               "gnunet-peerinfo",
159                               gettext_noop ("Print information about peers."),
160                               options, &run, NULL)) ? 0 : 1;
161 }
162
163 /* end of gnunet-peerinfo.c */