changes
[oweals/gnunet.git] / src / ats / gnunet-service-ats_addresses.c
1 /*
2      This file is part of GNUnet.
3      (C) 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 /**
22  * @file ats/gnunet-service-ats_addresses.c
23  * @brief ats service address management
24  * @author Matthias Wachs
25  * @author Christian Grothoff
26  */
27 #include "platform.h"
28 #include "gnunet_ats_service.h"
29 #include "gnunet-service-ats.h"
30 #include "gnunet-service-ats_addresses.h"
31 #include "gnunet-service-ats_performance.h"
32 #include "gnunet-service-ats_scheduling.h"
33 #include "gnunet-service-ats_reservations.h"
34 #if HAVE_LIBGLPK
35 #include "gnunet-service-ats_addresses_mlp.h"
36 #endif
37 #include "gnunet-service-ats_addresses_simplistic.h"
38
39 /**
40  * ATS address management
41  *
42  * Adding addresses:
43  *
44  * - If you add a new address without a session, a new address will be added
45  * - If you add this address again now with a session a, the existing address
46  *   will be updated with this session
47  * - If you add this address again now with a session b, a new address object
48  *   with this session will be added
49
50  * Destroying addresses:
51  *
52  * - If you destroy an address without a session, the address itself and all
53  *   address instances with an session will be removed
54  * - If you destroy an address with a session, the session for this address
55  *   will be removed
56  *
57  * Conclusion
58  * Addresses without a session will be updated with a new session and if the
59  * the session is destroyed the session is removed and address itself still
60  * exists for suggestion
61  *
62  */
63
64
65 /**
66  * Available ressource assignment modes
67  */
68 enum ATS_Mode
69 {
70   /*
71    * Simplistic mode:
72    *
73    * Assign each peer an equal amount of bandwidth (bw)
74    *
75    * bw_per_peer = bw_total / #active addresses
76    */
77   MODE_SIMPLISTIC,
78
79   /*
80    * MLP mode:
81    *
82    * Solve ressource assignment as an optimization problem
83    * Uses an mixed integer programming solver
84    */
85   MODE_MLP
86 };
87
88 /**
89  * Pending Address suggestion requests
90  */
91 struct GAS_Addresses_Suggestion_Requests
92 {
93   /**
94    * Next in DLL
95    */
96   struct GAS_Addresses_Suggestion_Requests *next;
97
98   /**
99    * Previous in DLL
100    */
101   struct GAS_Addresses_Suggestion_Requests *prev;
102
103   /**
104    * Peer ID
105    */
106   struct GNUNET_PeerIdentity id;
107 };
108
109 /**
110  * Handle for ATS address component
111  */
112 struct GAS_Addresses_Handle
113 {
114   /**
115    *
116    */
117   struct GNUNET_STATISTICS_Handle *stat;
118
119   /**
120    * A multihashmap to store all addresses
121    */
122   struct GNUNET_CONTAINER_MultiHashMap *addresses;
123
124   /**
125    * Configure WAN quota in
126    */
127   unsigned long long wan_quota_in;
128
129   /**
130    * Configure WAN quota out
131    */
132   unsigned long long wan_quota_out;
133
134   /**
135    * Is ATS addresses running
136    */
137   int running;
138
139   /**
140    * Configured ATS solver
141    */
142   int ats_mode;
143
144   /**
145    *  Solver handle
146    */
147   void *solver;
148
149   /**
150    * Address suggestion requests DLL head
151    */
152   struct GAS_Addresses_Suggestion_Requests *r_head;
153
154   /**
155    * Address suggestion requests DLL tail
156    */
157   struct GAS_Addresses_Suggestion_Requests *r_tail;
158
159   /* Solver functions */
160
161   /**
162    * Initialize solver
163    */
164   GAS_solver_init s_init;
165
166   /**
167    * Add an address to the solver
168    */
169   GAS_solver_address_add s_add;
170
171   /**
172    * Update address in solver
173    */
174   GAS_solver_address_update s_update;
175
176   /**
177    * Get address from solver
178    */
179   GAS_solver_get_preferred_address s_get;
180
181   /**
182    * Delete address in solver
183    */
184   GAS_solver_address_delete s_del;
185
186   /**
187    * Change preference for quality in solver
188    */
189   GAS_solver_address_change_preference s_pref;
190
191   /**
192    * Shutdown solver
193    */
194   GAS_solver_done s_done;
195 };
196
197
198 static unsigned int
199 assemble_ats_information (const struct ATS_Address *aa,  struct GNUNET_ATS_Information **dest)
200 {
201   unsigned int ats_count = GNUNET_ATS_PropertyCount - 1;
202   struct GNUNET_ATS_Information *ats = GNUNET_malloc (ats_count * sizeof (struct GNUNET_ATS_Information));
203   (*dest) = ats;
204
205   ats[0].type = ntohl(GNUNET_ATS_UTILIZATION_UP);
206   ats[0].value = aa->atsp_utilization_out.value__;
207   ats[1].type = ntohl(GNUNET_ATS_UTILIZATION_DOWN);
208   ats[1].value = aa->atsp_utilization_in.value__;
209   ats[2].type = ntohl(GNUNET_ATS_NETWORK_TYPE);
210   ats[2].value = ntohl(aa->atsp_network_type);
211   ats[3].type = ntohl(GNUNET_ATS_QUALITY_NET_DELAY);
212   ats[3].value = ntohl(aa->atsp_latency.rel_value);
213   ats[4].type = ntohl(GNUNET_ATS_QUALITY_NET_DISTANCE);
214   ats[4].value = ntohl(aa->atsp_distance);
215   ats[5].type = ntohl(GNUNET_ATS_COST_WAN);
216   ats[5].value = ntohl (aa->atsp_cost_wan);
217   ats[6].type = ntohl(GNUNET_ATS_COST_LAN);
218   ats[6].value = ntohl (aa->atsp_cost_lan);
219   ats[7].type = ntohl(GNUNET_ATS_COST_WLAN);
220   ats[7].value = ntohl (aa->atsp_cost_wlan);
221   return ats_count;
222 }
223
224 static unsigned int
225 disassemble_ats_information (const struct GNUNET_ATS_Information *src,
226                              uint32_t ats_count,
227                              struct ATS_Address *dest)
228 {
229   int i;
230   int res = 0;
231   for (i = 0; i < ats_count; i++)
232     switch (ntohl (src[i].type))
233     {
234     case GNUNET_ATS_UTILIZATION_UP:
235       dest->atsp_utilization_out.value__ = src[i].value;
236       res ++;
237       break;
238     case GNUNET_ATS_UTILIZATION_DOWN:
239       dest->atsp_utilization_in.value__ = src[i].value;
240       res ++;
241       break;
242     case GNUNET_ATS_QUALITY_NET_DELAY:
243       dest->atsp_latency.rel_value = ntohl (src[i].value);
244       res ++;
245       break;
246     case GNUNET_ATS_QUALITY_NET_DISTANCE:
247       dest->atsp_distance = ntohl (src[i].value);
248       res ++;
249       break;
250     case GNUNET_ATS_COST_WAN:
251       dest->atsp_cost_wan = ntohl (src[i].value);
252       res ++;
253       break;
254     case GNUNET_ATS_COST_LAN:
255       dest->atsp_cost_lan = ntohl (src[i].value);
256       res ++;
257       break;
258     case GNUNET_ATS_COST_WLAN:
259       dest->atsp_cost_wlan = ntohl (src[i].value);
260       res ++;
261       break;
262     case GNUNET_ATS_NETWORK_TYPE:
263       dest->atsp_network_type = ntohl (src[i].value);
264       res ++;
265       break;
266     case GNUNET_ATS_ARRAY_TERMINATOR:
267       break;
268     default:
269       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
270                   "Received unsupported ATS type %u\n", ntohl (src[i].type));
271       GNUNET_break (0);
272       break;
273     }
274   return res;
275 }
276
277 /**
278  * Free the given address
279  * @param addr address to destroy
280  */
281 static void
282 free_address (struct ATS_Address *addr)
283 {
284   GNUNET_free (addr->plugin);
285   GNUNET_free (addr);
286 }
287
288 /**
289  * Create a ATS_address with the given information
290  * @param peer peer
291  * @param plugin_name plugin
292  * @param plugin_addr address
293  * @param plugin_addr_len address length
294  * @param session_id session
295  * @return the ATS_Address
296  */
297 static struct ATS_Address *
298 create_address (const struct GNUNET_PeerIdentity *peer,
299                 const char *plugin_name,
300                 const void *plugin_addr, size_t plugin_addr_len,
301                 uint32_t session_id)
302 {
303   struct ATS_Address *aa = NULL;
304
305   aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
306   aa->peer = *peer;
307   aa->addr_len = plugin_addr_len;
308   aa->addr = &aa[1];
309   memcpy (&aa[1], plugin_addr, plugin_addr_len);
310   aa->plugin = GNUNET_strdup (plugin_name);
311   aa->session_id = session_id;
312   aa->active = GNUNET_NO;
313   aa->used = GNUNET_NO;
314   aa->solver_information = NULL;
315   aa->assigned_bw_in = GNUNET_BANDWIDTH_value_init(0);
316   aa->assigned_bw_out = GNUNET_BANDWIDTH_value_init(0);
317   return aa;
318 }
319
320
321 /**
322  * Destroy the given address.
323  *
324  * @param handle the address handle
325  * @param addr address to destroy
326  * @return GNUNET_YES if bandwidth allocations should be recalcualted
327  */
328 static int
329 destroy_address (struct GAS_Addresses_Handle *handle, struct ATS_Address *addr)
330 {
331   int ret;
332
333   ret = GNUNET_NO;
334   GNUNET_assert (GNUNET_YES ==
335                  GNUNET_CONTAINER_multihashmap_remove (handle->addresses,
336                                                        &addr->peer.hashPubKey,
337                                                        addr));
338   free_address (addr);
339   return ret;
340 }
341
342
343 struct CompareAddressContext
344 {
345   const struct ATS_Address *search;
346
347   /* exact_address != NULL if address and session is equal */
348   struct ATS_Address *exact_address;
349   /* exact_address != NULL if address and session is 0 */
350   struct ATS_Address *base_address;
351 };
352
353
354 static int
355 compare_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
356 {
357   struct CompareAddressContext *cac = cls;
358   struct ATS_Address *aa = value;
359
360   /* Find an matching exact address:
361    *
362    * Compare by:
363    * aa->addr_len == cac->search->addr_len
364    * aa->plugin == cac->search->plugin
365    * aa->addr == cac->search->addr
366    * aa->session == cac->search->session
367    *
368    * return as exact address
369    */
370   if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
371   {
372       if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
373         cac->exact_address = aa;
374   }
375
376   /* Find an matching base address:
377    *
378    * Properties:
379    *
380    * aa->session_id == 0
381    *
382    * Compare by:
383    * aa->addr_len == cac->search->addr_len
384    * aa->plugin == cac->search->plugin
385    * aa->addr == cac->search->addr
386    *
387    * return as base address
388    */
389   if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
390   {
391       if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == 0))
392         cac->base_address = aa;
393   }
394
395   /* Find an matching exact address based on session:
396    *
397    * Properties:
398    *
399    * cac->search->addr_len == 0
400    *
401    * Compare by:
402    * aa->plugin == cac->search->plugin
403    * aa->session_id == cac->search->session_id
404    *
405    * return as exact address
406    */
407   if (0 == cac->search->addr_len)
408   {
409       if ((0 == strcmp (aa->plugin, cac->search->plugin)) && (aa->session_id == cac->search->session_id))
410         cac->exact_address = aa;
411   }
412
413   if (cac->exact_address == NULL)
414     return GNUNET_YES; /* Continue iteration to find exact address */
415   else
416     return GNUNET_NO; /* Stop iteration since we have an exact address */
417 }
418
419
420 /**
421  * Find an existing equivalent address record.
422  * Compares by peer identity and network address OR by session ID
423  * (one of the two must match).
424  *
425  * @param handle the address handle
426  * @param peer peer to lookup addresses for
427  * @param addr existing address record
428  * @return existing address record, NULL for none
429  */
430 struct ATS_Address *
431 find_equivalent_address (struct GAS_Addresses_Handle *handle,
432                          const struct GNUNET_PeerIdentity *peer,
433                          const struct ATS_Address *addr)
434 {
435   struct CompareAddressContext cac;
436
437   cac.exact_address = NULL;
438   cac.base_address = NULL;
439   cac.search = addr;
440   GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
441                                               &compare_address_it, &cac);
442
443   if (cac.exact_address == NULL)
444     return cac.base_address;
445   return cac.exact_address;
446 }
447
448
449 static struct ATS_Address *
450 lookup_address (struct GAS_Addresses_Handle *handle,
451                 const struct GNUNET_PeerIdentity *peer,
452                 const char *plugin_name,
453                 const void *plugin_addr,
454                 size_t plugin_addr_len,
455                 uint32_t session_id,
456                 const struct GNUNET_ATS_Information *atsi,
457                 uint32_t atsi_count)
458 {
459   struct ATS_Address *aa;
460   struct ATS_Address *ea;
461
462   aa = create_address (peer,
463                        plugin_name,
464                        plugin_addr, plugin_addr_len,
465                        session_id);
466
467   /* Get existing address or address with session == 0 */
468   ea = find_equivalent_address (handle, peer, aa);
469   free_address (aa);
470   if (ea == NULL)
471   {
472     return NULL;
473   }
474   else if (ea->session_id != session_id)
475   {
476     return NULL;
477   }
478   return ea;
479 }
480
481
482 void
483 GAS_addresses_add (struct GAS_Addresses_Handle *handle,
484                    const struct GNUNET_PeerIdentity *peer,
485                    const char *plugin_name, const void *plugin_addr,
486                    size_t plugin_addr_len, uint32_t session_id,
487                    const struct GNUNET_ATS_Information *atsi,
488                    uint32_t atsi_count)
489 {
490   struct ATS_Address *aa;
491   struct ATS_Address *ea;
492   unsigned int ats_res;
493
494   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
495               "Received `%s' for peer `%s'\n",
496               "ADDRESS ADD",
497               GNUNET_i2s (peer));
498
499   if (GNUNET_NO == handle->running)
500     return;
501
502   GNUNET_assert (NULL != handle->addresses);
503
504   aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len,
505                        session_id);
506   if (atsi_count != (ats_res = disassemble_ats_information(atsi, atsi_count, aa)))
507   {
508       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
509                 "While adding address: had %u ATS elements to add, could only add %u\n",
510                 atsi_count, ats_res);
511   }
512
513   /* Get existing address or address with session == 0 */
514   ea = find_equivalent_address (handle, peer, aa);
515   if (ea == NULL)
516   {
517     /* We have a new address */
518     GNUNET_assert (GNUNET_OK ==
519                    GNUNET_CONTAINER_multihashmap_put (handle->addresses,
520                                                       &peer->hashPubKey, aa,
521                                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
522     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' session id %u, %p\n",
523                 GNUNET_i2s (peer), session_id, aa);
524     /* Tell solver about new address */
525     handle->s_add (handle->solver, handle->addresses, aa);
526     return;
527   }
528   GNUNET_free (aa->plugin);
529   GNUNET_free (aa);
530
531   if (ea->session_id != 0)
532   {
533       /* This address with the same session is already existing
534        * Should not happen */
535       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
536                 "Added already existing address for peer `%s' `%s' %p with new session %u\n",
537                 GNUNET_i2s (peer), plugin_name, session_id);
538       GNUNET_break (0);
539       return;
540   }
541
542   /* We have an address without an session, update this address */
543
544   /* Notify solver about update with atsi information and session */
545   handle->s_update (handle->solver, handle->addresses, ea, session_id, ea->used, atsi, atsi_count);
546
547   /* Do the update */
548   ea->session_id = session_id;
549   if (atsi_count != (ats_res = disassemble_ats_information(atsi, atsi_count, ea)))
550   {
551       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
552                   "While updating address: had %u ATS elements to add, could only add %u\n",
553                   atsi_count, ats_res);
554   }
555   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
556            "Updated existing address for peer `%s' %p with new session %u\n",
557            GNUNET_i2s (peer), ea, session_id);
558 }
559
560
561 void
562 GAS_addresses_update (struct GAS_Addresses_Handle *handle,
563                       const struct GNUNET_PeerIdentity *peer,
564                       const char *plugin_name, const void *plugin_addr,
565                       size_t plugin_addr_len, uint32_t session_id,
566                       const struct GNUNET_ATS_Information *atsi,
567                       uint32_t atsi_count)
568 {
569   struct ATS_Address *aa;
570   uint32_t ats_res;
571
572   if (GNUNET_NO == handle->running)
573     return;
574
575   GNUNET_assert (NULL != handle->addresses);
576
577   /* Get existing address */
578   aa = lookup_address (handle, peer, plugin_name, plugin_addr, plugin_addr_len,
579                        session_id, atsi, atsi_count);
580   if (aa == NULL)
581   {
582     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tried to update unknown address for peer `%s' `%s' session id %u\n",
583                 GNUNET_i2s (peer), plugin_name, session_id);
584     GNUNET_break (0);
585     return;
586   }
587
588   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
589                 "Received `%s' for peer `%s' address \n",
590                 "ADDRESS UPDATE",
591                 GNUNET_i2s (peer), aa);
592
593   /* Tell solver about update */
594   handle->s_update (handle->solver, handle->addresses, aa, session_id, aa->used, atsi, atsi_count);
595
596   /* Update address */
597   if (atsi_count != (ats_res = disassemble_ats_information (atsi, atsi_count, aa)))
598   {
599       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
600                 "While adding address: had %u ATS elements to add, could only add %u\n",
601                 atsi_count, ats_res);
602   }
603   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
604             "Updated %u ATS elements for address %p\n",
605             ats_res, aa);
606 }
607
608
609 struct DestroyContext
610 {
611   struct ATS_Address *aa;
612
613   struct GAS_Addresses_Handle *handle;
614
615   /**
616    * GNUNET_NO  : full address
617    * GNUNET_YES : just session
618    */
619   int result;
620 };
621
622
623 /**
624  * Delete an address
625  *
626  * If session != 0, just the session is deleted, the address itself still exists
627  * If session == 0, remove full address
628  * If session == 0 and addrlen == 0, destroy inbound address
629  *
630  * @param cls unused
631  * @param key unused
632  * @param value the 'struct ATS_Address'
633  * @return GNUNET_OK (continue to iterate)
634  */
635 static int
636 destroy_by_session_id (void *cls, const struct GNUNET_HashCode * key, void *value)
637 {
638   struct DestroyContext *dc = cls;
639   struct GAS_Addresses_Handle *handle = dc->handle;
640   const struct ATS_Address *des = dc->aa;
641   struct ATS_Address *aa = value;
642
643   GNUNET_assert (0 == memcmp (&aa->peer, &des->peer,
644                               sizeof (struct GNUNET_PeerIdentity)));
645
646
647   if (des->session_id == 0)
648   {
649     /* Session == 0, remove full address  */
650     if ((0 == strcmp (des->plugin, aa->plugin)) &&
651         (aa->addr_len == des->addr_len) &&
652         (0 == memcmp (des->addr, aa->addr, aa->addr_len)))
653     {
654
655       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
656                   "Deleting full address for peer `%s' session %u %p\n",
657                   GNUNET_i2s (&aa->peer), aa->session_id, aa);
658
659       /* Notify solver about deletion */
660       handle->s_del (handle->solver, handle->addresses, aa, GNUNET_NO);
661       destroy_address (handle, aa);
662       dc->result = GNUNET_NO;
663       return GNUNET_OK; /* Continue iteration */
664     }
665   }
666   else
667   {
668     /* Session != 0, just remove session */
669     if (aa->session_id != des->session_id)
670       return GNUNET_OK; /* irrelevant */
671
672     if ((aa->session_id != 0) &&
673         (0 != strcmp (des->plugin, aa->plugin)))
674     {
675         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
676                     "Different plugins during removal: `%s' vs `%s' \n",
677                     des->plugin, aa->plugin);
678         GNUNET_break (0);
679         return GNUNET_OK;
680     }
681
682     if (aa->addr_len == 0)
683     {
684         /* Inbound connection died, delete full address */
685         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
686                     "Deleting inbound address for peer `%s': `%s' session %u\n",
687                     GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
688
689         /* Notify solver about deletion */
690         handle->s_del (handle->solver, handle->addresses, aa, GNUNET_NO);
691         destroy_address (handle, aa);
692         dc->result = GNUNET_NO;
693         return GNUNET_OK; /* Continue iteration */
694     }
695     else
696     {
697         /* Session died */
698         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
699                     "Deleting session for peer `%s': `%s' %u\n",
700                     GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
701         /* Notify solver to delete session */
702         handle->s_del (handle->solver, handle->addresses, aa, GNUNET_YES);
703         aa->session_id = 0;
704         return GNUNET_OK;
705     }
706   }
707   return GNUNET_OK;
708 }
709
710 void
711 GAS_addresses_destroy (struct GAS_Addresses_Handle *handle,
712                        const struct GNUNET_PeerIdentity *peer,
713                        const char *plugin_name, const void *plugin_addr,
714                        size_t plugin_addr_len, uint32_t session_id)
715 {
716   struct ATS_Address *ea;
717   struct DestroyContext dc;
718
719   if (GNUNET_NO == handle->running)
720     return;
721
722   /* Get existing address */
723   ea = lookup_address (handle, peer, plugin_name, plugin_addr, plugin_addr_len,
724                        session_id, NULL, 0);
725
726   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
727               "Received `%s' for peer `%s' address %p session %u\n",
728               "ADDRESS DESTROY",
729               GNUNET_i2s (peer), ea, session_id);
730
731   if (ea == NULL)
732   {
733     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tried to destroy unknown address for peer `%s' `%s' session id %u\n",
734                 GNUNET_i2s (peer), plugin_name, session_id);
735     return;
736   }
737
738   GNUNET_break (0 < strlen (plugin_name));
739   dc.handle = handle;
740   dc.aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
741
742   GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
743                                               &destroy_by_session_id, &dc);
744   free_address (dc.aa);
745 }
746
747
748 int
749 GAS_addresses_in_use (struct GAS_Addresses_Handle *handle,
750                       const struct GNUNET_PeerIdentity *peer,
751                       const char *plugin_name, const void *plugin_addr,
752                       size_t plugin_addr_len, uint32_t session_id, int in_use)
753 {
754   struct ATS_Address *ea;
755
756   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
757                 "Received `%s' for peer `%s'\n",
758                 "ADDRESS IN USE",
759                 GNUNET_i2s (peer));
760
761   if (GNUNET_NO == handle->running)
762     return GNUNET_SYSERR;
763
764   ea = lookup_address (handle, peer, plugin_name,
765                         plugin_addr, plugin_addr_len,
766                         session_id, NULL, 0);
767   if (NULL == ea)
768   {
769     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
770                 "Trying to set unknown address `%s', %s %u %s \n",
771                 GNUNET_i2s (peer),
772                 plugin_name, session_id,
773                 (GNUNET_NO == in_use) ? "NO" : "YES");
774     GNUNET_break (0);
775     return GNUNET_SYSERR;
776   }
777   if (ea->used == in_use)
778   {
779     GNUNET_break (0);
780     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
781                 "Address in use called multiple times for peer `%s': %s -> %s \n",
782                 GNUNET_i2s (peer),
783                 (GNUNET_NO == ea->used) ? "NO" : "YES",
784                 (GNUNET_NO == in_use) ? "NO" : "YES");
785     return GNUNET_SYSERR;
786   }
787
788   /* Tell solver about update */
789   handle->s_update (handle->solver, handle->addresses, ea, session_id, in_use, NULL, 0);
790   ea->used = in_use;
791
792   return GNUNET_OK;
793 }
794
795
796 /**
797  * Cancel address suggestions for a peer
798  *
799  * @param handle the address handle
800  * @param peer the respective peer
801  */
802 void
803 GAS_addresses_request_address_cancel (struct GAS_Addresses_Handle *handle,
804                                       const struct GNUNET_PeerIdentity *peer)
805 {
806   struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
807
808   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
809               "Received request: `%s' for peer %s\n", "request_address_cancel", GNUNET_i2s (peer));
810
811   while (NULL != cur)
812   {
813       if (0 == memcmp (peer, &cur->id, sizeof (cur->id)))
814         break; /* found */
815       cur = cur->next;
816   }
817
818   if (NULL == cur)
819   {
820       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
821                   "No address requests pending for peer `%s', cannot remove!\n", GNUNET_i2s (peer));
822       return;
823   }
824   GAS_addresses_handle_backoff_reset (handle, peer);
825   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
826               "Removed request pending for peer `%s\n", GNUNET_i2s (peer));
827   GNUNET_CONTAINER_DLL_remove (handle->r_head, handle->r_tail, cur);
828   GNUNET_free (cur);
829 }
830
831
832 /**
833  * Add an address suggestions for a peer
834  *
835  * @param handle the address handle
836  * @param peer the respective peer
837  */
838 void
839 GAS_addresses_request_address (struct GAS_Addresses_Handle *handle,
840                                const struct GNUNET_PeerIdentity *peer)
841 {
842   struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
843   struct ATS_Address *aa;
844   struct GNUNET_ATS_Information *ats;
845   unsigned int ats_count;
846
847   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
848               "Received `%s' for peer `%s'\n",
849               "REQUEST ADDRESS",
850               GNUNET_i2s (peer));
851
852   if (GNUNET_NO == handle->running)
853     return;
854   while (NULL != cur)
855   {
856       if (0 == memcmp (peer, &cur->id, sizeof (cur->id)))
857         break; /* already suggesting */
858       cur = cur->next;
859   }
860   if (NULL == cur)
861   {
862       cur = GNUNET_malloc (sizeof (struct GAS_Addresses_Suggestion_Requests));
863       cur->id = (*peer);
864       GNUNET_CONTAINER_DLL_insert (handle->r_head, handle->r_tail, cur);
865   }
866
867   /* Get prefered address from solver */
868   aa = (struct ATS_Address *) handle->s_get (handle->solver, handle->addresses, peer);
869   if (NULL == aa)
870   {
871     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
872                 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
873     return;
874   }
875
876   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
877               "Suggesting address %p for peer `%s'\n", aa, GNUNET_i2s (peer));
878
879   ats_count = assemble_ats_information (aa, &ats);
880   GAS_scheduling_transmit_address_suggestion (peer,
881                                               aa->plugin,
882                                               aa->addr, aa->addr_len,
883                                               aa->session_id,
884                                               ats, ats_count,
885                                               aa->assigned_bw_out,
886                                               aa->assigned_bw_in);
887
888   aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval, ATS_BLOCKING_DELTA);
889   aa->blocked_until = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(), aa->block_interval);
890
891   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
892        "Address %p ready for suggestion, block interval now %llu \n",
893        aa, aa->block_interval);
894
895   GNUNET_free (ats);
896 }
897
898
899 static int
900 reset_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
901 {
902   struct ATS_Address *aa = value;
903
904   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
905               "Resetting interval for peer `%s' address %p from %llu to 0\n",
906               GNUNET_i2s (&aa->peer), aa, aa->block_interval);
907
908   aa->blocked_until = GNUNET_TIME_UNIT_ZERO_ABS;
909   aa->block_interval = GNUNET_TIME_UNIT_ZERO;
910   return GNUNET_OK;
911 }
912
913
914 void
915 GAS_addresses_handle_backoff_reset (struct GAS_Addresses_Handle *handle,
916                                     const struct GNUNET_PeerIdentity *peer)
917 {
918   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
919               "Received `%s' for peer `%s'\n",
920               "RESET BACKOFF",
921               GNUNET_i2s (peer));
922
923   GNUNET_break (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses,
924                                               &peer->hashPubKey,
925                                               &reset_address_it,
926                                               NULL));
927 }
928
929
930 void
931 GAS_addresses_change_preference (struct GAS_Addresses_Handle *handle,
932                                  void *client,
933                                  const struct GNUNET_PeerIdentity *peer,
934                                  enum GNUNET_ATS_PreferenceKind kind,
935                                  float score)
936 {
937   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
938               "Received `%s' for peer `%s' for client %p\n",
939               "CHANGE PREFERENCE",
940               GNUNET_i2s (peer), client);
941
942   if (GNUNET_NO == handle->running)
943     return;
944
945   if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->addresses,
946                                                           &peer->hashPubKey))
947   {
948       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
949                   "Received `%s' for unknown peer `%s' from client %p\n",
950                   "CHANGE PREFERENCE",
951                   GNUNET_i2s (peer), client);
952       return;
953   }
954
955   /* Tell solver about update */
956   handle->s_pref (handle->solver, client, peer, kind, score);
957 }
958
959 static unsigned int
960 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned long long *out_dest, unsigned long long *in_dest, int dest_length)
961 {
962   char *network_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
963   char * entry_in = NULL;
964   char * entry_out = NULL;
965   char * quota_out_str;
966   char * quota_in_str;
967   int c;
968
969   for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
970   {
971     in_dest[c] = 0;
972     out_dest[c] = 0;
973
974     GNUNET_asprintf (&entry_out, "%s_QUOTA_OUT", network_str[c]);
975     GNUNET_asprintf (&entry_in, "%s_QUOTA_IN", network_str[c]);
976
977
978     /* quota out */
979     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, &quota_out_str))
980     {
981       if (0 == strcmp(quota_out_str, BIG_M_STRING))
982       {
983         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Outbound quota configure for network `%s' is unlimited (%llu)\n"),
984             network_str[c], GNUNET_ATS_MaxBandwidth);
985         out_dest[c] = GNUNET_ATS_MaxBandwidth;
986       }
987       else if (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &out_dest[c]))
988       {
989         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not convert quota configure for network `%s':  `%s', assigning default bandwidth %llu\n"),
990             network_str[c], quota_out_str, GNUNET_ATS_DefaultBandwidth);
991         out_dest[c] = GNUNET_ATS_DefaultBandwidth;
992       }
993       GNUNET_free (quota_out_str);
994     }
995     else
996     {
997       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("No outbound quota configure for network `%s', assigning default bandwidth %llu\n"),
998           network_str[c], GNUNET_ATS_DefaultBandwidth);
999       out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1000     }
1001
1002     /* quota in */
1003     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, &quota_in_str))
1004     {
1005       if (0 == strcmp(quota_in_str, BIG_M_STRING))
1006       {
1007         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Inbound quota configure for network `%s' is unlimited (%llu)\n"),
1008             network_str[c], GNUNET_ATS_MaxBandwidth);
1009         in_dest[c] = GNUNET_ATS_MaxBandwidth;
1010       }
1011
1012       else if (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c]))
1013       {
1014         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not convert quota configure for network `%s':  `%s', assigning default bandwidth %llu\n"),
1015             network_str[c], quota_in_str, GNUNET_ATS_DefaultBandwidth);
1016         in_dest[c] = GNUNET_ATS_DefaultBandwidth;
1017       }
1018       GNUNET_free (quota_in_str);
1019     }
1020     else
1021     {
1022       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("No inbound quota configure for network `%s', assigning default bandwidth %llu\n"),
1023           network_str[c], GNUNET_ATS_DefaultBandwidth);
1024       in_dest[c] = GNUNET_ATS_DefaultBandwidth;
1025     }
1026     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loaded quota for network `%s' (in/out): %llu %llu\n", network_str[c], in_dest[c], out_dest[c]);
1027     GNUNET_free (entry_out);
1028     GNUNET_free (entry_in);
1029   }
1030   return GNUNET_ATS_NetworkTypeCount;
1031 }
1032
1033
1034 static void
1035 bandwidth_changed_cb (void *cls, struct ATS_Address *address)
1036 {
1037   struct GAS_Addresses_Handle *handle = cls;
1038   struct GAS_Addresses_Suggestion_Requests *cur;
1039
1040   GNUNET_assert (handle != NULL);
1041   GNUNET_assert (address != NULL);
1042
1043   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Bandwidth assignment changed for peer %s \n", GNUNET_i2s(&address->peer));
1044   struct GNUNET_ATS_Information *ats;
1045   unsigned int ats_count;
1046
1047   cur = handle->r_head;
1048   while (NULL != cur)
1049   {
1050       if (0 == memcmp (&address->peer, &cur->id, sizeof (cur->id)))
1051         break; /* we have an address request pending*/
1052       cur = cur->next;
1053   }
1054   if (NULL == cur)
1055   {
1056       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1057                   "Nobody is interested in peer `%s' :(\n",GNUNET_i2s (&address->peer));
1058       return;
1059   }
1060
1061   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1062               "Sending bandwidth update for peer `%s'\n",GNUNET_i2s (&address->peer));
1063
1064   ats_count = assemble_ats_information (address, &ats);
1065   GAS_scheduling_transmit_address_suggestion (&address->peer,
1066                                               address->plugin,
1067                                               address->addr, address->addr_len,
1068                                               address->session_id,
1069                                               ats, ats_count,
1070                                               address->assigned_bw_out,
1071                                               address->assigned_bw_in);
1072   GNUNET_free (ats);
1073 }
1074
1075
1076 /**
1077  * Initialize address subsystem.
1078  *
1079  * @param cfg configuration to use
1080  * @param stats the statistics handle to use
1081  */
1082 struct GAS_Addresses_Handle *
1083 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1084                     const struct GNUNET_STATISTICS_Handle *stats)
1085 {
1086   struct GAS_Addresses_Handle *ah;
1087   int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
1088   unsigned long long  quotas_in[GNUNET_ATS_NetworkTypeCount];
1089   unsigned long long  quotas_out[GNUNET_ATS_NetworkTypeCount];
1090   int quota_count;
1091   char *mode_str;
1092   int c;
1093
1094   ah = GNUNET_malloc (sizeof (struct GAS_Addresses_Handle));
1095   ah->running = GNUNET_NO;
1096
1097   ah->stat = (struct GNUNET_STATISTICS_Handle *) stats;
1098   /* Initialize the addresses database */
1099   ah->addresses = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
1100   GNUNET_assert (NULL != ah->addresses);
1101
1102   /* Figure out configured solution method */
1103   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "MODE", &mode_str))
1104   {
1105       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "No ressource assignment method configured, using simplistic approch\n");
1106       ah->ats_mode = MODE_SIMPLISTIC;
1107   }
1108   else
1109   {
1110       for (c = 0; c < strlen (mode_str); c++)
1111         mode_str[c] = toupper (mode_str[c]);
1112       if (0 == strcmp (mode_str, "SIMPLISTIC"))
1113       {
1114           ah->ats_mode = MODE_SIMPLISTIC;
1115       }
1116       else if (0 == strcmp (mode_str, "MLP"))
1117       {
1118           ah->ats_mode = MODE_MLP;
1119 #if !HAVE_LIBGLPK
1120           GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Assignment method `%s' configured, but GLPK is not availabe, please install \n", mode_str);
1121           ah->ats_mode = MODE_SIMPLISTIC;
1122 #endif
1123       }
1124       else
1125       {
1126           GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid ressource assignment method `%s' configured, using simplistic approch\n", mode_str);
1127           ah->ats_mode = MODE_SIMPLISTIC;
1128       }
1129       GNUNET_free (mode_str);
1130   }
1131   /* Start configured solution method */
1132   switch (ah->ats_mode)
1133   {
1134     case MODE_MLP:
1135       /* Init the MLP solver with default values */
1136 #if HAVE_LIBGLPK
1137       ah->ats_mode = MODE_MLP;
1138       ah->s_init = &GAS_mlp_init;
1139       ah->s_add = &GAS_mlp_address_add;
1140       ah->s_update = &GAS_mlp_address_update;
1141       ah->s_get = &GAS_mlp_get_preferred_address;
1142       ah->s_pref = &GAS_mlp_address_change_preference;
1143       ah->s_del =  &GAS_mlp_address_delete;
1144       ah->s_done = &GAS_mlp_done;
1145 #else
1146       GNUNET_free (ah);
1147       return NULL;
1148 #endif
1149       break;
1150     case MODE_SIMPLISTIC:
1151       /* Init the simplistic solver with default values */
1152       ah->ats_mode = MODE_SIMPLISTIC;
1153       ah->s_init = &GAS_simplistic_init;
1154       ah->s_add = &GAS_simplistic_address_add;
1155       ah->s_update = &GAS_simplistic_address_update;
1156       ah->s_get = &GAS_simplistic_get_preferred_address;
1157       ah->s_pref = &GAS_simplistic_address_change_preference;
1158       ah->s_del  = &GAS_simplistic_address_delete;
1159       ah->s_done = &GAS_simplistic_done;
1160       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS started in %s mode\n", "SIMPLISTIC");
1161       break;
1162     default:
1163       return NULL;
1164       break;
1165   }
1166
1167   GNUNET_assert (NULL != ah->s_init);
1168   GNUNET_assert (NULL != ah->s_add);
1169   GNUNET_assert (NULL != ah->s_update);
1170   GNUNET_assert (NULL != ah->s_get);
1171   GNUNET_assert (NULL != ah->s_pref);
1172   GNUNET_assert (NULL != ah->s_del);
1173   GNUNET_assert (NULL != ah->s_done);
1174
1175   quota_count = load_quotas(cfg, quotas_in, quotas_out, GNUNET_ATS_NetworkTypeCount);
1176
1177   ah->solver = ah->s_init (cfg, stats, quotas, quotas_in, quotas_out, quota_count, &bandwidth_changed_cb, ah);
1178   if (NULL == ah->solver)
1179   {
1180     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to initialize solver!\n");
1181     GNUNET_free (ah);
1182     return NULL;
1183   }
1184
1185   /* up and running */
1186   ah->running = GNUNET_YES;
1187   return ah;
1188 }
1189
1190
1191 /**
1192  * Free memory of address.
1193  *
1194  * @param cls NULL
1195  * @param key peer identity (unused)
1196  * @param value the 'struct ATS_Address' to free
1197  * @return GNUNET_OK (continue to iterate)
1198  */
1199 static int
1200 free_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1201 {
1202   struct GAS_Addresses_Handle *handle = cls;
1203   struct ATS_Address *aa = value;
1204   handle->s_del (handle->solver, handle->addresses, aa, GNUNET_NO);
1205   destroy_address (handle, aa);
1206   return GNUNET_OK;
1207 }
1208
1209
1210 void
1211 GAS_addresses_destroy_all (struct GAS_Addresses_Handle *handle)
1212 {
1213   if (GNUNET_NO == handle->running)
1214     return;
1215
1216   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1217               "Received `%s'\n",
1218               "DESTROY ALL");
1219
1220   if (handle->addresses != NULL)
1221     GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &free_address_it, handle);
1222 }
1223
1224
1225 /**
1226  * Shutdown address subsystem.
1227  */
1228 void
1229 GAS_addresses_done (struct GAS_Addresses_Handle *handle)
1230 {
1231   struct GAS_Addresses_Suggestion_Requests *cur;
1232
1233   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1234               "Shutting down addresses\n");
1235   GNUNET_assert (NULL != handle);
1236   GAS_addresses_destroy_all (handle);
1237   handle->running = GNUNET_NO;
1238   GNUNET_CONTAINER_multihashmap_destroy (handle->addresses);
1239   handle->addresses = NULL;
1240   while (NULL != (cur = handle->r_head))
1241   {
1242       GNUNET_CONTAINER_DLL_remove (handle->r_head, handle->r_tail, cur);
1243       GNUNET_free (cur);
1244   }
1245   handle->s_done (handle->solver);
1246   GNUNET_free (handle);
1247   /* Stop configured solution method */
1248
1249 }
1250
1251 struct PeerIteratorContext
1252 {
1253   GNUNET_ATS_Peer_Iterator it;
1254   void *it_cls;
1255   struct GNUNET_CONTAINER_MultiHashMap *peers_returned;
1256 };
1257
1258 static int
1259 peer_it (void *cls,
1260          const struct GNUNET_HashCode * key,
1261          void *value)
1262 {
1263   struct PeerIteratorContext *ip_ctx = cls;
1264   struct GNUNET_PeerIdentity tmp;
1265
1266   if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(ip_ctx->peers_returned, key))
1267   {
1268       GNUNET_CONTAINER_multihashmap_put(ip_ctx->peers_returned, key, NULL, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1269       tmp.hashPubKey = (*key);
1270       ip_ctx->it (ip_ctx->it_cls, &tmp);
1271   }
1272
1273   return GNUNET_OK;
1274 }
1275
1276 /**
1277  * Return all peers currently known to ATS
1278  *
1279  * @param handle the address handle
1280  * @param p_it the iterator to call for every peer, callbach with id == NULL
1281  *        when done
1282  * @param p_it_cls the closure for the iterator
1283  */
1284 void
1285 GAS_addresses_iterate_peers (struct GAS_Addresses_Handle *handle, GNUNET_ATS_Peer_Iterator p_it, void *p_it_cls)
1286 {
1287   struct PeerIteratorContext ip_ctx;
1288   unsigned int size;
1289
1290   if (NULL == p_it)
1291       return;
1292   GNUNET_assert (NULL != handle->addresses);
1293
1294   size = GNUNET_CONTAINER_multihashmap_size(handle->addresses);
1295   if (0 != size)
1296   {
1297     ip_ctx.it = p_it;
1298     ip_ctx.it_cls = p_it_cls;
1299     ip_ctx.peers_returned = GNUNET_CONTAINER_multihashmap_create (size, GNUNET_NO);
1300     GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &peer_it, &ip_ctx);
1301     GNUNET_CONTAINER_multihashmap_destroy (ip_ctx.peers_returned);
1302   }
1303   p_it (p_it_cls, NULL);
1304 }
1305
1306 struct PeerInfoIteratorContext
1307 {
1308   GNUNET_ATS_PeerInfo_Iterator it;
1309   void *it_cls;
1310 };
1311
1312
1313 static int 
1314 peerinfo_it (void *cls,
1315              const struct GNUNET_HashCode * key,
1316              void *value)
1317 {
1318   struct PeerInfoIteratorContext *pi_ctx = cls;
1319   struct ATS_Address *addr = (struct ATS_Address *)  value;
1320   struct GNUNET_ATS_Information *ats;
1321   uint32_t ats_count;
1322
1323   if (NULL != pi_ctx->it)
1324   {
1325     ats_count = assemble_ats_information (addr, &ats);
1326
1327     pi_ctx->it (pi_ctx->it_cls,
1328                 &addr->peer,
1329                 addr->plugin,
1330                 addr->addr, addr->addr_len,
1331                 addr->active,
1332                 ats, ats_count,
1333                 addr->assigned_bw_out,
1334                 addr->assigned_bw_in);
1335     GNUNET_free (ats);
1336   }
1337   return GNUNET_YES;
1338 }
1339
1340
1341 /**
1342  * Return all peers currently known to ATS
1343  *
1344  * @param handle the address handle
1345  * @param peer the respective peer
1346  * @param pi_it the iterator to call for every peer
1347  * @param pi_it_cls the closure for the iterator
1348  */
1349 void
1350 GAS_addresses_get_peer_info (struct GAS_Addresses_Handle *handle,
1351                              const struct GNUNET_PeerIdentity *peer,
1352                              GNUNET_ATS_PeerInfo_Iterator pi_it,
1353                              void *pi_it_cls)
1354 {
1355   struct PeerInfoIteratorContext pi_ctx;
1356   struct GNUNET_BANDWIDTH_Value32NBO zero_bw;
1357   GNUNET_assert (NULL != peer);
1358   GNUNET_assert (NULL != handle->addresses);
1359   if (NULL == pi_it)
1360     return; /* does not make sense without callback */
1361
1362   zero_bw = GNUNET_BANDWIDTH_value_init (0);
1363   pi_ctx.it = pi_it;
1364   pi_ctx.it_cls = pi_it_cls;
1365
1366   GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey, &peerinfo_it, &pi_ctx);
1367
1368   if (NULL != pi_it)
1369     pi_it (pi_it_cls, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, zero_bw, zero_bw);
1370
1371 }
1372
1373
1374 /* end of gnunet-service-ats_addresses.c */