aboutsummaryrefslogtreecommitdiff
path: root/src/ats/ats_api_connectivity.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ats/ats_api_connectivity.c')
-rw-r--r--src/ats/ats_api_connectivity.c221
1 files changed, 109 insertions, 112 deletions
diff --git a/src/ats/ats_api_connectivity.c b/src/ats/ats_api_connectivity.c
index d1295d04d..2ff734c25 100644
--- a/src/ats/ats_api_connectivity.c
+++ b/src/ats/ats_api_connectivity.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file ats/ats_api_connectivity.c 21 * @file ats/ats_api_connectivity.c
22 * @brief enable clients to ask ATS about establishing connections to peers 22 * @brief enable clients to ask ATS about establishing connections to peers
@@ -28,14 +28,13 @@
28#include "ats.h" 28#include "ats.h"
29 29
30 30
31#define LOG(kind,...) GNUNET_log_from(kind, "ats-connectivity-api", __VA_ARGS__) 31#define LOG(kind, ...) GNUNET_log_from(kind, "ats-connectivity-api", __VA_ARGS__)
32 32
33 33
34/** 34/**
35 * Handle for ATS address suggestion requests. 35 * Handle for ATS address suggestion requests.
36 */ 36 */
37struct GNUNET_ATS_ConnectivitySuggestHandle 37struct GNUNET_ATS_ConnectivitySuggestHandle {
38{
39 /** 38 /**
40 * ID of the peer for which address suggestion was requested. 39 * ID of the peer for which address suggestion was requested.
41 */ 40 */
@@ -56,9 +55,7 @@ struct GNUNET_ATS_ConnectivitySuggestHandle
56/** 55/**
57 * Handle to the ATS subsystem for connectivity management. 56 * Handle to the ATS subsystem for connectivity management.
58 */ 57 */
59struct GNUNET_ATS_ConnectivityHandle 58struct GNUNET_ATS_ConnectivityHandle {
60{
61
62 /** 59 /**
63 * Our configuration. 60 * Our configuration.
64 */ 61 */
@@ -94,7 +91,7 @@ struct GNUNET_ATS_ConnectivityHandle
94 * @param ch handle to use to re-connect. 91 * @param ch handle to use to re-connect.
95 */ 92 */
96static void 93static void
97reconnect (struct GNUNET_ATS_ConnectivityHandle *ch); 94reconnect(struct GNUNET_ATS_ConnectivityHandle *ch);
98 95
99 96
100/** 97/**
@@ -103,12 +100,12 @@ reconnect (struct GNUNET_ATS_ConnectivityHandle *ch);
103 * @param cls handle to use to re-connect. 100 * @param cls handle to use to re-connect.
104 */ 101 */
105static void 102static void
106reconnect_task (void *cls) 103reconnect_task(void *cls)
107{ 104{
108 struct GNUNET_ATS_ConnectivityHandle *ch = cls; 105 struct GNUNET_ATS_ConnectivityHandle *ch = cls;
109 106
110 ch->task = NULL; 107 ch->task = NULL;
111 reconnect (ch); 108 reconnect(ch);
112} 109}
113 110
114 111
@@ -118,17 +115,17 @@ reconnect_task (void *cls)
118 * @param ch our handle 115 * @param ch our handle
119 */ 116 */
120static void 117static void
121force_reconnect (struct GNUNET_ATS_ConnectivityHandle *ch) 118force_reconnect(struct GNUNET_ATS_ConnectivityHandle *ch)
122{ 119{
123 if (NULL != ch->mq) 120 if (NULL != ch->mq)
124 { 121 {
125 GNUNET_MQ_destroy (ch->mq); 122 GNUNET_MQ_destroy(ch->mq);
126 ch->mq = NULL; 123 ch->mq = NULL;
127 } 124 }
128 ch->backoff = GNUNET_TIME_STD_BACKOFF (ch->backoff); 125 ch->backoff = GNUNET_TIME_STD_BACKOFF(ch->backoff);
129 ch->task = GNUNET_SCHEDULER_add_delayed (ch->backoff, 126 ch->task = GNUNET_SCHEDULER_add_delayed(ch->backoff,
130 &reconnect_task, 127 &reconnect_task,
131 ch); 128 ch);
132} 129}
133 130
134 131
@@ -140,15 +137,15 @@ force_reconnect (struct GNUNET_ATS_ConnectivityHandle *ch)
140 * @param error details about the error 137 * @param error details about the error
141 */ 138 */
142static void 139static void
143error_handler (void *cls, 140error_handler(void *cls,
144 enum GNUNET_MQ_Error error) 141 enum GNUNET_MQ_Error error)
145{ 142{
146 struct GNUNET_ATS_ConnectivityHandle *ch = cls; 143 struct GNUNET_ATS_ConnectivityHandle *ch = cls;
147 144
148 LOG (GNUNET_ERROR_TYPE_DEBUG, 145 LOG(GNUNET_ERROR_TYPE_DEBUG,
149 "ATS connection died (code %d), reconnecting\n", 146 "ATS connection died (code %d), reconnecting\n",
150 (int) error); 147 (int)error);
151 force_reconnect (ch); 148 force_reconnect(ch);
152} 149}
153 150
154 151
@@ -162,9 +159,9 @@ error_handler (void *cls,
162 * failure (message queue no longer exists) 159 * failure (message queue no longer exists)
163 */ 160 */
164static int 161static int
165transmit_suggestion (void *cls, 162transmit_suggestion(void *cls,
166 const struct GNUNET_PeerIdentity *peer, 163 const struct GNUNET_PeerIdentity *peer,
167 void *value) 164 void *value)
168{ 165{
169 struct GNUNET_ATS_ConnectivityHandle *ch = cls; 166 struct GNUNET_ATS_ConnectivityHandle *ch = cls;
170 struct GNUNET_ATS_ConnectivitySuggestHandle *sh = value; 167 struct GNUNET_ATS_ConnectivitySuggestHandle *sh = value;
@@ -173,10 +170,10 @@ transmit_suggestion (void *cls,
173 170
174 if (NULL == ch->mq) 171 if (NULL == ch->mq)
175 return GNUNET_SYSERR; 172 return GNUNET_SYSERR;
176 ev = GNUNET_MQ_msg (m, GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS); 173 ev = GNUNET_MQ_msg(m, GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS);
177 m->strength = htonl (sh->strength); 174 m->strength = htonl(sh->strength);
178 m->peer = *peer; 175 m->peer = *peer;
179 GNUNET_MQ_send (ch->mq, ev); 176 GNUNET_MQ_send(ch->mq, ev);
180 return GNUNET_OK; 177 return GNUNET_OK;
181} 178}
182 179
@@ -187,33 +184,33 @@ transmit_suggestion (void *cls,
187 * @param ch handle to use to re-connect. 184 * @param ch handle to use to re-connect.
188 */ 185 */
189static void 186static void
190reconnect (struct GNUNET_ATS_ConnectivityHandle *ch) 187reconnect(struct GNUNET_ATS_ConnectivityHandle *ch)
191{ 188{
192 static const struct GNUNET_MQ_MessageHandler handlers[] = 189 static const struct GNUNET_MQ_MessageHandler handlers[] =
193 { { NULL, 0, 0 } }; 190 { { NULL, 0, 0 } };
194 struct GNUNET_MQ_Envelope *ev; 191 struct GNUNET_MQ_Envelope *ev;
195 struct ClientStartMessage *init; 192 struct ClientStartMessage *init;
196 193
197 GNUNET_assert (NULL == ch->mq); 194 GNUNET_assert(NULL == ch->mq);
198 ch->mq = GNUNET_CLIENT_connect (ch->cfg, 195 ch->mq = GNUNET_CLIENT_connect(ch->cfg,
199 "ats", 196 "ats",
200 handlers, 197 handlers,
201 &error_handler, 198 &error_handler,
202 ch); 199 ch);
203 if (NULL == ch->mq) 200 if (NULL == ch->mq)
204 { 201 {
205 force_reconnect (ch); 202 force_reconnect(ch);
206 return; 203 return;
207 } 204 }
208 ev = GNUNET_MQ_msg (init, 205 ev = GNUNET_MQ_msg(init,
209 GNUNET_MESSAGE_TYPE_ATS_START); 206 GNUNET_MESSAGE_TYPE_ATS_START);
210 init->start_flag = htonl (START_FLAG_CONNECTION_SUGGESTION); 207 init->start_flag = htonl(START_FLAG_CONNECTION_SUGGESTION);
211 GNUNET_MQ_send (ch->mq, ev); 208 GNUNET_MQ_send(ch->mq, ev);
212 if (NULL == ch->mq) 209 if (NULL == ch->mq)
213 return; 210 return;
214 GNUNET_CONTAINER_multipeermap_iterate (ch->sug_requests, 211 GNUNET_CONTAINER_multipeermap_iterate(ch->sug_requests,
215 &transmit_suggestion, 212 &transmit_suggestion,
216 ch); 213 ch);
217} 214}
218 215
219 216
@@ -224,15 +221,15 @@ reconnect (struct GNUNET_ATS_ConnectivityHandle *ch)
224 * @return ats connectivity handle, NULL on error 221 * @return ats connectivity handle, NULL on error
225 */ 222 */
226struct GNUNET_ATS_ConnectivityHandle * 223struct GNUNET_ATS_ConnectivityHandle *
227GNUNET_ATS_connectivity_init (const struct GNUNET_CONFIGURATION_Handle *cfg) 224GNUNET_ATS_connectivity_init(const struct GNUNET_CONFIGURATION_Handle *cfg)
228{ 225{
229 struct GNUNET_ATS_ConnectivityHandle *ch; 226 struct GNUNET_ATS_ConnectivityHandle *ch;
230 227
231 ch = GNUNET_new (struct GNUNET_ATS_ConnectivityHandle); 228 ch = GNUNET_new(struct GNUNET_ATS_ConnectivityHandle);
232 ch->cfg = cfg; 229 ch->cfg = cfg;
233 ch->sug_requests = GNUNET_CONTAINER_multipeermap_create (32, 230 ch->sug_requests = GNUNET_CONTAINER_multipeermap_create(32,
234 GNUNET_YES); 231 GNUNET_YES);
235 reconnect (ch); 232 reconnect(ch);
236 return ch; 233 return ch;
237} 234}
238 235
@@ -247,13 +244,13 @@ GNUNET_ATS_connectivity_init (const struct GNUNET_CONFIGURATION_Handle *cfg)
247 * @return #GNUNET_OK (continue to iterate) 244 * @return #GNUNET_OK (continue to iterate)
248 */ 245 */
249static int 246static int
250free_sug_handle (void *cls, 247free_sug_handle(void *cls,
251 const struct GNUNET_PeerIdentity *key, 248 const struct GNUNET_PeerIdentity *key,
252 void *value) 249 void *value)
253{ 250{
254 struct GNUNET_ATS_ConnectivitySuggestHandle *cur = value; 251 struct GNUNET_ATS_ConnectivitySuggestHandle *cur = value;
255 252
256 GNUNET_free (cur); 253 GNUNET_free(cur);
257 return GNUNET_OK; 254 return GNUNET_OK;
258} 255}
259 256
@@ -264,23 +261,23 @@ free_sug_handle (void *cls,
264 * @param ch handle to release 261 * @param ch handle to release
265 */ 262 */
266void 263void
267GNUNET_ATS_connectivity_done (struct GNUNET_ATS_ConnectivityHandle *ch) 264GNUNET_ATS_connectivity_done(struct GNUNET_ATS_ConnectivityHandle *ch)
268{ 265{
269 if (NULL != ch->mq) 266 if (NULL != ch->mq)
270 { 267 {
271 GNUNET_MQ_destroy (ch->mq); 268 GNUNET_MQ_destroy(ch->mq);
272 ch->mq = NULL; 269 ch->mq = NULL;
273 } 270 }
274 if (NULL != ch->task) 271 if (NULL != ch->task)
275 { 272 {
276 GNUNET_SCHEDULER_cancel (ch->task); 273 GNUNET_SCHEDULER_cancel(ch->task);
277 ch->task = NULL; 274 ch->task = NULL;
278 } 275 }
279 GNUNET_CONTAINER_multipeermap_iterate (ch->sug_requests, 276 GNUNET_CONTAINER_multipeermap_iterate(ch->sug_requests,
280 &free_sug_handle, 277 &free_sug_handle,
281 NULL); 278 NULL);
282 GNUNET_CONTAINER_multipeermap_destroy (ch->sug_requests); 279 GNUNET_CONTAINER_multipeermap_destroy(ch->sug_requests);
283 GNUNET_free (ch); 280 GNUNET_free(ch);
284} 281}
285 282
286 283
@@ -296,36 +293,36 @@ GNUNET_ATS_connectivity_done (struct GNUNET_ATS_ConnectivityHandle *ch)
296 * @return suggest handle, NULL if a request is already pending 293 * @return suggest handle, NULL if a request is already pending
297 */ 294 */
298struct GNUNET_ATS_ConnectivitySuggestHandle * 295struct GNUNET_ATS_ConnectivitySuggestHandle *
299GNUNET_ATS_connectivity_suggest (struct GNUNET_ATS_ConnectivityHandle *ch, 296GNUNET_ATS_connectivity_suggest(struct GNUNET_ATS_ConnectivityHandle *ch,
300 const struct GNUNET_PeerIdentity *peer, 297 const struct GNUNET_PeerIdentity *peer,
301 uint32_t strength) 298 uint32_t strength)
302{ 299{
303 struct GNUNET_ATS_ConnectivitySuggestHandle *s; 300 struct GNUNET_ATS_ConnectivitySuggestHandle *s;
304 301
305 s = GNUNET_new (struct GNUNET_ATS_ConnectivitySuggestHandle); 302 s = GNUNET_new(struct GNUNET_ATS_ConnectivitySuggestHandle);
306 s->ch = ch; 303 s->ch = ch;
307 s->id = *peer; 304 s->id = *peer;
308 s->strength = strength; 305 s->strength = strength;
309 if (GNUNET_OK != 306 if (GNUNET_OK !=
310 GNUNET_CONTAINER_multipeermap_put (ch->sug_requests, 307 GNUNET_CONTAINER_multipeermap_put(ch->sug_requests,
311 &s->id, 308 &s->id,
312 s, 309 s,
313 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)) 310 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
314 { 311 {
315 LOG (GNUNET_ERROR_TYPE_DEBUG, 312 LOG(GNUNET_ERROR_TYPE_DEBUG,
316 "Not requesting ATS to suggest address for `%s', request already pending\n", 313 "Not requesting ATS to suggest address for `%s', request already pending\n",
317 GNUNET_i2s (peer)); 314 GNUNET_i2s(peer));
318 GNUNET_free (s); 315 GNUNET_free(s);
319 return NULL; 316 return NULL;
320 } 317 }
321 LOG (GNUNET_ERROR_TYPE_DEBUG, 318 LOG(GNUNET_ERROR_TYPE_DEBUG,
322 "Requesting ATS to suggest address for `%s'\n", 319 "Requesting ATS to suggest address for `%s'\n",
323 GNUNET_i2s (peer)); 320 GNUNET_i2s(peer));
324 if (NULL == ch->mq) 321 if (NULL == ch->mq)
325 return s; 322 return s;
326 (void) transmit_suggestion (ch, 323 (void)transmit_suggestion(ch,
327 &s->id, 324 &s->id,
328 s); 325 s);
329 return s; 326 return s;
330} 327}
331 328
@@ -336,30 +333,30 @@ GNUNET_ATS_connectivity_suggest (struct GNUNET_ATS_ConnectivityHandle *ch,
336 * @param sh handle to stop 333 * @param sh handle to stop
337 */ 334 */
338void 335void
339GNUNET_ATS_connectivity_suggest_cancel (struct GNUNET_ATS_ConnectivitySuggestHandle *sh) 336GNUNET_ATS_connectivity_suggest_cancel(struct GNUNET_ATS_ConnectivitySuggestHandle *sh)
340{ 337{
341 struct GNUNET_ATS_ConnectivityHandle *ch = sh->ch; 338 struct GNUNET_ATS_ConnectivityHandle *ch = sh->ch;
342 struct GNUNET_MQ_Envelope *ev; 339 struct GNUNET_MQ_Envelope *ev;
343 struct RequestAddressMessage *m; 340 struct RequestAddressMessage *m;
344 341
345 LOG (GNUNET_ERROR_TYPE_DEBUG, 342 LOG(GNUNET_ERROR_TYPE_DEBUG,
346 "Telling ATS we no longer care for an address for `%s'\n", 343 "Telling ATS we no longer care for an address for `%s'\n",
347 GNUNET_i2s (&sh->id)); 344 GNUNET_i2s(&sh->id));
348 GNUNET_assert (GNUNET_OK == 345 GNUNET_assert(GNUNET_OK ==
349 GNUNET_CONTAINER_multipeermap_remove (ch->sug_requests, 346 GNUNET_CONTAINER_multipeermap_remove(ch->sug_requests,
350 &sh->id, 347 &sh->id,
351 sh)); 348 sh));
352 if (NULL == ch->mq) 349 if (NULL == ch->mq)
353 { 350 {
354 GNUNET_free (sh); 351 GNUNET_free(sh);
355 return; 352 return;
356 } 353 }
357 ev = GNUNET_MQ_msg (m, 354 ev = GNUNET_MQ_msg(m,
358 GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS_CANCEL); 355 GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS_CANCEL);
359 m->strength = htonl (0); 356 m->strength = htonl(0);
360 m->peer = sh->id; 357 m->peer = sh->id;
361 GNUNET_MQ_send (ch->mq, ev); 358 GNUNET_MQ_send(ch->mq, ev);
362 GNUNET_free (sh); 359 GNUNET_free(sh);
363} 360}
364 361
365 362