renaming
[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_ats_plugin.h"
30 #include "gnunet-service-ats.h"
31 #include "gnunet-service-ats_addresses.h"
32 #include "gnunet-service-ats_normalization.h"
33 #include "gnunet-service-ats_performance.h"
34 #include "gnunet-service-ats_scheduling.h"
35 #include "gnunet-service-ats_reservations.h"
36
37 #if 0
38 #if HAVE_LIBGLPK
39 #include "gnunet-service-ats-solver_mlp.h"
40 #endif
41 #include "gnunet-service-ats-solver_proportional.h"
42 #include "gnunet-service-ats-solver_ril.h"
43 #endif
44
45 /**
46  * NOTE: Do not change this documentation. This documentation is based on
47  * gnunet.org:/vcs/fsnsg/ats-paper.git/tech-doku/ats-tech-guide.tex
48  * use build_txt.sh to generate plaintext output
49  *
50  *   1 ATS addresses : ATS address management
51  *
52  *    This ATS addresses ("addresses") component manages the addresses known to
53  *    ATS service and suggests addresses to transport service when it is
54  *    interested in address suggestion for a peer. ATS addresses also
55  *    instantiates the bandwidth assignment mechanism (solver), notifies it
56  *    about changes to addresses and forwards changes to bandwidth assignments
57  *    to transport, depending if transport is interested in this change.
58  *
59  *     1.1 Input data
60  *
61  *       1.1.1 Addresses
62  *
63  *    Addresses are added by specifying peer ID, plugin, address, address length
64  *    and session, if available. ATS information can be specified if available.
65  *
66  *       1.1.2 Networks
67  *
68  *    ATS specifies a fix set of networks an address can belong to. For each
69  *    network an inbound and outbound quota will be specified. The available
70  *    networks and addtional helper varaibles are defined in
71  *    gnunet_ats_service.h. At the moment 5 networks are defined:
72  *      * GNUNET_ATS_NET_UNSPECIFIED
73  *      * GNUNET_ATS_NET_LOOPBACK
74  *      * GNUNET_ATS_NET_LAN
75  *      * GNUNET_ATS_NET_WAN
76  *      * GNUNET_ATS_NET_WLAN
77  *
78  *    The total number of networks defined is stored in
79  *    GNUNET_ATS_NetworkTypeCount GNUNET_ATS_NetworkType can be used array
80  *    initializer for an int array, while GNUNET_ATS_NetworkType is an
81  *    initializer for a char array containing a string description of all
82  *    networks
83  *
84  *       1.1.3 Quotas
85  *
86  *    An inbound and outbound quota for each of the networks mentioned in 1.1.2
87  *    is loaded from ats configuration during initialization. This quota defines
88  *    to total amount of inbound and outbound traffic allowed for a specific
89  *    network. The configuration values used are in section ats:
90  *      * "NETWORK"_QUOTA_IN = <value>
91  *      * "NETWORK"_QUOTA_IN = <value>
92  *
93  *    You can specify quotas by setting the <value> to a:
94  *      * unrestricted: unlimited
95  *      * number of bytes: e.g. 10240
96  *      * fancy value: e.g. 64 Kib
97  *
98  *    unlimited is defined as GNUNET_ATS_MaxBandwidthString and equivalent to
99  *    the value GNUNET_ATS_MaxBandwidth Important predefined values for quotas
100  *    are:
101  *      * GNUNET_ATS_DefaultBandwidth: 65536
102  *      * GNUNET_ATS_MaxBandwidth: UINT32_MAX
103  *      * GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT: 1024
104  *
105  *    Details of loading quotas and default values will be described on
106  *
107  *       1.1.4 Preference values
108  *
109  *     1.2 Data structures used
110  *
111  *    Addresse uses struct ATS_Address for each address. The structs are stored
112  *    in a linked list and provides a pointer void *solver_information for the
113  *    solver to store address specific information. It provides the int values
114  *    active which is set to GNUNET_YES if the address is select for transport
115  *    use and used, representing that transport service is actively using this
116  *    address. Address information are stored in peer, addr, addr_len, plugin.
117  *
118  *     1.3 Initialization
119  *
120  *    During initialization a hashmap to store addresses is created. The quotas
121  *    for all networks defined for ATS are loaded from configuration. For each
122  *    network first the logic will check if the string
123  *    GNUNET_ATS_MaxBandwidthString is configured, if not it will try to convert
124  *    the configured value as a fancy size and if this fails it will try to use
125  *    it as a value_number. If no configuration value is found it will assign
126  *    GNUNET_ATS_DefaultBandwidth. The most important step is to load the
127  *    configured solver using configuration "[ats]:MODE". Current solvers are
128  *    MODE_PROPORTIONAL, MODE_MLP. Interaction is done using a solver API
129  *
130  *     1.4 Solver API
131  *
132  *    Solver functions:
133  *      * s_init: init the solver with required information
134  *      * s_add: add a new address
135  *      * s_update: update ATS values or session for an address
136  *      * s_get: get prefered address for a peer
137  *      * s_del: delete an address
138  *      * s_pref: change preference value for a peer
139  *      * s_done: shutdown solver
140  *
141  *    Callbacks: addresses provides a bandwidth_changed_cb callback to the
142  *    solver which is called when bandwidth assigned to peer has changed
143  *
144  *     1.5 Shutdown
145  *
146  *    During shutdown all addresses are freed and the solver told to shutdown
147  *
148  *     1.6 Addresses and sessions
149  *
150  *    Addresses consist of the address itself and a numerical session. When a
151  *    new address without a session is added it has no session, so it gets
152  *    session 0 assigned. When an address with a session is added and an address
153  *    object with session 0 is found, this object is updated with the session
154  *    otherwise a new address object with this session assigned is created.
155  *
156  *       1.6.1 Terminology
157  *
158  *    Addresses a1,a2 with session s1, s2 are "exact" if:
159  *    (a1 == a2)&&(s1 == s2)
160  *    Addresses a1,a2 with session s1, s2 are "equivalent" if:
161  *    (a1 == a2)&&((s1 == s2)||(s1 == 0)||(s2 == 0)
162  *
163  *     1.7 Address management
164  *
165  *    Transport service notifies ATS about changes to the addresses known to
166  *    him.
167  *
168  *       1.7.1 Adding an address
169  *
170  *    When transport learns a new address it tells ATS and ATS is telling
171  *    addresses about it using GAS_address_add. If not known to addresses it
172  *    creates a new address object and calls solver's s_add. ATS information are
173  *    deserialized and solver is notified about the session and ATS information
174  *    using s_update.
175  *
176  *       1.7.2 Updating an address
177  *
178  *    Addresses does an lookup up for the existing address with the given
179  *    session. If disassembles included ATS information and notifies the solver
180  *    using s_update about the update.
181  *
182  *       1.7.3 Deleting an address
183  *
184  *    Addresses does an lookup for the exact address and session and if removes
185  *    this address. If session != 0 the session is set to 0 and the address is
186  *    kept. If session == 0, the addresses is removed.
187  *
188  *       1.7.4 Requesting an address suggestion
189  *
190  *    The address client issues a request address message to be notified about
191  *    address suggestions for a specific peer. Addresses asks the solver with
192  *    s_get. If no address is available, it will not send a response, otherwise
193  *    it will respond with the choosen address.
194  *
195  *       1.7.5 Address suggestions
196  *
197  *    Addresses will notify the client automatically on any bandwidth_changed_cb
198  *    by the solver if a address suggestion request is pending. If no address is
199  *    available it will not respond at all If the client is not interested
200  *    anymore, it has to cancel the address suggestion request.
201  *
202  *       1.7.6 Suggestions blocks and reset
203  *
204  *    After suggesting an address it is blocked for ATS_BLOCKING_DELTA sec. to
205  *    prevent the client from being thrashed. If the client requires immediately
206  *    it can reset this block using GAS_addresses_handle_backoff_reset.
207  *
208  *       1.7.7 Marking address in use
209  *
210  *    The client can notify addresses that it successfully uses an address and
211  *    wants this address to be kept by calling GSA_address_in_use. Adresses will
212  *    mark the address as used an notify the solver about the use.
213  *
214  *       1.7.8 Address lifecycle
215  *
216  *      * (add address)
217  *      * (updated address) || (address in use)
218  *      * (delete address)
219  *
220  *     1.8 Bandwidth assignment
221  *
222  *    The addresses are used to perform resource allocation operations. ATS
223  *    addresses takes care of instantiating the solver configured and notifies
224  *    the respective solver about address changes and receives changes to the
225  *    bandwidth assignment from the solver. The current bandwidth assignment is
226  *    sent to transport. The specific solvers will be described in the specific
227  *    section.
228  *
229  *     1.9 Changing peer preferences
230  *
231  *    The bandwidth assigned to a peer can be influenced by setting a preference
232  *    for a peer. The prefernce will be given to to the solver with s_pref which
233  *    has to take care of the preference value
234
235  */
236
237 /**
238  * Available ressource assignment modes
239  */
240 enum ATS_Mode
241 {
242   /*
243    * proportional mode:
244    *
245    * Assign each peer an equal amount of bandwidth (bw)
246    *
247    * bw_per_peer = bw_total / #active addresses
248    */
249   MODE_PROPORTIONAL,
250
251   /*
252    * MLP mode:
253    *
254    * Solve ressource assignment as an optimization problem
255    * Uses an mixed integer programming solver
256    */
257   MODE_MLP,
258
259   /*
260    * Reinforcement Learning mode:
261    *
262    * Solve resource assignment using a learning agent
263    */
264   MODE_RIL
265 };
266
267 /**
268  * Pending Address suggestion requests
269  */
270 struct GAS_Addresses_Suggestion_Requests
271 {
272   /**
273    * Next in DLL
274    */
275   struct GAS_Addresses_Suggestion_Requests *next;
276
277   /**
278    * Previous in DLL
279    */
280   struct GAS_Addresses_Suggestion_Requests *prev;
281
282   /**
283    * Peer ID
284    */
285   struct GNUNET_PeerIdentity id;
286 };
287
288 /**
289  * Handle for ATS address component
290  */
291 struct GAS_Addresses_Handle
292 {
293   /**
294    *
295    */
296   struct GNUNET_STATISTICS_Handle *stat;
297
298   /**
299    * A multihashmap to store all addresses
300    */
301   struct GNUNET_CONTAINER_MultiPeerMap *addresses;
302
303   /**
304    * Configure WAN quota in
305    */
306   unsigned long long wan_quota_in;
307
308   /**
309    * Configure WAN quota out
310    */
311   unsigned long long wan_quota_out;
312
313   /**
314    * Is ATS addresses running
315    */
316   int running;
317
318   /**
319    * Configured ATS solver
320    */
321   int ats_mode;
322
323   /**
324    *  Solver handle
325    */
326   void *solver;
327
328   /**
329    * Address suggestion requests DLL head
330    */
331   struct GAS_Addresses_Suggestion_Requests *r_head;
332
333   /**
334    * Address suggestion requests DLL tail
335    */
336   struct GAS_Addresses_Suggestion_Requests *r_tail;
337
338   /* Solver functions */
339   struct GNUNET_ATS_PluginEnvironment env;
340
341   char *plugin;
342 };
343
344 /**
345  * Disassemble ATS information and update performance information in address
346  *
347  * Updates existing information and adds new information
348  *
349  * @param dest destination address
350  * @param update source ATS information
351  * @param update_count number of ATS information
352  * @param delta_dest ats performance information which were updated
353  *                              including previous value
354  * @param delta_count number of ATS information in the delta
355  * @return GNUNET_YES if address was address updated, GNUNET_NO otherwise
356  */
357 static unsigned int
358 disassemble_ats_information (struct ATS_Address *dest,
359     const struct GNUNET_ATS_Information *update, uint32_t update_count,
360     struct GNUNET_ATS_Information **delta_dest, uint32_t *delta_count)
361 {
362
363   int c1;
364   int c2;
365   int found;
366   int change;
367
368   struct GNUNET_ATS_Information add_atsi[update_count];
369   struct GNUNET_ATS_Information delta_atsi[update_count];
370   struct GNUNET_ATS_Information *tmp_atsi;
371   uint32_t add_atsi_count;
372   uint32_t delta_atsi_count;
373
374   change = GNUNET_NO;
375   add_atsi_count = 0;
376   delta_atsi_count = 0;
377
378   if (0 == update_count)
379     return GNUNET_NO;
380
381   if (NULL == dest->atsi)
382   {
383     /* Create performance information */
384     dest->atsi =
385         GNUNET_malloc (update_count * sizeof (struct GNUNET_ATS_Information));
386     dest->atsi_count = update_count;
387     memcpy (dest->atsi, update,
388         update_count * sizeof(struct GNUNET_ATS_Information));
389     (*delta_dest) =
390         GNUNET_malloc (update_count * sizeof (struct GNUNET_ATS_Information));
391     for (c1 = 0; c1 < update_count; c1++)
392     {
393       (*delta_dest)[c1].type = update[c1].type;
394       (*delta_dest)[c1].value = htonl (GNUNET_ATS_VALUE_UNDEFINED);
395     }
396     (*delta_count) = update_count;
397     return GNUNET_YES;
398   }
399
400   for (c1 = 0; c1 < update_count; c1++)
401   {
402     /* Update existing performance information */
403     found = GNUNET_NO;
404     for (c2 = 0; c2 < dest->atsi_count; c2++)
405     {
406       if (update[c1].type == dest->atsi[c2].type)
407       {
408         if (update[c1].value != dest->atsi[c2].value)
409         {
410           /* Save previous value in delta */
411           delta_atsi[delta_atsi_count] = dest->atsi[c2];
412           delta_atsi_count++;
413           /* Set new value */
414           dest->atsi[c2].value = update[c1].value;
415           change = GNUNET_YES;
416         }
417         found = GNUNET_YES;
418         break;
419       }
420     }
421     if (GNUNET_NO == found)
422     {
423       add_atsi[add_atsi_count] = update[c1];
424       add_atsi_count++;
425       delta_atsi[delta_atsi_count].type = update[c1].type;
426       delta_atsi[delta_atsi_count].value = htonl (GNUNET_ATS_VALUE_UNDEFINED);
427       delta_atsi_count++;
428     }
429   }
430
431   if (add_atsi_count > 0)
432   {
433     /* Extend ats performance information */
434
435     tmp_atsi = GNUNET_malloc ((dest->atsi_count + add_atsi_count) *
436         (sizeof (struct GNUNET_ATS_Information)));
437     memcpy (tmp_atsi, dest->atsi,
438         dest->atsi_count * sizeof(struct GNUNET_ATS_Information));
439     memcpy (&tmp_atsi[dest->atsi_count], add_atsi,
440         add_atsi_count * sizeof(struct GNUNET_ATS_Information));
441     GNUNET_free(dest->atsi);
442     dest->atsi = tmp_atsi;
443     dest->atsi_count = dest->atsi_count + add_atsi_count;
444     change = GNUNET_YES;
445   }
446
447   if (delta_atsi_count > 0)
448   {
449     /* Copy delta */
450     (*delta_dest) =
451         GNUNET_malloc (delta_atsi_count * sizeof (struct GNUNET_ATS_Information));
452     memcpy ((*delta_dest), delta_atsi,
453         delta_atsi_count * sizeof(struct GNUNET_ATS_Information));
454     (*delta_count) = delta_atsi_count;
455   }
456
457   return change;
458 }
459
460 /**
461  * Free the given address
462  *
463  * @param addr address to destroy
464  */
465 static void
466 free_address (struct ATS_Address *addr)
467 {
468   GNUNET_free(addr->plugin);
469   GNUNET_free_non_null(addr->atsi);
470   GNUNET_free(addr);
471 }
472
473 /**
474  * Create a ATS_address with the given information
475  *
476  * @param peer peer
477  * @param plugin_name plugin
478  * @param plugin_addr address
479  * @param plugin_addr_len address length
480  * @param session_id session
481  * @return the ATS_Address
482  */
483 static struct ATS_Address *
484 create_address (const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
485     const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id)
486 {
487   struct ATS_Address *aa = NULL;
488   int c1;
489   int c2;
490
491   aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
492   aa->peer = *peer;
493   aa->addr_len = plugin_addr_len;
494   aa->addr = &aa[1];
495   memcpy (&aa[1], plugin_addr, plugin_addr_len);
496   aa->plugin = GNUNET_strdup (plugin_name);
497   aa->session_id = session_id;
498   aa->active = GNUNET_NO;
499   aa->used = GNUNET_NO;
500   aa->solver_information = NULL;
501   aa->atsi = NULL;
502   aa->atsi_count = 0;
503   aa->assigned_bw_in = GNUNET_BANDWIDTH_value_init (0);
504   aa->assigned_bw_out = GNUNET_BANDWIDTH_value_init (0);
505
506   for (c1 = 0; c1 < GNUNET_ATS_QualityPropertiesCount; c1++)
507   {
508     aa->atsin[c1].avg_queue_index = 0;
509     for (c2 = 0; c2 < GAS_normalization_queue_length; c2++)
510       aa->atsin[c1].atsi_abs[c2] = GNUNET_ATS_VALUE_UNDEFINED;
511   }
512
513   return aa;
514 }
515
516 struct CompareAddressContext
517 {
518   const struct ATS_Address *search;
519
520   /* exact_address != NULL if address and session is equal */
521   struct ATS_Address *exact_address;
522   /* exact_address != NULL if address and session is 0 */
523   struct ATS_Address *base_address;
524 };
525
526
527 /**
528  * Comapre addresses
529  *
530  * @param cls a CompareAddressContext containin the source address
531  * @param key peer id
532  * @param value the address to compare with
533  * @return #GNUNET_YES to continue, #GNUNET_NO if address is founce
534  */
535 static int
536 compare_address_it (void *cls,
537                     const struct GNUNET_PeerIdentity *key,
538                     void *value)
539 {
540   struct CompareAddressContext *cac = cls;
541   struct ATS_Address *aa = value;
542
543   /* Find an matching exact address:
544    *
545    * Compare by:
546    * aa->addr_len == cac->search->addr_len
547    * aa->plugin == cac->search->plugin
548    * aa->addr == cac->search->addr
549    * aa->session == cac->search->session
550    *
551    * return as exact address
552    */
553   if ((aa->addr_len == cac->search->addr_len)
554       && (0 == strcmp (aa->plugin, cac->search->plugin)))
555   {
556     if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len))
557         && (aa->session_id == cac->search->session_id))
558       cac->exact_address = aa;
559   }
560
561   /* Find an matching base address:
562    *
563    * Properties:
564    *
565    * aa->session_id == 0
566    *
567    * Compare by:
568    * aa->addr_len == cac->search->addr_len
569    * aa->plugin == cac->search->plugin
570    * aa->addr == cac->search->addr
571    *
572    * return as base address
573    */
574   if ((aa->addr_len == cac->search->addr_len)
575       && (0 == strcmp (aa->plugin, cac->search->plugin)))
576   {
577     if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len))
578         && (aa->session_id == 0))
579       cac->base_address = aa;
580   }
581
582   /* Find an matching exact address based on session:
583    *
584    * Properties:
585    *
586    * cac->search->addr_len == 0
587    *
588    * Compare by:
589    * aa->plugin == cac->search->plugin
590    * aa->session_id == cac->search->session_id
591    *
592    * return as exact address
593    */
594   if (0 == cac->search->addr_len)
595   {
596     if ((0 == strcmp (aa->plugin, cac->search->plugin))
597         && (aa->session_id == cac->search->session_id))
598       cac->exact_address = aa;
599   }
600
601   if (cac->exact_address == NULL )
602     return GNUNET_YES; /* Continue iteration to find exact address */
603   else
604     return GNUNET_NO; /* Stop iteration since we have an exact address */
605 }
606
607 /**
608  * Find an existing equivalent address record.
609  * Compares by peer identity and network address OR by session ID
610  * (one of the two must match).
611  *
612  * @param handle the address handle
613  * @param peer peer to lookup addresses for
614  * @param addr existing address record
615  * @return existing address record, NULL for none
616  */
617 struct ATS_Address *
618 find_equivalent_address (struct GAS_Addresses_Handle *handle,
619     const struct GNUNET_PeerIdentity *peer, const struct ATS_Address *addr)
620 {
621   struct CompareAddressContext cac;
622
623   cac.exact_address = NULL;
624   cac.base_address = NULL;
625   cac.search = addr;
626   GNUNET_CONTAINER_multipeermap_get_multiple (handle->addresses,
627                                               peer,
628                                               &compare_address_it, &cac);
629
630   if (cac.exact_address == NULL)
631     return cac.base_address;
632   return cac.exact_address;
633 }
634
635
636 /**
637  * Find the exact address
638  *
639  * @param handle the address handle to use
640  * @param peer peer
641  * @param plugin_name transport plugin name
642  * @param plugin_addr plugin address
643  * @param plugin_addr_len length of the plugin address
644  * @param session_id session id, can be 0
645  * @return an ATS_address or NULL
646  */
647
648 static struct ATS_Address *
649 find_exact_address (struct GAS_Addresses_Handle *handle,
650     const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
651     const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id)
652 {
653   struct ATS_Address *aa;
654   struct ATS_Address *ea;
655
656   aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len,
657       session_id);
658
659   /* Get existing address or address with session == 0 */
660   ea = find_equivalent_address (handle, peer, aa);
661   free_address (aa);
662   if (ea == NULL )
663     return NULL ;
664   else if (ea->session_id != session_id)
665     return NULL ;
666   return ea;
667 }
668
669 /**
670  * Extract an ATS performance info from an address
671  *
672  * @param address the address
673  * @param type the type to extract in HBO
674  * @return the value in HBO or GNUNET_ATS_VALUE_UNDEFINED in HBO if value does not exist
675  */
676 static int
677 get_performance_info (struct ATS_Address *address, uint32_t type)
678 {
679   int c1;
680   GNUNET_assert(NULL != address);
681
682   if ((NULL == address->atsi) || (0 == address->atsi_count))
683     return GNUNET_ATS_VALUE_UNDEFINED;
684
685   for (c1 = 0; c1 < address->atsi_count; c1++)
686   {
687     if (ntohl (address->atsi[c1].type) == type)
688       return ntohl (address->atsi[c1].value);
689   }
690   return GNUNET_ATS_VALUE_UNDEFINED;
691 }
692
693 /**
694  * Add a new address for a peer.
695  *
696  * @param handle the address handle to use
697  * @param peer peer
698  * @param plugin_name transport plugin name
699  * @param plugin_addr plugin address
700  * @param plugin_addr_len length of the plugin address
701  * @param session_id session id, can be 0
702  * @param atsi performance information for this address
703  * @param atsi_count number of performance information contained
704  */
705 void
706 GAS_addresses_add (struct GAS_Addresses_Handle *handle,
707     const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
708     const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id,
709     const struct GNUNET_ATS_Information *atsi, uint32_t atsi_count)
710 {
711   struct ATS_Address *new_address;
712   struct ATS_Address *existing_address;
713   struct GNUNET_ATS_Information *atsi_delta;
714   uint32_t atsi_delta_count;
715   uint32_t addr_net;
716   uint32_t previous_session;
717   int c1;
718
719   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received `%s' for peer `%s'\n",
720       "ADDRESS ADD", GNUNET_i2s (peer));
721
722   if (GNUNET_NO == handle->running)
723     return;
724
725   GNUNET_assert(NULL != handle->addresses);
726
727   new_address = create_address (peer, plugin_name, plugin_addr, plugin_addr_len,
728       session_id);
729   atsi_delta = NULL;
730   disassemble_ats_information (new_address, atsi, atsi_count, &atsi_delta,
731       &atsi_delta_count);
732   GNUNET_free_non_null(atsi_delta);
733   addr_net = get_performance_info (new_address, GNUNET_ATS_NETWORK_TYPE);
734   if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
735     addr_net = GNUNET_ATS_NET_UNSPECIFIED;
736
737   /* Get existing address or address with session == 0 */
738   existing_address = find_equivalent_address (handle, peer, new_address);
739   if (existing_address == NULL )
740   {
741     /* Add a new address */
742     GNUNET_assert(
743         GNUNET_OK == GNUNET_CONTAINER_multipeermap_put (handle->addresses,
744                                                         peer,
745                                                         new_address,
746                                                         GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
747
748     GNUNET_STATISTICS_set (handle->stat, "# addresses",
749         GNUNET_CONTAINER_multipeermap_size (handle->addresses), GNUNET_NO);
750
751     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
752         "Adding new address %p for peer `%s', length %u, session id %u, %s\n",
753         new_address, GNUNET_i2s (peer), plugin_addr_len, session_id,
754         GNUNET_ATS_print_network_type (addr_net));
755
756     /* Tell solver about new address */
757     handle->env.sf.s_add (handle->solver, new_address, addr_net);
758
759     handle->env.sf.s_bulk_start (handle->solver);
760     GAS_normalization_normalize_property (handle->addresses, new_address, atsi,
761         atsi_count);
762     handle->env.sf.s_bulk_stop (handle->solver);
763
764     /* Notify performance clients about new address */
765     GAS_performance_notify_all_clients (&new_address->peer, new_address->plugin,
766         new_address->addr, new_address->addr_len, new_address->session_id,
767         new_address->atsi, new_address->atsi_count,
768         new_address->assigned_bw_out, new_address->assigned_bw_in);
769     return;
770   }
771
772   /* We have an existing address we can use, clean up new */
773   GNUNET_free(new_address->plugin);
774   GNUNET_free_non_null(new_address->atsi);
775   GNUNET_free(new_address);
776   new_address = NULL;
777
778   if (0 != existing_address->session_id)
779   {
780     /* Should not happen */
781     GNUNET_break(0);
782     return;
783   }
784
785   addr_net = get_performance_info (existing_address, GNUNET_ATS_NETWORK_TYPE);
786   if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
787     addr_net = GNUNET_ATS_NET_UNSPECIFIED;
788
789   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
790       "Found existing address for peer `%s' %p with new session %u in network %s\n",
791       GNUNET_i2s (peer), existing_address, session_id,
792       GNUNET_ATS_print_network_type (addr_net));
793   /* We have an address without an session, update this address */
794   atsi_delta = NULL;
795   atsi_delta_count = 0;
796   if (GNUNET_YES
797       == disassemble_ats_information (existing_address, atsi, atsi_count,
798           &atsi_delta, &atsi_delta_count))
799   {
800     /* Notify performance clients about properties */
801     GAS_performance_notify_all_clients (&existing_address->peer,
802         existing_address->plugin, existing_address->addr,
803         existing_address->addr_len, existing_address->session_id,
804         existing_address->atsi, existing_address->atsi_count,
805         existing_address->assigned_bw_out, existing_address->assigned_bw_in);
806
807     for (c1 = 0; c1 < atsi_delta_count; c1++)
808     {
809       if ((GNUNET_ATS_NETWORK_TYPE == ntohl (atsi_delta[c1].type))
810           && (addr_net != ntohl (atsi_delta[c1].value)))
811       {
812         /* Network type changed */
813         GNUNET_log(GNUNET_ERROR_TYPE_INFO,
814             "Address for peer `%s' %p changed from network %s to %s\n",
815             GNUNET_i2s (peer), existing_address,
816             GNUNET_ATS_print_network_type (addr_net),
817             GNUNET_ATS_print_network_type (ntohl (atsi_delta[c1].value)));
818         handle->env.sf.s_address_update_network (handle->solver, existing_address,
819             ntohl (atsi_delta[c1].value),
820             get_performance_info (existing_address, GNUNET_ATS_NETWORK_TYPE));
821         addr_net = get_performance_info (existing_address,
822             GNUNET_ATS_NETWORK_TYPE);
823       }
824     }
825     /* Notify solver about update with atsi information and session */
826     handle->env.sf.s_bulk_start (handle->solver);
827     GAS_normalization_normalize_property (handle->addresses, existing_address,
828         atsi, atsi_count);
829     handle->env.sf.s_bulk_stop (handle->solver);
830   }
831   GNUNET_free_non_null(atsi_delta);
832
833   /* Notify solver about new session */
834   if (existing_address->session_id == session_id)
835     return; /* possible, can both be 0 since address is revalidated */
836
837   previous_session = existing_address->session_id;
838   existing_address->session_id = session_id;
839   handle->env.sf.s_address_update_session (handle->solver, existing_address,
840       previous_session, session_id);
841
842   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
843       "Updated existing address for peer `%s' %p length %u with new session %u in network %s\n",
844       GNUNET_i2s (peer), existing_address, existing_address->addr_len,
845       session_id, GNUNET_ATS_print_network_type (addr_net));
846 }
847
848 /**
849  * Update an address with a session or performance information for a peer.
850  *
851  * If an address was added without a session it will be updated with the
852  * session
853  *
854  * @param handle the address handle to use
855  * @param peer peer
856  * @param plugin_name transport plugin name
857  * @param plugin_addr plugin address
858  * @param plugin_addr_len length of the plugin address
859  * @param session_id session id, can be 0
860  * @param atsi performance information for this address
861  * @param atsi_count number of performance information contained
862  */
863 void
864 GAS_addresses_update (struct GAS_Addresses_Handle *handle,
865     const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
866     const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id,
867     const struct GNUNET_ATS_Information *atsi, uint32_t atsi_count)
868 {
869   struct ATS_Address *aa;
870   struct GNUNET_ATS_Information *atsi_delta;
871   uint32_t atsi_delta_count;
872   uint32_t prev_session;
873   int c1;
874
875   if (GNUNET_NO == handle->running)
876     return;
877
878   GNUNET_assert(NULL != handle->addresses);
879
880   /* Get existing address */
881   aa = find_exact_address (handle, peer, plugin_name, plugin_addr,
882       plugin_addr_len, session_id);
883   if (aa == NULL )
884   {
885     /* GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tried to update unknown address for peer `%s' `%s' session id %u\n", */
886     /*             GNUNET_i2s (peer), plugin_name, session_id); */
887     /* GNUNET_break (0); */
888     return;
889   }
890
891   if (NULL == aa->solver_information)
892   {
893     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
894         "Tried to update unknown address for peer `%s' `%s' session id %u\n",
895         GNUNET_i2s (peer), plugin_name, session_id);
896     return;
897   }
898
899   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received `%s' for peer `%s' address \n",
900       "ADDRESS UPDATE", GNUNET_i2s (peer), aa);
901
902   /* Update address */
903   if (session_id != aa->session_id)
904   {
905     /* Session changed */
906     prev_session = aa->session_id;
907     aa->session_id = session_id;
908     handle->env.sf.s_address_update_session (handle->solver, aa, prev_session,
909         aa->session_id);
910   }
911
912   atsi_delta = NULL;
913   atsi_delta_count = 0;
914   if (GNUNET_YES
915       == disassemble_ats_information (aa, atsi, atsi_count, &atsi_delta,
916           &atsi_delta_count))
917   {
918     /* ATS properties changed */
919     for (c1 = 0; c1 < atsi_delta_count; c1++)
920     {
921       if (GNUNET_ATS_NETWORK_TYPE == ntohl (atsi_delta[c1].type))
922       {
923         /* Network type changed */
924         handle->env.sf.s_address_update_network (handle->solver, aa,
925             ntohl (atsi_delta[c1].value),
926             get_performance_info (aa, GNUNET_ATS_NETWORK_TYPE));
927       }
928     }
929
930     /* Notify performance clients about updated address */
931     GAS_performance_notify_all_clients (&aa->peer, aa->plugin, aa->addr,
932         aa->addr_len, aa->session_id, aa->atsi, aa->atsi_count,
933         aa->assigned_bw_out, aa->assigned_bw_in);
934
935     handle->env.sf.s_bulk_start (handle->solver);
936     GAS_normalization_normalize_property (handle->addresses, aa, atsi,
937         atsi_count);
938     handle->env.sf.s_bulk_stop (handle->solver);
939   }
940   GNUNET_free_non_null(atsi_delta);
941 }
942
943 struct DestroyContext
944 {
945   struct ATS_Address *aa;
946
947   struct GAS_Addresses_Handle *handle;
948
949   /**
950    * GNUNET_NO  : full address
951    * GNUNET_YES : just session
952    */
953   int result;
954 };
955
956 /**
957  * Delete an address
958  *
959  * If session != 0, just the session is deleted, the address itself still exists
960  * If session == 0, remove full address
961  * If session == 0 and addrlen == 0, destroy inbound address
962  *
963  * @param cls unused
964  * @param key unused
965  * @param value the 'struct ATS_Address'
966  * @return GNUNET_OK (continue to iterate)
967  */
968 static int
969 destroy_by_session_id (void *cls,
970                        const struct GNUNET_PeerIdentity *key,
971                        void *value)
972 {
973   struct DestroyContext *dc = cls;
974   struct GAS_Addresses_Handle *handle = dc->handle;
975   const struct ATS_Address *des = dc->aa;
976   struct ATS_Address *aa = value;
977
978   GNUNET_assert(
979       0 == memcmp (&aa->peer, &des->peer, sizeof(struct GNUNET_PeerIdentity)));
980
981   if (des->session_id == 0)
982   {
983     /* Session == 0, remove full address  */
984     if ((0 == strcmp (des->plugin, aa->plugin))
985         && (aa->addr_len == des->addr_len)
986         && (0 == memcmp (des->addr, aa->addr, aa->addr_len)))
987     {
988
989       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
990           "Deleting full address for peer `%s' session %u %p\n",
991           GNUNET_i2s (&aa->peer), aa->session_id, aa);
992
993       /* Notify solver about deletion */
994       GNUNET_assert(
995           GNUNET_YES == GNUNET_CONTAINER_multipeermap_remove (handle->addresses,
996                                                               &aa->peer,
997                                                               aa));
998       handle->env.sf.s_del (handle->solver, aa, GNUNET_NO);
999       free_address (aa);
1000       dc->result = GNUNET_NO;
1001       return GNUNET_OK; /* Continue iteration */
1002     }
1003   }
1004   else
1005   {
1006     /* Session != 0, just remove session */
1007     if (aa->session_id != des->session_id)
1008       return GNUNET_OK; /* irrelevant */
1009
1010     if ((aa->session_id != 0) && (0 != strcmp (des->plugin, aa->plugin)))
1011     {
1012       GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1013           "Different plugins during removal: `%s' vs `%s' \n", des->plugin,
1014           aa->plugin);
1015       GNUNET_break(0);
1016       return GNUNET_OK;
1017     }
1018
1019     if (aa->addr_len == 0)
1020     {
1021       /* Inbound connection died, delete full address */
1022       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1023           "Deleting inbound address for peer `%s': `%s' session %u\n",
1024           GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
1025
1026       /* Notify solver about deletion */
1027       GNUNET_assert(
1028           GNUNET_YES == GNUNET_CONTAINER_multipeermap_remove (handle->addresses,
1029                                                               &aa->peer, aa));
1030       handle->env.sf.s_del (handle->solver, aa, GNUNET_NO);
1031       free_address (aa);
1032       dc->result = GNUNET_NO;
1033       return GNUNET_OK; /* Continue iteration */
1034     }
1035     else
1036     {
1037       /* Session died */
1038       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1039           "Deleting session for peer `%s': `%s' %u\n", GNUNET_i2s (&aa->peer),
1040           aa->plugin, aa->session_id);
1041       /* Notify solver to delete session */
1042       handle->env.sf.s_del (handle->solver, aa, GNUNET_YES);
1043       aa->session_id = 0;
1044       aa->active = GNUNET_NO;
1045       return GNUNET_OK;
1046     }
1047   }
1048   return GNUNET_OK;
1049 }
1050
1051
1052 /**
1053  * Remove an address or just a session for a peer.
1054  *
1055  * @param handle the address handle to use
1056  * @param peer peer
1057  * @param plugin_name transport plugin name
1058  * @param plugin_addr plugin address
1059  * @param plugin_addr_len length of the plugin address
1060  * @param session_id session id, can be 0
1061  */
1062 void
1063 GAS_addresses_destroy (struct GAS_Addresses_Handle *handle,
1064     const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
1065     const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id)
1066 {
1067   struct ATS_Address *ea;
1068   struct DestroyContext dc;
1069   if (GNUNET_NO == handle->running)
1070     return;
1071
1072   /* Get existing address */
1073   ea = find_exact_address (handle, peer, plugin_name, plugin_addr,
1074       plugin_addr_len, session_id);
1075   if (ea == NULL )
1076   {
1077     GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1078         "Tried to destroy unknown address for peer `%s' `%s' session id %u\n",
1079         GNUNET_i2s (peer), plugin_name, session_id);
1080     return;
1081   }
1082
1083   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1084       "Received `%s' for peer `%s' address %p session %u\n", "ADDRESS DESTROY",
1085       GNUNET_i2s (peer), ea, session_id);
1086
1087   GNUNET_break(0 < strlen (plugin_name));
1088   dc.handle = handle;
1089   dc.aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len,
1090       session_id);
1091
1092   GNUNET_CONTAINER_multipeermap_get_multiple (handle->addresses,
1093                                               peer,
1094                                               &destroy_by_session_id, &dc);
1095   GNUNET_STATISTICS_set (handle->stat, "# addresses",
1096       GNUNET_CONTAINER_multipeermap_size (handle->addresses), GNUNET_NO);
1097   free_address (dc.aa);
1098 }
1099
1100 /**
1101  * Notification about active use of an address.
1102  * in_use == GNUNET_YES:
1103  *      This address is used to maintain an active connection with a peer.
1104  * in_use == GNUNET_NO:
1105  *      This address is no longer used to maintain an active connection with a peer.
1106  *
1107  * Note: can only be called with in_use == GNUNET_NO if called with GNUNET_YES
1108  * before
1109  *
1110  * @param handle the address handle to use
1111  * @param peer peer
1112  * @param plugin_name transport plugin name
1113  * @param plugin_addr plugin address
1114  * @param plugin_addr_len length of the plugin address
1115  * @param session_id session id, can be 0
1116  * @param in_use GNUNET_YES if GNUNET_NO
1117  * @return GNUNET_SYSERR on failure (address unknown ...)
1118  */
1119 int
1120 GAS_addresses_in_use (struct GAS_Addresses_Handle *handle,
1121     const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
1122     const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id,
1123     int in_use)
1124 {
1125   struct ATS_Address *ea;
1126   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received `%s' for peer `%s'\n",
1127       "ADDRESS IN USE", GNUNET_i2s (peer));
1128
1129   if (GNUNET_NO == handle->running)
1130     return GNUNET_SYSERR;
1131
1132   ea = find_exact_address (handle, peer, plugin_name, plugin_addr,
1133       plugin_addr_len, session_id);
1134   if (NULL == ea)
1135   {
1136     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1137         "Trying to set unknown address `%s' `%s' `%u' to %s \n",
1138         GNUNET_i2s (peer), plugin_name, session_id,
1139         (GNUNET_NO == in_use) ? "NO" : "YES");
1140     GNUNET_break(0);
1141     return GNUNET_SYSERR;
1142   }
1143   if (ea->used == in_use)
1144   {
1145     GNUNET_break(0);
1146     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1147         "Address in use called multiple times for peer `%s': %s -> %s \n",
1148         GNUNET_i2s (peer), (GNUNET_NO == ea->used) ? "NO" : "YES",
1149         (GNUNET_NO == in_use) ? "NO" : "YES");
1150     return GNUNET_SYSERR;
1151   }
1152
1153   /* Tell solver about update */
1154   ea->used = in_use;
1155   handle->env.sf.s_address_update_inuse (handle->solver, ea, ea->used);
1156   return GNUNET_OK;
1157 }
1158
1159 /**
1160  * Cancel address suggestions for a peer
1161  *
1162  * @param handle the address handle
1163  * @param peer the peer id
1164  */
1165 void
1166 GAS_addresses_request_address_cancel (struct GAS_Addresses_Handle *handle,
1167     const struct GNUNET_PeerIdentity *peer)
1168 {
1169   struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
1170
1171   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received request: `%s' for peer %s\n",
1172       "request_address_cancel", GNUNET_i2s (peer));
1173
1174   while (NULL != cur)
1175   {
1176     if (0 == memcmp (peer, &cur->id, sizeof(cur->id)))
1177       break; /* found */
1178     cur = cur->next;
1179   }
1180
1181   if (NULL == cur)
1182   {
1183     GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1184         "No address requests pending for peer `%s', cannot remove!\n",
1185         GNUNET_i2s (peer));
1186     return;
1187   }
1188   handle->env.sf.s_get_stop (handle->solver, peer);
1189   GAS_addresses_handle_backoff_reset (handle, peer);
1190   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Removed request pending for peer `%s\n",
1191       GNUNET_i2s (peer));
1192   GNUNET_CONTAINER_DLL_remove(handle->r_head, handle->r_tail, cur);
1193   GNUNET_free(cur);
1194 }
1195
1196
1197 /**
1198  * Request address suggestions for a peer
1199  *
1200  * @param handle the address handle
1201  * @param peer the peer id
1202  */
1203 void
1204 GAS_addresses_request_address (struct GAS_Addresses_Handle *handle,
1205     const struct GNUNET_PeerIdentity *peer)
1206 {
1207   struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
1208   struct ATS_Address *aa;
1209
1210   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received `%s' for peer `%s'\n",
1211       "REQUEST ADDRESS", GNUNET_i2s (peer));
1212
1213   if (GNUNET_NO == handle->running)
1214     return;
1215   while (NULL != cur)
1216   {
1217     if (0 == memcmp (peer, &cur->id, sizeof(cur->id)))
1218       break; /* already suggesting */
1219     cur = cur->next;
1220   }
1221   if (NULL == cur)
1222   {
1223     cur = GNUNET_malloc (sizeof (struct GAS_Addresses_Suggestion_Requests));
1224     cur->id = (*peer);
1225     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1226         "Adding new address suggestion request for `%s'\n",
1227          GNUNET_i2s (peer));
1228     GNUNET_CONTAINER_DLL_insert(handle->r_head, handle->r_tail, cur);
1229   }
1230
1231   /*
1232    * Debuging information about addresses
1233    *
1234    * GNUNET_CONTAINER_multihashmap_get_multiple(handle->addresses,
1235    *  &peer->hashPubKey, &addrinfo_it, (void *) peer);
1236    */
1237
1238   /* Get prefered address from solver */
1239   aa = (struct ATS_Address *) handle->env.sf.s_get (handle->solver, peer);
1240   if (NULL == aa)
1241   {
1242     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Cannot suggest address for peer `%s'\n",
1243         GNUNET_i2s (peer));
1244     return;
1245   }
1246
1247   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Suggesting address %p for peer `%s'\n",
1248       aa, GNUNET_i2s (peer));
1249
1250   GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
1251       aa->addr_len, aa->session_id, aa->atsi, aa->atsi_count,
1252       aa->assigned_bw_out, aa->assigned_bw_in);
1253
1254   aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval,
1255       ATS_BLOCKING_DELTA);
1256   aa->blocked_until = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1257       aa->block_interval);
1258
1259   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1260       "Address %p ready for suggestion, block interval now %llu \n", aa,
1261       aa->block_interval);
1262 }
1263
1264 /**
1265  * Iterator to reset address blocking
1266  *
1267  * @param cls not used
1268  * @param key the peer
1269  * @param value the address to reset
1270  * @return #GNUNET_OK to continue
1271  */
1272 static int
1273 reset_address_it (void *cls,
1274                   const struct GNUNET_PeerIdentity *key,
1275                   void *value)
1276 {
1277   struct ATS_Address *aa = value;
1278
1279   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1280              "Resetting interval for peer `%s' address %p from %llu to 0\n",
1281              GNUNET_i2s (&aa->peer),
1282              aa,
1283              aa->block_interval);
1284   aa->blocked_until = GNUNET_TIME_UNIT_ZERO_ABS;
1285   aa->block_interval = GNUNET_TIME_UNIT_ZERO;
1286   return GNUNET_OK;
1287 }
1288
1289
1290 /**
1291  * Reset suggestion backoff for a peer
1292  *
1293  * Suggesting addresses is blocked for ATS_BLOCKING_DELTA. Blocking can be
1294  * reset using this function
1295  *
1296  * @param handle the address handle
1297  * @param peer the peer id
1298  */
1299 void
1300 GAS_addresses_handle_backoff_reset (struct GAS_Addresses_Handle *handle,
1301     const struct GNUNET_PeerIdentity *peer)
1302 {
1303   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received `%s' for peer `%s'\n",
1304       "RESET BACKOFF", GNUNET_i2s (peer));
1305
1306   GNUNET_break(
1307       GNUNET_SYSERR != GNUNET_CONTAINER_multipeermap_get_multiple (handle->addresses,
1308                                                                    peer,
1309                                                                    &reset_address_it, NULL));
1310 }
1311
1312
1313 /**
1314  * The preference changed for a peer
1315  *
1316  * @param cls the address handle
1317  * @param peer the peer
1318  * @param kind the ATS kind
1319  * @param pref_rel the new relative preference value
1320  */
1321 static void
1322 normalized_preference_changed_cb (void *cls,
1323     const struct GNUNET_PeerIdentity *peer, enum GNUNET_ATS_PreferenceKind kind,
1324     double pref_rel)
1325 {
1326   GNUNET_assert(NULL != cls);
1327   struct GAS_Addresses_Handle *handle = cls;
1328
1329   /* Tell solver about update */
1330   handle->env.sf.s_pref (handle->solver, peer, kind, pref_rel);
1331 }
1332
1333 /**
1334  * The relative value for a property changed
1335  *
1336  * @param cls the address handle
1337  * @param address the peer
1338  * @param type the ATS type
1339  * @param prop_rel the new relative preference value
1340  */
1341 static void
1342 normalized_property_changed_cb (void *cls, struct ATS_Address *address,
1343     uint32_t type, double prop_rel)
1344 {
1345   struct GAS_Addresses_Handle *ah = (struct GAS_Addresses_Handle *) cls;
1346   GNUNET_assert(NULL != ah);
1347
1348   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1349       "Normalized property %s for peer `%s' changed to %.3f \n",
1350       GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
1351       prop_rel);
1352
1353   ah->env.sf.s_address_update_property (ah->solver, address, type, 0, prop_rel);
1354 }
1355
1356 /**
1357  * Function allowing the solver to obtain normalized preference
1358  * values from solver
1359  *
1360  * @param cls unused
1361  * @param id the peer to return the normalized properties for
1362  * @return array of double values with |GNUNET_ATS_PreferenceCount| elements
1363  */
1364 const double *
1365 get_preferences_cb (void *cls, const struct GNUNET_PeerIdentity *id)
1366 {
1367   return GAS_normalization_get_preferences (id);
1368 }
1369
1370 /**
1371  * Function allowing the solver to obtain normalized property
1372  * values for an address from solver
1373  *
1374  * @param cls unused
1375  * @param address the address
1376  * @return array of double values with |GNUNET_ATS_QualityPropertiesCount| elements
1377  */
1378 const double *
1379 get_property_cb (void *cls, const struct ATS_Address *address)
1380 {
1381   return GAS_normalization_get_properties ((struct ATS_Address *) address);
1382 }
1383
1384 /**
1385  * Change the preference for a peer
1386  *
1387  * @param handle the address handle
1388  * @param client the client sending this request
1389  * @param peer the peer id
1390  * @param kind the preference kind to change
1391  * @param score_abs the new preference score
1392  */
1393 void
1394 GAS_addresses_change_preference (struct GAS_Addresses_Handle *handle,
1395     void *client, const struct GNUNET_PeerIdentity *peer,
1396     enum GNUNET_ATS_PreferenceKind kind, float score_abs)
1397 {
1398   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1399       "Received `%s' for peer `%s' for client %p\n", "CHANGE PREFERENCE",
1400       GNUNET_i2s (peer), client);
1401
1402   if (GNUNET_NO == handle->running)
1403     return;
1404
1405   if (GNUNET_NO ==
1406       GNUNET_CONTAINER_multipeermap_contains (handle->addresses,
1407                                               peer))
1408   {
1409     GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1410         "Received `%s' for unknown peer `%s' from client %p\n",
1411         "CHANGE PREFERENCE", GNUNET_i2s (peer), client);
1412     return;
1413   }
1414
1415   handle->env.sf.s_bulk_start (handle->solver);
1416   /* Tell normalization about change, normalization will call callback if preference changed */
1417   GAS_normalization_normalize_preference (client, peer, kind, score_abs);
1418   handle->env.sf.s_bulk_stop (handle->solver);
1419 }
1420
1421 /**
1422  * Change the preference for a peer
1423  *
1424  * @param handle the address handle
1425  * @param application the client sending this request
1426  * @param peer the peer id
1427  * @param scope the time interval for this feedback: [now - scope .. now]
1428  * @param kind the preference kind to change
1429  * @param score_abs the new preference score
1430  */
1431 void
1432 GAS_addresses_preference_feedback (struct GAS_Addresses_Handle *handle,
1433     void *application, const struct GNUNET_PeerIdentity *peer,
1434     const struct GNUNET_TIME_Relative scope,
1435     enum GNUNET_ATS_PreferenceKind kind, float score_abs)
1436 {
1437   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1438       "Received `%s' for peer `%s' for client %p\n", "PREFERENCE FEEDBACK",
1439       GNUNET_i2s (peer), application);
1440
1441   if (GNUNET_NO == handle->running)
1442     return;
1443
1444   if (GNUNET_NO ==
1445       GNUNET_CONTAINER_multipeermap_contains (handle->addresses,
1446                                               peer))
1447   {
1448     GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1449         "Received `%s' for unknown peer `%s' from client %p\n",
1450         "PREFERENCE FEEDBACK", GNUNET_i2s (peer), application);
1451     return;
1452   }
1453
1454   handle->env.sf.s_feedback (handle->solver, application, peer, scope, kind,
1455       score_abs);
1456 }
1457
1458 /**
1459  * Load quotas for networks from configuration
1460  *
1461  * @param cfg configuration handle
1462  * @param out_dest where to write outbound quotas
1463  * @param in_dest where to write inbound quotas
1464  * @param dest_length length of inbound and outbound arrays
1465  * @return number of networks loaded
1466  */
1467 static unsigned int
1468 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg,
1469     unsigned long long *out_dest, unsigned long long *in_dest, int dest_length)
1470 {
1471   char *network_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
1472   char * entry_in = NULL;
1473   char * entry_out = NULL;
1474   char * quota_out_str;
1475   char * quota_in_str;
1476   int c;
1477   int res;
1478
1479   for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
1480   {
1481     in_dest[c] = 0;
1482     out_dest[c] = 0;
1483     GNUNET_asprintf (&entry_out, "%s_QUOTA_OUT", network_str[c]);
1484     GNUNET_asprintf (&entry_in, "%s_QUOTA_IN", network_str[c]);
1485
1486     /* quota out */
1487     if (GNUNET_OK
1488         == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", entry_out,
1489             &quota_out_str))
1490     {
1491       res = GNUNET_NO;
1492       if (0 == strcmp (quota_out_str, GNUNET_ATS_MaxBandwidthString))
1493       {
1494         out_dest[c] = GNUNET_ATS_MaxBandwidth;
1495         res = GNUNET_YES;
1496       }
1497       if ((GNUNET_NO == res)
1498           && (GNUNET_OK
1499               == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str,
1500                   &out_dest[c])))
1501         res = GNUNET_YES;
1502       if ((GNUNET_NO == res)
1503           && (GNUNET_OK
1504               == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_out,
1505                   &out_dest[c])))
1506         res = GNUNET_YES;
1507
1508       if (GNUNET_NO == res)
1509       {
1510         GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1511             _("Could not load quota for network `%s':  `%s', assigning default bandwidth %llu\n"),
1512             network_str[c], quota_out_str, GNUNET_ATS_DefaultBandwidth);
1513         out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1514       }
1515       else
1516       {
1517         GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1518             _("Outbound quota configure for network `%s' is %llu\n"),
1519             network_str[c], out_dest[c]);
1520       }
1521       GNUNET_free(quota_out_str);
1522     }
1523     else
1524     {
1525       GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1526           _("No outbound quota configured for network `%s', assigning default bandwidth %llu\n"),
1527           network_str[c], GNUNET_ATS_DefaultBandwidth);
1528       out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1529     }
1530
1531     /* quota in */
1532     if (GNUNET_OK
1533         == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", entry_in,
1534             &quota_in_str))
1535     {
1536       res = GNUNET_NO;
1537       if (0 == strcmp (quota_in_str, GNUNET_ATS_MaxBandwidthString))
1538       {
1539         in_dest[c] = GNUNET_ATS_MaxBandwidth;
1540         res = GNUNET_YES;
1541       }
1542       if ((GNUNET_NO == res)
1543           && (GNUNET_OK
1544               == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
1545         res = GNUNET_YES;
1546       if ((GNUNET_NO == res)
1547           && (GNUNET_OK
1548               == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_in,
1549                   &in_dest[c])))
1550         res = GNUNET_YES;
1551
1552       if (GNUNET_NO == res)
1553       {
1554         GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1555             _("Could not load quota for network `%s':  `%s', assigning default bandwidth %llu\n"),
1556             network_str[c], quota_in_str, GNUNET_ATS_DefaultBandwidth);
1557         in_dest[c] = GNUNET_ATS_DefaultBandwidth;
1558       }
1559       else
1560       {
1561         GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1562             _("Inbound quota configured for network `%s' is %llu\n"),
1563             network_str[c], in_dest[c]);
1564       }
1565       GNUNET_free(quota_in_str);
1566     }
1567     else
1568     {
1569       GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1570           _("No outbound quota configure for network `%s', assigning default bandwidth %llu\n"),
1571           network_str[c], GNUNET_ATS_DefaultBandwidth);
1572       out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1573     }
1574     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1575         "Loaded quota for network `%s' (in/out): %llu %llu\n", network_str[c],
1576         in_dest[c], out_dest[c]);
1577     GNUNET_free(entry_out);
1578     GNUNET_free(entry_in);
1579   }
1580   return GNUNET_ATS_NetworkTypeCount;
1581 }
1582
1583 /**
1584  * Callback for solver to notify about assignment changes
1585  *
1586  * @param cls the GAS_Addresses_Handle
1587  * @param address the address with changes
1588  */
1589 static void
1590 bandwidth_changed_cb (void *cls, struct ATS_Address *address)
1591 {
1592   struct GAS_Addresses_Handle *handle = cls;
1593   struct GAS_Addresses_Suggestion_Requests *cur;
1594
1595   GNUNET_assert(handle != NULL);
1596   GNUNET_assert(address != NULL);
1597
1598   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1599       "Bandwidth assignment changed for peer %s \n",
1600       GNUNET_i2s (&address->peer));
1601
1602   /* Notify performance clients about changes to address */
1603   GAS_performance_notify_all_clients (&address->peer, address->plugin,
1604       address->addr, address->addr_len, address->session_id, address->atsi,
1605       address->atsi_count, address->assigned_bw_out, address->assigned_bw_in);
1606   cur = handle->r_head;
1607   while (NULL != cur)
1608   {
1609     if (0 == memcmp (&address->peer, &cur->id, sizeof(cur->id)))
1610       break; /* we have an address request pending*/
1611     cur = cur->next;
1612   }
1613   if (NULL == cur)
1614   {
1615     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Nobody is interested in peer `%s' :(\n",
1616         GNUNET_i2s (&address->peer));
1617     return;
1618   }
1619
1620   if ((0 == ntohl (address->assigned_bw_in.value__))
1621       && (0 == ntohl (address->assigned_bw_out.value__)))
1622   {
1623     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1624         "Telling transport to disconnect peer `%s'\n",
1625         GNUNET_i2s (&address->peer));
1626   }
1627   else
1628   {
1629     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1630         "Sending bandwidth update for peer `%s': %llu %llu\n",
1631         GNUNET_i2s (&address->peer), address->assigned_bw_out,
1632         address->assigned_bw_out);
1633   }
1634
1635   /* *Notify scheduling clients about suggestion */
1636   GAS_scheduling_transmit_address_suggestion (&address->peer, address->plugin,
1637       address->addr, address->addr_len, address->session_id, address->atsi,
1638       address->atsi_count, address->assigned_bw_out, address->assigned_bw_in);
1639 }
1640
1641 /**
1642  * Initialize address subsystem. The addresses subsystem manages the addresses
1643  * known and current performance information. It has a solver component
1644  * responsible for the resource allocation. It tells the solver about changes
1645  * and receives updates when the solver changes the resource allocation.
1646  *
1647  * @param cfg configuration to use
1648  * @param stats the statistics handle to use
1649  * @return an address handle
1650  */
1651 struct GAS_Addresses_Handle *
1652 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1653     const struct GNUNET_STATISTICS_Handle *stats)
1654 {
1655   struct GAS_Addresses_Handle *ah;
1656   unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
1657   unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
1658   char *mode_str;
1659   char *plugin_short;
1660   int c;
1661
1662   ah = GNUNET_malloc (sizeof (struct GAS_Addresses_Handle));
1663   ah->running = GNUNET_NO;
1664
1665   ah->stat = (struct GNUNET_STATISTICS_Handle *) stats;
1666   /* Initialize the addresses database */
1667   ah->addresses = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1668   GNUNET_assert(NULL != ah->addresses);
1669
1670   /* Figure out configured solution method */
1671   if (GNUNET_SYSERR
1672       == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "MODE", &mode_str))
1673   {
1674     GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1675         "No resource assignment method configured, using proportional approach\n");
1676     ah->ats_mode = MODE_PROPORTIONAL;
1677   }
1678   else
1679   {
1680     for (c = 0; c < strlen (mode_str); c++)
1681       mode_str[c] = toupper (mode_str[c]);
1682     if (0 == strcmp (mode_str, "PROPORTIONAL"))
1683     {
1684       ah->ats_mode = MODE_PROPORTIONAL;
1685       plugin_short = "proportional";
1686     }
1687     else if (0 == strcmp (mode_str, "MLP"))
1688     {
1689       ah->ats_mode = MODE_MLP;
1690       plugin_short = "mlp";
1691 #if !HAVE_LIBGLPK
1692       GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1693           "Assignment method `%s' configured, but GLPK is not available, please install \n",
1694           mode_str);
1695       ah->ats_mode = MODE_PROPORTIONAL;
1696       plugin_short = "proportional";
1697 #endif
1698     }
1699     else if (0 == strcmp (mode_str, "RIL"))
1700     {
1701       ah->ats_mode = MODE_RIL;
1702       plugin_short = "ril";
1703     }
1704     else
1705     {
1706       GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1707           "Invalid resource assignment method `%s' configured, using proportional approach\n",
1708           mode_str);
1709       ah->ats_mode = MODE_PROPORTIONAL;
1710       plugin_short = "proportional";
1711     }
1712     GNUNET_free(mode_str);
1713   }
1714
1715   load_quotas (cfg, quotas_in, quotas_out, GNUNET_ATS_NetworkTypeCount);
1716   ah->env.bandwidth_changed_cb = &bandwidth_changed_cb;
1717   ah->env.bw_changed_cb_cls = ah;
1718   ah->env.get_preferences = &get_preferences_cb;
1719   ah->env.get_preference_cls = ah;
1720   ah->env.get_property = &get_property_cb;
1721   ah->env.get_property_cls = ah;
1722   ah->env.cfg = cfg;
1723   ah->env.stats = stats;
1724   ah->env.addresses = ah->addresses;
1725
1726   ah->env.network_count = GNUNET_ATS_NetworkTypeCount;
1727   int networks[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
1728   for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
1729   {
1730     ah->env.networks[c] = networks[c];
1731     ah->env.out_quota[c] = quotas_out[c];
1732     ah->env.in_quota[c] = quotas_in[c];
1733   }
1734
1735   GNUNET_asprintf (&ah->plugin, "libgnunet_plugin_ats_%s", plugin_short);
1736   GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initializing solver `%s '`%s'\n"), plugin_short, ah->plugin);
1737   if  (NULL == (ah->solver = GNUNET_PLUGIN_load (ah->plugin, &ah->env)))
1738   {
1739     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to initialize solver `%s'!\n"), ah->plugin);
1740     return NULL;
1741   }
1742
1743   GNUNET_assert (NULL != ah->env.sf.s_add);
1744   GNUNET_assert (NULL != ah->env.sf.s_address_update_inuse);
1745   GNUNET_assert (NULL != ah->env.sf.s_address_update_property);
1746   GNUNET_assert (NULL != ah->env.sf.s_address_update_session);
1747   GNUNET_assert (NULL != ah->env.sf.s_address_update_network);
1748   GNUNET_assert (NULL != ah->env.sf.s_get);
1749   GNUNET_assert (NULL != ah->env.sf.s_get_stop);
1750   GNUNET_assert (NULL != ah->env.sf.s_pref);
1751   GNUNET_assert (NULL != ah->env.sf.s_feedback);
1752   GNUNET_assert (NULL != ah->env.sf.s_del);
1753   GNUNET_assert (NULL != ah->env.sf.s_bulk_start);
1754   GNUNET_assert (NULL != ah->env.sf.s_bulk_stop);
1755
1756
1757   GAS_normalization_start (&normalized_preference_changed_cb, ah,
1758       &normalized_property_changed_cb, ah);
1759
1760   if (NULL == ah->solver)
1761   {
1762     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to initialize solver!\n"));
1763     GNUNET_free(ah);
1764     return NULL ;
1765   }
1766   /* up and running */
1767   ah->running = GNUNET_YES;
1768
1769   GNUNET_STATISTICS_set (ah->stat, "# addresses",
1770       GNUNET_CONTAINER_multipeermap_size (ah->addresses), GNUNET_NO);
1771
1772   return ah;
1773 }
1774
1775 /**
1776  * Destroy all addresses iterator
1777  *
1778  * @param cls NULL
1779  * @param key peer identity (unused)
1780  * @param value the 'struct ATS_Address' to free
1781  * @return #GNUNET_OK (continue to iterate)
1782  */
1783 static int
1784 destroy_all_address_it (void *cls,
1785                         const struct GNUNET_PeerIdentity *key,
1786                         void *value)
1787 {
1788   struct GAS_Addresses_Handle *handle = cls;
1789   struct ATS_Address *aa = value;
1790
1791   /* Remove */
1792   GNUNET_assert(GNUNET_YES ==
1793                 GNUNET_CONTAINER_multipeermap_remove (handle->addresses, key, value));
1794   /* Notify */
1795   handle->env.sf.s_del (handle->solver, aa, GNUNET_NO);
1796   /* Destroy */
1797   free_address (aa);
1798
1799   return GNUNET_OK;
1800 }
1801
1802
1803 /**
1804  * Remove all addresses
1805  *
1806  * @param handle the address handle to use
1807  */
1808 void
1809 GAS_addresses_destroy_all (struct GAS_Addresses_Handle *handle)
1810 {
1811   if (GNUNET_NO == handle->running)
1812     return;
1813
1814   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Destroying all addresses\n");
1815   handle->env.sf.s_bulk_start (handle->solver);
1816   if (handle->addresses != NULL )
1817     GNUNET_CONTAINER_multipeermap_iterate (handle->addresses,
1818                                            &destroy_all_address_it,
1819                                            handle);
1820   handle->env.sf.s_bulk_start (handle->solver);
1821 }
1822
1823
1824 /**
1825  * Shutdown address subsystem.
1826  *
1827  * @param handle the address handle to shutdown
1828  */
1829 void
1830 GAS_addresses_done (struct GAS_Addresses_Handle *handle)
1831 {
1832   struct GAS_Addresses_Suggestion_Requests *cur;
1833
1834   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Shutting down addresses\n");
1835   GNUNET_assert(NULL != handle);
1836   GAS_addresses_destroy_all (handle);
1837   handle->running = GNUNET_NO;
1838   GNUNET_CONTAINER_multipeermap_destroy (handle->addresses);
1839   handle->addresses = NULL;
1840   while (NULL != (cur = handle->r_head))
1841   {
1842     GNUNET_CONTAINER_DLL_remove(handle->r_head, handle->r_tail, cur);
1843     GNUNET_free(cur);
1844   }
1845
1846   GNUNET_PLUGIN_unload (handle->plugin, handle->solver);
1847   GNUNET_free (handle->plugin);
1848   GNUNET_free(handle);
1849   /* Stop configured solution method */
1850   GAS_normalization_stop ();
1851 }
1852
1853
1854 struct PeerIteratorContext
1855 {
1856   GNUNET_ATS_Peer_Iterator it;
1857   void *it_cls;
1858   struct GNUNET_CONTAINER_MultiPeerMap *peers_returned;
1859 };
1860
1861
1862 /**
1863  * Iterator to iterate over all peers
1864  *
1865  * @param cls a PeerIteratorContext
1866  * @param key the peer id
1867  * @param value the ATS_address
1868  * @return #GNUNET_OK to continue
1869  */
1870 static int
1871 peer_it (void *cls,
1872          const struct GNUNET_PeerIdentity *key,
1873          void *value)
1874 {
1875   struct PeerIteratorContext *ip_ctx = cls;
1876
1877   if (GNUNET_NO ==
1878       GNUNET_CONTAINER_multipeermap_contains (ip_ctx->peers_returned, key))
1879   {
1880     GNUNET_CONTAINER_multipeermap_put (ip_ctx->peers_returned, key, NULL,
1881                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1882     ip_ctx->it (ip_ctx->it_cls, key);
1883   }
1884
1885   return GNUNET_OK;
1886 }
1887
1888 /**
1889  * Return information all peers currently known to ATS
1890  *
1891  * @param handle the address handle to use
1892  * @param p_it the iterator to call for every peer
1893  * @param p_it_cls the closure for the iterator
1894  */
1895 void
1896 GAS_addresses_iterate_peers (struct GAS_Addresses_Handle *handle,
1897     GNUNET_ATS_Peer_Iterator p_it, void *p_it_cls)
1898 {
1899   struct PeerIteratorContext ip_ctx;
1900   unsigned int size;
1901
1902   if (NULL == p_it)
1903     return;
1904   GNUNET_assert(NULL != handle->addresses);
1905
1906   size = GNUNET_CONTAINER_multipeermap_size (handle->addresses);
1907   if (0 != size)
1908   {
1909     ip_ctx.it = p_it;
1910     ip_ctx.it_cls = p_it_cls;
1911     ip_ctx.peers_returned = GNUNET_CONTAINER_multipeermap_create (size,
1912                                                                   GNUNET_NO);
1913     GNUNET_CONTAINER_multipeermap_iterate (handle->addresses,
1914                                            &peer_it,
1915                                            &ip_ctx);
1916     GNUNET_CONTAINER_multipeermap_destroy (ip_ctx.peers_returned);
1917   }
1918   p_it (p_it_cls, NULL );
1919 }
1920
1921 struct PeerInfoIteratorContext
1922 {
1923   GNUNET_ATS_PeerInfo_Iterator it;
1924   void *it_cls;
1925 };
1926
1927
1928 /**
1929  * Iterator to iterate over a peer's addresses
1930  *
1931  * @param cls a `struct PeerInfoIteratorContext`
1932  * @param key the peer id
1933  * @param value the `struct ATS_address`
1934  * @return #GNUNET_OK to continue
1935  */
1936 static int
1937 peerinfo_it (void *cls,
1938              const struct GNUNET_PeerIdentity *key,
1939              void *value)
1940 {
1941   struct PeerInfoIteratorContext *pi_ctx = cls;
1942   struct ATS_Address *addr = value;
1943
1944   if (NULL != pi_ctx->it)
1945   {
1946     pi_ctx->it (pi_ctx->it_cls, &addr->peer, addr->plugin, addr->addr,
1947         addr->addr_len, addr->active, addr->atsi, addr->atsi_count,
1948         addr->assigned_bw_out, addr->assigned_bw_in);
1949   }
1950   return GNUNET_YES;
1951 }
1952
1953
1954 /**
1955  * Return information all peers currently known to ATS
1956  *
1957  * @param handle the address handle to use
1958  * @param peer the respective peer
1959  * @param pi_it the iterator to call for every peer
1960  * @param pi_it_cls the closure for the iterator
1961  */
1962 void
1963 GAS_addresses_get_peer_info (struct GAS_Addresses_Handle *handle,
1964     const struct GNUNET_PeerIdentity *peer, GNUNET_ATS_PeerInfo_Iterator pi_it,
1965     void *pi_it_cls)
1966 {
1967   struct PeerInfoIteratorContext pi_ctx;
1968   struct GNUNET_BANDWIDTH_Value32NBO zero_bw;
1969
1970   GNUNET_assert(NULL != peer);
1971   GNUNET_assert(NULL != handle->addresses);
1972   if (NULL == pi_it)
1973     return; /* does not make sense without callback */
1974
1975   zero_bw = GNUNET_BANDWIDTH_value_init (0);
1976   pi_ctx.it = pi_it;
1977   pi_ctx.it_cls = pi_it_cls;
1978
1979   GNUNET_CONTAINER_multipeermap_get_multiple (handle->addresses,
1980                                               peer,
1981                                               &peerinfo_it, &pi_ctx);
1982
1983   if (NULL != pi_it)
1984     pi_it (pi_it_cls, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, zero_bw,
1985         zero_bw);
1986
1987 }
1988
1989 /* end of gnunet-service-ats_addresses.c */