indentation
[oweals/gnunet.git] / src / ats / ats_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010,2011 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.c
22  * @brief automatic transport selection API
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25  *
26  * TODO:
27  * - write test case
28  * - extend API to get performance data
29  * - implement simplistic strategy based on say 'lowest latency' or strict ordering
30  * - extend API to get peer preferences, implement proportional bandwidth assignment
31  * - re-implement API against a real ATS service (!)
32  */
33 #include "platform.h"
34 #include "gnunet_ats_service.h"
35
36 #define DEBUG_ATS GNUNET_NO
37
38 // NOTE: this implementation is simply supposed
39 // to implement a simplistic strategy in-process;
40 // in the future, we plan to replace it with a real
41 // service implementation
42
43 /**
44  * Allocation record for a peer's address.
45  */
46 struct AllocationRecord
47 {
48
49   /**
50    * Performance information associated with this address (array).
51    */
52   struct GNUNET_TRANSPORT_ATS_Information *ats;
53
54   /**
55    * Name of the plugin
56    */
57   char *plugin_name;
58
59   /**
60    * Address this record represents, allocated at the end of this struct.
61    */
62   const void *plugin_addr;
63
64   /**
65    * Session associated with this record.
66    */
67   struct Session *session;
68
69   /**
70    * Number of bytes in plugin_addr.
71    */
72   size_t plugin_addr_len;
73
74   /**
75    * Number of entries in 'ats'.
76    */
77   uint32_t ats_count;
78
79   /**
80    * Bandwidth assigned to this address right now, 0 for none.
81    */
82   struct GNUNET_BANDWIDTH_Value32NBO bandwidth;
83
84   /**
85    * Set to GNUNET_YES if this is the connected address of a connected peer.
86    */
87   int connected;
88
89 };
90
91
92 /**
93  * Opaque handle to obtain address suggestions.
94  */
95 struct GNUNET_ATS_SuggestionContext
96 {
97
98   /**
99    * Function to call with our final suggestion.
100    */
101   GNUNET_ATS_AddressSuggestionCallback cb;
102
103   /**
104    * Closure for 'cb'.
105    */
106   void *cb_cls;
107
108   /**
109    * Global ATS handle.
110    */
111   struct GNUNET_ATS_Handle *atc;
112
113   /**
114    * Which peer are we monitoring?
115    */
116   struct GNUNET_PeerIdentity target;
117
118 };
119
120
121 /**
122  * Handle to the ATS subsystem.
123  */
124 struct GNUNET_ATS_Handle
125 {
126   /**
127    * Configuration.
128    */
129   const struct GNUNET_CONFIGURATION_Handle *cfg;
130
131   /**
132    * Function to call when the allocation changes.
133    */
134   GNUNET_TRANSPORT_ATS_AllocationNotification alloc_cb;
135
136   /**
137    * Closure for 'alloc_cb'.
138    */
139   void *alloc_cb_cls;
140
141   /**
142    * Information about all connected peers.  Maps peer identities
143    * to one or more 'struct AllocationRecord' values.
144    */
145   struct GNUNET_CONTAINER_MultiHashMap *peers;
146
147   /**
148    * Map of PeerIdentities to 'struct GNUNET_ATS_SuggestionContext's.
149    */
150   struct GNUNET_CONTAINER_MultiHashMap *notify_map;
151
152
153   /**
154    * Task scheduled to update our bandwidth assignment.
155    */
156   GNUNET_SCHEDULER_TaskIdentifier ba_task;
157
158   /**
159    * Total bandwidth per configuration.
160    */
161   unsigned long long total_bps;
162 };
163
164
165 /**
166  * Count number of connected records.
167  *
168  * @param cls pointer to counter
169  * @param key identity of the peer associated with the records
170  * @param value a 'struct AllocationRecord'
171  * @return GNUNET_YES (continue iteration)
172  */
173 static int
174 count_connections (void *cls, const GNUNET_HashCode * key, void *value)
175 {
176   unsigned int *ac = cls;
177   struct AllocationRecord *ar = value;
178
179   if (GNUNET_YES == ar->connected)
180     (*ac)++;
181   return GNUNET_YES;
182 }
183
184
185 /**
186  * Closure for 'set_bw_connections'.
187  */
188 struct SetBandwidthContext
189 {
190   /**
191    * ATS handle.
192    */
193   struct GNUNET_ATS_Handle *atc;
194
195   /**
196    * Bandwidth to assign.
197    */
198   struct GNUNET_BANDWIDTH_Value32NBO bw;
199 };
200
201
202 /**
203  * Set bandwidth based on record.
204  *
205  * @param cls 'struct SetBandwidthContext'
206  * @param key identity of the peer associated with the records
207  * @param value a 'struct AllocationRecord'
208  * @return GNUNET_YES (continue iteration)
209  */
210 static int
211 set_bw_connections (void *cls, const GNUNET_HashCode * key, void *value)
212 {
213   struct SetBandwidthContext *sbc = cls;
214   struct AllocationRecord *ar = value;
215
216   if (GNUNET_YES == ar->connected)
217   {
218     ar->bandwidth = sbc->bw;
219     sbc->atc->alloc_cb (sbc->atc->alloc_cb_cls,
220                         (const struct GNUNET_PeerIdentity *) key,
221                         ar->plugin_name, ar->session, ar->plugin_addr,
222                         ar->plugin_addr_len, ar->bandwidth);
223   }
224   else if (ntohl (ar->bandwidth.value__) > 0)
225   {
226     ar->bandwidth = GNUNET_BANDWIDTH_value_init (0);
227     sbc->atc->alloc_cb (sbc->atc->alloc_cb_cls,
228                         (const struct GNUNET_PeerIdentity *) key,
229                         ar->plugin_name, ar->session, ar->plugin_addr,
230                         ar->plugin_addr_len, ar->bandwidth);
231   }
232   return GNUNET_YES;
233 }
234
235
236 /**
237  * Task run to update bandwidth assignments.
238  *
239  * @param cls the 'struct GNUNET_ATS_Handle'
240  * @param tc scheduler context
241  */
242 static void
243 update_bandwidth_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
244 {
245   struct GNUNET_ATS_Handle *atc = cls;
246   unsigned int ac = 0;
247   struct SetBandwidthContext bwc;
248
249   atc->ba_task = GNUNET_SCHEDULER_NO_TASK;
250   /* FIXME: update calculations NICELY; what follows is a naive version */
251   GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &count_connections, &ac);
252   bwc.atc = atc;
253   if (ac == 0)
254     ac++;
255   GNUNET_assert (ac > 0);
256   bwc.bw = GNUNET_BANDWIDTH_value_init (atc->total_bps / ac);
257   GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &set_bw_connections, &bwc);
258 }
259
260
261 /**
262  * Calculate an updated bandwidth assignment and notify.
263  *
264  * @param atc handle
265  * @param change which allocation record changed?
266  */
267 static void
268 update_bandwidth_assignment (struct GNUNET_ATS_Handle *atc,
269                              struct AllocationRecord *change)
270 {
271   /* FIXME: based on the 'change', update the LP-problem... */
272   if (atc->ba_task == GNUNET_SCHEDULER_NO_TASK)
273     atc->ba_task = GNUNET_SCHEDULER_add_now (&update_bandwidth_task, atc);
274 }
275
276
277 /**
278  * Function called with feasbile addresses we might want to suggest.
279  *
280  * @param cls the 'struct GNUNET_ATS_SuggestionContext'
281  * @param key identity of the peer
282  * @param value a 'struct AllocationRecord' for the peer
283  * @return GNUNET_NO if we're done, GNUNET_YES if we did not suggest an address yet
284  */
285 static int
286 suggest_address (void *cls, const GNUNET_HashCode * key, void *value)
287 {
288   struct GNUNET_ATS_SuggestionContext *asc = cls;
289   struct AllocationRecord *ar = value;
290
291 #if DEBUG_ATS
292   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-api",
293                    "Suggesting address for peer `%s'\n", GNUNET_h2s (key));
294 #endif
295
296   /* trivial strategy: pick first available address... */
297   asc->cb (asc->cb_cls, &asc->target, ar->plugin_name, ar->plugin_addr,
298            ar->plugin_addr_len, ar->session,
299            GNUNET_BANDWIDTH_value_init (asc->atc->total_bps / 32), ar->ats,
300            ar->ats_count);
301   asc->cb = NULL;
302   return GNUNET_NO;
303 }
304
305 int
306 map_it (void *cls, const GNUNET_HashCode * key, void *value)
307 {
308   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-api", "Found entry for %s\n",
309                    GNUNET_h2s (key));
310   return GNUNET_YES;
311 }
312
313 /**
314  * We would like to establish a new connection with a peer.
315  * ATS should suggest a good address to begin with.
316  *
317  * @param atc handle
318  * @param peer identity of the new peer
319  * @param cb function to call with the address
320  * @param cb_cls closure for cb
321  */
322 struct GNUNET_ATS_SuggestionContext *
323 GNUNET_ATS_suggest_address (struct GNUNET_ATS_Handle *atc,
324                             const struct GNUNET_PeerIdentity *peer,
325                             GNUNET_ATS_AddressSuggestionCallback cb,
326                             void *cb_cls)
327 {
328   struct GNUNET_ATS_SuggestionContext *asc;
329
330 #if DEBUG_ATS
331   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-api",
332                    "Looking up suggested address for peer `%s'\n",
333                    GNUNET_i2s (peer));
334 #endif
335   asc = GNUNET_malloc (sizeof (struct GNUNET_ATS_SuggestionContext));
336   asc->cb = cb;
337   asc->cb_cls = cb_cls;
338   asc->atc = atc;
339   asc->target = *peer;
340   (void) GNUNET_CONTAINER_multihashmap_get_multiple (atc->peers, &peer->hashPubKey,
341                                                      &suggest_address, asc);
342
343   if (NULL == asc->cb)
344   {
345     GNUNET_free (asc);
346     return NULL;
347   }
348   GNUNET_CONTAINER_multihashmap_put (atc->notify_map, &peer->hashPubKey, asc,
349                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
350   return asc;
351 }
352
353
354 /**
355  * Cancel suggestion request.
356  *
357  * @param asc handle of the request to cancel
358  */
359 void
360 GNUNET_ATS_suggest_address_cancel (struct GNUNET_ATS_SuggestionContext *asc)
361 {
362   GNUNET_assert (GNUNET_OK ==
363                  GNUNET_CONTAINER_multihashmap_remove (asc->atc->notify_map,
364                                                        &asc->target.hashPubKey,
365                                                        asc));
366   GNUNET_free (asc);
367 }
368
369
370 /**
371  * Initialize the ATS subsystem.
372  *
373  * @param cfg configuration to use
374  * @param alloc_cb notification to call whenever the allocation changed
375  * @param alloc_cb_cls closure for 'alloc_cb'
376  * @return ats context
377  */
378 struct GNUNET_ATS_Handle *
379 GNUNET_ATS_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
380                  GNUNET_TRANSPORT_ATS_AllocationNotification alloc_cb,
381                  void *alloc_cb_cls)
382 {
383   struct GNUNET_ATS_Handle *atc;
384
385 #if DEBUG_ATS
386   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-api", "ATS init\n");
387 #endif
388   atc = GNUNET_malloc (sizeof (struct GNUNET_ATS_Handle));
389   atc->cfg = cfg;
390   atc->alloc_cb = alloc_cb;
391   atc->alloc_cb_cls = alloc_cb_cls;
392   atc->peers = GNUNET_CONTAINER_multihashmap_create (256);
393   atc->notify_map = GNUNET_CONTAINER_multihashmap_create (256);
394   GNUNET_CONFIGURATION_get_value_number (cfg, "core", "TOTAL_QUOTA_OUT",
395                                          &atc->total_bps);
396   return atc;
397 }
398
399
400 /**
401  * Free an allocation record.
402  *
403  * @param cls unused
404  * @param key identity of the peer associated with the record
405  * @param value the 'struct AllocationRecord' to free
406  * @return GNUNET_OK (continue to iterate)
407  */
408 static int
409 destroy_allocation_record (void *cls, const GNUNET_HashCode * key, void *value)
410 {
411   struct AllocationRecord *ar = value;
412
413   GNUNET_array_grow (ar->ats, ar->ats_count, 0);
414   GNUNET_free (ar->plugin_name);
415   GNUNET_free (ar);
416   return GNUNET_OK;
417 }
418
419
420 /**
421  * Shutdown the ATS subsystem.
422  *
423  * @param atc handle
424  */
425 void
426 GNUNET_ATS_shutdown (struct GNUNET_ATS_Handle *atc)
427 {
428 #if DEBUG_ATS
429   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-api", "ATS shutdown\n");
430 #endif
431   if (GNUNET_SCHEDULER_NO_TASK != atc->ba_task)
432   {
433     GNUNET_SCHEDULER_cancel (atc->ba_task);
434     atc->ba_task = GNUNET_SCHEDULER_NO_TASK;
435   }
436   GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &destroy_allocation_record,
437                                          NULL);
438   GNUNET_CONTAINER_multihashmap_destroy (atc->peers);
439   GNUNET_assert (GNUNET_CONTAINER_multihashmap_size (atc->notify_map) == 0);
440   GNUNET_CONTAINER_multihashmap_destroy (atc->notify_map);
441   atc->notify_map = NULL;
442   GNUNET_free (atc);
443 }
444
445
446 /**
447  * Closure for 'update_session'
448  */
449 struct UpdateSessionContext
450 {
451   /**
452    * Ats handle.
453    */
454   struct GNUNET_ATS_Handle *atc;
455
456   /**
457    * Allocation record with new information.
458    */
459   struct AllocationRecord *arnew;
460 };
461
462
463 /**
464  * Update an allocation record, merging with the new information
465  *
466  * @param cls a new 'struct AllocationRecord'
467  * @param key identity of the peer associated with the records
468  * @param value the old 'struct AllocationRecord'
469  * @return GNUNET_YES if the records do not match,
470  *         GNUNET_NO if the record do match and 'old' was updated
471  */
472 static int
473 update_session (void *cls, const GNUNET_HashCode * key, void *value)
474 {
475   struct UpdateSessionContext *usc = cls;
476   struct AllocationRecord *arnew = usc->arnew;
477   struct AllocationRecord *arold = value;
478   int change;
479   int c_old;
480   int c_new;
481   int found;
482
483
484 #if DEBUG_ATS
485   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-api",
486                    "Updating session for peer `%s' plugin `%s'\n",
487                    GNUNET_h2s (key), arold->plugin_name);
488 #endif
489
490   if (0 != strcmp (arnew->plugin_name, arold->plugin_name))
491     return GNUNET_YES;
492   if (((arnew->session == arold->session) && (arnew->session != NULL)) ||
493       ((arold->session == NULL) &&
494        (arold->plugin_addr_len == arnew->plugin_addr_len) &&
495        (0 ==
496         memcmp (arold->plugin_addr, arnew->plugin_addr,
497                 arnew->plugin_addr_len))))
498   {
499     change = GNUNET_NO;
500     /* records match */
501     if (arnew->session != arold->session)
502     {
503       arold->session = arnew->session;
504       change = GNUNET_YES;
505     }
506     if ((arnew->connected == GNUNET_YES) && (arold->connected == GNUNET_NO))
507     {
508       arold->connected = GNUNET_YES;
509       change = GNUNET_YES;
510     }
511
512     /* Update existing value */
513     c_new = 0;
514     while (c_new < arnew->ats_count)
515     {
516       c_old = 0;
517       found = GNUNET_NO;
518       while (c_old < arold->ats_count)
519       {
520         if (arold->ats[c_old].type == arnew->ats[c_new].type)
521         {
522 #if DEBUG_ATS
523           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
524                       "Found type %i, old value=%i new value=%i\n",
525                       ntohl (arold->ats[c_old].type),
526                       ntohl (arold->ats[c_old].value),
527                       ntohl (arnew->ats[c_new].value));
528 #endif
529           arold->ats[c_old].value = arnew->ats[c_new].value;
530           found = GNUNET_YES;
531         }
532         c_old++;
533       }
534       /* Add new value */
535       if (found == GNUNET_NO)
536       {
537 #if DEBUG_ATS
538         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new type %i new value=%i\n",
539                     ntohl (arnew->ats[c_new].type),
540                     ntohl (arnew->ats[c_new].value));
541         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Old array size: %u\n",
542                     arold->ats_count);
543 #endif
544         GNUNET_array_grow (arold->ats, arold->ats_count, arold->ats_count + 1);
545         GNUNET_assert (arold->ats_count >= 2);
546         arold->ats[arold->ats_count - 2].type = arnew->ats[c_new].type;
547         arold->ats[arold->ats_count - 2].value = arnew->ats[c_new].value;
548         arold->ats[arold->ats_count - 1].type = htonl (0);
549         arold->ats[arold->ats_count - 1].value = htonl (0);
550 #if DEBUG_ATS
551         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New array size: %i\n",
552                     arold->ats_count);
553 #endif
554       }
555       c_new++;
556     }
557
558     if (GNUNET_YES == change)
559       update_bandwidth_assignment (usc->atc, arold);
560     return GNUNET_NO;
561   }
562   return GNUNET_YES;
563 }
564
565
566 /**
567  * Create an allocation record with the given properties.
568  *
569  * @param plugin_name name of the currently used transport plugin
570  * @param session session in use (if available)
571  * @param plugin_addr address in use (if available)
572  * @param plugin_addr_len number of bytes in plugin_addr
573  * @param ats performance data for the connection
574  * @param ats_count number of performance records in 'ats'
575  */
576 static struct AllocationRecord *
577 create_allocation_record (const char *plugin_name, struct Session *session,
578                           const void *plugin_addr, size_t plugin_addr_len,
579                           const struct GNUNET_TRANSPORT_ATS_Information *ats,
580                           uint32_t ats_count)
581 {
582   struct AllocationRecord *ar;
583
584   ar = GNUNET_malloc (sizeof (struct AllocationRecord) + plugin_addr_len);
585   ar->plugin_name = GNUNET_strdup (plugin_name);
586   ar->plugin_addr = &ar[1];
587   memcpy (&ar[1], plugin_addr, plugin_addr_len);
588   ar->session = session;
589   ar->plugin_addr_len = plugin_addr_len;  
590   GNUNET_assert (ats_count > 0);
591   GNUNET_array_grow (ar->ats, ar->ats_count, ats_count);
592   memcpy (ar->ats, ats,
593           ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
594   return ar;
595 }
596
597
598 /**
599  * Mark all matching allocation records as not connected.
600  *
601  * @param cls 'struct GTS_AtsHandle'
602  * @param key identity of the peer associated with the record
603  * @param value the 'struct AllocationRecord' to clear the 'connected' flag
604  * @return GNUNET_OK (continue to iterate)
605  */
606 static int
607 disconnect_peer (void *cls, const GNUNET_HashCode * key, void *value)
608 {
609   struct GNUNET_ATS_Handle *atc = cls;
610   struct AllocationRecord *ar = value;
611
612   if (GNUNET_YES == ar->connected)
613   {
614     ar->connected = GNUNET_NO;
615     update_bandwidth_assignment (atc, ar);
616   }
617   return GNUNET_OK;
618 }
619
620
621 /**
622  * We established a new connection with a peer (for example, because
623  * core asked for it or because the other peer connected to us).
624  * Calculate bandwidth assignments including the new peer.
625  *
626  * @param atc handle
627  * @param peer identity of the new peer
628  * @param plugin_name name of the currently used transport plugin
629  * @param session session in use (if available)
630  * @param plugin_addr address in use (if available)
631  * @param plugin_addr_len number of bytes in plugin_addr
632  * @param ats performance data for the connection
633  * @param ats_count number of performance records in 'ats'
634  */
635 void
636 GNUNET_ATS_peer_connect (struct GNUNET_ATS_Handle *atc,
637                          const struct GNUNET_PeerIdentity *peer,
638                          const char *plugin_name, struct Session *session,
639                          const void *plugin_addr, size_t plugin_addr_len,
640                          const struct GNUNET_TRANSPORT_ATS_Information *ats,
641                          uint32_t ats_count)
642 {
643   struct AllocationRecord *ar;
644   struct UpdateSessionContext usc;
645
646   (void) GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &disconnect_peer,
647                                                 atc);
648   ar = create_allocation_record (plugin_name, session, plugin_addr,
649                                  plugin_addr_len, ats, ats_count);
650   ar->connected = GNUNET_YES;
651   usc.atc = atc;
652   usc.arnew = ar;
653   if (GNUNET_SYSERR ==
654       GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &update_session, &usc))
655   {
656     destroy_allocation_record (NULL, &peer->hashPubKey, ar);
657     return;
658   }
659   GNUNET_assert (GNUNET_OK ==
660                  GNUNET_CONTAINER_multihashmap_put (atc->peers,
661                                                     &peer->hashPubKey, ar,
662                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
663 }
664
665
666 /**
667  * We disconnected from the given peer (for example, because ats, core
668  * or blacklist asked for it or because the other peer disconnected).
669  * Calculate bandwidth assignments without the peer.
670  *
671  * @param atc handle
672  * @param peer identity of the new peer
673  */
674 void
675 GNUNET_ATS_peer_disconnect (struct GNUNET_ATS_Handle *atc,
676                             const struct GNUNET_PeerIdentity *peer)
677 {
678   (void) GNUNET_CONTAINER_multihashmap_get_multiple (atc->peers,
679                                                      &peer->hashPubKey,
680                                                      &disconnect_peer, atc);
681 }
682
683
684 /**
685  * Closure for 'destroy_allocation_record'
686  */
687 struct SessionDestroyContext
688 {
689   /**
690    * Ats handle.
691    */
692   struct GNUNET_ATS_Handle *atc;
693
694   /**
695    * Session being destroyed.
696    */
697   const struct Session *session;
698 };
699
700
701 /**
702  * Free an allocation record matching the given session.
703  *
704  * @param cls the 'struct SessionDestroyContext'
705  * @param key identity of the peer associated with the record
706  * @param value the 'struct AllocationRecord' to free
707  * @return GNUNET_OK (continue to iterate)
708  */
709 static int
710 destroy_session (void *cls, const GNUNET_HashCode * key, void *value)
711 {
712   struct SessionDestroyContext *sdc = cls;
713   struct AllocationRecord *ar = value;
714
715   if (ar->session != sdc->session)
716     return GNUNET_OK;
717   ar->session = NULL;
718   if (ar->plugin_addr != NULL)
719     return GNUNET_OK;
720   GNUNET_assert (GNUNET_OK ==
721                  GNUNET_CONTAINER_multihashmap_remove (sdc->atc->peers, key,
722                                                        ar));
723   if (GNUNET_YES == ar->connected) ;
724   {
725     /* FIXME: is this supposed to be allowed? What to do then? */
726     GNUNET_break (0);
727   }
728   destroy_allocation_record (NULL, key, ar);
729   return GNUNET_OK;
730 }
731
732
733 /**
734  * A session got destroyed, stop including it as a valid address.
735  *
736  * @param atc handle
737  * @param peer identity of the peer
738  * @param session session handle that is no longer valid
739  */
740 void
741 GNUNET_ATS_session_destroyed (struct GNUNET_ATS_Handle *atc,
742                               const struct GNUNET_PeerIdentity *peer,
743                               const struct Session *session)
744 {
745   struct SessionDestroyContext sdc;
746
747   sdc.atc = atc;
748   sdc.session = session;
749   (void) GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &destroy_session,
750                                                 &sdc);
751 }
752
753
754 /**
755  * Notify validation watcher that an entry is now valid
756  *
757  * @param cls 'struct ValidationEntry' that is now valid
758  * @param key peer identity (unused)
759  * @param value a 'GST_ValidationIteratorContext' to notify
760  * @return GNUNET_YES (continue to iterate)
761  */
762 static int
763 notify_valid (void *cls, const GNUNET_HashCode * key, void *value)
764 {
765   struct AllocationRecord *ar = cls;
766   struct GNUNET_ATS_SuggestionContext *asc = value;
767
768   asc->cb (asc->cb_cls, &asc->target, ar->plugin_name, ar->plugin_addr,
769            ar->plugin_addr_len, ar->session,
770            GNUNET_BANDWIDTH_value_init (asc->atc->total_bps / 32), ar->ats,
771            ar->ats_count);
772   GNUNET_ATS_suggest_address_cancel (asc);
773   asc = NULL;
774   return GNUNET_OK;
775 }
776
777
778 /**
779  * We have updated performance statistics for a given address.  Note
780  * that this function can be called for addresses that are currently
781  * in use as well as addresses that are valid but not actively in use.
782  * Furthermore, the peer may not even be connected to us right now (in
783  * which case the call may be ignored or the information may be stored
784  * for later use).  Update bandwidth assignments.
785  *
786  * @param atc handle
787  * @param peer identity of the peer
788  * @param valid_until how long is the address valid?
789  * @param plugin_name name of the transport plugin
790  * @param session session handle (if available)
791  * @param plugin_addr address  (if available)
792  * @param plugin_addr_len number of bytes in plugin_addr
793  * @param ats performance data for the address
794  * @param ats_count number of performance records in 'ats'
795  */
796 void
797 GNUNET_ATS_address_update (struct GNUNET_ATS_Handle *atc,
798                            const struct GNUNET_PeerIdentity *peer,
799                            struct GNUNET_TIME_Absolute valid_until,
800                            const char *plugin_name, struct Session *session,
801                            const void *plugin_addr, size_t plugin_addr_len,
802                            const struct GNUNET_TRANSPORT_ATS_Information *ats,
803                            uint32_t ats_count)
804 {
805   struct AllocationRecord *ar;
806   struct UpdateSessionContext usc;
807
808 #if DEBUG_ATS
809   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-api",
810                    "Updating address for peer `%s', plugin `%s'\n",
811                    GNUNET_i2s (peer), plugin_name);
812 #endif
813   ar = create_allocation_record (plugin_name, session, plugin_addr,
814                                  plugin_addr_len, ats, ats_count);
815   usc.atc = atc;
816   usc.arnew = ar;
817   if (GNUNET_SYSERR ==
818       GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &update_session, &usc))
819   {
820     destroy_allocation_record (NULL, &peer->hashPubKey, ar);
821     return;
822   }
823 #if DEBUG_ATS
824   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-api",
825                    "Adding new address for peer `%s', plugin `%s'\n",
826                    GNUNET_i2s (peer), plugin_name);
827 #endif
828   GNUNET_assert (GNUNET_OK ==
829                  GNUNET_CONTAINER_multihashmap_put (atc->peers,
830                                                     &peer->hashPubKey, ar,
831                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
832   GNUNET_CONTAINER_multihashmap_get_multiple (atc->notify_map,
833                                               &peer->hashPubKey, &notify_valid,
834                                               ar);
835 }
836
837 /* end of file gnunet-service-transport_ats.c */