aboutsummaryrefslogtreecommitdiff
path: root/src/peerinfo-tool
diff options
context:
space:
mode:
authorNathan S. Evans <evans@in.tum.de>2011-07-08 16:47:31 +0000
committerNathan S. Evans <evans@in.tum.de>2011-07-08 16:47:31 +0000
commit7ba0869d2a5c3d72dd4ad654c1ec3e4269c0e3e6 (patch)
treee56cc3c2fe62eddf26acf988201ed5a9e7041e04 /src/peerinfo-tool
parent51204303f98feef6d3f100ac0a344294a091fbe5 (diff)
downloadgnunet-7ba0869d2a5c3d72dd4ad654c1ec3e4269c0e3e6.tar.gz
gnunet-7ba0869d2a5c3d72dd4ad654c1ec3e4269c0e3e6.zip
list connections binary source
Diffstat (limited to 'src/peerinfo-tool')
-rw-r--r--src/peerinfo-tool/gnunet-list-connections.c195
1 files changed, 195 insertions, 0 deletions
diff --git a/src/peerinfo-tool/gnunet-list-connections.c b/src/peerinfo-tool/gnunet-list-connections.c
new file mode 100644
index 000000000..d95b0a757
--- /dev/null
+++ b/src/peerinfo-tool/gnunet-list-connections.c
@@ -0,0 +1,195 @@
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 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 peerinfo-tool/gnunet-list-connections.c
23 * @brief Print information about other known _connected_ 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_core_service.h"
33#include "gnunet_program_lib.h"
34
35static int no_resolve;
36
37
38static const struct GNUNET_CONFIGURATION_Handle *cfg;
39
40struct AddressStringList
41{
42 /**
43 * Pointer to previous element.
44 */
45 struct AddressStringList *prev;
46
47 /**
48 * Pointer to next element.
49 */
50 struct AddressStringList *next;
51
52 /**
53 * Address as string.
54 */
55 char *address_string;
56};
57
58struct PrintContext
59{
60 struct GNUNET_PeerIdentity peer;
61 struct AddressStringList *address_list_head;
62 struct AddressStringList *address_list_tail;
63};
64
65
66static void
67dump_pc (struct PrintContext *pc)
68{
69 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
70 struct AddressStringList *address;
71
72 GNUNET_CRYPTO_hash_to_enc (&pc->peer.hashPubKey, &enc);
73 printf (_("Peer `%s'\n"),
74 (const char *) &enc);
75 while (NULL != (address = pc->address_list_head))
76 {
77 printf ("\t%s\n", address->address_string);
78 GNUNET_free(address->address_string);
79 GNUNET_CONTAINER_DLL_remove(pc->address_list_head, pc->address_list_tail, address);
80 GNUNET_free(address);
81 }
82
83 printf ("\n");
84
85 GNUNET_free (pc);
86}
87
88
89/**
90 * Function to call with a human-readable format of an address
91 *
92 * @param cls closure
93 * @param address NULL on error, otherwise 0-terminated printable UTF-8 string
94 */
95static void
96process_resolved_address (void *cls,
97 const char *address)
98{
99 struct PrintContext *pc = cls;
100 struct AddressStringList *new_address;
101
102 if (address == NULL)
103 {
104 dump_pc (pc);
105 return;
106 }
107
108 new_address = GNUNET_malloc(sizeof(struct AddressStringList));
109#if VERBOSE
110 fprintf(stderr, "Received address %s\n", address);
111#endif
112 new_address->address_string = GNUNET_strdup(address);
113 GNUNET_CONTAINER_DLL_insert(pc->address_list_head, pc->address_list_tail, new_address);
114}
115
116
117/**
118 * Callback for retrieving a list of connected peers.
119 */
120static void
121connected_peer_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
122 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
123{
124 struct PrintContext *pc;
125
126 if (peer != NULL) /* Not yet finished */
127 {
128#if VERBOSE
129 fprintf(stderr, "Learned about peer %s\n", GNUNET_i2s(peer));
130#endif
131 pc = GNUNET_malloc (sizeof (struct PrintContext));
132 pc->peer = *peer;
133 GNUNET_TRANSPORT_peer_address_lookup (cfg, peer,
134 GNUNET_TIME_UNIT_MINUTES,
135 &process_resolved_address, pc);
136 }
137}
138
139
140/**
141 * Main function that will be run by the scheduler.
142 *
143 * @param cls closure
144 * @param args remaining command-line arguments
145 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
146 * @param c configuration
147 */
148static void
149run (void *cls,
150 char *const *args,
151 const char *cfgfile,
152 const struct GNUNET_CONFIGURATION_Handle *c)
153{
154
155 cfg = c;
156 if (args[0] != NULL)
157 {
158 fprintf (stderr,
159 _("Invalid command line argument `%s'\n"),
160 args[0]);
161 return;
162 }
163
164 GNUNET_CORE_iterate_peers (cfg,
165 &connected_peer_callback,
166 NULL);
167
168}
169
170
171/**
172 * The main function to obtain peer information.
173 *
174 * @param argc number of arguments from the command line
175 * @param argv command line arguments
176 * @return 0 ok, 1 on error
177 */
178int
179main (int argc, char *const *argv)
180{
181 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
182 {'n', "numeric", NULL,
183 gettext_noop ("don't resolve host names"),
184 0, &GNUNET_GETOPT_set_one, &no_resolve},
185 GNUNET_GETOPT_OPTION_END
186 };
187 return (GNUNET_OK ==
188 GNUNET_PROGRAM_run (argc,
189 argv,
190 "gnunet-list-connections",
191 gettext_noop ("Print information about connected peers."),
192 options, &run, NULL)) ? 0 : 1;
193}
194
195/* end of gnunet-peerinfo.c */