-cleaning up bandwidth allocation routine
[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
83
84 /**
85  * Re-establish the connection to the ATS service.
86  *
87  * @param ch handle to use to re-connect.
88  */
89 static void
90 reconnect (struct GNUNET_ATS_ConnectivityHandle *ch);
91
92
93 /**
94  * Re-establish the connection to the ATS service.
95  *
96  * @param cls handle to use to re-connect.
97  * @param tc scheduler context
98  */
99 static void
100 reconnect_task (void *cls,
101                 const struct GNUNET_SCHEDULER_TaskContext *tc)
102 {
103   struct GNUNET_ATS_ConnectivityHandle *ch = cls;
104
105   ch->task = NULL;
106   reconnect (ch);
107 }
108
109
110 /**
111  * Disconnect from ATS and then reconnect.
112  *
113  * @param ch our handle
114  */
115 static void
116 force_reconnect (struct GNUNET_ATS_ConnectivityHandle *ch)
117 {
118   if (NULL != ch->mq)
119   {
120     GNUNET_MQ_destroy (ch->mq);
121     ch->mq = NULL;
122   }
123   if (NULL != ch->client)
124   {
125     GNUNET_CLIENT_disconnect (ch->client);
126     ch->client = NULL;
127   }
128   ch->task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
129                                            &reconnect_task,
130                                            ch);
131 }
132
133
134 /**
135  * We encountered an error handling the MQ to the
136  * ATS service.  Reconnect.
137  *
138  * @param cls the `struct GNUNET_ATS_ConnectivityHandle`
139  * @param error details about the error
140  */
141 static void
142 error_handler (void *cls,
143                enum GNUNET_MQ_Error error)
144 {
145   struct GNUNET_ATS_ConnectivityHandle *ch = cls;
146
147   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
148               "ATS connection died (code %d), reconnecting\n",
149               (int) error);
150   force_reconnect (ch);
151 }
152
153
154 /**
155  * Transmit request for an address suggestion.
156  *
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)
162  */
163 static int
164 transmit_suggestion (void *cls,
165                      const struct GNUNET_PeerIdentity *peer,
166                      void *value)
167 {
168   struct GNUNET_ATS_ConnectivityHandle *ch = cls;
169   struct GNUNET_MQ_Envelope *ev;
170   struct RequestAddressMessage *m;
171
172   if (NULL == ch->mq)
173     return GNUNET_SYSERR;
174   ev = GNUNET_MQ_msg (m, GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS);
175   m->reserved = htonl (0);
176   m->peer = *peer;
177   GNUNET_MQ_send (ch->mq, ev);
178   return GNUNET_OK;
179 }
180
181
182 /**
183  * Re-establish the connection to the ATS service.
184  *
185  * @param ch handle to use to re-connect.
186  */
187 static void
188 reconnect (struct GNUNET_ATS_ConnectivityHandle *ch)
189 {
190   static const struct GNUNET_MQ_MessageHandler handlers[] =
191     { { NULL, 0, 0 } };
192   struct GNUNET_MQ_Envelope *ev;
193   struct ClientStartMessage *init;
194
195   GNUNET_assert (NULL == ch->client);
196   ch->client = GNUNET_CLIENT_connect ("ats", ch->cfg);
197   if (NULL == ch->client)
198   {
199     force_reconnect (ch);
200     return;
201   }
202   ch->mq = GNUNET_MQ_queue_for_connection_client (ch->client,
203                                                   handlers,
204                                                   &error_handler,
205                                                   ch);
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);
210   if (NULL == ch->mq)
211     return;
212   GNUNET_CONTAINER_multipeermap_iterate (ch->sug_requests,
213                                          &transmit_suggestion,
214                                          ch);
215 }
216
217
218 /**
219  * Initialize the ATS connectivity suggestion client handle.
220  *
221  * @param cfg configuration to use
222  * @return ats connectivity handle, NULL on error
223  */
224 struct GNUNET_ATS_ConnectivityHandle *
225 GNUNET_ATS_connectivity_init (const struct GNUNET_CONFIGURATION_Handle *cfg)
226 {
227   struct GNUNET_ATS_ConnectivityHandle *ch;
228
229   ch = GNUNET_new (struct GNUNET_ATS_ConnectivityHandle);
230   ch->cfg = cfg;
231   ch->sug_requests = GNUNET_CONTAINER_multipeermap_create (32,
232                                                            GNUNET_YES);
233   reconnect (ch);
234   return ch;
235 }
236
237
238 /**
239  * Function called to free all `struct GNUNET_ATS_SuggestHandles`
240  * in the map.
241  *
242  * @param cls NULL
243  * @param key the key
244  * @param value the value to free
245  * @return #GNUNET_OK (continue to iterate)
246  */
247 static int
248 free_sug_handle (void *cls,
249                  const struct GNUNET_PeerIdentity *key,
250                  void *value)
251 {
252   struct GNUNET_ATS_ConnectivitySuggestHandle *cur = value;
253
254   GNUNET_free (cur);
255   return GNUNET_OK;
256 }
257
258
259 /**
260  * Client is done with ATS connectivity management, release resources.
261  *
262  * @param ch handle to release
263  */
264 void
265 GNUNET_ATS_connectivity_done (struct GNUNET_ATS_ConnectivityHandle *ch)
266 {
267   if (NULL != ch->mq)
268   {
269     GNUNET_MQ_destroy (ch->mq);
270     ch->mq = NULL;
271   }
272   if (NULL != ch->client)
273   {
274     GNUNET_CLIENT_disconnect (ch->client);
275     ch->client = NULL;
276   }
277   if (NULL != ch->task)
278   {
279     GNUNET_SCHEDULER_cancel (ch->task);
280     ch->task = NULL;
281   }
282   GNUNET_CONTAINER_multipeermap_iterate (ch->sug_requests,
283                                          &free_sug_handle,
284                                          NULL);
285   GNUNET_CONTAINER_multipeermap_destroy (ch->sug_requests);
286   GNUNET_free (ch);
287 }
288
289
290 /**
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().
295  *
296  * @param ch handle
297  * @param peer identity of the peer we need an address for
298  * @return suggest handle, NULL if a request is already pending
299  */
300 struct GNUNET_ATS_ConnectivitySuggestHandle *
301 GNUNET_ATS_connectivity_suggest (struct GNUNET_ATS_ConnectivityHandle *ch,
302                                  const struct GNUNET_PeerIdentity *peer)
303 {
304   struct GNUNET_ATS_ConnectivitySuggestHandle *s;
305
306   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
307               "Requesting ATS to suggest address for `%s'\n",
308               GNUNET_i2s (peer));
309   s = GNUNET_new (struct GNUNET_ATS_ConnectivitySuggestHandle);
310   s->ch = ch;
311   s->id = *peer;
312   if (GNUNET_OK !=
313       GNUNET_CONTAINER_multipeermap_put (ch->sug_requests,
314                                          &s->id,
315                                          s,
316                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
317   {
318     GNUNET_break (0);
319     return NULL;
320   }
321   if (NULL == ch->mq)
322     return s;
323   (void) transmit_suggestion (ch,
324                               &s->id,
325                               s);
326   return s;
327 }
328
329
330 /**
331  * We no longer care about being connected to a peer.
332  *
333  * @param sh handle to stop
334  */
335 void
336 GNUNET_ATS_connectivity_suggest_cancel (struct GNUNET_ATS_ConnectivitySuggestHandle *sh)
337 {
338   struct GNUNET_ATS_ConnectivityHandle *ch = sh->ch;
339   struct GNUNET_MQ_Envelope *ev;
340   struct RequestAddressMessage *m;
341
342   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
343               "Telling ATS we no longer care for an address for `%s'\n",
344               GNUNET_i2s (&sh->id));
345   GNUNET_assert (GNUNET_OK ==
346                  GNUNET_CONTAINER_multipeermap_remove (ch->sug_requests,
347                                                        &sh->id,
348                                                        sh));
349   if (NULL == ch->mq)
350   {
351     GNUNET_free (sh);
352     return;
353   }
354   ev = GNUNET_MQ_msg (m, GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS_CANCEL);
355   m->reserved = htonl (0);
356   m->peer = sh->id;
357   GNUNET_MQ_send (ch->mq, ev);
358   GNUNET_free (sh);
359 }
360
361
362 /* end of ats_api_connectivity.c */