ats_ril: - bug fixed in address_add()
[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_MultiPeerMap *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 /**
579  * Comapre addresses
580  *
581  * @param cls a CompareAddressContext containin the source address
582  * @param key peer id
583  * @param value the address to compare with
584  * @return #GNUNET_YES to continue, #GNUNET_NO if address is founce
585  */
586 static int
587 compare_address_it (void *cls,
588                     const struct GNUNET_PeerIdentity *key,
589                     void *value)
590 {
591   struct CompareAddressContext *cac = cls;
592   struct ATS_Address *aa = value;
593
594   /* Find an matching exact address:
595    *
596    * Compare by:
597    * aa->addr_len == cac->search->addr_len
598    * aa->plugin == cac->search->plugin
599    * aa->addr == cac->search->addr
600    * aa->session == cac->search->session
601    *
602    * return as exact address
603    */
604   if ((aa->addr_len == cac->search->addr_len)
605       && (0 == strcmp (aa->plugin, cac->search->plugin)))
606   {
607     if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len))
608         && (aa->session_id == cac->search->session_id))
609       cac->exact_address = aa;
610   }
611
612   /* Find an matching base address:
613    *
614    * Properties:
615    *
616    * aa->session_id == 0
617    *
618    * Compare by:
619    * aa->addr_len == cac->search->addr_len
620    * aa->plugin == cac->search->plugin
621    * aa->addr == cac->search->addr
622    *
623    * return as base address
624    */
625   if ((aa->addr_len == cac->search->addr_len)
626       && (0 == strcmp (aa->plugin, cac->search->plugin)))
627   {
628     if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len))
629         && (aa->session_id == 0))
630       cac->base_address = aa;
631   }
632
633   /* Find an matching exact address based on session:
634    *
635    * Properties:
636    *
637    * cac->search->addr_len == 0
638    *
639    * Compare by:
640    * aa->plugin == cac->search->plugin
641    * aa->session_id == cac->search->session_id
642    *
643    * return as exact address
644    */
645   if (0 == cac->search->addr_len)
646   {
647     if ((0 == strcmp (aa->plugin, cac->search->plugin))
648         && (aa->session_id == cac->search->session_id))
649       cac->exact_address = aa;
650   }
651
652   if (cac->exact_address == NULL )
653     return GNUNET_YES; /* Continue iteration to find exact address */
654   else
655     return GNUNET_NO; /* Stop iteration since we have an exact address */
656 }
657
658 /**
659  * Find an existing equivalent address record.
660  * Compares by peer identity and network address OR by session ID
661  * (one of the two must match).
662  *
663  * @param handle the address handle
664  * @param peer peer to lookup addresses for
665  * @param addr existing address record
666  * @return existing address record, NULL for none
667  */
668 struct ATS_Address *
669 find_equivalent_address (struct GAS_Addresses_Handle *handle,
670     const struct GNUNET_PeerIdentity *peer, const struct ATS_Address *addr)
671 {
672   struct CompareAddressContext cac;
673
674   cac.exact_address = NULL;
675   cac.base_address = NULL;
676   cac.search = addr;
677   GNUNET_CONTAINER_multipeermap_get_multiple (handle->addresses,
678                                               peer,
679                                               &compare_address_it, &cac);
680
681   if (cac.exact_address == NULL)
682     return cac.base_address;
683   return cac.exact_address;
684 }
685
686
687 /**
688  * Find the exact address
689  *
690  * @param handle the address handle to use
691  * @param peer peer
692  * @param plugin_name transport plugin name
693  * @param plugin_addr plugin address
694  * @param plugin_addr_len length of the plugin address
695  * @param session_id session id, can be 0
696  * @return an ATS_address or NULL
697  */
698
699 static struct ATS_Address *
700 find_exact_address (struct GAS_Addresses_Handle *handle,
701     const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
702     const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id)
703 {
704   struct ATS_Address *aa;
705   struct ATS_Address *ea;
706
707   aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len,
708       session_id);
709
710   /* Get existing address or address with session == 0 */
711   ea = find_equivalent_address (handle, peer, aa);
712   free_address (aa);
713   if (ea == NULL )
714     return NULL ;
715   else if (ea->session_id != session_id)
716     return NULL ;
717   return ea;
718 }
719
720 /**
721  * Extract an ATS performance info from an address
722  *
723  * @param address the address
724  * @param type the type to extract in HBO
725  * @return the value in HBO or GNUNET_ATS_VALUE_UNDEFINED in HBO if value does not exist
726  */
727 static int
728 get_performance_info (struct ATS_Address *address, uint32_t type)
729 {
730   int c1;
731   GNUNET_assert(NULL != address);
732
733   if ((NULL == address->atsi) || (0 == address->atsi_count))
734     return GNUNET_ATS_VALUE_UNDEFINED;
735
736   for (c1 = 0; c1 < address->atsi_count; c1++)
737   {
738     if (ntohl (address->atsi[c1].type) == type)
739       return ntohl (address->atsi[c1].value);
740   }
741   return GNUNET_ATS_VALUE_UNDEFINED;
742 }
743
744 /**
745  * Add a new address for a peer.
746  *
747  * @param handle the address handle to use
748  * @param peer peer
749  * @param plugin_name transport plugin name
750  * @param plugin_addr plugin address
751  * @param plugin_addr_len length of the plugin address
752  * @param session_id session id, can be 0
753  * @param atsi performance information for this address
754  * @param atsi_count number of performance information contained
755  */
756 void
757 GAS_addresses_add (struct GAS_Addresses_Handle *handle,
758     const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
759     const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id,
760     const struct GNUNET_ATS_Information *atsi, uint32_t atsi_count)
761 {
762   struct ATS_Address *new_address;
763   struct ATS_Address *existing_address;
764   struct GNUNET_ATS_Information *atsi_delta;
765   uint32_t atsi_delta_count;
766   uint32_t addr_net;
767   uint32_t previous_session;
768   int c1;
769
770   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received `%s' for peer `%s'\n",
771       "ADDRESS ADD", GNUNET_i2s (peer));
772
773   if (GNUNET_NO == handle->running)
774     return;
775
776   GNUNET_assert(NULL != handle->addresses);
777
778   new_address = create_address (peer, plugin_name, plugin_addr, plugin_addr_len,
779       session_id);
780   atsi_delta = NULL;
781   disassemble_ats_information (new_address, atsi, atsi_count, &atsi_delta,
782       &atsi_delta_count);
783   GNUNET_free_non_null(atsi_delta);
784   addr_net = get_performance_info (new_address, GNUNET_ATS_NETWORK_TYPE);
785   if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
786     addr_net = GNUNET_ATS_NET_UNSPECIFIED;
787
788   /* Get existing address or address with session == 0 */
789   existing_address = find_equivalent_address (handle, peer, new_address);
790   if (existing_address == NULL )
791   {
792     /* Add a new address */
793     GNUNET_assert(
794         GNUNET_OK == GNUNET_CONTAINER_multipeermap_put (handle->addresses,
795                                                         peer,
796                                                         new_address,
797                                                         GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
798
799     GNUNET_STATISTICS_set (handle->stat, "# addresses",
800         GNUNET_CONTAINER_multipeermap_size (handle->addresses), GNUNET_NO);
801
802     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
803         "Adding new address %p for peer `%s', length %u, session id %u, %s\n",
804         new_address, GNUNET_i2s (peer), plugin_addr_len, session_id,
805         GNUNET_ATS_print_network_type (addr_net));
806
807     /* Tell solver about new address */
808     handle->s_add (handle->solver, new_address, addr_net);
809
810     handle->s_bulk_start (handle->solver);
811     GAS_normalization_normalize_property (handle->addresses, new_address, atsi,
812         atsi_count);
813     handle->s_bulk_stop (handle->solver);
814
815     /* Notify performance clients about new address */
816     GAS_performance_notify_all_clients (&new_address->peer, new_address->plugin,
817         new_address->addr, new_address->addr_len, new_address->session_id,
818         new_address->atsi, new_address->atsi_count,
819         new_address->assigned_bw_out, new_address->assigned_bw_in);
820     return;
821   }
822
823   /* We have an existing address we can use, clean up new */
824   GNUNET_free(new_address->plugin);
825   GNUNET_free_non_null(new_address->atsi);
826   GNUNET_free(new_address);
827   new_address = NULL;
828
829   if (0 != existing_address->session_id)
830   {
831     /* Should not happen */
832     GNUNET_break(0);
833     return;
834   }
835
836   addr_net = get_performance_info (existing_address, GNUNET_ATS_NETWORK_TYPE);
837   if (GNUNET_ATS_VALUE_UNDEFINED == addr_net)
838     addr_net = GNUNET_ATS_NET_UNSPECIFIED;
839
840   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
841       "Found existing address for peer `%s' %p with new session %u in network %s\n",
842       GNUNET_i2s (peer), existing_address, session_id,
843       GNUNET_ATS_print_network_type (addr_net));
844   /* We have an address without an session, update this address */
845   atsi_delta = NULL;
846   atsi_delta_count = 0;
847   if (GNUNET_YES
848       == disassemble_ats_information (existing_address, atsi, atsi_count,
849           &atsi_delta, &atsi_delta_count))
850   {
851     /* Notify performance clients about properties */
852     GAS_performance_notify_all_clients (&existing_address->peer,
853         existing_address->plugin, existing_address->addr,
854         existing_address->addr_len, existing_address->session_id,
855         existing_address->atsi, existing_address->atsi_count,
856         existing_address->assigned_bw_out, existing_address->assigned_bw_in);
857
858     for (c1 = 0; c1 < atsi_delta_count; c1++)
859     {
860       if ((GNUNET_ATS_NETWORK_TYPE == ntohl (atsi_delta[c1].type))
861           && (addr_net != ntohl (atsi_delta[c1].value)))
862       {
863         /* Network type changed */
864         GNUNET_log(GNUNET_ERROR_TYPE_INFO,
865             "Address for peer `%s' %p changed from network %s to %s\n",
866             GNUNET_i2s (peer), existing_address,
867             GNUNET_ATS_print_network_type (addr_net),
868             GNUNET_ATS_print_network_type (ntohl (atsi_delta[c1].value)));
869         handle->s_address_update_network (handle->solver, existing_address,
870             ntohl (atsi_delta[c1].value),
871             get_performance_info (existing_address, GNUNET_ATS_NETWORK_TYPE));
872         addr_net = get_performance_info (existing_address,
873             GNUNET_ATS_NETWORK_TYPE);
874       }
875     }
876     /* Notify solver about update with atsi information and session */
877     handle->s_bulk_start (handle->solver);
878     GAS_normalization_normalize_property (handle->addresses, existing_address,
879         atsi, atsi_count);
880     handle->s_bulk_stop (handle->solver);
881   }
882   GNUNET_free_non_null(atsi_delta);
883
884   /* Notify solver about new session */
885   if (existing_address->session_id == session_id)
886     return; /* possible, can both be 0 since address is revalidated */
887
888   previous_session = existing_address->session_id;
889   existing_address->session_id = session_id;
890   handle->s_address_update_session (handle->solver, existing_address,
891       previous_session, session_id);
892
893   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
894       "Updated existing address for peer `%s' %p length %u with new session %u in network %s\n",
895       GNUNET_i2s (peer), existing_address, existing_address->addr_len,
896       session_id, GNUNET_ATS_print_network_type (addr_net));
897 }
898
899 /**
900  * Update an address with a session or performance information for a peer.
901  *
902  * If an address was added without a session it will be updated with the
903  * session
904  *
905  * @param handle the address handle to use
906  * @param peer peer
907  * @param plugin_name transport plugin name
908  * @param plugin_addr plugin address
909  * @param plugin_addr_len length of the plugin address
910  * @param session_id session id, can be 0
911  * @param atsi performance information for this address
912  * @param atsi_count number of performance information contained
913  */
914 void
915 GAS_addresses_update (struct GAS_Addresses_Handle *handle,
916     const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
917     const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id,
918     const struct GNUNET_ATS_Information *atsi, uint32_t atsi_count)
919 {
920   struct ATS_Address *aa;
921   struct GNUNET_ATS_Information *atsi_delta;
922   uint32_t atsi_delta_count;
923   uint32_t prev_session;
924   int c1;
925
926   if (GNUNET_NO == handle->running)
927     return;
928
929   GNUNET_assert(NULL != handle->addresses);
930
931   /* Get existing address */
932   aa = find_exact_address (handle, peer, plugin_name, plugin_addr,
933       plugin_addr_len, session_id);
934   if (aa == NULL )
935   {
936     /* GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tried to update unknown address for peer `%s' `%s' session id %u\n", */
937     /*             GNUNET_i2s (peer), plugin_name, session_id); */
938     /* GNUNET_break (0); */
939     return;
940   }
941
942   if (NULL == aa->solver_information)
943   {
944     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
945         "Tried to update unknown address for peer `%s' `%s' session id %u\n",
946         GNUNET_i2s (peer), plugin_name, session_id);
947     return;
948   }
949
950   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received `%s' for peer `%s' address \n",
951       "ADDRESS UPDATE", GNUNET_i2s (peer), aa);
952
953   /* Update address */
954   if (session_id != aa->session_id)
955   {
956     /* Session changed */
957     prev_session = aa->session_id;
958     aa->session_id = session_id;
959     handle->s_address_update_session (handle->solver, aa, prev_session,
960         aa->session_id);
961   }
962
963   atsi_delta = NULL;
964   atsi_delta_count = 0;
965   if (GNUNET_YES
966       == disassemble_ats_information (aa, atsi, atsi_count, &atsi_delta,
967           &atsi_delta_count))
968   {
969     /* ATS properties changed */
970     for (c1 = 0; c1 < atsi_delta_count; c1++)
971     {
972       if (GNUNET_ATS_NETWORK_TYPE == ntohl (atsi_delta[c1].type))
973       {
974         /* Network type changed */
975         handle->s_address_update_network (handle->solver, aa,
976             ntohl (atsi_delta[c1].value),
977             get_performance_info (aa, GNUNET_ATS_NETWORK_TYPE));
978       }
979     }
980
981     /* Notify performance clients about updated address */
982     GAS_performance_notify_all_clients (&aa->peer, aa->plugin, aa->addr,
983         aa->addr_len, aa->session_id, aa->atsi, aa->atsi_count,
984         aa->assigned_bw_out, aa->assigned_bw_in);
985
986     handle->s_bulk_start (handle->solver);
987     GAS_normalization_normalize_property (handle->addresses, aa, atsi,
988         atsi_count);
989     handle->s_bulk_stop (handle->solver);
990   }
991   GNUNET_free_non_null(atsi_delta);
992 }
993
994 struct DestroyContext
995 {
996   struct ATS_Address *aa;
997
998   struct GAS_Addresses_Handle *handle;
999
1000   /**
1001    * GNUNET_NO  : full address
1002    * GNUNET_YES : just session
1003    */
1004   int result;
1005 };
1006
1007 /**
1008  * Delete an address
1009  *
1010  * If session != 0, just the session is deleted, the address itself still exists
1011  * If session == 0, remove full address
1012  * If session == 0 and addrlen == 0, destroy inbound address
1013  *
1014  * @param cls unused
1015  * @param key unused
1016  * @param value the 'struct ATS_Address'
1017  * @return GNUNET_OK (continue to iterate)
1018  */
1019 static int
1020 destroy_by_session_id (void *cls,
1021                        const struct GNUNET_PeerIdentity *key,
1022                        void *value)
1023 {
1024   struct DestroyContext *dc = cls;
1025   struct GAS_Addresses_Handle *handle = dc->handle;
1026   const struct ATS_Address *des = dc->aa;
1027   struct ATS_Address *aa = value;
1028
1029   GNUNET_assert(
1030       0 == memcmp (&aa->peer, &des->peer, sizeof(struct GNUNET_PeerIdentity)));
1031
1032   if (des->session_id == 0)
1033   {
1034     /* Session == 0, remove full address  */
1035     if ((0 == strcmp (des->plugin, aa->plugin))
1036         && (aa->addr_len == des->addr_len)
1037         && (0 == memcmp (des->addr, aa->addr, aa->addr_len)))
1038     {
1039
1040       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1041           "Deleting full address for peer `%s' session %u %p\n",
1042           GNUNET_i2s (&aa->peer), aa->session_id, aa);
1043
1044       /* Notify solver about deletion */
1045       GNUNET_assert(
1046           GNUNET_YES == GNUNET_CONTAINER_multipeermap_remove (handle->addresses,
1047                                                               &aa->peer,
1048                                                               aa));
1049       handle->s_del (handle->solver, aa, GNUNET_NO);
1050       free_address (aa);
1051       dc->result = GNUNET_NO;
1052       return GNUNET_OK; /* Continue iteration */
1053     }
1054   }
1055   else
1056   {
1057     /* Session != 0, just remove session */
1058     if (aa->session_id != des->session_id)
1059       return GNUNET_OK; /* irrelevant */
1060
1061     if ((aa->session_id != 0) && (0 != strcmp (des->plugin, aa->plugin)))
1062     {
1063       GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1064           "Different plugins during removal: `%s' vs `%s' \n", des->plugin,
1065           aa->plugin);
1066       GNUNET_break(0);
1067       return GNUNET_OK;
1068     }
1069
1070     if (aa->addr_len == 0)
1071     {
1072       /* Inbound connection died, delete full address */
1073       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1074           "Deleting inbound address for peer `%s': `%s' session %u\n",
1075           GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
1076
1077       /* Notify solver about deletion */
1078       GNUNET_assert(
1079           GNUNET_YES == GNUNET_CONTAINER_multipeermap_remove (handle->addresses,
1080                                                               &aa->peer, aa));
1081       handle->s_del (handle->solver, aa, GNUNET_NO);
1082       free_address (aa);
1083       dc->result = GNUNET_NO;
1084       return GNUNET_OK; /* Continue iteration */
1085     }
1086     else
1087     {
1088       /* Session died */
1089       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1090           "Deleting session for peer `%s': `%s' %u\n", GNUNET_i2s (&aa->peer),
1091           aa->plugin, aa->session_id);
1092       /* Notify solver to delete session */
1093       handle->s_del (handle->solver, aa, GNUNET_YES);
1094       aa->session_id = 0;
1095       aa->active = GNUNET_NO;
1096       return GNUNET_OK;
1097     }
1098   }
1099   return GNUNET_OK;
1100 }
1101
1102
1103 /**
1104  * Remove an address or just a session for a peer.
1105  *
1106  * @param handle the address handle to use
1107  * @param peer peer
1108  * @param plugin_name transport plugin name
1109  * @param plugin_addr plugin address
1110  * @param plugin_addr_len length of the plugin address
1111  * @param session_id session id, can be 0
1112  */
1113 void
1114 GAS_addresses_destroy (struct GAS_Addresses_Handle *handle,
1115     const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
1116     const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id)
1117 {
1118   struct ATS_Address *ea;
1119   struct DestroyContext dc;
1120   if (GNUNET_NO == handle->running)
1121     return;
1122
1123   /* Get existing address */
1124   ea = find_exact_address (handle, peer, plugin_name, plugin_addr,
1125       plugin_addr_len, session_id);
1126   if (ea == NULL )
1127   {
1128     GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1129         "Tried to destroy unknown address for peer `%s' `%s' session id %u\n",
1130         GNUNET_i2s (peer), plugin_name, session_id);
1131     return;
1132   }
1133
1134   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1135       "Received `%s' for peer `%s' address %p session %u\n", "ADDRESS DESTROY",
1136       GNUNET_i2s (peer), ea, session_id);
1137
1138   GNUNET_break(0 < strlen (plugin_name));
1139   dc.handle = handle;
1140   dc.aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len,
1141       session_id);
1142
1143   GNUNET_CONTAINER_multipeermap_get_multiple (handle->addresses,
1144                                               peer,
1145                                               &destroy_by_session_id, &dc);
1146   GNUNET_STATISTICS_set (handle->stat, "# addresses",
1147       GNUNET_CONTAINER_multipeermap_size (handle->addresses), GNUNET_NO);
1148   free_address (dc.aa);
1149 }
1150
1151 /**
1152  * Notification about active use of an address.
1153  * in_use == GNUNET_YES:
1154  *      This address is used to maintain an active connection with a peer.
1155  * in_use == GNUNET_NO:
1156  *      This address is no longer used to maintain an active connection with a peer.
1157  *
1158  * Note: can only be called with in_use == GNUNET_NO if called with GNUNET_YES
1159  * before
1160  *
1161  * @param handle the address handle to use
1162  * @param peer peer
1163  * @param plugin_name transport plugin name
1164  * @param plugin_addr plugin address
1165  * @param plugin_addr_len length of the plugin address
1166  * @param session_id session id, can be 0
1167  * @param in_use GNUNET_YES if GNUNET_NO
1168  * @return GNUNET_SYSERR on failure (address unknown ...)
1169  */
1170 int
1171 GAS_addresses_in_use (struct GAS_Addresses_Handle *handle,
1172     const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
1173     const void *plugin_addr, size_t plugin_addr_len, uint32_t session_id,
1174     int in_use)
1175 {
1176   struct ATS_Address *ea;
1177   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received `%s' for peer `%s'\n",
1178       "ADDRESS IN USE", GNUNET_i2s (peer));
1179
1180   if (GNUNET_NO == handle->running)
1181     return GNUNET_SYSERR;
1182
1183   ea = find_exact_address (handle, peer, plugin_name, plugin_addr,
1184       plugin_addr_len, session_id);
1185   if (NULL == ea)
1186   {
1187     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1188         "Trying to set unknown address `%s' `%s' `%u' to %s \n",
1189         GNUNET_i2s (peer), plugin_name, session_id,
1190         (GNUNET_NO == in_use) ? "NO" : "YES");
1191     GNUNET_break(0);
1192     return GNUNET_SYSERR;
1193   }
1194   if (ea->used == in_use)
1195   {
1196     GNUNET_break(0);
1197     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1198         "Address in use called multiple times for peer `%s': %s -> %s \n",
1199         GNUNET_i2s (peer), (GNUNET_NO == ea->used) ? "NO" : "YES",
1200         (GNUNET_NO == in_use) ? "NO" : "YES");
1201     return GNUNET_SYSERR;
1202   }
1203
1204   /* Tell solver about update */
1205   ea->used = in_use;
1206   handle->s_address_update_inuse (handle->solver, ea, ea->used);
1207   return GNUNET_OK;
1208 }
1209
1210 /**
1211  * Cancel address suggestions for a peer
1212  *
1213  * @param handle the address handle
1214  * @param peer the peer id
1215  */
1216 void
1217 GAS_addresses_request_address_cancel (struct GAS_Addresses_Handle *handle,
1218     const struct GNUNET_PeerIdentity *peer)
1219 {
1220   struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
1221
1222   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received request: `%s' for peer %s\n",
1223       "request_address_cancel", GNUNET_i2s (peer));
1224
1225   while (NULL != cur)
1226   {
1227     if (0 == memcmp (peer, &cur->id, sizeof(cur->id)))
1228       break; /* found */
1229     cur = cur->next;
1230   }
1231
1232   if (NULL == cur)
1233   {
1234     GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1235         "No address requests pending for peer `%s', cannot remove!\n",
1236         GNUNET_i2s (peer));
1237     return;
1238   }
1239   handle->s_get_stop (handle->solver, peer);
1240   GAS_addresses_handle_backoff_reset (handle, peer);
1241   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Removed request pending for peer `%s\n",
1242       GNUNET_i2s (peer));
1243   GNUNET_CONTAINER_DLL_remove(handle->r_head, handle->r_tail, cur);
1244   GNUNET_free(cur);
1245 }
1246
1247
1248 /**
1249  * Request address suggestions for a peer
1250  *
1251  * @param handle the address handle
1252  * @param peer the peer id
1253  */
1254 void
1255 GAS_addresses_request_address (struct GAS_Addresses_Handle *handle,
1256     const struct GNUNET_PeerIdentity *peer)
1257 {
1258   struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
1259   struct ATS_Address *aa;
1260
1261   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received `%s' for peer `%s'\n",
1262       "REQUEST ADDRESS", GNUNET_i2s (peer));
1263
1264   if (GNUNET_NO == handle->running)
1265     return;
1266   while (NULL != cur)
1267   {
1268     if (0 == memcmp (peer, &cur->id, sizeof(cur->id)))
1269       break; /* already suggesting */
1270     cur = cur->next;
1271   }
1272   if (NULL == cur)
1273   {
1274     cur = GNUNET_malloc (sizeof (struct GAS_Addresses_Suggestion_Requests));
1275     cur->id = (*peer);
1276     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1277         "Adding new address suggestion request for `%s'\n",
1278          GNUNET_i2s (peer));
1279     GNUNET_CONTAINER_DLL_insert(handle->r_head, handle->r_tail, cur);
1280   }
1281
1282   /*
1283    * Debuging information about addresses
1284    *
1285    * GNUNET_CONTAINER_multihashmap_get_multiple(handle->addresses,
1286    *  &peer->hashPubKey, &addrinfo_it, (void *) peer);
1287    */
1288
1289   /* Get prefered address from solver */
1290   aa = (struct ATS_Address *) handle->s_get (handle->solver, peer);
1291   if (NULL == aa)
1292   {
1293     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Cannot suggest address for peer `%s'\n",
1294         GNUNET_i2s (peer));
1295     return;
1296   }
1297
1298   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Suggesting address %p for peer `%s'\n",
1299       aa, GNUNET_i2s (peer));
1300
1301   GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
1302       aa->addr_len, aa->session_id, aa->atsi, aa->atsi_count,
1303       aa->assigned_bw_out, aa->assigned_bw_in);
1304
1305   aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval,
1306       ATS_BLOCKING_DELTA);
1307   aa->blocked_until = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1308       aa->block_interval);
1309
1310   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1311       "Address %p ready for suggestion, block interval now %llu \n", aa,
1312       aa->block_interval);
1313 }
1314
1315 /**
1316  * Iterator to reset address blocking
1317  *
1318  * @param cls not used
1319  * @param key the peer
1320  * @param value the address to reset
1321  * @return #GNUNET_OK to continue
1322  */
1323 static int
1324 reset_address_it (void *cls,
1325                   const struct GNUNET_PeerIdentity *key,
1326                   void *value)
1327 {
1328   struct ATS_Address *aa = value;
1329
1330   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1331              "Resetting interval for peer `%s' address %p from %llu to 0\n",
1332              GNUNET_i2s (&aa->peer),
1333              aa,
1334              aa->block_interval);
1335   aa->blocked_until = GNUNET_TIME_UNIT_ZERO_ABS;
1336   aa->block_interval = GNUNET_TIME_UNIT_ZERO;
1337   return GNUNET_OK;
1338 }
1339
1340
1341 /**
1342  * Reset suggestion backoff for a peer
1343  *
1344  * Suggesting addresses is blocked for ATS_BLOCKING_DELTA. Blocking can be
1345  * reset using this function
1346  *
1347  * @param handle the address handle
1348  * @param peer the peer id
1349  */
1350 void
1351 GAS_addresses_handle_backoff_reset (struct GAS_Addresses_Handle *handle,
1352     const struct GNUNET_PeerIdentity *peer)
1353 {
1354   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received `%s' for peer `%s'\n",
1355       "RESET BACKOFF", GNUNET_i2s (peer));
1356
1357   GNUNET_break(
1358       GNUNET_SYSERR != GNUNET_CONTAINER_multipeermap_get_multiple (handle->addresses,
1359                                                                    peer,
1360                                                                    &reset_address_it, NULL));
1361 }
1362
1363
1364 /**
1365  * The preference changed for a peer
1366  *
1367  * @param cls the address handle
1368  * @param peer the peer
1369  * @param kind the ATS kind
1370  * @param pref_rel the new relative preference value
1371  */
1372 static void
1373 normalized_preference_changed_cb (void *cls,
1374     const struct GNUNET_PeerIdentity *peer, enum GNUNET_ATS_PreferenceKind kind,
1375     double pref_rel)
1376 {
1377   GNUNET_assert(NULL != cls);
1378   struct GAS_Addresses_Handle *handle = cls;
1379
1380   /* Tell solver about update */
1381   handle->s_pref (handle->solver, peer, kind, pref_rel);
1382 }
1383
1384 /**
1385  * The relative value for a property changed
1386  *
1387  * @param cls the address handle
1388  * @param address the peer
1389  * @param type the ATS type
1390  * @param prop_rel the new relative preference value
1391  */
1392 static void
1393 normalized_property_changed_cb (void *cls, struct ATS_Address *address,
1394     uint32_t type, double prop_rel)
1395 {
1396   struct GAS_Addresses_Handle *ah = (struct GAS_Addresses_Handle *) cls;
1397   GNUNET_assert(NULL != ah);
1398
1399   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1400       "Normalized property %s for peer `%s' changed to %.3f \n",
1401       GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
1402       prop_rel);
1403
1404   ah->s_address_update_property (ah->solver, address, type, 0, prop_rel);
1405 }
1406
1407 /**
1408  * Function allowing the solver to obtain normalized preference
1409  * values from solver
1410  *
1411  * @param cls unused
1412  * @param id the peer to return the normalized properties for
1413  * @return array of double values with |GNUNET_ATS_PreferenceCount| elements
1414  */
1415 const double *
1416 get_preferences_cb (void *cls, const struct GNUNET_PeerIdentity *id)
1417 {
1418   return GAS_normalization_get_preferences (id);
1419 }
1420
1421 /**
1422  * Function allowing the solver to obtain normalized property
1423  * values for an address from solver
1424  *
1425  * @param cls unused
1426  * @param address the address
1427  * @return array of double values with |GNUNET_ATS_QualityPropertiesCount| elements
1428  */
1429 const double *
1430 get_property_cb (void *cls, const struct ATS_Address *address)
1431 {
1432   return GAS_normalization_get_properties ((struct ATS_Address *) address);
1433 }
1434
1435 /**
1436  * Change the preference for a peer
1437  *
1438  * @param handle the address handle
1439  * @param client the client sending this request
1440  * @param peer the peer id
1441  * @param kind the preference kind to change
1442  * @param score_abs the new preference score
1443  */
1444 void
1445 GAS_addresses_change_preference (struct GAS_Addresses_Handle *handle,
1446     void *client, const struct GNUNET_PeerIdentity *peer,
1447     enum GNUNET_ATS_PreferenceKind kind, float score_abs)
1448 {
1449   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1450       "Received `%s' for peer `%s' for client %p\n", "CHANGE PREFERENCE",
1451       GNUNET_i2s (peer), client);
1452
1453   if (GNUNET_NO == handle->running)
1454     return;
1455
1456   if (GNUNET_NO ==
1457       GNUNET_CONTAINER_multipeermap_contains (handle->addresses,
1458                                               peer))
1459   {
1460     GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1461         "Received `%s' for unknown peer `%s' from client %p\n",
1462         "CHANGE PREFERENCE", GNUNET_i2s (peer), client);
1463     return;
1464   }
1465
1466   handle->s_bulk_start (handle->solver);
1467   /* Tell normalization about change, normalization will call callback if preference changed */
1468   GAS_normalization_normalize_preference (client, peer, kind, score_abs);
1469   handle->s_bulk_stop (handle->solver);
1470 }
1471
1472 /**
1473  * Change the preference for a peer
1474  *
1475  * @param handle the address handle
1476  * @param application the client sending this request
1477  * @param peer the peer id
1478  * @param scope the time interval for this feedback: [now - scope .. now]
1479  * @param kind the preference kind to change
1480  * @param score_abs the new preference score
1481  */
1482 void
1483 GAS_addresses_preference_feedback (struct GAS_Addresses_Handle *handle,
1484     void *application, const struct GNUNET_PeerIdentity *peer,
1485     const struct GNUNET_TIME_Relative scope,
1486     enum GNUNET_ATS_PreferenceKind kind, float score_abs)
1487 {
1488   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1489       "Received `%s' for peer `%s' for client %p\n", "PREFERENCE FEEDBACK",
1490       GNUNET_i2s (peer), application);
1491
1492   if (GNUNET_NO == handle->running)
1493     return;
1494
1495   if (GNUNET_NO ==
1496       GNUNET_CONTAINER_multipeermap_contains (handle->addresses,
1497                                               peer))
1498   {
1499     GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1500         "Received `%s' for unknown peer `%s' from client %p\n",
1501         "PREFERENCE FEEDBACK", GNUNET_i2s (peer), application);
1502     return;
1503   }
1504
1505   handle->s_feedback (handle->solver, application, peer, scope, kind,
1506       score_abs);
1507 }
1508
1509 /**
1510  * Load quotas for networks from configuration
1511  *
1512  * @param cfg configuration handle
1513  * @param out_dest where to write outbound quotas
1514  * @param in_dest where to write inbound quotas
1515  * @param dest_length length of inbound and outbound arrays
1516  * @return number of networks loaded
1517  */
1518 static unsigned int
1519 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg,
1520     unsigned long long *out_dest, unsigned long long *in_dest, int dest_length)
1521 {
1522   char *network_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
1523   char * entry_in = NULL;
1524   char * entry_out = NULL;
1525   char * quota_out_str;
1526   char * quota_in_str;
1527   int c;
1528   int res;
1529
1530   for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
1531   {
1532     in_dest[c] = 0;
1533     out_dest[c] = 0;
1534     GNUNET_asprintf (&entry_out, "%s_QUOTA_OUT", network_str[c]);
1535     GNUNET_asprintf (&entry_in, "%s_QUOTA_IN", network_str[c]);
1536
1537     /* quota out */
1538     if (GNUNET_OK
1539         == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", entry_out,
1540             &quota_out_str))
1541     {
1542       res = GNUNET_NO;
1543       if (0 == strcmp (quota_out_str, GNUNET_ATS_MaxBandwidthString))
1544       {
1545         out_dest[c] = GNUNET_ATS_MaxBandwidth;
1546         res = GNUNET_YES;
1547       }
1548       if ((GNUNET_NO == res)
1549           && (GNUNET_OK
1550               == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str,
1551                   &out_dest[c])))
1552         res = GNUNET_YES;
1553       if ((GNUNET_NO == res)
1554           && (GNUNET_OK
1555               == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_out,
1556                   &out_dest[c])))
1557         res = GNUNET_YES;
1558
1559       if (GNUNET_NO == res)
1560       {
1561         GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1562             _("Could not load quota for network `%s':  `%s', assigning default bandwidth %llu\n"),
1563             network_str[c], quota_out_str, GNUNET_ATS_DefaultBandwidth);
1564         out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1565       }
1566       else
1567       {
1568         GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1569             _("Outbound quota configure for network `%s' is %llu\n"),
1570             network_str[c], out_dest[c]);
1571       }
1572       GNUNET_free(quota_out_str);
1573     }
1574     else
1575     {
1576       GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1577           _("No outbound quota configured for network `%s', assigning default bandwidth %llu\n"),
1578           network_str[c], GNUNET_ATS_DefaultBandwidth);
1579       out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1580     }
1581
1582     /* quota in */
1583     if (GNUNET_OK
1584         == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", entry_in,
1585             &quota_in_str))
1586     {
1587       res = GNUNET_NO;
1588       if (0 == strcmp (quota_in_str, GNUNET_ATS_MaxBandwidthString))
1589       {
1590         in_dest[c] = GNUNET_ATS_MaxBandwidth;
1591         res = GNUNET_YES;
1592       }
1593       if ((GNUNET_NO == res)
1594           && (GNUNET_OK
1595               == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
1596         res = GNUNET_YES;
1597       if ((GNUNET_NO == res)
1598           && (GNUNET_OK
1599               == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_in,
1600                   &in_dest[c])))
1601         res = GNUNET_YES;
1602
1603       if (GNUNET_NO == res)
1604       {
1605         GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1606             _("Could not load quota for network `%s':  `%s', assigning default bandwidth %llu\n"),
1607             network_str[c], quota_in_str, GNUNET_ATS_DefaultBandwidth);
1608         in_dest[c] = GNUNET_ATS_DefaultBandwidth;
1609       }
1610       else
1611       {
1612         GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1613             _("Inbound quota configured for network `%s' is %llu\n"),
1614             network_str[c], in_dest[c]);
1615       }
1616       GNUNET_free(quota_in_str);
1617     }
1618     else
1619     {
1620       GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1621           _("No outbound quota configure for network `%s', assigning default bandwidth %llu\n"),
1622           network_str[c], GNUNET_ATS_DefaultBandwidth);
1623       out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1624     }
1625     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1626         "Loaded quota for network `%s' (in/out): %llu %llu\n", network_str[c],
1627         in_dest[c], out_dest[c]);
1628     GNUNET_free(entry_out);
1629     GNUNET_free(entry_in);
1630   }
1631   return GNUNET_ATS_NetworkTypeCount;
1632 }
1633
1634 /**
1635  * Callback for solver to notify about assignment changes
1636  *
1637  * @param cls the GAS_Addresses_Handle
1638  * @param address the address with changes
1639  */
1640 static void
1641 bandwidth_changed_cb (void *cls, struct ATS_Address *address)
1642 {
1643   struct GAS_Addresses_Handle *handle = cls;
1644   struct GAS_Addresses_Suggestion_Requests *cur;
1645
1646   GNUNET_assert(handle != NULL);
1647   GNUNET_assert(address != NULL);
1648
1649   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1650       "Bandwidth assignment changed for peer %s \n",
1651       GNUNET_i2s (&address->peer));
1652
1653   /* Notify performance clients about changes to address */
1654   GAS_performance_notify_all_clients (&address->peer, address->plugin,
1655       address->addr, address->addr_len, address->session_id, address->atsi,
1656       address->atsi_count, address->assigned_bw_out, address->assigned_bw_in);
1657   cur = handle->r_head;
1658   while (NULL != cur)
1659   {
1660     if (0 == memcmp (&address->peer, &cur->id, sizeof(cur->id)))
1661       break; /* we have an address request pending*/
1662     cur = cur->next;
1663   }
1664   if (NULL == cur)
1665   {
1666     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Nobody is interested in peer `%s' :(\n",
1667         GNUNET_i2s (&address->peer));
1668     return;
1669   }
1670
1671   if ((0 == ntohl (address->assigned_bw_in.value__))
1672       && (0 == ntohl (address->assigned_bw_out.value__)))
1673   {
1674     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1675         "Telling transport to disconnect peer `%s'\n",
1676         GNUNET_i2s (&address->peer));
1677   }
1678   else
1679   {
1680     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1681         "Sending bandwidth update for peer `%s': %llu %llu\n",
1682         GNUNET_i2s (&address->peer), address->assigned_bw_out,
1683         address->assigned_bw_out);
1684   }
1685
1686   /* *Notify scheduling clients about suggestion */
1687   GAS_scheduling_transmit_address_suggestion (&address->peer, address->plugin,
1688       address->addr, address->addr_len, address->session_id, address->atsi,
1689       address->atsi_count, address->assigned_bw_out, address->assigned_bw_in);
1690 }
1691
1692 /**
1693  * Initialize address subsystem. The addresses subsystem manages the addresses
1694  * known and current performance information. It has a solver component
1695  * responsible for the resource allocation. It tells the solver about changes
1696  * and receives updates when the solver changes the resource allocation.
1697  *
1698  * @param cfg configuration to use
1699  * @param stats the statistics handle to use
1700  * @return an address handle
1701  */
1702 struct GAS_Addresses_Handle *
1703 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1704     const struct GNUNET_STATISTICS_Handle *stats)
1705 {
1706   struct GAS_Addresses_Handle *ah;
1707   int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
1708   unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
1709   unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
1710   int quota_count;
1711   char *mode_str;
1712   int c;
1713
1714   ah = GNUNET_malloc (sizeof (struct GAS_Addresses_Handle));
1715   ah->running = GNUNET_NO;
1716
1717   ah->stat = (struct GNUNET_STATISTICS_Handle *) stats;
1718   /* Initialize the addresses database */
1719   ah->addresses = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1720   GNUNET_assert(NULL != ah->addresses);
1721
1722   /* Figure out configured solution method */
1723   if (GNUNET_SYSERR
1724       == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "MODE", &mode_str))
1725   {
1726     GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1727         "No resource assignment method configured, using proportional approach\n");
1728     ah->ats_mode = MODE_PROPORTIONAL;
1729   }
1730   else
1731   {
1732     for (c = 0; c < strlen (mode_str); c++)
1733       mode_str[c] = toupper (mode_str[c]);
1734     if (0 == strcmp (mode_str, "PROPORTIONAL"))
1735     {
1736       ah->ats_mode = MODE_PROPORTIONAL;
1737     }
1738     else if (0 == strcmp (mode_str, "MLP"))
1739     {
1740       ah->ats_mode = MODE_MLP;
1741 #if !HAVE_LIBGLPK
1742       GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1743           "Assignment method `%s' configured, but GLPK is not available, please install \n",
1744           mode_str);
1745       ah->ats_mode = MODE_PROPORTIONAL;
1746 #endif
1747     }
1748     else if (0 == strcmp (mode_str, "RIL"))
1749     {
1750       ah->ats_mode = MODE_RIL;
1751     }
1752     else
1753     {
1754       GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1755           "Invalid resource assignment method `%s' configured, using proportional approach\n",
1756           mode_str);
1757       ah->ats_mode = MODE_PROPORTIONAL;
1758     }
1759     GNUNET_free(mode_str);
1760   }
1761   /* Start configured solution method */
1762   switch (ah->ats_mode)
1763   {
1764   case MODE_MLP:
1765     /* Init the MLP solver with default values */
1766 #if HAVE_LIBGLPK
1767     ah->s_init = &GAS_mlp_init;
1768     ah->s_add = &GAS_mlp_address_add;
1769     ah->s_address_update_property = &GAS_mlp_address_property_changed;
1770     ah->s_address_update_session = &GAS_mlp_address_session_changed;
1771     ah->s_address_update_inuse = &GAS_mlp_address_inuse_changed;
1772     ah->s_address_update_network = &GAS_mlp_address_change_network;
1773     ah->s_get = &GAS_mlp_get_preferred_address;
1774     ah->s_get_stop = &GAS_mlp_stop_get_preferred_address;
1775     ah->s_pref = &GAS_mlp_address_change_preference;
1776     ah->s_feedback = &GAS_mlp_address_preference_feedback;
1777     ah->s_del = &GAS_mlp_address_delete;
1778     ah->s_bulk_start = &GAS_mlp_bulk_start;
1779     ah->s_bulk_stop = &GAS_mlp_bulk_stop;
1780     ah->s_done = &GAS_mlp_done;
1781 #else
1782     GNUNET_free(ah);
1783     return NULL ;
1784 #endif
1785     break;
1786   case MODE_PROPORTIONAL:
1787     /* Init the proportional solver with default values */
1788     ah->s_init = &GAS_proportional_init;
1789     ah->s_add = &GAS_proportional_address_add;
1790     ah->s_address_update_property = &GAS_proportional_address_property_changed;
1791     ah->s_address_update_session = &GAS_proportional_address_session_changed;
1792     ah->s_address_update_inuse = &GAS_proportional_address_inuse_changed;
1793     ah->s_address_update_network = &GAS_proportional_address_change_network;
1794     ah->s_get = &GAS_proportional_get_preferred_address;
1795     ah->s_get_stop = &GAS_proportional_stop_get_preferred_address;
1796     ah->s_pref = &GAS_proportional_address_change_preference;
1797     ah->s_feedback = &GAS_proportional_address_preference_feedback;
1798     ah->s_del = &GAS_proportional_address_delete;
1799     ah->s_bulk_start = &GAS_proportional_bulk_start;
1800     ah->s_bulk_stop = &GAS_proportional_bulk_stop;
1801     ah->s_done = &GAS_proportional_done;
1802     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "ATS started in %s mode\n",
1803         "PROPORTIONAL");
1804     break;
1805   case MODE_RIL:
1806     /* Init the ril solver with default values */
1807     ah->s_init = &GAS_ril_init;
1808     ah->s_add = &GAS_ril_address_add;
1809     ah->s_address_update_property = &GAS_ril_address_property_changed;
1810     ah->s_address_update_session = &GAS_ril_address_session_changed;
1811     ah->s_address_update_inuse = &GAS_ril_address_inuse_changed;
1812     ah->s_address_update_network = &GAS_ril_address_change_network;
1813     ah->s_get = &GAS_ril_get_preferred_address;
1814     ah->s_get_stop = &GAS_ril_stop_get_preferred_address;
1815     ah->s_pref = &GAS_ril_address_change_preference;
1816     ah->s_feedback = &GAS_ril_address_preference_feedback;
1817     ah->s_del = &GAS_ril_address_delete;
1818     ah->s_bulk_start = &GAS_ril_bulk_start;
1819     ah->s_bulk_stop = &GAS_ril_bulk_stop;
1820     ah->s_done = &GAS_ril_done;
1821     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "ATS started in %s mode\n", "RIL");
1822     break;
1823   default:
1824     return NULL ;
1825     break;
1826   }
1827
1828   GNUNET_assert (NULL != ah->s_init);
1829   GNUNET_assert (NULL != ah->s_add);
1830   GNUNET_assert (NULL != ah->s_address_update_inuse);
1831   GNUNET_assert (NULL != ah->s_address_update_property);
1832   GNUNET_assert (NULL != ah->s_address_update_session);
1833   GNUNET_assert (NULL != ah->s_address_update_network);
1834   GNUNET_assert (NULL != ah->s_get);
1835   GNUNET_assert (NULL != ah->s_get_stop);
1836   GNUNET_assert (NULL != ah->s_pref);
1837   GNUNET_assert (NULL != ah->s_feedback);
1838   GNUNET_assert (NULL != ah->s_del);
1839   GNUNET_assert (NULL != ah->s_done);
1840   GNUNET_assert (NULL != ah->s_bulk_start);
1841   GNUNET_assert (NULL != ah->s_bulk_stop);
1842
1843   GAS_normalization_start (&normalized_preference_changed_cb, ah,
1844       &normalized_property_changed_cb, ah);
1845   quota_count = load_quotas (cfg, quotas_in, quotas_out,
1846                              GNUNET_ATS_NetworkTypeCount);
1847
1848   ah->solver = ah->s_init (cfg, stats, ah->addresses, quotas, quotas_in,
1849                            quotas_out, quota_count,
1850                            &bandwidth_changed_cb, ah, &get_preferences_cb,
1851                            NULL, &get_property_cb, NULL );
1852   if (NULL == ah->solver)
1853   {
1854     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to initialize solver!\n"));
1855     GNUNET_free(ah);
1856     return NULL ;
1857   }
1858   /* up and running */
1859   ah->running = GNUNET_YES;
1860
1861   GNUNET_STATISTICS_set (ah->stat, "# addresses",
1862       GNUNET_CONTAINER_multipeermap_size (ah->addresses), GNUNET_NO);
1863
1864   return ah;
1865 }
1866
1867 /**
1868  * Destroy all addresses iterator
1869  *
1870  * @param cls NULL
1871  * @param key peer identity (unused)
1872  * @param value the 'struct ATS_Address' to free
1873  * @return #GNUNET_OK (continue to iterate)
1874  */
1875 static int
1876 destroy_all_address_it (void *cls,
1877                         const struct GNUNET_PeerIdentity *key,
1878                         void *value)
1879 {
1880   struct GAS_Addresses_Handle *handle = cls;
1881   struct ATS_Address *aa = value;
1882
1883   /* Remove */
1884   GNUNET_assert(GNUNET_YES ==
1885                 GNUNET_CONTAINER_multipeermap_remove (handle->addresses, key, value));
1886   /* Notify */
1887   handle->s_del (handle->solver, aa, GNUNET_NO);
1888   /* Destroy */
1889   free_address (aa);
1890
1891   return GNUNET_OK;
1892 }
1893
1894
1895 /**
1896  * Remove all addresses
1897  *
1898  * @param handle the address handle to use
1899  */
1900 void
1901 GAS_addresses_destroy_all (struct GAS_Addresses_Handle *handle)
1902 {
1903   if (GNUNET_NO == handle->running)
1904     return;
1905
1906   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Destroying all addresses\n");
1907   handle->s_bulk_start (handle->solver);
1908   if (handle->addresses != NULL )
1909     GNUNET_CONTAINER_multipeermap_iterate (handle->addresses,
1910                                            &destroy_all_address_it,
1911                                            handle);
1912   handle->s_bulk_start (handle->solver);
1913 }
1914
1915
1916 /**
1917  * Shutdown address subsystem.
1918  *
1919  * @param handle the address handle to shutdown
1920  */
1921 void
1922 GAS_addresses_done (struct GAS_Addresses_Handle *handle)
1923 {
1924   struct GAS_Addresses_Suggestion_Requests *cur;
1925
1926   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Shutting down addresses\n");
1927   GNUNET_assert(NULL != handle);
1928   GAS_addresses_destroy_all (handle);
1929   handle->running = GNUNET_NO;
1930   GNUNET_CONTAINER_multipeermap_destroy (handle->addresses);
1931   handle->addresses = NULL;
1932   while (NULL != (cur = handle->r_head))
1933   {
1934     GNUNET_CONTAINER_DLL_remove(handle->r_head, handle->r_tail, cur);
1935     GNUNET_free(cur);
1936   }
1937   handle->s_done (handle->solver);
1938   GNUNET_free(handle);
1939   /* Stop configured solution method */
1940   GAS_normalization_stop ();
1941 }
1942
1943
1944 struct PeerIteratorContext
1945 {
1946   GNUNET_ATS_Peer_Iterator it;
1947   void *it_cls;
1948   struct GNUNET_CONTAINER_MultiPeerMap *peers_returned;
1949 };
1950
1951
1952 /**
1953  * Iterator to iterate over all peers
1954  *
1955  * @param cls a PeerIteratorContext
1956  * @param key the peer id
1957  * @param value the ATS_address
1958  * @return #GNUNET_OK to continue
1959  */
1960 static int
1961 peer_it (void *cls,
1962          const struct GNUNET_PeerIdentity *key,
1963          void *value)
1964 {
1965   struct PeerIteratorContext *ip_ctx = cls;
1966
1967   if (GNUNET_NO ==
1968       GNUNET_CONTAINER_multipeermap_contains (ip_ctx->peers_returned, key))
1969   {
1970     GNUNET_CONTAINER_multipeermap_put (ip_ctx->peers_returned, key, NULL,
1971                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1972     ip_ctx->it (ip_ctx->it_cls, key);
1973   }
1974
1975   return GNUNET_OK;
1976 }
1977
1978 /**
1979  * Return information all peers currently known to ATS
1980  *
1981  * @param handle the address handle to use
1982  * @param p_it the iterator to call for every peer
1983  * @param p_it_cls the closure for the iterator
1984  */
1985 void
1986 GAS_addresses_iterate_peers (struct GAS_Addresses_Handle *handle,
1987     GNUNET_ATS_Peer_Iterator p_it, void *p_it_cls)
1988 {
1989   struct PeerIteratorContext ip_ctx;
1990   unsigned int size;
1991
1992   if (NULL == p_it)
1993     return;
1994   GNUNET_assert(NULL != handle->addresses);
1995
1996   size = GNUNET_CONTAINER_multipeermap_size (handle->addresses);
1997   if (0 != size)
1998   {
1999     ip_ctx.it = p_it;
2000     ip_ctx.it_cls = p_it_cls;
2001     ip_ctx.peers_returned = GNUNET_CONTAINER_multipeermap_create (size,
2002                                                                   GNUNET_NO);
2003     GNUNET_CONTAINER_multipeermap_iterate (handle->addresses,
2004                                            &peer_it,
2005                                            &ip_ctx);
2006     GNUNET_CONTAINER_multipeermap_destroy (ip_ctx.peers_returned);
2007   }
2008   p_it (p_it_cls, NULL );
2009 }
2010
2011 struct PeerInfoIteratorContext
2012 {
2013   GNUNET_ATS_PeerInfo_Iterator it;
2014   void *it_cls;
2015 };
2016
2017
2018 /**
2019  * Iterator to iterate over a peer's addresses
2020  *
2021  * @param cls a `struct PeerInfoIteratorContext`
2022  * @param key the peer id
2023  * @param value the `struct ATS_address`
2024  * @return #GNUNET_OK to continue
2025  */
2026 static int
2027 peerinfo_it (void *cls,
2028              const struct GNUNET_PeerIdentity *key,
2029              void *value)
2030 {
2031   struct PeerInfoIteratorContext *pi_ctx = cls;
2032   struct ATS_Address *addr = value;
2033
2034   if (NULL != pi_ctx->it)
2035   {
2036     pi_ctx->it (pi_ctx->it_cls, &addr->peer, addr->plugin, addr->addr,
2037         addr->addr_len, addr->active, addr->atsi, addr->atsi_count,
2038         addr->assigned_bw_out, addr->assigned_bw_in);
2039   }
2040   return GNUNET_YES;
2041 }
2042
2043
2044 /**
2045  * Return information all peers currently known to ATS
2046  *
2047  * @param handle the address handle to use
2048  * @param peer the respective peer
2049  * @param pi_it the iterator to call for every peer
2050  * @param pi_it_cls the closure for the iterator
2051  */
2052 void
2053 GAS_addresses_get_peer_info (struct GAS_Addresses_Handle *handle,
2054     const struct GNUNET_PeerIdentity *peer, GNUNET_ATS_PeerInfo_Iterator pi_it,
2055     void *pi_it_cls)
2056 {
2057   struct PeerInfoIteratorContext pi_ctx;
2058   struct GNUNET_BANDWIDTH_Value32NBO zero_bw;
2059
2060   GNUNET_assert(NULL != peer);
2061   GNUNET_assert(NULL != handle->addresses);
2062   if (NULL == pi_it)
2063     return; /* does not make sense without callback */
2064
2065   zero_bw = GNUNET_BANDWIDTH_value_init (0);
2066   pi_ctx.it = pi_it;
2067   pi_ctx.it_cls = pi_it_cls;
2068
2069   GNUNET_CONTAINER_multipeermap_get_multiple (handle->addresses,
2070                                               peer,
2071                                               &peerinfo_it, &pi_ctx);
2072
2073   if (NULL != pi_it)
2074     pi_it (pi_it_cls, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, zero_bw,
2075         zero_bw);
2076
2077 }
2078
2079 /* end of gnunet-service-ats_addresses.c */