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