-use backoff for reconnects
[oweals/gnunet.git] / src / ats / ats_api_connectivity.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2010-2015 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  * @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
25  */
26 #include "platform.h"
27 #include "gnunet_ats_service.h"
28 #include "ats.h"
29
30
31 /**
32  * Handle for ATS address suggestion requests.
33  */
34 struct GNUNET_ATS_ConnectivitySuggestHandle
35 {
36   /**
37    * ID of the peer for which address suggestion was requested.
38    */
39   struct GNUNET_PeerIdentity id;
40
41   /**
42    * Connecitivity handle this suggestion handle belongs to.
43    */
44   struct GNUNET_ATS_ConnectivityHandle *ch;
45 };
46
47
48 /**
49  * Handle to the ATS subsystem for connectivity management.
50  */
51 struct GNUNET_ATS_ConnectivityHandle
52 {
53
54   /**
55    * Our configuration.
56    */
57   const struct GNUNET_CONFIGURATION_Handle *cfg;
58
59   /**
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`
63    */
64   struct GNUNET_CONTAINER_MultiPeerMap *sug_requests;
65
66   /**
67    * Connection to ATS service.
68    */
69   struct GNUNET_CLIENT_Connection *client;
70
71   /**
72    * Message queue for sending requests to the ATS service.
73    */
74   struct GNUNET_MQ_Handle *mq;
75
76   /**
77    * Task to trigger reconnect.
78    */
79   struct GNUNET_SCHEDULER_Task *task;
80
81   /**
82    * Reconnect backoff delay.
83    */
84   struct GNUNET_TIME_Relative backoff;
85 };
86
87
88 /**
89  * Re-establish the connection to the ATS service.
90  *
91  * @param ch handle to use to re-connect.
92  */
93 static void
94 reconnect (struct GNUNET_ATS_ConnectivityHandle *ch);
95
96
97 /**
98  * Re-establish the connection to the ATS service.
99  *
100  * @param cls handle to use to re-connect.
101  * @param tc scheduler context
102  */
103 static void
104 reconnect_task (void *cls,
105                 const struct GNUNET_SCHEDULER_TaskContext *tc)
106 {
107   struct GNUNET_ATS_ConnectivityHandle *ch = cls;
108
109   ch->task = NULL;
110   reconnect (ch);
111 }
112
113
114 /**
115  * Disconnect from ATS and then reconnect.
116  *
117  * @param ch our handle
118  */
119 static void
120 force_reconnect (struct GNUNET_ATS_ConnectivityHandle *ch)
121 {
122   if (NULL != ch->mq)
123   {
124     GNUNET_MQ_destroy (ch->mq);
125     ch->mq = NULL;
126   }
127   if (NULL != ch->client)
128   {
129     GNUNET_CLIENT_disconnect (ch->client);
130     ch->client = NULL;
131   }
132   ch->backoff = GNUNET_TIME_STD_BACKOFF (ch->backoff);
133   ch->task = GNUNET_SCHEDULER_add_delayed (ch->backoff,
134                                            &reconnect_task,
135                                            ch);
136 }
137
138
139 /**
140  * We encountered an error handling the MQ to the
141  * ATS service.  Reconnect.
142  *
143  * @param cls the `struct GNUNET_ATS_ConnectivityHandle`
144  * @param error details about the error
145  */
146 static void
147 error_handler (void *cls,
148                enum GNUNET_MQ_Error error)
149 {
150   struct GNUNET_ATS_ConnectivityHandle *ch = cls;
151
152   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
153               "ATS connection died (code %d), reconnecting\n",
154               (int) error);
155   force_reconnect (ch);
156 }
157
158
159 /**
160  * Transmit request for an address suggestion.
161  *
162  * @param cls the `struct GNUNET_ATS_ConnectivityHandle`
163  * @param peer peer to ask for an address suggestion for
164  * @param value the `struct GNUNET_ATS_SuggestHandle`
165  * @return #GNUNET_OK (continue to iterate), #GNUNET_SYSERR on
166  *         failure (message queue no longer exists)
167  */
168 static int
169 transmit_suggestion (void *cls,
170                      const struct GNUNET_PeerIdentity *peer,
171                      void *value)
172 {
173   struct GNUNET_ATS_ConnectivityHandle *ch = cls;
174   struct GNUNET_MQ_Envelope *ev;
175   struct RequestAddressMessage *m;
176
177   if (NULL == ch->mq)
178     return GNUNET_SYSERR;
179   ev = GNUNET_MQ_msg (m, GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS);
180   m->reserved = htonl (0);
181   m->peer = *peer;
182   GNUNET_MQ_send (ch->mq, ev);
183   return GNUNET_OK;
184 }
185
186
187 /**
188  * Re-establish the connection to the ATS service.
189  *
190  * @param ch handle to use to re-connect.
191  */
192 static void
193 reconnect (struct GNUNET_ATS_ConnectivityHandle *ch)
194 {
195   static const struct GNUNET_MQ_MessageHandler handlers[] =
196     { { NULL, 0, 0 } };
197   struct GNUNET_MQ_Envelope *ev;
198   struct ClientStartMessage *init;
199
200   GNUNET_assert (NULL == ch->client);
201   ch->client = GNUNET_CLIENT_connect ("ats", ch->cfg);
202   if (NULL == ch->client)
203   {
204     force_reconnect (ch);
205     return;
206   }
207   ch->mq = GNUNET_MQ_queue_for_connection_client (ch->client,
208                                                   handlers,
209                                                   &error_handler,
210                                                   ch);
211   ev = GNUNET_MQ_msg (init,
212                       GNUNET_MESSAGE_TYPE_ATS_START);
213   init->start_flag = htonl (START_FLAG_CONNECTION_SUGGESTION);
214   GNUNET_MQ_send (ch->mq, ev);
215   if (NULL == ch->mq)
216     return;
217   GNUNET_CONTAINER_multipeermap_iterate (ch->sug_requests,
218                                          &transmit_suggestion,
219                                          ch);
220 }
221
222
223 /**
224  * Initialize the ATS connectivity suggestion client handle.
225  *
226  * @param cfg configuration to use
227  * @return ats connectivity handle, NULL on error
228  */
229 struct GNUNET_ATS_ConnectivityHandle *
230 GNUNET_ATS_connectivity_init (const struct GNUNET_CONFIGURATION_Handle *cfg)
231 {
232   struct GNUNET_ATS_ConnectivityHandle *ch;
233
234   ch = GNUNET_new (struct GNUNET_ATS_ConnectivityHandle);
235   ch->cfg = cfg;
236   ch->sug_requests = GNUNET_CONTAINER_multipeermap_create (32,
237                                                            GNUNET_YES);
238   reconnect (ch);
239   return ch;
240 }
241
242
243 /**
244  * Function called to free all `struct GNUNET_ATS_SuggestHandles`
245  * in the map.
246  *
247  * @param cls NULL
248  * @param key the key
249  * @param value the value to free
250  * @return #GNUNET_OK (continue to iterate)
251  */
252 static int
253 free_sug_handle (void *cls,
254                  const struct GNUNET_PeerIdentity *key,
255                  void *value)
256 {
257   struct GNUNET_ATS_ConnectivitySuggestHandle *cur = value;
258
259   GNUNET_free (cur);
260   return GNUNET_OK;
261 }
262
263
264 /**
265  * Client is done with ATS connectivity management, release resources.
266  *
267  * @param ch handle to release
268  */
269 void
270 GNUNET_ATS_connectivity_done (struct GNUNET_ATS_ConnectivityHandle *ch)
271 {
272   if (NULL != ch->mq)
273   {
274     GNUNET_MQ_destroy (ch->mq);
275     ch->mq = NULL;
276   }
277   if (NULL != ch->client)
278   {
279     GNUNET_CLIENT_disconnect (ch->client);
280     ch->client = NULL;
281   }
282   if (NULL != ch->task)
283   {
284     GNUNET_SCHEDULER_cancel (ch->task);
285     ch->task = NULL;
286   }
287   GNUNET_CONTAINER_multipeermap_iterate (ch->sug_requests,
288                                          &free_sug_handle,
289                                          NULL);
290   GNUNET_CONTAINER_multipeermap_destroy (ch->sug_requests);
291   GNUNET_free (ch);
292 }
293
294
295 /**
296  * We would like to receive address suggestions for a peer. ATS will
297  * respond with a call to the continuation immediately containing an address or
298  * no address if none is available. ATS can suggest more addresses until we call
299  * #GNUNET_ATS_connectivity_suggest_cancel().
300  *
301  * @param ch handle
302  * @param peer identity of the peer we need an address for
303  * @return suggest handle, NULL if a request is already pending
304  */
305 struct GNUNET_ATS_ConnectivitySuggestHandle *
306 GNUNET_ATS_connectivity_suggest (struct GNUNET_ATS_ConnectivityHandle *ch,
307                                  const struct GNUNET_PeerIdentity *peer)
308 {
309   struct GNUNET_ATS_ConnectivitySuggestHandle *s;
310
311   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
312               "Requesting ATS to suggest address for `%s'\n",
313               GNUNET_i2s (peer));
314   s = GNUNET_new (struct GNUNET_ATS_ConnectivitySuggestHandle);
315   s->ch = ch;
316   s->id = *peer;
317   if (GNUNET_OK !=
318       GNUNET_CONTAINER_multipeermap_put (ch->sug_requests,
319                                          &s->id,
320                                          s,
321                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
322   {
323     GNUNET_break (0);
324     return NULL;
325   }
326   if (NULL == ch->mq)
327     return s;
328   (void) transmit_suggestion (ch,
329                               &s->id,
330                               s);
331   return s;
332 }
333
334
335 /**
336  * We no longer care about being connected to a peer.
337  *
338  * @param sh handle to stop
339  */
340 void
341 GNUNET_ATS_connectivity_suggest_cancel (struct GNUNET_ATS_ConnectivitySuggestHandle *sh)
342 {
343   struct GNUNET_ATS_ConnectivityHandle *ch = sh->ch;
344   struct GNUNET_MQ_Envelope *ev;
345   struct RequestAddressMessage *m;
346
347   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
348               "Telling ATS we no longer care for an address for `%s'\n",
349               GNUNET_i2s (&sh->id));
350   GNUNET_assert (GNUNET_OK ==
351                  GNUNET_CONTAINER_multipeermap_remove (ch->sug_requests,
352                                                        &sh->id,
353                                                        sh));
354   if (NULL == ch->mq)
355   {
356     GNUNET_free (sh);
357     return;
358   }
359   ev = GNUNET_MQ_msg (m, GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS_CANCEL);
360   m->reserved = htonl (0);
361   m->peer = sh->id;
362   GNUNET_MQ_send (ch->mq, ev);
363   GNUNET_free (sh);
364 }
365
366
367 /* end of ats_api_connectivity.c */