aboutsummaryrefslogtreecommitdiff
path: root/src/ats/ats_api_connectivity.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2015-02-03 14:11:03 +0000
committerChristian Grothoff <christian@grothoff.org>2015-02-03 14:11:03 +0000
commitb2e99dd91c01b5fcc16b3b1031df897aeec05eef (patch)
tree31caea8a19fd943f8a9f7f7f3c19e52e11d95a77 /src/ats/ats_api_connectivity.c
parent55cecdea8638fa23c68e7ade7a8d5629da658f9e (diff)
downloadgnunet-b2e99dd91c01b5fcc16b3b1031df897aeec05eef.tar.gz
gnunet-b2e99dd91c01b5fcc16b3b1031df897aeec05eef.zip
separate ATS connectivity suggestions from ATS scheduling API
Diffstat (limited to 'src/ats/ats_api_connectivity.c')
-rw-r--r--src/ats/ats_api_connectivity.c356
1 files changed, 356 insertions, 0 deletions
diff --git a/src/ats/ats_api_connectivity.c b/src/ats/ats_api_connectivity.c
new file mode 100644
index 000000000..9198118a4
--- /dev/null
+++ b/src/ats/ats_api_connectivity.c
@@ -0,0 +1,356 @@
1/*
2 This file is part of GNUnet.
3 (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 */
34struct 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 */
51struct 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 */
89static void
90reconnect (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 */
99static void
100reconnect_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 */
115static void
116force_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 */
141static void
142error_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 */
163static int
164transmit_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 */
187static void
188reconnect (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 */
224struct GNUNET_ATS_ConnectivityHandle *
225GNUNET_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 */
247static int
248free_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 */
264void
265GNUNET_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 */
300struct GNUNET_ATS_ConnectivitySuggestHandle *
301GNUNET_ATS_connectivity_suggest (struct GNUNET_ATS_ConnectivityHandle *ch,
302 const struct GNUNET_PeerIdentity *peer)
303{
304 struct GNUNET_ATS_ConnectivitySuggestHandle *s;
305
306 s = GNUNET_new (struct GNUNET_ATS_ConnectivitySuggestHandle);
307 s->ch = ch;
308 s->id = *peer;
309 if (GNUNET_OK !=
310 GNUNET_CONTAINER_multipeermap_put (ch->sug_requests,
311 &s->id,
312 s,
313 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
314 {
315 GNUNET_break (0);
316 return NULL;
317 }
318 if (NULL == ch->mq)
319 return s;
320 (void) transmit_suggestion (ch,
321 &s->id,
322 s);
323 return s;
324}
325
326
327/**
328 * We no longer care about being connected to a peer.
329 *
330 * @param sh handle to stop
331 */
332void
333GNUNET_ATS_connectivity_suggest_cancel (struct GNUNET_ATS_ConnectivitySuggestHandle *sh)
334{
335 struct GNUNET_ATS_ConnectivityHandle *ch = sh->ch;
336 struct GNUNET_MQ_Envelope *ev;
337 struct RequestAddressMessage *m;
338
339 GNUNET_assert (GNUNET_OK ==
340 GNUNET_CONTAINER_multipeermap_remove (ch->sug_requests,
341 &sh->id,
342 sh));
343 if (NULL == ch->mq)
344 {
345 GNUNET_free (sh);
346 return;
347 }
348 ev = GNUNET_MQ_msg (m, GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS_CANCEL);
349 m->reserved = htonl (0);
350 m->peer = sh->id;
351 GNUNET_MQ_send (ch->mq, ev);
352 GNUNET_free (sh);
353}
354
355
356/* end of ats_api_connectivity.c */