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