39950467d98238be63682fbfa7b49da5ccee7007
[oweals/gnunet.git] / src / transport / gnunet-transport-list-connections.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 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 core/gnunet-transport-list-connections.c
23  *
24  * @brief Print all known address information about other peers.
25  *
26  * Lists all peers and connections that the transport service is
27  * aware of.  Pretty prints addresses, peer id's, and whether
28  * or not the _address_ is connected.  Note that these are not
29  * core level connections, only transport level connections.
30  *
31  * @author Nathan Evans
32  */
33 #include "platform.h"
34 #include "gnunet_crypto_lib.h"
35 #include "gnunet_configuration_lib.h"
36 #include "gnunet_getopt_lib.h"
37 #include "gnunet_transport_service.h"
38 #include "gnunet_program_lib.h"
39
40 #define VERBOSE 0
41 static int no_resolve;
42
43 #if VERBOSE
44   static unsigned int connection_count;
45 #endif
46
47 static const struct GNUNET_CONFIGURATION_Handle *cfg;
48
49 /**
50  * Function to call with a human-readable format of an address
51  *
52  * @param cls closure
53  * @param address NULL on error, otherwise 0-terminated printable UTF-8 string
54  */
55 static void
56 process_address (void *cls,
57                           const char *address)
58 {
59 #if VERBOSE
60   connection_count++;
61 #endif
62   if (address != NULL)
63     fprintf(stdout, "%s\n", address);
64 }
65
66
67 /**
68  * Main function that will be run by the scheduler.
69  *
70  * @param cls closure
71  * @param args remaining command-line arguments
72  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
73  * @param c configuration
74  */
75 static void
76 run (void *cls,
77      char *const *args,
78      const char *cfgfile,
79      const struct GNUNET_CONFIGURATION_Handle *c)
80 {
81
82   cfg = c;
83   if (args[0] != NULL)
84     {
85       fprintf (stderr,
86                _("Invalid command line argument `%s'\n"),
87                args[0]);
88       return;
89     }
90
91   GNUNET_TRANSPORT_address_iterate (cfg,
92                                     GNUNET_TIME_UNIT_MINUTES,
93                                     &process_address, NULL);
94 }
95
96
97 /**
98  * The main function to obtain peer information.
99  *
100  * @param argc number of arguments from the command line
101  * @param argv command line arguments
102  * @return 0 ok, 1 on error
103  */
104 int
105 main (int argc, char *const *argv)
106 {
107   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
108     {'n', "numeric", NULL,
109      gettext_noop ("don't resolve host names"),
110      0, &GNUNET_GETOPT_set_one, &no_resolve},
111     GNUNET_GETOPT_OPTION_END
112   };
113   return (GNUNET_OK ==
114           GNUNET_PROGRAM_run (argc,
115                               argv,
116                               "gnunet-list-connections",
117                               gettext_noop ("Print information about connected peers."),
118                               options, &run, NULL)) ? 0 : 1;
119 }
120
121 /* end of gnunet-transport-list-connections.c */