fixing list-connections to show correct peer id
[oweals/gnunet.git] / src / transport / transport_api_address_iterate.c
1 /*
2      This file is part of GNUnet.
3      (C) 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 transport/transport_api_address_iterate.c
23  * @brief api for asking transport service to iterate over all
24  *        known addresses
25  *
26  * This api provides a single function call to ask the transport
27  * service to list all peers and their known addresses, as pretty
28  * printed by the appropriate plugin.  Reports whether or not the
29  * address is connected as well.
30  */
31
32 #include "platform.h"
33 #include "gnunet_client_lib.h"
34 #include "gnunet_arm_service.h"
35 #include "gnunet_hello_lib.h"
36 #include "gnunet_protocols.h"
37 #include "gnunet_server_lib.h"
38 #include "gnunet_time_lib.h"
39 #include "gnunet_transport_service.h"
40 #include "transport.h"
41
42 /**
43  * Context for the address lookup.
44  */
45 struct AddressLookupCtx
46 {
47   /**
48    * Function to call with the human-readable address.
49    */
50   GNUNET_TRANSPORT_AddressLookUpBinaryCallback cb;
51
52   /**
53    * Closure for cb.
54    */
55   void *cb_cls;
56
57   /**
58    * Connection to the service.
59    */
60   struct GNUNET_CLIENT_Connection *client;
61
62   /**
63    * When should this operation time out?
64    */
65   struct GNUNET_TIME_Absolute timeout;
66 };
67
68
69 /**
70  * Function called with responses from the service.
71  *
72  * @param cls our 'struct AddressLookupCtx*'
73  * @param msg NULL on timeout or error, otherwise presumably a
74  *        message with the human-readable peer and address
75  */
76 static void
77 peer_address_response_processor (void *cls,
78                                  const struct GNUNET_MessageHeader *msg)
79 {
80   struct AddressLookupCtx *alucb = cls;
81   struct AddressIterateResponseMessage *address;
82   uint16_t size;
83   char * transport;
84   size_t transport_len;
85   //void * addr;
86   size_t addrlen;
87
88   if (msg == NULL)
89   {
90     alucb->cb (alucb->cb_cls, NULL, NULL, NULL, 0);
91     GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO);
92     GNUNET_free (alucb);
93     return;
94   }
95
96   GNUNET_break (ntohs (msg->type) ==
97                 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
98   size = ntohs (msg->size);
99   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message type %u size %u\n",
100               ntohs (msg->type), size);
101   if (size == sizeof (struct GNUNET_MessageHeader))
102   {
103     /* done! */
104     alucb->cb (alucb->cb_cls, NULL, NULL, NULL, 0);
105     GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO);
106     GNUNET_free (alucb);
107     return;
108   }
109   if (size < sizeof (struct AddressIterateResponseMessage))
110   {
111     /* invalid reply */
112     GNUNET_break (0);
113     alucb->cb (alucb->cb_cls, NULL, NULL, NULL, 0);
114     GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO);
115     GNUNET_free (alucb);
116     return;
117   }
118
119   address = (struct AddressIterateResponseMessage *) &msg[1];
120
121   transport = (char *) &address[0];
122   transport_len = ntohs(address->pluginlen);
123   addrlen = ntohs(address->addrlen);
124
125   /* expect more replies */
126   GNUNET_CLIENT_receive (alucb->client, &peer_address_response_processor, alucb,
127                          GNUNET_TIME_absolute_get_remaining (alucb->timeout));
128   alucb->cb (alucb->cb_cls, &address->peer, transport, NULL, addrlen);
129 }
130
131
132 /**
133  * Return all the known addresses for a peer.
134  *
135  * @param cfg configuration to use
136  * @param timeout how long is the lookup allowed to take at most
137  * @param peer_address_callback function to call with the results
138  * @param peer_address_callback_cls closure for peer_address_callback
139  */
140 void
141 GNUNET_TRANSPORT_address_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
142                                   struct GNUNET_TIME_Relative timeout,
143                                   GNUNET_TRANSPORT_AddressLookUpBinaryCallback
144                                   peer_address_callback,
145                                   void *peer_address_callback_cls)
146 {
147   struct AddressIterateMessage msg;
148   struct GNUNET_TIME_Absolute abs_timeout;
149   struct AddressLookupCtx *peer_address_lookup_cb;
150   struct GNUNET_CLIENT_Connection *client;
151
152   client = GNUNET_CLIENT_connect ("transport", cfg);
153   if (client == NULL)
154   {
155     peer_address_callback (peer_address_callback_cls, NULL, NULL, NULL, 0);
156     return;
157   }
158   abs_timeout = GNUNET_TIME_relative_to_absolute (timeout);
159
160   msg.header.size = htons (sizeof (struct AddressIterateMessage));
161   msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE);
162   msg.timeout = GNUNET_TIME_absolute_hton (abs_timeout);
163   peer_address_lookup_cb = GNUNET_malloc (sizeof (struct AddressLookupCtx));
164   peer_address_lookup_cb->cb = peer_address_callback;
165   peer_address_lookup_cb->cb_cls = peer_address_callback_cls;
166   peer_address_lookup_cb->timeout = abs_timeout;
167   peer_address_lookup_cb->client = client;
168   GNUNET_assert (GNUNET_OK ==
169                  GNUNET_CLIENT_transmit_and_get_response (client, &msg.header,
170                                                           timeout, GNUNET_YES,
171                                                           &peer_address_response_processor,
172                                                           peer_address_lookup_cb));
173 }
174
175 /* end of transport_api_address_iterate.c */