2 This file is part of GNUnet.
3 Copyright (C) 2010-2015 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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"
31 #define LOG(kind,...) GNUNET_log_from(kind, "ats-connectivity-api", __VA_ARGS__)
35 * Handle for ATS address suggestion requests.
37 struct GNUNET_ATS_ConnectivitySuggestHandle
40 * ID of the peer for which address suggestion was requested.
42 struct GNUNET_PeerIdentity id;
45 * Connecitivity handle this suggestion handle belongs to.
47 struct GNUNET_ATS_ConnectivityHandle *ch;
50 * How urgent is the request.
57 * Handle to the ATS subsystem for connectivity management.
59 struct GNUNET_ATS_ConnectivityHandle
65 const struct GNUNET_CONFIGURATION_Handle *cfg;
68 * Map with the identities of all the peers for which we would
69 * like to have address suggestions. The key is the PID, the
70 * value is currently the `struct GNUNET_ATS_ConnectivitySuggestHandle`
72 struct GNUNET_CONTAINER_MultiPeerMap *sug_requests;
75 * Connection to ATS service.
77 struct GNUNET_CLIENT_Connection *client;
80 * Message queue for sending requests to the ATS service.
82 struct GNUNET_MQ_Handle *mq;
85 * Task to trigger reconnect.
87 struct GNUNET_SCHEDULER_Task *task;
90 * Reconnect backoff delay.
92 struct GNUNET_TIME_Relative backoff;
97 * Re-establish the connection to the ATS service.
99 * @param ch handle to use to re-connect.
102 reconnect (struct GNUNET_ATS_ConnectivityHandle *ch);
106 * Re-establish the connection to the ATS service.
108 * @param cls handle to use to re-connect.
111 reconnect_task (void *cls)
113 struct GNUNET_ATS_ConnectivityHandle *ch = cls;
121 * Disconnect from ATS and then reconnect.
123 * @param ch our handle
126 force_reconnect (struct GNUNET_ATS_ConnectivityHandle *ch)
130 GNUNET_MQ_destroy (ch->mq);
133 if (NULL != ch->client)
135 GNUNET_CLIENT_disconnect (ch->client);
138 ch->backoff = GNUNET_TIME_STD_BACKOFF (ch->backoff);
139 ch->task = GNUNET_SCHEDULER_add_delayed (ch->backoff,
146 * We encountered an error handling the MQ to the
147 * ATS service. Reconnect.
149 * @param cls the `struct GNUNET_ATS_ConnectivityHandle`
150 * @param error details about the error
153 error_handler (void *cls,
154 enum GNUNET_MQ_Error error)
156 struct GNUNET_ATS_ConnectivityHandle *ch = cls;
158 LOG (GNUNET_ERROR_TYPE_DEBUG,
159 "ATS connection died (code %d), reconnecting\n",
161 force_reconnect (ch);
166 * Transmit request for an address suggestion.
168 * @param cls the `struct GNUNET_ATS_ConnectivityHandle`
169 * @param peer peer to ask for an address suggestion for
170 * @param value the `struct GNUNET_ATS_SuggestHandle`
171 * @return #GNUNET_OK (continue to iterate), #GNUNET_SYSERR on
172 * failure (message queue no longer exists)
175 transmit_suggestion (void *cls,
176 const struct GNUNET_PeerIdentity *peer,
179 struct GNUNET_ATS_ConnectivityHandle *ch = cls;
180 struct GNUNET_ATS_ConnectivitySuggestHandle *sh = value;
181 struct GNUNET_MQ_Envelope *ev;
182 struct RequestAddressMessage *m;
185 return GNUNET_SYSERR;
186 ev = GNUNET_MQ_msg (m, GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS);
187 m->strength = htonl (sh->strength);
189 GNUNET_MQ_send (ch->mq, ev);
195 * Re-establish the connection to the ATS service.
197 * @param ch handle to use to re-connect.
200 reconnect (struct GNUNET_ATS_ConnectivityHandle *ch)
202 static const struct GNUNET_MQ_MessageHandler handlers[] =
204 struct GNUNET_MQ_Envelope *ev;
205 struct ClientStartMessage *init;
207 GNUNET_assert (NULL == ch->client);
208 ch->client = GNUNET_CLIENT_connect ("ats", ch->cfg);
209 if (NULL == ch->client)
211 force_reconnect (ch);
214 ch->mq = GNUNET_MQ_queue_for_connection_client (ch->client,
218 ev = GNUNET_MQ_msg (init,
219 GNUNET_MESSAGE_TYPE_ATS_START);
220 init->start_flag = htonl (START_FLAG_CONNECTION_SUGGESTION);
221 GNUNET_MQ_send (ch->mq, ev);
224 GNUNET_CONTAINER_multipeermap_iterate (ch->sug_requests,
225 &transmit_suggestion,
231 * Initialize the ATS connectivity suggestion client handle.
233 * @param cfg configuration to use
234 * @return ats connectivity handle, NULL on error
236 struct GNUNET_ATS_ConnectivityHandle *
237 GNUNET_ATS_connectivity_init (const struct GNUNET_CONFIGURATION_Handle *cfg)
239 struct GNUNET_ATS_ConnectivityHandle *ch;
241 ch = GNUNET_new (struct GNUNET_ATS_ConnectivityHandle);
243 ch->sug_requests = GNUNET_CONTAINER_multipeermap_create (32,
251 * Function called to free all `struct GNUNET_ATS_ConnectivitySuggestHandle`s
256 * @param value the value to free
257 * @return #GNUNET_OK (continue to iterate)
260 free_sug_handle (void *cls,
261 const struct GNUNET_PeerIdentity *key,
264 struct GNUNET_ATS_ConnectivitySuggestHandle *cur = value;
272 * Client is done with ATS connectivity management, release resources.
274 * @param ch handle to release
277 GNUNET_ATS_connectivity_done (struct GNUNET_ATS_ConnectivityHandle *ch)
281 GNUNET_MQ_destroy (ch->mq);
284 if (NULL != ch->client)
286 GNUNET_CLIENT_disconnect (ch->client);
289 if (NULL != ch->task)
291 GNUNET_SCHEDULER_cancel (ch->task);
294 GNUNET_CONTAINER_multipeermap_iterate (ch->sug_requests,
297 GNUNET_CONTAINER_multipeermap_destroy (ch->sug_requests);
303 * We would like to receive address suggestions for a peer. ATS will
304 * respond with a call to the continuation immediately containing an address or
305 * no address if none is available. ATS can suggest more addresses until we call
306 * #GNUNET_ATS_connectivity_suggest_cancel().
309 * @param peer identity of the peer we need an address for
310 * @param strength how urgent is the need for such a suggestion
311 * @return suggest handle, NULL if a request is already pending
313 struct GNUNET_ATS_ConnectivitySuggestHandle *
314 GNUNET_ATS_connectivity_suggest (struct GNUNET_ATS_ConnectivityHandle *ch,
315 const struct GNUNET_PeerIdentity *peer,
318 struct GNUNET_ATS_ConnectivitySuggestHandle *s;
320 LOG (GNUNET_ERROR_TYPE_DEBUG,
321 "Requesting ATS to suggest address for `%s'\n",
323 s = GNUNET_new (struct GNUNET_ATS_ConnectivitySuggestHandle);
326 s->strength = strength;
328 GNUNET_CONTAINER_multipeermap_put (ch->sug_requests,
331 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
338 (void) transmit_suggestion (ch,
346 * We no longer care about being connected to a peer.
348 * @param sh handle to stop
351 GNUNET_ATS_connectivity_suggest_cancel (struct GNUNET_ATS_ConnectivitySuggestHandle *sh)
353 struct GNUNET_ATS_ConnectivityHandle *ch = sh->ch;
354 struct GNUNET_MQ_Envelope *ev;
355 struct RequestAddressMessage *m;
357 LOG (GNUNET_ERROR_TYPE_DEBUG,
358 "Telling ATS we no longer care for an address for `%s'\n",
359 GNUNET_i2s (&sh->id));
360 GNUNET_assert (GNUNET_OK ==
361 GNUNET_CONTAINER_multipeermap_remove (ch->sug_requests,
369 ev = GNUNET_MQ_msg (m, GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS_CANCEL);
370 m->strength = htonl (0);
372 GNUNET_MQ_send (ch->mq, ev);
377 /* end of ats_api_connectivity.c */