fix warning
[oweals/gnunet.git] / src / ats / gnunet-service-ats-solver_proportional.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-solver_proportional.c
23  * @brief ATS proportional solver
24  * @author Matthias Wachs
25  * @author Christian Grothoff
26  */
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet-service-ats_addresses.h"
30 #include "gnunet_statistics_service.h"
31
32 #define LOG(kind,...) GNUNET_log_from (kind, "ats-proportional",__VA_ARGS__)
33
34 /**
35  *
36  * NOTE: Do not change this documentation. This documentation is based
37  * on gnunet.org:/vcs/fsnsg/ats-paper.git/tech-doku/ats-tech-guide.tex
38  * use build_txt.sh to generate plaintext output
39  *
40  * ATS addresses : proportional solver
41  *
42  *    The proportional solver ("proportional") distributes the available
43  *    bandwidth fair over all the addresses influenced by the
44  *    preference values. For each available network type an in- and
45  *    outbound quota is configured and the bandwidth available in
46  *    these networks is distributed over the addresses.  The solver
47  *    first assigns every addresses the minimum amount of bandwidth
48  *    GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT and then distributes the
49  *    remaining bandwidth available according to the preference
50  *    values. For each peer only a single address gets bandwidth
51  *    assigned and only one address marked as active.  The most
52  *    important functionality for the solver is implemented in: *
53  *    find_address_it is an hashmap iterator returning the prefered
54  *    address for an peer * update_quota_per_network distributes
55  *    available bandwidth for a network over active addresses
56  *
57  *    Changes to addresses automatically have an impact on the the
58  *    bandwidth assigned to other addresses in the same network since
59  *    the solver distributes the remaining bandwidth over the
60  *    addresses in the network.  When changes to the addresses occur,
61  *    the solver first performs the changes, like adding or deleting
62  *    addresses, and then updates bandwidth assignment for the
63  *    affected network. Bandwidth assignment is only recalculated on
64  *    demand when an address is requested by a client for a peer or
65  *    when the addresses available have changed or an address changed
66  *    the network it is located in. When the bandwidth assignment has
67  *    changed the callback is called with the new bandwidth
68  *    assignments. The bandwidth distribution for a network is
69  *    recalculated due to: * address suggestion requests * address
70  *    deletions * address switching networks during address update *
71  *    preference changes
72  *
73  *     3.1 Data structures used
74  *
75  *    For each ATS network (e.g. WAN, LAN, loopback) a struct Network
76  *    is used to specify network related information as total adresses
77  *    and active addresses in this network and the configured in- and
78  *    outbound quota. Each network also contains a list of addresses
79  *    added to the solver located in this network. The proportional
80  *    solver uses the addresses' solver_information field to store the
81  *    proportional network it belongs to for each address.
82  *
83  *     3.2 Initializing
84  *
85  *    When the proportional solver is initialized the solver creates a
86  *    new solver handle and initializes the network structures with
87  *    the quotas passed from addresses and returns the handle solver.
88  *
89  *     3.3 Adding an address
90  *
91  *    When a new address is added to the solver using s_add, a lookup
92  *    for the network for this address is done and the address is
93  *    enqueued in in the linked list of the network.
94  *
95  *     3.4 Updating an address
96  *
97  *    The main purpose of address updates is to update the ATS
98  *    information for addresse selection. Important for the proportional
99  *    solver is when an address switches network it is located
100  *    in. This is common because addresses added by transport's
101  *    validation mechanism are commonly located in
102  *    GNUNET_ATS_NET_UNSPECIFIED. Addresses in validation are located
103  *    in this network type and only if a connection is successful on
104  *    return of payload data transport switches to the real network
105  *    the address is located in.  When an address changes networks it
106  *    is first of all removed from the old network using the solver
107  *    API function GAS_proportional_address_delete and the network in
108  *    the address struct is updated. A lookup for the respective new
109  *    proportional network is done and stored in the addresse's
110  *    solver_information field. Next the address is re-added to the
111  *    solver using the solver API function
112  *    GAS_proportional_address_add. If the address was marked as in
113  *    active, the solver checks if bandwidth is available in the
114  *    network and if yes sets the address to active and updates the
115  *    bandwidth distribution in this network. If no bandwidth is
116  *    available it sets the bandwidth for this address to 0 and tries
117  *    to suggest an alternative address. If an alternative address was
118  *    found, addresses' callback is called for this address.
119  *
120  *     3.5 Deleting an address
121  *
122  *    When an address is removed from the solver, it removes the
123  *    respective address from the network and if the address was
124  *    marked as active, it updates the bandwidth distribution for this
125  *    network.
126  *
127  *     3.6 Requesting addresses
128  *
129  *    When an address is requested for a peer the solver performs a
130  *    lookup for the peer entry in addresses address hashmap and
131  *    selects the best address.  The selection of the most suitable
132  *    address is done in the find_address_it hashmap iterator
133  *    described in detail in section 3.7. If no address is returned,
134  *    no address can be suggested at the moment. If the address
135  *    returned is marked as active, the solver can return this
136  *    address. If the address is not marked as active, the solver
137  *    checks if another address belongign to this peer is marked as
138  *    active and marks the address as inactive, updates the bandwidth
139  *    for this address to 0, call the bandwidth changed callback for
140  *    this address due to the change and updates quota assignment for
141  *    the addresse's network. the now in-active address is belonging
142  *    to. The solver marks the new address as active and updates the
143  *    bandwidth assignment for this network.
144  *
145  *     3.7 Choosing addresses
146  *
147  *    Choosing the best possible address for suggestion is done by
148  *    iterating over all addresses of a peer stored in addresses'
149  *    hashmap and using the hashmap iterator find_address_it to select
150  *    the best available address.  Several checks are done when an
151  *    address is selected. First if this address is currently blocked
152  *    by addresses from being suggested. An address is blocked for the
153  *    duration of ATS_BLOCKING_DELTA when it is suggested to
154  *    transport. Next it is checked if at least
155  *    GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT bytes bandwidth is available
156  *    in the addresse's network, because suggesting an address without
157  *    bandwidth does not make sense. This also ensures that all active
158  *    addresses in this network get at least the minimum amount of
159  *    bandwidth assigned. In the next step the solver ensures that for
160  *    tcp connections inbound connections are prefered over outbound
161  *    connections. In the next stet the solver ensures that
162  *    connections are prefered in the following order: * connections
163  *    are already established and have bandwidth assigned *
164  *    connections with a shorter distance * connectes have a shorter
165  *    latency
166  *
167  *     3.8 Changing preferences
168  *
169  *     3.9 Shutdown
170  *
171  *    During shutdown all network entries and aging processes are
172  *    destroyed and freed.
173  *
174  *
175  * OLD DOCUMENTATION
176  *
177  * This solver assigns in and outbound bandwidth equally for all
178  * addresses in specific network type (WAN, LAN) based on configured
179  * in and outbound quota for this network.
180  *
181  * The solver is notified by addresses about changes to the addresses
182  * and recalculates the bandwith assigned if required. The solver
183  * notifies addresses by calling the GAS_bandwidth_changed_cb
184  * callback.
185  *
186  * - Initialization
187  *
188  *
189  *
190  *
191  * For each peer only a single is selected and marked as "active" in the address
192  * struct.
193  *
194  * E.g.:
195  *
196  * You have the networks WAN and LAN and quotas
197  * WAN_TOTAL_IN, WAN_TOTAL_OUT
198  * LAN_TOTAL_IN, LAN_TOTAL_OUT
199  *
200  * If you have x addresses in the network segment LAN, the quotas are
201  * QUOTA_PER_ADDRESS = LAN_TOTAL_OUT / x
202  *
203  * Quotas are automatically recalculated and reported back when addresses are
204  * - requested
205  *
206  */
207
208 #define PREF_AGING_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
209 #define PREF_AGING_FACTOR 0.95
210
211 #define DEFAULT_REL_PREFERENCE 1.0
212 #define DEFAULT_ABS_PREFERENCE 0.0
213 #define MIN_UPDATE_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
214
215 /**
216  * A handle for the proportional solver
217  */
218 struct GAS_PROPORTIONAL_Handle
219 {
220   /**
221    * Statistics handle
222    */
223   struct GNUNET_STATISTICS_Handle *stats;
224
225   /**
226    * Hashmap containing all valid addresses
227    */
228   const struct GNUNET_CONTAINER_MultiPeerMap *addresses;
229
230   /**
231    * Pending address requests
232    */
233   struct GNUNET_CONTAINER_MultiPeerMap *requests;
234
235   /**
236    * Bandwidth changed callback
237    */
238   GAS_bandwidth_changed_cb bw_changed;
239
240   /**
241    * Bandwidth changed callback cls
242    */
243   void *bw_changed_cls;
244
245   /**
246    * ATS function to get preferences
247    */
248   GAS_get_preferences get_preferences;
249
250   /**
251    * Closure for ATS function to get preferences
252    */
253   void *get_preferences_cls;
254
255   /**
256    * ATS function to get properties
257    */
258   GAS_get_properties get_properties;
259
260   /**
261    * Closure for ATS function to get properties
262    */
263   void *get_properties_cls;
264
265   /**
266    * Bulk lock
267    */
268   int bulk_lock;
269
270   /**
271    * Number of changes while solver was locked
272    */
273   int bulk_requests;
274
275   /**
276    * Total number of addresses for solver
277    */
278   unsigned int total_addresses;
279
280   /**
281    * Number of active addresses for solver
282    */
283   unsigned int active_addresses;
284
285   /**
286    * Networks array
287    */
288   struct Network *network_entries;
289
290   /**
291    * Number of networks
292    */
293   unsigned int networks;
294
295 };
296
297 /**
298  * Representation of a network
299  */
300 struct Network
301 {
302   /**
303    * ATS network type
304    */
305   unsigned int type;
306
307   /**
308    * Network description
309    */
310   char *desc;
311
312   /**
313    * Total inbound quota
314    *
315    */
316   unsigned long long total_quota_in;
317
318   /**
319    * Total outbound quota
320    *
321    */
322   unsigned long long total_quota_out;
323
324   /**
325    * Number of active addresses for this network
326    */
327   unsigned int active_addresses;
328
329   /**
330    * Number of total addresses for this network
331    */
332   unsigned int total_addresses;
333
334   /**
335    * String for statistics total addresses
336    */
337   char *stat_total;
338
339   /**
340    * String for statistics active addresses
341    */
342   char *stat_active;
343
344   struct AddressWrapper *head;
345   struct AddressWrapper *tail;
346 };
347
348 /**
349  * Wrapper for addresses to store them in network's linked list
350  */
351 struct AddressWrapper
352 {
353   /**
354    * Next in DLL
355    */
356   struct AddressWrapper *next;
357
358   /**
359    * Previous in DLL
360    */
361   struct AddressWrapper *prev;
362
363   /**
364    * The address
365    */
366   struct ATS_Address *addr;
367 };
368
369 /**
370  *  Important solver functions
371  *  ---------------------------
372  */
373
374 /**
375  * Test if bandwidth is available in this network to add an additional address
376  *
377  * @param net the network type to update
378  * @return GNUNET_YES or GNUNET_NO
379  */
380 static int
381 is_bandwidth_available_in_network (struct Network *net)
382 {
383   GNUNET_assert(NULL != net);
384   unsigned int na = net->active_addresses + 1;
385   uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
386   if (((net->total_quota_in / na) > min_bw)
387       && ((net->total_quota_out / na) > min_bw))
388   {
389     LOG(GNUNET_ERROR_TYPE_DEBUG,
390         "Enough bandwidth available for %u active addresses in network `%s'\n",
391         na, net->desc);
392
393     return GNUNET_YES;
394   }
395   LOG(GNUNET_ERROR_TYPE_DEBUG,
396       "Not enough bandwidth available for %u active addresses in network `%s'\n",
397       na, net->desc);
398   return GNUNET_NO;
399 }
400
401 /**
402  * Update bandwidth assigned to peers in this network
403  *
404  * @param s the solver handle
405  * @param net the network type to update
406  * @param address_except address excluded from notification, since we suggest
407  * this address
408  */
409 static void
410 distribute_bandwidth_in_network (struct GAS_PROPORTIONAL_Handle *s,
411     struct Network *net, struct ATS_Address *address_except)
412 {
413   unsigned long long remaining_quota_in = 0;
414   unsigned long long quota_out_used = 0;
415
416   unsigned long long remaining_quota_out = 0;
417   unsigned long long quota_in_used = 0;
418   uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
419   double peer_prefs;
420   double total_prefs; /* Important: has to be double not float due to precision */
421   double cur_pref; /* Important: has to be double not float due to precision */
422   const double *t = NULL; /* Important: has to be double not float due to precision */
423   int c;
424
425   unsigned long long assigned_quota_in = 0;
426   unsigned long long assigned_quota_out = 0;
427   struct AddressWrapper *cur;
428
429   if (GNUNET_YES == s->bulk_lock)
430   {
431     s->bulk_requests++;
432     return;
433   }
434
435   LOG(GNUNET_ERROR_TYPE_DEBUG,
436       "Recalculate quota for network type `%s' for %u addresses (in/out): %llu/%llu \n",
437       net->desc, net->active_addresses, net->total_quota_in,
438       net->total_quota_in);
439
440   if (net->active_addresses == 0)
441     return; /* no addresses to update */
442
443   /* Idea
444    * Assign every peer in network minimum Bandwidth
445    * Distribute bandwidth left according to preference
446    */
447
448   if ((net->active_addresses * min_bw) > net->total_quota_in)
449   {
450     GNUNET_break(0);
451     return;
452   }
453   if ((net->active_addresses * min_bw) > net->total_quota_out)
454   {
455     GNUNET_break(0);
456     return;
457   }
458
459   remaining_quota_in = net->total_quota_in - (net->active_addresses * min_bw);
460   remaining_quota_out = net->total_quota_out - (net->active_addresses * min_bw);
461   LOG(GNUNET_ERROR_TYPE_DEBUG, "Remaining bandwidth : (in/out): %llu/%llu \n",
462       remaining_quota_in, remaining_quota_out);
463   total_prefs = 0.0;
464   for (cur = net->head; NULL != cur; cur = cur->next)
465   {
466     if (GNUNET_YES == cur->addr->active)
467     {
468       GNUNET_assert(
469           NULL != (t = s->get_preferences (s->get_preferences_cls, &cur->addr->peer)));
470
471       peer_prefs = 0.0;
472       for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
473       {
474         if (c != GNUNET_ATS_PREFERENCE_END)
475         {
476           //fprintf (stderr, "VALUE[%u] %s %.3f \n", c, GNUNET_i2s (&cur->addr->peer), t[c]);
477           peer_prefs += t[c];
478         }
479       }
480       total_prefs += (peer_prefs / (GNUNET_ATS_PreferenceCount - 1));
481     }
482   }
483   for (cur = net->head; NULL != cur; cur = cur->next)
484   {
485     if (GNUNET_YES == cur->addr->active)
486     {
487       cur_pref = 0.0;
488       GNUNET_assert(
489           NULL != (t = s->get_preferences (s->get_preferences_cls, &cur->addr->peer)));
490
491       for (c = 0; c < GNUNET_ATS_PreferenceCount; c++)
492       {
493         if (c != GNUNET_ATS_PREFERENCE_END)
494           cur_pref += t[c];
495       }
496       cur_pref /= 2;
497
498       assigned_quota_in = min_bw
499           + ((cur_pref / total_prefs) * remaining_quota_in);
500       assigned_quota_out = min_bw
501           + ((cur_pref / total_prefs) * remaining_quota_out);
502
503       LOG(GNUNET_ERROR_TYPE_DEBUG,
504           "New quota for peer `%s' with preference (cur/total) %.3f/%.3f (in/out): %llu / %llu\n",
505           GNUNET_i2s (&cur->addr->peer), cur_pref, total_prefs,
506           assigned_quota_in, assigned_quota_out);
507     }
508     else
509     {
510       assigned_quota_in = 0;
511       assigned_quota_out = 0;
512     }
513
514     quota_in_used += assigned_quota_in;
515     quota_out_used += assigned_quota_out;
516     /* Prevent overflow due to rounding errors */
517     if (assigned_quota_in > UINT32_MAX)
518       assigned_quota_in = UINT32_MAX;
519     if (assigned_quota_out > UINT32_MAX)
520       assigned_quota_out = UINT32_MAX;
521
522     /* Compare to current bandwidth assigned */
523     if ((assigned_quota_in != ntohl (cur->addr->assigned_bw_in.value__))
524         || (assigned_quota_out != ntohl (cur->addr->assigned_bw_out.value__)))
525     {
526       cur->addr->assigned_bw_in.value__ = htonl (assigned_quota_in);
527       cur->addr->assigned_bw_out.value__ = htonl (assigned_quota_out);
528       /* Notify on change */
529       if ((GNUNET_YES == cur->addr->active) && (cur->addr != address_except))
530         s->bw_changed (s->bw_changed_cls, cur->addr);
531     }
532
533   }
534   LOG(GNUNET_ERROR_TYPE_DEBUG,
535       "Total bandwidth assigned is (in/out): %llu /%llu\n", quota_in_used,
536       quota_out_used);
537   if (quota_out_used > net->total_quota_out + 1) /* +1 is required due to rounding errors */
538   {
539     LOG(GNUNET_ERROR_TYPE_ERROR,
540         "Total outbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n",
541         net->active_addresses, quota_out_used, net->total_quota_out);
542   }
543   if (quota_in_used > net->total_quota_in + 1) /* +1 is required due to rounding errors */
544   {
545     LOG(GNUNET_ERROR_TYPE_ERROR,
546         "Total inbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n",
547         net->active_addresses, quota_in_used, net->total_quota_in);
548   }
549 }
550
551 struct FindBestAddressCtx
552 {
553   struct GAS_PROPORTIONAL_Handle *s;
554   struct ATS_Address *best;
555 };
556
557 static int
558 find_property_index (uint32_t type)
559 {
560   int existing_types[] = GNUNET_ATS_QualityProperties;
561   int c;
562   for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
563     if (existing_types[c] == type)
564       return c;
565   return GNUNET_SYSERR;
566 }
567
568 /**
569  * Find a "good" address to use for a peer by iterating over the addresses for this peer.
570  * If we already have an existing address, we stick to it.
571  * Otherwise, we pick by lowest distance and then by lowest latency.
572  *
573  * @param cls the 'struct ATS_Address**' where we store the result
574  * @param key unused
575  * @param value another 'struct ATS_Address*' to consider using
576  * @return GNUNET_OK (continue to iterate)
577  */
578 static int
579 find_best_address_it (void *cls, 
580                       const struct GNUNET_PeerIdentity *key,
581                       void *value)
582 {
583   struct FindBestAddressCtx *fba_ctx = (struct FindBestAddressCtx *) cls;
584   struct ATS_Address *current = (struct ATS_Address *) value;
585   struct GNUNET_TIME_Absolute now;
586   struct Network *net = (struct Network *) current->solver_information;
587   const double *norm_prop_cur;
588   const double *norm_prop_prev;
589   int index;
590
591   now = GNUNET_TIME_absolute_get ();
592
593   if (current->blocked_until.abs_value_us
594       == GNUNET_TIME_absolute_max (now, current->blocked_until).abs_value_us)
595   {
596     /* This address is blocked for suggestion */
597     LOG(GNUNET_ERROR_TYPE_DEBUG, "Address %p blocked for suggestion for %s \n",
598         current,
599         GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_difference (now, current->blocked_until), GNUNET_YES));
600     return GNUNET_OK;
601   }
602   if (GNUNET_NO == is_bandwidth_available_in_network (net))
603     return GNUNET_OK; /* There's no bandwidth available in this network */
604   if (NULL != fba_ctx->best)
605   {
606     GNUNET_assert(NULL != fba_ctx->best->plugin);
607     GNUNET_assert(NULL != current->plugin);
608     if (0 == strcmp (fba_ctx->best->plugin, current->plugin))
609     {
610       if ((0 != fba_ctx->best->addr_len) && (0 == current->addr_len))
611       {
612         /* saved address was an outbound address, but we have an inbound address */
613         fba_ctx->best = current;
614         return GNUNET_OK;
615       }
616       if (0 == fba_ctx->best->addr_len)
617       {
618         /* saved address was an inbound address, so do not overwrite */
619         return GNUNET_OK;
620       }
621     }
622   }
623   if (NULL == fba_ctx->best)
624   {
625     fba_ctx->best = current;
626     return GNUNET_OK;
627   }
628   if ((ntohl (fba_ctx->best->assigned_bw_in.value__) == 0)
629       && (ntohl (current->assigned_bw_in.value__) > 0))
630   {
631     /* stick to existing connection */
632     fba_ctx->best = current;
633     return GNUNET_OK;
634   }
635
636   norm_prop_cur = fba_ctx->s->get_properties (fba_ctx->s->get_properties_cls,
637       (const struct ATS_Address *) current);
638   norm_prop_prev = fba_ctx->s->get_properties (fba_ctx->s->get_properties_cls,
639       (const struct ATS_Address *) fba_ctx->best);
640   /*
641    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s previous %.2f current %.2f\n",
642    "DISTANCE", norm_prop_cur[1], norm_prop_cur[1]);
643    */
644   index = find_property_index (GNUNET_ATS_QUALITY_NET_DISTANCE);
645   if (GNUNET_SYSERR == index)
646   {
647     GNUNET_break(0);
648     return GNUNET_OK;
649   }
650   if (norm_prop_cur[index] < norm_prop_prev[index])
651   {
652     /* user shorter distance */
653     fba_ctx->best = current;
654     return GNUNET_OK;
655   }
656   /*
657    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s previous %.2f current %.2f\n",
658    "DELAY", norm_prop_cur[1], norm_prop_cur[1]);
659    */
660   index = find_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
661   if (GNUNET_SYSERR == index)
662   {
663     GNUNET_break(0);
664     return GNUNET_OK;
665   }
666   if (norm_prop_cur[index] < norm_prop_prev[index])
667   {
668     /* user shorter delay */
669     fba_ctx->best = current;
670     return GNUNET_OK;
671   }
672
673   /* don't care */
674   return GNUNET_OK;
675 }
676
677 /**
678  *  Helper functions
679  *  ---------------------------
680  */
681
682 /**
683  * Update bandwidth assignment for all networks
684  *
685  * @param s the solver handle
686  */
687 static void
688 distribute_bandwidth_in_all_networks (struct GAS_PROPORTIONAL_Handle *s)
689 {
690   int i;
691   for (i = 0; i < s->networks; i++)
692     distribute_bandwidth_in_network (s, &s->network_entries[i], NULL );
693 }
694
695 /**
696  * Lookup network struct by type
697  *
698  * @param s the solver handle
699  * @param type the network type
700  * @return the network struct
701  */
702 static struct Network *
703 get_network (struct GAS_PROPORTIONAL_Handle *s, uint32_t type)
704 {
705   int c;
706   for (c = 0; c < s->networks; c++)
707   {
708     if (s->network_entries[c].type == type)
709       return &s->network_entries[c];
710
711   }
712   return NULL ;
713 }
714
715 /**
716  * Hashmap Iterator to find current active address for peer
717  *
718  * @param cls last active address
719  * @param key peer's key
720  * @param value address to check
721  * @return #GNUNET_NO on double active address else #GNUNET_YES;
722  */
723 static int
724 get_active_address_it (void *cls, 
725                        const struct GNUNET_PeerIdentity *key,
726                        void *value)
727 {
728   struct ATS_Address **dest = cls;
729   struct ATS_Address *aa = (struct ATS_Address *) value;
730
731   if (GNUNET_YES == aa->active)
732   {
733
734     if (NULL != (*dest))
735     {
736       /* should never happen */
737       LOG(GNUNET_ERROR_TYPE_ERROR, "Multiple active addresses for peer `%s'\n",
738           GNUNET_i2s (&aa->peer));
739       GNUNET_break(0);
740       return GNUNET_NO;
741     }
742     (*dest) = aa;
743   }
744   return GNUNET_OK;
745 }
746
747 /**
748  * Find current active address for peer
749  *
750  * @param solver the solver handle
751  * @param addresses the address set
752  * @param peer the peer
753  * @return active address or NULL
754  */
755 static struct ATS_Address *
756 get_active_address (void *solver,
757                     const struct GNUNET_CONTAINER_MultiPeerMap * addresses,
758                     const struct GNUNET_PeerIdentity *peer)
759 {
760   struct ATS_Address * dest = NULL;
761
762   GNUNET_CONTAINER_multipeermap_get_multiple (addresses, peer,
763                                               &get_active_address_it, &dest);
764   return dest;
765 }
766
767
768 static void
769 addresse_increment (struct GAS_PROPORTIONAL_Handle *s, struct Network *net,
770     int total, int active)
771 {
772   if (GNUNET_YES == total)
773   {
774     s->total_addresses++;
775     net->total_addresses++;
776     GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", 1, GNUNET_NO);
777     GNUNET_STATISTICS_update (s->stats, net->stat_total, 1, GNUNET_NO);
778   }
779   if (GNUNET_YES == active)
780   {
781     net->active_addresses++;
782     s->active_addresses++;
783     GNUNET_STATISTICS_update (s->stats, "# ATS active addresses total", 1,
784         GNUNET_NO);
785     GNUNET_STATISTICS_update (s->stats, net->stat_active, 1, GNUNET_NO);
786   }
787
788 }
789
790
791 static int
792 addresse_decrement (struct GAS_PROPORTIONAL_Handle *s, struct Network *net,
793     int total, int active)
794 {
795   int res = GNUNET_OK;
796   if (GNUNET_YES == total)
797   {
798     if (s->total_addresses < 1)
799     {
800       GNUNET_break(0);
801       res = GNUNET_SYSERR;
802     }
803     else
804     {
805       s->total_addresses--;
806       GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1,
807           GNUNET_NO);
808     }
809     if (net->total_addresses < 1)
810     {
811       GNUNET_break(0);
812       res = GNUNET_SYSERR;
813     }
814     else
815     {
816       net->total_addresses--;
817       GNUNET_STATISTICS_update (s->stats, net->stat_total, -1, GNUNET_NO);
818     }
819   }
820
821   if (GNUNET_YES == active)
822   {
823     if (net->active_addresses < 1)
824     {
825       GNUNET_break(0);
826       res = GNUNET_SYSERR;
827     }
828     else
829     {
830       net->active_addresses--;
831       GNUNET_STATISTICS_update (s->stats, net->stat_active, -1, GNUNET_NO);
832     }
833     if (s->active_addresses < 1)
834     {
835       GNUNET_break(0);
836       res = GNUNET_SYSERR;
837     }
838     else
839     {
840       s->active_addresses--;
841       GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1,
842           GNUNET_NO);
843     }
844   }
845   return res;
846 }
847
848 /**
849  *  Solver API functions
850  *  ---------------------------
851  */
852
853 /**
854  * Changes the preferences for a peer in the problem
855  *
856  * @param solver the solver handle
857  * @param peer the peer to change the preference for
858  * @param kind the kind to change the preference
859  * @param pref_rel the normalized preference value for this kind over all clients
860  */
861 void
862 GAS_proportional_address_change_preference (void *solver,
863                                             const struct GNUNET_PeerIdentity *peer,
864                                             enum GNUNET_ATS_PreferenceKind kind,
865                                             double pref_rel)
866 {
867   struct GAS_PROPORTIONAL_Handle *s = solver;
868   GNUNET_assert(NULL != solver);
869   GNUNET_assert(NULL != peer);
870
871   distribute_bandwidth_in_all_networks (s);
872 }
873
874
875 /**
876  * Get application feedback for a peer
877  *
878  * @param solver the solver handle
879  * @param application the application
880  * @param peer the peer to change the preference for
881  * @param scope the time interval for this feedback: [now - scope .. now]
882  * @param kind the kind to change the preference
883  * @param score the score
884  */
885 void
886 GAS_proportional_address_preference_feedback (void *solver, void *application,
887     const struct GNUNET_PeerIdentity *peer,
888     const struct GNUNET_TIME_Relative scope,
889     enum GNUNET_ATS_PreferenceKind kind, double score)
890 {
891   struct GAS_PROPORTIONAL_Handle *s = solver;
892   GNUNET_assert(NULL != solver);
893   GNUNET_assert(NULL != peer);
894
895   GNUNET_assert(NULL != s);
896   GNUNET_break(0);
897 }
898
899 /**
900  * Get the preferred address for a specific peer
901  *
902  * @param solver the solver handle
903  * @param peer the identity of the peer
904  */
905 const struct ATS_Address *
906 GAS_proportional_get_preferred_address (void *solver,
907     const struct GNUNET_PeerIdentity *peer)
908 {
909   struct GAS_PROPORTIONAL_Handle *s = solver;
910   struct Network *net_prev;
911   struct Network *net_cur;
912   struct ATS_Address *prev;
913   struct FindBestAddressCtx fba_ctx;
914
915   GNUNET_assert(s != NULL);
916   GNUNET_assert(peer != NULL);
917
918   /* Add to list of pending requests */
919   if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (s->requests,
920                                                            peer))
921   {
922     GNUNET_assert (GNUNET_OK ==
923                    GNUNET_CONTAINER_multipeermap_put (s->requests,
924                                                       peer, NULL,
925                                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
926   }
927
928   /* Get address with: stick to current address, lower distance, lower latency */
929   fba_ctx.s = s;
930   fba_ctx.best = NULL;
931
932   GNUNET_CONTAINER_multipeermap_get_multiple (s->addresses, peer,
933                                               &find_best_address_it, &fba_ctx);
934   if (NULL == fba_ctx.best)
935   {
936     LOG(GNUNET_ERROR_TYPE_INFO, "Cannot suggest address for peer `%s'\n",
937         GNUNET_i2s (peer));
938     return NULL ;
939   }
940
941   LOG(GNUNET_ERROR_TYPE_INFO, "Suggesting %s address %p for peer `%s'\n",
942       (GNUNET_NO == fba_ctx.best->active) ? "inactive" : "active", fba_ctx.best,
943       GNUNET_i2s (peer));
944   net_cur = (struct Network *) fba_ctx.best->solver_information;
945   if (NULL == fba_ctx.best)
946   {
947     LOG(GNUNET_ERROR_TYPE_ERROR,
948         "Trying to suggesting unknown address peer `%s'\n", GNUNET_i2s (peer));
949     GNUNET_break(0);
950     return NULL ;
951   }
952   if (GNUNET_YES == fba_ctx.best->active)
953   {
954     /* This address was selected previously, so no need to update quotas */
955     return fba_ctx.best;
956   }
957
958   /* This address was not active, so we have to:
959    *
960    * - mark previous active address as not active
961    * - update quota for previous address network
962    * - update quota for this address network
963    */
964   prev = get_active_address (s,
965                              s->addresses, peer);
966   if (NULL != prev)
967   {
968     net_prev = (struct Network *) prev->solver_information;
969     prev->active = GNUNET_NO; /* No active any longer */
970     prev->assigned_bw_in = BANDWIDTH_ZERO; /* no bandwidth assigned */
971     prev->assigned_bw_out = BANDWIDTH_ZERO; /* no bandwidth assigned */
972     if (GNUNET_SYSERR == addresse_decrement (s, net_prev, GNUNET_NO, GNUNET_YES))
973       GNUNET_break(0);
974     distribute_bandwidth_in_network (s, net_prev, NULL );
975   }
976
977   if (GNUNET_NO == (is_bandwidth_available_in_network (fba_ctx.best->solver_information)))
978   {
979     GNUNET_break(0); /* This should never happen*/
980     return NULL ;
981   }
982
983   fba_ctx.best->active = GNUNET_YES;
984   addresse_increment (s, net_cur, GNUNET_NO, GNUNET_YES);
985   distribute_bandwidth_in_network (s, net_cur, fba_ctx.best );
986   return fba_ctx.best;
987 }
988
989 /**
990  * Stop notifying about address and bandwidth changes for this peer
991  *
992  * @param solver the solver handle
993  * @param peer the peer
994  */
995 void
996 GAS_proportional_stop_get_preferred_address (void *solver,
997     const struct GNUNET_PeerIdentity *peer)
998 {
999   struct GAS_PROPORTIONAL_Handle *s = solver;
1000   struct ATS_Address *cur;
1001   struct Network *cur_net;
1002
1003   if (GNUNET_YES
1004       == GNUNET_CONTAINER_multipeermap_contains (s->requests,
1005                                                  peer))
1006     GNUNET_CONTAINER_multipeermap_remove (s->requests, peer,
1007                                           NULL);
1008
1009   cur = get_active_address (s,
1010                             s->addresses, peer);
1011   if (NULL != cur)
1012   {
1013     /* Disabling current address */
1014     cur_net = (struct Network *) cur->solver_information;
1015     cur->active = GNUNET_NO; /* No active any longer */
1016     cur->assigned_bw_in = BANDWIDTH_ZERO; /* no bandwidth assigned */
1017     cur->assigned_bw_out = BANDWIDTH_ZERO; /* no bandwidth assigned */
1018     if (GNUNET_SYSERR == addresse_decrement (s, cur_net, GNUNET_NO, GNUNET_YES))
1019       GNUNET_break(0);
1020     distribute_bandwidth_in_network (s, cur_net, NULL );
1021   }
1022   return;
1023 }
1024
1025 /**
1026  * Remove an address from the solver
1027  *
1028  * @param solver the solver handle
1029  * @param address the address to remove
1030  * @param session_only delete only session not whole address
1031  */
1032 void
1033 GAS_proportional_address_delete (void *solver, struct ATS_Address *address,
1034     int session_only)
1035 {
1036   struct GAS_PROPORTIONAL_Handle *s = solver;
1037   struct Network *net;
1038   struct AddressWrapper *aw;
1039   const struct ATS_Address *new_address;
1040
1041
1042   /* Remove an adress completely, we have to:
1043    * - Remove from specific network
1044    * - Decrease number of total addresses
1045    * - If active:
1046    *   - decrease number of active addreses
1047    *   - update quotas
1048    */
1049
1050   net = (struct Network *) address->solver_information;
1051
1052   if (GNUNET_NO == session_only)
1053   {
1054     LOG(GNUNET_ERROR_TYPE_INFO,
1055         "Deleting %s address %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
1056         (GNUNET_NO == address->active) ? "inactive" : "active", address,
1057         GNUNET_i2s (&address->peer), net->desc, net->total_addresses,
1058         net->active_addresses);
1059
1060     /* Remove address */
1061     addresse_decrement (s, net, GNUNET_YES, GNUNET_NO);
1062     for (aw = net->head; NULL != aw; aw = aw->next)
1063     {
1064       if (aw->addr == address)
1065         break;
1066     }
1067     if (NULL == aw)
1068     {
1069       GNUNET_break(0);
1070       return;
1071     }
1072     GNUNET_CONTAINER_DLL_remove(net->head, net->tail, aw);
1073     GNUNET_free(aw);
1074   }
1075   else
1076   {
1077     /* Remove session only: remove if active and update */
1078     LOG(GNUNET_ERROR_TYPE_INFO,
1079         "Deleting %s session %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
1080         (GNUNET_NO == address->active) ? "inactive" : "active", address,
1081         GNUNET_i2s (&address->peer), net->desc, net->total_addresses,
1082         net->active_addresses);
1083   }
1084
1085   if (GNUNET_YES == address->active)
1086   {
1087     /* Address was active, remove from network and update quotas*/
1088     address->active = GNUNET_NO;
1089     address->assigned_bw_in = BANDWIDTH_ZERO;
1090     address->assigned_bw_out = BANDWIDTH_ZERO;
1091
1092     if (GNUNET_SYSERR == addresse_decrement (s, net, GNUNET_NO, GNUNET_YES))
1093       GNUNET_break(0);
1094     distribute_bandwidth_in_network (s, net, NULL );
1095     if (NULL == (new_address = GAS_proportional_get_preferred_address (s, &address->peer)))
1096     {
1097       /* No alternative address found, disconnect peer */
1098       s->bw_changed (s->bw_changed_cls, address);
1099     }
1100     else
1101     {
1102       s->bw_changed (s->bw_changed_cls, (struct ATS_Address *) new_address);
1103     }
1104   }
1105   LOG(GNUNET_ERROR_TYPE_DEBUG,
1106       "After deleting address now total %u and active %u addresses in network `%s'\n",
1107       net->total_addresses, net->active_addresses, net->desc);
1108
1109 }
1110
1111 /**
1112  * Start a bulk operation
1113  *
1114  * @param solver the solver
1115  */
1116 void
1117 GAS_proportional_bulk_start (void *solver)
1118 {
1119   LOG(GNUNET_ERROR_TYPE_DEBUG, "Locking solver for bulk operation ...\n");
1120   struct GAS_PROPORTIONAL_Handle *s = (struct GAS_PROPORTIONAL_Handle *) solver;
1121
1122   GNUNET_assert(NULL != solver);
1123   s->bulk_lock++;
1124 }
1125
1126
1127 /**
1128  * Bulk operation done
1129  */
1130 void
1131 GAS_proportional_bulk_stop (void *solver)
1132 {
1133   LOG(GNUNET_ERROR_TYPE_DEBUG, "Unlocking solver from bulk operation ...\n");
1134
1135   struct GAS_PROPORTIONAL_Handle *s = (struct GAS_PROPORTIONAL_Handle *) solver;
1136   GNUNET_assert(NULL != solver);
1137
1138   if (s->bulk_lock < 1)
1139   {
1140     GNUNET_break(0);
1141     return;
1142   }
1143   s->bulk_lock--;
1144   if ((0 == s->bulk_lock) && (0 < s->bulk_requests))
1145   {
1146     LOG(GNUNET_ERROR_TYPE_DEBUG, "No lock pending, recalculating\n");
1147     distribute_bandwidth_in_all_networks (s);
1148     s->bulk_requests = 0;
1149   }
1150 }
1151
1152
1153 /**
1154  * Add a new single address to a network
1155  *
1156  * @param solver the solver Handle
1157  * @param address the address to add
1158  * @param network network type of this address
1159  */
1160 void
1161 GAS_proportional_address_add (void *solver, struct ATS_Address *address,
1162     uint32_t network);
1163
1164 /**
1165  * Transport properties for this address have changed
1166  *
1167  * @param solver solver handle
1168  * @param address the address
1169  * @param type the ATSI type in HBO
1170  * @param abs_value the absolute value of the property
1171  * @param rel_value the normalized value
1172  */
1173 void
1174 GAS_proportional_address_property_changed (void *solver,
1175     struct ATS_Address *address, uint32_t type, uint32_t abs_value,
1176     double rel_value)
1177 {
1178   struct GAS_PROPORTIONAL_Handle *s;
1179   struct Network *n;
1180
1181   GNUNET_assert(NULL != solver);
1182   GNUNET_assert(NULL != address);
1183
1184   s = (struct GAS_PROPORTIONAL_Handle *) solver;
1185   n = (struct Network *) address->solver_information;
1186
1187   if (NULL == n)
1188   {
1189     GNUNET_break(0);
1190     return;
1191   }
1192
1193   LOG(GNUNET_ERROR_TYPE_DEBUG,
1194       "Property `%s' for peer `%s' address %p changed to %.2f \n",
1195       GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
1196       address, rel_value);
1197   switch (type)
1198   {
1199   case GNUNET_ATS_UTILIZATION_UP:
1200   case GNUNET_ATS_UTILIZATION_DOWN:
1201   case GNUNET_ATS_QUALITY_NET_DELAY:
1202   case GNUNET_ATS_QUALITY_NET_DISTANCE:
1203   case GNUNET_ATS_COST_WAN:
1204   case GNUNET_ATS_COST_LAN:
1205   case GNUNET_ATS_COST_WLAN:
1206     distribute_bandwidth_in_network (s, n, GNUNET_NO);
1207     break;
1208   }
1209 }
1210
1211 /**
1212  * Transport session for this address has changed
1213  *
1214  * NOTE: values in addresses are already updated
1215  *
1216  * @param solver solver handle
1217  * @param address the address
1218  * @param cur_session the current session
1219  * @param new_session the new session
1220  */
1221 void
1222 GAS_proportional_address_session_changed (void *solver,
1223     struct ATS_Address *address, uint32_t cur_session, uint32_t new_session)
1224 {
1225   if (cur_session != new_session)
1226   {
1227     LOG(GNUNET_ERROR_TYPE_DEBUG, "Session changed from %u to %u\n", cur_session,
1228         new_session);
1229   }
1230 }
1231
1232 /**
1233  * Usage for this address has changed
1234  *
1235  * NOTE: values in addresses are already updated
1236  *
1237  * @param solver solver handle
1238  * @param address the address
1239  * @param in_use usage state
1240  */
1241 void
1242 GAS_proportional_address_inuse_changed (void *solver,
1243     struct ATS_Address *address, int in_use)
1244 {
1245   LOG(GNUNET_ERROR_TYPE_DEBUG, "Usage changed to %s\n",
1246       (GNUNET_YES == in_use) ? "USED" : "UNUSED");
1247 }
1248
1249 /**
1250  * Network scope for this address has changed
1251  *
1252  * NOTE: values in addresses are already updated
1253  *
1254  * @param solver solver handle
1255  * @param address the address
1256  * @param current_network the current network
1257  * @param new_network the new network
1258  */
1259 void
1260 GAS_proportional_address_change_network (void *solver,
1261     struct ATS_Address *address, uint32_t current_network, uint32_t new_network)
1262 {
1263   struct ATS_Address *new;
1264   struct GAS_PROPORTIONAL_Handle *s = (struct GAS_PROPORTIONAL_Handle *) solver;
1265   int save_active = GNUNET_NO;
1266   struct Network *new_net = NULL;
1267
1268   if (current_network == new_network)
1269   {
1270     GNUNET_break(0);
1271     return;
1272   }
1273
1274   /* Network changed */
1275   LOG(GNUNET_ERROR_TYPE_DEBUG,
1276       "Network type changed, moving %s address from `%s' to `%s'\n",
1277       (GNUNET_YES == address->active) ? "active" : "inactive",
1278       GNUNET_ATS_print_network_type (current_network),
1279       GNUNET_ATS_print_network_type (new_network));
1280
1281   save_active = address->active;
1282
1283   /* Disable and assign no bandwidth */
1284   address->active = GNUNET_NO;
1285   address->assigned_bw_in = BANDWIDTH_ZERO; /* no bandwidth assigned */
1286   address->assigned_bw_out = BANDWIDTH_ZERO; /* no bandwidth assigned */
1287
1288   /* Remove from old network */
1289   GAS_proportional_address_delete (solver, address, GNUNET_NO);
1290
1291   /* Set new network type */
1292   if (NULL == (new_net = get_network (solver, new_network)))
1293   {
1294     /* Address changed to invalid network... */
1295     LOG(GNUNET_ERROR_TYPE_ERROR,
1296         _("Invalid network type `%u' `%s': Disconnect!\n"), new_network,
1297         GNUNET_ATS_print_network_type (new_network));
1298
1299     /* Find new address to suggest since no bandwidth in network*/
1300     if (NULL == (new = (struct ATS_Address *) GAS_proportional_get_preferred_address (s, &address->peer)))
1301     {
1302       /* No alternative address found, disconnect peer */
1303       s->bw_changed (s->bw_changed_cls, address);
1304     }
1305     return;
1306   }
1307
1308   /* Add to new network and update*/
1309   address->solver_information = new_net;
1310   GAS_proportional_address_add (solver, address, new_network);
1311   if (GNUNET_YES == save_active)
1312   {
1313     /* check if bandwidth available in new network */
1314     if (GNUNET_YES == (is_bandwidth_available_in_network (new_net)))
1315     {
1316       /* Assign bandwidth to updated address */
1317       address->active = GNUNET_YES;
1318       addresse_increment (s, new_net, GNUNET_NO, GNUNET_YES);
1319       distribute_bandwidth_in_network (solver, new_net, NULL );
1320     }
1321     else
1322     {
1323       LOG(GNUNET_ERROR_TYPE_DEBUG,
1324           "Not enough bandwidth in new network, suggesting alternative address ..\n");
1325       /* Find new address to suggest since no bandwidth in network*/
1326       if (NULL
1327           == (new =
1328               (struct ATS_Address *) GAS_proportional_get_preferred_address (s,
1329                   &address->peer)))
1330       {
1331         /* No alternative address found, disconnect peer */
1332         s->bw_changed (s->bw_changed_cls, address);
1333       }
1334     }
1335   }
1336 }
1337
1338 /**
1339  * Add a new single address to a network
1340  *
1341  * @param solver the solver Handle
1342  * @param address the address to add
1343  * @param network network type of this address
1344  */
1345 void
1346 GAS_proportional_address_add (void *solver, struct ATS_Address *address,
1347     uint32_t network)
1348 {
1349   struct GAS_PROPORTIONAL_Handle *s = solver;
1350   struct Network *net = NULL;
1351   struct AddressWrapper *aw = NULL;
1352   const struct ATS_Address *new_address;
1353
1354   GNUNET_assert(NULL != s);
1355
1356
1357   net = get_network (s, network);
1358   if (NULL == net)
1359   {
1360     GNUNET_break(0);
1361     return;
1362   }
1363
1364   aw = GNUNET_malloc (sizeof (struct AddressWrapper));
1365   aw->addr = address;
1366   GNUNET_CONTAINER_DLL_insert(net->head, net->tail, aw);
1367   addresse_increment (s, net, GNUNET_YES, GNUNET_NO);
1368   aw->addr->solver_information = net;
1369
1370   if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (s->requests, &address->peer))
1371   {
1372     if (NULL == get_active_address (s, s->addresses, &address->peer))
1373     {
1374       if (NULL != (new_address = GAS_proportional_get_preferred_address (s, &address->peer)))
1375           s->bw_changed (s->bw_changed_cls, (struct ATS_Address *) address);
1376     }
1377   }
1378   LOG(GNUNET_ERROR_TYPE_DEBUG,
1379       "After adding address now total %u and active %u addresses in network `%s'\n",
1380       net->total_addresses, net->active_addresses, net->desc);
1381 }
1382
1383 /**
1384  * Init the proportional problem solver
1385  *
1386  * Quotas:
1387  * network[i] contains the network type as type GNUNET_ATS_NetworkType[i]
1388  * out_quota[i] contains outbound quota for network type i
1389  * in_quota[i] contains inbound quota for network type i
1390  *
1391  * Example
1392  * network = {GNUNET_ATS_NET_UNSPECIFIED, GNUNET_ATS_NET_LOOPBACK, GNUNET_ATS_NET_LAN, GNUNET_ATS_NET_WAN, GNUNET_ATS_NET_WLAN}
1393  * network[2]   == GNUNET_ATS_NET_LAN
1394  * out_quota[2] == 65353
1395  * in_quota[2]  == 65353
1396  *
1397  * @param cfg configuration handle
1398  * @param stats the GNUNET_STATISTICS handle
1399  * @param addresses hashmap containing all addresses
1400  * @param network array of GNUNET_ATS_NetworkType with length dest_length
1401  * @param out_quota array of outbound quotas
1402  * @param in_quota array of outbound quota
1403  * @param dest_length array length for quota arrays
1404  * @param bw_changed_cb callback for changed bandwidth amounts
1405  * @param bw_changed_cb_cls cls for callback
1406  * @param get_preference callback to get relative preferences for a peer
1407  * @param get_preference_cls cls for callback to get relative preferences
1408  * @param get_properties for callback to get relative properties
1409  * @param get_properties_cls cls for callback to get relative properties
1410  * @return handle for the solver on success, NULL on fail
1411  */
1412 void *
1413 GAS_proportional_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1414                        const struct GNUNET_STATISTICS_Handle *stats,
1415                        const struct GNUNET_CONTAINER_MultiPeerMap *addresses, int *network,
1416                        unsigned long long *out_quota, unsigned long long *in_quota,
1417                        int dest_length, GAS_bandwidth_changed_cb bw_changed_cb,
1418                        void *bw_changed_cb_cls, GAS_get_preferences get_preference,
1419                        void *get_preference_cls, GAS_get_properties get_properties,
1420                        void *get_properties_cls)
1421 {
1422   int c;
1423   struct GAS_PROPORTIONAL_Handle *s =
1424       GNUNET_malloc (sizeof (struct GAS_PROPORTIONAL_Handle));
1425   struct Network * cur;
1426   char * net_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
1427
1428   GNUNET_assert(NULL != cfg);
1429   GNUNET_assert(NULL != stats);
1430   GNUNET_assert(NULL != network);
1431   GNUNET_assert(NULL != bw_changed_cb);
1432   GNUNET_assert(NULL != get_preference);
1433   GNUNET_assert(NULL != get_properties);
1434
1435   s->stats = (struct GNUNET_STATISTICS_Handle *) stats;
1436   s->bw_changed = bw_changed_cb;
1437   s->bw_changed_cls = bw_changed_cb_cls;
1438   s->get_preferences = get_preference;
1439   s->get_preferences_cls = get_preference_cls;
1440   s->get_properties = get_properties;
1441   s->get_properties_cls = get_properties_cls;
1442   s->networks = dest_length;
1443   s->network_entries = GNUNET_malloc (dest_length * sizeof (struct Network));
1444   s->active_addresses = 0;
1445   s->total_addresses = 0;
1446   s->bulk_lock = GNUNET_NO;
1447   s->addresses = addresses;
1448
1449   s->requests = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1450
1451   for (c = 0; c < dest_length; c++)
1452   {
1453     cur = &s->network_entries[c];
1454     cur->total_addresses = 0;
1455     cur->active_addresses = 0;
1456     cur->type = network[c];
1457     cur->total_quota_in = in_quota[c];
1458     cur->total_quota_out = out_quota[c];
1459     cur->desc = net_str[c];
1460     GNUNET_asprintf (&cur->stat_total, "# ATS addresses %s total", cur->desc);
1461     GNUNET_asprintf (&cur->stat_active, "# ATS active addresses %s total",
1462         cur->desc);
1463   }
1464   return s;
1465 }
1466
1467 /**
1468  * Shutdown the proportional problem solver
1469  *
1470  * @param solver the respective handle to shutdown
1471  */
1472 void
1473 GAS_proportional_done (void *solver)
1474 {
1475   struct GAS_PROPORTIONAL_Handle *s = solver;
1476   struct AddressWrapper *cur;
1477   struct AddressWrapper *next;
1478   int c;
1479   GNUNET_assert(s != NULL);
1480
1481   for (c = 0; c < s->networks; c++)
1482   {
1483     if (s->network_entries[c].total_addresses > 0)
1484     {
1485       LOG(GNUNET_ERROR_TYPE_ERROR,
1486           "Had %u addresses for network `%s' not deleted during shutdown\n",
1487           s->network_entries[c].total_addresses, s->network_entries[c].desc);
1488       GNUNET_break(0);
1489     }
1490
1491     if (s->network_entries[c].active_addresses > 0)
1492     {
1493       LOG(GNUNET_ERROR_TYPE_ERROR,
1494           "Had %u active addresses for network `%s' not deleted during shutdown\n",
1495           s->network_entries[c].active_addresses, s->network_entries[c].desc);
1496       GNUNET_break(0);
1497     }
1498
1499     next = s->network_entries[c].head;
1500     while (NULL != (cur = next))
1501     {
1502       next = cur->next;
1503       GNUNET_CONTAINER_DLL_remove(s->network_entries[c].head,
1504           s->network_entries[c].tail, cur);
1505       GNUNET_free(cur);
1506     }
1507     GNUNET_free(s->network_entries[c].stat_total);
1508     GNUNET_free(s->network_entries[c].stat_active);
1509   }
1510   if (s->total_addresses > 0)
1511   {
1512     LOG(GNUNET_ERROR_TYPE_ERROR,
1513         "Had %u addresses not deleted during shutdown\n", s->total_addresses);
1514     GNUNET_break(0);
1515   }
1516   if (s->active_addresses > 0)
1517   {
1518     LOG(GNUNET_ERROR_TYPE_ERROR,
1519         "Had %u active addresses not deleted during shutdown\n",
1520         s->active_addresses);
1521     GNUNET_break (0);
1522   }
1523   GNUNET_free (s->network_entries);
1524   GNUNET_CONTAINER_multipeermap_destroy (s->requests);
1525   GNUNET_free (s);
1526 }
1527
1528 /* end of gnunet-service-ats-solver_proportional.c */