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