2 This file is part of GNUnet.
3 (C) 2010-2015 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.
21 * @file ats/ats_api_connectivity.c
22 * @brief enable clients to ask ATS about establishing connections to peers
23 * @author Christian Grothoff
24 * @author Matthias Wachs
27 #include "gnunet_ats_service.h"
32 * Handle for ATS address suggestion requests.
34 struct GNUNET_ATS_ConnectivitySuggestHandle
37 * ID of the peer for which address suggestion was requested.
39 struct GNUNET_PeerIdentity id;
42 * Connecitivity handle this suggestion handle belongs to.
44 struct GNUNET_ATS_ConnectivityHandle *ch;
49 * Handle to the ATS subsystem for connectivity management.
51 struct GNUNET_ATS_ConnectivityHandle
57 const struct GNUNET_CONFIGURATION_Handle *cfg;
60 * Map with the identities of all the peers for which we would
61 * like to have address suggestions. The key is the PID, the
62 * value is currently the `struct GNUNET_ATS_ConnectivitySuggestHandle`
64 struct GNUNET_CONTAINER_MultiPeerMap *sug_requests;
67 * Connection to ATS service.
69 struct GNUNET_CLIENT_Connection *client;
72 * Message queue for sending requests to the ATS service.
74 struct GNUNET_MQ_Handle *mq;
77 * Task to trigger reconnect.
79 struct GNUNET_SCHEDULER_Task *task;
85 * Re-establish the connection to the ATS service.
87 * @param ch handle to use to re-connect.
90 reconnect (struct GNUNET_ATS_ConnectivityHandle *ch);
94 * Re-establish the connection to the ATS service.
96 * @param cls handle to use to re-connect.
97 * @param tc scheduler context
100 reconnect_task (void *cls,
101 const struct GNUNET_SCHEDULER_TaskContext *tc)
103 struct GNUNET_ATS_ConnectivityHandle *ch = cls;
111 * Disconnect from ATS and then reconnect.
113 * @param ch our handle
116 force_reconnect (struct GNUNET_ATS_ConnectivityHandle *ch)
120 GNUNET_MQ_destroy (ch->mq);
123 if (NULL != ch->client)
125 GNUNET_CLIENT_disconnect (ch->client);
128 ch->task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
135 * We encountered an error handling the MQ to the
136 * ATS service. Reconnect.
138 * @param cls the `struct GNUNET_ATS_ConnectivityHandle`
139 * @param error details about the error
142 error_handler (void *cls,
143 enum GNUNET_MQ_Error error)
145 struct GNUNET_ATS_ConnectivityHandle *ch = cls;
147 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
148 "ATS connection died (code %d), reconnecting\n",
150 force_reconnect (ch);
155 * Transmit request for an address suggestion.
157 * @param cls the `struct GNUNET_ATS_ConnectivityHandle`
158 * @param peer peer to ask for an address suggestion for
159 * @param value the `struct GNUNET_ATS_SuggestHandle`
160 * @return #GNUNET_OK (continue to iterate), #GNUNET_SYSERR on
161 * failure (message queue no longer exists)
164 transmit_suggestion (void *cls,
165 const struct GNUNET_PeerIdentity *peer,
168 struct GNUNET_ATS_ConnectivityHandle *ch = cls;
169 struct GNUNET_MQ_Envelope *ev;
170 struct RequestAddressMessage *m;
173 return GNUNET_SYSERR;
174 ev = GNUNET_MQ_msg (m, GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS);
175 m->reserved = htonl (0);
177 GNUNET_MQ_send (ch->mq, ev);
183 * Re-establish the connection to the ATS service.
185 * @param ch handle to use to re-connect.
188 reconnect (struct GNUNET_ATS_ConnectivityHandle *ch)
190 static const struct GNUNET_MQ_MessageHandler handlers[] =
192 struct GNUNET_MQ_Envelope *ev;
193 struct ClientStartMessage *init;
195 GNUNET_assert (NULL == ch->client);
196 ch->client = GNUNET_CLIENT_connect ("ats", ch->cfg);
197 if (NULL == ch->client)
199 force_reconnect (ch);
202 ch->mq = GNUNET_MQ_queue_for_connection_client (ch->client,
206 ev = GNUNET_MQ_msg (init,
207 GNUNET_MESSAGE_TYPE_ATS_START);
208 init->start_flag = htonl (START_FLAG_CONNECTION_SUGGESTION);
209 GNUNET_MQ_send (ch->mq, ev);
212 GNUNET_CONTAINER_multipeermap_iterate (ch->sug_requests,
213 &transmit_suggestion,
219 * Initialize the ATS connectivity suggestion client handle.
221 * @param cfg configuration to use
222 * @return ats connectivity handle, NULL on error
224 struct GNUNET_ATS_ConnectivityHandle *
225 GNUNET_ATS_connectivity_init (const struct GNUNET_CONFIGURATION_Handle *cfg)
227 struct GNUNET_ATS_ConnectivityHandle *ch;
229 ch = GNUNET_new (struct GNUNET_ATS_ConnectivityHandle);
231 ch->sug_requests = GNUNET_CONTAINER_multipeermap_create (32,
239 * Function called to free all `struct GNUNET_ATS_SuggestHandles`
244 * @param value the value to free
245 * @return #GNUNET_OK (continue to iterate)
248 free_sug_handle (void *cls,
249 const struct GNUNET_PeerIdentity *key,
252 struct GNUNET_ATS_ConnectivitySuggestHandle *cur = value;
260 * Client is done with ATS connectivity management, release resources.
262 * @param ch handle to release
265 GNUNET_ATS_connectivity_done (struct GNUNET_ATS_ConnectivityHandle *ch)
269 GNUNET_MQ_destroy (ch->mq);
272 if (NULL != ch->client)
274 GNUNET_CLIENT_disconnect (ch->client);
277 if (NULL != ch->task)
279 GNUNET_SCHEDULER_cancel (ch->task);
282 GNUNET_CONTAINER_multipeermap_iterate (ch->sug_requests,
285 GNUNET_CONTAINER_multipeermap_destroy (ch->sug_requests);
291 * We would like to receive address suggestions for a peer. ATS will
292 * respond with a call to the continuation immediately containing an address or
293 * no address if none is available. ATS can suggest more addresses until we call
294 * #GNUNET_ATS_connectivity_suggest_cancel().
297 * @param peer identity of the peer we need an address for
298 * @return suggest handle, NULL if a request is already pending
300 struct GNUNET_ATS_ConnectivitySuggestHandle *
301 GNUNET_ATS_connectivity_suggest (struct GNUNET_ATS_ConnectivityHandle *ch,
302 const struct GNUNET_PeerIdentity *peer)
304 struct GNUNET_ATS_ConnectivitySuggestHandle *s;
306 s = GNUNET_new (struct GNUNET_ATS_ConnectivitySuggestHandle);
310 GNUNET_CONTAINER_multipeermap_put (ch->sug_requests,
313 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
320 (void) transmit_suggestion (ch,
328 * We no longer care about being connected to a peer.
330 * @param sh handle to stop
333 GNUNET_ATS_connectivity_suggest_cancel (struct GNUNET_ATS_ConnectivitySuggestHandle *sh)
335 struct GNUNET_ATS_ConnectivityHandle *ch = sh->ch;
336 struct GNUNET_MQ_Envelope *ev;
337 struct RequestAddressMessage *m;
339 GNUNET_assert (GNUNET_OK ==
340 GNUNET_CONTAINER_multipeermap_remove (ch->sug_requests,
348 ev = GNUNET_MQ_msg (m, GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS_CANCEL);
349 m->reserved = htonl (0);
351 GNUNET_MQ_send (ch->mq, ev);
356 /* end of ats_api_connectivity.c */