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