2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file transport/transport_api_address_iterate.c
23 * @brief api for asking transport service to iterate over all
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.
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"
43 * Context for the address lookup.
45 struct AddressLookupCtx
48 * Function to call with the human-readable address.
50 GNUNET_TRANSPORT_AddressLookUpBinaryCallback cb;
58 * Connection to the service.
60 struct GNUNET_CLIENT_Connection *client;
63 * When should this operation time out?
65 struct GNUNET_TIME_Absolute timeout;
70 * Function called with responses from the service.
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
77 peer_address_response_processor (void *cls,
78 const struct GNUNET_MessageHeader *msg)
80 struct AddressLookupCtx *alucb = cls;
81 struct AddressIterateResponseMessage *address;
85 //size_t transport_len;
91 alucb->cb (alucb->cb_cls, NULL, NULL, NULL, 0);
92 GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO);
97 GNUNET_break (ntohs (msg->type) ==
98 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
99 size = ntohs (msg->size);
100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message type %u size %u\n",
101 ntohs (msg->type), size);
102 if (size == sizeof (struct GNUNET_MessageHeader))
105 alucb->cb (alucb->cb_cls, NULL, NULL, NULL, 0);
106 GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO);
110 if (size < sizeof (struct AddressIterateResponseMessage))
114 alucb->cb (alucb->cb_cls, NULL, NULL, NULL, 0);
115 GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO);
120 address = (struct AddressIterateResponseMessage *) &msg[1];
122 transport = (char *) &address[0];
123 //transport_len = ntohs(address->pluginlen);
124 addrlen = ntohs (address->addrlen);
126 /* expect more replies */
127 GNUNET_CLIENT_receive (alucb->client, &peer_address_response_processor, alucb,
128 GNUNET_TIME_absolute_get_remaining (alucb->timeout));
129 alucb->cb (alucb->cb_cls, &address->peer, transport, NULL, addrlen);
134 * Return all the known addresses for a peer.
136 * @param cfg configuration to use
137 * @param timeout how long is the lookup allowed to take at most
138 * @param peer_address_callback function to call with the results
139 * @param peer_address_callback_cls closure for peer_address_callback
142 GNUNET_TRANSPORT_address_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
143 struct GNUNET_TIME_Relative timeout,
144 GNUNET_TRANSPORT_AddressLookUpBinaryCallback
145 peer_address_callback,
146 void *peer_address_callback_cls)
148 struct AddressIterateMessage msg;
149 struct GNUNET_TIME_Absolute abs_timeout;
150 struct AddressLookupCtx *peer_address_lookup_cb;
151 struct GNUNET_CLIENT_Connection *client;
153 client = GNUNET_CLIENT_connect ("transport", cfg);
156 peer_address_callback (peer_address_callback_cls, NULL, NULL, NULL, 0);
159 abs_timeout = GNUNET_TIME_relative_to_absolute (timeout);
161 msg.header.size = htons (sizeof (struct AddressIterateMessage));
162 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE);
163 msg.timeout = GNUNET_TIME_absolute_hton (abs_timeout);
164 peer_address_lookup_cb = GNUNET_malloc (sizeof (struct AddressLookupCtx));
165 peer_address_lookup_cb->cb = peer_address_callback;
166 peer_address_lookup_cb->cb_cls = peer_address_callback_cls;
167 peer_address_lookup_cb->timeout = abs_timeout;
168 peer_address_lookup_cb->client = client;
169 GNUNET_assert (GNUNET_OK ==
170 GNUNET_CLIENT_transmit_and_get_response (client, &msg.header,
172 &peer_address_response_processor,
173 peer_address_lookup_cb));
176 /* end of transport_api_address_iterate.c */