add address test
[oweals/gnunet.git] / src / ats / gnunet-service-ats_addresses.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file ats/gnunet-service-ats_addresses.c
23  * @brief ats service address management
24  * @author Matthias Wachs
25  * @author Christian Grothoff
26  */
27 #include "platform.h"
28 #include "gnunet_ats_service.h"
29 #include "gnunet-service-ats.h"
30 #include "gnunet-service-ats_addresses.h"
31 #include "gnunet-service-ats_normalization.h"
32 #include "gnunet-service-ats_performance.h"
33 #include "gnunet-service-ats_scheduling.h"
34 #include "gnunet-service-ats_reservations.h"
35 #if HAVE_LIBGLPK
36 #include "gnunet-service-ats-solver_mlp.h"
37 #endif
38 #include "gnunet-service-ats-solver_proportional.h"
39 #include "gnunet-service-ats-solver_ril.h"
40
41 /**
42  * NOTE: Do not change this documentation. This documentation is based on
43  * gnunet.org:/vcs/fsnsg/ats-paper.git/tech-doku/ats-tech-guide.tex
44  * use build_txt.sh to generate plaintext output
45  *
46  *   1 ATS addresses : ATS address management
47  *
48  *    This ATS addresses ("addresses") component manages the addresses known to
49  *    ATS service and suggests addresses to transport service when it is
50  *    interested in address suggestion for a peer. ATS addresses also
51  *    instantiates the bandwidth assignment mechanism (solver), notifies it
52  *    about changes to addresses and forwards changes to bandwidth assignments
53  *    to transport, depending if transport is interested in this change.
54  *
55  *     1.1 Input data
56  *
57  *       1.1.1 Addresses
58  *
59  *    Addresses are added by specifying peer ID, plugin, address, address length
60  *    and session, if available. ATS information can be specified if available.
61  *
62  *       1.1.2 Networks
63  *
64  *    ATS specifies a fix set of networks an address can belong to. For each
65  *    network an inbound and outbound quota will be specified. The available
66  *    networks and addtional helper varaibles are defined in
67  *    gnunet_ats_service.h. At the moment 5 networks are defined:
68  *      * GNUNET_ATS_NET_UNSPECIFIED
69  *      * GNUNET_ATS_NET_LOOPBACK
70  *      * GNUNET_ATS_NET_LAN
71  *      * GNUNET_ATS_NET_WAN
72  *      * GNUNET_ATS_NET_WLAN
73  *
74  *    The total number of networks defined is stored in
75  *    GNUNET_ATS_NetworkTypeCount GNUNET_ATS_NetworkType can be used array
76  *    initializer for an int array, while GNUNET_ATS_NetworkType is an
77  *    initializer for a char array containing a string description of all
78  *    networks
79  *
80  *       1.1.3 Quotas
81  *
82  *    An inbound and outbound quota for each of the networks mentioned in 1.1.2
83  *    is loaded from ats configuration during initialization. This quota defines
84  *    to total amount of inbound and outbound traffic allowed for a specific
85  *    network. The configuration values used are in section ats:
86  *      * "NETWORK"_QUOTA_IN = <value>
87  *      * "NETWORK"_QUOTA_IN = <value>
88  *
89  *    You can specify quotas by setting the <value> to a:
90  *      * unrestricted: unlimited
91  *      * number of bytes: e.g. 10240
92  *      * fancy value: e.g. 64 Kib
93  *
94  *    unlimited is defined as GNUNET_ATS_MaxBandwidthString and equivalent to
95  *    the value GNUNET_ATS_MaxBandwidth Important predefined values for quotas
96  *    are:
97  *      * GNUNET_ATS_DefaultBandwidth: 65536
98  *      * GNUNET_ATS_MaxBandwidth: UINT32_MAX
99  *      * GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT: 1024
100  *
101  *    Details of loading quotas and default values will be described on
102  *
103  *       1.1.4 Preference values
104  *
105  *     1.2 Data structures used
106  *
107  *    Addresse uses struct ATS_Address for each address. The structs are stored
108  *    in a linked list and provides a pointer void *solver_information for the
109  *    solver to store address specific information. It provides the int values
110  *    active which is set to GNUNET_YES if the address is select for transport
111  *    use and used, representing that transport service is actively using this
112  *    address. Address information are stored in peer, addr, addr_len, plugin.
113  *
114  *     1.3 Initialization
115  *
116  *    During initialization a hashmap to store addresses is created. The quotas
117  *    for all networks defined for ATS are loaded from configuration. For each
118  *    network first the logic will check if the string
119  *    GNUNET_ATS_MaxBandwidthString is configured, if not it will try to convert
120  *    the configured value as a fancy size and if this fails it will try to use
121  *    it as a value_number. If no configuration value is found it will assign
122  *    GNUNET_ATS_DefaultBandwidth. The most important step is to load the
123  *    configured solver using configuration "[ats]:MODE". Current solvers are
124  *    MODE_PROPORTIONAL, MODE_MLP. Interaction is done using a solver API
125  *
126  *     1.4 Solver API
127  *
128  *    Solver functions:
129  *      * s_init: init the solver with required information
130  *      * s_add: add a new address
131  *      * s_update: update ATS values or session for an address
132  *      * s_get: get prefered address for a peer
133  *      * s_del: delete an address
134  *      * s_pref: change preference value for a peer
135  *      * s_done: shutdown solver
136  *
137  *    Callbacks: addresses provides a bandwidth_changed_cb callback to the
138  *    solver which is called when bandwidth assigned to peer has changed
139  *
140  *     1.5 Shutdown
141  *
142  *    During shutdown all addresses are freed and the solver told to shutdown
143  *
144  *     1.6 Addresses and sessions
145  *
146  *    Addresses consist of the address itself and a numerical session. When a
147  *    new address without a session is added it has no session, so it gets
148  *    session 0 assigned. When an address with a session is added and an address
149  *    object with session 0 is found, this object is updated with the session
150  *    otherwise a new address object with this session assigned is created.
151  *
152  *       1.6.1 Terminology
153  *
154  *    Addresses a1,a2 with session s1, s2 are "exact" if:
155  *    (a1 == a2)&&(s1 == s2)
156  *    Addresses a1,a2 with session s1, s2 are "equivalent" if:
157  *    (a1 == a2)&&((s1 == s2)||(s1 == 0)||(s2 == 0)
158  *
159  *     1.7 Address management
160  *
161  *    Transport service notifies ATS about changes to the addresses known to
162  *    him.
163  *
164  *       1.7.1 Adding an address
165  *
166  *    When transport learns a new address it tells ATS and ATS is telling
167  *    addresses about it using GAS_address_add. If not known to addresses it
168  *    creates a new address object and calls solver's s_add. ATS information are
169  *    deserialized and solver is notified about the session and ATS information
170  *    using s_update.
171  *
172  *       1.7.2 Updating an address
173  *
174  *    Addresses does an lookup up for the existing address with the given
175  *    session. If disassembles included ATS information and notifies the solver
176  *    using s_update about the update.
177  *
178  *       1.7.3 Deleting an address
179  *
180  *    Addresses does an lookup for the exact address and session and if removes
181  *    this address. If session != 0 the session is set to 0 and the address is
182  *    kept. If session == 0, the addresses is removed.
183  *
184  *       1.7.4 Requesting an address suggestion
185  *
186  *    The address client issues a request address message to be notified about
187  *    address suggestions for a specific peer. Addresses asks the solver with
188  *    s_get. If no address is available, it will not send a response, otherwise
189  *    it will respond with the choosen address.
190  *
191  *       1.7.5 Address suggestions
192  *
193  *    Addresses will notify the client automatically on any bandwidth_changed_cb
194  *    by the solver if a address suggestion request is pending. If no address is
195  *    available it will not respond at all If the client is not interested
196  *    anymore, it has to cancel the address suggestion request.
197  *
198  *       1.7.6 Suggestions blocks and reset
199  *
200  *    After suggesting an address it is blocked for ATS_BLOCKING_DELTA sec. to
201  *    prevent the client from being thrashed. If the client requires immediately
202  *    it can reset this block using GAS_addresses_handle_backoff_reset.
203  *
204  *       1.7.7 Marking address in use
205  *
206  *    The client can notify addresses that it successfully uses an address and
207  *    wants this address to be kept by calling GSA_address_in_use. Adresses will
208  *    mark the address as used an notify the solver about the use.
209  *
210  *       1.7.8 Address lifecycle
211  *
212  *      * (add address)
213  *      * (updated address) || (address in use)
214  *      * (delete address)
215  *
216  *     1.8 Bandwidth assignment
217  *
218  *    The addresses are used to perform resource allocation operations. ATS
219  *    addresses takes care of instantiating the solver configured and notifies
220  *    the respective solver about address changes and receives changes to the
221  *    bandwidth assignment from the solver. The current bandwidth assignment is
222  *    sent to transport. The specific solvers will be described in the specific
223  *    section.
224  *
225  *     1.9 Changing peer preferences
226  *
227  *    The bandwidth assigned to a peer can be influenced by setting a preference
228  *    for a peer. The prefernce will be given to to the solver with s_pref which
229  *    has to take care of the preference value
230
231  */
232
233
234 /**
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', length %u, session id %u, %s\n",
808                 new_address, GNUNET_i2s (peer), plugin_addr_len, 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), existing_address,
873                   GNUNET_ATS_print_network_type (addr_net),
874                   GNUNET_ATS_print_network_type (ntohl (atsi_delta[c1].value)));
875           handle->s_address_update_network (handle->solver, existing_address,
876                   ntohl (atsi_delta[c1].value),
877                   get_performance_info (existing_address, GNUNET_ATS_NETWORK_TYPE));
878           addr_net = get_performance_info (existing_address, GNUNET_ATS_NETWORK_TYPE);
879       }
880     }
881     /* Notify solver about update with atsi information and session */
882     handle->s_bulk_start (handle->solver);
883     GAS_normalization_normalize_property (handle->addresses, existing_address, atsi, atsi_count);
884     handle->s_bulk_stop (handle->solver);
885   }
886   GNUNET_free_non_null (atsi_delta);
887
888   /* Notify solver about new session */
889   if (existing_address->session_id == session_id)
890         return; /* possible, can both be 0 since address is revalidated */
891
892   previous_session = existing_address->session_id;
893   existing_address->session_id = session_id;
894   handle->s_address_update_session (handle->solver, existing_address,
895                 previous_session, session_id);
896
897   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
898            "Updated existing address for peer `%s' %p length %u with new session %u in network %s\n",
899            GNUNET_i2s (peer),
900            existing_address,
901            existing_address->addr_len,
902            session_id,
903            GNUNET_ATS_print_network_type(addr_net));
904 }
905
906
907 /**
908  * Update an address with a session or performance information for a peer.
909  *
910  * If an address was added without a session it will be updated with the
911  * session
912  *
913  * @param handle the address handle to use
914  * @param peer peer
915  * @param plugin_name transport plugin name
916  * @param plugin_addr plugin address
917  * @param plugin_addr_len length of the plugin address
918  * @param session_id session id, can be 0
919  * @param atsi performance information for this address
920  * @param atsi_count number of performance information contained
921  */
922 void
923 GAS_addresses_update (struct GAS_Addresses_Handle *handle,
924                       const struct GNUNET_PeerIdentity *peer,
925                       const char *plugin_name, const void *plugin_addr,
926                       size_t plugin_addr_len, uint32_t session_id,
927                       const struct GNUNET_ATS_Information *atsi,
928                       uint32_t atsi_count)
929 {
930   struct ATS_Address *aa;
931   struct GNUNET_ATS_Information *atsi_delta;
932   uint32_t atsi_delta_count;
933   uint32_t prev_session;
934   int c1;
935
936   if (GNUNET_NO == handle->running)
937     return;
938
939   GNUNET_assert (NULL != handle->addresses);
940
941   /* Get existing address */
942   aa = find_exact_address (handle, peer, plugin_name,
943         plugin_addr, plugin_addr_len, session_id);
944   if (aa == NULL)
945   {
946     /* GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tried to update unknown address for peer `%s' `%s' session id %u\n", */
947     /*             GNUNET_i2s (peer), plugin_name, session_id); */
948     /* GNUNET_break (0); */
949     return;
950   }
951
952   if (NULL == aa->solver_information)
953   {
954     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tried to update unknown address for peer `%s' `%s' session id %u\n",
955                  GNUNET_i2s (peer), plugin_name, session_id);
956     return;
957   }
958
959   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
960                 "Received `%s' for peer `%s' address \n",
961                 "ADDRESS UPDATE",
962                 GNUNET_i2s (peer), aa);
963
964   /* Update address */
965   if (session_id != aa->session_id)
966   {
967         /* Session changed */
968     prev_session = aa->session_id;
969     aa->session_id = session_id;
970     handle->s_address_update_session (handle->solver, aa, prev_session, aa->session_id);
971   }
972
973   atsi_delta = NULL;
974   atsi_delta_count = 0;
975   if (GNUNET_YES == disassemble_ats_information (aa, atsi, atsi_count, &atsi_delta, &atsi_delta_count))
976   {
977     /* ATS properties changed */
978     for (c1 = 0; c1 < atsi_delta_count; c1++)
979     {
980         if (GNUNET_ATS_NETWORK_TYPE == ntohl (atsi_delta[c1].type))
981         {
982             /* Network type changed */
983             handle->s_address_update_network (handle->solver, aa,
984                       ntohl (atsi_delta[c1].value),
985                       get_performance_info (aa, GNUNET_ATS_NETWORK_TYPE));
986         }
987     }
988
989     /* Notify performance clients about updated address */
990     GAS_performance_notify_all_clients (&aa->peer,
991                     aa->plugin,
992                     aa->addr, aa->addr_len,
993                     aa->session_id,
994                     aa->atsi, aa->atsi_count,
995                     aa->assigned_bw_out,
996                     aa->assigned_bw_in);
997
998     handle->s_bulk_start (handle->solver);
999     GAS_normalization_normalize_property (handle->addresses, aa, atsi, atsi_count);
1000     handle->s_bulk_stop (handle->solver);
1001   }
1002   GNUNET_free_non_null (atsi_delta);
1003 }
1004
1005
1006 struct DestroyContext
1007 {
1008   struct ATS_Address *aa;
1009
1010   struct GAS_Addresses_Handle *handle;
1011
1012   /**
1013    * GNUNET_NO  : full address
1014    * GNUNET_YES : just session
1015    */
1016   int result;
1017 };
1018
1019
1020 /**
1021  * Delete an address
1022  *
1023  * If session != 0, just the session is deleted, the address itself still exists
1024  * If session == 0, remove full address
1025  * If session == 0 and addrlen == 0, destroy inbound address
1026  *
1027  * @param cls unused
1028  * @param key unused
1029  * @param value the 'struct ATS_Address'
1030  * @return GNUNET_OK (continue to iterate)
1031  */
1032 static int
1033 destroy_by_session_id (void *cls, const struct GNUNET_HashCode * key, void *value)
1034 {
1035   struct DestroyContext *dc = cls;
1036   struct GAS_Addresses_Handle *handle = dc->handle;
1037   const struct ATS_Address *des = dc->aa;
1038   struct ATS_Address *aa = value;
1039
1040   GNUNET_assert (0 == memcmp (&aa->peer, &des->peer,
1041                               sizeof (struct GNUNET_PeerIdentity)));
1042
1043
1044   if (des->session_id == 0)
1045   {
1046     /* Session == 0, remove full address  */
1047     if ((0 == strcmp (des->plugin, aa->plugin)) &&
1048         (aa->addr_len == des->addr_len) &&
1049         (0 == memcmp (des->addr, aa->addr, aa->addr_len)))
1050     {
1051
1052       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1053                   "Deleting full address for peer `%s' session %u %p\n",
1054                   GNUNET_i2s (&aa->peer), aa->session_id, aa);
1055
1056       /* Notify solver about deletion */
1057       GNUNET_assert (GNUNET_YES ==
1058                 GNUNET_CONTAINER_multihashmap_remove (handle->addresses,
1059                         &aa->peer.hashPubKey, aa));
1060       handle->s_del (handle->solver, aa, GNUNET_NO);
1061       free_address (aa);
1062       dc->result = GNUNET_NO;
1063       return GNUNET_OK; /* Continue iteration */
1064     }
1065   }
1066   else
1067   {
1068     /* Session != 0, just remove session */
1069     if (aa->session_id != des->session_id)
1070       return GNUNET_OK; /* irrelevant */
1071
1072     if ((aa->session_id != 0) &&
1073         (0 != strcmp (des->plugin, aa->plugin)))
1074     {
1075         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1076                     "Different plugins during removal: `%s' vs `%s' \n",
1077                     des->plugin, aa->plugin);
1078         GNUNET_break (0);
1079         return GNUNET_OK;
1080     }
1081
1082     if (aa->addr_len == 0)
1083     {
1084         /* Inbound connection died, delete full address */
1085         GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1086                     "Deleting inbound address for peer `%s': `%s' session %u\n",
1087                     GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
1088
1089         /* Notify solver about deletion */
1090         GNUNET_assert (GNUNET_YES ==
1091                         GNUNET_CONTAINER_multihashmap_remove (handle->addresses,
1092                         &aa->peer.hashPubKey, aa));
1093         handle->s_del (handle->solver, aa, GNUNET_NO);
1094         free_address (aa);
1095         dc->result = GNUNET_NO;
1096         return GNUNET_OK; /* Continue iteration */
1097     }
1098     else
1099     {
1100         /* Session died */
1101         GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1102                     "Deleting session for peer `%s': `%s' %u\n",
1103                     GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
1104         /* Notify solver to delete session */
1105         handle->s_del (handle->solver, aa, GNUNET_YES);
1106         aa->session_id = 0;
1107         aa->active = GNUNET_NO;
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 static int
1268 addrinfo_it (void *cls, const struct GNUNET_HashCode *key, void *value)
1269 {
1270         static int count = 0;
1271         struct GNUNET_PeerIdentity *id = cls;
1272         struct ATS_Address *aa = value;
1273
1274   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1275               "[%u] Peer `%s' %s length %u session %u active %s\n",
1276               count, GNUNET_i2s (id), aa->plugin, aa->addr_len, aa->session_id,
1277               (GNUNET_YES == aa->active) ? "active" : "inactive");
1278
1279   count ++;
1280         return GNUNET_OK;
1281 }
1282 */
1283
1284 /**
1285  * Request address suggestions for a peer
1286  *
1287  * @param handle the address handle
1288  * @param peer the peer id
1289  */
1290 void
1291 GAS_addresses_request_address (struct GAS_Addresses_Handle *handle,
1292                                const struct GNUNET_PeerIdentity *peer)
1293 {
1294   struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
1295   struct ATS_Address *aa;
1296
1297   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1298               "Received `%s' for peer `%s'\n",
1299               "REQUEST ADDRESS",
1300               GNUNET_i2s (peer));
1301
1302   if (GNUNET_NO == handle->running)
1303     return;
1304   while (NULL != cur)
1305   {
1306       if (0 == memcmp (peer, &cur->id, sizeof (cur->id)))
1307         break; /* already suggesting */
1308       cur = cur->next;
1309   }
1310   if (NULL == cur)
1311   {
1312       cur = GNUNET_malloc (sizeof (struct GAS_Addresses_Suggestion_Requests));
1313       cur->id = (*peer);
1314       GNUNET_CONTAINER_DLL_insert (handle->r_head, handle->r_tail, cur);
1315   }
1316
1317   /*
1318    * Debuging information about addresses
1319    *
1320    * GNUNET_CONTAINER_multihashmap_get_multiple(handle->addresses,
1321    *  &peer->hashPubKey, &addrinfo_it, (void *) peer);
1322    */
1323
1324   /* Get prefered address from solver */
1325   aa = (struct ATS_Address *) handle->s_get (handle->solver, peer);
1326   if (NULL == aa)
1327   {
1328     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1329                 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
1330     return;
1331   }
1332
1333   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1334               "Suggesting address %p for peer `%s'\n", aa, GNUNET_i2s (peer));
1335
1336   GAS_scheduling_transmit_address_suggestion (peer,
1337                                               aa->plugin,
1338                                               aa->addr, aa->addr_len,
1339                                               aa->session_id,
1340                                               aa->atsi, aa->atsi_count,
1341                                               aa->assigned_bw_out,
1342                                               aa->assigned_bw_in);
1343
1344   aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval, ATS_BLOCKING_DELTA);
1345   aa->blocked_until = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(), aa->block_interval);
1346
1347   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1348        "Address %p ready for suggestion, block interval now %llu \n",
1349        aa, aa->block_interval);
1350 }
1351
1352
1353 /**
1354  * Iterator to reset address blocking
1355  *
1356  * @param cls not used
1357  * @param key the peer
1358  * @param value the address to reset
1359  * @return GNUNET_OK to continue
1360  */
1361 static int
1362 reset_address_it (void *cls, const struct GNUNET_HashCode *key, void *value)
1363 {
1364   struct ATS_Address *aa = value;
1365
1366   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1367               "Resetting interval for peer `%s' address %p from %llu to 0\n",
1368               GNUNET_i2s (&aa->peer), aa, aa->block_interval);
1369
1370   aa->blocked_until = GNUNET_TIME_UNIT_ZERO_ABS;
1371   aa->block_interval = GNUNET_TIME_UNIT_ZERO;
1372   return GNUNET_OK;
1373 }
1374
1375
1376 /**
1377  * Reset suggestion backoff for a peer
1378  *
1379  * Suggesting addresses is blocked for ATS_BLOCKING_DELTA. Blocking can be
1380  * reset using this function
1381  *
1382  * @param handle the address handle
1383  * @param peer the peer id
1384  */
1385 void
1386 GAS_addresses_handle_backoff_reset (struct GAS_Addresses_Handle *handle,
1387                                     const struct GNUNET_PeerIdentity *peer)
1388 {
1389   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1390               "Received `%s' for peer `%s'\n",
1391               "RESET BACKOFF",
1392               GNUNET_i2s (peer));
1393
1394   GNUNET_break (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses,
1395                                               &peer->hashPubKey,
1396                                               &reset_address_it,
1397                                               NULL));
1398 }
1399
1400
1401 /**
1402  * The preference changed for a peer
1403  *
1404  * @param cls the address handle
1405  * @param peer the peer
1406  * @param kind the ATS kind
1407  * @param pref_rel the new relative preference value
1408  */
1409 static void
1410 normalized_preference_changed_cb (void *cls,
1411                                   const struct GNUNET_PeerIdentity *peer,
1412                                   enum GNUNET_ATS_PreferenceKind kind,
1413                                   double pref_rel)
1414 {
1415   GNUNET_assert (NULL != cls);
1416   struct GAS_Addresses_Handle *handle = cls;
1417
1418   /* Tell solver about update */
1419   handle->s_pref (handle->solver, peer, kind, pref_rel);
1420 }
1421
1422
1423 /**
1424  * The relative value for a property changed
1425  *
1426  * @param cls the address handle
1427  * @param address the peer
1428  * @param type the ATS type
1429  * @param prop_rel the new relative preference value
1430  */
1431 static void
1432 normalized_property_changed_cb (void *cls,
1433                                 struct ATS_Address *address,
1434                                 uint32_t type,
1435                                 double prop_rel)
1436 {
1437   struct GAS_Addresses_Handle *ah = (struct GAS_Addresses_Handle *) cls;
1438   GNUNET_assert (NULL != ah);
1439
1440   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1441         "Normalized property %s for peer `%s' changed to %.3f \n",
1442         GNUNET_ATS_print_property_type (type),
1443         GNUNET_i2s (&address->peer),
1444         prop_rel);
1445
1446   ah->s_address_update_property (ah->solver, address, type, 0, prop_rel);
1447 }
1448
1449
1450 /**
1451  * Function allowing the solver to obtain normalized preference
1452  * values from solver
1453  *
1454  * @param cls unused
1455  * @param id the peer to return the normalized properties for
1456  * @return array of double values with |GNUNET_ATS_PreferenceCount| elements
1457  */
1458 const double *
1459 get_preferences_cb (void *cls, const struct GNUNET_PeerIdentity *id)
1460 {
1461         return GAS_normalization_get_preferences (id);
1462 }
1463
1464 /**
1465  * Function allowing the solver to obtain normalized property
1466  * values for an address from solver
1467  *
1468  * @param cls unused
1469  * @param address the address
1470  * @return array of double values with |GNUNET_ATS_QualityPropertiesCount| elements
1471  */
1472 const double *
1473 get_property_cb (void *cls, const struct ATS_Address *address)
1474 {
1475         return GAS_normalization_get_properties ((struct ATS_Address *) address);
1476 }
1477
1478 /**
1479  * Change the preference for a peer
1480  *
1481  * @param handle the address handle
1482  * @param client the client sending this request
1483  * @param peer the peer id
1484  * @param kind the preference kind to change
1485  * @param score_abs the new preference score
1486  */
1487 void
1488 GAS_addresses_change_preference (struct GAS_Addresses_Handle *handle,
1489                                  void *client,
1490                                  const struct GNUNET_PeerIdentity *peer,
1491                                  enum GNUNET_ATS_PreferenceKind kind,
1492                                  float score_abs)
1493 {
1494   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1495         "Received `%s' for peer `%s' for client %p\n",
1496         "CHANGE PREFERENCE",
1497         GNUNET_i2s (peer), client);
1498
1499   if (GNUNET_NO == handle->running)
1500     return;
1501
1502   if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->addresses,
1503                                                           &peer->hashPubKey))
1504   {
1505       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1506                   "Received `%s' for unknown peer `%s' from client %p\n",
1507                   "CHANGE PREFERENCE",
1508                   GNUNET_i2s (peer), client);
1509       return;
1510   }
1511
1512   handle->s_bulk_start (handle->solver);
1513   /* Tell normalization about change, normalization will call callback if preference changed */
1514   GAS_normalization_normalize_preference (client, peer, kind, score_abs);
1515   handle->s_bulk_stop (handle->solver);
1516 }
1517
1518
1519 /**
1520  * Change the preference for a peer
1521  *
1522  * @param handle the address handle
1523  * @param application the client sending this request
1524  * @param peer the peer id
1525  * @param scope the time interval for this feedback: [now - scope .. now]
1526  * @param kind the preference kind to change
1527  * @param score_abs the new preference score
1528  */
1529 void
1530 GAS_addresses_preference_feedback (struct GAS_Addresses_Handle *handle,
1531                                   void *application,
1532                                   const struct GNUNET_PeerIdentity *peer,
1533                                   const struct GNUNET_TIME_Relative scope,
1534                                   enum GNUNET_ATS_PreferenceKind kind,
1535                                   float score_abs)
1536 {
1537   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1538         "Received `%s' for peer `%s' for client %p\n",
1539         "PREFERENCE FEEDBACK",
1540         GNUNET_i2s (peer), application);
1541
1542   if (GNUNET_NO == handle->running)
1543     return;
1544
1545   if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->addresses,
1546                                                           &peer->hashPubKey))
1547   {
1548     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1549                 "Received `%s' for unknown peer `%s' from client %p\n",
1550                 "PREFERENCE FEEDBACK",
1551                 GNUNET_i2s (peer), application);
1552     return;
1553   }
1554
1555   handle->s_feedback (handle->solver, application, peer, scope, kind, score_abs);
1556 }
1557
1558
1559 /**
1560  * Load quotas for networks from configuration
1561  *
1562  * @param cfg configuration handle
1563  * @param out_dest where to write outbound quotas
1564  * @param in_dest where to write inbound quotas
1565  * @param dest_length length of inbound and outbound arrays
1566  * @return number of networks loaded
1567  */
1568 static unsigned int
1569 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg,
1570             unsigned long long *out_dest,
1571             unsigned long long *in_dest,
1572             int dest_length)
1573 {
1574   char *network_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
1575   char * entry_in = NULL;
1576   char * entry_out = NULL;
1577   char * quota_out_str;
1578   char * quota_in_str;
1579   int c;
1580   int res;
1581
1582   for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
1583   {
1584     in_dest[c] = 0;
1585     out_dest[c] = 0;
1586     GNUNET_asprintf (&entry_out, "%s_QUOTA_OUT", network_str[c]);
1587     GNUNET_asprintf (&entry_in, "%s_QUOTA_IN", network_str[c]);
1588
1589     /* quota out */
1590     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, &quota_out_str))
1591     {
1592       res = GNUNET_NO;
1593       if (0 == strcmp(quota_out_str, GNUNET_ATS_MaxBandwidthString))
1594       {
1595         out_dest[c] = GNUNET_ATS_MaxBandwidth;
1596         res = GNUNET_YES;
1597       }
1598       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &out_dest[c])))
1599         res = GNUNET_YES;
1600       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_out,  &out_dest[c])))
1601          res = GNUNET_YES;
1602
1603       if (GNUNET_NO == res)
1604       {
1605           GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not load quota for network `%s':  `%s', assigning default bandwidth %llu\n"),
1606               network_str[c], quota_out_str, GNUNET_ATS_DefaultBandwidth);
1607           out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1608       }
1609       else
1610       {
1611           GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Outbound quota configure for network `%s' is %llu\n"),
1612               network_str[c], out_dest[c]);
1613       }
1614       GNUNET_free (quota_out_str);
1615     }
1616     else
1617     {
1618       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("No outbound quota configured for network `%s', assigning default bandwidth %llu\n"),
1619           network_str[c], GNUNET_ATS_DefaultBandwidth);
1620       out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1621     }
1622
1623     /* quota in */
1624     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, &quota_in_str))
1625     {
1626       res = GNUNET_NO;
1627       if (0 == strcmp(quota_in_str, GNUNET_ATS_MaxBandwidthString))
1628       {
1629         in_dest[c] = GNUNET_ATS_MaxBandwidth;
1630         res = GNUNET_YES;
1631       }
1632       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
1633         res = GNUNET_YES;
1634       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_in,  &in_dest[c])))
1635          res = GNUNET_YES;
1636
1637       if (GNUNET_NO == res)
1638       {
1639           GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not load quota for network `%s':  `%s', assigning default bandwidth %llu\n"),
1640               network_str[c], quota_in_str, GNUNET_ATS_DefaultBandwidth);
1641           in_dest[c] = GNUNET_ATS_DefaultBandwidth;
1642       }
1643       else
1644       {
1645           GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Inbound quota configured for network `%s' is %llu\n"),
1646               network_str[c], in_dest[c]);
1647       }
1648       GNUNET_free (quota_in_str);
1649     }
1650     else
1651     {
1652       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("No outbound quota configure for network `%s', assigning default bandwidth %llu\n"),
1653           network_str[c], GNUNET_ATS_DefaultBandwidth);
1654       out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1655     }
1656     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]);
1657     GNUNET_free (entry_out);
1658     GNUNET_free (entry_in);
1659   }
1660   return GNUNET_ATS_NetworkTypeCount;
1661 }
1662
1663
1664 /**
1665  * Callback for solver to notify about assignment changes
1666  *
1667  * @param cls the GAS_Addresses_Handle
1668  * @param address the address with changes
1669  */
1670 static void
1671 bandwidth_changed_cb (void *cls, struct ATS_Address *address)
1672 {
1673   struct GAS_Addresses_Handle *handle = cls;
1674   struct GAS_Addresses_Suggestion_Requests *cur;
1675
1676   GNUNET_assert (handle != NULL);
1677   GNUNET_assert (address != NULL);
1678
1679   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Bandwidth assignment changed for peer %s \n",
1680                 GNUNET_i2s(&address->peer));
1681
1682   /* Notify performance clients about changes to address */
1683   GAS_performance_notify_all_clients (&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   cur = handle->r_head;
1691   while (NULL != cur)
1692   {
1693       if (0 == memcmp (&address->peer, &cur->id, sizeof (cur->id)))
1694         break; /* we have an address request pending*/
1695       cur = cur->next;
1696   }
1697   if (NULL == cur)
1698   {
1699       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1700                   "Nobody is interested in peer `%s' :(\n",GNUNET_i2s (&address->peer));
1701       return;
1702   }
1703
1704   if ((0 == ntohl (address->assigned_bw_in.value__)) &&
1705                 (0 == ntohl (address->assigned_bw_out.value__)))
1706   {
1707     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1708                 "Telling transport to disconnect peer `%s'\n",
1709                 GNUNET_i2s (&address->peer));
1710   }
1711   else
1712   {
1713     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1714                 "Sending bandwidth update for peer `%s': %llu %llu\n",
1715                 GNUNET_i2s (&address->peer),
1716                 address->assigned_bw_out,
1717                 address->assigned_bw_out);
1718   }
1719
1720   /* *Notify scheduling clients about suggestion */
1721   GAS_scheduling_transmit_address_suggestion (&address->peer,
1722                                               address->plugin,
1723                                               address->addr, address->addr_len,
1724                                               address->session_id,
1725                                               address->atsi, address->atsi_count,
1726                                               address->assigned_bw_out,
1727                                               address->assigned_bw_in);
1728 }
1729
1730
1731 /**
1732  * Initialize address subsystem. The addresses subsystem manages the addresses
1733  * known and current performance information. It has a solver component
1734  * responsible for the resource allocation. It tells the solver about changes
1735  * and receives updates when the solver changes the resource allocation.
1736  *
1737  * @param cfg configuration to use
1738  * @param stats the statistics handle to use
1739  * @return an address handle
1740  */
1741 struct GAS_Addresses_Handle *
1742 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1743                     const struct GNUNET_STATISTICS_Handle *stats)
1744 {
1745   struct GAS_Addresses_Handle *ah;
1746   int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
1747   unsigned long long  quotas_in[GNUNET_ATS_NetworkTypeCount];
1748   unsigned long long  quotas_out[GNUNET_ATS_NetworkTypeCount];
1749   int quota_count;
1750   char *mode_str;
1751   int c;
1752
1753   ah = GNUNET_malloc (sizeof (struct GAS_Addresses_Handle));
1754   ah->running = GNUNET_NO;
1755
1756   ah->stat = (struct GNUNET_STATISTICS_Handle *) stats;
1757   /* Initialize the addresses database */
1758   ah->addresses = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
1759   GNUNET_assert (NULL != ah->addresses);
1760
1761   /* Figure out configured solution method */
1762   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "MODE", &mode_str))
1763   {
1764       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "No resource assignment method configured, using proportional approach\n");
1765       ah->ats_mode = MODE_PROPORTIONAL;
1766   }
1767   else
1768   {
1769       for (c = 0; c < strlen (mode_str); c++)
1770         mode_str[c] = toupper (mode_str[c]);
1771       if (0 == strcmp (mode_str, "PROPORTIONAL"))
1772       {
1773           ah->ats_mode = MODE_PROPORTIONAL;
1774       }
1775       else if (0 == strcmp (mode_str, "MLP"))
1776       {
1777           ah->ats_mode = MODE_MLP;
1778 #if !HAVE_LIBGLPK
1779           GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Assignment method `%s' configured, but GLPK is not available, please install \n", mode_str);
1780           ah->ats_mode = MODE_PROPORTIONAL;
1781 #endif
1782       }
1783       else if (0 == strcmp (mode_str, "RIL"))
1784       {
1785           ah->ats_mode = MODE_RIL;
1786       }
1787       else
1788       {
1789           GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid resource assignment method `%s' configured, using proportional approach\n", mode_str);
1790           ah->ats_mode = MODE_PROPORTIONAL;
1791       }
1792       GNUNET_free (mode_str);
1793   }
1794   /* Start configured solution method */
1795   switch (ah->ats_mode)
1796   {
1797     case MODE_MLP:
1798       /* Init the MLP solver with default values */
1799 #if HAVE_LIBGLPK
1800       ah->s_init = &GAS_mlp_init;
1801       ah->s_add = &GAS_mlp_address_add;
1802       ah->s_address_update_property = &GAS_mlp_address_property_changed;
1803       ah->s_address_update_session = &GAS_mlp_address_session_changed;
1804       ah->s_address_update_inuse = &GAS_mlp_address_inuse_changed;
1805       ah->s_address_update_network = &GAS_mlp_address_change_network;
1806       ah->s_get = &GAS_mlp_get_preferred_address;
1807       ah->s_get_stop = &GAS_mlp_stop_get_preferred_address;
1808       ah->s_pref = &GAS_mlp_address_change_preference;
1809       ah->s_feedback = &GAS_mlp_address_preference_feedback;
1810       ah->s_del =  &GAS_mlp_address_delete;
1811       ah->s_bulk_start = &GAS_mlp_bulk_start;
1812       ah->s_bulk_stop = &GAS_mlp_bulk_stop;
1813       ah->s_done = &GAS_mlp_done;
1814 #else
1815       GNUNET_free (ah);
1816       return NULL;
1817 #endif
1818       break;
1819     case MODE_PROPORTIONAL:
1820       /* Init the proportional solver with default values */
1821       ah->s_init = &GAS_proportional_init;
1822       ah->s_add = &GAS_proportional_address_add;
1823       ah->s_address_update_property = &GAS_proportional_address_property_changed;
1824       ah->s_address_update_session = &GAS_proportional_address_session_changed;
1825       ah->s_address_update_inuse = &GAS_proportional_address_inuse_changed;
1826       ah->s_address_update_network = &GAS_proportional_address_change_network;
1827       ah->s_get = &GAS_proportional_get_preferred_address;
1828       ah->s_get_stop = &GAS_proportional_stop_get_preferred_address;
1829       ah->s_pref = &GAS_proportional_address_change_preference;
1830       ah->s_feedback = &GAS_proportional_address_preference_feedback;
1831       ah->s_del  = &GAS_proportional_address_delete;
1832       ah->s_bulk_start = &GAS_proportional_bulk_start;
1833       ah->s_bulk_stop = &GAS_proportional_bulk_stop;
1834       ah->s_done = &GAS_proportional_done;
1835       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS started in %s mode\n", "PROPORTIONAL");
1836       break;
1837     case MODE_RIL:
1838       /* Init the ril solver with default values */
1839           ah->s_init = &GAS_ril_init;
1840           ah->s_add = &GAS_ril_address_add;
1841           ah->s_address_update_property = &GAS_ril_address_property_changed;
1842           ah->s_address_update_session = &GAS_ril_address_session_changed;
1843           ah->s_address_update_inuse = &GAS_ril_address_inuse_changed;
1844           ah->s_address_update_network = &GAS_ril_address_change_network;
1845           ah->s_get = &GAS_ril_get_preferred_address;
1846           ah->s_get_stop = &GAS_ril_stop_get_preferred_address;
1847           ah->s_pref = &GAS_ril_address_change_preference;
1848           ah->s_feedback = &GAS_ril_address_preference_feedback;
1849           ah->s_del  = &GAS_ril_address_delete;
1850           ah->s_bulk_start = &GAS_ril_bulk_start;
1851           ah->s_bulk_stop = &GAS_ril_bulk_stop;
1852           ah->s_done = &GAS_ril_done;
1853           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS started in %s mode\n", "RIL");
1854           break;
1855     default:
1856       return NULL;
1857       break;
1858   }
1859
1860   GNUNET_assert (NULL != ah->s_init);
1861   GNUNET_assert (NULL != ah->s_add);
1862   GNUNET_assert (NULL != ah->s_address_update_inuse);
1863   GNUNET_assert (NULL != ah->s_address_update_property);
1864   GNUNET_assert (NULL != ah->s_address_update_session);
1865   GNUNET_assert (NULL != ah->s_address_update_network);
1866   GNUNET_assert (NULL != ah->s_get);
1867   GNUNET_assert (NULL != ah->s_get_stop);
1868   GNUNET_assert (NULL != ah->s_pref);
1869   GNUNET_assert (NULL != ah->s_feedback);
1870   GNUNET_assert (NULL != ah->s_del);
1871   GNUNET_assert (NULL != ah->s_done);
1872   GNUNET_assert (NULL != ah->s_bulk_start);
1873   GNUNET_assert (NULL != ah->s_bulk_stop);
1874
1875   GAS_normalization_start (&normalized_preference_changed_cb, ah,
1876                                                                                                  &normalized_property_changed_cb, ah);
1877   quota_count = load_quotas(cfg, quotas_in, quotas_out, GNUNET_ATS_NetworkTypeCount);
1878
1879   ah->solver = ah->s_init (cfg, stats, ah->addresses,
1880                   quotas, quotas_in, quotas_out, quota_count,
1881                   &bandwidth_changed_cb, ah,
1882                   &get_preferences_cb, NULL,
1883                   &get_property_cb, NULL);
1884   if (NULL == ah->solver)
1885   {
1886     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to initialize solver!\n"));
1887     GNUNET_free (ah);
1888     return NULL;
1889   }
1890
1891   /* up and running */
1892   ah->running = GNUNET_YES;
1893   return ah;
1894 }
1895
1896
1897 /**
1898  * Destroy all addresses iterator
1899  *
1900  * @param cls NULL
1901  * @param key peer identity (unused)
1902  * @param value the 'struct ATS_Address' to free
1903  * @return GNUNET_OK (continue to iterate)
1904  */
1905 static int
1906 destroy_all_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1907 {
1908   struct GAS_Addresses_Handle *handle = cls;
1909   struct ATS_Address *aa = value;
1910
1911   /* Remove */
1912   GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (handle->addresses, key, value));
1913   /* Notify */
1914   handle->s_del (handle->solver, aa, GNUNET_NO);
1915   /* Destroy */
1916   free_address (aa);
1917
1918   return GNUNET_OK;
1919 }
1920
1921
1922 /**
1923  * Remove all addresses
1924  *
1925  * @param handle the address handle to use
1926  */
1927 void
1928 GAS_addresses_destroy_all (struct GAS_Addresses_Handle *handle)
1929 {
1930   if (GNUNET_NO == handle->running)
1931     return;
1932
1933   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1934               "Destroying all addresses\n");
1935   handle->s_bulk_start (handle->solver);
1936   if (handle->addresses != NULL)
1937     GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &destroy_all_address_it, handle);
1938   handle->s_bulk_start (handle->solver);
1939 }
1940
1941
1942 /**
1943  * Shutdown address subsystem.
1944  *
1945  * @param handle the address handle to shutdown
1946  */
1947 void
1948 GAS_addresses_done (struct GAS_Addresses_Handle *handle)
1949 {
1950   struct GAS_Addresses_Suggestion_Requests *cur;
1951
1952   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1953               "Shutting down addresses\n");
1954   GNUNET_assert (NULL != handle);
1955   GAS_addresses_destroy_all (handle);
1956   handle->running = GNUNET_NO;
1957   GNUNET_CONTAINER_multihashmap_destroy (handle->addresses);
1958   handle->addresses = NULL;
1959   while (NULL != (cur = handle->r_head))
1960   {
1961       GNUNET_CONTAINER_DLL_remove (handle->r_head, handle->r_tail, cur);
1962       GNUNET_free (cur);
1963   }
1964   handle->s_done (handle->solver);
1965   GNUNET_free (handle);
1966   /* Stop configured solution method */
1967   GAS_normalization_stop ();
1968 }
1969
1970
1971 struct PeerIteratorContext
1972 {
1973   GNUNET_ATS_Peer_Iterator it;
1974   void *it_cls;
1975   struct GNUNET_CONTAINER_MultiHashMap *peers_returned;
1976 };
1977
1978
1979 /**
1980  * Iterator to iterate over all peers
1981  *
1982  * @param cls a PeerIteratorContext
1983  * @param key the peer id
1984  * @param value the ATS_address
1985  * @return GNUNET_OK to continue
1986  */
1987 static int
1988 peer_it (void *cls,
1989          const struct GNUNET_HashCode * key,
1990          void *value)
1991 {
1992   struct PeerIteratorContext *ip_ctx = cls;
1993   struct GNUNET_PeerIdentity tmp;
1994
1995   if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(ip_ctx->peers_returned, key))
1996   {
1997       GNUNET_CONTAINER_multihashmap_put(ip_ctx->peers_returned, key, NULL, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1998       tmp.hashPubKey = (*key);
1999       ip_ctx->it (ip_ctx->it_cls, &tmp);
2000   }
2001
2002   return GNUNET_OK;
2003 }
2004
2005
2006 /**
2007  * Return information all peers currently known to ATS
2008  *
2009  * @param handle the address handle to use
2010  * @param p_it the iterator to call for every peer
2011  * @param p_it_cls the closure for the iterator
2012  */
2013 void
2014 GAS_addresses_iterate_peers (struct GAS_Addresses_Handle *handle,
2015                                                                                                                  GNUNET_ATS_Peer_Iterator p_it,
2016                                                                                                                  void *p_it_cls)
2017 {
2018   struct PeerIteratorContext ip_ctx;
2019   unsigned int size;
2020
2021   if (NULL == p_it)
2022       return;
2023   GNUNET_assert (NULL != handle->addresses);
2024
2025   size = GNUNET_CONTAINER_multihashmap_size(handle->addresses);
2026   if (0 != size)
2027   {
2028     ip_ctx.it = p_it;
2029     ip_ctx.it_cls = p_it_cls;
2030     ip_ctx.peers_returned = GNUNET_CONTAINER_multihashmap_create (size, GNUNET_NO);
2031     GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &peer_it, &ip_ctx);
2032     GNUNET_CONTAINER_multihashmap_destroy (ip_ctx.peers_returned);
2033   }
2034   p_it (p_it_cls, NULL);
2035 }
2036
2037
2038 struct PeerInfoIteratorContext
2039 {
2040   GNUNET_ATS_PeerInfo_Iterator it;
2041   void *it_cls;
2042 };
2043
2044
2045 /**
2046  * Iterator to iterate over a peer's addresses
2047  *
2048  * @param cls a PeerInfoIteratorContext
2049  * @param key the peer id
2050  * @param value the ATS_address
2051  * @return GNUNET_OK to continue
2052  */
2053 static int 
2054 peerinfo_it (void *cls,
2055              const struct GNUNET_HashCode * key,
2056              void *value)
2057 {
2058   struct PeerInfoIteratorContext *pi_ctx = cls;
2059   struct ATS_Address *addr = (struct ATS_Address *)  value;
2060
2061   if (NULL != pi_ctx->it)
2062   {
2063     pi_ctx->it (pi_ctx->it_cls,
2064                 &addr->peer,
2065                 addr->plugin,
2066                 addr->addr, addr->addr_len,
2067                 addr->active,
2068                 addr->atsi, addr->atsi_count,
2069                 addr->assigned_bw_out,
2070                 addr->assigned_bw_in);
2071   }
2072   return GNUNET_YES;
2073 }
2074
2075
2076 /**
2077  * Return information all peers currently known to ATS
2078  *
2079  * @param handle the address handle to use
2080  * @param peer the respective peer
2081  * @param pi_it the iterator to call for every peer
2082  * @param pi_it_cls the closure for the iterator
2083  */
2084 void
2085 GAS_addresses_get_peer_info (struct GAS_Addresses_Handle *handle,
2086                              const struct GNUNET_PeerIdentity *peer,
2087                              GNUNET_ATS_PeerInfo_Iterator pi_it,
2088                              void *pi_it_cls)
2089 {
2090   struct PeerInfoIteratorContext pi_ctx;
2091   struct GNUNET_BANDWIDTH_Value32NBO zero_bw;
2092   GNUNET_assert (NULL != peer);
2093   GNUNET_assert (NULL != handle->addresses);
2094   if (NULL == pi_it)
2095     return; /* does not make sense without callback */
2096
2097   zero_bw = GNUNET_BANDWIDTH_value_init (0);
2098   pi_ctx.it = pi_it;
2099   pi_ctx.it_cls = pi_it_cls;
2100
2101   GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey, &peerinfo_it, &pi_ctx);
2102
2103   if (NULL != pi_it)
2104     pi_it (pi_it_cls, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, zero_bw, zero_bw);
2105
2106 }
2107
2108
2109 /* end of gnunet-service-ats_addresses.c */