2 This file is part of GNUnet.
3 Copyright (C) 2010-2016 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
16 * @file ats/ats_api_connectivity.c
17 * @brief enable clients to ask ATS about establishing connections to peers
18 * @author Christian Grothoff
19 * @author Matthias Wachs
22 #include "gnunet_ats_service.h"
26 #define LOG(kind,...) GNUNET_log_from(kind, "ats-connectivity-api", __VA_ARGS__)
30 * Handle for ATS address suggestion requests.
32 struct GNUNET_ATS_ConnectivitySuggestHandle
35 * ID of the peer for which address suggestion was requested.
37 struct GNUNET_PeerIdentity id;
40 * Connecitivity handle this suggestion handle belongs to.
42 struct GNUNET_ATS_ConnectivityHandle *ch;
45 * How urgent is the request.
52 * Handle to the ATS subsystem for connectivity management.
54 struct GNUNET_ATS_ConnectivityHandle
60 const struct GNUNET_CONFIGURATION_Handle *cfg;
63 * Map with the identities of all the peers for which we would
64 * like to have address suggestions. The key is the PID, the
65 * value is currently the `struct GNUNET_ATS_ConnectivitySuggestHandle`
67 struct GNUNET_CONTAINER_MultiPeerMap *sug_requests;
70 * Message queue for sending requests to the ATS service.
72 struct GNUNET_MQ_Handle *mq;
75 * Task to trigger reconnect.
77 struct GNUNET_SCHEDULER_Task *task;
80 * Reconnect backoff delay.
82 struct GNUNET_TIME_Relative backoff;
87 * Re-establish the connection to the ATS service.
89 * @param ch handle to use to re-connect.
92 reconnect (struct GNUNET_ATS_ConnectivityHandle *ch);
96 * Re-establish the connection to the ATS service.
98 * @param cls handle to use to re-connect.
101 reconnect_task (void *cls)
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 ch->backoff = GNUNET_TIME_STD_BACKOFF (ch->backoff);
124 ch->task = GNUNET_SCHEDULER_add_delayed (ch->backoff,
131 * We encountered an error handling the MQ to the
132 * ATS service. Reconnect.
134 * @param cls the `struct GNUNET_ATS_ConnectivityHandle`
135 * @param error details about the error
138 error_handler (void *cls,
139 enum GNUNET_MQ_Error error)
141 struct GNUNET_ATS_ConnectivityHandle *ch = cls;
143 LOG (GNUNET_ERROR_TYPE_DEBUG,
144 "ATS connection died (code %d), reconnecting\n",
146 force_reconnect (ch);
151 * Transmit request for an address suggestion.
153 * @param cls the `struct GNUNET_ATS_ConnectivityHandle`
154 * @param peer peer to ask for an address suggestion for
155 * @param value the `struct GNUNET_ATS_SuggestHandle`
156 * @return #GNUNET_OK (continue to iterate), #GNUNET_SYSERR on
157 * failure (message queue no longer exists)
160 transmit_suggestion (void *cls,
161 const struct GNUNET_PeerIdentity *peer,
164 struct GNUNET_ATS_ConnectivityHandle *ch = cls;
165 struct GNUNET_ATS_ConnectivitySuggestHandle *sh = value;
166 struct GNUNET_MQ_Envelope *ev;
167 struct RequestAddressMessage *m;
170 return GNUNET_SYSERR;
171 ev = GNUNET_MQ_msg (m, GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS);
172 m->strength = htonl (sh->strength);
174 GNUNET_MQ_send (ch->mq, ev);
180 * Re-establish the connection to the ATS service.
182 * @param ch handle to use to re-connect.
185 reconnect (struct GNUNET_ATS_ConnectivityHandle *ch)
187 static const struct GNUNET_MQ_MessageHandler handlers[] =
189 struct GNUNET_MQ_Envelope *ev;
190 struct ClientStartMessage *init;
192 GNUNET_assert (NULL == ch->mq);
193 ch->mq = GNUNET_CLIENT_connect (ch->cfg,
200 force_reconnect (ch);
203 ev = GNUNET_MQ_msg (init,
204 GNUNET_MESSAGE_TYPE_ATS_START);
205 init->start_flag = htonl (START_FLAG_CONNECTION_SUGGESTION);
206 GNUNET_MQ_send (ch->mq, ev);
209 GNUNET_CONTAINER_multipeermap_iterate (ch->sug_requests,
210 &transmit_suggestion,
216 * Initialize the ATS connectivity suggestion client handle.
218 * @param cfg configuration to use
219 * @return ats connectivity handle, NULL on error
221 struct GNUNET_ATS_ConnectivityHandle *
222 GNUNET_ATS_connectivity_init (const struct GNUNET_CONFIGURATION_Handle *cfg)
224 struct GNUNET_ATS_ConnectivityHandle *ch;
226 ch = GNUNET_new (struct GNUNET_ATS_ConnectivityHandle);
228 ch->sug_requests = GNUNET_CONTAINER_multipeermap_create (32,
236 * Function called to free all `struct GNUNET_ATS_ConnectivitySuggestHandle`s
241 * @param value the value to free
242 * @return #GNUNET_OK (continue to iterate)
245 free_sug_handle (void *cls,
246 const struct GNUNET_PeerIdentity *key,
249 struct GNUNET_ATS_ConnectivitySuggestHandle *cur = value;
257 * Client is done with ATS connectivity management, release resources.
259 * @param ch handle to release
262 GNUNET_ATS_connectivity_done (struct GNUNET_ATS_ConnectivityHandle *ch)
266 GNUNET_MQ_destroy (ch->mq);
269 if (NULL != ch->task)
271 GNUNET_SCHEDULER_cancel (ch->task);
274 GNUNET_CONTAINER_multipeermap_iterate (ch->sug_requests,
277 GNUNET_CONTAINER_multipeermap_destroy (ch->sug_requests);
283 * We would like to receive address suggestions for a peer. ATS will
284 * respond with a call to the continuation immediately containing an address or
285 * no address if none is available. ATS can suggest more addresses until we call
286 * #GNUNET_ATS_connectivity_suggest_cancel().
289 * @param peer identity of the peer we need an address for
290 * @param strength how urgent is the need for such a suggestion
291 * @return suggest handle, NULL if a request is already pending
293 struct GNUNET_ATS_ConnectivitySuggestHandle *
294 GNUNET_ATS_connectivity_suggest (struct GNUNET_ATS_ConnectivityHandle *ch,
295 const struct GNUNET_PeerIdentity *peer,
298 struct GNUNET_ATS_ConnectivitySuggestHandle *s;
300 s = GNUNET_new (struct GNUNET_ATS_ConnectivitySuggestHandle);
303 s->strength = strength;
305 GNUNET_CONTAINER_multipeermap_put (ch->sug_requests,
308 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
310 LOG (GNUNET_ERROR_TYPE_DEBUG,
311 "Not requesting ATS to suggest address for `%s', request already pending\n",
316 LOG (GNUNET_ERROR_TYPE_DEBUG,
317 "Requesting ATS to suggest address for `%s'\n",
321 (void) transmit_suggestion (ch,
329 * We no longer care about being connected to a peer.
331 * @param sh handle to stop
334 GNUNET_ATS_connectivity_suggest_cancel (struct GNUNET_ATS_ConnectivitySuggestHandle *sh)
336 struct GNUNET_ATS_ConnectivityHandle *ch = sh->ch;
337 struct GNUNET_MQ_Envelope *ev;
338 struct RequestAddressMessage *m;
340 LOG (GNUNET_ERROR_TYPE_DEBUG,
341 "Telling ATS we no longer care for an address for `%s'\n",
342 GNUNET_i2s (&sh->id));
343 GNUNET_assert (GNUNET_OK ==
344 GNUNET_CONTAINER_multipeermap_remove (ch->sug_requests,
352 ev = GNUNET_MQ_msg (m,
353 GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS_CANCEL);
354 m->strength = htonl (0);
356 GNUNET_MQ_send (ch->mq, ev);
361 /* end of ats_api_connectivity.c */