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