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