aboutsummaryrefslogtreecommitdiff
path: root/src/transport/gnunet-service-transport_ats.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/transport/gnunet-service-transport_ats.c')
-rw-r--r--src/transport/gnunet-service-transport_ats.c750
1 files changed, 376 insertions, 374 deletions
diff --git a/src/transport/gnunet-service-transport_ats.c b/src/transport/gnunet-service-transport_ats.c
index 555302813..9c262dbdf 100644
--- a/src/transport/gnunet-service-transport_ats.c
+++ b/src/transport/gnunet-service-transport_ats.c
@@ -32,13 +32,14 @@
32/** 32/**
33 * Log convenience function. 33 * Log convenience function.
34 */ 34 */
35#define LOG(kind, ...) GNUNET_log_from(kind, "transport-ats", __VA_ARGS__) 35#define LOG(kind, ...) GNUNET_log_from (kind, "transport-ats", __VA_ARGS__)
36 36
37 37
38/** 38/**
39 * Information we track for each address known to ATS. 39 * Information we track for each address known to ATS.
40 */ 40 */
41struct AddressInfo { 41struct AddressInfo
42{
42 /** 43 /**
43 * The address (with peer identity). Must never change 44 * The address (with peer identity). Must never change
44 * while this struct is in the #p2a map. 45 * while this struct is in the #p2a map.
@@ -105,7 +106,8 @@ static unsigned int num_blocked;
105/** 106/**
106 * Closure for #find_ai_cb() and #find_ai_no_session_cb(). 107 * Closure for #find_ai_cb() and #find_ai_no_session_cb().
107 */ 108 */
108struct FindClosure { 109struct FindClosure
110{
109 /** 111 /**
110 * Session to look for (only used if the address is inbound). 112 * Session to look for (only used if the address is inbound).
111 */ 113 */
@@ -129,16 +131,16 @@ struct FindClosure {
129 * is changed. 131 * is changed.
130 */ 132 */
131static void 133static void
132publish_p2a_stat_update() 134publish_p2a_stat_update ()
133{ 135{
134 GNUNET_STATISTICS_set(GST_stats, 136 GNUNET_STATISTICS_set (GST_stats,
135 gettext_noop("# Addresses given to ATS"), 137 gettext_noop ("# Addresses given to ATS"),
136 GNUNET_CONTAINER_multipeermap_size(p2a) - num_blocked, 138 GNUNET_CONTAINER_multipeermap_size (p2a) - num_blocked,
137 GNUNET_NO); 139 GNUNET_NO);
138 GNUNET_STATISTICS_set(GST_stats, 140 GNUNET_STATISTICS_set (GST_stats,
139 "# blocked addresses", 141 "# blocked addresses",
140 num_blocked, 142 num_blocked,
141 GNUNET_NO); 143 GNUNET_NO);
142} 144}
143 145
144 146
@@ -153,21 +155,21 @@ publish_p2a_stat_update()
153 * @return #GNUNET_YES to continue to iterate, #GNUNET_NO if we found the value 155 * @return #GNUNET_YES to continue to iterate, #GNUNET_NO if we found the value
154 */ 156 */
155static int 157static int
156find_ai_cb(void *cls, 158find_ai_cb (void *cls,
157 const struct GNUNET_PeerIdentity *key, 159 const struct GNUNET_PeerIdentity *key,
158 void *value) 160 void *value)
159{ 161{
160 struct FindClosure *fc = cls; 162 struct FindClosure *fc = cls;
161 struct AddressInfo *ai = value; 163 struct AddressInfo *ai = value;
162 164
163 if ((0 == 165 if ((0 ==
164 GNUNET_HELLO_address_cmp(fc->address, 166 GNUNET_HELLO_address_cmp (fc->address,
165 ai->address)) && 167 ai->address)) &&
166 (fc->session == ai->session)) 168 (fc->session == ai->session))
167 { 169 {
168 fc->ret = ai; 170 fc->ret = ai;
169 return GNUNET_NO; 171 return GNUNET_NO;
170 } 172 }
171 return GNUNET_YES; 173 return GNUNET_YES;
172} 174}
173 175
@@ -181,18 +183,18 @@ find_ai_cb(void *cls,
181 * @return NULL if this combination is unknown 183 * @return NULL if this combination is unknown
182 */ 184 */
183static struct AddressInfo * 185static struct AddressInfo *
184find_ai(const struct GNUNET_HELLO_Address *address, 186find_ai (const struct GNUNET_HELLO_Address *address,
185 struct GNUNET_ATS_Session *session) 187 struct GNUNET_ATS_Session *session)
186{ 188{
187 struct FindClosure fc; 189 struct FindClosure fc;
188 190
189 fc.address = address; 191 fc.address = address;
190 fc.session = session; 192 fc.session = session;
191 fc.ret = NULL; 193 fc.ret = NULL;
192 GNUNET_CONTAINER_multipeermap_get_multiple(p2a, 194 GNUNET_CONTAINER_multipeermap_get_multiple (p2a,
193 &address->peer, 195 &address->peer,
194 &find_ai_cb, 196 &find_ai_cb,
195 &fc); 197 &fc);
196 return fc.ret; 198 return fc.ret;
197} 199}
198 200
@@ -207,9 +209,9 @@ find_ai(const struct GNUNET_HELLO_Address *address,
207 * @return #GNUNET_YES to continue to iterate, #GNUNET_NO if we found the value 209 * @return #GNUNET_YES to continue to iterate, #GNUNET_NO if we found the value
208 */ 210 */
209static int 211static int
210find_ai_no_session_cb(void *cls, 212find_ai_no_session_cb (void *cls,
211 const struct GNUNET_PeerIdentity *key, 213 const struct GNUNET_PeerIdentity *key,
212 void *value) 214 void *value)
213{ 215{
214 struct FindClosure *fc = cls; 216 struct FindClosure *fc = cls;
215 struct AddressInfo *ai = value; 217 struct AddressInfo *ai = value;
@@ -217,12 +219,12 @@ find_ai_no_session_cb(void *cls,
217 if (ai->expired) 219 if (ai->expired)
218 return GNUNET_YES; /* expired do not count here */ 220 return GNUNET_YES; /* expired do not count here */
219 if (0 == 221 if (0 ==
220 GNUNET_HELLO_address_cmp(fc->address, 222 GNUNET_HELLO_address_cmp (fc->address,
221 ai->address)) 223 ai->address))
222 { 224 {
223 fc->ret = ai; 225 fc->ret = ai;
224 return GNUNET_NO; 226 return GNUNET_NO;
225 } 227 }
226 return GNUNET_YES; 228 return GNUNET_YES;
227} 229}
228 230
@@ -235,17 +237,17 @@ find_ai_no_session_cb(void *cls,
235 * @return NULL if this combination is unknown 237 * @return NULL if this combination is unknown
236 */ 238 */
237static struct AddressInfo * 239static struct AddressInfo *
238find_ai_no_session(const struct GNUNET_HELLO_Address *address) 240find_ai_no_session (const struct GNUNET_HELLO_Address *address)
239{ 241{
240 struct FindClosure fc; 242 struct FindClosure fc;
241 243
242 fc.address = address; 244 fc.address = address;
243 fc.session = NULL; 245 fc.session = NULL;
244 fc.ret = NULL; 246 fc.ret = NULL;
245 GNUNET_CONTAINER_multipeermap_get_multiple(p2a, 247 GNUNET_CONTAINER_multipeermap_get_multiple (p2a,
246 &address->peer, 248 &address->peer,
247 &find_ai_no_session_cb, 249 &find_ai_no_session_cb,
248 &fc); 250 &fc);
249 return fc.ret; 251 return fc.ret;
250} 252}
251 253
@@ -260,10 +262,10 @@ find_ai_no_session(const struct GNUNET_HELLO_Address *address)
260 * @return #GNUNET_YES if @a address is known, #GNUNET_NO if not. 262 * @return #GNUNET_YES if @a address is known, #GNUNET_NO if not.
261 */ 263 */
262int 264int
263GST_ats_is_known(const struct GNUNET_HELLO_Address *address, 265GST_ats_is_known (const struct GNUNET_HELLO_Address *address,
264 struct GNUNET_ATS_Session *session) 266 struct GNUNET_ATS_Session *session)
265{ 267{
266 return (NULL != find_ai(address, session)) ? GNUNET_YES : GNUNET_NO; 268 return (NULL != find_ai (address, session)) ? GNUNET_YES : GNUNET_NO;
267} 269}
268 270
269 271
@@ -275,9 +277,9 @@ GST_ats_is_known(const struct GNUNET_HELLO_Address *address,
275 * @return #GNUNET_YES if @a address is known, #GNUNET_NO if not. 277 * @return #GNUNET_YES if @a address is known, #GNUNET_NO if not.
276 */ 278 */
277int 279int
278GST_ats_is_known_no_session(const struct GNUNET_HELLO_Address *address) 280GST_ats_is_known_no_session (const struct GNUNET_HELLO_Address *address)
279{ 281{
280 return (NULL != find_ai_no_session(address)) 282 return (NULL != find_ai_no_session (address))
281 ? GNUNET_YES 283 ? GNUNET_YES
282 : GNUNET_NO; 284 : GNUNET_NO;
283} 285}
@@ -290,22 +292,22 @@ GST_ats_is_known_no_session(const struct GNUNET_HELLO_Address *address)
290 * @param cls the `struct AddressInfo` of the address to unblock 292 * @param cls the `struct AddressInfo` of the address to unblock
291 */ 293 */
292static void 294static void
293unblock_address(void *cls) 295unblock_address (void *cls)
294{ 296{
295 struct AddressInfo *ai = cls; 297 struct AddressInfo *ai = cls;
296 298
297 ai->unblock_task = NULL; 299 ai->unblock_task = NULL;
298 LOG(GNUNET_ERROR_TYPE_DEBUG, 300 LOG (GNUNET_ERROR_TYPE_DEBUG,
299 "Unblocking address %s of peer %s\n", 301 "Unblocking address %s of peer %s\n",
300 GST_plugins_a2s(ai->address), 302 GST_plugins_a2s (ai->address),
301 GNUNET_i2s(&ai->address->peer)); 303 GNUNET_i2s (&ai->address->peer));
302 ai->ar = GNUNET_ATS_address_add(GST_ats, 304 ai->ar = GNUNET_ATS_address_add (GST_ats,
303 ai->address, 305 ai->address,
304 ai->session, 306 ai->session,
305 &ai->properties); 307 &ai->properties);
306 GNUNET_break(NULL != ai->ar); 308 GNUNET_break (NULL != ai->ar);
307 num_blocked--; 309 num_blocked--;
308 publish_p2a_stat_update(); 310 publish_p2a_stat_update ();
309} 311}
310 312
311 313
@@ -319,47 +321,47 @@ unblock_address(void *cls)
319 * @param session the session (can be NULL) 321 * @param session the session (can be NULL)
320 */ 322 */
321void 323void
322GST_ats_block_address(const struct GNUNET_HELLO_Address *address, 324GST_ats_block_address (const struct GNUNET_HELLO_Address *address,
323 struct GNUNET_ATS_Session *session) 325 struct GNUNET_ATS_Session *session)
324{ 326{
325 struct AddressInfo *ai; 327 struct AddressInfo *ai;
326 328
327 if (0 == 329 if (0 ==
328 memcmp(&GST_my_identity, 330 memcmp (&GST_my_identity,
329 &address->peer, 331 &address->peer,
330 sizeof(struct GNUNET_PeerIdentity))) 332 sizeof(struct GNUNET_PeerIdentity)))
331 return; /* our own, ignore! */ 333 return; /* our own, ignore! */
332 ai = find_ai(address, 334 ai = find_ai (address,
333 session); 335 session);
334 if (NULL == ai || NULL == ai->ar) 336 if ((NULL == ai)||(NULL == ai->ar))
335 { 337 {
336 /* The address is already gone/blocked, this can happen during a blacklist 338 /* The address is already gone/blocked, this can happen during a blacklist
337 * callback. */ 339 * callback. */
338 return; 340 return;
339 } 341 }
340 ai->back_off = GNUNET_TIME_STD_BACKOFF(ai->back_off); 342 ai->back_off = GNUNET_TIME_STD_BACKOFF (ai->back_off);
341 if (GNUNET_YES == 343 if (GNUNET_YES ==
342 GNUNET_HELLO_address_check_option(address, 344 GNUNET_HELLO_address_check_option (address,
343 GNUNET_HELLO_ADDRESS_INFO_INBOUND)) 345 GNUNET_HELLO_ADDRESS_INFO_INBOUND))
344 LOG(GNUNET_ERROR_TYPE_DEBUG, 346 LOG (GNUNET_ERROR_TYPE_DEBUG,
345 "Removing address %s of peer %s from use (inbound died)\n", 347 "Removing address %s of peer %s from use (inbound died)\n",
346 GST_plugins_a2s(address), 348 GST_plugins_a2s (address),
347 GNUNET_i2s(&address->peer)); 349 GNUNET_i2s (&address->peer));
348 else 350 else
349 LOG(GNUNET_ERROR_TYPE_INFO, 351 LOG (GNUNET_ERROR_TYPE_INFO,
350 "Blocking address %s of peer %s from use for %s\n", 352 "Blocking address %s of peer %s from use for %s\n",
351 GST_plugins_a2s(address), 353 GST_plugins_a2s (address),
352 GNUNET_i2s(&address->peer), 354 GNUNET_i2s (&address->peer),
353 GNUNET_STRINGS_relative_time_to_string(ai->back_off, 355 GNUNET_STRINGS_relative_time_to_string (ai->back_off,
354 GNUNET_YES)); 356 GNUNET_YES));
355 /* destroy session and address */ 357 /* destroy session and address */
356 if ((NULL == session) || 358 if ((NULL == session) ||
357 (GNUNET_NO == 359 (GNUNET_NO ==
358 GNUNET_ATS_address_del_session(ai->ar, 360 GNUNET_ATS_address_del_session (ai->ar,
359 session))) 361 session)))
360 { 362 {
361 GNUNET_ATS_address_destroy(ai->ar); 363 GNUNET_ATS_address_destroy (ai->ar);
362 } 364 }
363 /* "ar" has been freed, regardless how the branch 365 /* "ar" has been freed, regardless how the branch
364 above played out: it was either freed in 366 above played out: it was either freed in
365 #GNUNET_ATS_address_del_session() because it was 367 #GNUNET_ATS_address_del_session() because it was
@@ -368,12 +370,12 @@ GST_ats_block_address(const struct GNUNET_HELLO_Address *address,
368 ai->ar = NULL; 370 ai->ar = NULL;
369 371
370 /* determine when the address should come back to life */ 372 /* determine when the address should come back to life */
371 ai->blocked = GNUNET_TIME_relative_to_absolute(ai->back_off); 373 ai->blocked = GNUNET_TIME_relative_to_absolute (ai->back_off);
372 ai->unblock_task = GNUNET_SCHEDULER_add_delayed(ai->back_off, 374 ai->unblock_task = GNUNET_SCHEDULER_add_delayed (ai->back_off,
373 &unblock_address, 375 &unblock_address,
374 ai); 376 ai);
375 num_blocked++; 377 num_blocked++;
376 publish_p2a_stat_update(); 378 publish_p2a_stat_update ();
377} 379}
378 380
379 381
@@ -386,24 +388,24 @@ GST_ats_block_address(const struct GNUNET_HELLO_Address *address,
386 * @param session the session (can be NULL) 388 * @param session the session (can be NULL)
387 */ 389 */
388void 390void
389GST_ats_block_reset(const struct GNUNET_HELLO_Address *address, 391GST_ats_block_reset (const struct GNUNET_HELLO_Address *address,
390 struct GNUNET_ATS_Session *session) 392 struct GNUNET_ATS_Session *session)
391{ 393{
392 struct AddressInfo *ai; 394 struct AddressInfo *ai;
393 395
394 if (0 == 396 if (0 ==
395 memcmp(&GST_my_identity, 397 memcmp (&GST_my_identity,
396 &address->peer, 398 &address->peer,
397 sizeof(struct GNUNET_PeerIdentity))) 399 sizeof(struct GNUNET_PeerIdentity)))
398 return; /* our own, ignore! */ 400 return; /* our own, ignore! */
399 ai = find_ai(address, session); 401 ai = find_ai (address, session);
400 if (NULL == ai) 402 if (NULL == ai)
401 { 403 {
402 GNUNET_break(0); 404 GNUNET_break (0);
403 return; 405 return;
404 } 406 }
405 /* address is in successful use, so it should not be blocked right now */ 407 /* address is in successful use, so it should not be blocked right now */
406 GNUNET_break(NULL == ai->unblock_task); 408 GNUNET_break (NULL == ai->unblock_task);
407 ai->back_off = GNUNET_TIME_UNIT_ZERO; 409 ai->back_off = GNUNET_TIME_UNIT_ZERO;
408} 410}
409 411
@@ -419,60 +421,60 @@ GST_ats_block_reset(const struct GNUNET_HELLO_Address *address,
419 * @param prop performance information 421 * @param prop performance information
420 */ 422 */
421void 423void
422GST_ats_add_inbound_address(const struct GNUNET_HELLO_Address *address, 424GST_ats_add_inbound_address (const struct GNUNET_HELLO_Address *address,
423 struct GNUNET_ATS_Session *session, 425 struct GNUNET_ATS_Session *session,
424 const struct GNUNET_ATS_Properties *prop) 426 const struct GNUNET_ATS_Properties *prop)
425{ 427{
426 struct GNUNET_ATS_AddressRecord *ar; 428 struct GNUNET_ATS_AddressRecord *ar;
427 struct AddressInfo *ai; 429 struct AddressInfo *ai;
428 430
429 if (0 == 431 if (0 ==
430 memcmp(&GST_my_identity, 432 memcmp (&GST_my_identity,
431 &address->peer, 433 &address->peer,
432 sizeof(struct GNUNET_PeerIdentity))) 434 sizeof(struct GNUNET_PeerIdentity)))
433 return; /* our own, ignore! */ 435 return; /* our own, ignore! */
434 436
435 /* Sanity checks for a valid inbound address */ 437 /* Sanity checks for a valid inbound address */
436 if (NULL == address->transport_name) 438 if (NULL == address->transport_name)
437 { 439 {
438 GNUNET_break(0); 440 GNUNET_break (0);
439 return; 441 return;
440 } 442 }
441 GNUNET_break(GNUNET_NT_UNSPECIFIED != prop->scope); 443 GNUNET_break (GNUNET_NT_UNSPECIFIED != prop->scope);
442 GNUNET_assert(GNUNET_YES == 444 GNUNET_assert (GNUNET_YES ==
443 GNUNET_HELLO_address_check_option(address, 445 GNUNET_HELLO_address_check_option (address,
444 GNUNET_HELLO_ADDRESS_INFO_INBOUND)); 446 GNUNET_HELLO_ADDRESS_INFO_INBOUND));
445 GNUNET_assert(NULL != session); 447 GNUNET_assert (NULL != session);
446 ai = find_ai(address, session); 448 ai = find_ai (address, session);
447 if (NULL != ai) 449 if (NULL != ai)
448 { 450 {
449 /* This should only be called for new sessions, and thus 451 /* This should only be called for new sessions, and thus
450 we should not already have the address */ 452 we should not already have the address */
451 GNUNET_break(0); 453 GNUNET_break (0);
452 return; 454 return;
453 } 455 }
454 /* Is indeed new, let's tell ATS */ 456 /* Is indeed new, let's tell ATS */
455 LOG(GNUNET_ERROR_TYPE_DEBUG, 457 LOG (GNUNET_ERROR_TYPE_DEBUG,
456 "Notifying ATS about peer `%s''s new inbound address `%s' session %p in network %s\n", 458 "Notifying ATS about peer `%s''s new inbound address `%s' session %p in network %s\n",
457 GNUNET_i2s(&address->peer), 459 GNUNET_i2s (&address->peer),
458 GST_plugins_a2s(address), 460 GST_plugins_a2s (address),
459 session, 461 session,
460 GNUNET_NT_to_string(prop->scope)); 462 GNUNET_NT_to_string (prop->scope));
461 ar = GNUNET_ATS_address_add(GST_ats, 463 ar = GNUNET_ATS_address_add (GST_ats,
462 address, 464 address,
463 session, 465 session,
464 prop); 466 prop);
465 GNUNET_assert(NULL != ar); 467 GNUNET_assert (NULL != ar);
466 ai = GNUNET_new(struct AddressInfo); 468 ai = GNUNET_new (struct AddressInfo);
467 ai->address = GNUNET_HELLO_address_copy(address); 469 ai->address = GNUNET_HELLO_address_copy (address);
468 ai->session = session; 470 ai->session = session;
469 ai->properties = *prop; 471 ai->properties = *prop;
470 ai->ar = ar; 472 ai->ar = ar;
471 (void)GNUNET_CONTAINER_multipeermap_put(p2a, 473 (void) GNUNET_CONTAINER_multipeermap_put (p2a,
472 &ai->address->peer, 474 &ai->address->peer,
473 ai, 475 ai,
474 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); 476 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
475 publish_p2a_stat_update(); 477 publish_p2a_stat_update ();
476} 478}
477 479
478 480
@@ -484,49 +486,49 @@ GST_ats_add_inbound_address(const struct GNUNET_HELLO_Address *address,
484 * @param prop performance information 486 * @param prop performance information
485 */ 487 */
486void 488void
487GST_ats_add_address(const struct GNUNET_HELLO_Address *address, 489GST_ats_add_address (const struct GNUNET_HELLO_Address *address,
488 const struct GNUNET_ATS_Properties *prop) 490 const struct GNUNET_ATS_Properties *prop)
489{ 491{
490 struct GNUNET_ATS_AddressRecord *ar; 492 struct GNUNET_ATS_AddressRecord *ar;
491 struct AddressInfo *ai; 493 struct AddressInfo *ai;
492 494
493 if (0 == 495 if (0 ==
494 memcmp(&GST_my_identity, 496 memcmp (&GST_my_identity,
495 &address->peer, 497 &address->peer,
496 sizeof(struct GNUNET_PeerIdentity))) 498 sizeof(struct GNUNET_PeerIdentity)))
497 return; /* our own, ignore! */ 499 return; /* our own, ignore! */
498 /* validadte address */ 500 /* validadte address */
499 if (NULL == address->transport_name) 501 if (NULL == address->transport_name)
500 { 502 {
501 GNUNET_break(0); 503 GNUNET_break (0);
502 return; 504 return;
503 } 505 }
504 GNUNET_assert(GNUNET_YES != 506 GNUNET_assert (GNUNET_YES !=
505 GNUNET_HELLO_address_check_option(address, 507 GNUNET_HELLO_address_check_option (address,
506 GNUNET_HELLO_ADDRESS_INFO_INBOUND)); 508 GNUNET_HELLO_ADDRESS_INFO_INBOUND));
507 ai = find_ai_no_session(address); 509 ai = find_ai_no_session (address);
508 GNUNET_assert(NULL == ai); 510 GNUNET_assert (NULL == ai);
509 GNUNET_break(GNUNET_NT_UNSPECIFIED != prop->scope); 511 GNUNET_break (GNUNET_NT_UNSPECIFIED != prop->scope);
510 512
511 /* address seems sane, let's tell ATS */ 513 /* address seems sane, let's tell ATS */
512 LOG(GNUNET_ERROR_TYPE_INFO, 514 LOG (GNUNET_ERROR_TYPE_INFO,
513 "Notifying ATS about peer %s's new address `%s'\n", 515 "Notifying ATS about peer %s's new address `%s'\n",
514 GNUNET_i2s(&address->peer), 516 GNUNET_i2s (&address->peer),
515 GST_plugins_a2s(address)); 517 GST_plugins_a2s (address));
516 ar = GNUNET_ATS_address_add(GST_ats, 518 ar = GNUNET_ATS_address_add (GST_ats,
517 address, 519 address,
518 NULL, 520 NULL,
519 prop); 521 prop);
520 GNUNET_assert(NULL != ar); 522 GNUNET_assert (NULL != ar);
521 ai = GNUNET_new(struct AddressInfo); 523 ai = GNUNET_new (struct AddressInfo);
522 ai->address = GNUNET_HELLO_address_copy(address); 524 ai->address = GNUNET_HELLO_address_copy (address);
523 ai->ar = ar; 525 ai->ar = ar;
524 ai->properties = *prop; 526 ai->properties = *prop;
525 (void)GNUNET_CONTAINER_multipeermap_put(p2a, 527 (void) GNUNET_CONTAINER_multipeermap_put (p2a,
526 &ai->address->peer, 528 &ai->address->peer,
527 ai, 529 ai,
528 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); 530 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
529 publish_p2a_stat_update(); 531 publish_p2a_stat_update ();
530} 532}
531 533
532 534
@@ -540,41 +542,41 @@ GST_ats_add_address(const struct GNUNET_HELLO_Address *address,
540 * @param session the session 542 * @param session the session
541 */ 543 */
542void 544void
543GST_ats_new_session(const struct GNUNET_HELLO_Address *address, 545GST_ats_new_session (const struct GNUNET_HELLO_Address *address,
544 struct GNUNET_ATS_Session *session) 546 struct GNUNET_ATS_Session *session)
545{ 547{
546 struct AddressInfo *ai; 548 struct AddressInfo *ai;
547 549
548 if (0 == 550 if (0 ==
549 memcmp(&GST_my_identity, 551 memcmp (&GST_my_identity,
550 &address->peer, 552 &address->peer,
551 sizeof(struct GNUNET_PeerIdentity))) 553 sizeof(struct GNUNET_PeerIdentity)))
552 return; /* our own, ignore! */ 554 return; /* our own, ignore! */
553 ai = find_ai(address, NULL); 555 ai = find_ai (address, NULL);
554 if (NULL == ai) 556 if (NULL == ai)
555 { 557 {
556 /* We may simply already be aware of the session, even if some 558 /* We may simply already be aware of the session, even if some
557 other part of the code could not tell if it just created a new 559 other part of the code could not tell if it just created a new
558 session or just got one recycled from the plugin; hence, we may 560 session or just got one recycled from the plugin; hence, we may
559 be called with "new" session even for an "old" session; in that 561 be called with "new" session even for an "old" session; in that
560 case, check that this is the case, but just ignore it. */ 562 case, check that this is the case, but just ignore it. */
561 GNUNET_assert(NULL != (find_ai(address, session))); 563 GNUNET_assert (NULL != (find_ai (address, session)));
562 return; 564 return;
563 } 565 }
564 GNUNET_assert(NULL == ai->session); 566 GNUNET_assert (NULL == ai->session);
565 ai->session = session; 567 ai->session = session;
566 LOG(GNUNET_ERROR_TYPE_DEBUG, 568 LOG (GNUNET_ERROR_TYPE_DEBUG,
567 "Telling ATS about new session for peer %s\n", 569 "Telling ATS about new session for peer %s\n",
568 GNUNET_i2s(&address->peer)); 570 GNUNET_i2s (&address->peer));
569 /* Note that the address might currently be blocked; we only 571 /* Note that the address might currently be blocked; we only
570 tell ATS about the session if the address is currently not 572 tell ATS about the session if the address is currently not
571 blocked; otherwise, ATS will be told about the session on 573 blocked; otherwise, ATS will be told about the session on
572 unblock. */ 574 unblock. */
573 if (NULL != ai->ar) 575 if (NULL != ai->ar)
574 GNUNET_ATS_address_add_session(ai->ar, 576 GNUNET_ATS_address_add_session (ai->ar,
575 session); 577 session);
576 else 578 else
577 GNUNET_assert(NULL != ai->unblock_task); 579 GNUNET_assert (NULL != ai->unblock_task);
578} 580}
579 581
580 582
@@ -584,30 +586,30 @@ GST_ats_new_session(const struct GNUNET_HELLO_Address *address,
584 * @param ai the `struct AddressInfo` 586 * @param ai the `struct AddressInfo`
585 */ 587 */
586static void 588static void
587destroy_ai(struct AddressInfo *ai) 589destroy_ai (struct AddressInfo *ai)
588{ 590{
589 GNUNET_assert(NULL == ai->session); 591 GNUNET_assert (NULL == ai->session);
590 if (NULL != ai->unblock_task) 592 if (NULL != ai->unblock_task)
591 { 593 {
592 GNUNET_SCHEDULER_cancel(ai->unblock_task); 594 GNUNET_SCHEDULER_cancel (ai->unblock_task);
593 ai->unblock_task = NULL; 595 ai->unblock_task = NULL;
594 num_blocked--; 596 num_blocked--;
595 } 597 }
596 GNUNET_assert(GNUNET_YES == 598 GNUNET_assert (GNUNET_YES ==
597 GNUNET_CONTAINER_multipeermap_remove(p2a, 599 GNUNET_CONTAINER_multipeermap_remove (p2a,
598 &ai->address->peer, 600 &ai->address->peer,
599 ai)); 601 ai));
600 LOG(GNUNET_ERROR_TYPE_DEBUG, 602 LOG (GNUNET_ERROR_TYPE_DEBUG,
601 "Telling ATS to destroy address from peer %s\n", 603 "Telling ATS to destroy address from peer %s\n",
602 GNUNET_i2s(&ai->address->peer)); 604 GNUNET_i2s (&ai->address->peer));
603 if (NULL != ai->ar) 605 if (NULL != ai->ar)
604 { 606 {
605 GNUNET_ATS_address_destroy(ai->ar); 607 GNUNET_ATS_address_destroy (ai->ar);
606 ai->ar = NULL; 608 ai->ar = NULL;
607 } 609 }
608 publish_p2a_stat_update(); 610 publish_p2a_stat_update ();
609 GNUNET_HELLO_address_free(ai->address); 611 GNUNET_HELLO_address_free (ai->address);
610 GNUNET_free(ai); 612 GNUNET_free (ai);
611} 613}
612 614
613 615
@@ -623,100 +625,100 @@ destroy_ai(struct AddressInfo *ai)
623 * @param session the session 625 * @param session the session
624 */ 626 */
625void 627void
626GST_ats_del_session(const struct GNUNET_HELLO_Address *address, 628GST_ats_del_session (const struct GNUNET_HELLO_Address *address,
627 struct GNUNET_ATS_Session *session) 629 struct GNUNET_ATS_Session *session)
628{ 630{
629 struct AddressInfo *ai; 631 struct AddressInfo *ai;
630 632
631 if (0 == 633 if (0 ==
632 memcmp(&GST_my_identity, 634 memcmp (&GST_my_identity,
633 &address->peer, 635 &address->peer,
634 sizeof(struct GNUNET_PeerIdentity))) 636 sizeof(struct GNUNET_PeerIdentity)))
635 return; /* our own, ignore! */ 637 return; /* our own, ignore! */
636 if (NULL == session) 638 if (NULL == session)
637 { 639 {
638 GNUNET_break(0); 640 GNUNET_break (0);
639 return; 641 return;
640 } 642 }
641 ai = find_ai(address, 643 ai = find_ai (address,
642 session); 644 session);
643 if (NULL == ai) 645 if (NULL == ai)
644 { 646 {
645 /* We sometimes create sessions just for sending a PING, 647 /* We sometimes create sessions just for sending a PING,
646 and if those are destroyed they were never known to 648 and if those are destroyed they were never known to
647 ATS which means we end up here (however, in this 649 ATS which means we end up here (however, in this
648 case, the address must be an outbound address). */ 650 case, the address must be an outbound address). */
649 GNUNET_break(GNUNET_YES != 651 GNUNET_break (GNUNET_YES !=
650 GNUNET_HELLO_address_check_option(address, 652 GNUNET_HELLO_address_check_option (address,
651 GNUNET_HELLO_ADDRESS_INFO_INBOUND)); 653 GNUNET_HELLO_ADDRESS_INFO_INBOUND));
652 return; 654 return;
653 } 655 }
654 GNUNET_assert(session == ai->session); 656 GNUNET_assert (session == ai->session);
655 ai->session = NULL; 657 ai->session = NULL;
656 LOG(GNUNET_ERROR_TYPE_DEBUG, 658 LOG (GNUNET_ERROR_TYPE_DEBUG,
657 "Telling ATS to destroy session %p from peer %s\n", 659 "Telling ATS to destroy session %p from peer %s\n",
658 session, 660 session,
659 GNUNET_i2s(&address->peer)); 661 GNUNET_i2s (&address->peer));
660 if (GNUNET_YES == ai->expired) 662 if (GNUNET_YES == ai->expired)
663 {
664 /* last reason to keep this 'ai' around is now gone, the
665 session is dead as well, clean up */
666 if (NULL != ai->ar)
661 { 667 {
662 /* last reason to keep this 'ai' around is now gone, the 668 /* Address expired but not blocked, and thus 'ar' was still
663 session is dead as well, clean up */ 669 live because of the session; deleting just the session
664 if (NULL != ai->ar) 670 will do for an inbound session, but for an outbound we
665 { 671 then also need to destroy the address with ATS. */
666 /* Address expired but not blocked, and thus 'ar' was still 672 if (GNUNET_NO ==
667 live because of the session; deleting just the session 673 GNUNET_ATS_address_del_session (ai->ar,
668 will do for an inbound session, but for an outbound we 674 session))
669 then also need to destroy the address with ATS. */ 675 {
670 if (GNUNET_NO == 676 GNUNET_ATS_address_destroy (ai->ar);
671 GNUNET_ATS_address_del_session(ai->ar, 677 }
672 session)) 678 /* "ar" has been freed, regardless how the branch
673 { 679 above played out: it was either freed in
674 GNUNET_ATS_address_destroy(ai->ar); 680 #GNUNET_ATS_address_del_session() because it was
675 } 681 incoming, or explicitly in
676 /* "ar" has been freed, regardless how the branch 682 #GNUNET_ATS_address_del_session(). */
677 above played out: it was either freed in 683 ai->ar = NULL;
678 #GNUNET_ATS_address_del_session() because it was
679 incoming, or explicitly in
680 #GNUNET_ATS_address_del_session(). */
681 ai->ar = NULL;
682 }
683 destroy_ai(ai);
684 return;
685 } 684 }
685 destroy_ai (ai);
686 return;
687 }
686 688
687 if (NULL == ai->ar) 689 if (NULL == ai->ar)
690 {
691 /* If ATS doesn't know about the address/session, this means
692 this address was blocked. */
693 if (GNUNET_YES ==
694 GNUNET_HELLO_address_check_option (address,
695 GNUNET_HELLO_ADDRESS_INFO_INBOUND))
688 { 696 {
689 /* If ATS doesn't know about the address/session, this means 697 /* This was a blocked inbound session, which now lost the
690 this address was blocked. */ 698 session. But inbound addresses are by themselves useless,
691 if (GNUNET_YES == 699 so we must forget about the address as well. */
692 GNUNET_HELLO_address_check_option(address, 700 destroy_ai (ai);
693 GNUNET_HELLO_ADDRESS_INFO_INBOUND))
694 {
695 /* This was a blocked inbound session, which now lost the
696 session. But inbound addresses are by themselves useless,
697 so we must forget about the address as well. */
698 destroy_ai(ai);
699 return;
700 }
701 /* Otherwise, we are done as we have set `ai->session` to NULL
702 already and ATS will simply not be told about the session when
703 the connection is unblocked and the outbound address becomes
704 available again. . */
705 return; 701 return;
706 } 702 }
703 /* Otherwise, we are done as we have set `ai->session` to NULL
704 already and ATS will simply not be told about the session when
705 the connection is unblocked and the outbound address becomes
706 available again. . */
707 return;
708 }
707 709
708 /* This is the "simple" case where ATS knows about the session and 710 /* This is the "simple" case where ATS knows about the session and
709 the address is neither blocked nor expired. Delete the session, 711 the address is neither blocked nor expired. Delete the session,
710 and if it was inbound, free the address as well. */ 712 and if it was inbound, free the address as well. */
711 if (GNUNET_YES == 713 if (GNUNET_YES ==
712 GNUNET_ATS_address_del_session(ai->ar, 714 GNUNET_ATS_address_del_session (ai->ar,
713 session)) 715 session))
714 { 716 {
715 /* This was an inbound address, the session is now gone, so we 717 /* This was an inbound address, the session is now gone, so we
716 need to also forget about the address itself. */ 718 need to also forget about the address itself. */
717 ai->ar = NULL; 719 ai->ar = NULL;
718 destroy_ai(ai); 720 destroy_ai (ai);
719 } 721 }
720} 722}
721 723
722 724
@@ -728,31 +730,31 @@ GST_ats_del_session(const struct GNUNET_HELLO_Address *address,
728 * @param distance new distance value 730 * @param distance new distance value
729 */ 731 */
730void 732void
731GST_ats_update_distance(const struct GNUNET_HELLO_Address *address, 733GST_ats_update_distance (const struct GNUNET_HELLO_Address *address,
732 uint32_t distance) 734 uint32_t distance)
733{ 735{
734 struct AddressInfo *ai; 736 struct AddressInfo *ai;
735 737
736 ai = find_ai_no_session(address); 738 ai = find_ai_no_session (address);
737 if (NULL == ai) 739 if (NULL == ai)
738 { 740 {
739 /* We do not know about this address, do nothing. */ 741 /* We do not know about this address, do nothing. */
740 return; 742 return;
741 } 743 }
742 LOG(GNUNET_ERROR_TYPE_DEBUG, 744 LOG (GNUNET_ERROR_TYPE_DEBUG,
743 "Updated distance for peer `%s' to %u\n", 745 "Updated distance for peer `%s' to %u\n",
744 GNUNET_i2s(&address->peer), 746 GNUNET_i2s (&address->peer),
745 distance); 747 distance);
746 ai->properties.distance = distance; 748 ai->properties.distance = distance;
747 /* Give manipulation its chance to change metrics */ 749 /* Give manipulation its chance to change metrics */
748 GST_manipulation_manipulate_metrics(address, 750 GST_manipulation_manipulate_metrics (address,
749 ai->session, 751 ai->session,
750 &ai->properties); 752 &ai->properties);
751 /* Address may be blocked, only give ATS if address is 753 /* Address may be blocked, only give ATS if address is
752 currently active. */ 754 currently active. */
753 if (NULL != ai->ar) 755 if (NULL != ai->ar)
754 GNUNET_ATS_address_update(ai->ar, 756 GNUNET_ATS_address_update (ai->ar,
755 &ai->properties); 757 &ai->properties);
756} 758}
757 759
758 760
@@ -764,32 +766,32 @@ GST_ats_update_distance(const struct GNUNET_HELLO_Address *address,
764 * @param delay new delay value 766 * @param delay new delay value
765 */ 767 */
766void 768void
767GST_ats_update_delay(const struct GNUNET_HELLO_Address *address, 769GST_ats_update_delay (const struct GNUNET_HELLO_Address *address,
768 struct GNUNET_TIME_Relative delay) 770 struct GNUNET_TIME_Relative delay)
769{ 771{
770 struct AddressInfo *ai; 772 struct AddressInfo *ai;
771 773
772 ai = find_ai_no_session(address); 774 ai = find_ai_no_session (address);
773 if (NULL == ai) 775 if (NULL == ai)
774 { 776 {
775 /* We do not know about this address, do nothing. */ 777 /* We do not know about this address, do nothing. */
776 return; 778 return;
777 } 779 }
778 LOG(GNUNET_ERROR_TYPE_DEBUG, 780 LOG (GNUNET_ERROR_TYPE_DEBUG,
779 "Updated latency for peer `%s' to %s\n", 781 "Updated latency for peer `%s' to %s\n",
780 GNUNET_i2s(&address->peer), 782 GNUNET_i2s (&address->peer),
781 GNUNET_STRINGS_relative_time_to_string(delay, 783 GNUNET_STRINGS_relative_time_to_string (delay,
782 GNUNET_YES)); 784 GNUNET_YES));
783 ai->properties.delay = delay; 785 ai->properties.delay = delay;
784 /* Give manipulation its chance to change metrics */ 786 /* Give manipulation its chance to change metrics */
785 GST_manipulation_manipulate_metrics(address, 787 GST_manipulation_manipulate_metrics (address,
786 ai->session, 788 ai->session,
787 &ai->properties); 789 &ai->properties);
788 /* Address may be blocked, only give ATS if address is 790 /* Address may be blocked, only give ATS if address is
789 currently active. */ 791 currently active. */
790 if (NULL != ai->ar) 792 if (NULL != ai->ar)
791 GNUNET_ATS_address_update(ai->ar, 793 GNUNET_ATS_address_update (ai->ar,
792 &ai->properties); 794 &ai->properties);
793} 795}
794 796
795 797
@@ -802,35 +804,35 @@ GST_ats_update_delay(const struct GNUNET_HELLO_Address *address,
802 * @param bps_out new utilization outbound 804 * @param bps_out new utilization outbound
803 */ 805 */
804void 806void
805GST_ats_update_utilization(const struct GNUNET_HELLO_Address *address, 807GST_ats_update_utilization (const struct GNUNET_HELLO_Address *address,
806 uint32_t bps_in, 808 uint32_t bps_in,
807 uint32_t bps_out) 809 uint32_t bps_out)
808{ 810{
809 struct AddressInfo *ai; 811 struct AddressInfo *ai;
810 812
811 ai = find_ai_no_session(address); 813 ai = find_ai_no_session (address);
812 if (NULL == ai) 814 if (NULL == ai)
813 { 815 {
814 /* We do not know about this address, do nothing. */ 816 /* We do not know about this address, do nothing. */
815 return; 817 return;
816 } 818 }
817 LOG(GNUNET_ERROR_TYPE_DEBUG, 819 LOG (GNUNET_ERROR_TYPE_DEBUG,
818 "Updating utilization for peer `%s' address %s: %u/%u\n", 820 "Updating utilization for peer `%s' address %s: %u/%u\n",
819 GNUNET_i2s(&address->peer), 821 GNUNET_i2s (&address->peer),
820 GST_plugins_a2s(address), 822 GST_plugins_a2s (address),
821 (unsigned int)bps_in, 823 (unsigned int) bps_in,
822 (unsigned int)bps_out); 824 (unsigned int) bps_out);
823 ai->properties.utilization_in = bps_in; 825 ai->properties.utilization_in = bps_in;
824 ai->properties.utilization_out = bps_out; 826 ai->properties.utilization_out = bps_out;
825 /* Give manipulation its chance to change metrics */ 827 /* Give manipulation its chance to change metrics */
826 GST_manipulation_manipulate_metrics(address, 828 GST_manipulation_manipulate_metrics (address,
827 ai->session, 829 ai->session,
828 &ai->properties); 830 &ai->properties);
829 /* Address may be blocked, only give ATS if address is 831 /* Address may be blocked, only give ATS if address is
830 currently active. */ 832 currently active. */
831 if (NULL != ai->ar) 833 if (NULL != ai->ar)
832 GNUNET_ATS_address_update(ai->ar, 834 GNUNET_ATS_address_update (ai->ar,
833 &ai->properties); 835 &ai->properties);
834} 836}
835 837
836 838
@@ -842,34 +844,34 @@ GST_ats_update_utilization(const struct GNUNET_HELLO_Address *address,
842 * @param address the address 844 * @param address the address
843 */ 845 */
844void 846void
845GST_ats_expire_address(const struct GNUNET_HELLO_Address *address) 847GST_ats_expire_address (const struct GNUNET_HELLO_Address *address)
846{ 848{
847 struct AddressInfo *ai; 849 struct AddressInfo *ai;
848 850
849 if (0 == 851 if (0 ==
850 memcmp(&GST_my_identity, 852 memcmp (&GST_my_identity,
851 &address->peer, 853 &address->peer,
852 sizeof(struct GNUNET_PeerIdentity))) 854 sizeof(struct GNUNET_PeerIdentity)))
853 return; /* our own, ignore! */ 855 return; /* our own, ignore! */
854 LOG(GNUNET_ERROR_TYPE_DEBUG, 856 LOG (GNUNET_ERROR_TYPE_DEBUG,
855 "Address %s of peer %s expired\n", 857 "Address %s of peer %s expired\n",
856 GST_plugins_a2s(address), 858 GST_plugins_a2s (address),
857 GNUNET_i2s(&address->peer)); 859 GNUNET_i2s (&address->peer));
858 ai = find_ai_no_session(address); 860 ai = find_ai_no_session (address);
859 if (NULL == ai) 861 if (NULL == ai)
860 { 862 {
861 GNUNET_assert(0); 863 GNUNET_assert (0);
862 return; 864 return;
863 } 865 }
864 if (NULL != ai->session) 866 if (NULL != ai->session)
865 { 867 {
866 /* Got an active session, just remember the expiration 868 /* Got an active session, just remember the expiration
867 and act upon it when the session goes down. */ 869 and act upon it when the session goes down. */
868 ai->expired = GNUNET_YES; 870 ai->expired = GNUNET_YES;
869 return; 871 return;
870 } 872 }
871 /* Address expired, no session, free resources */ 873 /* Address expired, no session, free resources */
872 destroy_ai(ai); 874 destroy_ai (ai);
873} 875}
874 876
875 877
@@ -877,9 +879,9 @@ GST_ats_expire_address(const struct GNUNET_HELLO_Address *address)
877 * Initialize ATS subsystem. 879 * Initialize ATS subsystem.
878 */ 880 */
879void 881void
880GST_ats_init() 882GST_ats_init ()
881{ 883{
882 p2a = GNUNET_CONTAINER_multipeermap_create(4, GNUNET_YES); 884 p2a = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_YES);
883} 885}
884 886
885 887
@@ -892,13 +894,13 @@ GST_ats_init()
892 * @return #GNUNET_OK (continue to iterate) 894 * @return #GNUNET_OK (continue to iterate)
893 */ 895 */
894static int 896static int
895destroy_ai_cb(void *cls, 897destroy_ai_cb (void *cls,
896 const struct GNUNET_PeerIdentity *key, 898 const struct GNUNET_PeerIdentity *key,
897 void *value) 899 void *value)
898{ 900{
899 struct AddressInfo *ai = value; 901 struct AddressInfo *ai = value;
900 902
901 destroy_ai(ai); 903 destroy_ai (ai);
902 return GNUNET_OK; 904 return GNUNET_OK;
903} 905}
904 906
@@ -907,13 +909,13 @@ destroy_ai_cb(void *cls,
907 * Shutdown ATS subsystem. 909 * Shutdown ATS subsystem.
908 */ 910 */
909void 911void
910GST_ats_done() 912GST_ats_done ()
911{ 913{
912 GNUNET_CONTAINER_multipeermap_iterate(p2a, 914 GNUNET_CONTAINER_multipeermap_iterate (p2a,
913 &destroy_ai_cb, 915 &destroy_ai_cb,
914 NULL); 916 NULL);
915 publish_p2a_stat_update(); 917 publish_p2a_stat_update ();
916 GNUNET_CONTAINER_multipeermap_destroy(p2a); 918 GNUNET_CONTAINER_multipeermap_destroy (p2a);
917 p2a = NULL; 919 p2a = NULL;
918} 920}
919 921