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